cff_parser_spec.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. 'use strict';
  16. var _cff_parser = require('../../core/cff_parser');
  17. var _fonts = require('../../core/fonts');
  18. var _stream = require('../../core/stream');
  19. describe('CFFParser', function () {
  20. function createWithNullProto(obj) {
  21. var result = Object.create(null);
  22. for (var i in obj) {
  23. result[i] = obj[i];
  24. }
  25. return result;
  26. }
  27. var privateDictStub = {
  28. getByName: function getByName(name) {
  29. return 0;
  30. }
  31. };
  32. var fontData, parser, cff;
  33. beforeAll(function (done) {
  34. var exampleFont = '0100040100010101134142434445462b' + '54696d65732d526f6d616e000101011f' + 'f81b00f81c02f81d03f819041c6f000d' + 'fb3cfb6efa7cfa1605e911b8f1120003' + '01010813183030312e30303754696d65' + '7320526f6d616e54696d657300000002' + '010102030e0e7d99f92a99fb7695f773' + '8b06f79a93fc7c8c077d99f85695f75e' + '9908fb6e8cf87393f7108b09a70adf0b' + 'f78e14';
  35. var fontArr = [];
  36. for (var i = 0, ii = exampleFont.length; i < ii; i += 2) {
  37. var hex = exampleFont.substr(i, 2);
  38. fontArr.push(parseInt(hex, 16));
  39. }
  40. fontData = new _stream.Stream(fontArr);
  41. done();
  42. });
  43. afterAll(function () {
  44. fontData = null;
  45. });
  46. beforeEach(function (done) {
  47. parser = new _cff_parser.CFFParser(fontData, {}, _fonts.SEAC_ANALYSIS_ENABLED);
  48. cff = parser.parse();
  49. done();
  50. });
  51. afterEach(function (done) {
  52. parser = cff = null;
  53. done();
  54. });
  55. it('parses header', function () {
  56. var header = cff.header;
  57. expect(header.major).toEqual(1);
  58. expect(header.minor).toEqual(0);
  59. expect(header.hdrSize).toEqual(4);
  60. expect(header.offSize).toEqual(1);
  61. });
  62. it('parses name index', function () {
  63. var names = cff.names;
  64. expect(names.length).toEqual(1);
  65. expect(names[0]).toEqual('ABCDEF+Times-Roman');
  66. });
  67. it('parses string index', function () {
  68. var strings = cff.strings;
  69. expect(strings.count).toEqual(3);
  70. expect(strings.get(0)).toEqual('.notdef');
  71. expect(strings.get(391)).toEqual('001.007');
  72. });
  73. it('parses top dict', function () {
  74. var topDict = cff.topDict;
  75. expect(topDict.getByName('version')).toEqual(391);
  76. expect(topDict.getByName('FullName')).toEqual(392);
  77. expect(topDict.getByName('FamilyName')).toEqual(393);
  78. expect(topDict.getByName('Weight')).toEqual(389);
  79. expect(topDict.getByName('UniqueID')).toEqual(28416);
  80. expect(topDict.getByName('FontBBox')).toEqual([-168, -218, 1000, 898]);
  81. expect(topDict.getByName('CharStrings')).toEqual(94);
  82. expect(topDict.getByName('Private')).toEqual([45, 102]);
  83. });
  84. it('refuses to add topDict key with invalid value (bug 1068432)', function () {
  85. var topDict = cff.topDict;
  86. var defaultValue = topDict.getByName('UnderlinePosition');
  87. topDict.setByKey(3075, [NaN]);
  88. expect(topDict.getByName('UnderlinePosition')).toEqual(defaultValue);
  89. });
  90. it('ignores reserved commands in parseDict, and refuses to add privateDict ' + 'keys with invalid values (bug 1308536)', function () {
  91. var bytes = new Uint8Array([64, 39, 31, 30, 252, 114, 137, 115, 79, 30, 197, 119, 2, 99, 127, 6]);
  92. parser.bytes = bytes;
  93. var topDict = cff.topDict;
  94. topDict.setByName('Private', [bytes.length, 0]);
  95. var parsePrivateDict = function parsePrivateDict() {
  96. parser.parsePrivateDict(topDict);
  97. };
  98. expect(parsePrivateDict).not.toThrow();
  99. var privateDict = topDict.privateDict;
  100. expect(privateDict.getByName('BlueValues')).toBeNull();
  101. });
  102. it('parses a CharString having cntrmask', function () {
  103. 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]);
  104. parser.bytes = bytes;
  105. var charStringsIndex = parser.parseIndex(0).obj;
  106. var charStrings = parser.parseCharStrings({
  107. charStrings: charStringsIndex,
  108. privateDict: privateDictStub
  109. }).charStrings;
  110. expect(charStrings.count).toEqual(1);
  111. expect(charStrings.get(0).length).toEqual(38);
  112. });
  113. it('parses a CharString endchar with 4 args w/seac enabled', function () {
  114. var parser = new _cff_parser.CFFParser(fontData, {}, true);
  115. parser.parse();
  116. var bytes = new Uint8Array([0, 1, 1, 0, 237, 247, 22, 247, 72, 204, 247, 86, 14]);
  117. parser.bytes = bytes;
  118. var charStringsIndex = parser.parseIndex(0).obj;
  119. var result = parser.parseCharStrings({
  120. charStrings: charStringsIndex,
  121. privateDict: privateDictStub
  122. });
  123. expect(result.charStrings.count).toEqual(1);
  124. expect(result.charStrings.get(0).length).toEqual(1);
  125. expect(result.seacs.length).toEqual(1);
  126. expect(result.seacs[0].length).toEqual(4);
  127. expect(result.seacs[0][0]).toEqual(130);
  128. expect(result.seacs[0][1]).toEqual(180);
  129. expect(result.seacs[0][2]).toEqual(65);
  130. expect(result.seacs[0][3]).toEqual(194);
  131. });
  132. it('parses a CharString endchar with 4 args w/seac disabled', function () {
  133. var parser = new _cff_parser.CFFParser(fontData, {}, false);
  134. parser.parse();
  135. var bytes = new Uint8Array([0, 1, 1, 0, 237, 247, 22, 247, 72, 204, 247, 86, 14]);
  136. parser.bytes = bytes;
  137. var charStringsIndex = parser.parseIndex(0).obj;
  138. var result = parser.parseCharStrings({
  139. charStrings: charStringsIndex,
  140. privateDict: privateDictStub
  141. });
  142. expect(result.charStrings.count).toEqual(1);
  143. expect(result.charStrings.get(0).length).toEqual(9);
  144. expect(result.seacs.length).toEqual(0);
  145. });
  146. it('parses a CharString endchar no args', function () {
  147. var bytes = new Uint8Array([0, 1, 1, 0, 14]);
  148. parser.bytes = bytes;
  149. var charStringsIndex = parser.parseIndex(0).obj;
  150. var result = parser.parseCharStrings({
  151. charStrings: charStringsIndex,
  152. privateDict: privateDictStub
  153. });
  154. expect(result.charStrings.count).toEqual(1);
  155. expect(result.charStrings.get(0)[0]).toEqual(14);
  156. expect(result.seacs.length).toEqual(0);
  157. });
  158. it('parses predefined charsets', function () {
  159. var charset = parser.parseCharsets(0, 0, null, true);
  160. expect(charset.predefined).toEqual(true);
  161. });
  162. it('parses charset format 0', function () {
  163. var bytes = new Uint8Array([0x00, 0x00, 0x00, 0x00, 0x00, 0x02]);
  164. parser.bytes = bytes;
  165. var charset = parser.parseCharsets(3, 2, new _cff_parser.CFFStrings(), false);
  166. expect(charset.charset[1]).toEqual('exclam');
  167. charset = parser.parseCharsets(3, 2, new _cff_parser.CFFStrings(), true);
  168. expect(charset.charset[1]).toEqual(2);
  169. });
  170. it('parses charset format 1', function () {
  171. var bytes = new Uint8Array([0x00, 0x00, 0x00, 0x01, 0x00, 0x08, 0x01]);
  172. parser.bytes = bytes;
  173. var charset = parser.parseCharsets(3, 2, new _cff_parser.CFFStrings(), false);
  174. expect(charset.charset).toEqual(['.notdef', 'quoteright', 'parenleft']);
  175. charset = parser.parseCharsets(3, 2, new _cff_parser.CFFStrings(), true);
  176. expect(charset.charset).toEqual(['.notdef', 8, 9]);
  177. });
  178. it('parses charset format 2', function () {
  179. var bytes = new Uint8Array([0x00, 0x00, 0x00, 0x02, 0x00, 0x08, 0x00, 0x01]);
  180. parser.bytes = bytes;
  181. var charset = parser.parseCharsets(3, 2, new _cff_parser.CFFStrings(), false);
  182. expect(charset.charset).toEqual(['.notdef', 'quoteright', 'parenleft']);
  183. charset = parser.parseCharsets(3, 2, new _cff_parser.CFFStrings(), true);
  184. expect(charset.charset).toEqual(['.notdef', 8, 9]);
  185. });
  186. it('parses encoding format 0', function () {
  187. var bytes = new Uint8Array([0x00, 0x00, 0x00, 0x01, 0x08]);
  188. parser.bytes = bytes;
  189. var encoding = parser.parseEncoding(2, {}, new _cff_parser.CFFStrings(), null);
  190. expect(encoding.encoding).toEqual(createWithNullProto({ 0x8: 1 }));
  191. });
  192. it('parses encoding format 1', function () {
  193. var bytes = new Uint8Array([0x00, 0x00, 0x01, 0x01, 0x07, 0x01]);
  194. parser.bytes = bytes;
  195. var encoding = parser.parseEncoding(2, {}, new _cff_parser.CFFStrings(), null);
  196. expect(encoding.encoding).toEqual(createWithNullProto({
  197. 0x7: 0x01,
  198. 0x08: 0x02
  199. }));
  200. });
  201. it('parses fdselect format 0', function () {
  202. var bytes = new Uint8Array([0x00, 0x00, 0x01]);
  203. parser.bytes = bytes.slice();
  204. var fdSelect = parser.parseFDSelect(0, 2);
  205. expect(fdSelect.fdSelect).toEqual([0, 1]);
  206. expect(fdSelect.raw).toEqual(bytes);
  207. });
  208. it('parses fdselect format 3', function () {
  209. var bytes = new Uint8Array([0x03, 0x00, 0x02, 0x00, 0x00, 0x09, 0x00, 0x02, 0x0a, 0x00, 0x04]);
  210. parser.bytes = bytes.slice();
  211. var fdSelect = parser.parseFDSelect(0, 4);
  212. expect(fdSelect.fdSelect).toEqual([9, 9, 0xa, 0xa]);
  213. expect(fdSelect.raw).toEqual(bytes);
  214. });
  215. it('parses invalid fdselect format 3 (bug 1146106)', function () {
  216. var bytes = new Uint8Array([0x03, 0x00, 0x02, 0x00, 0x01, 0x09, 0x00, 0x02, 0x0a, 0x00, 0x04]);
  217. parser.bytes = bytes.slice();
  218. var fdSelect = parser.parseFDSelect(0, 4);
  219. expect(fdSelect.fdSelect).toEqual([9, 9, 0xa, 0xa]);
  220. bytes[3] = bytes[4] = 0x00;
  221. expect(fdSelect.raw).toEqual(bytes);
  222. });
  223. });
  224. describe('CFFCompiler', function () {
  225. function testParser(bytes) {
  226. bytes = new Uint8Array(bytes);
  227. return new _cff_parser.CFFParser({
  228. getBytes: function getBytes() {
  229. return bytes;
  230. }
  231. }, {}, _fonts.SEAC_ANALYSIS_ENABLED);
  232. }
  233. it('encodes integers', function () {
  234. var c = new _cff_parser.CFFCompiler();
  235. expect(c.encodeInteger(0)).toEqual([0x8b]);
  236. expect(c.encodeInteger(100)).toEqual([0xef]);
  237. expect(c.encodeInteger(-100)).toEqual([0x27]);
  238. expect(c.encodeInteger(1000)).toEqual([0xfa, 0x7c]);
  239. expect(c.encodeInteger(-1000)).toEqual([0xfe, 0x7c]);
  240. expect(c.encodeInteger(10000)).toEqual([0x1c, 0x27, 0x10]);
  241. expect(c.encodeInteger(-10000)).toEqual([0x1c, 0xd8, 0xf0]);
  242. expect(c.encodeInteger(100000)).toEqual([0x1d, 0x00, 0x01, 0x86, 0xa0]);
  243. expect(c.encodeInteger(-100000)).toEqual([0x1d, 0xff, 0xfe, 0x79, 0x60]);
  244. });
  245. it('encodes floats', function () {
  246. var c = new _cff_parser.CFFCompiler();
  247. expect(c.encodeFloat(-2.25)).toEqual([0x1e, 0xe2, 0xa2, 0x5f]);
  248. expect(c.encodeFloat(5e-11)).toEqual([0x1e, 0x5c, 0x11, 0xff]);
  249. });
  250. it('sanitizes name index', function () {
  251. var c = new _cff_parser.CFFCompiler();
  252. var nameIndexCompiled = c.compileNameIndex(['[a']);
  253. var parser = testParser(nameIndexCompiled);
  254. var nameIndex = parser.parseIndex(0);
  255. var names = parser.parseNameIndex(nameIndex.obj);
  256. expect(names).toEqual(['_a']);
  257. var longName = '';
  258. for (var i = 0; i < 129; i++) {
  259. longName += '_';
  260. }
  261. nameIndexCompiled = c.compileNameIndex([longName]);
  262. parser = testParser(nameIndexCompiled);
  263. nameIndex = parser.parseIndex(0);
  264. names = parser.parseNameIndex(nameIndex.obj);
  265. expect(names[0].length).toEqual(127);
  266. });
  267. });