2
0

cff_parser_spec.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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('sanitizes name index', function () {
  68. var index = new _cff_parser.CFFIndex();
  69. index.add(['['.charCodeAt(0), 'a'.charCodeAt(0)]);
  70. var names = parser.parseNameIndex(index);
  71. expect(names).toEqual(['_a']);
  72. index = new _cff_parser.CFFIndex();
  73. var longName = [];
  74. for (var i = 0; i < 129; i++) {
  75. longName.push(0);
  76. }
  77. index.add(longName);
  78. names = parser.parseNameIndex(index);
  79. expect(names[0].length).toEqual(127);
  80. });
  81. it('parses string index', function () {
  82. var strings = cff.strings;
  83. expect(strings.count).toEqual(3);
  84. expect(strings.get(0)).toEqual('.notdef');
  85. expect(strings.get(391)).toEqual('001.007');
  86. });
  87. it('parses top dict', function () {
  88. var topDict = cff.topDict;
  89. expect(topDict.getByName('version')).toEqual(391);
  90. expect(topDict.getByName('FullName')).toEqual(392);
  91. expect(topDict.getByName('FamilyName')).toEqual(393);
  92. expect(topDict.getByName('Weight')).toEqual(389);
  93. expect(topDict.getByName('UniqueID')).toEqual(28416);
  94. expect(topDict.getByName('FontBBox')).toEqual([-168, -218, 1000, 898]);
  95. expect(topDict.getByName('CharStrings')).toEqual(94);
  96. expect(topDict.getByName('Private')).toEqual([45, 102]);
  97. });
  98. it('refuses to add topDict key with invalid value (bug 1068432)', function () {
  99. var topDict = cff.topDict;
  100. var defaultValue = topDict.getByName('UnderlinePosition');
  101. topDict.setByKey(3075, [NaN]);
  102. expect(topDict.getByName('UnderlinePosition')).toEqual(defaultValue);
  103. });
  104. it('ignores reserved commands in parseDict, and refuses to add privateDict ' + 'keys with invalid values (bug 1308536)', function () {
  105. var bytes = new Uint8Array([64, 39, 31, 30, 252, 114, 137, 115, 79, 30, 197, 119, 2, 99, 127, 6]);
  106. parser.bytes = bytes;
  107. var topDict = cff.topDict;
  108. topDict.setByName('Private', [bytes.length, 0]);
  109. var parsePrivateDict = function parsePrivateDict() {
  110. parser.parsePrivateDict(topDict);
  111. };
  112. expect(parsePrivateDict).not.toThrow();
  113. var privateDict = topDict.privateDict;
  114. expect(privateDict.getByName('BlueValues')).toBeNull();
  115. });
  116. it('parses a CharString having cntrmask', function () {
  117. 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]);
  118. parser.bytes = bytes;
  119. var charStringsIndex = parser.parseIndex(0).obj;
  120. var charStrings = parser.parseCharStrings({
  121. charStrings: charStringsIndex,
  122. privateDict: privateDictStub
  123. }).charStrings;
  124. expect(charStrings.count).toEqual(1);
  125. expect(charStrings.get(0).length).toEqual(38);
  126. });
  127. it('parses a CharString endchar with 4 args w/seac enabled', function () {
  128. var parser = new _cff_parser.CFFParser(fontData, {}, true);
  129. parser.parse();
  130. var bytes = new Uint8Array([0, 1, 1, 0, 237, 247, 22, 247, 72, 204, 247, 86, 14]);
  131. parser.bytes = bytes;
  132. var charStringsIndex = parser.parseIndex(0).obj;
  133. var result = parser.parseCharStrings({
  134. charStrings: charStringsIndex,
  135. privateDict: privateDictStub
  136. });
  137. expect(result.charStrings.count).toEqual(1);
  138. expect(result.charStrings.get(0).length).toEqual(1);
  139. expect(result.seacs.length).toEqual(1);
  140. expect(result.seacs[0].length).toEqual(4);
  141. expect(result.seacs[0][0]).toEqual(130);
  142. expect(result.seacs[0][1]).toEqual(180);
  143. expect(result.seacs[0][2]).toEqual(65);
  144. expect(result.seacs[0][3]).toEqual(194);
  145. });
  146. it('parses a CharString endchar with 4 args w/seac disabled', function () {
  147. var parser = new _cff_parser.CFFParser(fontData, {}, false);
  148. parser.parse();
  149. var bytes = new Uint8Array([0, 1, 1, 0, 237, 247, 22, 247, 72, 204, 247, 86, 14]);
  150. parser.bytes = bytes;
  151. var charStringsIndex = parser.parseIndex(0).obj;
  152. var result = parser.parseCharStrings({
  153. charStrings: charStringsIndex,
  154. privateDict: privateDictStub
  155. });
  156. expect(result.charStrings.count).toEqual(1);
  157. expect(result.charStrings.get(0).length).toEqual(9);
  158. expect(result.seacs.length).toEqual(0);
  159. });
  160. it('parses a CharString endchar no args', function () {
  161. var bytes = new Uint8Array([0, 1, 1, 0, 14]);
  162. parser.bytes = bytes;
  163. var charStringsIndex = parser.parseIndex(0).obj;
  164. var result = parser.parseCharStrings({
  165. charStrings: charStringsIndex,
  166. privateDict: privateDictStub
  167. });
  168. expect(result.charStrings.count).toEqual(1);
  169. expect(result.charStrings.get(0)[0]).toEqual(14);
  170. expect(result.seacs.length).toEqual(0);
  171. });
  172. it('parses predefined charsets', function () {
  173. var charset = parser.parseCharsets(0, 0, null, true);
  174. expect(charset.predefined).toEqual(true);
  175. });
  176. it('parses charset format 0', function () {
  177. var bytes = new Uint8Array([0x00, 0x00, 0x00, 0x00, 0x00, 0x02]);
  178. parser.bytes = bytes;
  179. var charset = parser.parseCharsets(3, 2, new _cff_parser.CFFStrings(), false);
  180. expect(charset.charset[1]).toEqual('exclam');
  181. charset = parser.parseCharsets(3, 2, new _cff_parser.CFFStrings(), true);
  182. expect(charset.charset[1]).toEqual(2);
  183. });
  184. it('parses charset format 1', function () {
  185. var bytes = new Uint8Array([0x00, 0x00, 0x00, 0x01, 0x00, 0x08, 0x01]);
  186. parser.bytes = bytes;
  187. var charset = parser.parseCharsets(3, 2, new _cff_parser.CFFStrings(), false);
  188. expect(charset.charset).toEqual(['.notdef', 'quoteright', 'parenleft']);
  189. charset = parser.parseCharsets(3, 2, new _cff_parser.CFFStrings(), true);
  190. expect(charset.charset).toEqual(['.notdef', 8, 9]);
  191. });
  192. it('parses charset format 2', function () {
  193. var bytes = new Uint8Array([0x00, 0x00, 0x00, 0x02, 0x00, 0x08, 0x00, 0x01]);
  194. parser.bytes = bytes;
  195. var charset = parser.parseCharsets(3, 2, new _cff_parser.CFFStrings(), false);
  196. expect(charset.charset).toEqual(['.notdef', 'quoteright', 'parenleft']);
  197. charset = parser.parseCharsets(3, 2, new _cff_parser.CFFStrings(), true);
  198. expect(charset.charset).toEqual(['.notdef', 8, 9]);
  199. });
  200. it('parses encoding format 0', function () {
  201. var bytes = new Uint8Array([0x00, 0x00, 0x00, 0x01, 0x08]);
  202. parser.bytes = bytes;
  203. var encoding = parser.parseEncoding(2, {}, new _cff_parser.CFFStrings(), null);
  204. expect(encoding.encoding).toEqual(createWithNullProto({ 0x8: 1 }));
  205. });
  206. it('parses encoding format 1', function () {
  207. var bytes = new Uint8Array([0x00, 0x00, 0x01, 0x01, 0x07, 0x01]);
  208. parser.bytes = bytes;
  209. var encoding = parser.parseEncoding(2, {}, new _cff_parser.CFFStrings(), null);
  210. expect(encoding.encoding).toEqual(createWithNullProto({
  211. 0x7: 0x01,
  212. 0x08: 0x02
  213. }));
  214. });
  215. it('parses fdselect format 0', function () {
  216. var bytes = new Uint8Array([0x00, 0x00, 0x01]);
  217. parser.bytes = bytes.slice();
  218. var fdSelect = parser.parseFDSelect(0, 2);
  219. expect(fdSelect.fdSelect).toEqual([0, 1]);
  220. expect(fdSelect.raw).toEqual(bytes);
  221. });
  222. it('parses fdselect format 3', function () {
  223. var bytes = new Uint8Array([0x03, 0x00, 0x02, 0x00, 0x00, 0x09, 0x00, 0x02, 0x0a, 0x00, 0x04]);
  224. parser.bytes = bytes.slice();
  225. var fdSelect = parser.parseFDSelect(0, 4);
  226. expect(fdSelect.fdSelect).toEqual([9, 9, 0xa, 0xa]);
  227. expect(fdSelect.raw).toEqual(bytes);
  228. });
  229. it('parses invalid fdselect format 3 (bug 1146106)', function () {
  230. var bytes = new Uint8Array([0x03, 0x00, 0x02, 0x00, 0x01, 0x09, 0x00, 0x02, 0x0a, 0x00, 0x04]);
  231. parser.bytes = bytes.slice();
  232. var fdSelect = parser.parseFDSelect(0, 4);
  233. expect(fdSelect.fdSelect).toEqual([9, 9, 0xa, 0xa]);
  234. bytes[3] = bytes[4] = 0x00;
  235. expect(fdSelect.raw).toEqual(bytes);
  236. });
  237. });
  238. describe('CFFCompiler', function () {
  239. it('encodes integers', function () {
  240. var c = new _cff_parser.CFFCompiler();
  241. expect(c.encodeInteger(0)).toEqual([0x8b]);
  242. expect(c.encodeInteger(100)).toEqual([0xef]);
  243. expect(c.encodeInteger(-100)).toEqual([0x27]);
  244. expect(c.encodeInteger(1000)).toEqual([0xfa, 0x7c]);
  245. expect(c.encodeInteger(-1000)).toEqual([0xfe, 0x7c]);
  246. expect(c.encodeInteger(10000)).toEqual([0x1c, 0x27, 0x10]);
  247. expect(c.encodeInteger(-10000)).toEqual([0x1c, 0xd8, 0xf0]);
  248. expect(c.encodeInteger(100000)).toEqual([0x1d, 0x00, 0x01, 0x86, 0xa0]);
  249. expect(c.encodeInteger(-100000)).toEqual([0x1d, 0xff, 0xfe, 0x79, 0x60]);
  250. });
  251. it('encodes floats', function () {
  252. var c = new _cff_parser.CFFCompiler();
  253. expect(c.encodeFloat(-2.25)).toEqual([0x1e, 0xe2, 0xa2, 0x5f]);
  254. expect(c.encodeFloat(5e-11)).toEqual([0x1e, 0x5c, 0x11, 0xff]);
  255. });
  256. });