2
0

cff_parser.js 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2017 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. 'use strict';
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.CFFCompiler = exports.CFFPrivateDict = exports.CFFTopDict = exports.CFFCharset = exports.CFFIndex = exports.CFFStrings = exports.CFFHeader = exports.CFF = exports.CFFParser = exports.CFFStandardStrings = undefined;
  27. var _util = require('../shared/util');
  28. var _charsets = require('./charsets');
  29. var _encodings = require('./encodings');
  30. var MAX_SUBR_NESTING = 10;
  31. 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'];
  32. var CFFParser = function CFFParserClosure() {
  33. var CharstringValidationData = [null, {
  34. id: 'hstem',
  35. min: 2,
  36. stackClearing: true,
  37. stem: true
  38. }, null, {
  39. id: 'vstem',
  40. min: 2,
  41. stackClearing: true,
  42. stem: true
  43. }, {
  44. id: 'vmoveto',
  45. min: 1,
  46. stackClearing: true
  47. }, {
  48. id: 'rlineto',
  49. min: 2,
  50. resetStack: true
  51. }, {
  52. id: 'hlineto',
  53. min: 1,
  54. resetStack: true
  55. }, {
  56. id: 'vlineto',
  57. min: 1,
  58. resetStack: true
  59. }, {
  60. id: 'rrcurveto',
  61. min: 6,
  62. resetStack: true
  63. }, null, {
  64. id: 'callsubr',
  65. min: 1,
  66. undefStack: true
  67. }, {
  68. id: 'return',
  69. min: 0,
  70. undefStack: true
  71. }, null, null, {
  72. id: 'endchar',
  73. min: 0,
  74. stackClearing: true
  75. }, null, null, null, {
  76. id: 'hstemhm',
  77. min: 2,
  78. stackClearing: true,
  79. stem: true
  80. }, {
  81. id: 'hintmask',
  82. min: 0,
  83. stackClearing: true
  84. }, {
  85. id: 'cntrmask',
  86. min: 0,
  87. stackClearing: true
  88. }, {
  89. id: 'rmoveto',
  90. min: 2,
  91. stackClearing: true
  92. }, {
  93. id: 'hmoveto',
  94. min: 1,
  95. stackClearing: true
  96. }, {
  97. id: 'vstemhm',
  98. min: 2,
  99. stackClearing: true,
  100. stem: true
  101. }, {
  102. id: 'rcurveline',
  103. min: 8,
  104. resetStack: true
  105. }, {
  106. id: 'rlinecurve',
  107. min: 8,
  108. resetStack: true
  109. }, {
  110. id: 'vvcurveto',
  111. min: 4,
  112. resetStack: true
  113. }, {
  114. id: 'hhcurveto',
  115. min: 4,
  116. resetStack: true
  117. }, null, {
  118. id: 'callgsubr',
  119. min: 1,
  120. undefStack: true
  121. }, {
  122. id: 'vhcurveto',
  123. min: 4,
  124. resetStack: true
  125. }, {
  126. id: 'hvcurveto',
  127. min: 4,
  128. resetStack: true
  129. }];
  130. var CharstringValidationData12 = [null, null, null, {
  131. id: 'and',
  132. min: 2,
  133. stackDelta: -1
  134. }, {
  135. id: 'or',
  136. min: 2,
  137. stackDelta: -1
  138. }, {
  139. id: 'not',
  140. min: 1,
  141. stackDelta: 0
  142. }, null, null, null, {
  143. id: 'abs',
  144. min: 1,
  145. stackDelta: 0
  146. }, {
  147. id: 'add',
  148. min: 2,
  149. stackDelta: -1,
  150. stackFn: function stack_div(stack, index) {
  151. stack[index - 2] = stack[index - 2] + stack[index - 1];
  152. }
  153. }, {
  154. id: 'sub',
  155. min: 2,
  156. stackDelta: -1,
  157. stackFn: function stack_div(stack, index) {
  158. stack[index - 2] = stack[index - 2] - stack[index - 1];
  159. }
  160. }, {
  161. id: 'div',
  162. min: 2,
  163. stackDelta: -1,
  164. stackFn: function stack_div(stack, index) {
  165. stack[index - 2] = stack[index - 2] / stack[index - 1];
  166. }
  167. }, null, {
  168. id: 'neg',
  169. min: 1,
  170. stackDelta: 0,
  171. stackFn: function stack_div(stack, index) {
  172. stack[index - 1] = -stack[index - 1];
  173. }
  174. }, {
  175. id: 'eq',
  176. min: 2,
  177. stackDelta: -1
  178. }, null, null, {
  179. id: 'drop',
  180. min: 1,
  181. stackDelta: -1
  182. }, null, {
  183. id: 'put',
  184. min: 2,
  185. stackDelta: -2
  186. }, {
  187. id: 'get',
  188. min: 1,
  189. stackDelta: 0
  190. }, {
  191. id: 'ifelse',
  192. min: 4,
  193. stackDelta: -3
  194. }, {
  195. id: 'random',
  196. min: 0,
  197. stackDelta: 1
  198. }, {
  199. id: 'mul',
  200. min: 2,
  201. stackDelta: -1,
  202. stackFn: function stack_div(stack, index) {
  203. stack[index - 2] = stack[index - 2] * stack[index - 1];
  204. }
  205. }, null, {
  206. id: 'sqrt',
  207. min: 1,
  208. stackDelta: 0
  209. }, {
  210. id: 'dup',
  211. min: 1,
  212. stackDelta: 1
  213. }, {
  214. id: 'exch',
  215. min: 2,
  216. stackDelta: 0
  217. }, {
  218. id: 'index',
  219. min: 2,
  220. stackDelta: 0
  221. }, {
  222. id: 'roll',
  223. min: 3,
  224. stackDelta: -2
  225. }, null, null, null, {
  226. id: 'hflex',
  227. min: 7,
  228. resetStack: true
  229. }, {
  230. id: 'flex',
  231. min: 13,
  232. resetStack: true
  233. }, {
  234. id: 'hflex1',
  235. min: 9,
  236. resetStack: true
  237. }, {
  238. id: 'flex1',
  239. min: 11,
  240. resetStack: true
  241. }];
  242. function CFFParser(file, properties, seacAnalysisEnabled) {
  243. this.bytes = file.getBytes();
  244. this.properties = properties;
  245. this.seacAnalysisEnabled = !!seacAnalysisEnabled;
  246. }
  247. CFFParser.prototype = {
  248. parse: function CFFParser_parse() {
  249. var properties = this.properties;
  250. var cff = new CFF();
  251. this.cff = cff;
  252. var header = this.parseHeader();
  253. var nameIndex = this.parseIndex(header.endPos);
  254. var topDictIndex = this.parseIndex(nameIndex.endPos);
  255. var stringIndex = this.parseIndex(topDictIndex.endPos);
  256. var globalSubrIndex = this.parseIndex(stringIndex.endPos);
  257. var topDictParsed = this.parseDict(topDictIndex.obj.get(0));
  258. var topDict = this.createDict(CFFTopDict, topDictParsed, cff.strings);
  259. cff.header = header.obj;
  260. cff.names = this.parseNameIndex(nameIndex.obj);
  261. cff.strings = this.parseStringIndex(stringIndex.obj);
  262. cff.topDict = topDict;
  263. cff.globalSubrIndex = globalSubrIndex.obj;
  264. this.parsePrivateDict(cff.topDict);
  265. cff.isCIDFont = topDict.hasName('ROS');
  266. var charStringOffset = topDict.getByName('CharStrings');
  267. var charStringIndex = this.parseIndex(charStringOffset).obj;
  268. var fontMatrix = topDict.getByName('FontMatrix');
  269. if (fontMatrix) {
  270. properties.fontMatrix = fontMatrix;
  271. }
  272. var fontBBox = topDict.getByName('FontBBox');
  273. if (fontBBox) {
  274. properties.ascent = Math.max(fontBBox[3], fontBBox[1]);
  275. properties.descent = Math.min(fontBBox[1], fontBBox[3]);
  276. properties.ascentScaled = true;
  277. }
  278. var charset, encoding;
  279. if (cff.isCIDFont) {
  280. var fdArrayIndex = this.parseIndex(topDict.getByName('FDArray')).obj;
  281. for (var i = 0, ii = fdArrayIndex.count; i < ii; ++i) {
  282. var dictRaw = fdArrayIndex.get(i);
  283. var fontDict = this.createDict(CFFTopDict, this.parseDict(dictRaw), cff.strings);
  284. this.parsePrivateDict(fontDict);
  285. cff.fdArray.push(fontDict);
  286. }
  287. encoding = null;
  288. charset = this.parseCharsets(topDict.getByName('charset'), charStringIndex.count, cff.strings, true);
  289. cff.fdSelect = this.parseFDSelect(topDict.getByName('FDSelect'), charStringIndex.count);
  290. } else {
  291. charset = this.parseCharsets(topDict.getByName('charset'), charStringIndex.count, cff.strings, false);
  292. encoding = this.parseEncoding(topDict.getByName('Encoding'), properties, cff.strings, charset.charset);
  293. }
  294. cff.charset = charset;
  295. cff.encoding = encoding;
  296. var charStringsAndSeacs = this.parseCharStrings({
  297. charStrings: charStringIndex,
  298. localSubrIndex: topDict.privateDict.subrsIndex,
  299. globalSubrIndex: globalSubrIndex.obj,
  300. fdSelect: cff.fdSelect,
  301. fdArray: cff.fdArray,
  302. privateDict: topDict.privateDict
  303. });
  304. cff.charStrings = charStringsAndSeacs.charStrings;
  305. cff.seacs = charStringsAndSeacs.seacs;
  306. cff.widths = charStringsAndSeacs.widths;
  307. return cff;
  308. },
  309. parseHeader: function CFFParser_parseHeader() {
  310. var bytes = this.bytes;
  311. var bytesLength = bytes.length;
  312. var offset = 0;
  313. while (offset < bytesLength && bytes[offset] !== 1) {
  314. ++offset;
  315. }
  316. if (offset >= bytesLength) {
  317. throw new _util.FormatError('Invalid CFF header');
  318. }
  319. if (offset !== 0) {
  320. (0, _util.info)('cff data is shifted');
  321. bytes = bytes.subarray(offset);
  322. this.bytes = bytes;
  323. }
  324. var major = bytes[0];
  325. var minor = bytes[1];
  326. var hdrSize = bytes[2];
  327. var offSize = bytes[3];
  328. var header = new CFFHeader(major, minor, hdrSize, offSize);
  329. return {
  330. obj: header,
  331. endPos: hdrSize
  332. };
  333. },
  334. parseDict: function CFFParser_parseDict(dict) {
  335. var pos = 0;
  336. function parseOperand() {
  337. var value = dict[pos++];
  338. if (value === 30) {
  339. return parseFloatOperand();
  340. } else if (value === 28) {
  341. value = dict[pos++];
  342. value = (value << 24 | dict[pos++] << 16) >> 16;
  343. return value;
  344. } else if (value === 29) {
  345. value = dict[pos++];
  346. value = value << 8 | dict[pos++];
  347. value = value << 8 | dict[pos++];
  348. value = value << 8 | dict[pos++];
  349. return value;
  350. } else if (value >= 32 && value <= 246) {
  351. return value - 139;
  352. } else if (value >= 247 && value <= 250) {
  353. return (value - 247) * 256 + dict[pos++] + 108;
  354. } else if (value >= 251 && value <= 254) {
  355. return -((value - 251) * 256) - dict[pos++] - 108;
  356. }
  357. (0, _util.warn)('CFFParser_parseDict: "' + value + '" is a reserved command.');
  358. return NaN;
  359. }
  360. function parseFloatOperand() {
  361. var str = '';
  362. var eof = 15;
  363. var lookup = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', 'E', 'E-', null, '-'];
  364. var length = dict.length;
  365. while (pos < length) {
  366. var b = dict[pos++];
  367. var b1 = b >> 4;
  368. var b2 = b & 15;
  369. if (b1 === eof) {
  370. break;
  371. }
  372. str += lookup[b1];
  373. if (b2 === eof) {
  374. break;
  375. }
  376. str += lookup[b2];
  377. }
  378. return parseFloat(str);
  379. }
  380. var operands = [];
  381. var entries = [];
  382. pos = 0;
  383. var end = dict.length;
  384. while (pos < end) {
  385. var b = dict[pos];
  386. if (b <= 21) {
  387. if (b === 12) {
  388. b = b << 8 | dict[++pos];
  389. }
  390. entries.push([b, operands]);
  391. operands = [];
  392. ++pos;
  393. } else {
  394. operands.push(parseOperand());
  395. }
  396. }
  397. return entries;
  398. },
  399. parseIndex: function CFFParser_parseIndex(pos) {
  400. var cffIndex = new CFFIndex();
  401. var bytes = this.bytes;
  402. var count = bytes[pos++] << 8 | bytes[pos++];
  403. var offsets = [];
  404. var end = pos;
  405. var i, ii;
  406. if (count !== 0) {
  407. var offsetSize = bytes[pos++];
  408. var startPos = pos + (count + 1) * offsetSize - 1;
  409. for (i = 0, ii = count + 1; i < ii; ++i) {
  410. var offset = 0;
  411. for (var j = 0; j < offsetSize; ++j) {
  412. offset <<= 8;
  413. offset += bytes[pos++];
  414. }
  415. offsets.push(startPos + offset);
  416. }
  417. end = offsets[count];
  418. }
  419. for (i = 0, ii = offsets.length - 1; i < ii; ++i) {
  420. var offsetStart = offsets[i];
  421. var offsetEnd = offsets[i + 1];
  422. cffIndex.add(bytes.subarray(offsetStart, offsetEnd));
  423. }
  424. return {
  425. obj: cffIndex,
  426. endPos: end
  427. };
  428. },
  429. parseNameIndex: function CFFParser_parseNameIndex(index) {
  430. var names = [];
  431. for (var i = 0, ii = index.count; i < ii; ++i) {
  432. var name = index.get(i);
  433. names.push((0, _util.bytesToString)(name));
  434. }
  435. return names;
  436. },
  437. parseStringIndex: function CFFParser_parseStringIndex(index) {
  438. var strings = new CFFStrings();
  439. for (var i = 0, ii = index.count; i < ii; ++i) {
  440. var data = index.get(i);
  441. strings.add((0, _util.bytesToString)(data));
  442. }
  443. return strings;
  444. },
  445. createDict: function CFFParser_createDict(Type, dict, strings) {
  446. var cffDict = new Type(strings);
  447. for (var i = 0, ii = dict.length; i < ii; ++i) {
  448. var pair = dict[i];
  449. var key = pair[0];
  450. var value = pair[1];
  451. cffDict.setByKey(key, value);
  452. }
  453. return cffDict;
  454. },
  455. parseCharString: function CFFParser_parseCharString(state, data, localSubrIndex, globalSubrIndex) {
  456. if (!data || state.callDepth > MAX_SUBR_NESTING) {
  457. return false;
  458. }
  459. var stackSize = state.stackSize;
  460. var stack = state.stack;
  461. var length = data.length;
  462. for (var j = 0; j < length;) {
  463. var value = data[j++];
  464. var validationCommand = null;
  465. if (value === 12) {
  466. var q = data[j++];
  467. if (q === 0) {
  468. data[j - 2] = 139;
  469. data[j - 1] = 22;
  470. stackSize = 0;
  471. } else {
  472. validationCommand = CharstringValidationData12[q];
  473. }
  474. } else if (value === 28) {
  475. stack[stackSize] = (data[j] << 24 | data[j + 1] << 16) >> 16;
  476. j += 2;
  477. stackSize++;
  478. } else if (value === 14) {
  479. if (stackSize >= 4) {
  480. stackSize -= 4;
  481. if (this.seacAnalysisEnabled) {
  482. state.seac = stack.slice(stackSize, stackSize + 4);
  483. return false;
  484. }
  485. }
  486. validationCommand = CharstringValidationData[value];
  487. } else if (value >= 32 && value <= 246) {
  488. stack[stackSize] = value - 139;
  489. stackSize++;
  490. } else if (value >= 247 && value <= 254) {
  491. stack[stackSize] = value < 251 ? (value - 247 << 8) + data[j] + 108 : -(value - 251 << 8) - data[j] - 108;
  492. j++;
  493. stackSize++;
  494. } else if (value === 255) {
  495. stack[stackSize] = (data[j] << 24 | data[j + 1] << 16 | data[j + 2] << 8 | data[j + 3]) / 65536;
  496. j += 4;
  497. stackSize++;
  498. } else if (value === 19 || value === 20) {
  499. state.hints += stackSize >> 1;
  500. j += state.hints + 7 >> 3;
  501. stackSize %= 2;
  502. validationCommand = CharstringValidationData[value];
  503. } else if (value === 10 || value === 29) {
  504. var subrsIndex;
  505. if (value === 10) {
  506. subrsIndex = localSubrIndex;
  507. } else {
  508. subrsIndex = globalSubrIndex;
  509. }
  510. if (!subrsIndex) {
  511. validationCommand = CharstringValidationData[value];
  512. (0, _util.warn)('Missing subrsIndex for ' + validationCommand.id);
  513. return false;
  514. }
  515. var bias = 32768;
  516. if (subrsIndex.count < 1240) {
  517. bias = 107;
  518. } else if (subrsIndex.count < 33900) {
  519. bias = 1131;
  520. }
  521. var subrNumber = stack[--stackSize] + bias;
  522. if (subrNumber < 0 || subrNumber >= subrsIndex.count || isNaN(subrNumber)) {
  523. validationCommand = CharstringValidationData[value];
  524. (0, _util.warn)('Out of bounds subrIndex for ' + validationCommand.id);
  525. return false;
  526. }
  527. state.stackSize = stackSize;
  528. state.callDepth++;
  529. var valid = this.parseCharString(state, subrsIndex.get(subrNumber), localSubrIndex, globalSubrIndex);
  530. if (!valid) {
  531. return false;
  532. }
  533. state.callDepth--;
  534. stackSize = state.stackSize;
  535. continue;
  536. } else if (value === 11) {
  537. state.stackSize = stackSize;
  538. return true;
  539. } else {
  540. validationCommand = CharstringValidationData[value];
  541. }
  542. if (validationCommand) {
  543. if (validationCommand.stem) {
  544. state.hints += stackSize >> 1;
  545. }
  546. if ('min' in validationCommand) {
  547. if (!state.undefStack && stackSize < validationCommand.min) {
  548. (0, _util.warn)('Not enough parameters for ' + validationCommand.id + '; actual: ' + stackSize + ', expected: ' + validationCommand.min);
  549. return false;
  550. }
  551. }
  552. if (state.firstStackClearing && validationCommand.stackClearing) {
  553. state.firstStackClearing = false;
  554. stackSize -= validationCommand.min;
  555. if (stackSize >= 2 && validationCommand.stem) {
  556. stackSize %= 2;
  557. } else if (stackSize > 1) {
  558. (0, _util.warn)('Found too many parameters for stack-clearing command');
  559. }
  560. if (stackSize > 0 && stack[stackSize - 1] >= 0) {
  561. state.width = stack[stackSize - 1];
  562. }
  563. }
  564. if ('stackDelta' in validationCommand) {
  565. if ('stackFn' in validationCommand) {
  566. validationCommand.stackFn(stack, stackSize);
  567. }
  568. stackSize += validationCommand.stackDelta;
  569. } else if (validationCommand.stackClearing) {
  570. stackSize = 0;
  571. } else if (validationCommand.resetStack) {
  572. stackSize = 0;
  573. state.undefStack = false;
  574. } else if (validationCommand.undefStack) {
  575. stackSize = 0;
  576. state.undefStack = true;
  577. state.firstStackClearing = false;
  578. }
  579. }
  580. }
  581. state.stackSize = stackSize;
  582. return true;
  583. },
  584. parseCharStrings: function parseCharStrings(_ref) {
  585. var charStrings = _ref.charStrings,
  586. localSubrIndex = _ref.localSubrIndex,
  587. globalSubrIndex = _ref.globalSubrIndex,
  588. fdSelect = _ref.fdSelect,
  589. fdArray = _ref.fdArray,
  590. privateDict = _ref.privateDict;
  591. var seacs = [];
  592. var widths = [];
  593. var count = charStrings.count;
  594. for (var i = 0; i < count; i++) {
  595. var charstring = charStrings.get(i);
  596. var state = {
  597. callDepth: 0,
  598. stackSize: 0,
  599. stack: [],
  600. undefStack: true,
  601. hints: 0,
  602. firstStackClearing: true,
  603. seac: null,
  604. width: null
  605. };
  606. var valid = true;
  607. var localSubrToUse = null;
  608. var privateDictToUse = privateDict;
  609. if (fdSelect && fdArray.length) {
  610. var fdIndex = fdSelect.getFDIndex(i);
  611. if (fdIndex === -1) {
  612. (0, _util.warn)('Glyph index is not in fd select.');
  613. valid = false;
  614. }
  615. if (fdIndex >= fdArray.length) {
  616. (0, _util.warn)('Invalid fd index for glyph index.');
  617. valid = false;
  618. }
  619. if (valid) {
  620. privateDictToUse = fdArray[fdIndex].privateDict;
  621. localSubrToUse = privateDictToUse.subrsIndex;
  622. }
  623. } else if (localSubrIndex) {
  624. localSubrToUse = localSubrIndex;
  625. }
  626. if (valid) {
  627. valid = this.parseCharString(state, charstring, localSubrToUse, globalSubrIndex);
  628. }
  629. if (state.width !== null) {
  630. var nominalWidth = privateDictToUse.getByName('nominalWidthX');
  631. widths[i] = nominalWidth + state.width;
  632. } else {
  633. var defaultWidth = privateDictToUse.getByName('defaultWidthX');
  634. widths[i] = defaultWidth;
  635. }
  636. if (state.seac !== null) {
  637. seacs[i] = state.seac;
  638. }
  639. if (!valid) {
  640. charStrings.set(i, new Uint8Array([14]));
  641. }
  642. }
  643. return {
  644. charStrings: charStrings,
  645. seacs: seacs,
  646. widths: widths
  647. };
  648. },
  649. emptyPrivateDictionary: function CFFParser_emptyPrivateDictionary(parentDict) {
  650. var privateDict = this.createDict(CFFPrivateDict, [], parentDict.strings);
  651. parentDict.setByKey(18, [0, 0]);
  652. parentDict.privateDict = privateDict;
  653. },
  654. parsePrivateDict: function CFFParser_parsePrivateDict(parentDict) {
  655. if (!parentDict.hasName('Private')) {
  656. this.emptyPrivateDictionary(parentDict);
  657. return;
  658. }
  659. var privateOffset = parentDict.getByName('Private');
  660. if (!Array.isArray(privateOffset) || privateOffset.length !== 2) {
  661. parentDict.removeByName('Private');
  662. return;
  663. }
  664. var size = privateOffset[0];
  665. var offset = privateOffset[1];
  666. if (size === 0 || offset >= this.bytes.length) {
  667. this.emptyPrivateDictionary(parentDict);
  668. return;
  669. }
  670. var privateDictEnd = offset + size;
  671. var dictData = this.bytes.subarray(offset, privateDictEnd);
  672. var dict = this.parseDict(dictData);
  673. var privateDict = this.createDict(CFFPrivateDict, dict, parentDict.strings);
  674. parentDict.privateDict = privateDict;
  675. if (!privateDict.getByName('Subrs')) {
  676. return;
  677. }
  678. var subrsOffset = privateDict.getByName('Subrs');
  679. var relativeOffset = offset + subrsOffset;
  680. if (subrsOffset === 0 || relativeOffset >= this.bytes.length) {
  681. this.emptyPrivateDictionary(parentDict);
  682. return;
  683. }
  684. var subrsIndex = this.parseIndex(relativeOffset);
  685. privateDict.subrsIndex = subrsIndex.obj;
  686. },
  687. parseCharsets: function CFFParser_parseCharsets(pos, length, strings, cid) {
  688. if (pos === 0) {
  689. return new CFFCharset(true, CFFCharsetPredefinedTypes.ISO_ADOBE, _charsets.ISOAdobeCharset);
  690. } else if (pos === 1) {
  691. return new CFFCharset(true, CFFCharsetPredefinedTypes.EXPERT, _charsets.ExpertCharset);
  692. } else if (pos === 2) {
  693. return new CFFCharset(true, CFFCharsetPredefinedTypes.EXPERT_SUBSET, _charsets.ExpertSubsetCharset);
  694. }
  695. var bytes = this.bytes;
  696. var start = pos;
  697. var format = bytes[pos++];
  698. var charset = ['.notdef'];
  699. var id, count, i;
  700. length -= 1;
  701. switch (format) {
  702. case 0:
  703. for (i = 0; i < length; i++) {
  704. id = bytes[pos++] << 8 | bytes[pos++];
  705. charset.push(cid ? id : strings.get(id));
  706. }
  707. break;
  708. case 1:
  709. while (charset.length <= length) {
  710. id = bytes[pos++] << 8 | bytes[pos++];
  711. count = bytes[pos++];
  712. for (i = 0; i <= count; i++) {
  713. charset.push(cid ? id++ : strings.get(id++));
  714. }
  715. }
  716. break;
  717. case 2:
  718. while (charset.length <= length) {
  719. id = bytes[pos++] << 8 | bytes[pos++];
  720. count = bytes[pos++] << 8 | bytes[pos++];
  721. for (i = 0; i <= count; i++) {
  722. charset.push(cid ? id++ : strings.get(id++));
  723. }
  724. }
  725. break;
  726. default:
  727. throw new _util.FormatError('Unknown charset format');
  728. }
  729. var end = pos;
  730. var raw = bytes.subarray(start, end);
  731. return new CFFCharset(false, format, charset, raw);
  732. },
  733. parseEncoding: function CFFParser_parseEncoding(pos, properties, strings, charset) {
  734. var encoding = Object.create(null);
  735. var bytes = this.bytes;
  736. var predefined = false;
  737. var format, i, ii;
  738. var raw = null;
  739. function readSupplement() {
  740. var supplementsCount = bytes[pos++];
  741. for (i = 0; i < supplementsCount; i++) {
  742. var code = bytes[pos++];
  743. var sid = (bytes[pos++] << 8) + (bytes[pos++] & 0xff);
  744. encoding[code] = charset.indexOf(strings.get(sid));
  745. }
  746. }
  747. if (pos === 0 || pos === 1) {
  748. predefined = true;
  749. format = pos;
  750. var baseEncoding = pos ? _encodings.ExpertEncoding : _encodings.StandardEncoding;
  751. for (i = 0, ii = charset.length; i < ii; i++) {
  752. var index = baseEncoding.indexOf(charset[i]);
  753. if (index !== -1) {
  754. encoding[index] = i;
  755. }
  756. }
  757. } else {
  758. var dataStart = pos;
  759. format = bytes[pos++];
  760. switch (format & 0x7f) {
  761. case 0:
  762. var glyphsCount = bytes[pos++];
  763. for (i = 1; i <= glyphsCount; i++) {
  764. encoding[bytes[pos++]] = i;
  765. }
  766. break;
  767. case 1:
  768. var rangesCount = bytes[pos++];
  769. var gid = 1;
  770. for (i = 0; i < rangesCount; i++) {
  771. var start = bytes[pos++];
  772. var left = bytes[pos++];
  773. for (var j = start; j <= start + left; j++) {
  774. encoding[j] = gid++;
  775. }
  776. }
  777. break;
  778. default:
  779. throw new _util.FormatError('Unknown encoding format: ' + format + ' in CFF');
  780. }
  781. var dataEnd = pos;
  782. if (format & 0x80) {
  783. bytes[dataStart] &= 0x7f;
  784. readSupplement();
  785. }
  786. raw = bytes.subarray(dataStart, dataEnd);
  787. }
  788. format = format & 0x7f;
  789. return new CFFEncoding(predefined, format, encoding, raw);
  790. },
  791. parseFDSelect: function CFFParser_parseFDSelect(pos, length) {
  792. var start = pos;
  793. var bytes = this.bytes;
  794. var format = bytes[pos++];
  795. var fdSelect = [],
  796. rawBytes;
  797. var i,
  798. invalidFirstGID = false;
  799. switch (format) {
  800. case 0:
  801. for (i = 0; i < length; ++i) {
  802. var id = bytes[pos++];
  803. fdSelect.push(id);
  804. }
  805. rawBytes = bytes.subarray(start, pos);
  806. break;
  807. case 3:
  808. var rangesCount = bytes[pos++] << 8 | bytes[pos++];
  809. for (i = 0; i < rangesCount; ++i) {
  810. var first = bytes[pos++] << 8 | bytes[pos++];
  811. if (i === 0 && first !== 0) {
  812. (0, _util.warn)('parseFDSelect: The first range must have a first GID of 0' + ' -- trying to recover.');
  813. invalidFirstGID = true;
  814. first = 0;
  815. }
  816. var fdIndex = bytes[pos++];
  817. var next = bytes[pos] << 8 | bytes[pos + 1];
  818. for (var j = first; j < next; ++j) {
  819. fdSelect.push(fdIndex);
  820. }
  821. }
  822. pos += 2;
  823. rawBytes = bytes.subarray(start, pos);
  824. if (invalidFirstGID) {
  825. rawBytes[3] = rawBytes[4] = 0;
  826. }
  827. break;
  828. default:
  829. throw new _util.FormatError('parseFDSelect: Unknown format "' + format + '".');
  830. }
  831. if (fdSelect.length !== length) {
  832. throw new _util.FormatError('parseFDSelect: Invalid font data.');
  833. }
  834. return new CFFFDSelect(fdSelect, rawBytes);
  835. }
  836. };
  837. return CFFParser;
  838. }();
  839. var CFF = function CFFClosure() {
  840. function CFF() {
  841. this.header = null;
  842. this.names = [];
  843. this.topDict = null;
  844. this.strings = new CFFStrings();
  845. this.globalSubrIndex = null;
  846. this.encoding = null;
  847. this.charset = null;
  848. this.charStrings = null;
  849. this.fdArray = [];
  850. this.fdSelect = null;
  851. this.isCIDFont = false;
  852. }
  853. return CFF;
  854. }();
  855. var CFFHeader = function CFFHeaderClosure() {
  856. function CFFHeader(major, minor, hdrSize, offSize) {
  857. this.major = major;
  858. this.minor = minor;
  859. this.hdrSize = hdrSize;
  860. this.offSize = offSize;
  861. }
  862. return CFFHeader;
  863. }();
  864. var CFFStrings = function CFFStringsClosure() {
  865. function CFFStrings() {
  866. this.strings = [];
  867. }
  868. CFFStrings.prototype = {
  869. get: function CFFStrings_get(index) {
  870. if (index >= 0 && index <= 390) {
  871. return CFFStandardStrings[index];
  872. }
  873. if (index - 391 <= this.strings.length) {
  874. return this.strings[index - 391];
  875. }
  876. return CFFStandardStrings[0];
  877. },
  878. add: function CFFStrings_add(value) {
  879. this.strings.push(value);
  880. },
  881. get count() {
  882. return this.strings.length;
  883. }
  884. };
  885. return CFFStrings;
  886. }();
  887. var CFFIndex = function CFFIndexClosure() {
  888. function CFFIndex() {
  889. this.objects = [];
  890. this.length = 0;
  891. }
  892. CFFIndex.prototype = {
  893. add: function CFFIndex_add(data) {
  894. this.length += data.length;
  895. this.objects.push(data);
  896. },
  897. set: function CFFIndex_set(index, data) {
  898. this.length += data.length - this.objects[index].length;
  899. this.objects[index] = data;
  900. },
  901. get: function CFFIndex_get(index) {
  902. return this.objects[index];
  903. },
  904. get count() {
  905. return this.objects.length;
  906. }
  907. };
  908. return CFFIndex;
  909. }();
  910. var CFFDict = function CFFDictClosure() {
  911. function CFFDict(tables, strings) {
  912. this.keyToNameMap = tables.keyToNameMap;
  913. this.nameToKeyMap = tables.nameToKeyMap;
  914. this.defaults = tables.defaults;
  915. this.types = tables.types;
  916. this.opcodes = tables.opcodes;
  917. this.order = tables.order;
  918. this.strings = strings;
  919. this.values = Object.create(null);
  920. }
  921. CFFDict.prototype = {
  922. setByKey: function CFFDict_setByKey(key, value) {
  923. if (!(key in this.keyToNameMap)) {
  924. return false;
  925. }
  926. var valueLength = value.length;
  927. if (valueLength === 0) {
  928. return true;
  929. }
  930. for (var i = 0; i < valueLength; i++) {
  931. if (isNaN(value[i])) {
  932. (0, _util.warn)('Invalid CFFDict value: "' + value + '" for key "' + key + '".');
  933. return true;
  934. }
  935. }
  936. var type = this.types[key];
  937. if (type === 'num' || type === 'sid' || type === 'offset') {
  938. value = value[0];
  939. }
  940. this.values[key] = value;
  941. return true;
  942. },
  943. setByName: function CFFDict_setByName(name, value) {
  944. if (!(name in this.nameToKeyMap)) {
  945. throw new _util.FormatError('Invalid dictionary name "' + name + '"');
  946. }
  947. this.values[this.nameToKeyMap[name]] = value;
  948. },
  949. hasName: function CFFDict_hasName(name) {
  950. return this.nameToKeyMap[name] in this.values;
  951. },
  952. getByName: function CFFDict_getByName(name) {
  953. if (!(name in this.nameToKeyMap)) {
  954. throw new _util.FormatError('Invalid dictionary name ' + name + '"');
  955. }
  956. var key = this.nameToKeyMap[name];
  957. if (!(key in this.values)) {
  958. return this.defaults[key];
  959. }
  960. return this.values[key];
  961. },
  962. removeByName: function CFFDict_removeByName(name) {
  963. delete this.values[this.nameToKeyMap[name]];
  964. }
  965. };
  966. CFFDict.createTables = function CFFDict_createTables(layout) {
  967. var tables = {
  968. keyToNameMap: {},
  969. nameToKeyMap: {},
  970. defaults: {},
  971. types: {},
  972. opcodes: {},
  973. order: []
  974. };
  975. for (var i = 0, ii = layout.length; i < ii; ++i) {
  976. var entry = layout[i];
  977. var key = Array.isArray(entry[0]) ? (entry[0][0] << 8) + entry[0][1] : entry[0];
  978. tables.keyToNameMap[key] = entry[1];
  979. tables.nameToKeyMap[entry[1]] = key;
  980. tables.types[key] = entry[2];
  981. tables.defaults[key] = entry[3];
  982. tables.opcodes[key] = Array.isArray(entry[0]) ? entry[0] : [entry[0]];
  983. tables.order.push(key);
  984. }
  985. return tables;
  986. };
  987. return CFFDict;
  988. }();
  989. var CFFTopDict = function CFFTopDictClosure() {
  990. 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]];
  991. var tables = null;
  992. function CFFTopDict(strings) {
  993. if (tables === null) {
  994. tables = CFFDict.createTables(layout);
  995. }
  996. CFFDict.call(this, tables, strings);
  997. this.privateDict = null;
  998. }
  999. CFFTopDict.prototype = Object.create(CFFDict.prototype);
  1000. return CFFTopDict;
  1001. }();
  1002. var CFFPrivateDict = function CFFPrivateDictClosure() {
  1003. 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]];
  1004. var tables = null;
  1005. function CFFPrivateDict(strings) {
  1006. if (tables === null) {
  1007. tables = CFFDict.createTables(layout);
  1008. }
  1009. CFFDict.call(this, tables, strings);
  1010. this.subrsIndex = null;
  1011. }
  1012. CFFPrivateDict.prototype = Object.create(CFFDict.prototype);
  1013. return CFFPrivateDict;
  1014. }();
  1015. var CFFCharsetPredefinedTypes = {
  1016. ISO_ADOBE: 0,
  1017. EXPERT: 1,
  1018. EXPERT_SUBSET: 2
  1019. };
  1020. var CFFCharset = function CFFCharsetClosure() {
  1021. function CFFCharset(predefined, format, charset, raw) {
  1022. this.predefined = predefined;
  1023. this.format = format;
  1024. this.charset = charset;
  1025. this.raw = raw;
  1026. }
  1027. return CFFCharset;
  1028. }();
  1029. var CFFEncoding = function CFFEncodingClosure() {
  1030. function CFFEncoding(predefined, format, encoding, raw) {
  1031. this.predefined = predefined;
  1032. this.format = format;
  1033. this.encoding = encoding;
  1034. this.raw = raw;
  1035. }
  1036. return CFFEncoding;
  1037. }();
  1038. var CFFFDSelect = function CFFFDSelectClosure() {
  1039. function CFFFDSelect(fdSelect, raw) {
  1040. this.fdSelect = fdSelect;
  1041. this.raw = raw;
  1042. }
  1043. CFFFDSelect.prototype = {
  1044. getFDIndex: function CFFFDSelect_get(glyphIndex) {
  1045. if (glyphIndex < 0 || glyphIndex >= this.fdSelect.length) {
  1046. return -1;
  1047. }
  1048. return this.fdSelect[glyphIndex];
  1049. }
  1050. };
  1051. return CFFFDSelect;
  1052. }();
  1053. var CFFOffsetTracker = function CFFOffsetTrackerClosure() {
  1054. function CFFOffsetTracker() {
  1055. this.offsets = Object.create(null);
  1056. }
  1057. CFFOffsetTracker.prototype = {
  1058. isTracking: function CFFOffsetTracker_isTracking(key) {
  1059. return key in this.offsets;
  1060. },
  1061. track: function CFFOffsetTracker_track(key, location) {
  1062. if (key in this.offsets) {
  1063. throw new _util.FormatError('Already tracking location of ' + key);
  1064. }
  1065. this.offsets[key] = location;
  1066. },
  1067. offset: function CFFOffsetTracker_offset(value) {
  1068. for (var key in this.offsets) {
  1069. this.offsets[key] += value;
  1070. }
  1071. },
  1072. setEntryLocation: function CFFOffsetTracker_setEntryLocation(key, values, output) {
  1073. if (!(key in this.offsets)) {
  1074. throw new _util.FormatError('Not tracking location of ' + key);
  1075. }
  1076. var data = output.data;
  1077. var dataOffset = this.offsets[key];
  1078. var size = 5;
  1079. for (var i = 0, ii = values.length; i < ii; ++i) {
  1080. var offset0 = i * size + dataOffset;
  1081. var offset1 = offset0 + 1;
  1082. var offset2 = offset0 + 2;
  1083. var offset3 = offset0 + 3;
  1084. var offset4 = offset0 + 4;
  1085. if (data[offset0] !== 0x1d || data[offset1] !== 0 || data[offset2] !== 0 || data[offset3] !== 0 || data[offset4] !== 0) {
  1086. throw new _util.FormatError('writing to an offset that is not empty');
  1087. }
  1088. var value = values[i];
  1089. data[offset0] = 0x1d;
  1090. data[offset1] = value >> 24 & 0xFF;
  1091. data[offset2] = value >> 16 & 0xFF;
  1092. data[offset3] = value >> 8 & 0xFF;
  1093. data[offset4] = value & 0xFF;
  1094. }
  1095. }
  1096. };
  1097. return CFFOffsetTracker;
  1098. }();
  1099. var CFFCompiler = function CFFCompilerClosure() {
  1100. function CFFCompiler(cff) {
  1101. this.cff = cff;
  1102. }
  1103. CFFCompiler.prototype = {
  1104. compile: function CFFCompiler_compile() {
  1105. var cff = this.cff;
  1106. var output = {
  1107. data: [],
  1108. length: 0,
  1109. add: function CFFCompiler_add(data) {
  1110. this.data = this.data.concat(data);
  1111. this.length = this.data.length;
  1112. }
  1113. };
  1114. var header = this.compileHeader(cff.header);
  1115. output.add(header);
  1116. var nameIndex = this.compileNameIndex(cff.names);
  1117. output.add(nameIndex);
  1118. if (cff.isCIDFont) {
  1119. if (cff.topDict.hasName('FontMatrix')) {
  1120. var base = cff.topDict.getByName('FontMatrix');
  1121. cff.topDict.removeByName('FontMatrix');
  1122. for (var i = 0, ii = cff.fdArray.length; i < ii; i++) {
  1123. var subDict = cff.fdArray[i];
  1124. var matrix = base.slice(0);
  1125. if (subDict.hasName('FontMatrix')) {
  1126. matrix = _util.Util.transform(matrix, subDict.getByName('FontMatrix'));
  1127. }
  1128. subDict.setByName('FontMatrix', matrix);
  1129. }
  1130. }
  1131. }
  1132. var compiled = this.compileTopDicts([cff.topDict], output.length, cff.isCIDFont);
  1133. output.add(compiled.output);
  1134. var topDictTracker = compiled.trackers[0];
  1135. var stringIndex = this.compileStringIndex(cff.strings.strings);
  1136. output.add(stringIndex);
  1137. var globalSubrIndex = this.compileIndex(cff.globalSubrIndex);
  1138. output.add(globalSubrIndex);
  1139. if (cff.encoding && cff.topDict.hasName('Encoding')) {
  1140. if (cff.encoding.predefined) {
  1141. topDictTracker.setEntryLocation('Encoding', [cff.encoding.format], output);
  1142. } else {
  1143. var encoding = this.compileEncoding(cff.encoding);
  1144. topDictTracker.setEntryLocation('Encoding', [output.length], output);
  1145. output.add(encoding);
  1146. }
  1147. }
  1148. if (cff.charset && cff.topDict.hasName('charset')) {
  1149. if (cff.charset.predefined) {
  1150. topDictTracker.setEntryLocation('charset', [cff.charset.format], output);
  1151. } else {
  1152. var charset = this.compileCharset(cff.charset);
  1153. topDictTracker.setEntryLocation('charset', [output.length], output);
  1154. output.add(charset);
  1155. }
  1156. }
  1157. var charStrings = this.compileCharStrings(cff.charStrings);
  1158. topDictTracker.setEntryLocation('CharStrings', [output.length], output);
  1159. output.add(charStrings);
  1160. if (cff.isCIDFont) {
  1161. topDictTracker.setEntryLocation('FDSelect', [output.length], output);
  1162. var fdSelect = this.compileFDSelect(cff.fdSelect.raw);
  1163. output.add(fdSelect);
  1164. compiled = this.compileTopDicts(cff.fdArray, output.length, true);
  1165. topDictTracker.setEntryLocation('FDArray', [output.length], output);
  1166. output.add(compiled.output);
  1167. var fontDictTrackers = compiled.trackers;
  1168. this.compilePrivateDicts(cff.fdArray, fontDictTrackers, output);
  1169. }
  1170. this.compilePrivateDicts([cff.topDict], [topDictTracker], output);
  1171. output.add([0]);
  1172. return output.data;
  1173. },
  1174. encodeNumber: function CFFCompiler_encodeNumber(value) {
  1175. if (parseFloat(value) === parseInt(value, 10) && !isNaN(value)) {
  1176. return this.encodeInteger(value);
  1177. }
  1178. return this.encodeFloat(value);
  1179. },
  1180. encodeFloat: function CFFCompiler_encodeFloat(num) {
  1181. var value = num.toString();
  1182. var m = /\.(\d*?)(?:9{5,20}|0{5,20})\d{0,2}(?:e(.+)|$)/.exec(value);
  1183. if (m) {
  1184. var epsilon = parseFloat('1e' + ((m[2] ? +m[2] : 0) + m[1].length));
  1185. value = (Math.round(num * epsilon) / epsilon).toString();
  1186. }
  1187. var nibbles = '';
  1188. var i, ii;
  1189. for (i = 0, ii = value.length; i < ii; ++i) {
  1190. var a = value[i];
  1191. if (a === 'e') {
  1192. nibbles += value[++i] === '-' ? 'c' : 'b';
  1193. } else if (a === '.') {
  1194. nibbles += 'a';
  1195. } else if (a === '-') {
  1196. nibbles += 'e';
  1197. } else {
  1198. nibbles += a;
  1199. }
  1200. }
  1201. nibbles += nibbles.length & 1 ? 'f' : 'ff';
  1202. var out = [30];
  1203. for (i = 0, ii = nibbles.length; i < ii; i += 2) {
  1204. out.push(parseInt(nibbles.substr(i, 2), 16));
  1205. }
  1206. return out;
  1207. },
  1208. encodeInteger: function CFFCompiler_encodeInteger(value) {
  1209. var code;
  1210. if (value >= -107 && value <= 107) {
  1211. code = [value + 139];
  1212. } else if (value >= 108 && value <= 1131) {
  1213. value = value - 108;
  1214. code = [(value >> 8) + 247, value & 0xFF];
  1215. } else if (value >= -1131 && value <= -108) {
  1216. value = -value - 108;
  1217. code = [(value >> 8) + 251, value & 0xFF];
  1218. } else if (value >= -32768 && value <= 32767) {
  1219. code = [0x1c, value >> 8 & 0xFF, value & 0xFF];
  1220. } else {
  1221. code = [0x1d, value >> 24 & 0xFF, value >> 16 & 0xFF, value >> 8 & 0xFF, value & 0xFF];
  1222. }
  1223. return code;
  1224. },
  1225. compileHeader: function CFFCompiler_compileHeader(header) {
  1226. return [header.major, header.minor, header.hdrSize, header.offSize];
  1227. },
  1228. compileNameIndex: function CFFCompiler_compileNameIndex(names) {
  1229. var nameIndex = new CFFIndex();
  1230. for (var i = 0, ii = names.length; i < ii; ++i) {
  1231. var name = names[i];
  1232. var length = Math.min(name.length, 127);
  1233. var sanitizedName = new Array(length);
  1234. for (var j = 0; j < length; j++) {
  1235. var char = name[j];
  1236. if (char < '!' || char > '~' || char === '[' || char === ']' || char === '(' || char === ')' || char === '{' || char === '}' || char === '<' || char === '>' || char === '/' || char === '%') {
  1237. char = '_';
  1238. }
  1239. sanitizedName[j] = char;
  1240. }
  1241. sanitizedName = sanitizedName.join('');
  1242. if (sanitizedName === '') {
  1243. sanitizedName = 'Bad_Font_Name';
  1244. }
  1245. nameIndex.add((0, _util.stringToBytes)(sanitizedName));
  1246. }
  1247. return this.compileIndex(nameIndex);
  1248. },
  1249. compileTopDicts: function CFFCompiler_compileTopDicts(dicts, length, removeCidKeys) {
  1250. var fontDictTrackers = [];
  1251. var fdArrayIndex = new CFFIndex();
  1252. for (var i = 0, ii = dicts.length; i < ii; ++i) {
  1253. var fontDict = dicts[i];
  1254. if (removeCidKeys) {
  1255. fontDict.removeByName('CIDFontVersion');
  1256. fontDict.removeByName('CIDFontRevision');
  1257. fontDict.removeByName('CIDFontType');
  1258. fontDict.removeByName('CIDCount');
  1259. fontDict.removeByName('UIDBase');
  1260. }
  1261. var fontDictTracker = new CFFOffsetTracker();
  1262. var fontDictData = this.compileDict(fontDict, fontDictTracker);
  1263. fontDictTrackers.push(fontDictTracker);
  1264. fdArrayIndex.add(fontDictData);
  1265. fontDictTracker.offset(length);
  1266. }
  1267. fdArrayIndex = this.compileIndex(fdArrayIndex, fontDictTrackers);
  1268. return {
  1269. trackers: fontDictTrackers,
  1270. output: fdArrayIndex
  1271. };
  1272. },
  1273. compilePrivateDicts: function CFFCompiler_compilePrivateDicts(dicts, trackers, output) {
  1274. for (var i = 0, ii = dicts.length; i < ii; ++i) {
  1275. var fontDict = dicts[i];
  1276. var privateDict = fontDict.privateDict;
  1277. if (!privateDict || !fontDict.hasName('Private')) {
  1278. throw new _util.FormatError('There must be a private dictionary.');
  1279. }
  1280. var privateDictTracker = new CFFOffsetTracker();
  1281. var privateDictData = this.compileDict(privateDict, privateDictTracker);
  1282. var outputLength = output.length;
  1283. privateDictTracker.offset(outputLength);
  1284. if (!privateDictData.length) {
  1285. outputLength = 0;
  1286. }
  1287. trackers[i].setEntryLocation('Private', [privateDictData.length, outputLength], output);
  1288. output.add(privateDictData);
  1289. if (privateDict.subrsIndex && privateDict.hasName('Subrs')) {
  1290. var subrs = this.compileIndex(privateDict.subrsIndex);
  1291. privateDictTracker.setEntryLocation('Subrs', [privateDictData.length], output);
  1292. output.add(subrs);
  1293. }
  1294. }
  1295. },
  1296. compileDict: function CFFCompiler_compileDict(dict, offsetTracker) {
  1297. var out = [];
  1298. var order = dict.order;
  1299. for (var i = 0; i < order.length; ++i) {
  1300. var key = order[i];
  1301. if (!(key in dict.values)) {
  1302. continue;
  1303. }
  1304. var values = dict.values[key];
  1305. var types = dict.types[key];
  1306. if (!Array.isArray(types)) {
  1307. types = [types];
  1308. }
  1309. if (!Array.isArray(values)) {
  1310. values = [values];
  1311. }
  1312. if (values.length === 0) {
  1313. continue;
  1314. }
  1315. for (var j = 0, jj = types.length; j < jj; ++j) {
  1316. var type = types[j];
  1317. var value = values[j];
  1318. switch (type) {
  1319. case 'num':
  1320. case 'sid':
  1321. out = out.concat(this.encodeNumber(value));
  1322. break;
  1323. case 'offset':
  1324. var name = dict.keyToNameMap[key];
  1325. if (!offsetTracker.isTracking(name)) {
  1326. offsetTracker.track(name, out.length);
  1327. }
  1328. out = out.concat([0x1d, 0, 0, 0, 0]);
  1329. break;
  1330. case 'array':
  1331. case 'delta':
  1332. out = out.concat(this.encodeNumber(value));
  1333. for (var k = 1, kk = values.length; k < kk; ++k) {
  1334. out = out.concat(this.encodeNumber(values[k]));
  1335. }
  1336. break;
  1337. default:
  1338. throw new _util.FormatError('Unknown data type of ' + type);
  1339. }
  1340. }
  1341. out = out.concat(dict.opcodes[key]);
  1342. }
  1343. return out;
  1344. },
  1345. compileStringIndex: function CFFCompiler_compileStringIndex(strings) {
  1346. var stringIndex = new CFFIndex();
  1347. for (var i = 0, ii = strings.length; i < ii; ++i) {
  1348. stringIndex.add((0, _util.stringToBytes)(strings[i]));
  1349. }
  1350. return this.compileIndex(stringIndex);
  1351. },
  1352. compileGlobalSubrIndex: function CFFCompiler_compileGlobalSubrIndex() {
  1353. var globalSubrIndex = this.cff.globalSubrIndex;
  1354. this.out.writeByteArray(this.compileIndex(globalSubrIndex));
  1355. },
  1356. compileCharStrings: function CFFCompiler_compileCharStrings(charStrings) {
  1357. return this.compileIndex(charStrings);
  1358. },
  1359. compileCharset: function CFFCompiler_compileCharset(charset) {
  1360. return this.compileTypedArray(charset.raw);
  1361. },
  1362. compileEncoding: function CFFCompiler_compileEncoding(encoding) {
  1363. return this.compileTypedArray(encoding.raw);
  1364. },
  1365. compileFDSelect: function CFFCompiler_compileFDSelect(fdSelect) {
  1366. return this.compileTypedArray(fdSelect);
  1367. },
  1368. compileTypedArray: function CFFCompiler_compileTypedArray(data) {
  1369. var out = [];
  1370. for (var i = 0, ii = data.length; i < ii; ++i) {
  1371. out[i] = data[i];
  1372. }
  1373. return out;
  1374. },
  1375. compileIndex: function CFFCompiler_compileIndex(index, trackers) {
  1376. trackers = trackers || [];
  1377. var objects = index.objects;
  1378. var count = objects.length;
  1379. if (count === 0) {
  1380. return [0, 0, 0];
  1381. }
  1382. var data = [count >> 8 & 0xFF, count & 0xff];
  1383. var lastOffset = 1,
  1384. i;
  1385. for (i = 0; i < count; ++i) {
  1386. lastOffset += objects[i].length;
  1387. }
  1388. var offsetSize;
  1389. if (lastOffset < 0x100) {
  1390. offsetSize = 1;
  1391. } else if (lastOffset < 0x10000) {
  1392. offsetSize = 2;
  1393. } else if (lastOffset < 0x1000000) {
  1394. offsetSize = 3;
  1395. } else {
  1396. offsetSize = 4;
  1397. }
  1398. data.push(offsetSize);
  1399. var relativeOffset = 1;
  1400. for (i = 0; i < count + 1; i++) {
  1401. if (offsetSize === 1) {
  1402. data.push(relativeOffset & 0xFF);
  1403. } else if (offsetSize === 2) {
  1404. data.push(relativeOffset >> 8 & 0xFF, relativeOffset & 0xFF);
  1405. } else if (offsetSize === 3) {
  1406. data.push(relativeOffset >> 16 & 0xFF, relativeOffset >> 8 & 0xFF, relativeOffset & 0xFF);
  1407. } else {
  1408. data.push(relativeOffset >>> 24 & 0xFF, relativeOffset >> 16 & 0xFF, relativeOffset >> 8 & 0xFF, relativeOffset & 0xFF);
  1409. }
  1410. if (objects[i]) {
  1411. relativeOffset += objects[i].length;
  1412. }
  1413. }
  1414. for (i = 0; i < count; i++) {
  1415. if (trackers[i]) {
  1416. trackers[i].offset(data.length);
  1417. }
  1418. for (var j = 0, jj = objects[i].length; j < jj; j++) {
  1419. data.push(objects[i][j]);
  1420. }
  1421. }
  1422. return data;
  1423. }
  1424. };
  1425. return CFFCompiler;
  1426. }();
  1427. exports.CFFStandardStrings = CFFStandardStrings;
  1428. exports.CFFParser = CFFParser;
  1429. exports.CFF = CFF;
  1430. exports.CFFHeader = CFFHeader;
  1431. exports.CFFStrings = CFFStrings;
  1432. exports.CFFIndex = CFFIndex;
  1433. exports.CFFCharset = CFFCharset;
  1434. exports.CFFTopDict = CFFTopDict;
  1435. exports.CFFPrivateDict = CFFPrivateDict;
  1436. exports.CFFCompiler = CFFCompiler;