2
0

cff_parser.js 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  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. throw new _util.FormatError('Invalid CFF header');
  304. }
  305. if (offset !== 0) {
  306. (0, _util.info)('cff data is shifted');
  307. bytes = bytes.subarray(offset);
  308. this.bytes = bytes;
  309. }
  310. var major = bytes[0];
  311. var minor = bytes[1];
  312. var hdrSize = bytes[2];
  313. var offSize = bytes[3];
  314. var header = new CFFHeader(major, minor, hdrSize, offSize);
  315. return {
  316. obj: header,
  317. endPos: hdrSize
  318. };
  319. },
  320. parseDict: function CFFParser_parseDict(dict) {
  321. var pos = 0;
  322. function parseOperand() {
  323. var value = dict[pos++];
  324. if (value === 30) {
  325. return parseFloatOperand();
  326. } else if (value === 28) {
  327. value = dict[pos++];
  328. value = (value << 24 | dict[pos++] << 16) >> 16;
  329. return value;
  330. } else if (value === 29) {
  331. value = dict[pos++];
  332. value = value << 8 | dict[pos++];
  333. value = value << 8 | dict[pos++];
  334. value = value << 8 | dict[pos++];
  335. return value;
  336. } else if (value >= 32 && value <= 246) {
  337. return value - 139;
  338. } else if (value >= 247 && value <= 250) {
  339. return (value - 247) * 256 + dict[pos++] + 108;
  340. } else if (value >= 251 && value <= 254) {
  341. return -((value - 251) * 256) - dict[pos++] - 108;
  342. }
  343. (0, _util.warn)('CFFParser_parseDict: "' + value + '" is a reserved command.');
  344. return NaN;
  345. }
  346. function parseFloatOperand() {
  347. var str = '';
  348. var eof = 15;
  349. var lookup = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', 'E', 'E-', null, '-'];
  350. var length = dict.length;
  351. while (pos < length) {
  352. var b = dict[pos++];
  353. var b1 = b >> 4;
  354. var b2 = b & 15;
  355. if (b1 === eof) {
  356. break;
  357. }
  358. str += lookup[b1];
  359. if (b2 === eof) {
  360. break;
  361. }
  362. str += lookup[b2];
  363. }
  364. return parseFloat(str);
  365. }
  366. var operands = [];
  367. var entries = [];
  368. pos = 0;
  369. var end = dict.length;
  370. while (pos < end) {
  371. var b = dict[pos];
  372. if (b <= 21) {
  373. if (b === 12) {
  374. b = b << 8 | dict[++pos];
  375. }
  376. entries.push([b, operands]);
  377. operands = [];
  378. ++pos;
  379. } else {
  380. operands.push(parseOperand());
  381. }
  382. }
  383. return entries;
  384. },
  385. parseIndex: function CFFParser_parseIndex(pos) {
  386. var cffIndex = new CFFIndex();
  387. var bytes = this.bytes;
  388. var count = bytes[pos++] << 8 | bytes[pos++];
  389. var offsets = [];
  390. var end = pos;
  391. var i, ii;
  392. if (count !== 0) {
  393. var offsetSize = bytes[pos++];
  394. var startPos = pos + (count + 1) * offsetSize - 1;
  395. for (i = 0, ii = count + 1; i < ii; ++i) {
  396. var offset = 0;
  397. for (var j = 0; j < offsetSize; ++j) {
  398. offset <<= 8;
  399. offset += bytes[pos++];
  400. }
  401. offsets.push(startPos + offset);
  402. }
  403. end = offsets[count];
  404. }
  405. for (i = 0, ii = offsets.length - 1; i < ii; ++i) {
  406. var offsetStart = offsets[i];
  407. var offsetEnd = offsets[i + 1];
  408. cffIndex.add(bytes.subarray(offsetStart, offsetEnd));
  409. }
  410. return {
  411. obj: cffIndex,
  412. endPos: end
  413. };
  414. },
  415. parseNameIndex: function CFFParser_parseNameIndex(index) {
  416. var names = [];
  417. for (var i = 0, ii = index.count; i < ii; ++i) {
  418. var name = index.get(i);
  419. var length = Math.min(name.length, 127);
  420. var data = [];
  421. for (var j = 0; j < length; ++j) {
  422. var c = name[j];
  423. if (j === 0 && c === 0) {
  424. data[j] = c;
  425. continue;
  426. }
  427. 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) {
  428. data[j] = 95;
  429. continue;
  430. }
  431. data[j] = c;
  432. }
  433. names.push((0, _util.bytesToString)(data));
  434. }
  435. return names;
  436. },
  437. parseStringIndex: function CFFParser_parseStringIndex(index) {
  438. var strings = new CFFStrings();
  439. for (var i = 0, ii = index.count; i < ii; ++i) {
  440. var data = index.get(i);
  441. strings.add((0, _util.bytesToString)(data));
  442. }
  443. return strings;
  444. },
  445. createDict: function CFFParser_createDict(Type, dict, strings) {
  446. var cffDict = new Type(strings);
  447. for (var i = 0, ii = dict.length; i < ii; ++i) {
  448. var pair = dict[i];
  449. var key = pair[0];
  450. var value = pair[1];
  451. cffDict.setByKey(key, value);
  452. }
  453. return cffDict;
  454. },
  455. parseCharString: function CFFParser_parseCharString(state, data, localSubrIndex, globalSubrIndex) {
  456. if (!data || state.callDepth > MAX_SUBR_NESTING) {
  457. return false;
  458. }
  459. var stackSize = state.stackSize;
  460. var stack = state.stack;
  461. var length = data.length;
  462. for (var j = 0; j < length;) {
  463. var value = data[j++];
  464. var validationCommand = null;
  465. if (value === 12) {
  466. var q = data[j++];
  467. if (q === 0) {
  468. data[j - 2] = 139;
  469. data[j - 1] = 22;
  470. stackSize = 0;
  471. } else {
  472. validationCommand = CharstringValidationData12[q];
  473. }
  474. } else if (value === 28) {
  475. stack[stackSize] = (data[j] << 24 | data[j + 1] << 16) >> 16;
  476. j += 2;
  477. stackSize++;
  478. } else if (value === 14) {
  479. if (stackSize >= 4) {
  480. stackSize -= 4;
  481. if (this.seacAnalysisEnabled) {
  482. state.seac = stack.slice(stackSize, stackSize + 4);
  483. return false;
  484. }
  485. }
  486. validationCommand = CharstringValidationData[value];
  487. } else if (value >= 32 && value <= 246) {
  488. stack[stackSize] = value - 139;
  489. stackSize++;
  490. } else if (value >= 247 && value <= 254) {
  491. stack[stackSize] = value < 251 ? (value - 247 << 8) + data[j] + 108 : -(value - 251 << 8) - data[j] - 108;
  492. j++;
  493. stackSize++;
  494. } else if (value === 255) {
  495. stack[stackSize] = (data[j] << 24 | data[j + 1] << 16 | data[j + 2] << 8 | data[j + 3]) / 65536;
  496. j += 4;
  497. stackSize++;
  498. } else if (value === 19 || value === 20) {
  499. state.hints += stackSize >> 1;
  500. j += state.hints + 7 >> 3;
  501. stackSize %= 2;
  502. validationCommand = CharstringValidationData[value];
  503. } else if (value === 10 || value === 29) {
  504. var subrsIndex;
  505. if (value === 10) {
  506. subrsIndex = localSubrIndex;
  507. } else {
  508. subrsIndex = globalSubrIndex;
  509. }
  510. if (!subrsIndex) {
  511. validationCommand = CharstringValidationData[value];
  512. (0, _util.warn)('Missing subrsIndex for ' + validationCommand.id);
  513. return false;
  514. }
  515. var bias = 32768;
  516. if (subrsIndex.count < 1240) {
  517. bias = 107;
  518. } else if (subrsIndex.count < 33900) {
  519. bias = 1131;
  520. }
  521. var subrNumber = stack[--stackSize] + bias;
  522. if (subrNumber < 0 || subrNumber >= subrsIndex.count || isNaN(subrNumber)) {
  523. validationCommand = CharstringValidationData[value];
  524. (0, _util.warn)('Out of bounds subrIndex for ' + validationCommand.id);
  525. return false;
  526. }
  527. state.stackSize = stackSize;
  528. state.callDepth++;
  529. var valid = this.parseCharString(state, subrsIndex.get(subrNumber), localSubrIndex, globalSubrIndex);
  530. if (!valid) {
  531. return false;
  532. }
  533. state.callDepth--;
  534. stackSize = state.stackSize;
  535. continue;
  536. } else if (value === 11) {
  537. state.stackSize = stackSize;
  538. return true;
  539. } else {
  540. validationCommand = CharstringValidationData[value];
  541. }
  542. if (validationCommand) {
  543. if (validationCommand.stem) {
  544. state.hints += stackSize >> 1;
  545. }
  546. if ('min' in validationCommand) {
  547. if (!state.undefStack && stackSize < validationCommand.min) {
  548. (0, _util.warn)('Not enough parameters for ' + validationCommand.id + '; actual: ' + stackSize + ', expected: ' + validationCommand.min);
  549. return false;
  550. }
  551. }
  552. if (state.firstStackClearing && validationCommand.stackClearing) {
  553. state.firstStackClearing = false;
  554. stackSize -= validationCommand.min;
  555. if (stackSize >= 2 && validationCommand.stem) {
  556. stackSize %= 2;
  557. } else if (stackSize > 1) {
  558. (0, _util.warn)('Found too many parameters for stack-clearing command');
  559. }
  560. if (stackSize > 0 && stack[stackSize - 1] >= 0) {
  561. state.width = stack[stackSize - 1];
  562. }
  563. }
  564. if ('stackDelta' in validationCommand) {
  565. if ('stackFn' in validationCommand) {
  566. validationCommand.stackFn(stack, stackSize);
  567. }
  568. stackSize += validationCommand.stackDelta;
  569. } else if (validationCommand.stackClearing) {
  570. stackSize = 0;
  571. } else if (validationCommand.resetStack) {
  572. stackSize = 0;
  573. state.undefStack = false;
  574. } else if (validationCommand.undefStack) {
  575. stackSize = 0;
  576. state.undefStack = true;
  577. state.firstStackClearing = false;
  578. }
  579. }
  580. }
  581. state.stackSize = stackSize;
  582. return true;
  583. },
  584. parseCharStrings: function CFFParser_parseCharStrings(charStrings, localSubrIndex, globalSubrIndex, fdSelect, fdArray) {
  585. var seacs = [];
  586. var widths = [];
  587. var count = charStrings.count;
  588. for (var i = 0; i < count; i++) {
  589. var charstring = charStrings.get(i);
  590. var state = {
  591. callDepth: 0,
  592. stackSize: 0,
  593. stack: [],
  594. undefStack: true,
  595. hints: 0,
  596. firstStackClearing: true,
  597. seac: null,
  598. width: null
  599. };
  600. var valid = true;
  601. var localSubrToUse = null;
  602. if (fdSelect && fdArray.length) {
  603. var fdIndex = fdSelect.getFDIndex(i);
  604. if (fdIndex === -1) {
  605. (0, _util.warn)('Glyph index is not in fd select.');
  606. valid = false;
  607. }
  608. if (fdIndex >= fdArray.length) {
  609. (0, _util.warn)('Invalid fd index for glyph index.');
  610. valid = false;
  611. }
  612. if (valid) {
  613. localSubrToUse = fdArray[fdIndex].privateDict.subrsIndex;
  614. }
  615. } else if (localSubrIndex) {
  616. localSubrToUse = localSubrIndex;
  617. }
  618. if (valid) {
  619. valid = this.parseCharString(state, charstring, localSubrToUse, globalSubrIndex);
  620. }
  621. if (state.width !== null) {
  622. widths[i] = state.width;
  623. }
  624. if (state.seac !== null) {
  625. seacs[i] = state.seac;
  626. }
  627. if (!valid) {
  628. charStrings.set(i, new Uint8Array([14]));
  629. }
  630. }
  631. return {
  632. charStrings: charStrings,
  633. seacs: seacs,
  634. widths: widths
  635. };
  636. },
  637. emptyPrivateDictionary: function CFFParser_emptyPrivateDictionary(parentDict) {
  638. var privateDict = this.createDict(CFFPrivateDict, [], parentDict.strings);
  639. parentDict.setByKey(18, [0, 0]);
  640. parentDict.privateDict = privateDict;
  641. },
  642. parsePrivateDict: function CFFParser_parsePrivateDict(parentDict) {
  643. if (!parentDict.hasName('Private')) {
  644. this.emptyPrivateDictionary(parentDict);
  645. return;
  646. }
  647. var privateOffset = parentDict.getByName('Private');
  648. if (!(0, _util.isArray)(privateOffset) || privateOffset.length !== 2) {
  649. parentDict.removeByName('Private');
  650. return;
  651. }
  652. var size = privateOffset[0];
  653. var offset = privateOffset[1];
  654. if (size === 0 || offset >= this.bytes.length) {
  655. this.emptyPrivateDictionary(parentDict);
  656. return;
  657. }
  658. var privateDictEnd = offset + size;
  659. var dictData = this.bytes.subarray(offset, privateDictEnd);
  660. var dict = this.parseDict(dictData);
  661. var privateDict = this.createDict(CFFPrivateDict, dict, parentDict.strings);
  662. parentDict.privateDict = privateDict;
  663. if (!privateDict.getByName('Subrs')) {
  664. return;
  665. }
  666. var subrsOffset = privateDict.getByName('Subrs');
  667. var relativeOffset = offset + subrsOffset;
  668. if (subrsOffset === 0 || relativeOffset >= this.bytes.length) {
  669. this.emptyPrivateDictionary(parentDict);
  670. return;
  671. }
  672. var subrsIndex = this.parseIndex(relativeOffset);
  673. privateDict.subrsIndex = subrsIndex.obj;
  674. },
  675. parseCharsets: function CFFParser_parseCharsets(pos, length, strings, cid) {
  676. if (pos === 0) {
  677. return new CFFCharset(true, CFFCharsetPredefinedTypes.ISO_ADOBE, _charsets.ISOAdobeCharset);
  678. } else if (pos === 1) {
  679. return new CFFCharset(true, CFFCharsetPredefinedTypes.EXPERT, _charsets.ExpertCharset);
  680. } else if (pos === 2) {
  681. return new CFFCharset(true, CFFCharsetPredefinedTypes.EXPERT_SUBSET, _charsets.ExpertSubsetCharset);
  682. }
  683. var bytes = this.bytes;
  684. var start = pos;
  685. var format = bytes[pos++];
  686. var charset = ['.notdef'];
  687. var id, count, i;
  688. length -= 1;
  689. switch (format) {
  690. case 0:
  691. for (i = 0; i < length; i++) {
  692. id = bytes[pos++] << 8 | bytes[pos++];
  693. charset.push(cid ? id : strings.get(id));
  694. }
  695. break;
  696. case 1:
  697. while (charset.length <= length) {
  698. id = bytes[pos++] << 8 | bytes[pos++];
  699. count = bytes[pos++];
  700. for (i = 0; i <= count; i++) {
  701. charset.push(cid ? id++ : strings.get(id++));
  702. }
  703. }
  704. break;
  705. case 2:
  706. while (charset.length <= length) {
  707. id = bytes[pos++] << 8 | bytes[pos++];
  708. count = bytes[pos++] << 8 | bytes[pos++];
  709. for (i = 0; i <= count; i++) {
  710. charset.push(cid ? id++ : strings.get(id++));
  711. }
  712. }
  713. break;
  714. default:
  715. throw new _util.FormatError('Unknown charset format');
  716. }
  717. var end = pos;
  718. var raw = bytes.subarray(start, end);
  719. return new CFFCharset(false, format, charset, raw);
  720. },
  721. parseEncoding: function CFFParser_parseEncoding(pos, properties, strings, charset) {
  722. var encoding = Object.create(null);
  723. var bytes = this.bytes;
  724. var predefined = false;
  725. var format, i, ii;
  726. var raw = null;
  727. function readSupplement() {
  728. var supplementsCount = bytes[pos++];
  729. for (i = 0; i < supplementsCount; i++) {
  730. var code = bytes[pos++];
  731. var sid = (bytes[pos++] << 8) + (bytes[pos++] & 0xff);
  732. encoding[code] = charset.indexOf(strings.get(sid));
  733. }
  734. }
  735. if (pos === 0 || pos === 1) {
  736. predefined = true;
  737. format = pos;
  738. var baseEncoding = pos ? _encodings.ExpertEncoding : _encodings.StandardEncoding;
  739. for (i = 0, ii = charset.length; i < ii; i++) {
  740. var index = baseEncoding.indexOf(charset[i]);
  741. if (index !== -1) {
  742. encoding[index] = i;
  743. }
  744. }
  745. } else {
  746. var dataStart = pos;
  747. format = bytes[pos++];
  748. switch (format & 0x7f) {
  749. case 0:
  750. var glyphsCount = bytes[pos++];
  751. for (i = 1; i <= glyphsCount; i++) {
  752. encoding[bytes[pos++]] = i;
  753. }
  754. break;
  755. case 1:
  756. var rangesCount = bytes[pos++];
  757. var gid = 1;
  758. for (i = 0; i < rangesCount; i++) {
  759. var start = bytes[pos++];
  760. var left = bytes[pos++];
  761. for (var j = start; j <= start + left; j++) {
  762. encoding[j] = gid++;
  763. }
  764. }
  765. break;
  766. default:
  767. throw new _util.FormatError('Unknown encoding format: ' + format + ' in CFF');
  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. throw new _util.FormatError('parseFDSelect: Unknown format "' + format + '".');
  818. }
  819. (0, _util.assert)(fdSelect.length === length, 'parseFDSelect: Invalid font data.');
  820. return new CFFFDSelect(fdSelect, rawBytes);
  821. }
  822. };
  823. return CFFParser;
  824. }();
  825. var CFF = function CFFClosure() {
  826. function CFF() {
  827. this.header = null;
  828. this.names = [];
  829. this.topDict = null;
  830. this.strings = new CFFStrings();
  831. this.globalSubrIndex = null;
  832. this.encoding = null;
  833. this.charset = null;
  834. this.charStrings = null;
  835. this.fdArray = [];
  836. this.fdSelect = null;
  837. this.isCIDFont = false;
  838. }
  839. return CFF;
  840. }();
  841. var CFFHeader = function CFFHeaderClosure() {
  842. function CFFHeader(major, minor, hdrSize, offSize) {
  843. this.major = major;
  844. this.minor = minor;
  845. this.hdrSize = hdrSize;
  846. this.offSize = offSize;
  847. }
  848. return CFFHeader;
  849. }();
  850. var CFFStrings = function CFFStringsClosure() {
  851. function CFFStrings() {
  852. this.strings = [];
  853. }
  854. CFFStrings.prototype = {
  855. get: function CFFStrings_get(index) {
  856. if (index >= 0 && index <= 390) {
  857. return CFFStandardStrings[index];
  858. }
  859. if (index - 391 <= this.strings.length) {
  860. return this.strings[index - 391];
  861. }
  862. return CFFStandardStrings[0];
  863. },
  864. add: function CFFStrings_add(value) {
  865. this.strings.push(value);
  866. },
  867. get count() {
  868. return this.strings.length;
  869. }
  870. };
  871. return CFFStrings;
  872. }();
  873. var CFFIndex = function CFFIndexClosure() {
  874. function CFFIndex() {
  875. this.objects = [];
  876. this.length = 0;
  877. }
  878. CFFIndex.prototype = {
  879. add: function CFFIndex_add(data) {
  880. this.length += data.length;
  881. this.objects.push(data);
  882. },
  883. set: function CFFIndex_set(index, data) {
  884. this.length += data.length - this.objects[index].length;
  885. this.objects[index] = data;
  886. },
  887. get: function CFFIndex_get(index) {
  888. return this.objects[index];
  889. },
  890. get count() {
  891. return this.objects.length;
  892. }
  893. };
  894. return CFFIndex;
  895. }();
  896. var CFFDict = function CFFDictClosure() {
  897. function CFFDict(tables, strings) {
  898. this.keyToNameMap = tables.keyToNameMap;
  899. this.nameToKeyMap = tables.nameToKeyMap;
  900. this.defaults = tables.defaults;
  901. this.types = tables.types;
  902. this.opcodes = tables.opcodes;
  903. this.order = tables.order;
  904. this.strings = strings;
  905. this.values = Object.create(null);
  906. }
  907. CFFDict.prototype = {
  908. setByKey: function CFFDict_setByKey(key, value) {
  909. if (!(key in this.keyToNameMap)) {
  910. return false;
  911. }
  912. var valueLength = value.length;
  913. if (valueLength === 0) {
  914. return true;
  915. }
  916. for (var i = 0; i < valueLength; i++) {
  917. if (isNaN(value[i])) {
  918. (0, _util.warn)('Invalid CFFDict value: "' + value + '" for key "' + key + '".');
  919. return true;
  920. }
  921. }
  922. var type = this.types[key];
  923. if (type === 'num' || type === 'sid' || type === 'offset') {
  924. value = value[0];
  925. }
  926. this.values[key] = value;
  927. return true;
  928. },
  929. setByName: function CFFDict_setByName(name, value) {
  930. if (!(name in this.nameToKeyMap)) {
  931. throw new _util.FormatError('Invalid dictionary name "' + name + '"');
  932. }
  933. this.values[this.nameToKeyMap[name]] = value;
  934. },
  935. hasName: function CFFDict_hasName(name) {
  936. return this.nameToKeyMap[name] in this.values;
  937. },
  938. getByName: function CFFDict_getByName(name) {
  939. if (!(name in this.nameToKeyMap)) {
  940. throw new _util.FormatError('Invalid dictionary name ' + name + '"');
  941. }
  942. var key = this.nameToKeyMap[name];
  943. if (!(key in this.values)) {
  944. return this.defaults[key];
  945. }
  946. return this.values[key];
  947. },
  948. removeByName: function CFFDict_removeByName(name) {
  949. delete this.values[this.nameToKeyMap[name]];
  950. }
  951. };
  952. CFFDict.createTables = function CFFDict_createTables(layout) {
  953. var tables = {
  954. keyToNameMap: {},
  955. nameToKeyMap: {},
  956. defaults: {},
  957. types: {},
  958. opcodes: {},
  959. order: []
  960. };
  961. for (var i = 0, ii = layout.length; i < ii; ++i) {
  962. var entry = layout[i];
  963. var key = (0, _util.isArray)(entry[0]) ? (entry[0][0] << 8) + entry[0][1] : entry[0];
  964. tables.keyToNameMap[key] = entry[1];
  965. tables.nameToKeyMap[entry[1]] = key;
  966. tables.types[key] = entry[2];
  967. tables.defaults[key] = entry[3];
  968. tables.opcodes[key] = (0, _util.isArray)(entry[0]) ? entry[0] : [entry[0]];
  969. tables.order.push(key);
  970. }
  971. return tables;
  972. };
  973. return CFFDict;
  974. }();
  975. var CFFTopDict = function CFFTopDictClosure() {
  976. 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]];
  977. var tables = null;
  978. function CFFTopDict(strings) {
  979. if (tables === null) {
  980. tables = CFFDict.createTables(layout);
  981. }
  982. CFFDict.call(this, tables, strings);
  983. this.privateDict = null;
  984. }
  985. CFFTopDict.prototype = Object.create(CFFDict.prototype);
  986. return CFFTopDict;
  987. }();
  988. var CFFPrivateDict = function CFFPrivateDictClosure() {
  989. 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]];
  990. var tables = null;
  991. function CFFPrivateDict(strings) {
  992. if (tables === null) {
  993. tables = CFFDict.createTables(layout);
  994. }
  995. CFFDict.call(this, tables, strings);
  996. this.subrsIndex = null;
  997. }
  998. CFFPrivateDict.prototype = Object.create(CFFDict.prototype);
  999. return CFFPrivateDict;
  1000. }();
  1001. var CFFCharsetPredefinedTypes = {
  1002. ISO_ADOBE: 0,
  1003. EXPERT: 1,
  1004. EXPERT_SUBSET: 2
  1005. };
  1006. var CFFCharset = function CFFCharsetClosure() {
  1007. function CFFCharset(predefined, format, charset, raw) {
  1008. this.predefined = predefined;
  1009. this.format = format;
  1010. this.charset = charset;
  1011. this.raw = raw;
  1012. }
  1013. return CFFCharset;
  1014. }();
  1015. var CFFEncoding = function CFFEncodingClosure() {
  1016. function CFFEncoding(predefined, format, encoding, raw) {
  1017. this.predefined = predefined;
  1018. this.format = format;
  1019. this.encoding = encoding;
  1020. this.raw = raw;
  1021. }
  1022. return CFFEncoding;
  1023. }();
  1024. var CFFFDSelect = function CFFFDSelectClosure() {
  1025. function CFFFDSelect(fdSelect, raw) {
  1026. this.fdSelect = fdSelect;
  1027. this.raw = raw;
  1028. }
  1029. CFFFDSelect.prototype = {
  1030. getFDIndex: function CFFFDSelect_get(glyphIndex) {
  1031. if (glyphIndex < 0 || glyphIndex >= this.fdSelect.length) {
  1032. return -1;
  1033. }
  1034. return this.fdSelect[glyphIndex];
  1035. }
  1036. };
  1037. return CFFFDSelect;
  1038. }();
  1039. var CFFOffsetTracker = function CFFOffsetTrackerClosure() {
  1040. function CFFOffsetTracker() {
  1041. this.offsets = Object.create(null);
  1042. }
  1043. CFFOffsetTracker.prototype = {
  1044. isTracking: function CFFOffsetTracker_isTracking(key) {
  1045. return key in this.offsets;
  1046. },
  1047. track: function CFFOffsetTracker_track(key, location) {
  1048. if (key in this.offsets) {
  1049. throw new _util.FormatError('Already tracking location of ' + key);
  1050. }
  1051. this.offsets[key] = location;
  1052. },
  1053. offset: function CFFOffsetTracker_offset(value) {
  1054. for (var key in this.offsets) {
  1055. this.offsets[key] += value;
  1056. }
  1057. },
  1058. setEntryLocation: function CFFOffsetTracker_setEntryLocation(key, values, output) {
  1059. if (!(key in this.offsets)) {
  1060. throw new _util.FormatError('Not tracking location of ' + key);
  1061. }
  1062. var data = output.data;
  1063. var dataOffset = this.offsets[key];
  1064. var size = 5;
  1065. for (var i = 0, ii = values.length; i < ii; ++i) {
  1066. var offset0 = i * size + dataOffset;
  1067. var offset1 = offset0 + 1;
  1068. var offset2 = offset0 + 2;
  1069. var offset3 = offset0 + 3;
  1070. var offset4 = offset0 + 4;
  1071. if (data[offset0] !== 0x1d || data[offset1] !== 0 || data[offset2] !== 0 || data[offset3] !== 0 || data[offset4] !== 0) {
  1072. throw new _util.FormatError('writing to an offset that is not empty');
  1073. }
  1074. var value = values[i];
  1075. data[offset0] = 0x1d;
  1076. data[offset1] = value >> 24 & 0xFF;
  1077. data[offset2] = value >> 16 & 0xFF;
  1078. data[offset3] = value >> 8 & 0xFF;
  1079. data[offset4] = value & 0xFF;
  1080. }
  1081. }
  1082. };
  1083. return CFFOffsetTracker;
  1084. }();
  1085. var CFFCompiler = function CFFCompilerClosure() {
  1086. function CFFCompiler(cff) {
  1087. this.cff = cff;
  1088. }
  1089. CFFCompiler.prototype = {
  1090. compile: function CFFCompiler_compile() {
  1091. var cff = this.cff;
  1092. var output = {
  1093. data: [],
  1094. length: 0,
  1095. add: function CFFCompiler_add(data) {
  1096. this.data = this.data.concat(data);
  1097. this.length = this.data.length;
  1098. }
  1099. };
  1100. var header = this.compileHeader(cff.header);
  1101. output.add(header);
  1102. var nameIndex = this.compileNameIndex(cff.names);
  1103. output.add(nameIndex);
  1104. if (cff.isCIDFont) {
  1105. if (cff.topDict.hasName('FontMatrix')) {
  1106. var base = cff.topDict.getByName('FontMatrix');
  1107. cff.topDict.removeByName('FontMatrix');
  1108. for (var i = 0, ii = cff.fdArray.length; i < ii; i++) {
  1109. var subDict = cff.fdArray[i];
  1110. var matrix = base.slice(0);
  1111. if (subDict.hasName('FontMatrix')) {
  1112. matrix = _util.Util.transform(matrix, subDict.getByName('FontMatrix'));
  1113. }
  1114. subDict.setByName('FontMatrix', matrix);
  1115. }
  1116. }
  1117. }
  1118. var compiled = this.compileTopDicts([cff.topDict], output.length, cff.isCIDFont);
  1119. output.add(compiled.output);
  1120. var topDictTracker = compiled.trackers[0];
  1121. var stringIndex = this.compileStringIndex(cff.strings.strings);
  1122. output.add(stringIndex);
  1123. var globalSubrIndex = this.compileIndex(cff.globalSubrIndex);
  1124. output.add(globalSubrIndex);
  1125. if (cff.encoding && cff.topDict.hasName('Encoding')) {
  1126. if (cff.encoding.predefined) {
  1127. topDictTracker.setEntryLocation('Encoding', [cff.encoding.format], output);
  1128. } else {
  1129. var encoding = this.compileEncoding(cff.encoding);
  1130. topDictTracker.setEntryLocation('Encoding', [output.length], output);
  1131. output.add(encoding);
  1132. }
  1133. }
  1134. if (cff.charset && cff.topDict.hasName('charset')) {
  1135. if (cff.charset.predefined) {
  1136. topDictTracker.setEntryLocation('charset', [cff.charset.format], output);
  1137. } else {
  1138. var charset = this.compileCharset(cff.charset);
  1139. topDictTracker.setEntryLocation('charset', [output.length], output);
  1140. output.add(charset);
  1141. }
  1142. }
  1143. var charStrings = this.compileCharStrings(cff.charStrings);
  1144. topDictTracker.setEntryLocation('CharStrings', [output.length], output);
  1145. output.add(charStrings);
  1146. if (cff.isCIDFont) {
  1147. topDictTracker.setEntryLocation('FDSelect', [output.length], output);
  1148. var fdSelect = this.compileFDSelect(cff.fdSelect.raw);
  1149. output.add(fdSelect);
  1150. compiled = this.compileTopDicts(cff.fdArray, output.length, true);
  1151. topDictTracker.setEntryLocation('FDArray', [output.length], output);
  1152. output.add(compiled.output);
  1153. var fontDictTrackers = compiled.trackers;
  1154. this.compilePrivateDicts(cff.fdArray, fontDictTrackers, output);
  1155. }
  1156. this.compilePrivateDicts([cff.topDict], [topDictTracker], output);
  1157. output.add([0]);
  1158. return output.data;
  1159. },
  1160. encodeNumber: function CFFCompiler_encodeNumber(value) {
  1161. if (parseFloat(value) === parseInt(value, 10) && !isNaN(value)) {
  1162. return this.encodeInteger(value);
  1163. }
  1164. return this.encodeFloat(value);
  1165. },
  1166. encodeFloat: function CFFCompiler_encodeFloat(num) {
  1167. var value = num.toString();
  1168. var m = /\.(\d*?)(?:9{5,20}|0{5,20})\d{0,2}(?:e(.+)|$)/.exec(value);
  1169. if (m) {
  1170. var epsilon = parseFloat('1e' + ((m[2] ? +m[2] : 0) + m[1].length));
  1171. value = (Math.round(num * epsilon) / epsilon).toString();
  1172. }
  1173. var nibbles = '';
  1174. var i, ii;
  1175. for (i = 0, ii = value.length; i < ii; ++i) {
  1176. var a = value[i];
  1177. if (a === 'e') {
  1178. nibbles += value[++i] === '-' ? 'c' : 'b';
  1179. } else if (a === '.') {
  1180. nibbles += 'a';
  1181. } else if (a === '-') {
  1182. nibbles += 'e';
  1183. } else {
  1184. nibbles += a;
  1185. }
  1186. }
  1187. nibbles += nibbles.length & 1 ? 'f' : 'ff';
  1188. var out = [30];
  1189. for (i = 0, ii = nibbles.length; i < ii; i += 2) {
  1190. out.push(parseInt(nibbles.substr(i, 2), 16));
  1191. }
  1192. return out;
  1193. },
  1194. encodeInteger: function CFFCompiler_encodeInteger(value) {
  1195. var code;
  1196. if (value >= -107 && value <= 107) {
  1197. code = [value + 139];
  1198. } else if (value >= 108 && value <= 1131) {
  1199. value = value - 108;
  1200. code = [(value >> 8) + 247, value & 0xFF];
  1201. } else if (value >= -1131 && value <= -108) {
  1202. value = -value - 108;
  1203. code = [(value >> 8) + 251, value & 0xFF];
  1204. } else if (value >= -32768 && value <= 32767) {
  1205. code = [0x1c, value >> 8 & 0xFF, value & 0xFF];
  1206. } else {
  1207. code = [0x1d, value >> 24 & 0xFF, value >> 16 & 0xFF, value >> 8 & 0xFF, value & 0xFF];
  1208. }
  1209. return code;
  1210. },
  1211. compileHeader: function CFFCompiler_compileHeader(header) {
  1212. return [header.major, header.minor, header.hdrSize, header.offSize];
  1213. },
  1214. compileNameIndex: function CFFCompiler_compileNameIndex(names) {
  1215. var nameIndex = new CFFIndex();
  1216. for (var i = 0, ii = names.length; i < ii; ++i) {
  1217. nameIndex.add((0, _util.stringToBytes)(names[i]));
  1218. }
  1219. return this.compileIndex(nameIndex);
  1220. },
  1221. compileTopDicts: function CFFCompiler_compileTopDicts(dicts, length, removeCidKeys) {
  1222. var fontDictTrackers = [];
  1223. var fdArrayIndex = new CFFIndex();
  1224. for (var i = 0, ii = dicts.length; i < ii; ++i) {
  1225. var fontDict = dicts[i];
  1226. if (removeCidKeys) {
  1227. fontDict.removeByName('CIDFontVersion');
  1228. fontDict.removeByName('CIDFontRevision');
  1229. fontDict.removeByName('CIDFontType');
  1230. fontDict.removeByName('CIDCount');
  1231. fontDict.removeByName('UIDBase');
  1232. }
  1233. var fontDictTracker = new CFFOffsetTracker();
  1234. var fontDictData = this.compileDict(fontDict, fontDictTracker);
  1235. fontDictTrackers.push(fontDictTracker);
  1236. fdArrayIndex.add(fontDictData);
  1237. fontDictTracker.offset(length);
  1238. }
  1239. fdArrayIndex = this.compileIndex(fdArrayIndex, fontDictTrackers);
  1240. return {
  1241. trackers: fontDictTrackers,
  1242. output: fdArrayIndex
  1243. };
  1244. },
  1245. compilePrivateDicts: function CFFCompiler_compilePrivateDicts(dicts, trackers, output) {
  1246. for (var i = 0, ii = dicts.length; i < ii; ++i) {
  1247. var fontDict = dicts[i];
  1248. (0, _util.assert)(fontDict.privateDict && fontDict.hasName('Private'), 'There must be an private dictionary.');
  1249. var privateDict = fontDict.privateDict;
  1250. var privateDictTracker = new CFFOffsetTracker();
  1251. var privateDictData = this.compileDict(privateDict, privateDictTracker);
  1252. var outputLength = output.length;
  1253. privateDictTracker.offset(outputLength);
  1254. if (!privateDictData.length) {
  1255. outputLength = 0;
  1256. }
  1257. trackers[i].setEntryLocation('Private', [privateDictData.length, outputLength], output);
  1258. output.add(privateDictData);
  1259. if (privateDict.subrsIndex && privateDict.hasName('Subrs')) {
  1260. var subrs = this.compileIndex(privateDict.subrsIndex);
  1261. privateDictTracker.setEntryLocation('Subrs', [privateDictData.length], output);
  1262. output.add(subrs);
  1263. }
  1264. }
  1265. },
  1266. compileDict: function CFFCompiler_compileDict(dict, offsetTracker) {
  1267. var out = [];
  1268. var order = dict.order;
  1269. for (var i = 0; i < order.length; ++i) {
  1270. var key = order[i];
  1271. if (!(key in dict.values)) {
  1272. continue;
  1273. }
  1274. var values = dict.values[key];
  1275. var types = dict.types[key];
  1276. if (!(0, _util.isArray)(types)) {
  1277. types = [types];
  1278. }
  1279. if (!(0, _util.isArray)(values)) {
  1280. values = [values];
  1281. }
  1282. if (values.length === 0) {
  1283. continue;
  1284. }
  1285. for (var j = 0, jj = types.length; j < jj; ++j) {
  1286. var type = types[j];
  1287. var value = values[j];
  1288. switch (type) {
  1289. case 'num':
  1290. case 'sid':
  1291. out = out.concat(this.encodeNumber(value));
  1292. break;
  1293. case 'offset':
  1294. var name = dict.keyToNameMap[key];
  1295. if (!offsetTracker.isTracking(name)) {
  1296. offsetTracker.track(name, out.length);
  1297. }
  1298. out = out.concat([0x1d, 0, 0, 0, 0]);
  1299. break;
  1300. case 'array':
  1301. case 'delta':
  1302. out = out.concat(this.encodeNumber(value));
  1303. for (var k = 1, kk = values.length; k < kk; ++k) {
  1304. out = out.concat(this.encodeNumber(values[k]));
  1305. }
  1306. break;
  1307. default:
  1308. throw new _util.FormatError('Unknown data type of ' + type);
  1309. }
  1310. }
  1311. out = out.concat(dict.opcodes[key]);
  1312. }
  1313. return out;
  1314. },
  1315. compileStringIndex: function CFFCompiler_compileStringIndex(strings) {
  1316. var stringIndex = new CFFIndex();
  1317. for (var i = 0, ii = strings.length; i < ii; ++i) {
  1318. stringIndex.add((0, _util.stringToBytes)(strings[i]));
  1319. }
  1320. return this.compileIndex(stringIndex);
  1321. },
  1322. compileGlobalSubrIndex: function CFFCompiler_compileGlobalSubrIndex() {
  1323. var globalSubrIndex = this.cff.globalSubrIndex;
  1324. this.out.writeByteArray(this.compileIndex(globalSubrIndex));
  1325. },
  1326. compileCharStrings: function CFFCompiler_compileCharStrings(charStrings) {
  1327. return this.compileIndex(charStrings);
  1328. },
  1329. compileCharset: function CFFCompiler_compileCharset(charset) {
  1330. return this.compileTypedArray(charset.raw);
  1331. },
  1332. compileEncoding: function CFFCompiler_compileEncoding(encoding) {
  1333. return this.compileTypedArray(encoding.raw);
  1334. },
  1335. compileFDSelect: function CFFCompiler_compileFDSelect(fdSelect) {
  1336. return this.compileTypedArray(fdSelect);
  1337. },
  1338. compileTypedArray: function CFFCompiler_compileTypedArray(data) {
  1339. var out = [];
  1340. for (var i = 0, ii = data.length; i < ii; ++i) {
  1341. out[i] = data[i];
  1342. }
  1343. return out;
  1344. },
  1345. compileIndex: function CFFCompiler_compileIndex(index, trackers) {
  1346. trackers = trackers || [];
  1347. var objects = index.objects;
  1348. var count = objects.length;
  1349. if (count === 0) {
  1350. return [0, 0, 0];
  1351. }
  1352. var data = [count >> 8 & 0xFF, count & 0xff];
  1353. var lastOffset = 1,
  1354. i;
  1355. for (i = 0; i < count; ++i) {
  1356. lastOffset += objects[i].length;
  1357. }
  1358. var offsetSize;
  1359. if (lastOffset < 0x100) {
  1360. offsetSize = 1;
  1361. } else if (lastOffset < 0x10000) {
  1362. offsetSize = 2;
  1363. } else if (lastOffset < 0x1000000) {
  1364. offsetSize = 3;
  1365. } else {
  1366. offsetSize = 4;
  1367. }
  1368. data.push(offsetSize);
  1369. var relativeOffset = 1;
  1370. for (i = 0; i < count + 1; i++) {
  1371. if (offsetSize === 1) {
  1372. data.push(relativeOffset & 0xFF);
  1373. } else if (offsetSize === 2) {
  1374. data.push(relativeOffset >> 8 & 0xFF, relativeOffset & 0xFF);
  1375. } else if (offsetSize === 3) {
  1376. data.push(relativeOffset >> 16 & 0xFF, relativeOffset >> 8 & 0xFF, relativeOffset & 0xFF);
  1377. } else {
  1378. data.push(relativeOffset >>> 24 & 0xFF, relativeOffset >> 16 & 0xFF, relativeOffset >> 8 & 0xFF, relativeOffset & 0xFF);
  1379. }
  1380. if (objects[i]) {
  1381. relativeOffset += objects[i].length;
  1382. }
  1383. }
  1384. for (i = 0; i < count; i++) {
  1385. if (trackers[i]) {
  1386. trackers[i].offset(data.length);
  1387. }
  1388. for (var j = 0, jj = objects[i].length; j < jj; j++) {
  1389. data.push(objects[i][j]);
  1390. }
  1391. }
  1392. return data;
  1393. }
  1394. };
  1395. return CFFCompiler;
  1396. }();
  1397. exports.CFFStandardStrings = CFFStandardStrings;
  1398. exports.CFFParser = CFFParser;
  1399. exports.CFF = CFF;
  1400. exports.CFFHeader = CFFHeader;
  1401. exports.CFFStrings = CFFStrings;
  1402. exports.CFFIndex = CFFIndex;
  1403. exports.CFFCharset = CFFCharset;
  1404. exports.CFFTopDict = CFFTopDict;
  1405. exports.CFFPrivateDict = CFFPrivateDict;
  1406. exports.CFFCompiler = CFFCompiler;