cff_parser.js 51 KB

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