cff_parser.js 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930
  1. /* Copyright 2016 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. import {
  16. bytesToString,
  17. FormatError,
  18. info,
  19. shadow,
  20. stringToBytes,
  21. Util,
  22. warn,
  23. } from "../shared/util.js";
  24. import {
  25. ExpertCharset,
  26. ExpertSubsetCharset,
  27. ISOAdobeCharset,
  28. } from "./charsets.js";
  29. import { ExpertEncoding, StandardEncoding } from "./encodings.js";
  30. // Maximum subroutine call depth of type 2 charstrings. Matches OTS.
  31. const MAX_SUBR_NESTING = 10;
  32. /**
  33. * The CFF class takes a Type1 file and wrap it into a
  34. * 'Compact Font Format' which itself embed Type2 charstrings.
  35. */
  36. // prettier-ignore
  37. const CFFStandardStrings = [
  38. ".notdef", "space", "exclam", "quotedbl", "numbersign", "dollar", "percent",
  39. "ampersand", "quoteright", "parenleft", "parenright", "asterisk", "plus",
  40. "comma", "hyphen", "period", "slash", "zero", "one", "two", "three", "four",
  41. "five", "six", "seven", "eight", "nine", "colon", "semicolon", "less",
  42. "equal", "greater", "question", "at", "A", "B", "C", "D", "E", "F", "G", "H",
  43. "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
  44. "X", "Y", "Z", "bracketleft", "backslash", "bracketright", "asciicircum",
  45. "underscore", "quoteleft", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
  46. "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y",
  47. "z", "braceleft", "bar", "braceright", "asciitilde", "exclamdown", "cent",
  48. "sterling", "fraction", "yen", "florin", "section", "currency",
  49. "quotesingle", "quotedblleft", "guillemotleft", "guilsinglleft",
  50. "guilsinglright", "fi", "fl", "endash", "dagger", "daggerdbl",
  51. "periodcentered", "paragraph", "bullet", "quotesinglbase", "quotedblbase",
  52. "quotedblright", "guillemotright", "ellipsis", "perthousand", "questiondown",
  53. "grave", "acute", "circumflex", "tilde", "macron", "breve", "dotaccent",
  54. "dieresis", "ring", "cedilla", "hungarumlaut", "ogonek", "caron", "emdash",
  55. "AE", "ordfeminine", "Lslash", "Oslash", "OE", "ordmasculine", "ae",
  56. "dotlessi", "lslash", "oslash", "oe", "germandbls", "onesuperior",
  57. "logicalnot", "mu", "trademark", "Eth", "onehalf", "plusminus", "Thorn",
  58. "onequarter", "divide", "brokenbar", "degree", "thorn", "threequarters",
  59. "twosuperior", "registered", "minus", "eth", "multiply", "threesuperior",
  60. "copyright", "Aacute", "Acircumflex", "Adieresis", "Agrave", "Aring",
  61. "Atilde", "Ccedilla", "Eacute", "Ecircumflex", "Edieresis", "Egrave",
  62. "Iacute", "Icircumflex", "Idieresis", "Igrave", "Ntilde", "Oacute",
  63. "Ocircumflex", "Odieresis", "Ograve", "Otilde", "Scaron", "Uacute",
  64. "Ucircumflex", "Udieresis", "Ugrave", "Yacute", "Ydieresis", "Zcaron",
  65. "aacute", "acircumflex", "adieresis", "agrave", "aring", "atilde",
  66. "ccedilla", "eacute", "ecircumflex", "edieresis", "egrave", "iacute",
  67. "icircumflex", "idieresis", "igrave", "ntilde", "oacute", "ocircumflex",
  68. "odieresis", "ograve", "otilde", "scaron", "uacute", "ucircumflex",
  69. "udieresis", "ugrave", "yacute", "ydieresis", "zcaron", "exclamsmall",
  70. "Hungarumlautsmall", "dollaroldstyle", "dollarsuperior", "ampersandsmall",
  71. "Acutesmall", "parenleftsuperior", "parenrightsuperior", "twodotenleader",
  72. "onedotenleader", "zerooldstyle", "oneoldstyle", "twooldstyle",
  73. "threeoldstyle", "fouroldstyle", "fiveoldstyle", "sixoldstyle",
  74. "sevenoldstyle", "eightoldstyle", "nineoldstyle", "commasuperior",
  75. "threequartersemdash", "periodsuperior", "questionsmall", "asuperior",
  76. "bsuperior", "centsuperior", "dsuperior", "esuperior", "isuperior",
  77. "lsuperior", "msuperior", "nsuperior", "osuperior", "rsuperior", "ssuperior",
  78. "tsuperior", "ff", "ffi", "ffl", "parenleftinferior", "parenrightinferior",
  79. "Circumflexsmall", "hyphensuperior", "Gravesmall", "Asmall", "Bsmall",
  80. "Csmall", "Dsmall", "Esmall", "Fsmall", "Gsmall", "Hsmall", "Ismall",
  81. "Jsmall", "Ksmall", "Lsmall", "Msmall", "Nsmall", "Osmall", "Psmall",
  82. "Qsmall", "Rsmall", "Ssmall", "Tsmall", "Usmall", "Vsmall", "Wsmall",
  83. "Xsmall", "Ysmall", "Zsmall", "colonmonetary", "onefitted", "rupiah",
  84. "Tildesmall", "exclamdownsmall", "centoldstyle", "Lslashsmall",
  85. "Scaronsmall", "Zcaronsmall", "Dieresissmall", "Brevesmall", "Caronsmall",
  86. "Dotaccentsmall", "Macronsmall", "figuredash", "hypheninferior",
  87. "Ogoneksmall", "Ringsmall", "Cedillasmall", "questiondownsmall", "oneeighth",
  88. "threeeighths", "fiveeighths", "seveneighths", "onethird", "twothirds",
  89. "zerosuperior", "foursuperior", "fivesuperior", "sixsuperior",
  90. "sevensuperior", "eightsuperior", "ninesuperior", "zeroinferior",
  91. "oneinferior", "twoinferior", "threeinferior", "fourinferior",
  92. "fiveinferior", "sixinferior", "seveninferior", "eightinferior",
  93. "nineinferior", "centinferior", "dollarinferior", "periodinferior",
  94. "commainferior", "Agravesmall", "Aacutesmall", "Acircumflexsmall",
  95. "Atildesmall", "Adieresissmall", "Aringsmall", "AEsmall", "Ccedillasmall",
  96. "Egravesmall", "Eacutesmall", "Ecircumflexsmall", "Edieresissmall",
  97. "Igravesmall", "Iacutesmall", "Icircumflexsmall", "Idieresissmall",
  98. "Ethsmall", "Ntildesmall", "Ogravesmall", "Oacutesmall", "Ocircumflexsmall",
  99. "Otildesmall", "Odieresissmall", "OEsmall", "Oslashsmall", "Ugravesmall",
  100. "Uacutesmall", "Ucircumflexsmall", "Udieresissmall", "Yacutesmall",
  101. "Thornsmall", "Ydieresissmall", "001.000", "001.001", "001.002", "001.003",
  102. "Black", "Bold", "Book", "Light", "Medium", "Regular", "Roman", "Semibold"
  103. ];
  104. const NUM_STANDARD_CFF_STRINGS = 391;
  105. const CharstringValidationData = [
  106. /* 0 */ null,
  107. /* 1 */ { id: "hstem", min: 2, stackClearing: true, stem: true },
  108. /* 2 */ null,
  109. /* 3 */ { id: "vstem", min: 2, stackClearing: true, stem: true },
  110. /* 4 */ { id: "vmoveto", min: 1, stackClearing: true },
  111. /* 5 */ { id: "rlineto", min: 2, resetStack: true },
  112. /* 6 */ { id: "hlineto", min: 1, resetStack: true },
  113. /* 7 */ { id: "vlineto", min: 1, resetStack: true },
  114. /* 8 */ { id: "rrcurveto", min: 6, resetStack: true },
  115. /* 9 */ null,
  116. /* 10 */ { id: "callsubr", min: 1, undefStack: true },
  117. /* 11 */ { id: "return", min: 0, undefStack: true },
  118. /* 12 */ null,
  119. /* 13 */ null,
  120. /* 14 */ { id: "endchar", min: 0, stackClearing: true },
  121. /* 15 */ null,
  122. /* 16 */ null,
  123. /* 17 */ null,
  124. /* 18 */ { id: "hstemhm", min: 2, stackClearing: true, stem: true },
  125. /* 19 */ { id: "hintmask", min: 0, stackClearing: true },
  126. /* 20 */ { id: "cntrmask", min: 0, stackClearing: true },
  127. /* 21 */ { id: "rmoveto", min: 2, stackClearing: true },
  128. /* 22 */ { id: "hmoveto", min: 1, stackClearing: true },
  129. /* 23 */ { id: "vstemhm", min: 2, stackClearing: true, stem: true },
  130. /* 24 */ { id: "rcurveline", min: 8, resetStack: true },
  131. /* 25 */ { id: "rlinecurve", min: 8, resetStack: true },
  132. /* 26 */ { id: "vvcurveto", min: 4, resetStack: true },
  133. /* 27 */ { id: "hhcurveto", min: 4, resetStack: true },
  134. /* 28 */ null, // shortint
  135. /* 29 */ { id: "callgsubr", min: 1, undefStack: true },
  136. /* 30 */ { id: "vhcurveto", min: 4, resetStack: true },
  137. /* 31 */ { id: "hvcurveto", min: 4, resetStack: true },
  138. ];
  139. const CharstringValidationData12 = [
  140. null,
  141. null,
  142. null,
  143. { id: "and", min: 2, stackDelta: -1 },
  144. { id: "or", min: 2, stackDelta: -1 },
  145. { id: "not", min: 1, stackDelta: 0 },
  146. null,
  147. null,
  148. null,
  149. { id: "abs", min: 1, stackDelta: 0 },
  150. {
  151. id: "add",
  152. min: 2,
  153. stackDelta: -1,
  154. stackFn(stack, index) {
  155. stack[index - 2] = stack[index - 2] + stack[index - 1];
  156. },
  157. },
  158. {
  159. id: "sub",
  160. min: 2,
  161. stackDelta: -1,
  162. stackFn(stack, index) {
  163. stack[index - 2] = stack[index - 2] - stack[index - 1];
  164. },
  165. },
  166. {
  167. id: "div",
  168. min: 2,
  169. stackDelta: -1,
  170. stackFn(stack, index) {
  171. stack[index - 2] = stack[index - 2] / stack[index - 1];
  172. },
  173. },
  174. null,
  175. {
  176. id: "neg",
  177. min: 1,
  178. stackDelta: 0,
  179. stackFn(stack, index) {
  180. stack[index - 1] = -stack[index - 1];
  181. },
  182. },
  183. { id: "eq", min: 2, stackDelta: -1 },
  184. null,
  185. null,
  186. { id: "drop", min: 1, stackDelta: -1 },
  187. null,
  188. { id: "put", min: 2, stackDelta: -2 },
  189. { id: "get", min: 1, stackDelta: 0 },
  190. { id: "ifelse", min: 4, stackDelta: -3 },
  191. { id: "random", min: 0, stackDelta: 1 },
  192. {
  193. id: "mul",
  194. min: 2,
  195. stackDelta: -1,
  196. stackFn(stack, index) {
  197. stack[index - 2] = stack[index - 2] * stack[index - 1];
  198. },
  199. },
  200. null,
  201. { id: "sqrt", min: 1, stackDelta: 0 },
  202. { id: "dup", min: 1, stackDelta: 1 },
  203. { id: "exch", min: 2, stackDelta: 0 },
  204. { id: "index", min: 2, stackDelta: 0 },
  205. { id: "roll", min: 3, stackDelta: -2 },
  206. null,
  207. null,
  208. null,
  209. { id: "hflex", min: 7, resetStack: true },
  210. { id: "flex", min: 13, resetStack: true },
  211. { id: "hflex1", min: 9, resetStack: true },
  212. { id: "flex1", min: 11, resetStack: true },
  213. ];
  214. class CFFParser {
  215. constructor(file, properties, seacAnalysisEnabled) {
  216. this.bytes = file.getBytes();
  217. this.properties = properties;
  218. this.seacAnalysisEnabled = !!seacAnalysisEnabled;
  219. }
  220. parse() {
  221. const properties = this.properties;
  222. const cff = new CFF();
  223. this.cff = cff;
  224. // The first five sections must be in order, all the others are reached
  225. // via offsets contained in one of the below.
  226. const header = this.parseHeader();
  227. const nameIndex = this.parseIndex(header.endPos);
  228. const topDictIndex = this.parseIndex(nameIndex.endPos);
  229. const stringIndex = this.parseIndex(topDictIndex.endPos);
  230. const globalSubrIndex = this.parseIndex(stringIndex.endPos);
  231. const topDictParsed = this.parseDict(topDictIndex.obj.get(0));
  232. const topDict = this.createDict(CFFTopDict, topDictParsed, cff.strings);
  233. cff.header = header.obj;
  234. cff.names = this.parseNameIndex(nameIndex.obj);
  235. cff.strings = this.parseStringIndex(stringIndex.obj);
  236. cff.topDict = topDict;
  237. cff.globalSubrIndex = globalSubrIndex.obj;
  238. this.parsePrivateDict(cff.topDict);
  239. cff.isCIDFont = topDict.hasName("ROS");
  240. const charStringOffset = topDict.getByName("CharStrings");
  241. const charStringIndex = this.parseIndex(charStringOffset).obj;
  242. const fontMatrix = topDict.getByName("FontMatrix");
  243. if (fontMatrix) {
  244. properties.fontMatrix = fontMatrix;
  245. }
  246. const fontBBox = topDict.getByName("FontBBox");
  247. if (fontBBox) {
  248. // adjusting ascent/descent
  249. properties.ascent = Math.max(fontBBox[3], fontBBox[1]);
  250. properties.descent = Math.min(fontBBox[1], fontBBox[3]);
  251. properties.ascentScaled = true;
  252. }
  253. let charset, encoding;
  254. if (cff.isCIDFont) {
  255. const fdArrayIndex = this.parseIndex(topDict.getByName("FDArray")).obj;
  256. for (let i = 0, ii = fdArrayIndex.count; i < ii; ++i) {
  257. const dictRaw = fdArrayIndex.get(i);
  258. const fontDict = this.createDict(
  259. CFFTopDict,
  260. this.parseDict(dictRaw),
  261. cff.strings
  262. );
  263. this.parsePrivateDict(fontDict);
  264. cff.fdArray.push(fontDict);
  265. }
  266. // cid fonts don't have an encoding
  267. encoding = null;
  268. charset = this.parseCharsets(
  269. topDict.getByName("charset"),
  270. charStringIndex.count,
  271. cff.strings,
  272. true
  273. );
  274. cff.fdSelect = this.parseFDSelect(
  275. topDict.getByName("FDSelect"),
  276. charStringIndex.count
  277. );
  278. } else {
  279. charset = this.parseCharsets(
  280. topDict.getByName("charset"),
  281. charStringIndex.count,
  282. cff.strings,
  283. false
  284. );
  285. encoding = this.parseEncoding(
  286. topDict.getByName("Encoding"),
  287. properties,
  288. cff.strings,
  289. charset.charset
  290. );
  291. }
  292. cff.charset = charset;
  293. cff.encoding = encoding;
  294. const charStringsAndSeacs = this.parseCharStrings({
  295. charStrings: charStringIndex,
  296. localSubrIndex: topDict.privateDict.subrsIndex,
  297. globalSubrIndex: globalSubrIndex.obj,
  298. fdSelect: cff.fdSelect,
  299. fdArray: cff.fdArray,
  300. privateDict: topDict.privateDict,
  301. });
  302. cff.charStrings = charStringsAndSeacs.charStrings;
  303. cff.seacs = charStringsAndSeacs.seacs;
  304. cff.widths = charStringsAndSeacs.widths;
  305. return cff;
  306. }
  307. parseHeader() {
  308. let bytes = this.bytes;
  309. const bytesLength = bytes.length;
  310. let offset = 0;
  311. // Prevent an infinite loop, by checking that the offset is within the
  312. // bounds of the bytes array. Necessary in empty, or invalid, font files.
  313. while (offset < bytesLength && bytes[offset] !== 1) {
  314. ++offset;
  315. }
  316. if (offset >= bytesLength) {
  317. throw new FormatError("Invalid CFF header");
  318. }
  319. if (offset !== 0) {
  320. info("cff data is shifted");
  321. bytes = bytes.subarray(offset);
  322. this.bytes = bytes;
  323. }
  324. const major = bytes[0];
  325. const minor = bytes[1];
  326. const hdrSize = bytes[2];
  327. const offSize = bytes[3];
  328. const header = new CFFHeader(major, minor, hdrSize, offSize);
  329. return { obj: header, endPos: hdrSize };
  330. }
  331. parseDict(dict) {
  332. let pos = 0;
  333. function parseOperand() {
  334. let value = dict[pos++];
  335. if (value === 30) {
  336. return parseFloatOperand();
  337. } else if (value === 28) {
  338. value = dict[pos++];
  339. value = ((value << 24) | (dict[pos++] << 16)) >> 16;
  340. return value;
  341. } else if (value === 29) {
  342. value = dict[pos++];
  343. value = (value << 8) | dict[pos++];
  344. value = (value << 8) | dict[pos++];
  345. value = (value << 8) | dict[pos++];
  346. return value;
  347. } else if (value >= 32 && value <= 246) {
  348. return value - 139;
  349. } else if (value >= 247 && value <= 250) {
  350. return (value - 247) * 256 + dict[pos++] + 108;
  351. } else if (value >= 251 && value <= 254) {
  352. return -((value - 251) * 256) - dict[pos++] - 108;
  353. }
  354. warn('CFFParser_parseDict: "' + value + '" is a reserved command.');
  355. return NaN;
  356. }
  357. function parseFloatOperand() {
  358. let str = "";
  359. const eof = 15;
  360. // prettier-ignore
  361. const lookup = ["0", "1", "2", "3", "4", "5", "6", "7", "8",
  362. "9", ".", "E", "E-", null, "-"];
  363. const length = dict.length;
  364. while (pos < length) {
  365. const b = dict[pos++];
  366. const b1 = b >> 4;
  367. const b2 = b & 15;
  368. if (b1 === eof) {
  369. break;
  370. }
  371. str += lookup[b1];
  372. if (b2 === eof) {
  373. break;
  374. }
  375. str += lookup[b2];
  376. }
  377. return parseFloat(str);
  378. }
  379. let operands = [];
  380. const entries = [];
  381. pos = 0;
  382. const end = dict.length;
  383. while (pos < end) {
  384. let b = dict[pos];
  385. if (b <= 21) {
  386. if (b === 12) {
  387. b = (b << 8) | dict[++pos];
  388. }
  389. entries.push([b, operands]);
  390. operands = [];
  391. ++pos;
  392. } else {
  393. operands.push(parseOperand());
  394. }
  395. }
  396. return entries;
  397. }
  398. parseIndex(pos) {
  399. const cffIndex = new CFFIndex();
  400. const bytes = this.bytes;
  401. const count = (bytes[pos++] << 8) | bytes[pos++];
  402. const offsets = [];
  403. let end = pos;
  404. let i, ii;
  405. if (count !== 0) {
  406. const offsetSize = bytes[pos++];
  407. // add 1 for offset to determine size of last object
  408. const startPos = pos + (count + 1) * offsetSize - 1;
  409. for (i = 0, ii = count + 1; i < ii; ++i) {
  410. let offset = 0;
  411. for (let j = 0; j < offsetSize; ++j) {
  412. offset <<= 8;
  413. offset += bytes[pos++];
  414. }
  415. offsets.push(startPos + offset);
  416. }
  417. end = offsets[count];
  418. }
  419. for (i = 0, ii = offsets.length - 1; i < ii; ++i) {
  420. const offsetStart = offsets[i];
  421. const offsetEnd = offsets[i + 1];
  422. cffIndex.add(bytes.subarray(offsetStart, offsetEnd));
  423. }
  424. return { obj: cffIndex, endPos: end };
  425. }
  426. parseNameIndex(index) {
  427. const names = [];
  428. for (let i = 0, ii = index.count; i < ii; ++i) {
  429. const name = index.get(i);
  430. names.push(bytesToString(name));
  431. }
  432. return names;
  433. }
  434. parseStringIndex(index) {
  435. const strings = new CFFStrings();
  436. for (let i = 0, ii = index.count; i < ii; ++i) {
  437. const data = index.get(i);
  438. strings.add(bytesToString(data));
  439. }
  440. return strings;
  441. }
  442. createDict(Type, dict, strings) {
  443. const cffDict = new Type(strings);
  444. for (const [key, value] of dict) {
  445. cffDict.setByKey(key, value);
  446. }
  447. return cffDict;
  448. }
  449. parseCharString(state, data, localSubrIndex, globalSubrIndex) {
  450. if (!data || state.callDepth > MAX_SUBR_NESTING) {
  451. return false;
  452. }
  453. let stackSize = state.stackSize;
  454. const stack = state.stack;
  455. let length = data.length;
  456. for (let j = 0; j < length; ) {
  457. const value = data[j++];
  458. let validationCommand = null;
  459. if (value === 12) {
  460. const q = data[j++];
  461. if (q === 0) {
  462. // The CFF specification state that the 'dotsection' command
  463. // (12, 0) is deprecated and treated as a no-op, but all Type2
  464. // charstrings processors should support them. Unfortunately
  465. // the font sanitizer don't. As a workaround the sequence (12, 0)
  466. // is replaced by a useless (0, hmoveto).
  467. data[j - 2] = 139;
  468. data[j - 1] = 22;
  469. stackSize = 0;
  470. } else {
  471. validationCommand = CharstringValidationData12[q];
  472. }
  473. } else if (value === 28) {
  474. // number (16 bit)
  475. stack[stackSize] = ((data[j] << 24) | (data[j + 1] << 16)) >> 16;
  476. j += 2;
  477. stackSize++;
  478. } else if (value === 14) {
  479. if (stackSize >= 4) {
  480. stackSize -= 4;
  481. if (this.seacAnalysisEnabled) {
  482. state.seac = stack.slice(stackSize, stackSize + 4);
  483. return false;
  484. }
  485. }
  486. validationCommand = CharstringValidationData[value];
  487. } else if (value >= 32 && value <= 246) {
  488. // number
  489. stack[stackSize] = value - 139;
  490. stackSize++;
  491. } else if (value >= 247 && value <= 254) {
  492. // number (+1 bytes)
  493. stack[stackSize] =
  494. value < 251
  495. ? ((value - 247) << 8) + data[j] + 108
  496. : -((value - 251) << 8) - data[j] - 108;
  497. j++;
  498. stackSize++;
  499. } else if (value === 255) {
  500. // number (32 bit)
  501. stack[stackSize] =
  502. ((data[j] << 24) |
  503. (data[j + 1] << 16) |
  504. (data[j + 2] << 8) |
  505. data[j + 3]) /
  506. 65536;
  507. j += 4;
  508. stackSize++;
  509. } else if (value === 19 || value === 20) {
  510. state.hints += stackSize >> 1;
  511. // skipping right amount of hints flag data
  512. j += (state.hints + 7) >> 3;
  513. stackSize %= 2;
  514. validationCommand = CharstringValidationData[value];
  515. } else if (value === 10 || value === 29) {
  516. let subrsIndex;
  517. if (value === 10) {
  518. subrsIndex = localSubrIndex;
  519. } else {
  520. subrsIndex = globalSubrIndex;
  521. }
  522. if (!subrsIndex) {
  523. validationCommand = CharstringValidationData[value];
  524. warn("Missing subrsIndex for " + validationCommand.id);
  525. return false;
  526. }
  527. let bias = 32768;
  528. if (subrsIndex.count < 1240) {
  529. bias = 107;
  530. } else if (subrsIndex.count < 33900) {
  531. bias = 1131;
  532. }
  533. const subrNumber = stack[--stackSize] + bias;
  534. if (
  535. subrNumber < 0 ||
  536. subrNumber >= subrsIndex.count ||
  537. isNaN(subrNumber)
  538. ) {
  539. validationCommand = CharstringValidationData[value];
  540. warn("Out of bounds subrIndex for " + validationCommand.id);
  541. return false;
  542. }
  543. state.stackSize = stackSize;
  544. state.callDepth++;
  545. const valid = this.parseCharString(
  546. state,
  547. subrsIndex.get(subrNumber),
  548. localSubrIndex,
  549. globalSubrIndex
  550. );
  551. if (!valid) {
  552. return false;
  553. }
  554. state.callDepth--;
  555. stackSize = state.stackSize;
  556. continue;
  557. } else if (value === 11) {
  558. state.stackSize = stackSize;
  559. return true;
  560. } else if (value === 0 && j === data.length) {
  561. // Operator 0 is not used according to the current spec and
  562. // it's the last char and consequently it's likely a terminator.
  563. // So just replace it by endchar command to make OTS happy.
  564. data[j - 1] = 14;
  565. validationCommand = CharstringValidationData[14];
  566. } else if (value === 9) {
  567. // Not a valid value.
  568. data.copyWithin(j - 1, j, -1);
  569. j -= 1;
  570. length -= 1;
  571. continue;
  572. } else {
  573. validationCommand = CharstringValidationData[value];
  574. }
  575. if (validationCommand) {
  576. if (validationCommand.stem) {
  577. state.hints += stackSize >> 1;
  578. if (value === 3 || value === 23) {
  579. // vstem or vstemhm.
  580. state.hasVStems = true;
  581. } else if (state.hasVStems && (value === 1 || value === 18)) {
  582. // Some browsers don't draw glyphs that specify vstems before
  583. // hstems. As a workaround, replace hstem (1) and hstemhm (18)
  584. // with a pointless vstem (3) or vstemhm (23).
  585. warn("CFF stem hints are in wrong order");
  586. data[j - 1] = value === 1 ? 3 : 23;
  587. }
  588. }
  589. if ("min" in validationCommand) {
  590. if (!state.undefStack && stackSize < validationCommand.min) {
  591. warn(
  592. "Not enough parameters for " +
  593. validationCommand.id +
  594. "; actual: " +
  595. stackSize +
  596. ", expected: " +
  597. validationCommand.min
  598. );
  599. if (stackSize === 0) {
  600. // Just "fix" the outline in replacing command by a endchar:
  601. // it could lead to wrong rendering of some glyphs or not.
  602. // For example, the pdf in #6132 is well-rendered.
  603. data[j - 1] = 14;
  604. return true;
  605. }
  606. return false;
  607. }
  608. }
  609. if (state.firstStackClearing && validationCommand.stackClearing) {
  610. state.firstStackClearing = false;
  611. // the optional character width can be found before the first
  612. // stack-clearing command arguments
  613. stackSize -= validationCommand.min;
  614. if (stackSize >= 2 && validationCommand.stem) {
  615. // there are even amount of arguments for stem commands
  616. stackSize %= 2;
  617. } else if (stackSize > 1) {
  618. warn("Found too many parameters for stack-clearing command");
  619. }
  620. if (stackSize > 0) {
  621. // Width can be any number since its the difference
  622. // from nominalWidthX.
  623. state.width = stack[stackSize - 1];
  624. }
  625. }
  626. if ("stackDelta" in validationCommand) {
  627. if ("stackFn" in validationCommand) {
  628. validationCommand.stackFn(stack, stackSize);
  629. }
  630. stackSize += validationCommand.stackDelta;
  631. } else if (validationCommand.stackClearing) {
  632. stackSize = 0;
  633. } else if (validationCommand.resetStack) {
  634. stackSize = 0;
  635. state.undefStack = false;
  636. } else if (validationCommand.undefStack) {
  637. stackSize = 0;
  638. state.undefStack = true;
  639. state.firstStackClearing = false;
  640. }
  641. }
  642. }
  643. if (length < data.length) {
  644. data.fill(/* endchar = */ 14, length);
  645. }
  646. state.stackSize = stackSize;
  647. return true;
  648. }
  649. parseCharStrings({
  650. charStrings,
  651. localSubrIndex,
  652. globalSubrIndex,
  653. fdSelect,
  654. fdArray,
  655. privateDict,
  656. }) {
  657. const seacs = [];
  658. const widths = [];
  659. const count = charStrings.count;
  660. for (let i = 0; i < count; i++) {
  661. const charstring = charStrings.get(i);
  662. const state = {
  663. callDepth: 0,
  664. stackSize: 0,
  665. stack: [],
  666. undefStack: true,
  667. hints: 0,
  668. firstStackClearing: true,
  669. seac: null,
  670. width: null,
  671. hasVStems: false,
  672. };
  673. let valid = true;
  674. let localSubrToUse = null;
  675. let privateDictToUse = privateDict;
  676. if (fdSelect && fdArray.length) {
  677. const fdIndex = fdSelect.getFDIndex(i);
  678. if (fdIndex === -1) {
  679. warn("Glyph index is not in fd select.");
  680. valid = false;
  681. }
  682. if (fdIndex >= fdArray.length) {
  683. warn("Invalid fd index for glyph index.");
  684. valid = false;
  685. }
  686. if (valid) {
  687. privateDictToUse = fdArray[fdIndex].privateDict;
  688. localSubrToUse = privateDictToUse.subrsIndex;
  689. }
  690. } else if (localSubrIndex) {
  691. localSubrToUse = localSubrIndex;
  692. }
  693. if (valid) {
  694. valid = this.parseCharString(
  695. state,
  696. charstring,
  697. localSubrToUse,
  698. globalSubrIndex
  699. );
  700. }
  701. if (state.width !== null) {
  702. const nominalWidth = privateDictToUse.getByName("nominalWidthX");
  703. widths[i] = nominalWidth + state.width;
  704. } else {
  705. const defaultWidth = privateDictToUse.getByName("defaultWidthX");
  706. widths[i] = defaultWidth;
  707. }
  708. if (state.seac !== null) {
  709. seacs[i] = state.seac;
  710. }
  711. if (!valid) {
  712. // resetting invalid charstring to single 'endchar'
  713. charStrings.set(i, new Uint8Array([14]));
  714. }
  715. }
  716. return { charStrings, seacs, widths };
  717. }
  718. emptyPrivateDictionary(parentDict) {
  719. const privateDict = this.createDict(CFFPrivateDict, [], parentDict.strings);
  720. parentDict.setByKey(18, [0, 0]);
  721. parentDict.privateDict = privateDict;
  722. }
  723. parsePrivateDict(parentDict) {
  724. // no private dict, do nothing
  725. if (!parentDict.hasName("Private")) {
  726. this.emptyPrivateDictionary(parentDict);
  727. return;
  728. }
  729. const privateOffset = parentDict.getByName("Private");
  730. // make sure the params are formatted correctly
  731. if (!Array.isArray(privateOffset) || privateOffset.length !== 2) {
  732. parentDict.removeByName("Private");
  733. return;
  734. }
  735. const size = privateOffset[0];
  736. const offset = privateOffset[1];
  737. // remove empty dicts or ones that refer to invalid location
  738. if (size === 0 || offset >= this.bytes.length) {
  739. this.emptyPrivateDictionary(parentDict);
  740. return;
  741. }
  742. const privateDictEnd = offset + size;
  743. const dictData = this.bytes.subarray(offset, privateDictEnd);
  744. const dict = this.parseDict(dictData);
  745. const privateDict = this.createDict(
  746. CFFPrivateDict,
  747. dict,
  748. parentDict.strings
  749. );
  750. parentDict.privateDict = privateDict;
  751. // Parse the Subrs index also since it's relative to the private dict.
  752. if (!privateDict.getByName("Subrs")) {
  753. return;
  754. }
  755. const subrsOffset = privateDict.getByName("Subrs");
  756. const relativeOffset = offset + subrsOffset;
  757. // Validate the offset.
  758. if (subrsOffset === 0 || relativeOffset >= this.bytes.length) {
  759. this.emptyPrivateDictionary(parentDict);
  760. return;
  761. }
  762. const subrsIndex = this.parseIndex(relativeOffset);
  763. privateDict.subrsIndex = subrsIndex.obj;
  764. }
  765. parseCharsets(pos, length, strings, cid) {
  766. if (pos === 0) {
  767. return new CFFCharset(
  768. true,
  769. CFFCharsetPredefinedTypes.ISO_ADOBE,
  770. ISOAdobeCharset
  771. );
  772. } else if (pos === 1) {
  773. return new CFFCharset(
  774. true,
  775. CFFCharsetPredefinedTypes.EXPERT,
  776. ExpertCharset
  777. );
  778. } else if (pos === 2) {
  779. return new CFFCharset(
  780. true,
  781. CFFCharsetPredefinedTypes.EXPERT_SUBSET,
  782. ExpertSubsetCharset
  783. );
  784. }
  785. const bytes = this.bytes;
  786. const start = pos;
  787. const format = bytes[pos++];
  788. const charset = [cid ? 0 : ".notdef"];
  789. let id, count, i;
  790. // subtract 1 for the .notdef glyph
  791. length -= 1;
  792. switch (format) {
  793. case 0:
  794. for (i = 0; i < length; i++) {
  795. id = (bytes[pos++] << 8) | bytes[pos++];
  796. charset.push(cid ? id : strings.get(id));
  797. }
  798. break;
  799. case 1:
  800. while (charset.length <= length) {
  801. id = (bytes[pos++] << 8) | bytes[pos++];
  802. count = bytes[pos++];
  803. for (i = 0; i <= count; i++) {
  804. charset.push(cid ? id++ : strings.get(id++));
  805. }
  806. }
  807. break;
  808. case 2:
  809. while (charset.length <= length) {
  810. id = (bytes[pos++] << 8) | bytes[pos++];
  811. count = (bytes[pos++] << 8) | bytes[pos++];
  812. for (i = 0; i <= count; i++) {
  813. charset.push(cid ? id++ : strings.get(id++));
  814. }
  815. }
  816. break;
  817. default:
  818. throw new FormatError("Unknown charset format");
  819. }
  820. // Raw won't be needed if we actually compile the charset.
  821. const end = pos;
  822. const raw = bytes.subarray(start, end);
  823. return new CFFCharset(false, format, charset, raw);
  824. }
  825. parseEncoding(pos, properties, strings, charset) {
  826. const encoding = Object.create(null);
  827. const bytes = this.bytes;
  828. let predefined = false;
  829. let format, i, ii;
  830. let raw = null;
  831. function readSupplement() {
  832. const supplementsCount = bytes[pos++];
  833. for (i = 0; i < supplementsCount; i++) {
  834. const code = bytes[pos++];
  835. const sid = (bytes[pos++] << 8) + (bytes[pos++] & 0xff);
  836. encoding[code] = charset.indexOf(strings.get(sid));
  837. }
  838. }
  839. if (pos === 0 || pos === 1) {
  840. predefined = true;
  841. format = pos;
  842. const baseEncoding = pos ? ExpertEncoding : StandardEncoding;
  843. for (i = 0, ii = charset.length; i < ii; i++) {
  844. const index = baseEncoding.indexOf(charset[i]);
  845. if (index !== -1) {
  846. encoding[index] = i;
  847. }
  848. }
  849. } else {
  850. const dataStart = pos;
  851. format = bytes[pos++];
  852. switch (format & 0x7f) {
  853. case 0:
  854. const glyphsCount = bytes[pos++];
  855. for (i = 1; i <= glyphsCount; i++) {
  856. encoding[bytes[pos++]] = i;
  857. }
  858. break;
  859. case 1:
  860. const rangesCount = bytes[pos++];
  861. let gid = 1;
  862. for (i = 0; i < rangesCount; i++) {
  863. const start = bytes[pos++];
  864. const left = bytes[pos++];
  865. for (let j = start; j <= start + left; j++) {
  866. encoding[j] = gid++;
  867. }
  868. }
  869. break;
  870. default:
  871. throw new FormatError(`Unknown encoding format: ${format} in CFF`);
  872. }
  873. const dataEnd = pos;
  874. if (format & 0x80) {
  875. // hasSupplement
  876. // The font sanitizer does not support CFF encoding with a
  877. // supplement, since the encoding is not really used to map
  878. // between gid to glyph, let's overwrite what is declared in
  879. // the top dictionary to let the sanitizer think the font use
  880. // StandardEncoding, that's a lie but that's ok.
  881. bytes[dataStart] &= 0x7f;
  882. readSupplement();
  883. }
  884. raw = bytes.subarray(dataStart, dataEnd);
  885. }
  886. format &= 0x7f;
  887. return new CFFEncoding(predefined, format, encoding, raw);
  888. }
  889. parseFDSelect(pos, length) {
  890. const bytes = this.bytes;
  891. const format = bytes[pos++];
  892. const fdSelect = [];
  893. let i;
  894. switch (format) {
  895. case 0:
  896. for (i = 0; i < length; ++i) {
  897. const id = bytes[pos++];
  898. fdSelect.push(id);
  899. }
  900. break;
  901. case 3:
  902. const rangesCount = (bytes[pos++] << 8) | bytes[pos++];
  903. for (i = 0; i < rangesCount; ++i) {
  904. let first = (bytes[pos++] << 8) | bytes[pos++];
  905. if (i === 0 && first !== 0) {
  906. warn(
  907. "parseFDSelect: The first range must have a first GID of 0" +
  908. " -- trying to recover."
  909. );
  910. first = 0;
  911. }
  912. const fdIndex = bytes[pos++];
  913. const next = (bytes[pos] << 8) | bytes[pos + 1];
  914. for (let j = first; j < next; ++j) {
  915. fdSelect.push(fdIndex);
  916. }
  917. }
  918. // Advance past the sentinel(next).
  919. pos += 2;
  920. break;
  921. default:
  922. throw new FormatError(`parseFDSelect: Unknown format "${format}".`);
  923. }
  924. if (fdSelect.length !== length) {
  925. throw new FormatError("parseFDSelect: Invalid font data.");
  926. }
  927. return new CFFFDSelect(format, fdSelect);
  928. }
  929. }
  930. // Compact Font Format
  931. class CFF {
  932. constructor() {
  933. this.header = null;
  934. this.names = [];
  935. this.topDict = null;
  936. this.strings = new CFFStrings();
  937. this.globalSubrIndex = null;
  938. // The following could really be per font, but since we only have one font
  939. // store them here.
  940. this.encoding = null;
  941. this.charset = null;
  942. this.charStrings = null;
  943. this.fdArray = [];
  944. this.fdSelect = null;
  945. this.isCIDFont = false;
  946. }
  947. duplicateFirstGlyph() {
  948. // Browsers will not display a glyph at position 0. Typically glyph 0 is
  949. // notdef, but a number of fonts put a valid glyph there so it must be
  950. // duplicated and appended.
  951. if (this.charStrings.count >= 65535) {
  952. warn("Not enough space in charstrings to duplicate first glyph.");
  953. return;
  954. }
  955. const glyphZero = this.charStrings.get(0);
  956. this.charStrings.add(glyphZero);
  957. if (this.isCIDFont) {
  958. this.fdSelect.fdSelect.push(this.fdSelect.fdSelect[0]);
  959. }
  960. }
  961. hasGlyphId(id) {
  962. if (id < 0 || id >= this.charStrings.count) {
  963. return false;
  964. }
  965. const glyph = this.charStrings.get(id);
  966. return glyph.length > 0;
  967. }
  968. }
  969. class CFFHeader {
  970. constructor(major, minor, hdrSize, offSize) {
  971. this.major = major;
  972. this.minor = minor;
  973. this.hdrSize = hdrSize;
  974. this.offSize = offSize;
  975. }
  976. }
  977. class CFFStrings {
  978. constructor() {
  979. this.strings = [];
  980. }
  981. get(index) {
  982. if (index >= 0 && index <= NUM_STANDARD_CFF_STRINGS - 1) {
  983. return CFFStandardStrings[index];
  984. }
  985. if (index - NUM_STANDARD_CFF_STRINGS <= this.strings.length) {
  986. return this.strings[index - NUM_STANDARD_CFF_STRINGS];
  987. }
  988. return CFFStandardStrings[0];
  989. }
  990. getSID(str) {
  991. let index = CFFStandardStrings.indexOf(str);
  992. if (index !== -1) {
  993. return index;
  994. }
  995. index = this.strings.indexOf(str);
  996. if (index !== -1) {
  997. return index + NUM_STANDARD_CFF_STRINGS;
  998. }
  999. return -1;
  1000. }
  1001. add(value) {
  1002. this.strings.push(value);
  1003. }
  1004. get count() {
  1005. return this.strings.length;
  1006. }
  1007. }
  1008. class CFFIndex {
  1009. constructor() {
  1010. this.objects = [];
  1011. this.length = 0;
  1012. }
  1013. add(data) {
  1014. this.length += data.length;
  1015. this.objects.push(data);
  1016. }
  1017. set(index, data) {
  1018. this.length += data.length - this.objects[index].length;
  1019. this.objects[index] = data;
  1020. }
  1021. get(index) {
  1022. return this.objects[index];
  1023. }
  1024. get count() {
  1025. return this.objects.length;
  1026. }
  1027. }
  1028. class CFFDict {
  1029. constructor(tables, strings) {
  1030. this.keyToNameMap = tables.keyToNameMap;
  1031. this.nameToKeyMap = tables.nameToKeyMap;
  1032. this.defaults = tables.defaults;
  1033. this.types = tables.types;
  1034. this.opcodes = tables.opcodes;
  1035. this.order = tables.order;
  1036. this.strings = strings;
  1037. this.values = Object.create(null);
  1038. }
  1039. // value should always be an array
  1040. setByKey(key, value) {
  1041. if (!(key in this.keyToNameMap)) {
  1042. return false;
  1043. }
  1044. // ignore empty values
  1045. if (value.length === 0) {
  1046. return true;
  1047. }
  1048. // Ignore invalid values (fixes bug1068432.pdf and bug1308536.pdf).
  1049. for (const val of value) {
  1050. if (isNaN(val)) {
  1051. warn(`Invalid CFFDict value: "${value}" for key "${key}".`);
  1052. return true;
  1053. }
  1054. }
  1055. const type = this.types[key];
  1056. // remove the array wrapping these types of values
  1057. if (type === "num" || type === "sid" || type === "offset") {
  1058. value = value[0];
  1059. }
  1060. this.values[key] = value;
  1061. return true;
  1062. }
  1063. setByName(name, value) {
  1064. if (!(name in this.nameToKeyMap)) {
  1065. throw new FormatError(`Invalid dictionary name "${name}"`);
  1066. }
  1067. this.values[this.nameToKeyMap[name]] = value;
  1068. }
  1069. hasName(name) {
  1070. return this.nameToKeyMap[name] in this.values;
  1071. }
  1072. getByName(name) {
  1073. if (!(name in this.nameToKeyMap)) {
  1074. throw new FormatError(`Invalid dictionary name ${name}"`);
  1075. }
  1076. const key = this.nameToKeyMap[name];
  1077. if (!(key in this.values)) {
  1078. return this.defaults[key];
  1079. }
  1080. return this.values[key];
  1081. }
  1082. removeByName(name) {
  1083. delete this.values[this.nameToKeyMap[name]];
  1084. }
  1085. static createTables(layout) {
  1086. const tables = {
  1087. keyToNameMap: {},
  1088. nameToKeyMap: {},
  1089. defaults: {},
  1090. types: {},
  1091. opcodes: {},
  1092. order: [],
  1093. };
  1094. for (const entry of layout) {
  1095. const key = Array.isArray(entry[0])
  1096. ? (entry[0][0] << 8) + entry[0][1]
  1097. : entry[0];
  1098. tables.keyToNameMap[key] = entry[1];
  1099. tables.nameToKeyMap[entry[1]] = key;
  1100. tables.types[key] = entry[2];
  1101. tables.defaults[key] = entry[3];
  1102. tables.opcodes[key] = Array.isArray(entry[0]) ? entry[0] : [entry[0]];
  1103. tables.order.push(key);
  1104. }
  1105. return tables;
  1106. }
  1107. }
  1108. const CFFTopDictLayout = [
  1109. [[12, 30], "ROS", ["sid", "sid", "num"], null],
  1110. [[12, 20], "SyntheticBase", "num", null],
  1111. [0, "version", "sid", null],
  1112. [1, "Notice", "sid", null],
  1113. [[12, 0], "Copyright", "sid", null],
  1114. [2, "FullName", "sid", null],
  1115. [3, "FamilyName", "sid", null],
  1116. [4, "Weight", "sid", null],
  1117. [[12, 1], "isFixedPitch", "num", 0],
  1118. [[12, 2], "ItalicAngle", "num", 0],
  1119. [[12, 3], "UnderlinePosition", "num", -100],
  1120. [[12, 4], "UnderlineThickness", "num", 50],
  1121. [[12, 5], "PaintType", "num", 0],
  1122. [[12, 6], "CharstringType", "num", 2],
  1123. // prettier-ignore
  1124. [[12, 7], "FontMatrix", ["num", "num", "num", "num", "num", "num"],
  1125. [0.001, 0, 0, 0.001, 0, 0]],
  1126. [13, "UniqueID", "num", null],
  1127. [5, "FontBBox", ["num", "num", "num", "num"], [0, 0, 0, 0]],
  1128. [[12, 8], "StrokeWidth", "num", 0],
  1129. [14, "XUID", "array", null],
  1130. [15, "charset", "offset", 0],
  1131. [16, "Encoding", "offset", 0],
  1132. [17, "CharStrings", "offset", 0],
  1133. [18, "Private", ["offset", "offset"], null],
  1134. [[12, 21], "PostScript", "sid", null],
  1135. [[12, 22], "BaseFontName", "sid", null],
  1136. [[12, 23], "BaseFontBlend", "delta", null],
  1137. [[12, 31], "CIDFontVersion", "num", 0],
  1138. [[12, 32], "CIDFontRevision", "num", 0],
  1139. [[12, 33], "CIDFontType", "num", 0],
  1140. [[12, 34], "CIDCount", "num", 8720],
  1141. [[12, 35], "UIDBase", "num", null],
  1142. // XXX: CID Fonts on DirectWrite 6.1 only seem to work if FDSelect comes
  1143. // before FDArray.
  1144. [[12, 37], "FDSelect", "offset", null],
  1145. [[12, 36], "FDArray", "offset", null],
  1146. [[12, 38], "FontName", "sid", null],
  1147. ];
  1148. class CFFTopDict extends CFFDict {
  1149. static get tables() {
  1150. return shadow(this, "tables", this.createTables(CFFTopDictLayout));
  1151. }
  1152. constructor(strings) {
  1153. super(CFFTopDict.tables, strings);
  1154. this.privateDict = null;
  1155. }
  1156. }
  1157. const CFFPrivateDictLayout = [
  1158. [6, "BlueValues", "delta", null],
  1159. [7, "OtherBlues", "delta", null],
  1160. [8, "FamilyBlues", "delta", null],
  1161. [9, "FamilyOtherBlues", "delta", null],
  1162. [[12, 9], "BlueScale", "num", 0.039625],
  1163. [[12, 10], "BlueShift", "num", 7],
  1164. [[12, 11], "BlueFuzz", "num", 1],
  1165. [10, "StdHW", "num", null],
  1166. [11, "StdVW", "num", null],
  1167. [[12, 12], "StemSnapH", "delta", null],
  1168. [[12, 13], "StemSnapV", "delta", null],
  1169. [[12, 14], "ForceBold", "num", 0],
  1170. [[12, 17], "LanguageGroup", "num", 0],
  1171. [[12, 18], "ExpansionFactor", "num", 0.06],
  1172. [[12, 19], "initialRandomSeed", "num", 0],
  1173. [20, "defaultWidthX", "num", 0],
  1174. [21, "nominalWidthX", "num", 0],
  1175. [19, "Subrs", "offset", null],
  1176. ];
  1177. class CFFPrivateDict extends CFFDict {
  1178. static get tables() {
  1179. return shadow(this, "tables", this.createTables(CFFPrivateDictLayout));
  1180. }
  1181. constructor(strings) {
  1182. super(CFFPrivateDict.tables, strings);
  1183. this.subrsIndex = null;
  1184. }
  1185. }
  1186. const CFFCharsetPredefinedTypes = {
  1187. ISO_ADOBE: 0,
  1188. EXPERT: 1,
  1189. EXPERT_SUBSET: 2,
  1190. };
  1191. class CFFCharset {
  1192. constructor(predefined, format, charset, raw) {
  1193. this.predefined = predefined;
  1194. this.format = format;
  1195. this.charset = charset;
  1196. this.raw = raw;
  1197. }
  1198. }
  1199. class CFFEncoding {
  1200. constructor(predefined, format, encoding, raw) {
  1201. this.predefined = predefined;
  1202. this.format = format;
  1203. this.encoding = encoding;
  1204. this.raw = raw;
  1205. }
  1206. }
  1207. class CFFFDSelect {
  1208. constructor(format, fdSelect) {
  1209. this.format = format;
  1210. this.fdSelect = fdSelect;
  1211. }
  1212. getFDIndex(glyphIndex) {
  1213. if (glyphIndex < 0 || glyphIndex >= this.fdSelect.length) {
  1214. return -1;
  1215. }
  1216. return this.fdSelect[glyphIndex];
  1217. }
  1218. }
  1219. // Helper class to keep track of where an offset is within the data and helps
  1220. // filling in that offset once it's known.
  1221. class CFFOffsetTracker {
  1222. constructor() {
  1223. this.offsets = Object.create(null);
  1224. }
  1225. isTracking(key) {
  1226. return key in this.offsets;
  1227. }
  1228. track(key, location) {
  1229. if (key in this.offsets) {
  1230. throw new FormatError(`Already tracking location of ${key}`);
  1231. }
  1232. this.offsets[key] = location;
  1233. }
  1234. offset(value) {
  1235. for (const key in this.offsets) {
  1236. this.offsets[key] += value;
  1237. }
  1238. }
  1239. setEntryLocation(key, values, output) {
  1240. if (!(key in this.offsets)) {
  1241. throw new FormatError(`Not tracking location of ${key}`);
  1242. }
  1243. const data = output.data;
  1244. const dataOffset = this.offsets[key];
  1245. const size = 5;
  1246. for (let i = 0, ii = values.length; i < ii; ++i) {
  1247. const offset0 = i * size + dataOffset;
  1248. const offset1 = offset0 + 1;
  1249. const offset2 = offset0 + 2;
  1250. const offset3 = offset0 + 3;
  1251. const offset4 = offset0 + 4;
  1252. // It's easy to screw up offsets so perform this sanity check.
  1253. if (
  1254. data[offset0] !== 0x1d ||
  1255. data[offset1] !== 0 ||
  1256. data[offset2] !== 0 ||
  1257. data[offset3] !== 0 ||
  1258. data[offset4] !== 0
  1259. ) {
  1260. throw new FormatError("writing to an offset that is not empty");
  1261. }
  1262. const value = values[i];
  1263. data[offset0] = 0x1d;
  1264. data[offset1] = (value >> 24) & 0xff;
  1265. data[offset2] = (value >> 16) & 0xff;
  1266. data[offset3] = (value >> 8) & 0xff;
  1267. data[offset4] = value & 0xff;
  1268. }
  1269. }
  1270. }
  1271. // Takes a CFF and converts it to the binary representation.
  1272. class CFFCompiler {
  1273. constructor(cff) {
  1274. this.cff = cff;
  1275. }
  1276. compile() {
  1277. const cff = this.cff;
  1278. const output = {
  1279. data: [],
  1280. length: 0,
  1281. add(data) {
  1282. this.data = this.data.concat(data);
  1283. this.length = this.data.length;
  1284. },
  1285. };
  1286. // Compile the five entries that must be in order.
  1287. const header = this.compileHeader(cff.header);
  1288. output.add(header);
  1289. const nameIndex = this.compileNameIndex(cff.names);
  1290. output.add(nameIndex);
  1291. if (cff.isCIDFont) {
  1292. // The spec is unclear on how font matrices should relate to each other
  1293. // when there is one in the main top dict and the sub top dicts.
  1294. // Windows handles this differently than linux and osx so we have to
  1295. // normalize to work on all.
  1296. // Rules based off of some mailing list discussions:
  1297. // - If main font has a matrix and subfont doesn't, use the main matrix.
  1298. // - If no main font matrix and there is a subfont matrix, use the
  1299. // subfont matrix.
  1300. // - If both have matrices, concat together.
  1301. // - If neither have matrices, use default.
  1302. // To make this work on all platforms we move the top matrix into each
  1303. // sub top dict and concat if necessary.
  1304. if (cff.topDict.hasName("FontMatrix")) {
  1305. const base = cff.topDict.getByName("FontMatrix");
  1306. cff.topDict.removeByName("FontMatrix");
  1307. for (const subDict of cff.fdArray) {
  1308. let matrix = base.slice(0);
  1309. if (subDict.hasName("FontMatrix")) {
  1310. matrix = Util.transform(matrix, subDict.getByName("FontMatrix"));
  1311. }
  1312. subDict.setByName("FontMatrix", matrix);
  1313. }
  1314. }
  1315. }
  1316. const xuid = cff.topDict.getByName("XUID");
  1317. if (xuid && xuid.length > 16) {
  1318. // Length of XUID array must not be greater than 16 (issue #12399).
  1319. cff.topDict.removeByName("XUID");
  1320. }
  1321. cff.topDict.setByName("charset", 0);
  1322. let compiled = this.compileTopDicts(
  1323. [cff.topDict],
  1324. output.length,
  1325. cff.isCIDFont
  1326. );
  1327. output.add(compiled.output);
  1328. const topDictTracker = compiled.trackers[0];
  1329. const stringIndex = this.compileStringIndex(cff.strings.strings);
  1330. output.add(stringIndex);
  1331. const globalSubrIndex = this.compileIndex(cff.globalSubrIndex);
  1332. output.add(globalSubrIndex);
  1333. // Now start on the other entries that have no specific order.
  1334. if (cff.encoding && cff.topDict.hasName("Encoding")) {
  1335. if (cff.encoding.predefined) {
  1336. topDictTracker.setEntryLocation(
  1337. "Encoding",
  1338. [cff.encoding.format],
  1339. output
  1340. );
  1341. } else {
  1342. const encoding = this.compileEncoding(cff.encoding);
  1343. topDictTracker.setEntryLocation("Encoding", [output.length], output);
  1344. output.add(encoding);
  1345. }
  1346. }
  1347. const charset = this.compileCharset(
  1348. cff.charset,
  1349. cff.charStrings.count,
  1350. cff.strings,
  1351. cff.isCIDFont
  1352. );
  1353. topDictTracker.setEntryLocation("charset", [output.length], output);
  1354. output.add(charset);
  1355. const charStrings = this.compileCharStrings(cff.charStrings);
  1356. topDictTracker.setEntryLocation("CharStrings", [output.length], output);
  1357. output.add(charStrings);
  1358. if (cff.isCIDFont) {
  1359. // For some reason FDSelect must be in front of FDArray on windows. OSX
  1360. // and linux don't seem to care.
  1361. topDictTracker.setEntryLocation("FDSelect", [output.length], output);
  1362. const fdSelect = this.compileFDSelect(cff.fdSelect);
  1363. output.add(fdSelect);
  1364. // It is unclear if the sub font dictionary can have CID related
  1365. // dictionary keys, but the sanitizer doesn't like them so remove them.
  1366. compiled = this.compileTopDicts(cff.fdArray, output.length, true);
  1367. topDictTracker.setEntryLocation("FDArray", [output.length], output);
  1368. output.add(compiled.output);
  1369. const fontDictTrackers = compiled.trackers;
  1370. this.compilePrivateDicts(cff.fdArray, fontDictTrackers, output);
  1371. }
  1372. this.compilePrivateDicts([cff.topDict], [topDictTracker], output);
  1373. // If the font data ends with INDEX whose object data is zero-length,
  1374. // the sanitizer will bail out. Add a dummy byte to avoid that.
  1375. output.add([0]);
  1376. return output.data;
  1377. }
  1378. encodeNumber(value) {
  1379. if (Number.isInteger(value)) {
  1380. return this.encodeInteger(value);
  1381. }
  1382. return this.encodeFloat(value);
  1383. }
  1384. static get EncodeFloatRegExp() {
  1385. return shadow(
  1386. this,
  1387. "EncodeFloatRegExp",
  1388. /\.(\d*?)(?:9{5,20}|0{5,20})\d{0,2}(?:e(.+)|$)/
  1389. );
  1390. }
  1391. encodeFloat(num) {
  1392. let value = num.toString();
  1393. // Rounding inaccurate doubles.
  1394. const m = CFFCompiler.EncodeFloatRegExp.exec(value);
  1395. if (m) {
  1396. const epsilon = parseFloat("1e" + ((m[2] ? +m[2] : 0) + m[1].length));
  1397. value = (Math.round(num * epsilon) / epsilon).toString();
  1398. }
  1399. let nibbles = "";
  1400. let i, ii;
  1401. for (i = 0, ii = value.length; i < ii; ++i) {
  1402. const a = value[i];
  1403. if (a === "e") {
  1404. nibbles += value[++i] === "-" ? "c" : "b";
  1405. } else if (a === ".") {
  1406. nibbles += "a";
  1407. } else if (a === "-") {
  1408. nibbles += "e";
  1409. } else {
  1410. nibbles += a;
  1411. }
  1412. }
  1413. nibbles += nibbles.length & 1 ? "f" : "ff";
  1414. const out = [30];
  1415. for (i = 0, ii = nibbles.length; i < ii; i += 2) {
  1416. out.push(parseInt(nibbles.substring(i, i + 2), 16));
  1417. }
  1418. return out;
  1419. }
  1420. encodeInteger(value) {
  1421. let code;
  1422. if (value >= -107 && value <= 107) {
  1423. code = [value + 139];
  1424. } else if (value >= 108 && value <= 1131) {
  1425. value -= 108;
  1426. code = [(value >> 8) + 247, value & 0xff];
  1427. } else if (value >= -1131 && value <= -108) {
  1428. value = -value - 108;
  1429. code = [(value >> 8) + 251, value & 0xff];
  1430. } else if (value >= -32768 && value <= 32767) {
  1431. code = [0x1c, (value >> 8) & 0xff, value & 0xff];
  1432. } else {
  1433. code = [
  1434. 0x1d,
  1435. (value >> 24) & 0xff,
  1436. (value >> 16) & 0xff,
  1437. (value >> 8) & 0xff,
  1438. value & 0xff,
  1439. ];
  1440. }
  1441. return code;
  1442. }
  1443. compileHeader(header) {
  1444. // `header.hdrSize` can be any value but we only write 4 values
  1445. // so header size is 4 (prevents OTS from rejecting the font).
  1446. return [header.major, header.minor, 4, header.offSize];
  1447. }
  1448. compileNameIndex(names) {
  1449. const nameIndex = new CFFIndex();
  1450. for (const name of names) {
  1451. // OTS doesn't allow names to be over 127 characters.
  1452. const length = Math.min(name.length, 127);
  1453. let sanitizedName = new Array(length);
  1454. for (let j = 0; j < length; j++) {
  1455. // OTS requires chars to be between a range and not certain other
  1456. // chars.
  1457. let char = name[j];
  1458. if (
  1459. char < "!" ||
  1460. char > "~" ||
  1461. char === "[" ||
  1462. char === "]" ||
  1463. char === "(" ||
  1464. char === ")" ||
  1465. char === "{" ||
  1466. char === "}" ||
  1467. char === "<" ||
  1468. char === ">" ||
  1469. char === "/" ||
  1470. char === "%"
  1471. ) {
  1472. char = "_";
  1473. }
  1474. sanitizedName[j] = char;
  1475. }
  1476. sanitizedName = sanitizedName.join("");
  1477. if (sanitizedName === "") {
  1478. sanitizedName = "Bad_Font_Name";
  1479. }
  1480. nameIndex.add(stringToBytes(sanitizedName));
  1481. }
  1482. return this.compileIndex(nameIndex);
  1483. }
  1484. compileTopDicts(dicts, length, removeCidKeys) {
  1485. const fontDictTrackers = [];
  1486. let fdArrayIndex = new CFFIndex();
  1487. for (const fontDict of dicts) {
  1488. if (removeCidKeys) {
  1489. fontDict.removeByName("CIDFontVersion");
  1490. fontDict.removeByName("CIDFontRevision");
  1491. fontDict.removeByName("CIDFontType");
  1492. fontDict.removeByName("CIDCount");
  1493. fontDict.removeByName("UIDBase");
  1494. }
  1495. const fontDictTracker = new CFFOffsetTracker();
  1496. const fontDictData = this.compileDict(fontDict, fontDictTracker);
  1497. fontDictTrackers.push(fontDictTracker);
  1498. fdArrayIndex.add(fontDictData);
  1499. fontDictTracker.offset(length);
  1500. }
  1501. fdArrayIndex = this.compileIndex(fdArrayIndex, fontDictTrackers);
  1502. return {
  1503. trackers: fontDictTrackers,
  1504. output: fdArrayIndex,
  1505. };
  1506. }
  1507. compilePrivateDicts(dicts, trackers, output) {
  1508. for (let i = 0, ii = dicts.length; i < ii; ++i) {
  1509. const fontDict = dicts[i];
  1510. const privateDict = fontDict.privateDict;
  1511. if (!privateDict || !fontDict.hasName("Private")) {
  1512. throw new FormatError("There must be a private dictionary.");
  1513. }
  1514. const privateDictTracker = new CFFOffsetTracker();
  1515. const privateDictData = this.compileDict(privateDict, privateDictTracker);
  1516. let outputLength = output.length;
  1517. privateDictTracker.offset(outputLength);
  1518. if (!privateDictData.length) {
  1519. // The private dictionary was empty, set the output length to zero to
  1520. // ensure the offset length isn't out of bounds in the eyes of the
  1521. // sanitizer.
  1522. outputLength = 0;
  1523. }
  1524. trackers[i].setEntryLocation(
  1525. "Private",
  1526. [privateDictData.length, outputLength],
  1527. output
  1528. );
  1529. output.add(privateDictData);
  1530. if (privateDict.subrsIndex && privateDict.hasName("Subrs")) {
  1531. const subrs = this.compileIndex(privateDict.subrsIndex);
  1532. privateDictTracker.setEntryLocation(
  1533. "Subrs",
  1534. [privateDictData.length],
  1535. output
  1536. );
  1537. output.add(subrs);
  1538. }
  1539. }
  1540. }
  1541. compileDict(dict, offsetTracker) {
  1542. const out = [];
  1543. // The dictionary keys must be in a certain order.
  1544. for (const key of dict.order) {
  1545. if (!(key in dict.values)) {
  1546. continue;
  1547. }
  1548. let values = dict.values[key];
  1549. let types = dict.types[key];
  1550. if (!Array.isArray(types)) {
  1551. types = [types];
  1552. }
  1553. if (!Array.isArray(values)) {
  1554. values = [values];
  1555. }
  1556. // Remove any empty dict values.
  1557. if (values.length === 0) {
  1558. continue;
  1559. }
  1560. for (let j = 0, jj = types.length; j < jj; ++j) {
  1561. const type = types[j];
  1562. const value = values[j];
  1563. switch (type) {
  1564. case "num":
  1565. case "sid":
  1566. out.push(...this.encodeNumber(value));
  1567. break;
  1568. case "offset":
  1569. // For offsets we just insert a 32bit integer so we don't have to
  1570. // deal with figuring out the length of the offset when it gets
  1571. // replaced later on by the compiler.
  1572. const name = dict.keyToNameMap[key];
  1573. // Some offsets have the offset and the length, so just record the
  1574. // position of the first one.
  1575. if (!offsetTracker.isTracking(name)) {
  1576. offsetTracker.track(name, out.length);
  1577. }
  1578. out.push(0x1d, 0, 0, 0, 0);
  1579. break;
  1580. case "array":
  1581. case "delta":
  1582. out.push(...this.encodeNumber(value));
  1583. for (let k = 1, kk = values.length; k < kk; ++k) {
  1584. out.push(...this.encodeNumber(values[k]));
  1585. }
  1586. break;
  1587. default:
  1588. throw new FormatError(`Unknown data type of ${type}`);
  1589. }
  1590. }
  1591. out.push(...dict.opcodes[key]);
  1592. }
  1593. return out;
  1594. }
  1595. compileStringIndex(strings) {
  1596. const stringIndex = new CFFIndex();
  1597. for (const string of strings) {
  1598. stringIndex.add(stringToBytes(string));
  1599. }
  1600. return this.compileIndex(stringIndex);
  1601. }
  1602. compileGlobalSubrIndex() {
  1603. const globalSubrIndex = this.cff.globalSubrIndex;
  1604. this.out.writeByteArray(this.compileIndex(globalSubrIndex));
  1605. }
  1606. compileCharStrings(charStrings) {
  1607. const charStringsIndex = new CFFIndex();
  1608. for (let i = 0; i < charStrings.count; i++) {
  1609. const glyph = charStrings.get(i);
  1610. // If the CharString outline is empty, replace it with .notdef to
  1611. // prevent OTS from rejecting the font (fixes bug1252420.pdf).
  1612. if (glyph.length === 0) {
  1613. charStringsIndex.add(new Uint8Array([0x8b, 0x0e]));
  1614. continue;
  1615. }
  1616. charStringsIndex.add(glyph);
  1617. }
  1618. return this.compileIndex(charStringsIndex);
  1619. }
  1620. compileCharset(charset, numGlyphs, strings, isCIDFont) {
  1621. // Freetype requires the number of charset strings be correct and MacOS
  1622. // requires a valid mapping for printing.
  1623. let out;
  1624. const numGlyphsLessNotDef = numGlyphs - 1;
  1625. if (isCIDFont) {
  1626. // In a CID font, the charset is a mapping of CIDs not SIDs so just
  1627. // create an identity mapping.
  1628. out = new Uint8Array([
  1629. 2, // format
  1630. 0, // first CID upper byte
  1631. 0, // first CID lower byte
  1632. (numGlyphsLessNotDef >> 8) & 0xff,
  1633. numGlyphsLessNotDef & 0xff,
  1634. ]);
  1635. } else {
  1636. const length = 1 + numGlyphsLessNotDef * 2;
  1637. out = new Uint8Array(length);
  1638. out[0] = 0; // format 0
  1639. let charsetIndex = 0;
  1640. const numCharsets = charset.charset.length;
  1641. let warned = false;
  1642. for (let i = 1; i < out.length; i += 2) {
  1643. let sid = 0;
  1644. if (charsetIndex < numCharsets) {
  1645. const name = charset.charset[charsetIndex++];
  1646. sid = strings.getSID(name);
  1647. if (sid === -1) {
  1648. sid = 0;
  1649. if (!warned) {
  1650. warned = true;
  1651. warn(`Couldn't find ${name} in CFF strings`);
  1652. }
  1653. }
  1654. }
  1655. out[i] = (sid >> 8) & 0xff;
  1656. out[i + 1] = sid & 0xff;
  1657. }
  1658. }
  1659. return this.compileTypedArray(out);
  1660. }
  1661. compileEncoding(encoding) {
  1662. return this.compileTypedArray(encoding.raw);
  1663. }
  1664. compileFDSelect(fdSelect) {
  1665. const format = fdSelect.format;
  1666. let out, i;
  1667. switch (format) {
  1668. case 0:
  1669. out = new Uint8Array(1 + fdSelect.fdSelect.length);
  1670. out[0] = format;
  1671. for (i = 0; i < fdSelect.fdSelect.length; i++) {
  1672. out[i + 1] = fdSelect.fdSelect[i];
  1673. }
  1674. break;
  1675. case 3:
  1676. const start = 0;
  1677. let lastFD = fdSelect.fdSelect[0];
  1678. const ranges = [
  1679. format,
  1680. 0, // nRanges place holder
  1681. 0, // nRanges place holder
  1682. (start >> 8) & 0xff,
  1683. start & 0xff,
  1684. lastFD,
  1685. ];
  1686. for (i = 1; i < fdSelect.fdSelect.length; i++) {
  1687. const currentFD = fdSelect.fdSelect[i];
  1688. if (currentFD !== lastFD) {
  1689. ranges.push((i >> 8) & 0xff, i & 0xff, currentFD);
  1690. lastFD = currentFD;
  1691. }
  1692. }
  1693. // 3 bytes are pushed for every range and there are 3 header bytes.
  1694. const numRanges = (ranges.length - 3) / 3;
  1695. ranges[1] = (numRanges >> 8) & 0xff;
  1696. ranges[2] = numRanges & 0xff;
  1697. // sentinel
  1698. ranges.push((i >> 8) & 0xff, i & 0xff);
  1699. out = new Uint8Array(ranges);
  1700. break;
  1701. }
  1702. return this.compileTypedArray(out);
  1703. }
  1704. compileTypedArray(data) {
  1705. const out = [];
  1706. for (let i = 0, ii = data.length; i < ii; ++i) {
  1707. out[i] = data[i];
  1708. }
  1709. return out;
  1710. }
  1711. compileIndex(index, trackers = []) {
  1712. const objects = index.objects;
  1713. // First 2 bytes contains the number of objects contained into this index
  1714. const count = objects.length;
  1715. // If there is no object, just create an index.
  1716. if (count === 0) {
  1717. return [0, 0];
  1718. }
  1719. const data = [(count >> 8) & 0xff, count & 0xff];
  1720. let lastOffset = 1,
  1721. i;
  1722. for (i = 0; i < count; ++i) {
  1723. lastOffset += objects[i].length;
  1724. }
  1725. let offsetSize;
  1726. if (lastOffset < 0x100) {
  1727. offsetSize = 1;
  1728. } else if (lastOffset < 0x10000) {
  1729. offsetSize = 2;
  1730. } else if (lastOffset < 0x1000000) {
  1731. offsetSize = 3;
  1732. } else {
  1733. offsetSize = 4;
  1734. }
  1735. // Next byte contains the offset size use to reference object in the file
  1736. data.push(offsetSize);
  1737. // Add another offset after this one because we need a new offset
  1738. let relativeOffset = 1;
  1739. for (i = 0; i < count + 1; i++) {
  1740. if (offsetSize === 1) {
  1741. data.push(relativeOffset & 0xff);
  1742. } else if (offsetSize === 2) {
  1743. data.push((relativeOffset >> 8) & 0xff, relativeOffset & 0xff);
  1744. } else if (offsetSize === 3) {
  1745. data.push(
  1746. (relativeOffset >> 16) & 0xff,
  1747. (relativeOffset >> 8) & 0xff,
  1748. relativeOffset & 0xff
  1749. );
  1750. } else {
  1751. data.push(
  1752. (relativeOffset >>> 24) & 0xff,
  1753. (relativeOffset >> 16) & 0xff,
  1754. (relativeOffset >> 8) & 0xff,
  1755. relativeOffset & 0xff
  1756. );
  1757. }
  1758. if (objects[i]) {
  1759. relativeOffset += objects[i].length;
  1760. }
  1761. }
  1762. for (i = 0; i < count; i++) {
  1763. // Notify the tracker where the object will be offset in the data.
  1764. if (trackers[i]) {
  1765. trackers[i].offset(data.length);
  1766. }
  1767. data.push(...objects[i]);
  1768. }
  1769. return data;
  1770. }
  1771. }
  1772. export {
  1773. CFF,
  1774. CFFCharset,
  1775. CFFCompiler,
  1776. CFFFDSelect,
  1777. CFFHeader,
  1778. CFFIndex,
  1779. CFFParser,
  1780. CFFPrivateDict,
  1781. CFFStandardStrings,
  1782. CFFStrings,
  1783. CFFTopDict,
  1784. };