cff_parser_spec.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. var _cff_parser = require("../../core/cff_parser.js");
  24. var _fonts = require("../../core/fonts.js");
  25. var _stream = require("../../core/stream.js");
  26. describe("CFFParser", function () {
  27. function createWithNullProto(obj) {
  28. var result = Object.create(null);
  29. for (var i in obj) {
  30. result[i] = obj[i];
  31. }
  32. return result;
  33. }
  34. var privateDictStub = {
  35. getByName(name) {
  36. return 0;
  37. }
  38. };
  39. var fontData, parser, cff;
  40. beforeAll(function (done) {
  41. var exampleFont = "0100040100010101134142434445462b" + "54696d65732d526f6d616e000101011f" + "f81b00f81c02f81d03f819041c6f000d" + "fb3cfb6efa7cfa1605e911b8f1120003" + "01010813183030312e30303754696d65" + "7320526f6d616e54696d657300000002" + "010102030e0e7d99f92a99fb7695f773" + "8b06f79a93fc7c8c077d99f85695f75e" + "9908fb6e8cf87393f7108b09a70adf0b" + "f78e14";
  42. var fontArr = [];
  43. for (var i = 0, ii = exampleFont.length; i < ii; i += 2) {
  44. var hex = exampleFont.substring(i, i + 2);
  45. fontArr.push(parseInt(hex, 16));
  46. }
  47. fontData = new _stream.Stream(fontArr);
  48. done();
  49. });
  50. afterAll(function () {
  51. fontData = null;
  52. });
  53. beforeEach(function (done) {
  54. parser = new _cff_parser.CFFParser(fontData, {}, _fonts.SEAC_ANALYSIS_ENABLED);
  55. cff = parser.parse();
  56. done();
  57. });
  58. afterEach(function (done) {
  59. parser = cff = null;
  60. done();
  61. });
  62. it("parses header", function () {
  63. var header = cff.header;
  64. expect(header.major).toEqual(1);
  65. expect(header.minor).toEqual(0);
  66. expect(header.hdrSize).toEqual(4);
  67. expect(header.offSize).toEqual(1);
  68. });
  69. it("parses name index", function () {
  70. var names = cff.names;
  71. expect(names.length).toEqual(1);
  72. expect(names[0]).toEqual("ABCDEF+Times-Roman");
  73. });
  74. it("parses string index", function () {
  75. var strings = cff.strings;
  76. expect(strings.count).toEqual(3);
  77. expect(strings.get(0)).toEqual(".notdef");
  78. expect(strings.get(391)).toEqual("001.007");
  79. });
  80. it("parses top dict", function () {
  81. var topDict = cff.topDict;
  82. expect(topDict.getByName("version")).toEqual(391);
  83. expect(topDict.getByName("FullName")).toEqual(392);
  84. expect(topDict.getByName("FamilyName")).toEqual(393);
  85. expect(topDict.getByName("Weight")).toEqual(389);
  86. expect(topDict.getByName("UniqueID")).toEqual(28416);
  87. expect(topDict.getByName("FontBBox")).toEqual([-168, -218, 1000, 898]);
  88. expect(topDict.getByName("CharStrings")).toEqual(94);
  89. expect(topDict.getByName("Private")).toEqual([45, 102]);
  90. });
  91. it("refuses to add topDict key with invalid value (bug 1068432)", function () {
  92. var topDict = cff.topDict;
  93. var defaultValue = topDict.getByName("UnderlinePosition");
  94. topDict.setByKey(3075, [NaN]);
  95. expect(topDict.getByName("UnderlinePosition")).toEqual(defaultValue);
  96. });
  97. it("ignores reserved commands in parseDict, and refuses to add privateDict " + "keys with invalid values (bug 1308536)", function () {
  98. var bytes = new Uint8Array([64, 39, 31, 30, 252, 114, 137, 115, 79, 30, 197, 119, 2, 99, 127, 6]);
  99. parser.bytes = bytes;
  100. var topDict = cff.topDict;
  101. topDict.setByName("Private", [bytes.length, 0]);
  102. var parsePrivateDict = function () {
  103. parser.parsePrivateDict(topDict);
  104. };
  105. expect(parsePrivateDict).not.toThrow();
  106. var privateDict = topDict.privateDict;
  107. expect(privateDict.getByName("BlueValues")).toBeNull();
  108. });
  109. it("parses a CharString having cntrmask", function () {
  110. var bytes = new Uint8Array([0, 1, 1, 0, 38, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 1, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 3, 20, 22, 22, 14]);
  111. parser.bytes = bytes;
  112. var charStringsIndex = parser.parseIndex(0).obj;
  113. var charStrings = parser.parseCharStrings({
  114. charStrings: charStringsIndex,
  115. privateDict: privateDictStub
  116. }).charStrings;
  117. expect(charStrings.count).toEqual(1);
  118. expect(charStrings.get(0).length).toEqual(38);
  119. });
  120. it("parses a CharString endchar with 4 args w/seac enabled", function () {
  121. const cffParser = new _cff_parser.CFFParser(fontData, {}, true);
  122. cffParser.parse();
  123. var bytes = new Uint8Array([0, 1, 1, 0, 237, 247, 22, 247, 72, 204, 247, 86, 14]);
  124. cffParser.bytes = bytes;
  125. var charStringsIndex = cffParser.parseIndex(0).obj;
  126. var result = cffParser.parseCharStrings({
  127. charStrings: charStringsIndex,
  128. privateDict: privateDictStub
  129. });
  130. expect(result.charStrings.count).toEqual(1);
  131. expect(result.charStrings.get(0).length).toEqual(1);
  132. expect(result.seacs.length).toEqual(1);
  133. expect(result.seacs[0].length).toEqual(4);
  134. expect(result.seacs[0][0]).toEqual(130);
  135. expect(result.seacs[0][1]).toEqual(180);
  136. expect(result.seacs[0][2]).toEqual(65);
  137. expect(result.seacs[0][3]).toEqual(194);
  138. });
  139. it("parses a CharString endchar with 4 args w/seac disabled", function () {
  140. const cffParser = new _cff_parser.CFFParser(fontData, {}, false);
  141. cffParser.parse();
  142. var bytes = new Uint8Array([0, 1, 1, 0, 237, 247, 22, 247, 72, 204, 247, 86, 14]);
  143. cffParser.bytes = bytes;
  144. var charStringsIndex = cffParser.parseIndex(0).obj;
  145. var result = cffParser.parseCharStrings({
  146. charStrings: charStringsIndex,
  147. privateDict: privateDictStub
  148. });
  149. expect(result.charStrings.count).toEqual(1);
  150. expect(result.charStrings.get(0).length).toEqual(9);
  151. expect(result.seacs.length).toEqual(0);
  152. });
  153. it("parses a CharString endchar no args", function () {
  154. var bytes = new Uint8Array([0, 1, 1, 0, 14]);
  155. parser.bytes = bytes;
  156. var charStringsIndex = parser.parseIndex(0).obj;
  157. var result = parser.parseCharStrings({
  158. charStrings: charStringsIndex,
  159. privateDict: privateDictStub
  160. });
  161. expect(result.charStrings.count).toEqual(1);
  162. expect(result.charStrings.get(0)[0]).toEqual(14);
  163. expect(result.seacs.length).toEqual(0);
  164. });
  165. it("parses predefined charsets", function () {
  166. var charset = parser.parseCharsets(0, 0, null, true);
  167. expect(charset.predefined).toEqual(true);
  168. });
  169. it("parses charset format 0", function () {
  170. var bytes = new Uint8Array([0x00, 0x00, 0x00, 0x00, 0x00, 0x02]);
  171. parser.bytes = bytes;
  172. var charset = parser.parseCharsets(3, 2, new _cff_parser.CFFStrings(), false);
  173. expect(charset.charset[1]).toEqual("exclam");
  174. charset = parser.parseCharsets(3, 2, new _cff_parser.CFFStrings(), true);
  175. expect(charset.charset[1]).toEqual(2);
  176. });
  177. it("parses charset format 1", function () {
  178. var bytes = new Uint8Array([0x00, 0x00, 0x00, 0x01, 0x00, 0x08, 0x01]);
  179. parser.bytes = bytes;
  180. var charset = parser.parseCharsets(3, 2, new _cff_parser.CFFStrings(), false);
  181. expect(charset.charset).toEqual([".notdef", "quoteright", "parenleft"]);
  182. charset = parser.parseCharsets(3, 2, new _cff_parser.CFFStrings(), true);
  183. expect(charset.charset).toEqual([0, 8, 9]);
  184. });
  185. it("parses charset format 2", function () {
  186. var bytes = new Uint8Array([0x00, 0x00, 0x00, 0x02, 0x00, 0x08, 0x00, 0x01]);
  187. parser.bytes = bytes;
  188. var charset = parser.parseCharsets(3, 2, new _cff_parser.CFFStrings(), false);
  189. expect(charset.charset).toEqual([".notdef", "quoteright", "parenleft"]);
  190. charset = parser.parseCharsets(3, 2, new _cff_parser.CFFStrings(), true);
  191. expect(charset.charset).toEqual([0, 8, 9]);
  192. });
  193. it("parses encoding format 0", function () {
  194. var bytes = new Uint8Array([0x00, 0x00, 0x00, 0x01, 0x08]);
  195. parser.bytes = bytes;
  196. var encoding = parser.parseEncoding(2, {}, new _cff_parser.CFFStrings(), null);
  197. expect(encoding.encoding).toEqual(createWithNullProto({
  198. 0x8: 1
  199. }));
  200. });
  201. it("parses encoding format 1", function () {
  202. var bytes = new Uint8Array([0x00, 0x00, 0x01, 0x01, 0x07, 0x01]);
  203. parser.bytes = bytes;
  204. var encoding = parser.parseEncoding(2, {}, new _cff_parser.CFFStrings(), null);
  205. expect(encoding.encoding).toEqual(createWithNullProto({
  206. 0x7: 0x01,
  207. 0x08: 0x02
  208. }));
  209. });
  210. it("parses fdselect format 0", function () {
  211. var bytes = new Uint8Array([0x00, 0x00, 0x01]);
  212. parser.bytes = bytes.slice();
  213. var fdSelect = parser.parseFDSelect(0, 2);
  214. expect(fdSelect.fdSelect).toEqual([0, 1]);
  215. expect(fdSelect.format).toEqual(0);
  216. });
  217. it("parses fdselect format 3", function () {
  218. var bytes = new Uint8Array([0x03, 0x00, 0x02, 0x00, 0x00, 0x09, 0x00, 0x02, 0x0a, 0x00, 0x04]);
  219. parser.bytes = bytes.slice();
  220. var fdSelect = parser.parseFDSelect(0, 4);
  221. expect(fdSelect.fdSelect).toEqual([9, 9, 0xa, 0xa]);
  222. expect(fdSelect.format).toEqual(3);
  223. });
  224. it("parses invalid fdselect format 3 (bug 1146106)", function () {
  225. var bytes = new Uint8Array([0x03, 0x00, 0x02, 0x00, 0x01, 0x09, 0x00, 0x02, 0x0a, 0x00, 0x04]);
  226. parser.bytes = bytes.slice();
  227. var fdSelect = parser.parseFDSelect(0, 4);
  228. expect(fdSelect.fdSelect).toEqual([9, 9, 0xa, 0xa]);
  229. expect(fdSelect.format).toEqual(3);
  230. });
  231. });
  232. describe("CFFCompiler", function () {
  233. function testParser(bytes) {
  234. bytes = new Uint8Array(bytes);
  235. return new _cff_parser.CFFParser({
  236. getBytes: () => {
  237. return bytes;
  238. }
  239. }, {}, _fonts.SEAC_ANALYSIS_ENABLED);
  240. }
  241. it("encodes integers", function () {
  242. var c = new _cff_parser.CFFCompiler();
  243. expect(c.encodeInteger(0)).toEqual([0x8b]);
  244. expect(c.encodeInteger(100)).toEqual([0xef]);
  245. expect(c.encodeInteger(-100)).toEqual([0x27]);
  246. expect(c.encodeInteger(1000)).toEqual([0xfa, 0x7c]);
  247. expect(c.encodeInteger(-1000)).toEqual([0xfe, 0x7c]);
  248. expect(c.encodeInteger(10000)).toEqual([0x1c, 0x27, 0x10]);
  249. expect(c.encodeInteger(-10000)).toEqual([0x1c, 0xd8, 0xf0]);
  250. expect(c.encodeInteger(100000)).toEqual([0x1d, 0x00, 0x01, 0x86, 0xa0]);
  251. expect(c.encodeInteger(-100000)).toEqual([0x1d, 0xff, 0xfe, 0x79, 0x60]);
  252. });
  253. it("encodes floats", function () {
  254. var c = new _cff_parser.CFFCompiler();
  255. expect(c.encodeFloat(-2.25)).toEqual([0x1e, 0xe2, 0xa2, 0x5f]);
  256. expect(c.encodeFloat(5e-11)).toEqual([0x1e, 0x5c, 0x11, 0xff]);
  257. });
  258. it("sanitizes name index", function () {
  259. var c = new _cff_parser.CFFCompiler();
  260. var nameIndexCompiled = c.compileNameIndex(["[a"]);
  261. var parser = testParser(nameIndexCompiled);
  262. var nameIndex = parser.parseIndex(0);
  263. var names = parser.parseNameIndex(nameIndex.obj);
  264. expect(names).toEqual(["_a"]);
  265. var longName = "";
  266. for (var i = 0; i < 129; i++) {
  267. longName += "_";
  268. }
  269. nameIndexCompiled = c.compileNameIndex([longName]);
  270. parser = testParser(nameIndexCompiled);
  271. nameIndex = parser.parseIndex(0);
  272. names = parser.parseNameIndex(nameIndex.obj);
  273. expect(names[0].length).toEqual(127);
  274. });
  275. it("compiles fdselect format 0", function () {
  276. var fdSelect = new _cff_parser.CFFFDSelect(0, [3, 2, 1]);
  277. var c = new _cff_parser.CFFCompiler();
  278. var out = c.compileFDSelect(fdSelect);
  279. expect(out).toEqual([0, 3, 2, 1]);
  280. });
  281. it("compiles fdselect format 3", function () {
  282. var fdSelect = new _cff_parser.CFFFDSelect(3, [0, 0, 1, 1]);
  283. var c = new _cff_parser.CFFCompiler();
  284. var out = c.compileFDSelect(fdSelect);
  285. expect(out).toEqual([3, 0, 2, 0, 0, 0, 0, 2, 1, 0, 4]);
  286. });
  287. it("compiles fdselect format 3, single range", function () {
  288. var fdSelect = new _cff_parser.CFFFDSelect(3, [0, 0]);
  289. var c = new _cff_parser.CFFCompiler();
  290. var out = c.compileFDSelect(fdSelect);
  291. expect(out).toEqual([3, 0, 1, 0, 0, 0, 0, 2]);
  292. });
  293. it("compiles charset of CID font", function () {
  294. var charset = new _cff_parser.CFFCharset();
  295. var c = new _cff_parser.CFFCompiler();
  296. var numGlyphs = 7;
  297. var out = c.compileCharset(charset, numGlyphs, new _cff_parser.CFFStrings(), true);
  298. expect(out).toEqual([2, 0, 0, 0, numGlyphs - 1]);
  299. });
  300. it("compiles charset of non CID font", function () {
  301. var charset = new _cff_parser.CFFCharset(false, 0, ["space", "exclam"]);
  302. var c = new _cff_parser.CFFCompiler();
  303. var numGlyphs = 3;
  304. var out = c.compileCharset(charset, numGlyphs, new _cff_parser.CFFStrings(), false);
  305. expect(out).toEqual([0, 0, 1, 0, 2]);
  306. });
  307. });