cff_parser.js 54 KB

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