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. names.push((0, _util.bytesToString)(name));
  427. }
  428. return names;
  429. },
  430. parseStringIndex: function CFFParser_parseStringIndex(index) {
  431. var strings = new CFFStrings();
  432. for (var i = 0, ii = index.count; i < ii; ++i) {
  433. var data = index.get(i);
  434. strings.add((0, _util.bytesToString)(data));
  435. }
  436. return strings;
  437. },
  438. createDict: function CFFParser_createDict(Type, dict, strings) {
  439. var cffDict = new Type(strings);
  440. for (var i = 0, ii = dict.length; i < ii; ++i) {
  441. var pair = dict[i];
  442. var key = pair[0];
  443. var value = pair[1];
  444. cffDict.setByKey(key, value);
  445. }
  446. return cffDict;
  447. },
  448. parseCharString: function CFFParser_parseCharString(state, data, localSubrIndex, globalSubrIndex) {
  449. if (!data || state.callDepth > MAX_SUBR_NESTING) {
  450. return false;
  451. }
  452. var stackSize = state.stackSize;
  453. var stack = state.stack;
  454. var length = data.length;
  455. for (var j = 0; j < length;) {
  456. var value = data[j++];
  457. var validationCommand = null;
  458. if (value === 12) {
  459. var q = data[j++];
  460. if (q === 0) {
  461. data[j - 2] = 139;
  462. data[j - 1] = 22;
  463. stackSize = 0;
  464. } else {
  465. validationCommand = CharstringValidationData12[q];
  466. }
  467. } else if (value === 28) {
  468. stack[stackSize] = (data[j] << 24 | data[j + 1] << 16) >> 16;
  469. j += 2;
  470. stackSize++;
  471. } else if (value === 14) {
  472. if (stackSize >= 4) {
  473. stackSize -= 4;
  474. if (this.seacAnalysisEnabled) {
  475. state.seac = stack.slice(stackSize, stackSize + 4);
  476. return false;
  477. }
  478. }
  479. validationCommand = CharstringValidationData[value];
  480. } else if (value >= 32 && value <= 246) {
  481. stack[stackSize] = value - 139;
  482. stackSize++;
  483. } else if (value >= 247 && value <= 254) {
  484. stack[stackSize] = value < 251 ? (value - 247 << 8) + data[j] + 108 : -(value - 251 << 8) - data[j] - 108;
  485. j++;
  486. stackSize++;
  487. } else if (value === 255) {
  488. stack[stackSize] = (data[j] << 24 | data[j + 1] << 16 | data[j + 2] << 8 | data[j + 3]) / 65536;
  489. j += 4;
  490. stackSize++;
  491. } else if (value === 19 || value === 20) {
  492. state.hints += stackSize >> 1;
  493. j += state.hints + 7 >> 3;
  494. stackSize %= 2;
  495. validationCommand = CharstringValidationData[value];
  496. } else if (value === 10 || value === 29) {
  497. var subrsIndex;
  498. if (value === 10) {
  499. subrsIndex = localSubrIndex;
  500. } else {
  501. subrsIndex = globalSubrIndex;
  502. }
  503. if (!subrsIndex) {
  504. validationCommand = CharstringValidationData[value];
  505. (0, _util.warn)('Missing subrsIndex for ' + validationCommand.id);
  506. return false;
  507. }
  508. var bias = 32768;
  509. if (subrsIndex.count < 1240) {
  510. bias = 107;
  511. } else if (subrsIndex.count < 33900) {
  512. bias = 1131;
  513. }
  514. var subrNumber = stack[--stackSize] + bias;
  515. if (subrNumber < 0 || subrNumber >= subrsIndex.count || isNaN(subrNumber)) {
  516. validationCommand = CharstringValidationData[value];
  517. (0, _util.warn)('Out of bounds subrIndex for ' + validationCommand.id);
  518. return false;
  519. }
  520. state.stackSize = stackSize;
  521. state.callDepth++;
  522. var valid = this.parseCharString(state, subrsIndex.get(subrNumber), localSubrIndex, globalSubrIndex);
  523. if (!valid) {
  524. return false;
  525. }
  526. state.callDepth--;
  527. stackSize = state.stackSize;
  528. continue;
  529. } else if (value === 11) {
  530. state.stackSize = stackSize;
  531. return true;
  532. } else {
  533. validationCommand = CharstringValidationData[value];
  534. }
  535. if (validationCommand) {
  536. if (validationCommand.stem) {
  537. state.hints += stackSize >> 1;
  538. }
  539. if ('min' in validationCommand) {
  540. if (!state.undefStack && stackSize < validationCommand.min) {
  541. (0, _util.warn)('Not enough parameters for ' + validationCommand.id + '; actual: ' + stackSize + ', expected: ' + validationCommand.min);
  542. return false;
  543. }
  544. }
  545. if (state.firstStackClearing && validationCommand.stackClearing) {
  546. state.firstStackClearing = false;
  547. stackSize -= validationCommand.min;
  548. if (stackSize >= 2 && validationCommand.stem) {
  549. stackSize %= 2;
  550. } else if (stackSize > 1) {
  551. (0, _util.warn)('Found too many parameters for stack-clearing command');
  552. }
  553. if (stackSize > 0 && stack[stackSize - 1] >= 0) {
  554. state.width = stack[stackSize - 1];
  555. }
  556. }
  557. if ('stackDelta' in validationCommand) {
  558. if ('stackFn' in validationCommand) {
  559. validationCommand.stackFn(stack, stackSize);
  560. }
  561. stackSize += validationCommand.stackDelta;
  562. } else if (validationCommand.stackClearing) {
  563. stackSize = 0;
  564. } else if (validationCommand.resetStack) {
  565. stackSize = 0;
  566. state.undefStack = false;
  567. } else if (validationCommand.undefStack) {
  568. stackSize = 0;
  569. state.undefStack = true;
  570. state.firstStackClearing = false;
  571. }
  572. }
  573. }
  574. state.stackSize = stackSize;
  575. return true;
  576. },
  577. parseCharStrings: function parseCharStrings(_ref) {
  578. var charStrings = _ref.charStrings,
  579. localSubrIndex = _ref.localSubrIndex,
  580. globalSubrIndex = _ref.globalSubrIndex,
  581. fdSelect = _ref.fdSelect,
  582. fdArray = _ref.fdArray,
  583. privateDict = _ref.privateDict;
  584. var seacs = [];
  585. var widths = [];
  586. var count = charStrings.count;
  587. for (var i = 0; i < count; i++) {
  588. var charstring = charStrings.get(i);
  589. var state = {
  590. callDepth: 0,
  591. stackSize: 0,
  592. stack: [],
  593. undefStack: true,
  594. hints: 0,
  595. firstStackClearing: true,
  596. seac: null,
  597. width: null
  598. };
  599. var valid = true;
  600. var localSubrToUse = null;
  601. var privateDictToUse = privateDict;
  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. privateDictToUse = fdArray[fdIndex].privateDict;
  614. localSubrToUse = privateDictToUse.subrsIndex;
  615. }
  616. } else if (localSubrIndex) {
  617. localSubrToUse = localSubrIndex;
  618. }
  619. if (valid) {
  620. valid = this.parseCharString(state, charstring, localSubrToUse, globalSubrIndex);
  621. }
  622. if (state.width !== null) {
  623. var nominalWidth = privateDictToUse.getByName('nominalWidthX');
  624. widths[i] = nominalWidth + state.width;
  625. } else {
  626. var defaultWidth = privateDictToUse.getByName('defaultWidthX');
  627. widths[i] = defaultWidth;
  628. }
  629. if (state.seac !== null) {
  630. seacs[i] = state.seac;
  631. }
  632. if (!valid) {
  633. charStrings.set(i, new Uint8Array([14]));
  634. }
  635. }
  636. return {
  637. charStrings: charStrings,
  638. seacs: seacs,
  639. widths: widths
  640. };
  641. },
  642. emptyPrivateDictionary: function CFFParser_emptyPrivateDictionary(parentDict) {
  643. var privateDict = this.createDict(CFFPrivateDict, [], parentDict.strings);
  644. parentDict.setByKey(18, [0, 0]);
  645. parentDict.privateDict = privateDict;
  646. },
  647. parsePrivateDict: function CFFParser_parsePrivateDict(parentDict) {
  648. if (!parentDict.hasName('Private')) {
  649. this.emptyPrivateDictionary(parentDict);
  650. return;
  651. }
  652. var privateOffset = parentDict.getByName('Private');
  653. if (!Array.isArray(privateOffset) || privateOffset.length !== 2) {
  654. parentDict.removeByName('Private');
  655. return;
  656. }
  657. var size = privateOffset[0];
  658. var offset = privateOffset[1];
  659. if (size === 0 || offset >= this.bytes.length) {
  660. this.emptyPrivateDictionary(parentDict);
  661. return;
  662. }
  663. var privateDictEnd = offset + size;
  664. var dictData = this.bytes.subarray(offset, privateDictEnd);
  665. var dict = this.parseDict(dictData);
  666. var privateDict = this.createDict(CFFPrivateDict, dict, parentDict.strings);
  667. parentDict.privateDict = privateDict;
  668. if (!privateDict.getByName('Subrs')) {
  669. return;
  670. }
  671. var subrsOffset = privateDict.getByName('Subrs');
  672. var relativeOffset = offset + subrsOffset;
  673. if (subrsOffset === 0 || relativeOffset >= this.bytes.length) {
  674. this.emptyPrivateDictionary(parentDict);
  675. return;
  676. }
  677. var subrsIndex = this.parseIndex(relativeOffset);
  678. privateDict.subrsIndex = subrsIndex.obj;
  679. },
  680. parseCharsets: function CFFParser_parseCharsets(pos, length, strings, cid) {
  681. if (pos === 0) {
  682. return new CFFCharset(true, CFFCharsetPredefinedTypes.ISO_ADOBE, _charsets.ISOAdobeCharset);
  683. } else if (pos === 1) {
  684. return new CFFCharset(true, CFFCharsetPredefinedTypes.EXPERT, _charsets.ExpertCharset);
  685. } else if (pos === 2) {
  686. return new CFFCharset(true, CFFCharsetPredefinedTypes.EXPERT_SUBSET, _charsets.ExpertSubsetCharset);
  687. }
  688. var bytes = this.bytes;
  689. var start = pos;
  690. var format = bytes[pos++];
  691. var charset = ['.notdef'];
  692. var id, count, i;
  693. length -= 1;
  694. switch (format) {
  695. case 0:
  696. for (i = 0; i < length; i++) {
  697. id = bytes[pos++] << 8 | bytes[pos++];
  698. charset.push(cid ? id : strings.get(id));
  699. }
  700. break;
  701. case 1:
  702. while (charset.length <= length) {
  703. id = bytes[pos++] << 8 | bytes[pos++];
  704. count = bytes[pos++];
  705. for (i = 0; i <= count; i++) {
  706. charset.push(cid ? id++ : strings.get(id++));
  707. }
  708. }
  709. break;
  710. case 2:
  711. while (charset.length <= length) {
  712. id = bytes[pos++] << 8 | bytes[pos++];
  713. count = bytes[pos++] << 8 | bytes[pos++];
  714. for (i = 0; i <= count; i++) {
  715. charset.push(cid ? id++ : strings.get(id++));
  716. }
  717. }
  718. break;
  719. default:
  720. throw new _util.FormatError('Unknown charset format');
  721. }
  722. var end = pos;
  723. var raw = bytes.subarray(start, end);
  724. return new CFFCharset(false, format, charset, raw);
  725. },
  726. parseEncoding: function CFFParser_parseEncoding(pos, properties, strings, charset) {
  727. var encoding = Object.create(null);
  728. var bytes = this.bytes;
  729. var predefined = false;
  730. var format, i, ii;
  731. var raw = null;
  732. function readSupplement() {
  733. var supplementsCount = bytes[pos++];
  734. for (i = 0; i < supplementsCount; i++) {
  735. var code = bytes[pos++];
  736. var sid = (bytes[pos++] << 8) + (bytes[pos++] & 0xff);
  737. encoding[code] = charset.indexOf(strings.get(sid));
  738. }
  739. }
  740. if (pos === 0 || pos === 1) {
  741. predefined = true;
  742. format = pos;
  743. var baseEncoding = pos ? _encodings.ExpertEncoding : _encodings.StandardEncoding;
  744. for (i = 0, ii = charset.length; i < ii; i++) {
  745. var index = baseEncoding.indexOf(charset[i]);
  746. if (index !== -1) {
  747. encoding[index] = i;
  748. }
  749. }
  750. } else {
  751. var dataStart = pos;
  752. format = bytes[pos++];
  753. switch (format & 0x7f) {
  754. case 0:
  755. var glyphsCount = bytes[pos++];
  756. for (i = 1; i <= glyphsCount; i++) {
  757. encoding[bytes[pos++]] = i;
  758. }
  759. break;
  760. case 1:
  761. var rangesCount = bytes[pos++];
  762. var gid = 1;
  763. for (i = 0; i < rangesCount; i++) {
  764. var start = bytes[pos++];
  765. var left = bytes[pos++];
  766. for (var j = start; j <= start + left; j++) {
  767. encoding[j] = gid++;
  768. }
  769. }
  770. break;
  771. default:
  772. throw new _util.FormatError('Unknown encoding format: ' + format + ' in CFF');
  773. }
  774. var dataEnd = pos;
  775. if (format & 0x80) {
  776. bytes[dataStart] &= 0x7f;
  777. readSupplement();
  778. }
  779. raw = bytes.subarray(dataStart, dataEnd);
  780. }
  781. format = format & 0x7f;
  782. return new CFFEncoding(predefined, format, encoding, raw);
  783. },
  784. parseFDSelect: function CFFParser_parseFDSelect(pos, length) {
  785. var start = pos;
  786. var bytes = this.bytes;
  787. var format = bytes[pos++];
  788. var fdSelect = [],
  789. rawBytes;
  790. var i,
  791. invalidFirstGID = false;
  792. switch (format) {
  793. case 0:
  794. for (i = 0; i < length; ++i) {
  795. var id = bytes[pos++];
  796. fdSelect.push(id);
  797. }
  798. rawBytes = bytes.subarray(start, pos);
  799. break;
  800. case 3:
  801. var rangesCount = bytes[pos++] << 8 | bytes[pos++];
  802. for (i = 0; i < rangesCount; ++i) {
  803. var first = bytes[pos++] << 8 | bytes[pos++];
  804. if (i === 0 && first !== 0) {
  805. (0, _util.warn)('parseFDSelect: The first range must have a first GID of 0' + ' -- trying to recover.');
  806. invalidFirstGID = true;
  807. first = 0;
  808. }
  809. var fdIndex = bytes[pos++];
  810. var next = bytes[pos] << 8 | bytes[pos + 1];
  811. for (var j = first; j < next; ++j) {
  812. fdSelect.push(fdIndex);
  813. }
  814. }
  815. pos += 2;
  816. rawBytes = bytes.subarray(start, pos);
  817. if (invalidFirstGID) {
  818. rawBytes[3] = rawBytes[4] = 0;
  819. }
  820. break;
  821. default:
  822. throw new _util.FormatError('parseFDSelect: Unknown format "' + format + '".');
  823. }
  824. if (fdSelect.length !== length) {
  825. throw new _util.FormatError('parseFDSelect: Invalid font data.');
  826. }
  827. return new CFFFDSelect(fdSelect, rawBytes);
  828. }
  829. };
  830. return CFFParser;
  831. }();
  832. var CFF = function CFFClosure() {
  833. function CFF() {
  834. this.header = null;
  835. this.names = [];
  836. this.topDict = null;
  837. this.strings = new CFFStrings();
  838. this.globalSubrIndex = null;
  839. this.encoding = null;
  840. this.charset = null;
  841. this.charStrings = null;
  842. this.fdArray = [];
  843. this.fdSelect = null;
  844. this.isCIDFont = false;
  845. }
  846. return CFF;
  847. }();
  848. var CFFHeader = function CFFHeaderClosure() {
  849. function CFFHeader(major, minor, hdrSize, offSize) {
  850. this.major = major;
  851. this.minor = minor;
  852. this.hdrSize = hdrSize;
  853. this.offSize = offSize;
  854. }
  855. return CFFHeader;
  856. }();
  857. var CFFStrings = function CFFStringsClosure() {
  858. function CFFStrings() {
  859. this.strings = [];
  860. }
  861. CFFStrings.prototype = {
  862. get: function CFFStrings_get(index) {
  863. if (index >= 0 && index <= 390) {
  864. return CFFStandardStrings[index];
  865. }
  866. if (index - 391 <= this.strings.length) {
  867. return this.strings[index - 391];
  868. }
  869. return CFFStandardStrings[0];
  870. },
  871. add: function CFFStrings_add(value) {
  872. this.strings.push(value);
  873. },
  874. get count() {
  875. return this.strings.length;
  876. }
  877. };
  878. return CFFStrings;
  879. }();
  880. var CFFIndex = function CFFIndexClosure() {
  881. function CFFIndex() {
  882. this.objects = [];
  883. this.length = 0;
  884. }
  885. CFFIndex.prototype = {
  886. add: function CFFIndex_add(data) {
  887. this.length += data.length;
  888. this.objects.push(data);
  889. },
  890. set: function CFFIndex_set(index, data) {
  891. this.length += data.length - this.objects[index].length;
  892. this.objects[index] = data;
  893. },
  894. get: function CFFIndex_get(index) {
  895. return this.objects[index];
  896. },
  897. get count() {
  898. return this.objects.length;
  899. }
  900. };
  901. return CFFIndex;
  902. }();
  903. var CFFDict = function CFFDictClosure() {
  904. function CFFDict(tables, strings) {
  905. this.keyToNameMap = tables.keyToNameMap;
  906. this.nameToKeyMap = tables.nameToKeyMap;
  907. this.defaults = tables.defaults;
  908. this.types = tables.types;
  909. this.opcodes = tables.opcodes;
  910. this.order = tables.order;
  911. this.strings = strings;
  912. this.values = Object.create(null);
  913. }
  914. CFFDict.prototype = {
  915. setByKey: function CFFDict_setByKey(key, value) {
  916. if (!(key in this.keyToNameMap)) {
  917. return false;
  918. }
  919. var valueLength = value.length;
  920. if (valueLength === 0) {
  921. return true;
  922. }
  923. for (var i = 0; i < valueLength; i++) {
  924. if (isNaN(value[i])) {
  925. (0, _util.warn)('Invalid CFFDict value: "' + value + '" for key "' + key + '".');
  926. return true;
  927. }
  928. }
  929. var type = this.types[key];
  930. if (type === 'num' || type === 'sid' || type === 'offset') {
  931. value = value[0];
  932. }
  933. this.values[key] = value;
  934. return true;
  935. },
  936. setByName: function CFFDict_setByName(name, value) {
  937. if (!(name in this.nameToKeyMap)) {
  938. throw new _util.FormatError('Invalid dictionary name "' + name + '"');
  939. }
  940. this.values[this.nameToKeyMap[name]] = value;
  941. },
  942. hasName: function CFFDict_hasName(name) {
  943. return this.nameToKeyMap[name] in this.values;
  944. },
  945. getByName: function CFFDict_getByName(name) {
  946. if (!(name in this.nameToKeyMap)) {
  947. throw new _util.FormatError('Invalid dictionary name ' + name + '"');
  948. }
  949. var key = this.nameToKeyMap[name];
  950. if (!(key in this.values)) {
  951. return this.defaults[key];
  952. }
  953. return this.values[key];
  954. },
  955. removeByName: function CFFDict_removeByName(name) {
  956. delete this.values[this.nameToKeyMap[name]];
  957. }
  958. };
  959. CFFDict.createTables = function CFFDict_createTables(layout) {
  960. var tables = {
  961. keyToNameMap: {},
  962. nameToKeyMap: {},
  963. defaults: {},
  964. types: {},
  965. opcodes: {},
  966. order: []
  967. };
  968. for (var i = 0, ii = layout.length; i < ii; ++i) {
  969. var entry = layout[i];
  970. var key = Array.isArray(entry[0]) ? (entry[0][0] << 8) + entry[0][1] : entry[0];
  971. tables.keyToNameMap[key] = entry[1];
  972. tables.nameToKeyMap[entry[1]] = key;
  973. tables.types[key] = entry[2];
  974. tables.defaults[key] = entry[3];
  975. tables.opcodes[key] = Array.isArray(entry[0]) ? entry[0] : [entry[0]];
  976. tables.order.push(key);
  977. }
  978. return tables;
  979. };
  980. return CFFDict;
  981. }();
  982. var CFFTopDict = function CFFTopDictClosure() {
  983. 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]];
  984. var tables = null;
  985. function CFFTopDict(strings) {
  986. if (tables === null) {
  987. tables = CFFDict.createTables(layout);
  988. }
  989. CFFDict.call(this, tables, strings);
  990. this.privateDict = null;
  991. }
  992. CFFTopDict.prototype = Object.create(CFFDict.prototype);
  993. return CFFTopDict;
  994. }();
  995. var CFFPrivateDict = function CFFPrivateDictClosure() {
  996. 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]];
  997. var tables = null;
  998. function CFFPrivateDict(strings) {
  999. if (tables === null) {
  1000. tables = CFFDict.createTables(layout);
  1001. }
  1002. CFFDict.call(this, tables, strings);
  1003. this.subrsIndex = null;
  1004. }
  1005. CFFPrivateDict.prototype = Object.create(CFFDict.prototype);
  1006. return CFFPrivateDict;
  1007. }();
  1008. var CFFCharsetPredefinedTypes = {
  1009. ISO_ADOBE: 0,
  1010. EXPERT: 1,
  1011. EXPERT_SUBSET: 2
  1012. };
  1013. var CFFCharset = function CFFCharsetClosure() {
  1014. function CFFCharset(predefined, format, charset, raw) {
  1015. this.predefined = predefined;
  1016. this.format = format;
  1017. this.charset = charset;
  1018. this.raw = raw;
  1019. }
  1020. return CFFCharset;
  1021. }();
  1022. var CFFEncoding = function CFFEncodingClosure() {
  1023. function CFFEncoding(predefined, format, encoding, raw) {
  1024. this.predefined = predefined;
  1025. this.format = format;
  1026. this.encoding = encoding;
  1027. this.raw = raw;
  1028. }
  1029. return CFFEncoding;
  1030. }();
  1031. var CFFFDSelect = function CFFFDSelectClosure() {
  1032. function CFFFDSelect(fdSelect, raw) {
  1033. this.fdSelect = fdSelect;
  1034. this.raw = raw;
  1035. }
  1036. CFFFDSelect.prototype = {
  1037. getFDIndex: function CFFFDSelect_get(glyphIndex) {
  1038. if (glyphIndex < 0 || glyphIndex >= this.fdSelect.length) {
  1039. return -1;
  1040. }
  1041. return this.fdSelect[glyphIndex];
  1042. }
  1043. };
  1044. return CFFFDSelect;
  1045. }();
  1046. var CFFOffsetTracker = function CFFOffsetTrackerClosure() {
  1047. function CFFOffsetTracker() {
  1048. this.offsets = Object.create(null);
  1049. }
  1050. CFFOffsetTracker.prototype = {
  1051. isTracking: function CFFOffsetTracker_isTracking(key) {
  1052. return key in this.offsets;
  1053. },
  1054. track: function CFFOffsetTracker_track(key, location) {
  1055. if (key in this.offsets) {
  1056. throw new _util.FormatError('Already tracking location of ' + key);
  1057. }
  1058. this.offsets[key] = location;
  1059. },
  1060. offset: function CFFOffsetTracker_offset(value) {
  1061. for (var key in this.offsets) {
  1062. this.offsets[key] += value;
  1063. }
  1064. },
  1065. setEntryLocation: function CFFOffsetTracker_setEntryLocation(key, values, output) {
  1066. if (!(key in this.offsets)) {
  1067. throw new _util.FormatError('Not tracking location of ' + key);
  1068. }
  1069. var data = output.data;
  1070. var dataOffset = this.offsets[key];
  1071. var size = 5;
  1072. for (var i = 0, ii = values.length; i < ii; ++i) {
  1073. var offset0 = i * size + dataOffset;
  1074. var offset1 = offset0 + 1;
  1075. var offset2 = offset0 + 2;
  1076. var offset3 = offset0 + 3;
  1077. var offset4 = offset0 + 4;
  1078. if (data[offset0] !== 0x1d || data[offset1] !== 0 || data[offset2] !== 0 || data[offset3] !== 0 || data[offset4] !== 0) {
  1079. throw new _util.FormatError('writing to an offset that is not empty');
  1080. }
  1081. var value = values[i];
  1082. data[offset0] = 0x1d;
  1083. data[offset1] = value >> 24 & 0xFF;
  1084. data[offset2] = value >> 16 & 0xFF;
  1085. data[offset3] = value >> 8 & 0xFF;
  1086. data[offset4] = value & 0xFF;
  1087. }
  1088. }
  1089. };
  1090. return CFFOffsetTracker;
  1091. }();
  1092. var CFFCompiler = function CFFCompilerClosure() {
  1093. function CFFCompiler(cff) {
  1094. this.cff = cff;
  1095. }
  1096. CFFCompiler.prototype = {
  1097. compile: function CFFCompiler_compile() {
  1098. var cff = this.cff;
  1099. var output = {
  1100. data: [],
  1101. length: 0,
  1102. add: function CFFCompiler_add(data) {
  1103. this.data = this.data.concat(data);
  1104. this.length = this.data.length;
  1105. }
  1106. };
  1107. var header = this.compileHeader(cff.header);
  1108. output.add(header);
  1109. var nameIndex = this.compileNameIndex(cff.names);
  1110. output.add(nameIndex);
  1111. if (cff.isCIDFont) {
  1112. if (cff.topDict.hasName('FontMatrix')) {
  1113. var base = cff.topDict.getByName('FontMatrix');
  1114. cff.topDict.removeByName('FontMatrix');
  1115. for (var i = 0, ii = cff.fdArray.length; i < ii; i++) {
  1116. var subDict = cff.fdArray[i];
  1117. var matrix = base.slice(0);
  1118. if (subDict.hasName('FontMatrix')) {
  1119. matrix = _util.Util.transform(matrix, subDict.getByName('FontMatrix'));
  1120. }
  1121. subDict.setByName('FontMatrix', matrix);
  1122. }
  1123. }
  1124. }
  1125. var compiled = this.compileTopDicts([cff.topDict], output.length, cff.isCIDFont);
  1126. output.add(compiled.output);
  1127. var topDictTracker = compiled.trackers[0];
  1128. var stringIndex = this.compileStringIndex(cff.strings.strings);
  1129. output.add(stringIndex);
  1130. var globalSubrIndex = this.compileIndex(cff.globalSubrIndex);
  1131. output.add(globalSubrIndex);
  1132. if (cff.encoding && cff.topDict.hasName('Encoding')) {
  1133. if (cff.encoding.predefined) {
  1134. topDictTracker.setEntryLocation('Encoding', [cff.encoding.format], output);
  1135. } else {
  1136. var encoding = this.compileEncoding(cff.encoding);
  1137. topDictTracker.setEntryLocation('Encoding', [output.length], output);
  1138. output.add(encoding);
  1139. }
  1140. }
  1141. if (cff.charset && cff.topDict.hasName('charset')) {
  1142. if (cff.charset.predefined) {
  1143. topDictTracker.setEntryLocation('charset', [cff.charset.format], output);
  1144. } else {
  1145. var charset = this.compileCharset(cff.charset);
  1146. topDictTracker.setEntryLocation('charset', [output.length], output);
  1147. output.add(charset);
  1148. }
  1149. }
  1150. var charStrings = this.compileCharStrings(cff.charStrings);
  1151. topDictTracker.setEntryLocation('CharStrings', [output.length], output);
  1152. output.add(charStrings);
  1153. if (cff.isCIDFont) {
  1154. topDictTracker.setEntryLocation('FDSelect', [output.length], output);
  1155. var fdSelect = this.compileFDSelect(cff.fdSelect.raw);
  1156. output.add(fdSelect);
  1157. compiled = this.compileTopDicts(cff.fdArray, output.length, true);
  1158. topDictTracker.setEntryLocation('FDArray', [output.length], output);
  1159. output.add(compiled.output);
  1160. var fontDictTrackers = compiled.trackers;
  1161. this.compilePrivateDicts(cff.fdArray, fontDictTrackers, output);
  1162. }
  1163. this.compilePrivateDicts([cff.topDict], [topDictTracker], output);
  1164. output.add([0]);
  1165. return output.data;
  1166. },
  1167. encodeNumber: function CFFCompiler_encodeNumber(value) {
  1168. if (parseFloat(value) === parseInt(value, 10) && !isNaN(value)) {
  1169. return this.encodeInteger(value);
  1170. }
  1171. return this.encodeFloat(value);
  1172. },
  1173. encodeFloat: function CFFCompiler_encodeFloat(num) {
  1174. var value = num.toString();
  1175. var m = /\.(\d*?)(?:9{5,20}|0{5,20})\d{0,2}(?:e(.+)|$)/.exec(value);
  1176. if (m) {
  1177. var epsilon = parseFloat('1e' + ((m[2] ? +m[2] : 0) + m[1].length));
  1178. value = (Math.round(num * epsilon) / epsilon).toString();
  1179. }
  1180. var nibbles = '';
  1181. var i, ii;
  1182. for (i = 0, ii = value.length; i < ii; ++i) {
  1183. var a = value[i];
  1184. if (a === 'e') {
  1185. nibbles += value[++i] === '-' ? 'c' : 'b';
  1186. } else if (a === '.') {
  1187. nibbles += 'a';
  1188. } else if (a === '-') {
  1189. nibbles += 'e';
  1190. } else {
  1191. nibbles += a;
  1192. }
  1193. }
  1194. nibbles += nibbles.length & 1 ? 'f' : 'ff';
  1195. var out = [30];
  1196. for (i = 0, ii = nibbles.length; i < ii; i += 2) {
  1197. out.push(parseInt(nibbles.substr(i, 2), 16));
  1198. }
  1199. return out;
  1200. },
  1201. encodeInteger: function CFFCompiler_encodeInteger(value) {
  1202. var code;
  1203. if (value >= -107 && value <= 107) {
  1204. code = [value + 139];
  1205. } else if (value >= 108 && value <= 1131) {
  1206. value = value - 108;
  1207. code = [(value >> 8) + 247, value & 0xFF];
  1208. } else if (value >= -1131 && value <= -108) {
  1209. value = -value - 108;
  1210. code = [(value >> 8) + 251, value & 0xFF];
  1211. } else if (value >= -32768 && value <= 32767) {
  1212. code = [0x1c, value >> 8 & 0xFF, value & 0xFF];
  1213. } else {
  1214. code = [0x1d, value >> 24 & 0xFF, value >> 16 & 0xFF, value >> 8 & 0xFF, value & 0xFF];
  1215. }
  1216. return code;
  1217. },
  1218. compileHeader: function CFFCompiler_compileHeader(header) {
  1219. return [header.major, header.minor, header.hdrSize, header.offSize];
  1220. },
  1221. compileNameIndex: function CFFCompiler_compileNameIndex(names) {
  1222. var nameIndex = new CFFIndex();
  1223. for (var i = 0, ii = names.length; i < ii; ++i) {
  1224. var name = names[i];
  1225. var length = Math.min(name.length, 127);
  1226. var sanitizedName = new Array(length);
  1227. for (var j = 0; j < length; j++) {
  1228. var char = name[j];
  1229. if (char < '!' || char > '~' || char === '[' || char === ']' || char === '(' || char === ')' || char === '{' || char === '}' || char === '<' || char === '>' || char === '/' || char === '%') {
  1230. char = '_';
  1231. }
  1232. sanitizedName[j] = char;
  1233. }
  1234. sanitizedName = sanitizedName.join('');
  1235. if (sanitizedName === '') {
  1236. sanitizedName = 'Bad_Font_Name';
  1237. }
  1238. nameIndex.add((0, _util.stringToBytes)(sanitizedName));
  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 (!Array.isArray(types)) {
  1300. types = [types];
  1301. }
  1302. if (!Array.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;