pdfjs-dist 2.0.489 → 2.2.228

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of pdfjs-dist might be problematic. Click here for more details.

Files changed (169) hide show
  1. package/CODE_OF_CONDUCT.md +15 -0
  2. package/bower.json +1 -1
  3. package/build/pdf.js +18515 -11402
  4. package/build/pdf.js.map +1 -1
  5. package/build/pdf.min.js +1 -1
  6. package/build/pdf.worker.js +48266 -37137
  7. package/build/pdf.worker.js.map +1 -1
  8. package/build/pdf.worker.min.js +1 -1
  9. package/external/url/url-lib.js +627 -0
  10. package/image_decoders/pdf.image_decoders.js +11350 -0
  11. package/image_decoders/pdf.image_decoders.js.map +1 -0
  12. package/image_decoders/pdf.image_decoders.min.js +1 -0
  13. package/lib/core/annotation.js +587 -242
  14. package/lib/core/arithmetic_decoder.js +275 -245
  15. package/lib/core/bidi.js +65 -6
  16. package/lib/core/ccitt.js +173 -18
  17. package/lib/core/ccitt_stream.js +15 -6
  18. package/lib/core/cff_parser.js +433 -61
  19. package/lib/core/charsets.js +5 -4
  20. package/lib/core/chunked_stream.js +400 -152
  21. package/lib/core/cmap.js +326 -87
  22. package/lib/core/colorspace.js +874 -594
  23. package/lib/core/core_utils.js +147 -0
  24. package/lib/core/crypto.js +290 -45
  25. package/lib/core/document.js +560 -268
  26. package/lib/core/encodings.js +19 -10
  27. package/lib/core/evaluator.js +1005 -360
  28. package/lib/core/font_renderer.js +331 -97
  29. package/lib/core/fonts.js +812 -195
  30. package/lib/core/function.js +284 -71
  31. package/lib/core/glyphlist.js +4 -3
  32. package/lib/core/image.js +169 -62
  33. package/lib/core/image_utils.js +111 -0
  34. package/lib/core/jbig2.js +479 -66
  35. package/lib/core/jbig2_stream.js +19 -8
  36. package/lib/core/jpeg_stream.js +38 -13
  37. package/lib/core/jpg.js +253 -29
  38. package/lib/core/jpx.js +396 -6
  39. package/lib/core/jpx_stream.js +18 -6
  40. package/lib/core/metrics.js +15 -15
  41. package/lib/core/murmurhash3.js +56 -34
  42. package/lib/core/obj.js +1354 -488
  43. package/lib/core/operator_list.js +144 -31
  44. package/lib/core/parser.js +539 -191
  45. package/lib/core/pattern.js +148 -14
  46. package/lib/core/pdf_manager.js +323 -133
  47. package/lib/core/primitives.js +111 -24
  48. package/lib/core/ps_parser.js +134 -45
  49. package/lib/core/standard_fonts.js +17 -17
  50. package/lib/core/stream.js +313 -34
  51. package/lib/core/type1_parser.js +143 -13
  52. package/lib/core/unicode.js +32 -5
  53. package/lib/core/worker.js +217 -190
  54. package/lib/core/worker_stream.js +277 -0
  55. package/lib/display/annotation_layer.js +450 -133
  56. package/lib/display/api.js +1597 -784
  57. package/lib/display/api_compatibility.js +11 -13
  58. package/lib/display/canvas.js +360 -44
  59. package/lib/display/content_disposition.js +83 -32
  60. package/lib/display/display_utils.js +747 -0
  61. package/lib/display/fetch_stream.js +221 -90
  62. package/lib/display/font_loader.js +468 -236
  63. package/lib/display/metadata.js +38 -16
  64. package/lib/display/network.js +635 -428
  65. package/lib/display/network_utils.js +32 -19
  66. package/lib/display/node_stream.js +367 -175
  67. package/lib/display/pattern_helper.js +81 -31
  68. package/lib/display/svg.js +1235 -519
  69. package/lib/display/text_layer.js +153 -29
  70. package/lib/display/transport_stream.js +345 -94
  71. package/lib/display/webgl.js +64 -18
  72. package/lib/display/worker_options.js +5 -4
  73. package/lib/display/xml_parser.js +166 -53
  74. package/lib/examples/node/domstubs.js +60 -4
  75. package/lib/pdf.js +36 -14
  76. package/lib/pdf.worker.js +5 -3
  77. package/lib/shared/compatibility.js +158 -564
  78. package/lib/shared/global_scope.js +2 -2
  79. package/lib/shared/is_node.js +4 -4
  80. package/lib/shared/message_handler.js +521 -0
  81. package/lib/shared/streams_polyfill.js +21 -17
  82. package/lib/shared/url_polyfill.js +56 -0
  83. package/lib/shared/util.js +243 -710
  84. package/lib/test/unit/annotation_spec.js +870 -401
  85. package/lib/test/unit/api_spec.js +657 -345
  86. package/lib/test/unit/bidi_spec.js +7 -7
  87. package/lib/test/unit/cff_parser_spec.js +54 -11
  88. package/lib/test/unit/clitests_helper.js +10 -8
  89. package/lib/test/unit/cmap_spec.js +95 -41
  90. package/lib/test/unit/colorspace_spec.js +115 -63
  91. package/lib/test/unit/core_utils_spec.js +191 -0
  92. package/lib/test/unit/crypto_spec.js +17 -5
  93. package/lib/test/unit/custom_spec.js +43 -55
  94. package/lib/test/unit/display_svg_spec.js +34 -18
  95. package/lib/test/unit/display_utils_spec.js +273 -0
  96. package/lib/test/unit/document_spec.js +8 -13
  97. package/lib/test/unit/encodings_spec.js +25 -45
  98. package/lib/test/unit/evaluator_spec.js +38 -15
  99. package/lib/test/unit/fetch_stream_spec.js +109 -0
  100. package/lib/test/unit/function_spec.js +17 -5
  101. package/lib/test/unit/jasmine-boot.js +33 -20
  102. package/lib/test/unit/{util_stream_spec.js → message_handler_spec.js} +41 -69
  103. package/lib/test/unit/metadata_spec.js +71 -11
  104. package/lib/test/unit/murmurhash3_spec.js +3 -3
  105. package/lib/test/unit/network_spec.js +19 -54
  106. package/lib/test/unit/network_utils_spec.js +91 -14
  107. package/lib/test/unit/node_stream_spec.js +56 -32
  108. package/lib/test/unit/parser_spec.js +162 -71
  109. package/lib/test/unit/pdf_find_controller_spec.js +230 -0
  110. package/lib/test/unit/pdf_find_utils_spec.js +63 -0
  111. package/lib/test/unit/pdf_history_spec.js +21 -9
  112. package/lib/test/unit/primitives_spec.js +53 -20
  113. package/lib/test/unit/stream_spec.js +12 -4
  114. package/lib/test/unit/test_utils.js +273 -56
  115. package/lib/test/unit/testreporter.js +21 -3
  116. package/lib/test/unit/type1_parser_spec.js +8 -6
  117. package/lib/test/unit/ui_utils_spec.js +454 -16
  118. package/lib/test/unit/unicode_spec.js +18 -15
  119. package/lib/test/unit/util_spec.js +87 -128
  120. package/lib/web/annotation_layer_builder.js +39 -22
  121. package/lib/web/app.js +1290 -588
  122. package/lib/web/app_options.js +100 -62
  123. package/lib/web/base_viewer.js +511 -182
  124. package/lib/web/chromecom.js +261 -117
  125. package/lib/web/debugger.js +166 -22
  126. package/lib/web/download_manager.js +32 -13
  127. package/lib/web/firefox_print_service.js +20 -10
  128. package/lib/web/firefoxcom.js +315 -80
  129. package/lib/web/genericcom.js +89 -30
  130. package/lib/web/genericl10n.js +142 -30
  131. package/lib/web/grab_to_pan.js +28 -4
  132. package/lib/web/interfaces.js +170 -47
  133. package/lib/web/overlay_manager.js +235 -85
  134. package/lib/web/password_prompt.js +22 -14
  135. package/lib/web/pdf_attachment_viewer.js +38 -18
  136. package/lib/web/pdf_cursor_tools.js +39 -16
  137. package/lib/web/pdf_document_properties.js +255 -136
  138. package/lib/web/pdf_find_bar.js +84 -40
  139. package/lib/web/pdf_find_controller.js +495 -184
  140. package/lib/web/pdf_find_utils.js +111 -0
  141. package/lib/web/pdf_history.js +190 -53
  142. package/lib/web/pdf_link_service.js +138 -77
  143. package/lib/web/pdf_outline_viewer.js +122 -46
  144. package/lib/web/pdf_page_view.js +191 -67
  145. package/lib/web/pdf_presentation_mode.js +99 -34
  146. package/lib/web/pdf_print_service.js +61 -13
  147. package/lib/web/pdf_rendering_queue.js +28 -9
  148. package/lib/web/pdf_sidebar.js +141 -81
  149. package/lib/web/pdf_sidebar_resizer.js +42 -16
  150. package/lib/web/pdf_single_page_viewer.js +74 -66
  151. package/lib/web/pdf_thumbnail_view.js +104 -33
  152. package/lib/web/pdf_thumbnail_viewer.js +66 -26
  153. package/lib/web/pdf_viewer.component.js +112 -32
  154. package/lib/web/pdf_viewer.js +91 -52
  155. package/lib/web/preferences.js +284 -89
  156. package/lib/web/secondary_toolbar.js +165 -40
  157. package/lib/web/text_layer_builder.js +134 -59
  158. package/lib/web/toolbar.js +78 -43
  159. package/lib/web/ui_utils.js +462 -136
  160. package/lib/web/view_history.js +215 -67
  161. package/lib/web/viewer_compatibility.js +4 -13
  162. package/package.json +5 -4
  163. package/web/pdf_viewer.css +79 -11
  164. package/web/pdf_viewer.js +6107 -3748
  165. package/web/pdf_viewer.js.map +1 -1
  166. package/lib/display/dom_utils.js +0 -309
  167. package/lib/test/unit/dom_utils_spec.js +0 -89
  168. package/lib/test/unit/fonts_spec.js +0 -81
  169. package/lib/web/dom_events.js +0 -137
@@ -2,7 +2,7 @@
2
2
  * @licstart The following is the entire license notice for the
3
3
  * Javascript code in this page
4
4
  *
5
- * Copyright 2017 Mozilla Foundation
5
+ * Copyright 2019 Mozilla Foundation
6
6
  *
7
7
  * Licensed under the Apache License, Version 2.0 (the "License");
8
8
  * you may not use this file except in compliance with the License.
@@ -19,21 +19,24 @@
19
19
  * @licend The above is the entire license notice for the
20
20
  * Javascript code in this page
21
21
  */
22
- 'use strict';
22
+ "use strict";
23
23
 
24
24
  Object.defineProperty(exports, "__esModule", {
25
25
  value: true
26
26
  });
27
- exports.CFFCompiler = exports.CFFPrivateDict = exports.CFFTopDict = exports.CFFCharset = exports.CFFIndex = exports.CFFStrings = exports.CFFHeader = exports.CFF = exports.CFFParser = exports.CFFStandardStrings = undefined;
27
+ exports.CFFFDSelect = exports.CFFCompiler = exports.CFFPrivateDict = exports.CFFTopDict = exports.CFFCharset = exports.CFFIndex = exports.CFFStrings = exports.CFFHeader = exports.CFF = exports.CFFParser = exports.CFFStandardStrings = void 0;
28
28
 
29
- var _util = require('../shared/util');
29
+ var _util = require("../shared/util");
30
30
 
31
- var _charsets = require('./charsets');
31
+ var _charsets = require("./charsets");
32
32
 
33
- var _encodings = require('./encodings');
33
+ var _encodings = require("./encodings");
34
34
 
35
35
  var MAX_SUBR_NESTING = 10;
36
36
  var CFFStandardStrings = ['.notdef', 'space', 'exclam', 'quotedbl', 'numbersign', 'dollar', 'percent', 'ampersand', 'quoteright', 'parenleft', 'parenright', 'asterisk', 'plus', 'comma', 'hyphen', 'period', 'slash', 'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'colon', 'semicolon', 'less', 'equal', 'greater', 'question', 'at', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'bracketleft', 'backslash', 'bracketright', 'asciicircum', 'underscore', 'quoteleft', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'braceleft', 'bar', 'braceright', 'asciitilde', 'exclamdown', 'cent', 'sterling', 'fraction', 'yen', 'florin', 'section', 'currency', 'quotesingle', 'quotedblleft', 'guillemotleft', 'guilsinglleft', 'guilsinglright', 'fi', 'fl', 'endash', 'dagger', 'daggerdbl', 'periodcentered', 'paragraph', 'bullet', 'quotesinglbase', 'quotedblbase', 'quotedblright', 'guillemotright', 'ellipsis', 'perthousand', 'questiondown', 'grave', 'acute', 'circumflex', 'tilde', 'macron', 'breve', 'dotaccent', 'dieresis', 'ring', 'cedilla', 'hungarumlaut', 'ogonek', 'caron', 'emdash', 'AE', 'ordfeminine', 'Lslash', 'Oslash', 'OE', 'ordmasculine', 'ae', 'dotlessi', 'lslash', 'oslash', 'oe', 'germandbls', 'onesuperior', 'logicalnot', 'mu', 'trademark', 'Eth', 'onehalf', 'plusminus', 'Thorn', 'onequarter', 'divide', 'brokenbar', 'degree', 'thorn', 'threequarters', 'twosuperior', 'registered', 'minus', 'eth', 'multiply', 'threesuperior', 'copyright', 'Aacute', 'Acircumflex', 'Adieresis', 'Agrave', 'Aring', 'Atilde', 'Ccedilla', 'Eacute', 'Ecircumflex', 'Edieresis', 'Egrave', 'Iacute', 'Icircumflex', 'Idieresis', 'Igrave', 'Ntilde', 'Oacute', 'Ocircumflex', 'Odieresis', 'Ograve', 'Otilde', 'Scaron', 'Uacute', 'Ucircumflex', 'Udieresis', 'Ugrave', 'Yacute', 'Ydieresis', 'Zcaron', 'aacute', 'acircumflex', 'adieresis', 'agrave', 'aring', 'atilde', 'ccedilla', 'eacute', 'ecircumflex', 'edieresis', 'egrave', 'iacute', 'icircumflex', 'idieresis', 'igrave', 'ntilde', 'oacute', 'ocircumflex', 'odieresis', 'ograve', 'otilde', 'scaron', 'uacute', 'ucircumflex', 'udieresis', 'ugrave', 'yacute', 'ydieresis', 'zcaron', 'exclamsmall', 'Hungarumlautsmall', 'dollaroldstyle', 'dollarsuperior', 'ampersandsmall', 'Acutesmall', 'parenleftsuperior', 'parenrightsuperior', 'twodotenleader', 'onedotenleader', 'zerooldstyle', 'oneoldstyle', 'twooldstyle', 'threeoldstyle', 'fouroldstyle', 'fiveoldstyle', 'sixoldstyle', 'sevenoldstyle', 'eightoldstyle', 'nineoldstyle', 'commasuperior', 'threequartersemdash', 'periodsuperior', 'questionsmall', 'asuperior', 'bsuperior', 'centsuperior', 'dsuperior', 'esuperior', 'isuperior', 'lsuperior', 'msuperior', 'nsuperior', 'osuperior', 'rsuperior', 'ssuperior', 'tsuperior', 'ff', 'ffi', 'ffl', 'parenleftinferior', 'parenrightinferior', 'Circumflexsmall', 'hyphensuperior', 'Gravesmall', 'Asmall', 'Bsmall', 'Csmall', 'Dsmall', 'Esmall', 'Fsmall', 'Gsmall', 'Hsmall', 'Ismall', 'Jsmall', 'Ksmall', 'Lsmall', 'Msmall', 'Nsmall', 'Osmall', 'Psmall', 'Qsmall', 'Rsmall', 'Ssmall', 'Tsmall', 'Usmall', 'Vsmall', 'Wsmall', 'Xsmall', 'Ysmall', 'Zsmall', 'colonmonetary', 'onefitted', 'rupiah', 'Tildesmall', 'exclamdownsmall', 'centoldstyle', 'Lslashsmall', 'Scaronsmall', 'Zcaronsmall', 'Dieresissmall', 'Brevesmall', 'Caronsmall', 'Dotaccentsmall', 'Macronsmall', 'figuredash', 'hypheninferior', 'Ogoneksmall', 'Ringsmall', 'Cedillasmall', 'questiondownsmall', 'oneeighth', 'threeeighths', 'fiveeighths', 'seveneighths', 'onethird', 'twothirds', 'zerosuperior', 'foursuperior', 'fivesuperior', 'sixsuperior', 'sevensuperior', 'eightsuperior', 'ninesuperior', 'zeroinferior', 'oneinferior', 'twoinferior', 'threeinferior', 'fourinferior', 'fiveinferior', 'sixinferior', 'seveninferior', 'eightinferior', 'nineinferior', 'centinferior', 'dollarinferior', 'periodinferior', 'commainferior', 'Agravesmall', 'Aacutesmall', 'Acircumflexsmall', 'Atildesmall', 'Adieresissmall', 'Aringsmall', 'AEsmall', 'Ccedillasmall', 'Egravesmall', 'Eacutesmall', 'Ecircumflexsmall', 'Edieresissmall', 'Igravesmall', 'Iacutesmall', 'Icircumflexsmall', 'Idieresissmall', 'Ethsmall', 'Ntildesmall', 'Ogravesmall', 'Oacutesmall', 'Ocircumflexsmall', 'Otildesmall', 'Odieresissmall', 'OEsmall', 'Oslashsmall', 'Ugravesmall', 'Uacutesmall', 'Ucircumflexsmall', 'Udieresissmall', 'Yacutesmall', 'Thornsmall', 'Ydieresissmall', '001.000', '001.001', '001.002', '001.003', 'Black', 'Bold', 'Book', 'Light', 'Medium', 'Regular', 'Roman', 'Semibold'];
37
+ exports.CFFStandardStrings = CFFStandardStrings;
38
+ var NUM_STANDARD_CFF_STRINGS = 391;
39
+
37
40
  var CFFParser = function CFFParserClosure() {
38
41
  var CharstringValidationData = [null, {
39
42
  id: 'hstem',
@@ -244,11 +247,13 @@ var CFFParser = function CFFParserClosure() {
244
247
  min: 11,
245
248
  resetStack: true
246
249
  }];
250
+
247
251
  function CFFParser(file, properties, seacAnalysisEnabled) {
248
252
  this.bytes = file.getBytes();
249
253
  this.properties = properties;
250
254
  this.seacAnalysisEnabled = !!seacAnalysisEnabled;
251
255
  }
256
+
252
257
  CFFParser.prototype = {
253
258
  parse: function CFFParser_parse() {
254
259
  var properties = this.properties;
@@ -271,24 +276,31 @@ var CFFParser = function CFFParserClosure() {
271
276
  var charStringOffset = topDict.getByName('CharStrings');
272
277
  var charStringIndex = this.parseIndex(charStringOffset).obj;
273
278
  var fontMatrix = topDict.getByName('FontMatrix');
279
+
274
280
  if (fontMatrix) {
275
281
  properties.fontMatrix = fontMatrix;
276
282
  }
283
+
277
284
  var fontBBox = topDict.getByName('FontBBox');
285
+
278
286
  if (fontBBox) {
279
287
  properties.ascent = Math.max(fontBBox[3], fontBBox[1]);
280
288
  properties.descent = Math.min(fontBBox[1], fontBBox[3]);
281
289
  properties.ascentScaled = true;
282
290
  }
291
+
283
292
  var charset, encoding;
293
+
284
294
  if (cff.isCIDFont) {
285
295
  var fdArrayIndex = this.parseIndex(topDict.getByName('FDArray')).obj;
296
+
286
297
  for (var i = 0, ii = fdArrayIndex.count; i < ii; ++i) {
287
298
  var dictRaw = fdArrayIndex.get(i);
288
299
  var fontDict = this.createDict(CFFTopDict, this.parseDict(dictRaw), cff.strings);
289
300
  this.parsePrivateDict(fontDict);
290
301
  cff.fdArray.push(fontDict);
291
302
  }
303
+
292
304
  encoding = null;
293
305
  charset = this.parseCharsets(topDict.getByName('charset'), charStringIndex.count, cff.strings, true);
294
306
  cff.fdSelect = this.parseFDSelect(topDict.getByName('FDSelect'), charStringIndex.count);
@@ -296,6 +308,7 @@ var CFFParser = function CFFParserClosure() {
296
308
  charset = this.parseCharsets(topDict.getByName('charset'), charStringIndex.count, cff.strings, false);
297
309
  encoding = this.parseEncoding(topDict.getByName('Encoding'), properties, cff.strings, charset.charset);
298
310
  }
311
+
299
312
  cff.charset = charset;
300
313
  cff.encoding = encoding;
301
314
  var charStringsAndSeacs = this.parseCharStrings({
@@ -315,17 +328,21 @@ var CFFParser = function CFFParserClosure() {
315
328
  var bytes = this.bytes;
316
329
  var bytesLength = bytes.length;
317
330
  var offset = 0;
331
+
318
332
  while (offset < bytesLength && bytes[offset] !== 1) {
319
333
  ++offset;
320
334
  }
335
+
321
336
  if (offset >= bytesLength) {
322
337
  throw new _util.FormatError('Invalid CFF header');
323
338
  }
339
+
324
340
  if (offset !== 0) {
325
341
  (0, _util.info)('cff data is shifted');
326
342
  bytes = bytes.subarray(offset);
327
343
  this.bytes = bytes;
328
344
  }
345
+
329
346
  var major = bytes[0];
330
347
  var minor = bytes[1];
331
348
  var hdrSize = bytes[2];
@@ -338,8 +355,10 @@ var CFFParser = function CFFParserClosure() {
338
355
  },
339
356
  parseDict: function CFFParser_parseDict(dict) {
340
357
  var pos = 0;
358
+
341
359
  function parseOperand() {
342
360
  var value = dict[pos++];
361
+
343
362
  if (value === 30) {
344
363
  return parseFloatOperand();
345
364
  } else if (value === 28) {
@@ -359,39 +378,51 @@ var CFFParser = function CFFParserClosure() {
359
378
  } else if (value >= 251 && value <= 254) {
360
379
  return -((value - 251) * 256) - dict[pos++] - 108;
361
380
  }
381
+
362
382
  (0, _util.warn)('CFFParser_parseDict: "' + value + '" is a reserved command.');
363
383
  return NaN;
364
384
  }
385
+
365
386
  function parseFloatOperand() {
366
387
  var str = '';
367
388
  var eof = 15;
368
389
  var lookup = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', 'E', 'E-', null, '-'];
369
390
  var length = dict.length;
391
+
370
392
  while (pos < length) {
371
393
  var b = dict[pos++];
372
394
  var b1 = b >> 4;
373
395
  var b2 = b & 15;
396
+
374
397
  if (b1 === eof) {
375
398
  break;
376
399
  }
400
+
377
401
  str += lookup[b1];
402
+
378
403
  if (b2 === eof) {
379
404
  break;
380
405
  }
406
+
381
407
  str += lookup[b2];
382
408
  }
409
+
383
410
  return parseFloat(str);
384
411
  }
412
+
385
413
  var operands = [];
386
414
  var entries = [];
387
415
  pos = 0;
388
416
  var end = dict.length;
417
+
389
418
  while (pos < end) {
390
419
  var b = dict[pos];
420
+
391
421
  if (b <= 21) {
392
422
  if (b === 12) {
393
423
  b = b << 8 | dict[++pos];
394
424
  }
425
+
395
426
  entries.push([b, operands]);
396
427
  operands = [];
397
428
  ++pos;
@@ -399,6 +430,7 @@ var CFFParser = function CFFParserClosure() {
399
430
  operands.push(parseOperand());
400
431
  }
401
432
  }
433
+
402
434
  return entries;
403
435
  },
404
436
  parseIndex: function CFFParser_parseIndex(pos) {
@@ -408,24 +440,31 @@ var CFFParser = function CFFParserClosure() {
408
440
  var offsets = [];
409
441
  var end = pos;
410
442
  var i, ii;
443
+
411
444
  if (count !== 0) {
412
445
  var offsetSize = bytes[pos++];
413
446
  var startPos = pos + (count + 1) * offsetSize - 1;
447
+
414
448
  for (i = 0, ii = count + 1; i < ii; ++i) {
415
449
  var offset = 0;
450
+
416
451
  for (var j = 0; j < offsetSize; ++j) {
417
452
  offset <<= 8;
418
453
  offset += bytes[pos++];
419
454
  }
455
+
420
456
  offsets.push(startPos + offset);
421
457
  }
458
+
422
459
  end = offsets[count];
423
460
  }
461
+
424
462
  for (i = 0, ii = offsets.length - 1; i < ii; ++i) {
425
463
  var offsetStart = offsets[i];
426
464
  var offsetEnd = offsets[i + 1];
427
465
  cffIndex.add(bytes.subarray(offsetStart, offsetEnd));
428
466
  }
467
+
429
468
  return {
430
469
  obj: cffIndex,
431
470
  endPos: end
@@ -433,42 +472,52 @@ var CFFParser = function CFFParserClosure() {
433
472
  },
434
473
  parseNameIndex: function CFFParser_parseNameIndex(index) {
435
474
  var names = [];
475
+
436
476
  for (var i = 0, ii = index.count; i < ii; ++i) {
437
477
  var name = index.get(i);
438
478
  names.push((0, _util.bytesToString)(name));
439
479
  }
480
+
440
481
  return names;
441
482
  },
442
483
  parseStringIndex: function CFFParser_parseStringIndex(index) {
443
484
  var strings = new CFFStrings();
485
+
444
486
  for (var i = 0, ii = index.count; i < ii; ++i) {
445
487
  var data = index.get(i);
446
488
  strings.add((0, _util.bytesToString)(data));
447
489
  }
490
+
448
491
  return strings;
449
492
  },
450
493
  createDict: function CFFParser_createDict(Type, dict, strings) {
451
494
  var cffDict = new Type(strings);
495
+
452
496
  for (var i = 0, ii = dict.length; i < ii; ++i) {
453
497
  var pair = dict[i];
454
498
  var key = pair[0];
455
499
  var value = pair[1];
456
500
  cffDict.setByKey(key, value);
457
501
  }
502
+
458
503
  return cffDict;
459
504
  },
460
505
  parseCharString: function CFFParser_parseCharString(state, data, localSubrIndex, globalSubrIndex) {
461
506
  if (!data || state.callDepth > MAX_SUBR_NESTING) {
462
507
  return false;
463
508
  }
509
+
464
510
  var stackSize = state.stackSize;
465
511
  var stack = state.stack;
466
512
  var length = data.length;
513
+
467
514
  for (var j = 0; j < length;) {
468
515
  var value = data[j++];
469
516
  var validationCommand = null;
517
+
470
518
  if (value === 12) {
471
519
  var q = data[j++];
520
+
472
521
  if (q === 0) {
473
522
  data[j - 2] = 139;
474
523
  data[j - 1] = 22;
@@ -483,11 +532,13 @@ var CFFParser = function CFFParserClosure() {
483
532
  } else if (value === 14) {
484
533
  if (stackSize >= 4) {
485
534
  stackSize -= 4;
535
+
486
536
  if (this.seacAnalysisEnabled) {
487
537
  state.seac = stack.slice(stackSize, stackSize + 4);
488
538
  return false;
489
539
  }
490
540
  }
541
+
491
542
  validationCommand = CharstringValidationData[value];
492
543
  } else if (value >= 32 && value <= 246) {
493
544
  stack[stackSize] = value - 139;
@@ -507,34 +558,43 @@ var CFFParser = function CFFParserClosure() {
507
558
  validationCommand = CharstringValidationData[value];
508
559
  } else if (value === 10 || value === 29) {
509
560
  var subrsIndex;
561
+
510
562
  if (value === 10) {
511
563
  subrsIndex = localSubrIndex;
512
564
  } else {
513
565
  subrsIndex = globalSubrIndex;
514
566
  }
567
+
515
568
  if (!subrsIndex) {
516
569
  validationCommand = CharstringValidationData[value];
517
570
  (0, _util.warn)('Missing subrsIndex for ' + validationCommand.id);
518
571
  return false;
519
572
  }
573
+
520
574
  var bias = 32768;
575
+
521
576
  if (subrsIndex.count < 1240) {
522
577
  bias = 107;
523
578
  } else if (subrsIndex.count < 33900) {
524
579
  bias = 1131;
525
580
  }
581
+
526
582
  var subrNumber = stack[--stackSize] + bias;
583
+
527
584
  if (subrNumber < 0 || subrNumber >= subrsIndex.count || isNaN(subrNumber)) {
528
585
  validationCommand = CharstringValidationData[value];
529
586
  (0, _util.warn)('Out of bounds subrIndex for ' + validationCommand.id);
530
587
  return false;
531
588
  }
589
+
532
590
  state.stackSize = stackSize;
533
591
  state.callDepth++;
534
592
  var valid = this.parseCharString(state, subrsIndex.get(subrNumber), localSubrIndex, globalSubrIndex);
593
+
535
594
  if (!valid) {
536
595
  return false;
537
596
  }
597
+
538
598
  state.callDepth--;
539
599
  stackSize = state.stackSize;
540
600
  continue;
@@ -544,32 +604,46 @@ var CFFParser = function CFFParserClosure() {
544
604
  } else {
545
605
  validationCommand = CharstringValidationData[value];
546
606
  }
607
+
547
608
  if (validationCommand) {
548
609
  if (validationCommand.stem) {
549
610
  state.hints += stackSize >> 1;
611
+
612
+ if (value === 3 || value === 23) {
613
+ state.hasVStems = true;
614
+ } else if (state.hasVStems && (value === 1 || value === 18)) {
615
+ (0, _util.warn)('CFF stem hints are in wrong order');
616
+ data[j - 1] = value === 1 ? 3 : 23;
617
+ }
550
618
  }
619
+
551
620
  if ('min' in validationCommand) {
552
621
  if (!state.undefStack && stackSize < validationCommand.min) {
553
622
  (0, _util.warn)('Not enough parameters for ' + validationCommand.id + '; actual: ' + stackSize + ', expected: ' + validationCommand.min);
554
623
  return false;
555
624
  }
556
625
  }
626
+
557
627
  if (state.firstStackClearing && validationCommand.stackClearing) {
558
628
  state.firstStackClearing = false;
559
629
  stackSize -= validationCommand.min;
630
+
560
631
  if (stackSize >= 2 && validationCommand.stem) {
561
632
  stackSize %= 2;
562
633
  } else if (stackSize > 1) {
563
634
  (0, _util.warn)('Found too many parameters for stack-clearing command');
564
635
  }
636
+
565
637
  if (stackSize > 0 && stack[stackSize - 1] >= 0) {
566
638
  state.width = stack[stackSize - 1];
567
639
  }
568
640
  }
641
+
569
642
  if ('stackDelta' in validationCommand) {
570
643
  if ('stackFn' in validationCommand) {
571
644
  validationCommand.stackFn(stack, stackSize);
572
645
  }
646
+
573
647
  stackSize += validationCommand.stackDelta;
574
648
  } else if (validationCommand.stackClearing) {
575
649
  stackSize = 0;
@@ -583,6 +657,7 @@ var CFFParser = function CFFParserClosure() {
583
657
  }
584
658
  }
585
659
  }
660
+
586
661
  state.stackSize = stackSize;
587
662
  return true;
588
663
  },
@@ -593,10 +668,10 @@ var CFFParser = function CFFParserClosure() {
593
668
  fdSelect = _ref.fdSelect,
594
669
  fdArray = _ref.fdArray,
595
670
  privateDict = _ref.privateDict;
596
-
597
671
  var seacs = [];
598
672
  var widths = [];
599
673
  var count = charStrings.count;
674
+
600
675
  for (var i = 0; i < count; i++) {
601
676
  var charstring = charStrings.get(i);
602
677
  var state = {
@@ -607,21 +682,26 @@ var CFFParser = function CFFParserClosure() {
607
682
  hints: 0,
608
683
  firstStackClearing: true,
609
684
  seac: null,
610
- width: null
685
+ width: null,
686
+ hasVStems: false
611
687
  };
612
688
  var valid = true;
613
689
  var localSubrToUse = null;
614
690
  var privateDictToUse = privateDict;
691
+
615
692
  if (fdSelect && fdArray.length) {
616
693
  var fdIndex = fdSelect.getFDIndex(i);
694
+
617
695
  if (fdIndex === -1) {
618
696
  (0, _util.warn)('Glyph index is not in fd select.');
619
697
  valid = false;
620
698
  }
699
+
621
700
  if (fdIndex >= fdArray.length) {
622
701
  (0, _util.warn)('Invalid fd index for glyph index.');
623
702
  valid = false;
624
703
  }
704
+
625
705
  if (valid) {
626
706
  privateDictToUse = fdArray[fdIndex].privateDict;
627
707
  localSubrToUse = privateDictToUse.subrsIndex;
@@ -629,9 +709,11 @@ var CFFParser = function CFFParserClosure() {
629
709
  } else if (localSubrIndex) {
630
710
  localSubrToUse = localSubrIndex;
631
711
  }
712
+
632
713
  if (valid) {
633
714
  valid = this.parseCharString(state, charstring, localSubrToUse, globalSubrIndex);
634
715
  }
716
+
635
717
  if (state.width !== null) {
636
718
  var nominalWidth = privateDictToUse.getByName('nominalWidthX');
637
719
  widths[i] = nominalWidth + state.width;
@@ -639,20 +721,22 @@ var CFFParser = function CFFParserClosure() {
639
721
  var defaultWidth = privateDictToUse.getByName('defaultWidthX');
640
722
  widths[i] = defaultWidth;
641
723
  }
724
+
642
725
  if (state.seac !== null) {
643
726
  seacs[i] = state.seac;
644
727
  }
728
+
645
729
  if (!valid) {
646
730
  charStrings.set(i, new Uint8Array([14]));
647
731
  }
648
732
  }
733
+
649
734
  return {
650
735
  charStrings: charStrings,
651
736
  seacs: seacs,
652
737
  widths: widths
653
738
  };
654
739
  },
655
-
656
740
  emptyPrivateDictionary: function CFFParser_emptyPrivateDictionary(parentDict) {
657
741
  var privateDict = this.createDict(CFFPrivateDict, [], parentDict.strings);
658
742
  parentDict.setByKey(18, [0, 0]);
@@ -663,31 +747,40 @@ var CFFParser = function CFFParserClosure() {
663
747
  this.emptyPrivateDictionary(parentDict);
664
748
  return;
665
749
  }
750
+
666
751
  var privateOffset = parentDict.getByName('Private');
752
+
667
753
  if (!Array.isArray(privateOffset) || privateOffset.length !== 2) {
668
754
  parentDict.removeByName('Private');
669
755
  return;
670
756
  }
757
+
671
758
  var size = privateOffset[0];
672
759
  var offset = privateOffset[1];
760
+
673
761
  if (size === 0 || offset >= this.bytes.length) {
674
762
  this.emptyPrivateDictionary(parentDict);
675
763
  return;
676
764
  }
765
+
677
766
  var privateDictEnd = offset + size;
678
767
  var dictData = this.bytes.subarray(offset, privateDictEnd);
679
768
  var dict = this.parseDict(dictData);
680
769
  var privateDict = this.createDict(CFFPrivateDict, dict, parentDict.strings);
681
770
  parentDict.privateDict = privateDict;
771
+
682
772
  if (!privateDict.getByName('Subrs')) {
683
773
  return;
684
774
  }
775
+
685
776
  var subrsOffset = privateDict.getByName('Subrs');
686
777
  var relativeOffset = offset + subrsOffset;
778
+
687
779
  if (subrsOffset === 0 || relativeOffset >= this.bytes.length) {
688
780
  this.emptyPrivateDictionary(parentDict);
689
781
  return;
690
782
  }
783
+
691
784
  var subrsIndex = this.parseIndex(relativeOffset);
692
785
  privateDict.subrsIndex = subrsIndex.obj;
693
786
  },
@@ -699,40 +792,51 @@ var CFFParser = function CFFParserClosure() {
699
792
  } else if (pos === 2) {
700
793
  return new CFFCharset(true, CFFCharsetPredefinedTypes.EXPERT_SUBSET, _charsets.ExpertSubsetCharset);
701
794
  }
795
+
702
796
  var bytes = this.bytes;
703
797
  var start = pos;
704
798
  var format = bytes[pos++];
705
799
  var charset = ['.notdef'];
706
800
  var id, count, i;
707
801
  length -= 1;
802
+
708
803
  switch (format) {
709
804
  case 0:
710
805
  for (i = 0; i < length; i++) {
711
806
  id = bytes[pos++] << 8 | bytes[pos++];
712
807
  charset.push(cid ? id : strings.get(id));
713
808
  }
809
+
714
810
  break;
811
+
715
812
  case 1:
716
813
  while (charset.length <= length) {
717
814
  id = bytes[pos++] << 8 | bytes[pos++];
718
815
  count = bytes[pos++];
816
+
719
817
  for (i = 0; i <= count; i++) {
720
818
  charset.push(cid ? id++ : strings.get(id++));
721
819
  }
722
820
  }
821
+
723
822
  break;
823
+
724
824
  case 2:
725
825
  while (charset.length <= length) {
726
826
  id = bytes[pos++] << 8 | bytes[pos++];
727
827
  count = bytes[pos++] << 8 | bytes[pos++];
828
+
728
829
  for (i = 0; i <= count; i++) {
729
830
  charset.push(cid ? id++ : strings.get(id++));
730
831
  }
731
832
  }
833
+
732
834
  break;
835
+
733
836
  default:
734
837
  throw new _util.FormatError('Unknown charset format');
735
838
  }
839
+
736
840
  var end = pos;
737
841
  var raw = bytes.subarray(start, end);
738
842
  return new CFFCharset(false, format, charset, raw);
@@ -743,20 +847,25 @@ var CFFParser = function CFFParserClosure() {
743
847
  var predefined = false;
744
848
  var format, i, ii;
745
849
  var raw = null;
850
+
746
851
  function readSupplement() {
747
852
  var supplementsCount = bytes[pos++];
853
+
748
854
  for (i = 0; i < supplementsCount; i++) {
749
855
  var code = bytes[pos++];
750
856
  var sid = (bytes[pos++] << 8) + (bytes[pos++] & 0xff);
751
857
  encoding[code] = charset.indexOf(strings.get(sid));
752
858
  }
753
859
  }
860
+
754
861
  if (pos === 0 || pos === 1) {
755
862
  predefined = true;
756
863
  format = pos;
757
864
  var baseEncoding = pos ? _encodings.ExpertEncoding : _encodings.StandardEncoding;
865
+
758
866
  for (i = 0, ii = charset.length; i < ii; i++) {
759
867
  var index = baseEncoding.indexOf(charset[i]);
868
+
760
869
  if (index !== -1) {
761
870
  encoding[index] = i;
762
871
  }
@@ -764,85 +873,102 @@ var CFFParser = function CFFParserClosure() {
764
873
  } else {
765
874
  var dataStart = pos;
766
875
  format = bytes[pos++];
876
+
767
877
  switch (format & 0x7f) {
768
878
  case 0:
769
879
  var glyphsCount = bytes[pos++];
880
+
770
881
  for (i = 1; i <= glyphsCount; i++) {
771
882
  encoding[bytes[pos++]] = i;
772
883
  }
884
+
773
885
  break;
886
+
774
887
  case 1:
775
888
  var rangesCount = bytes[pos++];
776
889
  var gid = 1;
890
+
777
891
  for (i = 0; i < rangesCount; i++) {
778
892
  var start = bytes[pos++];
779
893
  var left = bytes[pos++];
894
+
780
895
  for (var j = start; j <= start + left; j++) {
781
896
  encoding[j] = gid++;
782
897
  }
783
898
  }
899
+
784
900
  break;
901
+
785
902
  default:
786
- throw new _util.FormatError('Unknown encoding format: ' + format + ' in CFF');
903
+ throw new _util.FormatError("Unknown encoding format: ".concat(format, " in CFF"));
787
904
  }
905
+
788
906
  var dataEnd = pos;
907
+
789
908
  if (format & 0x80) {
790
909
  bytes[dataStart] &= 0x7f;
791
910
  readSupplement();
792
911
  }
912
+
793
913
  raw = bytes.subarray(dataStart, dataEnd);
794
914
  }
915
+
795
916
  format = format & 0x7f;
796
917
  return new CFFEncoding(predefined, format, encoding, raw);
797
918
  },
798
919
  parseFDSelect: function CFFParser_parseFDSelect(pos, length) {
799
- var start = pos;
800
920
  var bytes = this.bytes;
801
921
  var format = bytes[pos++];
802
- var fdSelect = [],
803
- rawBytes;
804
- var i,
805
- invalidFirstGID = false;
922
+ var fdSelect = [];
923
+ var i;
924
+
806
925
  switch (format) {
807
926
  case 0:
808
927
  for (i = 0; i < length; ++i) {
809
928
  var id = bytes[pos++];
810
929
  fdSelect.push(id);
811
930
  }
812
- rawBytes = bytes.subarray(start, pos);
931
+
813
932
  break;
933
+
814
934
  case 3:
815
935
  var rangesCount = bytes[pos++] << 8 | bytes[pos++];
936
+
816
937
  for (i = 0; i < rangesCount; ++i) {
817
938
  var first = bytes[pos++] << 8 | bytes[pos++];
939
+
818
940
  if (i === 0 && first !== 0) {
819
941
  (0, _util.warn)('parseFDSelect: The first range must have a first GID of 0' + ' -- trying to recover.');
820
- invalidFirstGID = true;
821
942
  first = 0;
822
943
  }
944
+
823
945
  var fdIndex = bytes[pos++];
824
946
  var next = bytes[pos] << 8 | bytes[pos + 1];
947
+
825
948
  for (var j = first; j < next; ++j) {
826
949
  fdSelect.push(fdIndex);
827
950
  }
828
951
  }
952
+
829
953
  pos += 2;
830
- rawBytes = bytes.subarray(start, pos);
831
- if (invalidFirstGID) {
832
- rawBytes[3] = rawBytes[4] = 0;
833
- }
834
954
  break;
955
+
835
956
  default:
836
- throw new _util.FormatError('parseFDSelect: Unknown format "' + format + '".');
957
+ throw new _util.FormatError("parseFDSelect: Unknown format \"".concat(format, "\"."));
837
958
  }
959
+
838
960
  if (fdSelect.length !== length) {
839
961
  throw new _util.FormatError('parseFDSelect: Invalid font data.');
840
962
  }
841
- return new CFFFDSelect(fdSelect, rawBytes);
963
+
964
+ return new CFFFDSelect(format, fdSelect);
842
965
  }
843
966
  };
844
967
  return CFFParser;
845
968
  }();
969
+
970
+ exports.CFFParser = CFFParser;
971
+
846
972
  var CFF = function CFFClosure() {
847
973
  function CFF() {
848
974
  this.header = null;
@@ -857,8 +983,35 @@ var CFF = function CFFClosure() {
857
983
  this.fdSelect = null;
858
984
  this.isCIDFont = false;
859
985
  }
986
+
987
+ CFF.prototype = {
988
+ duplicateFirstGlyph: function CFF_duplicateFirstGlyph() {
989
+ if (this.charStrings.count >= 65535) {
990
+ (0, _util.warn)('Not enough space in charstrings to duplicate first glyph.');
991
+ return;
992
+ }
993
+
994
+ var glyphZero = this.charStrings.get(0);
995
+ this.charStrings.add(glyphZero);
996
+
997
+ if (this.isCIDFont) {
998
+ this.fdSelect.fdSelect.push(this.fdSelect.fdSelect[0]);
999
+ }
1000
+ },
1001
+ hasGlyphId: function CFF_hasGlyphID(id) {
1002
+ if (id < 0 || id >= this.charStrings.count) {
1003
+ return false;
1004
+ }
1005
+
1006
+ var glyph = this.charStrings.get(id);
1007
+ return glyph.length > 0;
1008
+ }
1009
+ };
860
1010
  return CFF;
861
1011
  }();
1012
+
1013
+ exports.CFF = CFF;
1014
+
862
1015
  var CFFHeader = function CFFHeaderClosure() {
863
1016
  function CFFHeader(major, minor, hdrSize, offSize) {
864
1017
  this.major = major;
@@ -866,36 +1019,64 @@ var CFFHeader = function CFFHeaderClosure() {
866
1019
  this.hdrSize = hdrSize;
867
1020
  this.offSize = offSize;
868
1021
  }
1022
+
869
1023
  return CFFHeader;
870
1024
  }();
1025
+
1026
+ exports.CFFHeader = CFFHeader;
1027
+
871
1028
  var CFFStrings = function CFFStringsClosure() {
872
1029
  function CFFStrings() {
873
1030
  this.strings = [];
874
1031
  }
1032
+
875
1033
  CFFStrings.prototype = {
876
1034
  get: function CFFStrings_get(index) {
877
- if (index >= 0 && index <= 390) {
1035
+ if (index >= 0 && index <= NUM_STANDARD_CFF_STRINGS - 1) {
878
1036
  return CFFStandardStrings[index];
879
1037
  }
880
- if (index - 391 <= this.strings.length) {
881
- return this.strings[index - 391];
1038
+
1039
+ if (index - NUM_STANDARD_CFF_STRINGS <= this.strings.length) {
1040
+ return this.strings[index - NUM_STANDARD_CFF_STRINGS];
882
1041
  }
1042
+
883
1043
  return CFFStandardStrings[0];
884
1044
  },
1045
+ getSID: function CFFStrings_getSID(str) {
1046
+ var index = CFFStandardStrings.indexOf(str);
1047
+
1048
+ if (index !== -1) {
1049
+ return index;
1050
+ }
1051
+
1052
+ index = this.strings.indexOf(str);
1053
+
1054
+ if (index !== -1) {
1055
+ return index + NUM_STANDARD_CFF_STRINGS;
1056
+ }
1057
+
1058
+ return -1;
1059
+ },
885
1060
  add: function CFFStrings_add(value) {
886
1061
  this.strings.push(value);
887
1062
  },
1063
+
888
1064
  get count() {
889
1065
  return this.strings.length;
890
1066
  }
1067
+
891
1068
  };
892
1069
  return CFFStrings;
893
1070
  }();
1071
+
1072
+ exports.CFFStrings = CFFStrings;
1073
+
894
1074
  var CFFIndex = function CFFIndexClosure() {
895
1075
  function CFFIndex() {
896
1076
  this.objects = [];
897
1077
  this.length = 0;
898
1078
  }
1079
+
899
1080
  CFFIndex.prototype = {
900
1081
  add: function CFFIndex_add(data) {
901
1082
  this.length += data.length;
@@ -908,12 +1089,17 @@ var CFFIndex = function CFFIndexClosure() {
908
1089
  get: function CFFIndex_get(index) {
909
1090
  return this.objects[index];
910
1091
  },
1092
+
911
1093
  get count() {
912
1094
  return this.objects.length;
913
1095
  }
1096
+
914
1097
  };
915
1098
  return CFFIndex;
916
1099
  }();
1100
+
1101
+ exports.CFFIndex = CFFIndex;
1102
+
917
1103
  var CFFDict = function CFFDictClosure() {
918
1104
  function CFFDict(tables, strings) {
919
1105
  this.keyToNameMap = tables.keyToNameMap;
@@ -925,32 +1111,40 @@ var CFFDict = function CFFDictClosure() {
925
1111
  this.strings = strings;
926
1112
  this.values = Object.create(null);
927
1113
  }
1114
+
928
1115
  CFFDict.prototype = {
929
1116
  setByKey: function CFFDict_setByKey(key, value) {
930
1117
  if (!(key in this.keyToNameMap)) {
931
1118
  return false;
932
1119
  }
1120
+
933
1121
  var valueLength = value.length;
1122
+
934
1123
  if (valueLength === 0) {
935
1124
  return true;
936
1125
  }
1126
+
937
1127
  for (var i = 0; i < valueLength; i++) {
938
1128
  if (isNaN(value[i])) {
939
1129
  (0, _util.warn)('Invalid CFFDict value: "' + value + '" for key "' + key + '".');
940
1130
  return true;
941
1131
  }
942
1132
  }
1133
+
943
1134
  var type = this.types[key];
1135
+
944
1136
  if (type === 'num' || type === 'sid' || type === 'offset') {
945
1137
  value = value[0];
946
1138
  }
1139
+
947
1140
  this.values[key] = value;
948
1141
  return true;
949
1142
  },
950
1143
  setByName: function CFFDict_setByName(name, value) {
951
1144
  if (!(name in this.nameToKeyMap)) {
952
- throw new _util.FormatError('Invalid dictionary name "' + name + '"');
1145
+ throw new _util.FormatError("Invalid dictionary name \"".concat(name, "\""));
953
1146
  }
1147
+
954
1148
  this.values[this.nameToKeyMap[name]] = value;
955
1149
  },
956
1150
  hasName: function CFFDict_hasName(name) {
@@ -958,18 +1152,22 @@ var CFFDict = function CFFDictClosure() {
958
1152
  },
959
1153
  getByName: function CFFDict_getByName(name) {
960
1154
  if (!(name in this.nameToKeyMap)) {
961
- throw new _util.FormatError('Invalid dictionary name ' + name + '"');
1155
+ throw new _util.FormatError("Invalid dictionary name ".concat(name, "\""));
962
1156
  }
1157
+
963
1158
  var key = this.nameToKeyMap[name];
1159
+
964
1160
  if (!(key in this.values)) {
965
1161
  return this.defaults[key];
966
1162
  }
1163
+
967
1164
  return this.values[key];
968
1165
  },
969
1166
  removeByName: function CFFDict_removeByName(name) {
970
1167
  delete this.values[this.nameToKeyMap[name]];
971
1168
  }
972
1169
  };
1170
+
973
1171
  CFFDict.createTables = function CFFDict_createTables(layout) {
974
1172
  var tables = {
975
1173
  keyToNameMap: {},
@@ -979,6 +1177,7 @@ var CFFDict = function CFFDictClosure() {
979
1177
  opcodes: {},
980
1178
  order: []
981
1179
  };
1180
+
982
1181
  for (var i = 0, ii = layout.length; i < ii; ++i) {
983
1182
  var entry = layout[i];
984
1183
  var key = Array.isArray(entry[0]) ? (entry[0][0] << 8) + entry[0][1] : entry[0];
@@ -989,41 +1188,56 @@ var CFFDict = function CFFDictClosure() {
989
1188
  tables.opcodes[key] = Array.isArray(entry[0]) ? entry[0] : [entry[0]];
990
1189
  tables.order.push(key);
991
1190
  }
1191
+
992
1192
  return tables;
993
1193
  };
1194
+
994
1195
  return CFFDict;
995
1196
  }();
1197
+
996
1198
  var CFFTopDict = function CFFTopDictClosure() {
997
1199
  var layout = [[[12, 30], 'ROS', ['sid', 'sid', 'num'], null], [[12, 20], 'SyntheticBase', 'num', null], [0, 'version', 'sid', null], [1, 'Notice', 'sid', null], [[12, 0], 'Copyright', 'sid', null], [2, 'FullName', 'sid', null], [3, 'FamilyName', 'sid', null], [4, 'Weight', 'sid', null], [[12, 1], 'isFixedPitch', 'num', 0], [[12, 2], 'ItalicAngle', 'num', 0], [[12, 3], 'UnderlinePosition', 'num', -100], [[12, 4], 'UnderlineThickness', 'num', 50], [[12, 5], 'PaintType', 'num', 0], [[12, 6], 'CharstringType', 'num', 2], [[12, 7], 'FontMatrix', ['num', 'num', 'num', 'num', 'num', 'num'], [0.001, 0, 0, 0.001, 0, 0]], [13, 'UniqueID', 'num', null], [5, 'FontBBox', ['num', 'num', 'num', 'num'], [0, 0, 0, 0]], [[12, 8], 'StrokeWidth', 'num', 0], [14, 'XUID', 'array', null], [15, 'charset', 'offset', 0], [16, 'Encoding', 'offset', 0], [17, 'CharStrings', 'offset', 0], [18, 'Private', ['offset', 'offset'], null], [[12, 21], 'PostScript', 'sid', null], [[12, 22], 'BaseFontName', 'sid', null], [[12, 23], 'BaseFontBlend', 'delta', null], [[12, 31], 'CIDFontVersion', 'num', 0], [[12, 32], 'CIDFontRevision', 'num', 0], [[12, 33], 'CIDFontType', 'num', 0], [[12, 34], 'CIDCount', 'num', 8720], [[12, 35], 'UIDBase', 'num', null], [[12, 37], 'FDSelect', 'offset', null], [[12, 36], 'FDArray', 'offset', null], [[12, 38], 'FontName', 'sid', null]];
998
1200
  var tables = null;
1201
+
999
1202
  function CFFTopDict(strings) {
1000
1203
  if (tables === null) {
1001
1204
  tables = CFFDict.createTables(layout);
1002
1205
  }
1206
+
1003
1207
  CFFDict.call(this, tables, strings);
1004
1208
  this.privateDict = null;
1005
1209
  }
1210
+
1006
1211
  CFFTopDict.prototype = Object.create(CFFDict.prototype);
1007
1212
  return CFFTopDict;
1008
1213
  }();
1214
+
1215
+ exports.CFFTopDict = CFFTopDict;
1216
+
1009
1217
  var CFFPrivateDict = function CFFPrivateDictClosure() {
1010
1218
  var layout = [[6, 'BlueValues', 'delta', null], [7, 'OtherBlues', 'delta', null], [8, 'FamilyBlues', 'delta', null], [9, 'FamilyOtherBlues', 'delta', null], [[12, 9], 'BlueScale', 'num', 0.039625], [[12, 10], 'BlueShift', 'num', 7], [[12, 11], 'BlueFuzz', 'num', 1], [10, 'StdHW', 'num', null], [11, 'StdVW', 'num', null], [[12, 12], 'StemSnapH', 'delta', null], [[12, 13], 'StemSnapV', 'delta', null], [[12, 14], 'ForceBold', 'num', 0], [[12, 17], 'LanguageGroup', 'num', 0], [[12, 18], 'ExpansionFactor', 'num', 0.06], [[12, 19], 'initialRandomSeed', 'num', 0], [20, 'defaultWidthX', 'num', 0], [21, 'nominalWidthX', 'num', 0], [19, 'Subrs', 'offset', null]];
1011
1219
  var tables = null;
1220
+
1012
1221
  function CFFPrivateDict(strings) {
1013
1222
  if (tables === null) {
1014
1223
  tables = CFFDict.createTables(layout);
1015
1224
  }
1225
+
1016
1226
  CFFDict.call(this, tables, strings);
1017
1227
  this.subrsIndex = null;
1018
1228
  }
1229
+
1019
1230
  CFFPrivateDict.prototype = Object.create(CFFDict.prototype);
1020
1231
  return CFFPrivateDict;
1021
1232
  }();
1233
+
1234
+ exports.CFFPrivateDict = CFFPrivateDict;
1022
1235
  var CFFCharsetPredefinedTypes = {
1023
1236
  ISO_ADOBE: 0,
1024
1237
  EXPERT: 1,
1025
1238
  EXPERT_SUBSET: 2
1026
1239
  };
1240
+
1027
1241
  var CFFCharset = function CFFCharsetClosure() {
1028
1242
  function CFFCharset(predefined, format, charset, raw) {
1029
1243
  this.predefined = predefined;
@@ -1031,8 +1245,12 @@ var CFFCharset = function CFFCharsetClosure() {
1031
1245
  this.charset = charset;
1032
1246
  this.raw = raw;
1033
1247
  }
1248
+
1034
1249
  return CFFCharset;
1035
1250
  }();
1251
+
1252
+ exports.CFFCharset = CFFCharset;
1253
+
1036
1254
  var CFFEncoding = function CFFEncodingClosure() {
1037
1255
  function CFFEncoding(predefined, format, encoding, raw) {
1038
1256
  this.predefined = predefined;
@@ -1040,35 +1258,44 @@ var CFFEncoding = function CFFEncodingClosure() {
1040
1258
  this.encoding = encoding;
1041
1259
  this.raw = raw;
1042
1260
  }
1261
+
1043
1262
  return CFFEncoding;
1044
1263
  }();
1264
+
1045
1265
  var CFFFDSelect = function CFFFDSelectClosure() {
1046
- function CFFFDSelect(fdSelect, raw) {
1266
+ function CFFFDSelect(format, fdSelect) {
1267
+ this.format = format;
1047
1268
  this.fdSelect = fdSelect;
1048
- this.raw = raw;
1049
1269
  }
1270
+
1050
1271
  CFFFDSelect.prototype = {
1051
1272
  getFDIndex: function CFFFDSelect_get(glyphIndex) {
1052
1273
  if (glyphIndex < 0 || glyphIndex >= this.fdSelect.length) {
1053
1274
  return -1;
1054
1275
  }
1276
+
1055
1277
  return this.fdSelect[glyphIndex];
1056
1278
  }
1057
1279
  };
1058
1280
  return CFFFDSelect;
1059
1281
  }();
1282
+
1283
+ exports.CFFFDSelect = CFFFDSelect;
1284
+
1060
1285
  var CFFOffsetTracker = function CFFOffsetTrackerClosure() {
1061
1286
  function CFFOffsetTracker() {
1062
1287
  this.offsets = Object.create(null);
1063
1288
  }
1289
+
1064
1290
  CFFOffsetTracker.prototype = {
1065
1291
  isTracking: function CFFOffsetTracker_isTracking(key) {
1066
1292
  return key in this.offsets;
1067
1293
  },
1068
1294
  track: function CFFOffsetTracker_track(key, location) {
1069
1295
  if (key in this.offsets) {
1070
- throw new _util.FormatError('Already tracking location of ' + key);
1296
+ throw new _util.FormatError("Already tracking location of ".concat(key));
1071
1297
  }
1298
+
1072
1299
  this.offsets[key] = location;
1073
1300
  },
1074
1301
  offset: function CFFOffsetTracker_offset(value) {
@@ -1078,20 +1305,24 @@ var CFFOffsetTracker = function CFFOffsetTrackerClosure() {
1078
1305
  },
1079
1306
  setEntryLocation: function CFFOffsetTracker_setEntryLocation(key, values, output) {
1080
1307
  if (!(key in this.offsets)) {
1081
- throw new _util.FormatError('Not tracking location of ' + key);
1308
+ throw new _util.FormatError("Not tracking location of ".concat(key));
1082
1309
  }
1310
+
1083
1311
  var data = output.data;
1084
1312
  var dataOffset = this.offsets[key];
1085
1313
  var size = 5;
1314
+
1086
1315
  for (var i = 0, ii = values.length; i < ii; ++i) {
1087
1316
  var offset0 = i * size + dataOffset;
1088
1317
  var offset1 = offset0 + 1;
1089
1318
  var offset2 = offset0 + 2;
1090
1319
  var offset3 = offset0 + 3;
1091
1320
  var offset4 = offset0 + 4;
1321
+
1092
1322
  if (data[offset0] !== 0x1d || data[offset1] !== 0 || data[offset2] !== 0 || data[offset3] !== 0 || data[offset4] !== 0) {
1093
1323
  throw new _util.FormatError('writing to an offset that is not empty');
1094
1324
  }
1325
+
1095
1326
  var value = values[i];
1096
1327
  data[offset0] = 0x1d;
1097
1328
  data[offset1] = value >> 24 & 0xFF;
@@ -1103,10 +1334,12 @@ var CFFOffsetTracker = function CFFOffsetTrackerClosure() {
1103
1334
  };
1104
1335
  return CFFOffsetTracker;
1105
1336
  }();
1337
+
1106
1338
  var CFFCompiler = function CFFCompilerClosure() {
1107
1339
  function CFFCompiler(cff) {
1108
1340
  this.cff = cff;
1109
1341
  }
1342
+
1110
1343
  CFFCompiler.prototype = {
1111
1344
  compile: function CFFCompiler_compile() {
1112
1345
  var cff = this.cff;
@@ -1122,20 +1355,26 @@ var CFFCompiler = function CFFCompilerClosure() {
1122
1355
  output.add(header);
1123
1356
  var nameIndex = this.compileNameIndex(cff.names);
1124
1357
  output.add(nameIndex);
1358
+
1125
1359
  if (cff.isCIDFont) {
1126
1360
  if (cff.topDict.hasName('FontMatrix')) {
1127
1361
  var base = cff.topDict.getByName('FontMatrix');
1128
1362
  cff.topDict.removeByName('FontMatrix');
1363
+
1129
1364
  for (var i = 0, ii = cff.fdArray.length; i < ii; i++) {
1130
1365
  var subDict = cff.fdArray[i];
1131
1366
  var matrix = base.slice(0);
1367
+
1132
1368
  if (subDict.hasName('FontMatrix')) {
1133
1369
  matrix = _util.Util.transform(matrix, subDict.getByName('FontMatrix'));
1134
1370
  }
1371
+
1135
1372
  subDict.setByName('FontMatrix', matrix);
1136
1373
  }
1137
1374
  }
1138
1375
  }
1376
+
1377
+ cff.topDict.setByName('charset', 0);
1139
1378
  var compiled = this.compileTopDicts([cff.topDict], output.length, cff.isCIDFont);
1140
1379
  output.add(compiled.output);
1141
1380
  var topDictTracker = compiled.trackers[0];
@@ -1143,6 +1382,7 @@ var CFFCompiler = function CFFCompilerClosure() {
1143
1382
  output.add(stringIndex);
1144
1383
  var globalSubrIndex = this.compileIndex(cff.globalSubrIndex);
1145
1384
  output.add(globalSubrIndex);
1385
+
1146
1386
  if (cff.encoding && cff.topDict.hasName('Encoding')) {
1147
1387
  if (cff.encoding.predefined) {
1148
1388
  topDictTracker.setEntryLocation('Encoding', [cff.encoding.format], output);
@@ -1152,21 +1392,17 @@ var CFFCompiler = function CFFCompilerClosure() {
1152
1392
  output.add(encoding);
1153
1393
  }
1154
1394
  }
1155
- if (cff.charset && cff.topDict.hasName('charset')) {
1156
- if (cff.charset.predefined) {
1157
- topDictTracker.setEntryLocation('charset', [cff.charset.format], output);
1158
- } else {
1159
- var charset = this.compileCharset(cff.charset);
1160
- topDictTracker.setEntryLocation('charset', [output.length], output);
1161
- output.add(charset);
1162
- }
1163
- }
1395
+
1396
+ var charset = this.compileCharset(cff.charset, cff.charStrings.count, cff.strings, cff.isCIDFont);
1397
+ topDictTracker.setEntryLocation('charset', [output.length], output);
1398
+ output.add(charset);
1164
1399
  var charStrings = this.compileCharStrings(cff.charStrings);
1165
1400
  topDictTracker.setEntryLocation('CharStrings', [output.length], output);
1166
1401
  output.add(charStrings);
1402
+
1167
1403
  if (cff.isCIDFont) {
1168
1404
  topDictTracker.setEntryLocation('FDSelect', [output.length], output);
1169
- var fdSelect = this.compileFDSelect(cff.fdSelect.raw);
1405
+ var fdSelect = this.compileFDSelect(cff.fdSelect);
1170
1406
  output.add(fdSelect);
1171
1407
  compiled = this.compileTopDicts(cff.fdArray, output.length, true);
1172
1408
  topDictTracker.setEntryLocation('FDArray', [output.length], output);
@@ -1174,6 +1410,7 @@ var CFFCompiler = function CFFCompilerClosure() {
1174
1410
  var fontDictTrackers = compiled.trackers;
1175
1411
  this.compilePrivateDicts(cff.fdArray, fontDictTrackers, output);
1176
1412
  }
1413
+
1177
1414
  this.compilePrivateDicts([cff.topDict], [topDictTracker], output);
1178
1415
  output.add([0]);
1179
1416
  return output.data;
@@ -1182,19 +1419,24 @@ var CFFCompiler = function CFFCompilerClosure() {
1182
1419
  if (parseFloat(value) === parseInt(value, 10) && !isNaN(value)) {
1183
1420
  return this.encodeInteger(value);
1184
1421
  }
1422
+
1185
1423
  return this.encodeFloat(value);
1186
1424
  },
1187
1425
  encodeFloat: function CFFCompiler_encodeFloat(num) {
1188
1426
  var value = num.toString();
1189
1427
  var m = /\.(\d*?)(?:9{5,20}|0{5,20})\d{0,2}(?:e(.+)|$)/.exec(value);
1428
+
1190
1429
  if (m) {
1191
1430
  var epsilon = parseFloat('1e' + ((m[2] ? +m[2] : 0) + m[1].length));
1192
1431
  value = (Math.round(num * epsilon) / epsilon).toString();
1193
1432
  }
1433
+
1194
1434
  var nibbles = '';
1195
1435
  var i, ii;
1436
+
1196
1437
  for (i = 0, ii = value.length; i < ii; ++i) {
1197
1438
  var a = value[i];
1439
+
1198
1440
  if (a === 'e') {
1199
1441
  nibbles += value[++i] === '-' ? 'c' : 'b';
1200
1442
  } else if (a === '.') {
@@ -1205,15 +1447,19 @@ var CFFCompiler = function CFFCompilerClosure() {
1205
1447
  nibbles += a;
1206
1448
  }
1207
1449
  }
1450
+
1208
1451
  nibbles += nibbles.length & 1 ? 'f' : 'ff';
1209
1452
  var out = [30];
1453
+
1210
1454
  for (i = 0, ii = nibbles.length; i < ii; i += 2) {
1211
- out.push(parseInt(nibbles.substr(i, 2), 16));
1455
+ out.push(parseInt(nibbles.substring(i, i + 2), 16));
1212
1456
  }
1457
+
1213
1458
  return out;
1214
1459
  },
1215
1460
  encodeInteger: function CFFCompiler_encodeInteger(value) {
1216
1461
  var code;
1462
+
1217
1463
  if (value >= -107 && value <= 107) {
1218
1464
  code = [value + 139];
1219
1465
  } else if (value >= 108 && value <= 1131) {
@@ -1227,6 +1473,7 @@ var CFFCompiler = function CFFCompilerClosure() {
1227
1473
  } else {
1228
1474
  code = [0x1d, value >> 24 & 0xFF, value >> 16 & 0xFF, value >> 8 & 0xFF, value & 0xFF];
1229
1475
  }
1476
+
1230
1477
  return code;
1231
1478
  },
1232
1479
  compileHeader: function CFFCompiler_compileHeader(header) {
@@ -1234,30 +1481,40 @@ var CFFCompiler = function CFFCompilerClosure() {
1234
1481
  },
1235
1482
  compileNameIndex: function CFFCompiler_compileNameIndex(names) {
1236
1483
  var nameIndex = new CFFIndex();
1484
+
1237
1485
  for (var i = 0, ii = names.length; i < ii; ++i) {
1238
1486
  var name = names[i];
1239
1487
  var length = Math.min(name.length, 127);
1240
1488
  var sanitizedName = new Array(length);
1489
+
1241
1490
  for (var j = 0; j < length; j++) {
1242
- var char = name[j];
1243
- if (char < '!' || char > '~' || char === '[' || char === ']' || char === '(' || char === ')' || char === '{' || char === '}' || char === '<' || char === '>' || char === '/' || char === '%') {
1244
- char = '_';
1491
+ var _char = name[j];
1492
+
1493
+ if (_char < '!' || _char > '~' || _char === '[' || _char === ']' || _char === '(' || _char === ')' || _char === '{' || _char === '}' || _char === '<' || _char === '>' || _char === '/' || _char === '%') {
1494
+ _char = '_';
1245
1495
  }
1246
- sanitizedName[j] = char;
1496
+
1497
+ sanitizedName[j] = _char;
1247
1498
  }
1499
+
1248
1500
  sanitizedName = sanitizedName.join('');
1501
+
1249
1502
  if (sanitizedName === '') {
1250
1503
  sanitizedName = 'Bad_Font_Name';
1251
1504
  }
1505
+
1252
1506
  nameIndex.add((0, _util.stringToBytes)(sanitizedName));
1253
1507
  }
1508
+
1254
1509
  return this.compileIndex(nameIndex);
1255
1510
  },
1256
1511
  compileTopDicts: function CFFCompiler_compileTopDicts(dicts, length, removeCidKeys) {
1257
1512
  var fontDictTrackers = [];
1258
1513
  var fdArrayIndex = new CFFIndex();
1514
+
1259
1515
  for (var i = 0, ii = dicts.length; i < ii; ++i) {
1260
1516
  var fontDict = dicts[i];
1517
+
1261
1518
  if (removeCidKeys) {
1262
1519
  fontDict.removeByName('CIDFontVersion');
1263
1520
  fontDict.removeByName('CIDFontRevision');
@@ -1265,12 +1522,14 @@ var CFFCompiler = function CFFCompilerClosure() {
1265
1522
  fontDict.removeByName('CIDCount');
1266
1523
  fontDict.removeByName('UIDBase');
1267
1524
  }
1525
+
1268
1526
  var fontDictTracker = new CFFOffsetTracker();
1269
1527
  var fontDictData = this.compileDict(fontDict, fontDictTracker);
1270
1528
  fontDictTrackers.push(fontDictTracker);
1271
1529
  fdArrayIndex.add(fontDictData);
1272
1530
  fontDictTracker.offset(length);
1273
1531
  }
1532
+
1274
1533
  fdArrayIndex = this.compileIndex(fdArrayIndex, fontDictTrackers);
1275
1534
  return {
1276
1535
  trackers: fontDictTrackers,
@@ -1281,18 +1540,23 @@ var CFFCompiler = function CFFCompilerClosure() {
1281
1540
  for (var i = 0, ii = dicts.length; i < ii; ++i) {
1282
1541
  var fontDict = dicts[i];
1283
1542
  var privateDict = fontDict.privateDict;
1543
+
1284
1544
  if (!privateDict || !fontDict.hasName('Private')) {
1285
1545
  throw new _util.FormatError('There must be a private dictionary.');
1286
1546
  }
1547
+
1287
1548
  var privateDictTracker = new CFFOffsetTracker();
1288
1549
  var privateDictData = this.compileDict(privateDict, privateDictTracker);
1289
1550
  var outputLength = output.length;
1290
1551
  privateDictTracker.offset(outputLength);
1552
+
1291
1553
  if (!privateDictData.length) {
1292
1554
  outputLength = 0;
1293
1555
  }
1556
+
1294
1557
  trackers[i].setEntryLocation('Private', [privateDictData.length, outputLength], output);
1295
1558
  output.add(privateDictData);
1559
+
1296
1560
  if (privateDict.subrsIndex && privateDict.hasName('Subrs')) {
1297
1561
  var subrs = this.compileIndex(privateDict.subrsIndex);
1298
1562
  privateDictTracker.setEntryLocation('Subrs', [privateDictData.length], output);
@@ -1303,57 +1567,76 @@ var CFFCompiler = function CFFCompilerClosure() {
1303
1567
  compileDict: function CFFCompiler_compileDict(dict, offsetTracker) {
1304
1568
  var out = [];
1305
1569
  var order = dict.order;
1570
+
1306
1571
  for (var i = 0; i < order.length; ++i) {
1307
1572
  var key = order[i];
1573
+
1308
1574
  if (!(key in dict.values)) {
1309
1575
  continue;
1310
1576
  }
1577
+
1311
1578
  var values = dict.values[key];
1312
1579
  var types = dict.types[key];
1580
+
1313
1581
  if (!Array.isArray(types)) {
1314
1582
  types = [types];
1315
1583
  }
1584
+
1316
1585
  if (!Array.isArray(values)) {
1317
1586
  values = [values];
1318
1587
  }
1588
+
1319
1589
  if (values.length === 0) {
1320
1590
  continue;
1321
1591
  }
1592
+
1322
1593
  for (var j = 0, jj = types.length; j < jj; ++j) {
1323
1594
  var type = types[j];
1324
1595
  var value = values[j];
1596
+
1325
1597
  switch (type) {
1326
1598
  case 'num':
1327
1599
  case 'sid':
1328
1600
  out = out.concat(this.encodeNumber(value));
1329
1601
  break;
1602
+
1330
1603
  case 'offset':
1331
1604
  var name = dict.keyToNameMap[key];
1605
+
1332
1606
  if (!offsetTracker.isTracking(name)) {
1333
1607
  offsetTracker.track(name, out.length);
1334
1608
  }
1609
+
1335
1610
  out = out.concat([0x1d, 0, 0, 0, 0]);
1336
1611
  break;
1612
+
1337
1613
  case 'array':
1338
1614
  case 'delta':
1339
1615
  out = out.concat(this.encodeNumber(value));
1616
+
1340
1617
  for (var k = 1, kk = values.length; k < kk; ++k) {
1341
1618
  out = out.concat(this.encodeNumber(values[k]));
1342
1619
  }
1620
+
1343
1621
  break;
1622
+
1344
1623
  default:
1345
- throw new _util.FormatError('Unknown data type of ' + type);
1624
+ throw new _util.FormatError("Unknown data type of ".concat(type));
1346
1625
  }
1347
1626
  }
1627
+
1348
1628
  out = out.concat(dict.opcodes[key]);
1349
1629
  }
1630
+
1350
1631
  return out;
1351
1632
  },
1352
1633
  compileStringIndex: function CFFCompiler_compileStringIndex(strings) {
1353
1634
  var stringIndex = new CFFIndex();
1635
+
1354
1636
  for (var i = 0, ii = strings.length; i < ii; ++i) {
1355
1637
  stringIndex.add((0, _util.stringToBytes)(strings[i]));
1356
1638
  }
1639
+
1357
1640
  return this.compileIndex(stringIndex);
1358
1641
  },
1359
1642
  compileGlobalSubrIndex: function CFFCompiler_compileGlobalSubrIndex() {
@@ -1361,38 +1644,129 @@ var CFFCompiler = function CFFCompilerClosure() {
1361
1644
  this.out.writeByteArray(this.compileIndex(globalSubrIndex));
1362
1645
  },
1363
1646
  compileCharStrings: function CFFCompiler_compileCharStrings(charStrings) {
1364
- return this.compileIndex(charStrings);
1647
+ var charStringsIndex = new CFFIndex();
1648
+
1649
+ for (var i = 0; i < charStrings.count; i++) {
1650
+ var glyph = charStrings.get(i);
1651
+
1652
+ if (glyph.length === 0) {
1653
+ charStringsIndex.add(new Uint8Array([0x8B, 0x0E]));
1654
+ continue;
1655
+ }
1656
+
1657
+ charStringsIndex.add(glyph);
1658
+ }
1659
+
1660
+ return this.compileIndex(charStringsIndex);
1365
1661
  },
1366
- compileCharset: function CFFCompiler_compileCharset(charset) {
1367
- return this.compileTypedArray(charset.raw);
1662
+ compileCharset: function CFFCompiler_compileCharset(charset, numGlyphs, strings, isCIDFont) {
1663
+ var out;
1664
+ var numGlyphsLessNotDef = numGlyphs - 1;
1665
+
1666
+ if (isCIDFont) {
1667
+ out = new Uint8Array([2, 0, 0, numGlyphsLessNotDef >> 8 & 0xFF, numGlyphsLessNotDef & 0xFF]);
1668
+ } else {
1669
+ var length = 1 + numGlyphsLessNotDef * 2;
1670
+ out = new Uint8Array(length);
1671
+ out[0] = 0;
1672
+ var charsetIndex = 0;
1673
+ var numCharsets = charset.charset.length;
1674
+ var warned = false;
1675
+
1676
+ for (var i = 1; i < out.length; i += 2) {
1677
+ var sid = 0;
1678
+
1679
+ if (charsetIndex < numCharsets) {
1680
+ var name = charset.charset[charsetIndex++];
1681
+ sid = strings.getSID(name);
1682
+
1683
+ if (sid === -1) {
1684
+ sid = 0;
1685
+
1686
+ if (!warned) {
1687
+ warned = true;
1688
+ (0, _util.warn)("Couldn't find ".concat(name, " in CFF strings"));
1689
+ }
1690
+ }
1691
+ }
1692
+
1693
+ out[i] = sid >> 8 & 0xFF;
1694
+ out[i + 1] = sid & 0xFF;
1695
+ }
1696
+ }
1697
+
1698
+ return this.compileTypedArray(out);
1368
1699
  },
1369
1700
  compileEncoding: function CFFCompiler_compileEncoding(encoding) {
1370
1701
  return this.compileTypedArray(encoding.raw);
1371
1702
  },
1372
1703
  compileFDSelect: function CFFCompiler_compileFDSelect(fdSelect) {
1373
- return this.compileTypedArray(fdSelect);
1704
+ var format = fdSelect.format;
1705
+ var out, i;
1706
+
1707
+ switch (format) {
1708
+ case 0:
1709
+ out = new Uint8Array(1 + fdSelect.fdSelect.length);
1710
+ out[0] = format;
1711
+
1712
+ for (i = 0; i < fdSelect.fdSelect.length; i++) {
1713
+ out[i + 1] = fdSelect.fdSelect[i];
1714
+ }
1715
+
1716
+ break;
1717
+
1718
+ case 3:
1719
+ var start = 0;
1720
+ var lastFD = fdSelect.fdSelect[0];
1721
+ var ranges = [format, 0, 0, start >> 8 & 0xFF, start & 0xFF, lastFD];
1722
+
1723
+ for (i = 1; i < fdSelect.fdSelect.length; i++) {
1724
+ var currentFD = fdSelect.fdSelect[i];
1725
+
1726
+ if (currentFD !== lastFD) {
1727
+ ranges.push(i >> 8 & 0xFF, i & 0xFF, currentFD);
1728
+ lastFD = currentFD;
1729
+ }
1730
+ }
1731
+
1732
+ var numRanges = (ranges.length - 3) / 3;
1733
+ ranges[1] = numRanges >> 8 & 0xFF;
1734
+ ranges[2] = numRanges & 0xFF;
1735
+ ranges.push(i >> 8 & 0xFF, i & 0xFF);
1736
+ out = new Uint8Array(ranges);
1737
+ break;
1738
+ }
1739
+
1740
+ return this.compileTypedArray(out);
1374
1741
  },
1375
1742
  compileTypedArray: function CFFCompiler_compileTypedArray(data) {
1376
1743
  var out = [];
1744
+
1377
1745
  for (var i = 0, ii = data.length; i < ii; ++i) {
1378
1746
  out[i] = data[i];
1379
1747
  }
1748
+
1380
1749
  return out;
1381
1750
  },
1382
1751
  compileIndex: function CFFCompiler_compileIndex(index, trackers) {
1383
1752
  trackers = trackers || [];
1384
1753
  var objects = index.objects;
1385
1754
  var count = objects.length;
1755
+
1386
1756
  if (count === 0) {
1387
1757
  return [0, 0, 0];
1388
1758
  }
1759
+
1389
1760
  var data = [count >> 8 & 0xFF, count & 0xff];
1390
1761
  var lastOffset = 1,
1391
1762
  i;
1763
+
1392
1764
  for (i = 0; i < count; ++i) {
1393
1765
  lastOffset += objects[i].length;
1394
1766
  }
1767
+
1395
1768
  var offsetSize;
1769
+
1396
1770
  if (lastOffset < 0x100) {
1397
1771
  offsetSize = 1;
1398
1772
  } else if (lastOffset < 0x10000) {
@@ -1402,8 +1776,10 @@ var CFFCompiler = function CFFCompilerClosure() {
1402
1776
  } else {
1403
1777
  offsetSize = 4;
1404
1778
  }
1779
+
1405
1780
  data.push(offsetSize);
1406
1781
  var relativeOffset = 1;
1782
+
1407
1783
  for (i = 0; i < count + 1; i++) {
1408
1784
  if (offsetSize === 1) {
1409
1785
  data.push(relativeOffset & 0xFF);
@@ -1414,30 +1790,26 @@ var CFFCompiler = function CFFCompilerClosure() {
1414
1790
  } else {
1415
1791
  data.push(relativeOffset >>> 24 & 0xFF, relativeOffset >> 16 & 0xFF, relativeOffset >> 8 & 0xFF, relativeOffset & 0xFF);
1416
1792
  }
1793
+
1417
1794
  if (objects[i]) {
1418
1795
  relativeOffset += objects[i].length;
1419
1796
  }
1420
1797
  }
1798
+
1421
1799
  for (i = 0; i < count; i++) {
1422
1800
  if (trackers[i]) {
1423
1801
  trackers[i].offset(data.length);
1424
1802
  }
1803
+
1425
1804
  for (var j = 0, jj = objects[i].length; j < jj; j++) {
1426
1805
  data.push(objects[i][j]);
1427
1806
  }
1428
1807
  }
1808
+
1429
1809
  return data;
1430
1810
  }
1431
1811
  };
1432
1812
  return CFFCompiler;
1433
1813
  }();
1434
- exports.CFFStandardStrings = CFFStandardStrings;
1435
- exports.CFFParser = CFFParser;
1436
- exports.CFF = CFF;
1437
- exports.CFFHeader = CFFHeader;
1438
- exports.CFFStrings = CFFStrings;
1439
- exports.CFFIndex = CFFIndex;
1440
- exports.CFFCharset = CFFCharset;
1441
- exports.CFFTopDict = CFFTopDict;
1442
- exports.CFFPrivateDict = CFFPrivateDict;
1814
+
1443
1815
  exports.CFFCompiler = CFFCompiler;