2
0

cff_parser.js 51 KB

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