2
0

cff_parser.js 52 KB

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