cmap_spec.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. import { CMap, CMapFactory, IdentityCMap } from "../../src/core/cmap.js";
  16. import { CMAP_PARAMS } from "./test_utils.js";
  17. import { DefaultCMapReaderFactory } from "../../src/display/api.js";
  18. import { Name } from "../../src/core/primitives.js";
  19. import { StringStream } from "../../src/core/stream.js";
  20. describe("cmap", function () {
  21. let fetchBuiltInCMap;
  22. beforeAll(function () {
  23. // Allow CMap testing in Node.js, e.g. for Travis.
  24. const CMapReaderFactory = new DefaultCMapReaderFactory({
  25. baseUrl: CMAP_PARAMS.cMapUrl,
  26. isCompressed: CMAP_PARAMS.cMapPacked,
  27. });
  28. fetchBuiltInCMap = function (name) {
  29. return CMapReaderFactory.fetch({
  30. name,
  31. });
  32. };
  33. });
  34. afterAll(function () {
  35. fetchBuiltInCMap = null;
  36. });
  37. it("parses beginbfchar", async function () {
  38. // prettier-ignore
  39. const str = "2 beginbfchar\n" +
  40. "<03> <00>\n" +
  41. "<04> <01>\n" +
  42. "endbfchar\n";
  43. const stream = new StringStream(str);
  44. const cmap = await CMapFactory.create({ encoding: stream });
  45. expect(cmap.lookup(0x03)).toEqual(String.fromCharCode(0x00));
  46. expect(cmap.lookup(0x04)).toEqual(String.fromCharCode(0x01));
  47. expect(cmap.lookup(0x05)).toBeUndefined();
  48. });
  49. it("parses beginbfrange with range", async function () {
  50. // prettier-ignore
  51. const str = "1 beginbfrange\n" +
  52. "<06> <0B> 0\n" +
  53. "endbfrange\n";
  54. const stream = new StringStream(str);
  55. const cmap = await CMapFactory.create({ encoding: stream });
  56. expect(cmap.lookup(0x05)).toBeUndefined();
  57. expect(cmap.lookup(0x06)).toEqual(String.fromCharCode(0x00));
  58. expect(cmap.lookup(0x0b)).toEqual(String.fromCharCode(0x05));
  59. expect(cmap.lookup(0x0c)).toBeUndefined();
  60. });
  61. it("parses beginbfrange with array", async function () {
  62. // prettier-ignore
  63. const str = "1 beginbfrange\n" +
  64. "<0D> <12> [ 0 1 2 3 4 5 ]\n" +
  65. "endbfrange\n";
  66. const stream = new StringStream(str);
  67. const cmap = await CMapFactory.create({ encoding: stream });
  68. expect(cmap.lookup(0x0c)).toBeUndefined();
  69. expect(cmap.lookup(0x0d)).toEqual(0x00);
  70. expect(cmap.lookup(0x12)).toEqual(0x05);
  71. expect(cmap.lookup(0x13)).toBeUndefined();
  72. });
  73. it("parses begincidchar", async function () {
  74. // prettier-ignore
  75. const str = "1 begincidchar\n" +
  76. "<14> 0\n" +
  77. "endcidchar\n";
  78. const stream = new StringStream(str);
  79. const cmap = await CMapFactory.create({ encoding: stream });
  80. expect(cmap.lookup(0x14)).toEqual(0x00);
  81. expect(cmap.lookup(0x15)).toBeUndefined();
  82. });
  83. it("parses begincidrange", async function () {
  84. // prettier-ignore
  85. const str = "1 begincidrange\n" +
  86. "<0016> <001B> 0\n" +
  87. "endcidrange\n";
  88. const stream = new StringStream(str);
  89. const cmap = await CMapFactory.create({ encoding: stream });
  90. expect(cmap.lookup(0x15)).toBeUndefined();
  91. expect(cmap.lookup(0x16)).toEqual(0x00);
  92. expect(cmap.lookup(0x1b)).toEqual(0x05);
  93. expect(cmap.lookup(0x1c)).toBeUndefined();
  94. });
  95. it("decodes codespace ranges", async function () {
  96. // prettier-ignore
  97. const str = "1 begincodespacerange\n" +
  98. "<01> <02>\n" +
  99. "<00000003> <00000004>\n" +
  100. "endcodespacerange\n";
  101. const stream = new StringStream(str);
  102. const cmap = await CMapFactory.create({ encoding: stream });
  103. const c = {};
  104. cmap.readCharCode(String.fromCharCode(1), 0, c);
  105. expect(c.charcode).toEqual(1);
  106. expect(c.length).toEqual(1);
  107. cmap.readCharCode(String.fromCharCode(0, 0, 0, 3), 0, c);
  108. expect(c.charcode).toEqual(3);
  109. expect(c.length).toEqual(4);
  110. });
  111. it("decodes 4 byte codespace ranges", async function () {
  112. // prettier-ignore
  113. const str = "1 begincodespacerange\n" +
  114. "<8EA1A1A1> <8EA1FEFE>\n" +
  115. "endcodespacerange\n";
  116. const stream = new StringStream(str);
  117. const cmap = await CMapFactory.create({ encoding: stream });
  118. const c = {};
  119. cmap.readCharCode(String.fromCharCode(0x8e, 0xa1, 0xa1, 0xa1), 0, c);
  120. expect(c.charcode).toEqual(0x8ea1a1a1);
  121. expect(c.length).toEqual(4);
  122. });
  123. it("read usecmap", async function () {
  124. const str = "/Adobe-Japan1-1 usecmap\n";
  125. const stream = new StringStream(str);
  126. const cmap = await CMapFactory.create({
  127. encoding: stream,
  128. fetchBuiltInCMap,
  129. useCMap: null,
  130. });
  131. expect(cmap instanceof CMap).toEqual(true);
  132. expect(cmap.useCMap).not.toBeNull();
  133. expect(cmap.builtInCMap).toBeFalsy();
  134. expect(cmap.length).toEqual(0x20a7);
  135. expect(cmap.isIdentityCMap).toEqual(false);
  136. });
  137. it("parses cmapname", async function () {
  138. const str = "/CMapName /Identity-H def\n";
  139. const stream = new StringStream(str);
  140. const cmap = await CMapFactory.create({ encoding: stream });
  141. expect(cmap.name).toEqual("Identity-H");
  142. });
  143. it("parses wmode", async function () {
  144. const str = "/WMode 1 def\n";
  145. const stream = new StringStream(str);
  146. const cmap = await CMapFactory.create({ encoding: stream });
  147. expect(cmap.vertical).toEqual(true);
  148. });
  149. it("loads built in cmap", async function () {
  150. const cmap = await CMapFactory.create({
  151. encoding: Name.get("Adobe-Japan1-1"),
  152. fetchBuiltInCMap,
  153. useCMap: null,
  154. });
  155. expect(cmap instanceof CMap).toEqual(true);
  156. expect(cmap.useCMap).toBeNull();
  157. expect(cmap.builtInCMap).toBeTruthy();
  158. expect(cmap.length).toEqual(0x20a7);
  159. expect(cmap.isIdentityCMap).toEqual(false);
  160. });
  161. it("loads built in identity cmap", async function () {
  162. const cmap = await CMapFactory.create({
  163. encoding: Name.get("Identity-H"),
  164. fetchBuiltInCMap,
  165. useCMap: null,
  166. });
  167. expect(cmap instanceof IdentityCMap).toEqual(true);
  168. expect(cmap.vertical).toEqual(false);
  169. expect(cmap.length).toEqual(0x10000);
  170. expect(function () {
  171. return cmap.isIdentityCMap;
  172. }).toThrow(new Error("should not access .isIdentityCMap"));
  173. });
  174. it("attempts to load a non-existent built-in CMap", async function () {
  175. try {
  176. await CMapFactory.create({
  177. encoding: Name.get("null"),
  178. fetchBuiltInCMap,
  179. useCMap: null,
  180. });
  181. // Shouldn't get here.
  182. expect(false).toEqual(true);
  183. } catch (reason) {
  184. expect(reason instanceof Error).toEqual(true);
  185. expect(reason.message).toEqual("Unknown CMap name: null");
  186. }
  187. });
  188. it("attempts to load a built-in CMap without the necessary API parameters", async function () {
  189. function tmpFetchBuiltInCMap(name) {
  190. const CMapReaderFactory = new DefaultCMapReaderFactory({});
  191. return CMapReaderFactory.fetch({ name });
  192. }
  193. try {
  194. await CMapFactory.create({
  195. encoding: Name.get("Adobe-Japan1-1"),
  196. fetchBuiltInCMap: tmpFetchBuiltInCMap,
  197. useCMap: null,
  198. });
  199. // Shouldn't get here.
  200. expect(false).toEqual(true);
  201. } catch (reason) {
  202. expect(reason instanceof Error).toEqual(true);
  203. expect(reason.message).toEqual(
  204. 'The CMap "baseUrl" parameter must be specified, ensure that ' +
  205. 'the "cMapUrl" and "cMapPacked" API parameters are provided.'
  206. );
  207. }
  208. });
  209. it("attempts to load a built-in CMap with inconsistent API parameters", async function () {
  210. function tmpFetchBuiltInCMap(name) {
  211. const CMapReaderFactory = new DefaultCMapReaderFactory({
  212. baseUrl: CMAP_PARAMS.cMapUrl,
  213. isCompressed: false,
  214. });
  215. return CMapReaderFactory.fetch({ name });
  216. }
  217. try {
  218. await CMapFactory.create({
  219. encoding: Name.get("Adobe-Japan1-1"),
  220. fetchBuiltInCMap: tmpFetchBuiltInCMap,
  221. useCMap: null,
  222. });
  223. // Shouldn't get here.
  224. expect(false).toEqual(true);
  225. } catch (reason) {
  226. expect(reason instanceof Error).toEqual(true);
  227. const message = reason.message;
  228. expect(message.startsWith("Unable to load CMap at: ")).toEqual(true);
  229. expect(message.endsWith("/external/bcmaps/Adobe-Japan1-1")).toEqual(true);
  230. }
  231. });
  232. });