cff_parser_spec.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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 {
  16. CFFCharset,
  17. CFFCompiler,
  18. CFFFDSelect,
  19. CFFParser,
  20. CFFStrings,
  21. } from "../../src/core/cff_parser.js";
  22. import { SEAC_ANALYSIS_ENABLED } from "../../src/core/fonts_utils.js";
  23. import { Stream } from "../../src/core/stream.js";
  24. describe("CFFParser", function () {
  25. function createWithNullProto(obj) {
  26. const result = Object.create(null);
  27. for (const i in obj) {
  28. result[i] = obj[i];
  29. }
  30. return result;
  31. }
  32. // Stub that returns `0` for any privateDict key.
  33. const privateDictStub = {
  34. getByName(name) {
  35. return 0;
  36. },
  37. };
  38. let fontData, parser, cff;
  39. beforeAll(function () {
  40. // This example font comes from the CFF spec:
  41. // http://www.adobe.com/content/dam/Adobe/en/devnet/font/pdfs/5176.CFF.pdf
  42. const exampleFont =
  43. "0100040100010101134142434445462b" +
  44. "54696d65732d526f6d616e000101011f" +
  45. "f81b00f81c02f81d03f819041c6f000d" +
  46. "fb3cfb6efa7cfa1605e911b8f1120003" +
  47. "01010813183030312e30303754696d65" +
  48. "7320526f6d616e54696d657300000002" +
  49. "010102030e0e7d99f92a99fb7695f773" +
  50. "8b06f79a93fc7c8c077d99f85695f75e" +
  51. "9908fb6e8cf87393f7108b09a70adf0b" +
  52. "f78e14";
  53. const fontArr = [];
  54. for (let i = 0, ii = exampleFont.length; i < ii; i += 2) {
  55. const hex = exampleFont.substring(i, i + 2);
  56. fontArr.push(parseInt(hex, 16));
  57. }
  58. fontData = new Stream(fontArr);
  59. });
  60. afterAll(function () {
  61. fontData = null;
  62. });
  63. beforeEach(function () {
  64. parser = new CFFParser(fontData, {}, SEAC_ANALYSIS_ENABLED);
  65. cff = parser.parse();
  66. });
  67. afterEach(function () {
  68. parser = cff = null;
  69. });
  70. it("parses header", function () {
  71. const header = cff.header;
  72. expect(header.major).toEqual(1);
  73. expect(header.minor).toEqual(0);
  74. expect(header.hdrSize).toEqual(4);
  75. expect(header.offSize).toEqual(1);
  76. });
  77. it("parses name index", function () {
  78. const names = cff.names;
  79. expect(names.length).toEqual(1);
  80. expect(names[0]).toEqual("ABCDEF+Times-Roman");
  81. });
  82. it("parses string index", function () {
  83. const strings = cff.strings;
  84. expect(strings.count).toEqual(3);
  85. expect(strings.get(0)).toEqual(".notdef");
  86. expect(strings.get(391)).toEqual("001.007");
  87. });
  88. it("parses top dict", function () {
  89. const topDict = cff.topDict;
  90. // 391 version 392 FullName 393 FamilyName 389 Weight 28416 UniqueID
  91. // -168 -218 1000 898 FontBBox 94 CharStrings 45 102 Private
  92. expect(topDict.getByName("version")).toEqual(391);
  93. expect(topDict.getByName("FullName")).toEqual(392);
  94. expect(topDict.getByName("FamilyName")).toEqual(393);
  95. expect(topDict.getByName("Weight")).toEqual(389);
  96. expect(topDict.getByName("UniqueID")).toEqual(28416);
  97. expect(topDict.getByName("FontBBox")).toEqual([-168, -218, 1000, 898]);
  98. expect(topDict.getByName("CharStrings")).toEqual(94);
  99. expect(topDict.getByName("Private")).toEqual([45, 102]);
  100. });
  101. it("refuses to add topDict key with invalid value (bug 1068432)", function () {
  102. const topDict = cff.topDict;
  103. const defaultValue = topDict.getByName("UnderlinePosition");
  104. topDict.setByKey(/* [12, 3] = */ 3075, [NaN]);
  105. expect(topDict.getByName("UnderlinePosition")).toEqual(defaultValue);
  106. });
  107. it(
  108. "ignores reserved commands in parseDict, and refuses to add privateDict " +
  109. "keys with invalid values (bug 1308536)",
  110. function () {
  111. const bytes = new Uint8Array([
  112. 64, 39, 31, 30, 252, 114, 137, 115, 79, 30, 197, 119, 2, 99, 127, 6,
  113. ]);
  114. parser.bytes = bytes;
  115. const topDict = cff.topDict;
  116. topDict.setByName("Private", [bytes.length, 0]);
  117. const parsePrivateDict = function () {
  118. parser.parsePrivateDict(topDict);
  119. };
  120. expect(parsePrivateDict).not.toThrow();
  121. const privateDict = topDict.privateDict;
  122. expect(privateDict.getByName("BlueValues")).toBeNull();
  123. }
  124. );
  125. it("parses a CharString having cntrmask", function () {
  126. // prettier-ignore
  127. const bytes = new Uint8Array([0, 1, // count
  128. 1, // offsetSize
  129. 0, // offset[0]
  130. 38, // end
  131. 149, 149, 149, 149, 149, 149, 149, 149,
  132. 149, 149, 149, 149, 149, 149, 149, 149,
  133. 1, // hstem
  134. 149, 149, 149, 149, 149, 149, 149, 149,
  135. 149, 149, 149, 149, 149, 149, 149, 149,
  136. 3, // vstem
  137. 20, // cntrmask
  138. 22, 22, // fail if misparsed as hmoveto
  139. 14 // endchar
  140. ]);
  141. parser.bytes = bytes;
  142. const charStringsIndex = parser.parseIndex(0).obj;
  143. const charStrings = parser.parseCharStrings({
  144. charStrings: charStringsIndex,
  145. privateDict: privateDictStub,
  146. }).charStrings;
  147. expect(charStrings.count).toEqual(1);
  148. // shouldn't be sanitized
  149. expect(charStrings.get(0).length).toEqual(38);
  150. });
  151. it("parses a CharString endchar with 4 args w/seac enabled", function () {
  152. const cffParser = new CFFParser(
  153. fontData,
  154. {},
  155. /* seacAnalysisEnabled = */ true
  156. );
  157. cffParser.parse(); // cff
  158. // prettier-ignore
  159. const bytes = new Uint8Array([0, 1, // count
  160. 1, // offsetSize
  161. 0, // offset[0]
  162. 237, 247, 22, 247, 72, 204, 247, 86, 14]);
  163. cffParser.bytes = bytes;
  164. const charStringsIndex = cffParser.parseIndex(0).obj;
  165. const result = cffParser.parseCharStrings({
  166. charStrings: charStringsIndex,
  167. privateDict: privateDictStub,
  168. });
  169. expect(result.charStrings.count).toEqual(1);
  170. expect(result.charStrings.get(0).length).toEqual(1);
  171. expect(result.seacs.length).toEqual(1);
  172. expect(result.seacs[0].length).toEqual(4);
  173. expect(result.seacs[0][0]).toEqual(130);
  174. expect(result.seacs[0][1]).toEqual(180);
  175. expect(result.seacs[0][2]).toEqual(65);
  176. expect(result.seacs[0][3]).toEqual(194);
  177. });
  178. it("parses a CharString endchar with 4 args w/seac disabled", function () {
  179. const cffParser = new CFFParser(
  180. fontData,
  181. {},
  182. /* seacAnalysisEnabled = */ false
  183. );
  184. cffParser.parse(); // cff
  185. // prettier-ignore
  186. const bytes = new Uint8Array([0, 1, // count
  187. 1, // offsetSize
  188. 0, // offset[0]
  189. 237, 247, 22, 247, 72, 204, 247, 86, 14]);
  190. cffParser.bytes = bytes;
  191. const charStringsIndex = cffParser.parseIndex(0).obj;
  192. const result = cffParser.parseCharStrings({
  193. charStrings: charStringsIndex,
  194. privateDict: privateDictStub,
  195. });
  196. expect(result.charStrings.count).toEqual(1);
  197. expect(result.charStrings.get(0).length).toEqual(9);
  198. expect(result.seacs.length).toEqual(0);
  199. });
  200. it("parses a CharString endchar no args", function () {
  201. // prettier-ignore
  202. const bytes = new Uint8Array([0, 1, // count
  203. 1, // offsetSize
  204. 0, // offset[0]
  205. 14]);
  206. parser.bytes = bytes;
  207. const charStringsIndex = parser.parseIndex(0).obj;
  208. const result = parser.parseCharStrings({
  209. charStrings: charStringsIndex,
  210. privateDict: privateDictStub,
  211. });
  212. expect(result.charStrings.count).toEqual(1);
  213. expect(result.charStrings.get(0)[0]).toEqual(14);
  214. expect(result.seacs.length).toEqual(0);
  215. });
  216. it("parses predefined charsets", function () {
  217. const charset = parser.parseCharsets(0, 0, null, true);
  218. expect(charset.predefined).toEqual(true);
  219. });
  220. it("parses charset format 0", function () {
  221. // The first three bytes make the offset large enough to skip predefined.
  222. // prettier-ignore
  223. const bytes = new Uint8Array([0x00, 0x00, 0x00,
  224. 0x00, // format
  225. 0x00, 0x02 // sid/cid
  226. ]);
  227. parser.bytes = bytes;
  228. let charset = parser.parseCharsets(3, 2, new CFFStrings(), false);
  229. expect(charset.charset[1]).toEqual("exclam");
  230. // CID font
  231. charset = parser.parseCharsets(3, 2, new CFFStrings(), true);
  232. expect(charset.charset[1]).toEqual(2);
  233. });
  234. it("parses charset format 1", function () {
  235. // The first three bytes make the offset large enough to skip predefined.
  236. // prettier-ignore
  237. const bytes = new Uint8Array([0x00, 0x00, 0x00,
  238. 0x01, // format
  239. 0x00, 0x08, // sid/cid start
  240. 0x01 // sid/cid left
  241. ]);
  242. parser.bytes = bytes;
  243. let charset = parser.parseCharsets(3, 2, new CFFStrings(), false);
  244. expect(charset.charset).toEqual([".notdef", "quoteright", "parenleft"]);
  245. // CID font
  246. charset = parser.parseCharsets(3, 2, new CFFStrings(), true);
  247. expect(charset.charset).toEqual([0, 8, 9]);
  248. });
  249. it("parses charset format 2", function () {
  250. // format 2 is the same as format 1 but the left is card16
  251. // The first three bytes make the offset large enough to skip predefined.
  252. // prettier-ignore
  253. const bytes = new Uint8Array([0x00, 0x00, 0x00,
  254. 0x02, // format
  255. 0x00, 0x08, // sid/cid start
  256. 0x00, 0x01 // sid/cid left
  257. ]);
  258. parser.bytes = bytes;
  259. let charset = parser.parseCharsets(3, 2, new CFFStrings(), false);
  260. expect(charset.charset).toEqual([".notdef", "quoteright", "parenleft"]);
  261. // CID font
  262. charset = parser.parseCharsets(3, 2, new CFFStrings(), true);
  263. expect(charset.charset).toEqual([0, 8, 9]);
  264. });
  265. it("parses encoding format 0", function () {
  266. // The first two bytes make the offset large enough to skip predefined.
  267. // prettier-ignore
  268. const bytes = new Uint8Array([0x00, 0x00,
  269. 0x00, // format
  270. 0x01, // count
  271. 0x08 // start
  272. ]);
  273. parser.bytes = bytes;
  274. const encoding = parser.parseEncoding(2, {}, new CFFStrings(), null);
  275. expect(encoding.encoding).toEqual(createWithNullProto({ 0x8: 1 }));
  276. });
  277. it("parses encoding format 1", function () {
  278. // The first two bytes make the offset large enough to skip predefined.
  279. // prettier-ignore
  280. const bytes = new Uint8Array([0x00, 0x00,
  281. 0x01, // format
  282. 0x01, // num ranges
  283. 0x07, // range1 start
  284. 0x01 // range2 left
  285. ]);
  286. parser.bytes = bytes;
  287. const encoding = parser.parseEncoding(2, {}, new CFFStrings(), null);
  288. expect(encoding.encoding).toEqual(
  289. createWithNullProto({ 0x7: 0x01, 0x08: 0x02 })
  290. );
  291. });
  292. it("parses fdselect format 0", function () {
  293. // prettier-ignore
  294. const bytes = new Uint8Array([0x00, // format
  295. 0x00, // gid: 0 fd: 0
  296. 0x01 // gid: 1 fd: 1
  297. ]);
  298. parser.bytes = bytes.slice();
  299. const fdSelect = parser.parseFDSelect(0, 2);
  300. expect(fdSelect.fdSelect).toEqual([0, 1]);
  301. expect(fdSelect.format).toEqual(0);
  302. });
  303. it("parses fdselect format 3", function () {
  304. // prettier-ignore
  305. const bytes = new Uint8Array([0x03, // format
  306. 0x00, 0x02, // range count
  307. 0x00, 0x00, // first gid
  308. 0x09, // font dict 1 id
  309. 0x00, 0x02, // next gid
  310. 0x0a, // font dict 2 id
  311. 0x00, 0x04 // sentinel (last gid)
  312. ]);
  313. parser.bytes = bytes.slice();
  314. const fdSelect = parser.parseFDSelect(0, 4);
  315. expect(fdSelect.fdSelect).toEqual([9, 9, 0xa, 0xa]);
  316. expect(fdSelect.format).toEqual(3);
  317. });
  318. it("parses invalid fdselect format 3 (bug 1146106)", function () {
  319. // prettier-ignore
  320. const bytes = new Uint8Array([0x03, // format
  321. 0x00, 0x02, // range count
  322. 0x00, 0x01, // first gid (invalid)
  323. 0x09, // font dict 1 id
  324. 0x00, 0x02, // next gid
  325. 0x0a, // font dict 2 id
  326. 0x00, 0x04 // sentinel (last gid)
  327. ]);
  328. parser.bytes = bytes.slice();
  329. const fdSelect = parser.parseFDSelect(0, 4);
  330. expect(fdSelect.fdSelect).toEqual([9, 9, 0xa, 0xa]);
  331. expect(fdSelect.format).toEqual(3);
  332. });
  333. // TODO fdArray
  334. });
  335. describe("CFFCompiler", function () {
  336. function testParser(bytes) {
  337. bytes = new Uint8Array(bytes);
  338. return new CFFParser(
  339. {
  340. getBytes: () => {
  341. return bytes;
  342. },
  343. },
  344. {},
  345. SEAC_ANALYSIS_ENABLED
  346. );
  347. }
  348. it("encodes integers", function () {
  349. const c = new CFFCompiler();
  350. // all the examples from the spec
  351. expect(c.encodeInteger(0)).toEqual([0x8b]);
  352. expect(c.encodeInteger(100)).toEqual([0xef]);
  353. expect(c.encodeInteger(-100)).toEqual([0x27]);
  354. expect(c.encodeInteger(1000)).toEqual([0xfa, 0x7c]);
  355. expect(c.encodeInteger(-1000)).toEqual([0xfe, 0x7c]);
  356. expect(c.encodeInteger(10000)).toEqual([0x1c, 0x27, 0x10]);
  357. expect(c.encodeInteger(-10000)).toEqual([0x1c, 0xd8, 0xf0]);
  358. expect(c.encodeInteger(100000)).toEqual([0x1d, 0x00, 0x01, 0x86, 0xa0]);
  359. expect(c.encodeInteger(-100000)).toEqual([0x1d, 0xff, 0xfe, 0x79, 0x60]);
  360. });
  361. it("encodes floats", function () {
  362. const c = new CFFCompiler();
  363. expect(c.encodeFloat(-2.25)).toEqual([0x1e, 0xe2, 0xa2, 0x5f]);
  364. expect(c.encodeFloat(5e-11)).toEqual([0x1e, 0x5c, 0x11, 0xff]);
  365. });
  366. it("sanitizes name index", function () {
  367. const c = new CFFCompiler();
  368. let nameIndexCompiled = c.compileNameIndex(["[a"]);
  369. let parser = testParser(nameIndexCompiled);
  370. let nameIndex = parser.parseIndex(0);
  371. let names = parser.parseNameIndex(nameIndex.obj);
  372. expect(names).toEqual(["_a"]);
  373. let longName = "";
  374. for (let i = 0; i < 129; i++) {
  375. longName += "_";
  376. }
  377. nameIndexCompiled = c.compileNameIndex([longName]);
  378. parser = testParser(nameIndexCompiled);
  379. nameIndex = parser.parseIndex(0);
  380. names = parser.parseNameIndex(nameIndex.obj);
  381. expect(names[0].length).toEqual(127);
  382. });
  383. it("compiles fdselect format 0", function () {
  384. const fdSelect = new CFFFDSelect(0, [3, 2, 1]);
  385. const c = new CFFCompiler();
  386. const out = c.compileFDSelect(fdSelect);
  387. expect(out).toEqual([
  388. 0, // format
  389. 3, // gid: 0 fd 3
  390. 2, // gid: 1 fd 3
  391. 1, // gid: 2 fd 3
  392. ]);
  393. });
  394. it("compiles fdselect format 3", function () {
  395. const fdSelect = new CFFFDSelect(3, [0, 0, 1, 1]);
  396. const c = new CFFCompiler();
  397. const out = c.compileFDSelect(fdSelect);
  398. expect(out).toEqual([
  399. 3, // format
  400. 0, // nRanges (high)
  401. 2, // nRanges (low)
  402. 0, // range struct 0 - first (high)
  403. 0, // range struct 0 - first (low)
  404. 0, // range struct 0 - fd
  405. 0, // range struct 0 - first (high)
  406. 2, // range struct 0 - first (low)
  407. 1, // range struct 0 - fd
  408. 0, // sentinel (high)
  409. 4, // sentinel (low)
  410. ]);
  411. });
  412. it("compiles fdselect format 3, single range", function () {
  413. const fdSelect = new CFFFDSelect(3, [0, 0]);
  414. const c = new CFFCompiler();
  415. const out = c.compileFDSelect(fdSelect);
  416. expect(out).toEqual([
  417. 3, // format
  418. 0, // nRanges (high)
  419. 1, // nRanges (low)
  420. 0, // range struct 0 - first (high)
  421. 0, // range struct 0 - first (low)
  422. 0, // range struct 0 - fd
  423. 0, // sentinel (high)
  424. 2, // sentinel (low)
  425. ]);
  426. });
  427. it("compiles charset of CID font", function () {
  428. const charset = new CFFCharset();
  429. const c = new CFFCompiler();
  430. const numGlyphs = 7;
  431. const out = c.compileCharset(charset, numGlyphs, new CFFStrings(), true);
  432. // All CID charsets get turned into a simple format 2.
  433. expect(out).toEqual([
  434. 2, // format
  435. 0, // cid (high)
  436. 0, // cid (low)
  437. 0, // nLeft (high)
  438. numGlyphs - 1, // nLeft (low)
  439. ]);
  440. });
  441. it("compiles charset of non CID font", function () {
  442. const charset = new CFFCharset(false, 0, ["space", "exclam"]);
  443. const c = new CFFCompiler();
  444. const numGlyphs = 3;
  445. const out = c.compileCharset(charset, numGlyphs, new CFFStrings(), false);
  446. // All non-CID fonts use a format 0 charset.
  447. expect(out).toEqual([
  448. 0, // format
  449. 0, // sid of 'space' (high)
  450. 1, // sid of 'space' (low)
  451. 0, // sid of 'exclam' (high)
  452. 2, // sid of 'exclam' (low)
  453. ]);
  454. });
  455. // TODO a lot more compiler tests
  456. });