cff_parser_spec.js 10 KB

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