cff_parser.js 56 KB

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