cff_parser.js 51 KB

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