cff_parser_spec.js 10 KB

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