fonts.js 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 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.getFontType = getFontType;
  27. exports.IdentityToUnicodeMap = exports.ToUnicodeMap = exports.FontFlags = exports.Font = exports.ErrorFont = exports.SEAC_ANALYSIS_ENABLED = void 0;
  28. var _util = require("../shared/util.js");
  29. var _cff_parser = require("./cff_parser.js");
  30. var _glyphlist = require("./glyphlist.js");
  31. var _encodings = require("./encodings.js");
  32. var _standard_fonts = require("./standard_fonts.js");
  33. var _unicode = require("./unicode.js");
  34. var _core_utils = require("./core_utils.js");
  35. var _font_renderer = require("./font_renderer.js");
  36. var _cmap = require("./cmap.js");
  37. var _stream = require("./stream.js");
  38. var _type1_parser = require("./type1_parser.js");
  39. const PRIVATE_USE_AREAS = [[0xe000, 0xf8ff], [0x100000, 0x10fffd]];
  40. var PDF_GLYPH_SPACE_UNITS = 1000;
  41. var SEAC_ANALYSIS_ENABLED = true;
  42. exports.SEAC_ANALYSIS_ENABLED = SEAC_ANALYSIS_ENABLED;
  43. const EXPORT_DATA_PROPERTIES = ["ascent", "bbox", "black", "bold", "charProcOperatorList", "composite", "data", "defaultVMetrics", "defaultWidth", "descent", "fallbackName", "fontMatrix", "fontType", "isMonospace", "isSerifFont", "isType3Font", "italic", "loadedName", "mimetype", "missingFile", "name", "remeasure", "subtype", "type", "vertical"];
  44. const EXPORT_DATA_EXTRA_PROPERTIES = ["cMap", "defaultEncoding", "differences", "isSymbolicFont", "seacMap", "toFontChar", "toUnicode", "vmetrics", "widths"];
  45. var FontFlags = {
  46. FixedPitch: 1,
  47. Serif: 2,
  48. Symbolic: 4,
  49. Script: 8,
  50. Nonsymbolic: 32,
  51. Italic: 64,
  52. AllCap: 65536,
  53. SmallCap: 131072,
  54. ForceBold: 262144
  55. };
  56. exports.FontFlags = FontFlags;
  57. var MacStandardGlyphOrdering = [".notdef", ".null", "nonmarkingreturn", "space", "exclam", "quotedbl", "numbersign", "dollar", "percent", "ampersand", "quotesingle", "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", "grave", "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", "Adieresis", "Aring", "Ccedilla", "Eacute", "Ntilde", "Odieresis", "Udieresis", "aacute", "agrave", "acircumflex", "adieresis", "atilde", "aring", "ccedilla", "eacute", "egrave", "ecircumflex", "edieresis", "iacute", "igrave", "icircumflex", "idieresis", "ntilde", "oacute", "ograve", "ocircumflex", "odieresis", "otilde", "uacute", "ugrave", "ucircumflex", "udieresis", "dagger", "degree", "cent", "sterling", "section", "bullet", "paragraph", "germandbls", "registered", "copyright", "trademark", "acute", "dieresis", "notequal", "AE", "Oslash", "infinity", "plusminus", "lessequal", "greaterequal", "yen", "mu", "partialdiff", "summation", "product", "pi", "integral", "ordfeminine", "ordmasculine", "Omega", "ae", "oslash", "questiondown", "exclamdown", "logicalnot", "radical", "florin", "approxequal", "Delta", "guillemotleft", "guillemotright", "ellipsis", "nonbreakingspace", "Agrave", "Atilde", "Otilde", "OE", "oe", "endash", "emdash", "quotedblleft", "quotedblright", "quoteleft", "quoteright", "divide", "lozenge", "ydieresis", "Ydieresis", "fraction", "currency", "guilsinglleft", "guilsinglright", "fi", "fl", "daggerdbl", "periodcentered", "quotesinglbase", "quotedblbase", "perthousand", "Acircumflex", "Ecircumflex", "Aacute", "Edieresis", "Egrave", "Iacute", "Icircumflex", "Idieresis", "Igrave", "Oacute", "Ocircumflex", "apple", "Ograve", "Uacute", "Ucircumflex", "Ugrave", "dotlessi", "circumflex", "tilde", "macron", "breve", "dotaccent", "ring", "cedilla", "hungarumlaut", "ogonek", "caron", "Lslash", "lslash", "Scaron", "scaron", "Zcaron", "zcaron", "brokenbar", "Eth", "eth", "Yacute", "yacute", "Thorn", "thorn", "minus", "multiply", "onesuperior", "twosuperior", "threesuperior", "onehalf", "onequarter", "threequarters", "franc", "Gbreve", "gbreve", "Idotaccent", "Scedilla", "scedilla", "Cacute", "cacute", "Ccaron", "ccaron", "dcroat"];
  58. function adjustWidths(properties) {
  59. if (!properties.fontMatrix) {
  60. return;
  61. }
  62. if (properties.fontMatrix[0] === _util.FONT_IDENTITY_MATRIX[0]) {
  63. return;
  64. }
  65. var scale = 0.001 / properties.fontMatrix[0];
  66. var glyphsWidths = properties.widths;
  67. for (var glyph in glyphsWidths) {
  68. glyphsWidths[glyph] *= scale;
  69. }
  70. properties.defaultWidth *= scale;
  71. }
  72. function adjustToUnicode(properties, builtInEncoding) {
  73. if (properties.hasIncludedToUnicodeMap) {
  74. return;
  75. }
  76. if (properties.hasEncoding) {
  77. return;
  78. }
  79. if (builtInEncoding === properties.defaultEncoding) {
  80. return;
  81. }
  82. if (properties.toUnicode instanceof IdentityToUnicodeMap) {
  83. return;
  84. }
  85. var toUnicode = [],
  86. glyphsUnicodeMap = (0, _glyphlist.getGlyphsUnicode)();
  87. for (var charCode in builtInEncoding) {
  88. var glyphName = builtInEncoding[charCode];
  89. var unicode = (0, _unicode.getUnicodeForGlyph)(glyphName, glyphsUnicodeMap);
  90. if (unicode !== -1) {
  91. toUnicode[charCode] = String.fromCharCode(unicode);
  92. }
  93. }
  94. properties.toUnicode.amend(toUnicode);
  95. }
  96. function getFontType(type, subtype) {
  97. switch (type) {
  98. case "Type1":
  99. return subtype === "Type1C" ? _util.FontType.TYPE1C : _util.FontType.TYPE1;
  100. case "CIDFontType0":
  101. return subtype === "CIDFontType0C" ? _util.FontType.CIDFONTTYPE0C : _util.FontType.CIDFONTTYPE0;
  102. case "OpenType":
  103. return _util.FontType.OPENTYPE;
  104. case "TrueType":
  105. return _util.FontType.TRUETYPE;
  106. case "CIDFontType2":
  107. return _util.FontType.CIDFONTTYPE2;
  108. case "MMType1":
  109. return _util.FontType.MMTYPE1;
  110. case "Type0":
  111. return _util.FontType.TYPE0;
  112. default:
  113. return _util.FontType.UNKNOWN;
  114. }
  115. }
  116. function recoverGlyphName(name, glyphsUnicodeMap) {
  117. if (glyphsUnicodeMap[name] !== undefined) {
  118. return name;
  119. }
  120. var unicode = (0, _unicode.getUnicodeForGlyph)(name, glyphsUnicodeMap);
  121. if (unicode !== -1) {
  122. for (var key in glyphsUnicodeMap) {
  123. if (glyphsUnicodeMap[key] === unicode) {
  124. return key;
  125. }
  126. }
  127. }
  128. (0, _util.info)("Unable to recover a standard glyph name for: " + name);
  129. return name;
  130. }
  131. var Glyph = function GlyphClosure() {
  132. function Glyph(fontChar, unicode, accent, width, vmetric, operatorListId, isSpace, isInFont) {
  133. this.fontChar = fontChar;
  134. this.unicode = unicode;
  135. this.accent = accent;
  136. this.width = width;
  137. this.vmetric = vmetric;
  138. this.operatorListId = operatorListId;
  139. this.isSpace = isSpace;
  140. this.isInFont = isInFont;
  141. }
  142. Glyph.prototype.matchesForCache = function (fontChar, unicode, accent, width, vmetric, operatorListId, isSpace, isInFont) {
  143. return this.fontChar === fontChar && this.unicode === unicode && this.accent === accent && this.width === width && this.vmetric === vmetric && this.operatorListId === operatorListId && this.isSpace === isSpace && this.isInFont === isInFont;
  144. };
  145. return Glyph;
  146. }();
  147. var ToUnicodeMap = function ToUnicodeMapClosure() {
  148. function ToUnicodeMap(cmap = []) {
  149. this._map = cmap;
  150. }
  151. ToUnicodeMap.prototype = {
  152. get length() {
  153. return this._map.length;
  154. },
  155. forEach(callback) {
  156. for (var charCode in this._map) {
  157. callback(charCode, this._map[charCode].charCodeAt(0));
  158. }
  159. },
  160. has(i) {
  161. return this._map[i] !== undefined;
  162. },
  163. get(i) {
  164. return this._map[i];
  165. },
  166. charCodeOf(value) {
  167. const map = this._map;
  168. if (map.length <= 0x10000) {
  169. return map.indexOf(value);
  170. }
  171. for (const charCode in map) {
  172. if (map[charCode] === value) {
  173. return charCode | 0;
  174. }
  175. }
  176. return -1;
  177. },
  178. amend(map) {
  179. for (var charCode in map) {
  180. this._map[charCode] = map[charCode];
  181. }
  182. }
  183. };
  184. return ToUnicodeMap;
  185. }();
  186. exports.ToUnicodeMap = ToUnicodeMap;
  187. var IdentityToUnicodeMap = function IdentityToUnicodeMapClosure() {
  188. function IdentityToUnicodeMap(firstChar, lastChar) {
  189. this.firstChar = firstChar;
  190. this.lastChar = lastChar;
  191. }
  192. IdentityToUnicodeMap.prototype = {
  193. get length() {
  194. return this.lastChar + 1 - this.firstChar;
  195. },
  196. forEach(callback) {
  197. for (var i = this.firstChar, ii = this.lastChar; i <= ii; i++) {
  198. callback(i, i);
  199. }
  200. },
  201. has(i) {
  202. return this.firstChar <= i && i <= this.lastChar;
  203. },
  204. get(i) {
  205. if (this.firstChar <= i && i <= this.lastChar) {
  206. return String.fromCharCode(i);
  207. }
  208. return undefined;
  209. },
  210. charCodeOf(v) {
  211. return Number.isInteger(v) && v >= this.firstChar && v <= this.lastChar ? v : -1;
  212. },
  213. amend(map) {
  214. (0, _util.unreachable)("Should not call amend()");
  215. }
  216. };
  217. return IdentityToUnicodeMap;
  218. }();
  219. exports.IdentityToUnicodeMap = IdentityToUnicodeMap;
  220. var OpenTypeFileBuilder = function OpenTypeFileBuilderClosure() {
  221. function writeInt16(dest, offset, num) {
  222. dest[offset] = num >> 8 & 0xff;
  223. dest[offset + 1] = num & 0xff;
  224. }
  225. function writeInt32(dest, offset, num) {
  226. dest[offset] = num >> 24 & 0xff;
  227. dest[offset + 1] = num >> 16 & 0xff;
  228. dest[offset + 2] = num >> 8 & 0xff;
  229. dest[offset + 3] = num & 0xff;
  230. }
  231. function writeData(dest, offset, data) {
  232. var i, ii;
  233. if (data instanceof Uint8Array) {
  234. dest.set(data, offset);
  235. } else if (typeof data === "string") {
  236. for (i = 0, ii = data.length; i < ii; i++) {
  237. dest[offset++] = data.charCodeAt(i) & 0xff;
  238. }
  239. } else {
  240. for (i = 0, ii = data.length; i < ii; i++) {
  241. dest[offset++] = data[i] & 0xff;
  242. }
  243. }
  244. }
  245. function OpenTypeFileBuilder(sfnt) {
  246. this.sfnt = sfnt;
  247. this.tables = Object.create(null);
  248. }
  249. OpenTypeFileBuilder.getSearchParams = function OpenTypeFileBuilder_getSearchParams(entriesCount, entrySize) {
  250. var maxPower2 = 1,
  251. log2 = 0;
  252. while ((maxPower2 ^ entriesCount) > maxPower2) {
  253. maxPower2 <<= 1;
  254. log2++;
  255. }
  256. var searchRange = maxPower2 * entrySize;
  257. return {
  258. range: searchRange,
  259. entry: log2,
  260. rangeShift: entrySize * entriesCount - searchRange
  261. };
  262. };
  263. var OTF_HEADER_SIZE = 12;
  264. var OTF_TABLE_ENTRY_SIZE = 16;
  265. OpenTypeFileBuilder.prototype = {
  266. toArray: function OpenTypeFileBuilder_toArray() {
  267. var sfnt = this.sfnt;
  268. var tables = this.tables;
  269. var tablesNames = Object.keys(tables);
  270. tablesNames.sort();
  271. var numTables = tablesNames.length;
  272. var i, j, jj, table, tableName;
  273. var offset = OTF_HEADER_SIZE + numTables * OTF_TABLE_ENTRY_SIZE;
  274. var tableOffsets = [offset];
  275. for (i = 0; i < numTables; i++) {
  276. table = tables[tablesNames[i]];
  277. var paddedLength = (table.length + 3 & ~3) >>> 0;
  278. offset += paddedLength;
  279. tableOffsets.push(offset);
  280. }
  281. var file = new Uint8Array(offset);
  282. for (i = 0; i < numTables; i++) {
  283. table = tables[tablesNames[i]];
  284. writeData(file, tableOffsets[i], table);
  285. }
  286. if (sfnt === "true") {
  287. sfnt = (0, _util.string32)(0x00010000);
  288. }
  289. file[0] = sfnt.charCodeAt(0) & 0xff;
  290. file[1] = sfnt.charCodeAt(1) & 0xff;
  291. file[2] = sfnt.charCodeAt(2) & 0xff;
  292. file[3] = sfnt.charCodeAt(3) & 0xff;
  293. writeInt16(file, 4, numTables);
  294. var searchParams = OpenTypeFileBuilder.getSearchParams(numTables, 16);
  295. writeInt16(file, 6, searchParams.range);
  296. writeInt16(file, 8, searchParams.entry);
  297. writeInt16(file, 10, searchParams.rangeShift);
  298. offset = OTF_HEADER_SIZE;
  299. for (i = 0; i < numTables; i++) {
  300. tableName = tablesNames[i];
  301. file[offset] = tableName.charCodeAt(0) & 0xff;
  302. file[offset + 1] = tableName.charCodeAt(1) & 0xff;
  303. file[offset + 2] = tableName.charCodeAt(2) & 0xff;
  304. file[offset + 3] = tableName.charCodeAt(3) & 0xff;
  305. var checksum = 0;
  306. for (j = tableOffsets[i], jj = tableOffsets[i + 1]; j < jj; j += 4) {
  307. var quad = (0, _core_utils.readUint32)(file, j);
  308. checksum = checksum + quad >>> 0;
  309. }
  310. writeInt32(file, offset + 4, checksum);
  311. writeInt32(file, offset + 8, tableOffsets[i]);
  312. writeInt32(file, offset + 12, tables[tableName].length);
  313. offset += OTF_TABLE_ENTRY_SIZE;
  314. }
  315. return file;
  316. },
  317. addTable: function OpenTypeFileBuilder_addTable(tag, data) {
  318. if (tag in this.tables) {
  319. throw new Error("Table " + tag + " already exists");
  320. }
  321. this.tables[tag] = data;
  322. }
  323. };
  324. return OpenTypeFileBuilder;
  325. }();
  326. var Font = function FontClosure() {
  327. function Font(name, file, properties) {
  328. var charCode;
  329. this.name = name;
  330. this.loadedName = properties.loadedName;
  331. this.isType3Font = properties.isType3Font;
  332. this.missingFile = false;
  333. this.glyphCache = Object.create(null);
  334. this.isSerifFont = !!(properties.flags & FontFlags.Serif);
  335. this.isSymbolicFont = !!(properties.flags & FontFlags.Symbolic);
  336. this.isMonospace = !!(properties.flags & FontFlags.FixedPitch);
  337. var type = properties.type;
  338. var subtype = properties.subtype;
  339. this.type = type;
  340. this.subtype = subtype;
  341. let fallbackName = "sans-serif";
  342. if (this.isMonospace) {
  343. fallbackName = "monospace";
  344. } else if (this.isSerifFont) {
  345. fallbackName = "serif";
  346. }
  347. this.fallbackName = fallbackName;
  348. this.differences = properties.differences;
  349. this.widths = properties.widths;
  350. this.defaultWidth = properties.defaultWidth;
  351. this.composite = properties.composite;
  352. this.cMap = properties.cMap;
  353. this.ascent = properties.ascent / PDF_GLYPH_SPACE_UNITS;
  354. this.descent = properties.descent / PDF_GLYPH_SPACE_UNITS;
  355. this.fontMatrix = properties.fontMatrix;
  356. this.bbox = properties.bbox;
  357. this.defaultEncoding = properties.defaultEncoding;
  358. this.toUnicode = properties.toUnicode;
  359. this.fallbackToUnicode = properties.fallbackToUnicode || new ToUnicodeMap();
  360. this.toFontChar = [];
  361. if (properties.type === "Type3") {
  362. for (charCode = 0; charCode < 256; charCode++) {
  363. this.toFontChar[charCode] = this.differences[charCode] || properties.defaultEncoding[charCode];
  364. }
  365. this.fontType = _util.FontType.TYPE3;
  366. return;
  367. }
  368. this.cidEncoding = properties.cidEncoding;
  369. this.vertical = !!properties.vertical;
  370. if (this.vertical) {
  371. this.vmetrics = properties.vmetrics;
  372. this.defaultVMetrics = properties.defaultVMetrics;
  373. }
  374. if (!file || file.isEmpty) {
  375. if (file) {
  376. (0, _util.warn)('Font file is empty in "' + name + '" (' + this.loadedName + ")");
  377. }
  378. this.fallbackToSystemFont();
  379. return;
  380. }
  381. [type, subtype] = getFontFileType(file, properties);
  382. if (type !== this.type || subtype !== this.subtype) {
  383. (0, _util.info)("Inconsistent font file Type/SubType, expected: " + `${this.type}/${this.subtype} but found: ${type}/${subtype}.`);
  384. }
  385. try {
  386. var data;
  387. switch (type) {
  388. case "MMType1":
  389. (0, _util.info)("MMType1 font (" + name + "), falling back to Type1.");
  390. case "Type1":
  391. case "CIDFontType0":
  392. this.mimetype = "font/opentype";
  393. var cff = subtype === "Type1C" || subtype === "CIDFontType0C" ? new CFFFont(file, properties) : new Type1Font(name, file, properties);
  394. adjustWidths(properties);
  395. data = this.convert(name, cff, properties);
  396. break;
  397. case "OpenType":
  398. case "TrueType":
  399. case "CIDFontType2":
  400. this.mimetype = "font/opentype";
  401. data = this.checkAndRepair(name, file, properties);
  402. if (this.isOpenType) {
  403. adjustWidths(properties);
  404. type = "OpenType";
  405. }
  406. break;
  407. default:
  408. throw new _util.FormatError(`Font ${type} is not supported`);
  409. }
  410. } catch (e) {
  411. (0, _util.warn)(e);
  412. this.fallbackToSystemFont();
  413. return;
  414. }
  415. this.data = data;
  416. this.fontType = getFontType(type, subtype);
  417. this.fontMatrix = properties.fontMatrix;
  418. this.widths = properties.widths;
  419. this.defaultWidth = properties.defaultWidth;
  420. this.toUnicode = properties.toUnicode;
  421. this.seacMap = properties.seacMap;
  422. }
  423. Font.getFontID = function () {
  424. var ID = 1;
  425. return function Font_getFontID() {
  426. return String(ID++);
  427. };
  428. }();
  429. function int16(b0, b1) {
  430. return (b0 << 8) + b1;
  431. }
  432. function writeSignedInt16(bytes, index, value) {
  433. bytes[index + 1] = value;
  434. bytes[index] = value >>> 8;
  435. }
  436. function signedInt16(b0, b1) {
  437. var value = (b0 << 8) + b1;
  438. return value & 1 << 15 ? value - 0x10000 : value;
  439. }
  440. function int32(b0, b1, b2, b3) {
  441. return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
  442. }
  443. function string16(value) {
  444. return String.fromCharCode(value >> 8 & 0xff, value & 0xff);
  445. }
  446. function safeString16(value) {
  447. if (value > 0x7fff) {
  448. value = 0x7fff;
  449. } else if (value < -0x8000) {
  450. value = -0x8000;
  451. }
  452. return String.fromCharCode(value >> 8 & 0xff, value & 0xff);
  453. }
  454. function isTrueTypeFile(file) {
  455. var header = file.peekBytes(4);
  456. return (0, _core_utils.readUint32)(header, 0) === 0x00010000 || (0, _util.bytesToString)(header) === "true";
  457. }
  458. function isTrueTypeCollectionFile(file) {
  459. const header = file.peekBytes(4);
  460. return (0, _util.bytesToString)(header) === "ttcf";
  461. }
  462. function isOpenTypeFile(file) {
  463. var header = file.peekBytes(4);
  464. return (0, _util.bytesToString)(header) === "OTTO";
  465. }
  466. function isType1File(file) {
  467. var header = file.peekBytes(2);
  468. if (header[0] === 0x25 && header[1] === 0x21) {
  469. return true;
  470. }
  471. if (header[0] === 0x80 && header[1] === 0x01) {
  472. return true;
  473. }
  474. return false;
  475. }
  476. function isCFFFile(file) {
  477. const header = file.peekBytes(4);
  478. if (header[0] >= 1 && header[3] >= 1 && header[3] <= 4) {
  479. return true;
  480. }
  481. return false;
  482. }
  483. function getFontFileType(file, {
  484. type,
  485. subtype,
  486. composite
  487. }) {
  488. let fileType, fileSubtype;
  489. if (isTrueTypeFile(file) || isTrueTypeCollectionFile(file)) {
  490. if (composite) {
  491. fileType = "CIDFontType2";
  492. } else {
  493. fileType = "TrueType";
  494. }
  495. } else if (isOpenTypeFile(file)) {
  496. if (composite) {
  497. fileType = "CIDFontType2";
  498. } else {
  499. fileType = "OpenType";
  500. }
  501. } else if (isType1File(file)) {
  502. if (composite) {
  503. fileType = "CIDFontType0";
  504. } else {
  505. fileType = type === "MMType1" ? "MMType1" : "Type1";
  506. }
  507. } else if (isCFFFile(file)) {
  508. if (composite) {
  509. fileType = "CIDFontType0";
  510. fileSubtype = "CIDFontType0C";
  511. } else {
  512. fileType = type === "MMType1" ? "MMType1" : "Type1";
  513. fileSubtype = "Type1C";
  514. }
  515. } else {
  516. (0, _util.warn)("getFontFileType: Unable to detect correct font file Type/Subtype.");
  517. fileType = type;
  518. fileSubtype = subtype;
  519. }
  520. return [fileType, fileSubtype];
  521. }
  522. function buildToFontChar(encoding, glyphsUnicodeMap, differences) {
  523. var toFontChar = [],
  524. unicode;
  525. for (var i = 0, ii = encoding.length; i < ii; i++) {
  526. unicode = (0, _unicode.getUnicodeForGlyph)(encoding[i], glyphsUnicodeMap);
  527. if (unicode !== -1) {
  528. toFontChar[i] = unicode;
  529. }
  530. }
  531. for (var charCode in differences) {
  532. unicode = (0, _unicode.getUnicodeForGlyph)(differences[charCode], glyphsUnicodeMap);
  533. if (unicode !== -1) {
  534. toFontChar[+charCode] = unicode;
  535. }
  536. }
  537. return toFontChar;
  538. }
  539. function adjustMapping(charCodeToGlyphId, hasGlyph, newGlyphZeroId) {
  540. var newMap = Object.create(null);
  541. var toFontChar = [];
  542. var privateUseAreaIndex = 0;
  543. var nextAvailableFontCharCode = PRIVATE_USE_AREAS[privateUseAreaIndex][0];
  544. var privateUseOffetEnd = PRIVATE_USE_AREAS[privateUseAreaIndex][1];
  545. for (var originalCharCode in charCodeToGlyphId) {
  546. originalCharCode |= 0;
  547. var glyphId = charCodeToGlyphId[originalCharCode];
  548. if (!hasGlyph(glyphId)) {
  549. continue;
  550. }
  551. if (nextAvailableFontCharCode > privateUseOffetEnd) {
  552. privateUseAreaIndex++;
  553. if (privateUseAreaIndex >= PRIVATE_USE_AREAS.length) {
  554. (0, _util.warn)("Ran out of space in font private use area.");
  555. break;
  556. }
  557. nextAvailableFontCharCode = PRIVATE_USE_AREAS[privateUseAreaIndex][0];
  558. privateUseOffetEnd = PRIVATE_USE_AREAS[privateUseAreaIndex][1];
  559. }
  560. var fontCharCode = nextAvailableFontCharCode++;
  561. if (glyphId === 0) {
  562. glyphId = newGlyphZeroId;
  563. }
  564. newMap[fontCharCode] = glyphId;
  565. toFontChar[originalCharCode] = fontCharCode;
  566. }
  567. return {
  568. toFontChar,
  569. charCodeToGlyphId: newMap,
  570. nextAvailableFontCharCode
  571. };
  572. }
  573. function getRanges(glyphs, numGlyphs) {
  574. var codes = [];
  575. for (var charCode in glyphs) {
  576. if (glyphs[charCode] >= numGlyphs) {
  577. continue;
  578. }
  579. codes.push({
  580. fontCharCode: charCode | 0,
  581. glyphId: glyphs[charCode]
  582. });
  583. }
  584. if (codes.length === 0) {
  585. codes.push({
  586. fontCharCode: 0,
  587. glyphId: 0
  588. });
  589. }
  590. codes.sort(function fontGetRangesSort(a, b) {
  591. return a.fontCharCode - b.fontCharCode;
  592. });
  593. var ranges = [];
  594. var length = codes.length;
  595. for (var n = 0; n < length;) {
  596. var start = codes[n].fontCharCode;
  597. var codeIndices = [codes[n].glyphId];
  598. ++n;
  599. var end = start;
  600. while (n < length && end + 1 === codes[n].fontCharCode) {
  601. codeIndices.push(codes[n].glyphId);
  602. ++end;
  603. ++n;
  604. if (end === 0xffff) {
  605. break;
  606. }
  607. }
  608. ranges.push([start, end, codeIndices]);
  609. }
  610. return ranges;
  611. }
  612. function createCmapTable(glyphs, numGlyphs) {
  613. var ranges = getRanges(glyphs, numGlyphs);
  614. var numTables = ranges[ranges.length - 1][1] > 0xffff ? 2 : 1;
  615. var cmap = "\x00\x00" + string16(numTables) + "\x00\x03" + "\x00\x01" + (0, _util.string32)(4 + numTables * 8);
  616. var i, ii, j, jj;
  617. for (i = ranges.length - 1; i >= 0; --i) {
  618. if (ranges[i][0] <= 0xffff) {
  619. break;
  620. }
  621. }
  622. var bmpLength = i + 1;
  623. if (ranges[i][0] < 0xffff && ranges[i][1] === 0xffff) {
  624. ranges[i][1] = 0xfffe;
  625. }
  626. var trailingRangesCount = ranges[i][1] < 0xffff ? 1 : 0;
  627. var segCount = bmpLength + trailingRangesCount;
  628. var searchParams = OpenTypeFileBuilder.getSearchParams(segCount, 2);
  629. var startCount = "";
  630. var endCount = "";
  631. var idDeltas = "";
  632. var idRangeOffsets = "";
  633. var glyphsIds = "";
  634. var bias = 0;
  635. var range, start, end, codes;
  636. for (i = 0, ii = bmpLength; i < ii; i++) {
  637. range = ranges[i];
  638. start = range[0];
  639. end = range[1];
  640. startCount += string16(start);
  641. endCount += string16(end);
  642. codes = range[2];
  643. var contiguous = true;
  644. for (j = 1, jj = codes.length; j < jj; ++j) {
  645. if (codes[j] !== codes[j - 1] + 1) {
  646. contiguous = false;
  647. break;
  648. }
  649. }
  650. if (!contiguous) {
  651. var offset = (segCount - i) * 2 + bias * 2;
  652. bias += end - start + 1;
  653. idDeltas += string16(0);
  654. idRangeOffsets += string16(offset);
  655. for (j = 0, jj = codes.length; j < jj; ++j) {
  656. glyphsIds += string16(codes[j]);
  657. }
  658. } else {
  659. var startCode = codes[0];
  660. idDeltas += string16(startCode - start & 0xffff);
  661. idRangeOffsets += string16(0);
  662. }
  663. }
  664. if (trailingRangesCount > 0) {
  665. endCount += "\xFF\xFF";
  666. startCount += "\xFF\xFF";
  667. idDeltas += "\x00\x01";
  668. idRangeOffsets += "\x00\x00";
  669. }
  670. var format314 = "\x00\x00" + string16(2 * segCount) + string16(searchParams.range) + string16(searchParams.entry) + string16(searchParams.rangeShift) + endCount + "\x00\x00" + startCount + idDeltas + idRangeOffsets + glyphsIds;
  671. var format31012 = "";
  672. var header31012 = "";
  673. if (numTables > 1) {
  674. cmap += "\x00\x03" + "\x00\x0A" + (0, _util.string32)(4 + numTables * 8 + 4 + format314.length);
  675. format31012 = "";
  676. for (i = 0, ii = ranges.length; i < ii; i++) {
  677. range = ranges[i];
  678. start = range[0];
  679. codes = range[2];
  680. var code = codes[0];
  681. for (j = 1, jj = codes.length; j < jj; ++j) {
  682. if (codes[j] !== codes[j - 1] + 1) {
  683. end = range[0] + j - 1;
  684. format31012 += (0, _util.string32)(start) + (0, _util.string32)(end) + (0, _util.string32)(code);
  685. start = end + 1;
  686. code = codes[j];
  687. }
  688. }
  689. format31012 += (0, _util.string32)(start) + (0, _util.string32)(range[1]) + (0, _util.string32)(code);
  690. }
  691. header31012 = "\x00\x0C" + "\x00\x00" + (0, _util.string32)(format31012.length + 16) + "\x00\x00\x00\x00" + (0, _util.string32)(format31012.length / 12);
  692. }
  693. return cmap + "\x00\x04" + string16(format314.length + 4) + format314 + header31012 + format31012;
  694. }
  695. function validateOS2Table(os2, file) {
  696. file.pos = (file.start || 0) + os2.offset;
  697. var version = file.getUint16();
  698. file.skip(60);
  699. var selection = file.getUint16();
  700. if (version < 4 && selection & 0x0300) {
  701. return false;
  702. }
  703. var firstChar = file.getUint16();
  704. var lastChar = file.getUint16();
  705. if (firstChar > lastChar) {
  706. return false;
  707. }
  708. file.skip(6);
  709. var usWinAscent = file.getUint16();
  710. if (usWinAscent === 0) {
  711. return false;
  712. }
  713. os2.data[8] = os2.data[9] = 0;
  714. return true;
  715. }
  716. function createOS2Table(properties, charstrings, override) {
  717. override = override || {
  718. unitsPerEm: 0,
  719. yMax: 0,
  720. yMin: 0,
  721. ascent: 0,
  722. descent: 0
  723. };
  724. var ulUnicodeRange1 = 0;
  725. var ulUnicodeRange2 = 0;
  726. var ulUnicodeRange3 = 0;
  727. var ulUnicodeRange4 = 0;
  728. var firstCharIndex = null;
  729. var lastCharIndex = 0;
  730. if (charstrings) {
  731. for (var code in charstrings) {
  732. code |= 0;
  733. if (firstCharIndex > code || !firstCharIndex) {
  734. firstCharIndex = code;
  735. }
  736. if (lastCharIndex < code) {
  737. lastCharIndex = code;
  738. }
  739. var position = (0, _unicode.getUnicodeRangeFor)(code);
  740. if (position < 32) {
  741. ulUnicodeRange1 |= 1 << position;
  742. } else if (position < 64) {
  743. ulUnicodeRange2 |= 1 << position - 32;
  744. } else if (position < 96) {
  745. ulUnicodeRange3 |= 1 << position - 64;
  746. } else if (position < 123) {
  747. ulUnicodeRange4 |= 1 << position - 96;
  748. } else {
  749. throw new _util.FormatError("Unicode ranges Bits > 123 are reserved for internal usage");
  750. }
  751. }
  752. if (lastCharIndex > 0xffff) {
  753. lastCharIndex = 0xffff;
  754. }
  755. } else {
  756. firstCharIndex = 0;
  757. lastCharIndex = 255;
  758. }
  759. var bbox = properties.bbox || [0, 0, 0, 0];
  760. var unitsPerEm = override.unitsPerEm || 1 / (properties.fontMatrix || _util.FONT_IDENTITY_MATRIX)[0];
  761. var scale = properties.ascentScaled ? 1.0 : unitsPerEm / PDF_GLYPH_SPACE_UNITS;
  762. var typoAscent = override.ascent || Math.round(scale * (properties.ascent || bbox[3]));
  763. var typoDescent = override.descent || Math.round(scale * (properties.descent || bbox[1]));
  764. if (typoDescent > 0 && properties.descent > 0 && bbox[1] < 0) {
  765. typoDescent = -typoDescent;
  766. }
  767. var winAscent = override.yMax || typoAscent;
  768. var winDescent = -override.yMin || -typoDescent;
  769. return "\x00\x03" + "\x02\x24" + "\x01\xF4" + "\x00\x05" + "\x00\x00" + "\x02\x8A" + "\x02\xBB" + "\x00\x00" + "\x00\x8C" + "\x02\x8A" + "\x02\xBB" + "\x00\x00" + "\x01\xDF" + "\x00\x31" + "\x01\x02" + "\x00\x00" + "\x00\x00\x06" + String.fromCharCode(properties.fixedPitch ? 0x09 : 0x00) + "\x00\x00\x00\x00\x00\x00" + (0, _util.string32)(ulUnicodeRange1) + (0, _util.string32)(ulUnicodeRange2) + (0, _util.string32)(ulUnicodeRange3) + (0, _util.string32)(ulUnicodeRange4) + "\x2A\x32\x31\x2A" + string16(properties.italicAngle ? 1 : 0) + string16(firstCharIndex || properties.firstChar) + string16(lastCharIndex || properties.lastChar) + string16(typoAscent) + string16(typoDescent) + "\x00\x64" + string16(winAscent) + string16(winDescent) + "\x00\x00\x00\x00" + "\x00\x00\x00\x00" + string16(properties.xHeight) + string16(properties.capHeight) + string16(0) + string16(firstCharIndex || properties.firstChar) + "\x00\x03";
  770. }
  771. function createPostTable(properties) {
  772. var angle = Math.floor(properties.italicAngle * 2 ** 16);
  773. return "\x00\x03\x00\x00" + (0, _util.string32)(angle) + "\x00\x00" + "\x00\x00" + (0, _util.string32)(properties.fixedPitch) + "\x00\x00\x00\x00" + "\x00\x00\x00\x00" + "\x00\x00\x00\x00" + "\x00\x00\x00\x00";
  774. }
  775. function createNameTable(name, proto) {
  776. if (!proto) {
  777. proto = [[], []];
  778. }
  779. var strings = [proto[0][0] || "Original licence", proto[0][1] || name, proto[0][2] || "Unknown", proto[0][3] || "uniqueID", proto[0][4] || name, proto[0][5] || "Version 0.11", proto[0][6] || "", proto[0][7] || "Unknown", proto[0][8] || "Unknown", proto[0][9] || "Unknown"];
  780. var stringsUnicode = [];
  781. var i, ii, j, jj, str;
  782. for (i = 0, ii = strings.length; i < ii; i++) {
  783. str = proto[1][i] || strings[i];
  784. var strBufUnicode = [];
  785. for (j = 0, jj = str.length; j < jj; j++) {
  786. strBufUnicode.push(string16(str.charCodeAt(j)));
  787. }
  788. stringsUnicode.push(strBufUnicode.join(""));
  789. }
  790. var names = [strings, stringsUnicode];
  791. var platforms = ["\x00\x01", "\x00\x03"];
  792. var encodings = ["\x00\x00", "\x00\x01"];
  793. var languages = ["\x00\x00", "\x04\x09"];
  794. var namesRecordCount = strings.length * platforms.length;
  795. var nameTable = "\x00\x00" + string16(namesRecordCount) + string16(namesRecordCount * 12 + 6);
  796. var strOffset = 0;
  797. for (i = 0, ii = platforms.length; i < ii; i++) {
  798. var strs = names[i];
  799. for (j = 0, jj = strs.length; j < jj; j++) {
  800. str = strs[j];
  801. var nameRecord = platforms[i] + encodings[i] + languages[i] + string16(j) + string16(str.length) + string16(strOffset);
  802. nameTable += nameRecord;
  803. strOffset += str.length;
  804. }
  805. }
  806. nameTable += strings.join("") + stringsUnicode.join("");
  807. return nameTable;
  808. }
  809. Font.prototype = {
  810. name: null,
  811. font: null,
  812. mimetype: null,
  813. disableFontFace: false,
  814. get renderer() {
  815. var renderer = _font_renderer.FontRendererFactory.create(this, SEAC_ANALYSIS_ENABLED);
  816. return (0, _util.shadow)(this, "renderer", renderer);
  817. },
  818. exportData(extraProperties = false) {
  819. const exportDataProperties = extraProperties ? [...EXPORT_DATA_PROPERTIES, ...EXPORT_DATA_EXTRA_PROPERTIES] : EXPORT_DATA_PROPERTIES;
  820. const data = Object.create(null);
  821. let property, value;
  822. for (property of exportDataProperties) {
  823. value = this[property];
  824. if (value !== undefined) {
  825. data[property] = value;
  826. }
  827. }
  828. return data;
  829. },
  830. fallbackToSystemFont: function Font_fallbackToSystemFont() {
  831. this.missingFile = true;
  832. var name = this.name;
  833. var type = this.type;
  834. var subtype = this.subtype;
  835. let fontName = name.replace(/[,_]/g, "-").replace(/\s/g, "");
  836. var stdFontMap = (0, _standard_fonts.getStdFontMap)(),
  837. nonStdFontMap = (0, _standard_fonts.getNonStdFontMap)();
  838. var isStandardFont = !!stdFontMap[fontName] || !!(nonStdFontMap[fontName] && stdFontMap[nonStdFontMap[fontName]]);
  839. fontName = stdFontMap[fontName] || nonStdFontMap[fontName] || fontName;
  840. this.bold = fontName.search(/bold/gi) !== -1;
  841. this.italic = fontName.search(/oblique/gi) !== -1 || fontName.search(/italic/gi) !== -1;
  842. this.black = name.search(/Black/g) !== -1;
  843. this.remeasure = Object.keys(this.widths).length > 0;
  844. if (isStandardFont && type === "CIDFontType2" && this.cidEncoding.startsWith("Identity-")) {
  845. const GlyphMapForStandardFonts = (0, _standard_fonts.getGlyphMapForStandardFonts)();
  846. const map = [];
  847. for (const charCode in GlyphMapForStandardFonts) {
  848. map[+charCode] = GlyphMapForStandardFonts[charCode];
  849. }
  850. if (/Arial-?Black/i.test(name)) {
  851. var SupplementalGlyphMapForArialBlack = (0, _standard_fonts.getSupplementalGlyphMapForArialBlack)();
  852. for (const charCode in SupplementalGlyphMapForArialBlack) {
  853. map[+charCode] = SupplementalGlyphMapForArialBlack[charCode];
  854. }
  855. } else if (/Calibri/i.test(name)) {
  856. const SupplementalGlyphMapForCalibri = (0, _standard_fonts.getSupplementalGlyphMapForCalibri)();
  857. for (const charCode in SupplementalGlyphMapForCalibri) {
  858. map[+charCode] = SupplementalGlyphMapForCalibri[charCode];
  859. }
  860. }
  861. var isIdentityUnicode = this.toUnicode instanceof IdentityToUnicodeMap;
  862. if (!isIdentityUnicode) {
  863. this.toUnicode.forEach(function (charCode, unicodeCharCode) {
  864. map[+charCode] = unicodeCharCode;
  865. });
  866. }
  867. this.toFontChar = map;
  868. this.toUnicode = new ToUnicodeMap(map);
  869. } else if (/Symbol/i.test(fontName)) {
  870. this.toFontChar = buildToFontChar(_encodings.SymbolSetEncoding, (0, _glyphlist.getGlyphsUnicode)(), this.differences);
  871. } else if (/Dingbats/i.test(fontName)) {
  872. if (/Wingdings/i.test(name)) {
  873. (0, _util.warn)("Non-embedded Wingdings font, falling back to ZapfDingbats.");
  874. }
  875. this.toFontChar = buildToFontChar(_encodings.ZapfDingbatsEncoding, (0, _glyphlist.getDingbatsGlyphsUnicode)(), this.differences);
  876. } else if (isStandardFont) {
  877. this.toFontChar = buildToFontChar(this.defaultEncoding, (0, _glyphlist.getGlyphsUnicode)(), this.differences);
  878. } else {
  879. const glyphsUnicodeMap = (0, _glyphlist.getGlyphsUnicode)();
  880. const map = [];
  881. this.toUnicode.forEach((charCode, unicodeCharCode) => {
  882. if (!this.composite) {
  883. var glyphName = this.differences[charCode] || this.defaultEncoding[charCode];
  884. const unicode = (0, _unicode.getUnicodeForGlyph)(glyphName, glyphsUnicodeMap);
  885. if (unicode !== -1) {
  886. unicodeCharCode = unicode;
  887. }
  888. }
  889. map[+charCode] = unicodeCharCode;
  890. });
  891. if (this.composite && this.toUnicode instanceof IdentityToUnicodeMap) {
  892. if (/Verdana/i.test(name)) {
  893. const GlyphMapForStandardFonts = (0, _standard_fonts.getGlyphMapForStandardFonts)();
  894. for (const charCode in GlyphMapForStandardFonts) {
  895. map[+charCode] = GlyphMapForStandardFonts[charCode];
  896. }
  897. }
  898. }
  899. this.toFontChar = map;
  900. }
  901. this.loadedName = fontName.split("-")[0];
  902. this.fontType = getFontType(type, subtype);
  903. },
  904. checkAndRepair: function Font_checkAndRepair(name, font, properties) {
  905. const VALID_TABLES = ["OS/2", "cmap", "head", "hhea", "hmtx", "maxp", "name", "post", "loca", "glyf", "fpgm", "prep", "cvt ", "CFF "];
  906. function readTables(file, numTables) {
  907. const tables = Object.create(null);
  908. tables["OS/2"] = null;
  909. tables.cmap = null;
  910. tables.head = null;
  911. tables.hhea = null;
  912. tables.hmtx = null;
  913. tables.maxp = null;
  914. tables.name = null;
  915. tables.post = null;
  916. for (let i = 0; i < numTables; i++) {
  917. const table = readTableEntry(file);
  918. if (!VALID_TABLES.includes(table.tag)) {
  919. continue;
  920. }
  921. if (table.length === 0) {
  922. continue;
  923. }
  924. tables[table.tag] = table;
  925. }
  926. return tables;
  927. }
  928. function readTableEntry(file) {
  929. var tag = (0, _util.bytesToString)(file.getBytes(4));
  930. var checksum = file.getInt32() >>> 0;
  931. var offset = file.getInt32() >>> 0;
  932. var length = file.getInt32() >>> 0;
  933. var previousPosition = file.pos;
  934. file.pos = file.start ? file.start : 0;
  935. file.skip(offset);
  936. var data = file.getBytes(length);
  937. file.pos = previousPosition;
  938. if (tag === "head") {
  939. data[8] = data[9] = data[10] = data[11] = 0;
  940. data[17] |= 0x20;
  941. }
  942. return {
  943. tag,
  944. checksum,
  945. length,
  946. offset,
  947. data
  948. };
  949. }
  950. function readOpenTypeHeader(ttf) {
  951. return {
  952. version: (0, _util.bytesToString)(ttf.getBytes(4)),
  953. numTables: ttf.getUint16(),
  954. searchRange: ttf.getUint16(),
  955. entrySelector: ttf.getUint16(),
  956. rangeShift: ttf.getUint16()
  957. };
  958. }
  959. function readTrueTypeCollectionHeader(ttc) {
  960. const ttcTag = (0, _util.bytesToString)(ttc.getBytes(4));
  961. (0, _util.assert)(ttcTag === "ttcf", "Must be a TrueType Collection font.");
  962. const majorVersion = ttc.getUint16();
  963. const minorVersion = ttc.getUint16();
  964. const numFonts = ttc.getInt32() >>> 0;
  965. const offsetTable = [];
  966. for (let i = 0; i < numFonts; i++) {
  967. offsetTable.push(ttc.getInt32() >>> 0);
  968. }
  969. const header = {
  970. ttcTag,
  971. majorVersion,
  972. minorVersion,
  973. numFonts,
  974. offsetTable
  975. };
  976. switch (majorVersion) {
  977. case 1:
  978. return header;
  979. case 2:
  980. header.dsigTag = ttc.getInt32() >>> 0;
  981. header.dsigLength = ttc.getInt32() >>> 0;
  982. header.dsigOffset = ttc.getInt32() >>> 0;
  983. return header;
  984. }
  985. throw new _util.FormatError(`Invalid TrueType Collection majorVersion: ${majorVersion}.`);
  986. }
  987. function readTrueTypeCollectionData(ttc, fontName) {
  988. const {
  989. numFonts,
  990. offsetTable
  991. } = readTrueTypeCollectionHeader(ttc);
  992. for (let i = 0; i < numFonts; i++) {
  993. ttc.pos = (ttc.start || 0) + offsetTable[i];
  994. const potentialHeader = readOpenTypeHeader(ttc);
  995. const potentialTables = readTables(ttc, potentialHeader.numTables);
  996. if (!potentialTables.name) {
  997. throw new _util.FormatError('TrueType Collection font must contain a "name" table.');
  998. }
  999. const nameTable = readNameTable(potentialTables.name);
  1000. for (let j = 0, jj = nameTable.length; j < jj; j++) {
  1001. for (let k = 0, kk = nameTable[j].length; k < kk; k++) {
  1002. const nameEntry = nameTable[j][k];
  1003. if (nameEntry && nameEntry.replace(/\s/g, "") === fontName) {
  1004. return {
  1005. header: potentialHeader,
  1006. tables: potentialTables
  1007. };
  1008. }
  1009. }
  1010. }
  1011. }
  1012. throw new _util.FormatError(`TrueType Collection does not contain "${fontName}" font.`);
  1013. }
  1014. function readCmapTable(cmap, file, isSymbolicFont, hasEncoding) {
  1015. if (!cmap) {
  1016. (0, _util.warn)("No cmap table available.");
  1017. return {
  1018. platformId: -1,
  1019. encodingId: -1,
  1020. mappings: [],
  1021. hasShortCmap: false
  1022. };
  1023. }
  1024. var segment;
  1025. var start = (file.start ? file.start : 0) + cmap.offset;
  1026. file.pos = start;
  1027. file.skip(2);
  1028. var numTables = file.getUint16();
  1029. var potentialTable;
  1030. var canBreak = false;
  1031. for (var i = 0; i < numTables; i++) {
  1032. var platformId = file.getUint16();
  1033. var encodingId = file.getUint16();
  1034. var offset = file.getInt32() >>> 0;
  1035. var useTable = false;
  1036. if (potentialTable && potentialTable.platformId === platformId && potentialTable.encodingId === encodingId) {
  1037. continue;
  1038. }
  1039. if (platformId === 0 && encodingId === 0) {
  1040. useTable = true;
  1041. } else if (platformId === 1 && encodingId === 0) {
  1042. useTable = true;
  1043. } else if (platformId === 3 && encodingId === 1 && (hasEncoding || !potentialTable)) {
  1044. useTable = true;
  1045. if (!isSymbolicFont) {
  1046. canBreak = true;
  1047. }
  1048. } else if (isSymbolicFont && platformId === 3 && encodingId === 0) {
  1049. useTable = true;
  1050. canBreak = true;
  1051. }
  1052. if (useTable) {
  1053. potentialTable = {
  1054. platformId,
  1055. encodingId,
  1056. offset
  1057. };
  1058. }
  1059. if (canBreak) {
  1060. break;
  1061. }
  1062. }
  1063. if (potentialTable) {
  1064. file.pos = start + potentialTable.offset;
  1065. }
  1066. if (!potentialTable || file.peekByte() === -1) {
  1067. (0, _util.warn)("Could not find a preferred cmap table.");
  1068. return {
  1069. platformId: -1,
  1070. encodingId: -1,
  1071. mappings: [],
  1072. hasShortCmap: false
  1073. };
  1074. }
  1075. var format = file.getUint16();
  1076. file.skip(2 + 2);
  1077. var hasShortCmap = false;
  1078. var mappings = [];
  1079. var j, glyphId;
  1080. if (format === 0) {
  1081. for (j = 0; j < 256; j++) {
  1082. var index = file.getByte();
  1083. if (!index) {
  1084. continue;
  1085. }
  1086. mappings.push({
  1087. charCode: j,
  1088. glyphId: index
  1089. });
  1090. }
  1091. hasShortCmap = true;
  1092. } else if (format === 4) {
  1093. var segCount = file.getUint16() >> 1;
  1094. file.skip(6);
  1095. var segIndex,
  1096. segments = [];
  1097. for (segIndex = 0; segIndex < segCount; segIndex++) {
  1098. segments.push({
  1099. end: file.getUint16()
  1100. });
  1101. }
  1102. file.skip(2);
  1103. for (segIndex = 0; segIndex < segCount; segIndex++) {
  1104. segments[segIndex].start = file.getUint16();
  1105. }
  1106. for (segIndex = 0; segIndex < segCount; segIndex++) {
  1107. segments[segIndex].delta = file.getUint16();
  1108. }
  1109. var offsetsCount = 0;
  1110. for (segIndex = 0; segIndex < segCount; segIndex++) {
  1111. segment = segments[segIndex];
  1112. var rangeOffset = file.getUint16();
  1113. if (!rangeOffset) {
  1114. segment.offsetIndex = -1;
  1115. continue;
  1116. }
  1117. var offsetIndex = (rangeOffset >> 1) - (segCount - segIndex);
  1118. segment.offsetIndex = offsetIndex;
  1119. offsetsCount = Math.max(offsetsCount, offsetIndex + segment.end - segment.start + 1);
  1120. }
  1121. var offsets = [];
  1122. for (j = 0; j < offsetsCount; j++) {
  1123. offsets.push(file.getUint16());
  1124. }
  1125. for (segIndex = 0; segIndex < segCount; segIndex++) {
  1126. segment = segments[segIndex];
  1127. start = segment.start;
  1128. var end = segment.end;
  1129. var delta = segment.delta;
  1130. offsetIndex = segment.offsetIndex;
  1131. for (j = start; j <= end; j++) {
  1132. if (j === 0xffff) {
  1133. continue;
  1134. }
  1135. glyphId = offsetIndex < 0 ? j : offsets[offsetIndex + j - start];
  1136. glyphId = glyphId + delta & 0xffff;
  1137. mappings.push({
  1138. charCode: j,
  1139. glyphId
  1140. });
  1141. }
  1142. }
  1143. } else if (format === 6) {
  1144. var firstCode = file.getUint16();
  1145. var entryCount = file.getUint16();
  1146. for (j = 0; j < entryCount; j++) {
  1147. glyphId = file.getUint16();
  1148. var charCode = firstCode + j;
  1149. mappings.push({
  1150. charCode,
  1151. glyphId
  1152. });
  1153. }
  1154. } else {
  1155. (0, _util.warn)("cmap table has unsupported format: " + format);
  1156. return {
  1157. platformId: -1,
  1158. encodingId: -1,
  1159. mappings: [],
  1160. hasShortCmap: false
  1161. };
  1162. }
  1163. mappings.sort(function (a, b) {
  1164. return a.charCode - b.charCode;
  1165. });
  1166. for (i = 1; i < mappings.length; i++) {
  1167. if (mappings[i - 1].charCode === mappings[i].charCode) {
  1168. mappings.splice(i, 1);
  1169. i--;
  1170. }
  1171. }
  1172. return {
  1173. platformId: potentialTable.platformId,
  1174. encodingId: potentialTable.encodingId,
  1175. mappings,
  1176. hasShortCmap
  1177. };
  1178. }
  1179. function sanitizeMetrics(file, header, metrics, numGlyphs, dupFirstEntry) {
  1180. if (!header) {
  1181. if (metrics) {
  1182. metrics.data = null;
  1183. }
  1184. return;
  1185. }
  1186. file.pos = (file.start ? file.start : 0) + header.offset;
  1187. file.pos += 4;
  1188. file.pos += 2;
  1189. file.pos += 2;
  1190. file.pos += 2;
  1191. file.pos += 2;
  1192. file.pos += 2;
  1193. file.pos += 2;
  1194. file.pos += 2;
  1195. file.pos += 2;
  1196. file.pos += 2;
  1197. file.pos += 2;
  1198. file.pos += 8;
  1199. file.pos += 2;
  1200. var numOfMetrics = file.getUint16();
  1201. if (numOfMetrics > numGlyphs) {
  1202. (0, _util.info)("The numOfMetrics (" + numOfMetrics + ") should not be " + "greater than the numGlyphs (" + numGlyphs + ")");
  1203. numOfMetrics = numGlyphs;
  1204. header.data[34] = (numOfMetrics & 0xff00) >> 8;
  1205. header.data[35] = numOfMetrics & 0x00ff;
  1206. }
  1207. var numOfSidebearings = numGlyphs - numOfMetrics;
  1208. var numMissing = numOfSidebearings - (metrics.length - numOfMetrics * 4 >> 1);
  1209. if (numMissing > 0) {
  1210. var entries = new Uint8Array(metrics.length + numMissing * 2);
  1211. entries.set(metrics.data);
  1212. if (dupFirstEntry) {
  1213. entries[metrics.length] = metrics.data[2];
  1214. entries[metrics.length + 1] = metrics.data[3];
  1215. }
  1216. metrics.data = entries;
  1217. }
  1218. }
  1219. function sanitizeGlyph(source, sourceStart, sourceEnd, dest, destStart, hintsValid) {
  1220. var glyphProfile = {
  1221. length: 0,
  1222. sizeOfInstructions: 0
  1223. };
  1224. if (sourceEnd - sourceStart <= 12) {
  1225. return glyphProfile;
  1226. }
  1227. var glyf = source.subarray(sourceStart, sourceEnd);
  1228. var contoursCount = signedInt16(glyf[0], glyf[1]);
  1229. if (contoursCount < 0) {
  1230. contoursCount = -1;
  1231. writeSignedInt16(glyf, 0, contoursCount);
  1232. dest.set(glyf, destStart);
  1233. glyphProfile.length = glyf.length;
  1234. return glyphProfile;
  1235. }
  1236. var i,
  1237. j = 10,
  1238. flagsCount = 0;
  1239. for (i = 0; i < contoursCount; i++) {
  1240. var endPoint = glyf[j] << 8 | glyf[j + 1];
  1241. flagsCount = endPoint + 1;
  1242. j += 2;
  1243. }
  1244. var instructionsStart = j;
  1245. var instructionsLength = glyf[j] << 8 | glyf[j + 1];
  1246. glyphProfile.sizeOfInstructions = instructionsLength;
  1247. j += 2 + instructionsLength;
  1248. var instructionsEnd = j;
  1249. var coordinatesLength = 0;
  1250. for (i = 0; i < flagsCount; i++) {
  1251. var flag = glyf[j++];
  1252. if (flag & 0xc0) {
  1253. glyf[j - 1] = flag & 0x3f;
  1254. }
  1255. let xLength = 2;
  1256. if (flag & 2) {
  1257. xLength = 1;
  1258. } else if (flag & 16) {
  1259. xLength = 0;
  1260. }
  1261. let yLength = 2;
  1262. if (flag & 4) {
  1263. yLength = 1;
  1264. } else if (flag & 32) {
  1265. yLength = 0;
  1266. }
  1267. const xyLength = xLength + yLength;
  1268. coordinatesLength += xyLength;
  1269. if (flag & 8) {
  1270. var repeat = glyf[j++];
  1271. i += repeat;
  1272. coordinatesLength += repeat * xyLength;
  1273. }
  1274. }
  1275. if (coordinatesLength === 0) {
  1276. return glyphProfile;
  1277. }
  1278. var glyphDataLength = j + coordinatesLength;
  1279. if (glyphDataLength > glyf.length) {
  1280. return glyphProfile;
  1281. }
  1282. if (!hintsValid && instructionsLength > 0) {
  1283. dest.set(glyf.subarray(0, instructionsStart), destStart);
  1284. dest.set([0, 0], destStart + instructionsStart);
  1285. dest.set(glyf.subarray(instructionsEnd, glyphDataLength), destStart + instructionsStart + 2);
  1286. glyphDataLength -= instructionsLength;
  1287. if (glyf.length - glyphDataLength > 3) {
  1288. glyphDataLength = glyphDataLength + 3 & ~3;
  1289. }
  1290. glyphProfile.length = glyphDataLength;
  1291. return glyphProfile;
  1292. }
  1293. if (glyf.length - glyphDataLength > 3) {
  1294. glyphDataLength = glyphDataLength + 3 & ~3;
  1295. dest.set(glyf.subarray(0, glyphDataLength), destStart);
  1296. glyphProfile.length = glyphDataLength;
  1297. return glyphProfile;
  1298. }
  1299. dest.set(glyf, destStart);
  1300. glyphProfile.length = glyf.length;
  1301. return glyphProfile;
  1302. }
  1303. function sanitizeHead(head, numGlyphs, locaLength) {
  1304. var data = head.data;
  1305. var version = int32(data[0], data[1], data[2], data[3]);
  1306. if (version >> 16 !== 1) {
  1307. (0, _util.info)("Attempting to fix invalid version in head table: " + version);
  1308. data[0] = 0;
  1309. data[1] = 1;
  1310. data[2] = 0;
  1311. data[3] = 0;
  1312. }
  1313. var indexToLocFormat = int16(data[50], data[51]);
  1314. if (indexToLocFormat < 0 || indexToLocFormat > 1) {
  1315. (0, _util.info)("Attempting to fix invalid indexToLocFormat in head table: " + indexToLocFormat);
  1316. var numGlyphsPlusOne = numGlyphs + 1;
  1317. if (locaLength === numGlyphsPlusOne << 1) {
  1318. data[50] = 0;
  1319. data[51] = 0;
  1320. } else if (locaLength === numGlyphsPlusOne << 2) {
  1321. data[50] = 0;
  1322. data[51] = 1;
  1323. } else {
  1324. throw new _util.FormatError("Could not fix indexToLocFormat: " + indexToLocFormat);
  1325. }
  1326. }
  1327. }
  1328. function sanitizeGlyphLocations(loca, glyf, numGlyphs, isGlyphLocationsLong, hintsValid, dupFirstEntry, maxSizeOfInstructions) {
  1329. var itemSize, itemDecode, itemEncode;
  1330. if (isGlyphLocationsLong) {
  1331. itemSize = 4;
  1332. itemDecode = function fontItemDecodeLong(data, offset) {
  1333. return data[offset] << 24 | data[offset + 1] << 16 | data[offset + 2] << 8 | data[offset + 3];
  1334. };
  1335. itemEncode = function fontItemEncodeLong(data, offset, value) {
  1336. data[offset] = value >>> 24 & 0xff;
  1337. data[offset + 1] = value >> 16 & 0xff;
  1338. data[offset + 2] = value >> 8 & 0xff;
  1339. data[offset + 3] = value & 0xff;
  1340. };
  1341. } else {
  1342. itemSize = 2;
  1343. itemDecode = function fontItemDecode(data, offset) {
  1344. return data[offset] << 9 | data[offset + 1] << 1;
  1345. };
  1346. itemEncode = function fontItemEncode(data, offset, value) {
  1347. data[offset] = value >> 9 & 0xff;
  1348. data[offset + 1] = value >> 1 & 0xff;
  1349. };
  1350. }
  1351. var numGlyphsOut = dupFirstEntry ? numGlyphs + 1 : numGlyphs;
  1352. var locaDataSize = itemSize * (1 + numGlyphsOut);
  1353. var locaData = new Uint8Array(locaDataSize);
  1354. locaData.set(loca.data.subarray(0, locaDataSize));
  1355. loca.data = locaData;
  1356. var oldGlyfData = glyf.data;
  1357. var oldGlyfDataLength = oldGlyfData.length;
  1358. var newGlyfData = new Uint8Array(oldGlyfDataLength);
  1359. var startOffset = itemDecode(locaData, 0);
  1360. var writeOffset = 0;
  1361. var missingGlyphs = Object.create(null);
  1362. itemEncode(locaData, 0, writeOffset);
  1363. var i, j;
  1364. for (i = 0, j = itemSize; i < numGlyphs; i++, j += itemSize) {
  1365. var endOffset = itemDecode(locaData, j);
  1366. if (endOffset === 0) {
  1367. endOffset = startOffset;
  1368. }
  1369. if (endOffset > oldGlyfDataLength && (oldGlyfDataLength + 3 & ~3) === endOffset) {
  1370. endOffset = oldGlyfDataLength;
  1371. }
  1372. if (endOffset > oldGlyfDataLength) {
  1373. startOffset = endOffset;
  1374. }
  1375. var glyphProfile = sanitizeGlyph(oldGlyfData, startOffset, endOffset, newGlyfData, writeOffset, hintsValid);
  1376. var newLength = glyphProfile.length;
  1377. if (newLength === 0) {
  1378. missingGlyphs[i] = true;
  1379. }
  1380. if (glyphProfile.sizeOfInstructions > maxSizeOfInstructions) {
  1381. maxSizeOfInstructions = glyphProfile.sizeOfInstructions;
  1382. }
  1383. writeOffset += newLength;
  1384. itemEncode(locaData, j, writeOffset);
  1385. startOffset = endOffset;
  1386. }
  1387. if (writeOffset === 0) {
  1388. var simpleGlyph = new Uint8Array([0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 49, 0]);
  1389. for (i = 0, j = itemSize; i < numGlyphsOut; i++, j += itemSize) {
  1390. itemEncode(locaData, j, simpleGlyph.length);
  1391. }
  1392. glyf.data = simpleGlyph;
  1393. } else if (dupFirstEntry) {
  1394. var firstEntryLength = itemDecode(locaData, itemSize);
  1395. if (newGlyfData.length > firstEntryLength + writeOffset) {
  1396. glyf.data = newGlyfData.subarray(0, firstEntryLength + writeOffset);
  1397. } else {
  1398. glyf.data = new Uint8Array(firstEntryLength + writeOffset);
  1399. glyf.data.set(newGlyfData.subarray(0, writeOffset));
  1400. }
  1401. glyf.data.set(newGlyfData.subarray(0, firstEntryLength), writeOffset);
  1402. itemEncode(loca.data, locaData.length - itemSize, writeOffset + firstEntryLength);
  1403. } else {
  1404. glyf.data = newGlyfData.subarray(0, writeOffset);
  1405. }
  1406. return {
  1407. missingGlyphs,
  1408. maxSizeOfInstructions
  1409. };
  1410. }
  1411. function readPostScriptTable(post, propertiesObj, maxpNumGlyphs) {
  1412. var start = (font.start ? font.start : 0) + post.offset;
  1413. font.pos = start;
  1414. var length = post.length,
  1415. end = start + length;
  1416. var version = font.getInt32();
  1417. font.skip(28);
  1418. var glyphNames;
  1419. var valid = true;
  1420. var i;
  1421. switch (version) {
  1422. case 0x00010000:
  1423. glyphNames = MacStandardGlyphOrdering;
  1424. break;
  1425. case 0x00020000:
  1426. var numGlyphs = font.getUint16();
  1427. if (numGlyphs !== maxpNumGlyphs) {
  1428. valid = false;
  1429. break;
  1430. }
  1431. var glyphNameIndexes = [];
  1432. for (i = 0; i < numGlyphs; ++i) {
  1433. var index = font.getUint16();
  1434. if (index >= 32768) {
  1435. valid = false;
  1436. break;
  1437. }
  1438. glyphNameIndexes.push(index);
  1439. }
  1440. if (!valid) {
  1441. break;
  1442. }
  1443. var customNames = [];
  1444. var strBuf = [];
  1445. while (font.pos < end) {
  1446. var stringLength = font.getByte();
  1447. strBuf.length = stringLength;
  1448. for (i = 0; i < stringLength; ++i) {
  1449. strBuf[i] = String.fromCharCode(font.getByte());
  1450. }
  1451. customNames.push(strBuf.join(""));
  1452. }
  1453. glyphNames = [];
  1454. for (i = 0; i < numGlyphs; ++i) {
  1455. var j = glyphNameIndexes[i];
  1456. if (j < 258) {
  1457. glyphNames.push(MacStandardGlyphOrdering[j]);
  1458. continue;
  1459. }
  1460. glyphNames.push(customNames[j - 258]);
  1461. }
  1462. break;
  1463. case 0x00030000:
  1464. break;
  1465. default:
  1466. (0, _util.warn)("Unknown/unsupported post table version " + version);
  1467. valid = false;
  1468. if (propertiesObj.defaultEncoding) {
  1469. glyphNames = propertiesObj.defaultEncoding;
  1470. }
  1471. break;
  1472. }
  1473. propertiesObj.glyphNames = glyphNames;
  1474. return valid;
  1475. }
  1476. function readNameTable(nameTable) {
  1477. var start = (font.start ? font.start : 0) + nameTable.offset;
  1478. font.pos = start;
  1479. var names = [[], []];
  1480. var length = nameTable.length,
  1481. end = start + length;
  1482. var format = font.getUint16();
  1483. var FORMAT_0_HEADER_LENGTH = 6;
  1484. if (format !== 0 || length < FORMAT_0_HEADER_LENGTH) {
  1485. return names;
  1486. }
  1487. var numRecords = font.getUint16();
  1488. var stringsStart = font.getUint16();
  1489. var records = [];
  1490. var NAME_RECORD_LENGTH = 12;
  1491. var i, ii;
  1492. for (i = 0; i < numRecords && font.pos + NAME_RECORD_LENGTH <= end; i++) {
  1493. var r = {
  1494. platform: font.getUint16(),
  1495. encoding: font.getUint16(),
  1496. language: font.getUint16(),
  1497. name: font.getUint16(),
  1498. length: font.getUint16(),
  1499. offset: font.getUint16()
  1500. };
  1501. if (r.platform === 1 && r.encoding === 0 && r.language === 0 || r.platform === 3 && r.encoding === 1 && r.language === 0x409) {
  1502. records.push(r);
  1503. }
  1504. }
  1505. for (i = 0, ii = records.length; i < ii; i++) {
  1506. var record = records[i];
  1507. if (record.length <= 0) {
  1508. continue;
  1509. }
  1510. var pos = start + stringsStart + record.offset;
  1511. if (pos + record.length > end) {
  1512. continue;
  1513. }
  1514. font.pos = pos;
  1515. var nameIndex = record.name;
  1516. if (record.encoding) {
  1517. var str = "";
  1518. for (var j = 0, jj = record.length; j < jj; j += 2) {
  1519. str += String.fromCharCode(font.getUint16());
  1520. }
  1521. names[1][nameIndex] = str;
  1522. } else {
  1523. names[0][nameIndex] = (0, _util.bytesToString)(font.getBytes(record.length));
  1524. }
  1525. }
  1526. return names;
  1527. }
  1528. var TTOpsStackDeltas = [0, 0, 0, 0, 0, 0, 0, 0, -2, -2, -2, -2, 0, 0, -2, -5, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, -1, 0, -1, -1, -1, -1, 1, -1, -999, 0, 1, 0, -1, -2, 0, -1, -2, -1, -1, 0, -1, -1, 0, 0, -999, -999, -1, -1, -1, -1, -2, -999, -2, -2, -999, 0, -2, -2, 0, 0, -2, 0, -2, 0, 0, 0, -2, -1, -1, 1, 1, 0, 0, -1, -1, -1, -1, -1, -1, -1, 0, 0, -1, 0, -1, -1, 0, -999, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, -999, -999, -999, -999, -999, -1, -1, -2, -2, 0, 0, 0, 0, -1, -1, -999, -2, -2, 0, 0, -1, -2, -2, 0, 0, 0, -1, -1, -1, -2];
  1529. function sanitizeTTProgram(table, ttContext) {
  1530. var data = table.data;
  1531. var i = 0,
  1532. j,
  1533. n,
  1534. b,
  1535. funcId,
  1536. pc,
  1537. lastEndf = 0,
  1538. lastDeff = 0;
  1539. var stack = [];
  1540. var callstack = [];
  1541. var functionsCalled = [];
  1542. var tooComplexToFollowFunctions = ttContext.tooComplexToFollowFunctions;
  1543. var inFDEF = false,
  1544. ifLevel = 0,
  1545. inELSE = 0;
  1546. for (var ii = data.length; i < ii;) {
  1547. var op = data[i++];
  1548. if (op === 0x40) {
  1549. n = data[i++];
  1550. if (inFDEF || inELSE) {
  1551. i += n;
  1552. } else {
  1553. for (j = 0; j < n; j++) {
  1554. stack.push(data[i++]);
  1555. }
  1556. }
  1557. } else if (op === 0x41) {
  1558. n = data[i++];
  1559. if (inFDEF || inELSE) {
  1560. i += n * 2;
  1561. } else {
  1562. for (j = 0; j < n; j++) {
  1563. b = data[i++];
  1564. stack.push(b << 8 | data[i++]);
  1565. }
  1566. }
  1567. } else if ((op & 0xf8) === 0xb0) {
  1568. n = op - 0xb0 + 1;
  1569. if (inFDEF || inELSE) {
  1570. i += n;
  1571. } else {
  1572. for (j = 0; j < n; j++) {
  1573. stack.push(data[i++]);
  1574. }
  1575. }
  1576. } else if ((op & 0xf8) === 0xb8) {
  1577. n = op - 0xb8 + 1;
  1578. if (inFDEF || inELSE) {
  1579. i += n * 2;
  1580. } else {
  1581. for (j = 0; j < n; j++) {
  1582. b = data[i++];
  1583. stack.push(b << 8 | data[i++]);
  1584. }
  1585. }
  1586. } else if (op === 0x2b && !tooComplexToFollowFunctions) {
  1587. if (!inFDEF && !inELSE) {
  1588. funcId = stack[stack.length - 1];
  1589. if (isNaN(funcId)) {
  1590. (0, _util.info)("TT: CALL empty stack (or invalid entry).");
  1591. } else {
  1592. ttContext.functionsUsed[funcId] = true;
  1593. if (funcId in ttContext.functionsStackDeltas) {
  1594. const newStackLength = stack.length + ttContext.functionsStackDeltas[funcId];
  1595. if (newStackLength < 0) {
  1596. (0, _util.warn)("TT: CALL invalid functions stack delta.");
  1597. ttContext.hintsValid = false;
  1598. return;
  1599. }
  1600. stack.length = newStackLength;
  1601. } else if (funcId in ttContext.functionsDefined && !functionsCalled.includes(funcId)) {
  1602. callstack.push({
  1603. data,
  1604. i,
  1605. stackTop: stack.length - 1
  1606. });
  1607. functionsCalled.push(funcId);
  1608. pc = ttContext.functionsDefined[funcId];
  1609. if (!pc) {
  1610. (0, _util.warn)("TT: CALL non-existent function");
  1611. ttContext.hintsValid = false;
  1612. return;
  1613. }
  1614. data = pc.data;
  1615. i = pc.i;
  1616. }
  1617. }
  1618. }
  1619. } else if (op === 0x2c && !tooComplexToFollowFunctions) {
  1620. if (inFDEF || inELSE) {
  1621. (0, _util.warn)("TT: nested FDEFs not allowed");
  1622. tooComplexToFollowFunctions = true;
  1623. }
  1624. inFDEF = true;
  1625. lastDeff = i;
  1626. funcId = stack.pop();
  1627. ttContext.functionsDefined[funcId] = {
  1628. data,
  1629. i
  1630. };
  1631. } else if (op === 0x2d) {
  1632. if (inFDEF) {
  1633. inFDEF = false;
  1634. lastEndf = i;
  1635. } else {
  1636. pc = callstack.pop();
  1637. if (!pc) {
  1638. (0, _util.warn)("TT: ENDF bad stack");
  1639. ttContext.hintsValid = false;
  1640. return;
  1641. }
  1642. funcId = functionsCalled.pop();
  1643. data = pc.data;
  1644. i = pc.i;
  1645. ttContext.functionsStackDeltas[funcId] = stack.length - pc.stackTop;
  1646. }
  1647. } else if (op === 0x89) {
  1648. if (inFDEF || inELSE) {
  1649. (0, _util.warn)("TT: nested IDEFs not allowed");
  1650. tooComplexToFollowFunctions = true;
  1651. }
  1652. inFDEF = true;
  1653. lastDeff = i;
  1654. } else if (op === 0x58) {
  1655. ++ifLevel;
  1656. } else if (op === 0x1b) {
  1657. inELSE = ifLevel;
  1658. } else if (op === 0x59) {
  1659. if (inELSE === ifLevel) {
  1660. inELSE = 0;
  1661. }
  1662. --ifLevel;
  1663. } else if (op === 0x1c) {
  1664. if (!inFDEF && !inELSE) {
  1665. var offset = stack[stack.length - 1];
  1666. if (offset > 0) {
  1667. i += offset - 1;
  1668. }
  1669. }
  1670. }
  1671. if (!inFDEF && !inELSE) {
  1672. let stackDelta = 0;
  1673. if (op <= 0x8e) {
  1674. stackDelta = TTOpsStackDeltas[op];
  1675. } else if (op >= 0xc0 && op <= 0xdf) {
  1676. stackDelta = -1;
  1677. } else if (op >= 0xe0) {
  1678. stackDelta = -2;
  1679. }
  1680. if (op >= 0x71 && op <= 0x75) {
  1681. n = stack.pop();
  1682. if (!isNaN(n)) {
  1683. stackDelta = -n * 2;
  1684. }
  1685. }
  1686. while (stackDelta < 0 && stack.length > 0) {
  1687. stack.pop();
  1688. stackDelta++;
  1689. }
  1690. while (stackDelta > 0) {
  1691. stack.push(NaN);
  1692. stackDelta--;
  1693. }
  1694. }
  1695. }
  1696. ttContext.tooComplexToFollowFunctions = tooComplexToFollowFunctions;
  1697. var content = [data];
  1698. if (i > data.length) {
  1699. content.push(new Uint8Array(i - data.length));
  1700. }
  1701. if (lastDeff > lastEndf) {
  1702. (0, _util.warn)("TT: complementing a missing function tail");
  1703. content.push(new Uint8Array([0x22, 0x2d]));
  1704. }
  1705. foldTTTable(table, content);
  1706. }
  1707. function checkInvalidFunctions(ttContext, maxFunctionDefs) {
  1708. if (ttContext.tooComplexToFollowFunctions) {
  1709. return;
  1710. }
  1711. if (ttContext.functionsDefined.length > maxFunctionDefs) {
  1712. (0, _util.warn)("TT: more functions defined than expected");
  1713. ttContext.hintsValid = false;
  1714. return;
  1715. }
  1716. for (var j = 0, jj = ttContext.functionsUsed.length; j < jj; j++) {
  1717. if (j > maxFunctionDefs) {
  1718. (0, _util.warn)("TT: invalid function id: " + j);
  1719. ttContext.hintsValid = false;
  1720. return;
  1721. }
  1722. if (ttContext.functionsUsed[j] && !ttContext.functionsDefined[j]) {
  1723. (0, _util.warn)("TT: undefined function: " + j);
  1724. ttContext.hintsValid = false;
  1725. return;
  1726. }
  1727. }
  1728. }
  1729. function foldTTTable(table, content) {
  1730. if (content.length > 1) {
  1731. var newLength = 0;
  1732. var j, jj;
  1733. for (j = 0, jj = content.length; j < jj; j++) {
  1734. newLength += content[j].length;
  1735. }
  1736. newLength = newLength + 3 & ~3;
  1737. var result = new Uint8Array(newLength);
  1738. var pos = 0;
  1739. for (j = 0, jj = content.length; j < jj; j++) {
  1740. result.set(content[j], pos);
  1741. pos += content[j].length;
  1742. }
  1743. table.data = result;
  1744. table.length = newLength;
  1745. }
  1746. }
  1747. function sanitizeTTPrograms(fpgm, prep, cvt, maxFunctionDefs) {
  1748. var ttContext = {
  1749. functionsDefined: [],
  1750. functionsUsed: [],
  1751. functionsStackDeltas: [],
  1752. tooComplexToFollowFunctions: false,
  1753. hintsValid: true
  1754. };
  1755. if (fpgm) {
  1756. sanitizeTTProgram(fpgm, ttContext);
  1757. }
  1758. if (prep) {
  1759. sanitizeTTProgram(prep, ttContext);
  1760. }
  1761. if (fpgm) {
  1762. checkInvalidFunctions(ttContext, maxFunctionDefs);
  1763. }
  1764. if (cvt && cvt.length & 1) {
  1765. var cvtData = new Uint8Array(cvt.length + 1);
  1766. cvtData.set(cvt.data);
  1767. cvt.data = cvtData;
  1768. }
  1769. return ttContext.hintsValid;
  1770. }
  1771. font = new _stream.Stream(new Uint8Array(font.getBytes()));
  1772. let header, tables;
  1773. if (isTrueTypeCollectionFile(font)) {
  1774. const ttcData = readTrueTypeCollectionData(font, this.name);
  1775. header = ttcData.header;
  1776. tables = ttcData.tables;
  1777. } else {
  1778. header = readOpenTypeHeader(font);
  1779. tables = readTables(font, header.numTables);
  1780. }
  1781. let cff, cffFile;
  1782. var isTrueType = !tables["CFF "];
  1783. if (!isTrueType) {
  1784. const isComposite = properties.composite && ((properties.cidToGidMap || []).length > 0 || !(properties.cMap instanceof _cmap.IdentityCMap));
  1785. if (header.version === "OTTO" && !isComposite || !tables.head || !tables.hhea || !tables.maxp || !tables.post) {
  1786. cffFile = new _stream.Stream(tables["CFF "].data);
  1787. cff = new CFFFont(cffFile, properties);
  1788. adjustWidths(properties);
  1789. return this.convert(name, cff, properties);
  1790. }
  1791. delete tables.glyf;
  1792. delete tables.loca;
  1793. delete tables.fpgm;
  1794. delete tables.prep;
  1795. delete tables["cvt "];
  1796. this.isOpenType = true;
  1797. } else {
  1798. if (!tables.loca) {
  1799. throw new _util.FormatError('Required "loca" table is not found');
  1800. }
  1801. if (!tables.glyf) {
  1802. (0, _util.warn)('Required "glyf" table is not found -- trying to recover.');
  1803. tables.glyf = {
  1804. tag: "glyf",
  1805. data: new Uint8Array(0)
  1806. };
  1807. }
  1808. this.isOpenType = false;
  1809. }
  1810. if (!tables.maxp) {
  1811. throw new _util.FormatError('Required "maxp" table is not found');
  1812. }
  1813. font.pos = (font.start || 0) + tables.maxp.offset;
  1814. var version = font.getInt32();
  1815. const numGlyphs = font.getUint16();
  1816. let numGlyphsOut = numGlyphs + 1;
  1817. let dupFirstEntry = true;
  1818. if (numGlyphsOut > 0xffff) {
  1819. dupFirstEntry = false;
  1820. numGlyphsOut = numGlyphs;
  1821. (0, _util.warn)("Not enough space in glyfs to duplicate first glyph.");
  1822. }
  1823. var maxFunctionDefs = 0;
  1824. var maxSizeOfInstructions = 0;
  1825. if (version >= 0x00010000 && tables.maxp.length >= 22) {
  1826. font.pos += 8;
  1827. var maxZones = font.getUint16();
  1828. if (maxZones > 2) {
  1829. tables.maxp.data[14] = 0;
  1830. tables.maxp.data[15] = 2;
  1831. }
  1832. font.pos += 4;
  1833. maxFunctionDefs = font.getUint16();
  1834. font.pos += 4;
  1835. maxSizeOfInstructions = font.getUint16();
  1836. }
  1837. tables.maxp.data[4] = numGlyphsOut >> 8;
  1838. tables.maxp.data[5] = numGlyphsOut & 255;
  1839. var hintsValid = sanitizeTTPrograms(tables.fpgm, tables.prep, tables["cvt "], maxFunctionDefs);
  1840. if (!hintsValid) {
  1841. delete tables.fpgm;
  1842. delete tables.prep;
  1843. delete tables["cvt "];
  1844. }
  1845. sanitizeMetrics(font, tables.hhea, tables.hmtx, numGlyphsOut, dupFirstEntry);
  1846. if (!tables.head) {
  1847. throw new _util.FormatError('Required "head" table is not found');
  1848. }
  1849. sanitizeHead(tables.head, numGlyphs, isTrueType ? tables.loca.length : 0);
  1850. var missingGlyphs = Object.create(null);
  1851. if (isTrueType) {
  1852. var isGlyphLocationsLong = int16(tables.head.data[50], tables.head.data[51]);
  1853. var glyphsInfo = sanitizeGlyphLocations(tables.loca, tables.glyf, numGlyphs, isGlyphLocationsLong, hintsValid, dupFirstEntry, maxSizeOfInstructions);
  1854. missingGlyphs = glyphsInfo.missingGlyphs;
  1855. if (version >= 0x00010000 && tables.maxp.length >= 22) {
  1856. tables.maxp.data[26] = glyphsInfo.maxSizeOfInstructions >> 8;
  1857. tables.maxp.data[27] = glyphsInfo.maxSizeOfInstructions & 255;
  1858. }
  1859. }
  1860. if (!tables.hhea) {
  1861. throw new _util.FormatError('Required "hhea" table is not found');
  1862. }
  1863. if (tables.hhea.data[10] === 0 && tables.hhea.data[11] === 0) {
  1864. tables.hhea.data[10] = 0xff;
  1865. tables.hhea.data[11] = 0xff;
  1866. }
  1867. var metricsOverride = {
  1868. unitsPerEm: int16(tables.head.data[18], tables.head.data[19]),
  1869. yMax: int16(tables.head.data[42], tables.head.data[43]),
  1870. yMin: signedInt16(tables.head.data[38], tables.head.data[39]),
  1871. ascent: int16(tables.hhea.data[4], tables.hhea.data[5]),
  1872. descent: signedInt16(tables.hhea.data[6], tables.hhea.data[7])
  1873. };
  1874. this.ascent = metricsOverride.ascent / metricsOverride.unitsPerEm;
  1875. this.descent = metricsOverride.descent / metricsOverride.unitsPerEm;
  1876. if (tables.post) {
  1877. readPostScriptTable(tables.post, properties, numGlyphs);
  1878. }
  1879. tables.post = {
  1880. tag: "post",
  1881. data: createPostTable(properties)
  1882. };
  1883. const charCodeToGlyphId = [];
  1884. function hasGlyph(glyphId) {
  1885. return !missingGlyphs[glyphId];
  1886. }
  1887. if (properties.composite) {
  1888. var cidToGidMap = properties.cidToGidMap || [];
  1889. var isCidToGidMapEmpty = cidToGidMap.length === 0;
  1890. properties.cMap.forEach(function (charCode, cid) {
  1891. if (cid > 0xffff) {
  1892. throw new _util.FormatError("Max size of CID is 65,535");
  1893. }
  1894. var glyphId = -1;
  1895. if (isCidToGidMapEmpty) {
  1896. glyphId = cid;
  1897. } else if (cidToGidMap[cid] !== undefined) {
  1898. glyphId = cidToGidMap[cid];
  1899. }
  1900. if (glyphId >= 0 && glyphId < numGlyphs && hasGlyph(glyphId)) {
  1901. charCodeToGlyphId[charCode] = glyphId;
  1902. }
  1903. });
  1904. } else {
  1905. var cmapTable = readCmapTable(tables.cmap, font, this.isSymbolicFont, properties.hasEncoding);
  1906. var cmapPlatformId = cmapTable.platformId;
  1907. var cmapEncodingId = cmapTable.encodingId;
  1908. var cmapMappings = cmapTable.mappings;
  1909. var cmapMappingsLength = cmapMappings.length;
  1910. if (properties.hasEncoding && (cmapPlatformId === 3 && cmapEncodingId === 1 || cmapPlatformId === 1 && cmapEncodingId === 0) || cmapPlatformId === -1 && cmapEncodingId === -1 && !!(0, _encodings.getEncoding)(properties.baseEncodingName)) {
  1911. var baseEncoding = [];
  1912. if (properties.baseEncodingName === "MacRomanEncoding" || properties.baseEncodingName === "WinAnsiEncoding") {
  1913. baseEncoding = (0, _encodings.getEncoding)(properties.baseEncodingName);
  1914. }
  1915. var glyphsUnicodeMap = (0, _glyphlist.getGlyphsUnicode)();
  1916. for (let charCode = 0; charCode < 256; charCode++) {
  1917. var glyphName, standardGlyphName;
  1918. if (this.differences && charCode in this.differences) {
  1919. glyphName = this.differences[charCode];
  1920. } else if (charCode in baseEncoding && baseEncoding[charCode] !== "") {
  1921. glyphName = baseEncoding[charCode];
  1922. } else {
  1923. glyphName = _encodings.StandardEncoding[charCode];
  1924. }
  1925. if (!glyphName) {
  1926. continue;
  1927. }
  1928. standardGlyphName = recoverGlyphName(glyphName, glyphsUnicodeMap);
  1929. var unicodeOrCharCode;
  1930. if (cmapPlatformId === 3 && cmapEncodingId === 1) {
  1931. unicodeOrCharCode = glyphsUnicodeMap[standardGlyphName];
  1932. } else if (cmapPlatformId === 1 && cmapEncodingId === 0) {
  1933. unicodeOrCharCode = _encodings.MacRomanEncoding.indexOf(standardGlyphName);
  1934. }
  1935. var found = false;
  1936. for (let i = 0; i < cmapMappingsLength; ++i) {
  1937. if (cmapMappings[i].charCode !== unicodeOrCharCode) {
  1938. continue;
  1939. }
  1940. charCodeToGlyphId[charCode] = cmapMappings[i].glyphId;
  1941. found = true;
  1942. break;
  1943. }
  1944. if (!found && properties.glyphNames) {
  1945. var glyphId = properties.glyphNames.indexOf(glyphName);
  1946. if (glyphId === -1 && standardGlyphName !== glyphName) {
  1947. glyphId = properties.glyphNames.indexOf(standardGlyphName);
  1948. }
  1949. if (glyphId > 0 && hasGlyph(glyphId)) {
  1950. charCodeToGlyphId[charCode] = glyphId;
  1951. }
  1952. }
  1953. }
  1954. } else if (cmapPlatformId === 0 && cmapEncodingId === 0) {
  1955. for (let i = 0; i < cmapMappingsLength; ++i) {
  1956. charCodeToGlyphId[cmapMappings[i].charCode] = cmapMappings[i].glyphId;
  1957. }
  1958. } else {
  1959. for (let i = 0; i < cmapMappingsLength; ++i) {
  1960. let charCode = cmapMappings[i].charCode;
  1961. if (cmapPlatformId === 3 && charCode >= 0xf000 && charCode <= 0xf0ff) {
  1962. charCode &= 0xff;
  1963. }
  1964. charCodeToGlyphId[charCode] = cmapMappings[i].glyphId;
  1965. }
  1966. }
  1967. }
  1968. if (charCodeToGlyphId.length === 0) {
  1969. charCodeToGlyphId[0] = 0;
  1970. }
  1971. let glyphZeroId = numGlyphsOut - 1;
  1972. if (!dupFirstEntry) {
  1973. glyphZeroId = 0;
  1974. }
  1975. var newMapping = adjustMapping(charCodeToGlyphId, hasGlyph, glyphZeroId);
  1976. this.toFontChar = newMapping.toFontChar;
  1977. tables.cmap = {
  1978. tag: "cmap",
  1979. data: createCmapTable(newMapping.charCodeToGlyphId, numGlyphsOut)
  1980. };
  1981. if (!tables["OS/2"] || !validateOS2Table(tables["OS/2"], font)) {
  1982. tables["OS/2"] = {
  1983. tag: "OS/2",
  1984. data: createOS2Table(properties, newMapping.charCodeToGlyphId, metricsOverride)
  1985. };
  1986. }
  1987. if (!isTrueType) {
  1988. try {
  1989. cffFile = new _stream.Stream(tables["CFF "].data);
  1990. var parser = new _cff_parser.CFFParser(cffFile, properties, SEAC_ANALYSIS_ENABLED);
  1991. cff = parser.parse();
  1992. cff.duplicateFirstGlyph();
  1993. var compiler = new _cff_parser.CFFCompiler(cff);
  1994. tables["CFF "].data = compiler.compile();
  1995. } catch (e) {
  1996. (0, _util.warn)("Failed to compile font " + properties.loadedName);
  1997. }
  1998. }
  1999. if (!tables.name) {
  2000. tables.name = {
  2001. tag: "name",
  2002. data: createNameTable(this.name)
  2003. };
  2004. } else {
  2005. var namePrototype = readNameTable(tables.name);
  2006. tables.name.data = createNameTable(name, namePrototype);
  2007. }
  2008. var builder = new OpenTypeFileBuilder(header.version);
  2009. for (var tableTag in tables) {
  2010. builder.addTable(tableTag, tables[tableTag].data);
  2011. }
  2012. return builder.toArray();
  2013. },
  2014. convert: function Font_convert(fontName, font, properties) {
  2015. properties.fixedPitch = false;
  2016. if (properties.builtInEncoding) {
  2017. adjustToUnicode(properties, properties.builtInEncoding);
  2018. }
  2019. let glyphZeroId = 1;
  2020. if (font instanceof CFFFont) {
  2021. glyphZeroId = font.numGlyphs - 1;
  2022. }
  2023. var mapping = font.getGlyphMapping(properties);
  2024. var newMapping = adjustMapping(mapping, font.hasGlyphId.bind(font), glyphZeroId);
  2025. this.toFontChar = newMapping.toFontChar;
  2026. var numGlyphs = font.numGlyphs;
  2027. function getCharCodes(charCodeToGlyphId, glyphId) {
  2028. var charCodes = null;
  2029. for (var charCode in charCodeToGlyphId) {
  2030. if (glyphId === charCodeToGlyphId[charCode]) {
  2031. if (!charCodes) {
  2032. charCodes = [];
  2033. }
  2034. charCodes.push(charCode | 0);
  2035. }
  2036. }
  2037. return charCodes;
  2038. }
  2039. function createCharCode(charCodeToGlyphId, glyphId) {
  2040. for (var charCode in charCodeToGlyphId) {
  2041. if (glyphId === charCodeToGlyphId[charCode]) {
  2042. return charCode | 0;
  2043. }
  2044. }
  2045. newMapping.charCodeToGlyphId[newMapping.nextAvailableFontCharCode] = glyphId;
  2046. return newMapping.nextAvailableFontCharCode++;
  2047. }
  2048. var seacs = font.seacs;
  2049. if (SEAC_ANALYSIS_ENABLED && seacs && seacs.length) {
  2050. var matrix = properties.fontMatrix || _util.FONT_IDENTITY_MATRIX;
  2051. var charset = font.getCharset();
  2052. var seacMap = Object.create(null);
  2053. for (var glyphId in seacs) {
  2054. glyphId |= 0;
  2055. var seac = seacs[glyphId];
  2056. var baseGlyphName = _encodings.StandardEncoding[seac[2]];
  2057. var accentGlyphName = _encodings.StandardEncoding[seac[3]];
  2058. var baseGlyphId = charset.indexOf(baseGlyphName);
  2059. var accentGlyphId = charset.indexOf(accentGlyphName);
  2060. if (baseGlyphId < 0 || accentGlyphId < 0) {
  2061. continue;
  2062. }
  2063. var accentOffset = {
  2064. x: seac[0] * matrix[0] + seac[1] * matrix[2] + matrix[4],
  2065. y: seac[0] * matrix[1] + seac[1] * matrix[3] + matrix[5]
  2066. };
  2067. var charCodes = getCharCodes(mapping, glyphId);
  2068. if (!charCodes) {
  2069. continue;
  2070. }
  2071. for (let i = 0, ii = charCodes.length; i < ii; i++) {
  2072. var charCode = charCodes[i];
  2073. var charCodeToGlyphId = newMapping.charCodeToGlyphId;
  2074. var baseFontCharCode = createCharCode(charCodeToGlyphId, baseGlyphId);
  2075. var accentFontCharCode = createCharCode(charCodeToGlyphId, accentGlyphId);
  2076. seacMap[charCode] = {
  2077. baseFontCharCode,
  2078. accentFontCharCode,
  2079. accentOffset
  2080. };
  2081. }
  2082. }
  2083. properties.seacMap = seacMap;
  2084. }
  2085. var unitsPerEm = 1 / (properties.fontMatrix || _util.FONT_IDENTITY_MATRIX)[0];
  2086. var builder = new OpenTypeFileBuilder("\x4F\x54\x54\x4F");
  2087. builder.addTable("CFF ", font.data);
  2088. builder.addTable("OS/2", createOS2Table(properties, newMapping.charCodeToGlyphId));
  2089. builder.addTable("cmap", createCmapTable(newMapping.charCodeToGlyphId, numGlyphs));
  2090. builder.addTable("head", "\x00\x01\x00\x00" + "\x00\x00\x10\x00" + "\x00\x00\x00\x00" + "\x5F\x0F\x3C\xF5" + "\x00\x00" + safeString16(unitsPerEm) + "\x00\x00\x00\x00\x9e\x0b\x7e\x27" + "\x00\x00\x00\x00\x9e\x0b\x7e\x27" + "\x00\x00" + safeString16(properties.descent) + "\x0F\xFF" + safeString16(properties.ascent) + string16(properties.italicAngle ? 2 : 0) + "\x00\x11" + "\x00\x00" + "\x00\x00" + "\x00\x00");
  2091. builder.addTable("hhea", "\x00\x01\x00\x00" + safeString16(properties.ascent) + safeString16(properties.descent) + "\x00\x00" + "\xFF\xFF" + "\x00\x00" + "\x00\x00" + "\x00\x00" + safeString16(properties.capHeight) + safeString16(Math.tan(properties.italicAngle) * properties.xHeight) + "\x00\x00" + "\x00\x00" + "\x00\x00" + "\x00\x00" + "\x00\x00" + "\x00\x00" + string16(numGlyphs));
  2092. builder.addTable("hmtx", function fontFieldsHmtx() {
  2093. var charstrings = font.charstrings;
  2094. var cffWidths = font.cff ? font.cff.widths : null;
  2095. var hmtx = "\x00\x00\x00\x00";
  2096. for (let i = 1, ii = numGlyphs; i < ii; i++) {
  2097. var width = 0;
  2098. if (charstrings) {
  2099. var charstring = charstrings[i - 1];
  2100. width = "width" in charstring ? charstring.width : 0;
  2101. } else if (cffWidths) {
  2102. width = Math.ceil(cffWidths[i] || 0);
  2103. }
  2104. hmtx += string16(width) + string16(0);
  2105. }
  2106. return hmtx;
  2107. }());
  2108. builder.addTable("maxp", "\x00\x00\x50\x00" + string16(numGlyphs));
  2109. builder.addTable("name", createNameTable(fontName));
  2110. builder.addTable("post", createPostTable(properties));
  2111. return builder.toArray();
  2112. },
  2113. get spaceWidth() {
  2114. var possibleSpaceReplacements = ["space", "minus", "one", "i", "I"];
  2115. var width;
  2116. for (var i = 0, ii = possibleSpaceReplacements.length; i < ii; i++) {
  2117. var glyphName = possibleSpaceReplacements[i];
  2118. if (glyphName in this.widths) {
  2119. width = this.widths[glyphName];
  2120. break;
  2121. }
  2122. var glyphsUnicodeMap = (0, _glyphlist.getGlyphsUnicode)();
  2123. var glyphUnicode = glyphsUnicodeMap[glyphName];
  2124. var charcode = 0;
  2125. if (this.composite && this.cMap.contains(glyphUnicode)) {
  2126. charcode = this.cMap.lookup(glyphUnicode);
  2127. }
  2128. if (!charcode && this.toUnicode) {
  2129. charcode = this.toUnicode.charCodeOf(glyphUnicode);
  2130. }
  2131. if (charcode <= 0) {
  2132. charcode = glyphUnicode;
  2133. }
  2134. width = this.widths[charcode];
  2135. if (width) {
  2136. break;
  2137. }
  2138. }
  2139. width = width || this.defaultWidth;
  2140. return (0, _util.shadow)(this, "spaceWidth", width);
  2141. },
  2142. charToGlyph: function Font_charToGlyph(charcode, isSpace) {
  2143. var fontCharCode, width, operatorListId;
  2144. var widthCode = charcode;
  2145. if (this.cMap && this.cMap.contains(charcode)) {
  2146. widthCode = this.cMap.lookup(charcode);
  2147. }
  2148. width = this.widths[widthCode];
  2149. width = (0, _util.isNum)(width) ? width : this.defaultWidth;
  2150. var vmetric = this.vmetrics && this.vmetrics[widthCode];
  2151. let unicode = this.toUnicode.get(charcode) || this.fallbackToUnicode.get(charcode) || charcode;
  2152. if (typeof unicode === "number") {
  2153. unicode = String.fromCharCode(unicode);
  2154. }
  2155. var isInFont = (charcode in this.toFontChar);
  2156. fontCharCode = this.toFontChar[charcode] || charcode;
  2157. if (this.missingFile) {
  2158. const glyphName = this.differences[charcode] || this.defaultEncoding[charcode];
  2159. if ((glyphName === ".notdef" || glyphName === "") && this.type === "Type1") {
  2160. fontCharCode = 0x20;
  2161. }
  2162. fontCharCode = (0, _unicode.mapSpecialUnicodeValues)(fontCharCode);
  2163. }
  2164. if (this.isType3Font) {
  2165. operatorListId = fontCharCode;
  2166. }
  2167. var accent = null;
  2168. if (this.seacMap && this.seacMap[charcode]) {
  2169. isInFont = true;
  2170. var seac = this.seacMap[charcode];
  2171. fontCharCode = seac.baseFontCharCode;
  2172. accent = {
  2173. fontChar: String.fromCodePoint(seac.accentFontCharCode),
  2174. offset: seac.accentOffset
  2175. };
  2176. }
  2177. let fontChar = "";
  2178. if (typeof fontCharCode === "number") {
  2179. if (fontCharCode <= 0x10ffff) {
  2180. fontChar = String.fromCodePoint(fontCharCode);
  2181. } else {
  2182. (0, _util.warn)(`charToGlyph - invalid fontCharCode: ${fontCharCode}`);
  2183. }
  2184. }
  2185. var glyph = this.glyphCache[charcode];
  2186. if (!glyph || !glyph.matchesForCache(fontChar, unicode, accent, width, vmetric, operatorListId, isSpace, isInFont)) {
  2187. glyph = new Glyph(fontChar, unicode, accent, width, vmetric, operatorListId, isSpace, isInFont);
  2188. this.glyphCache[charcode] = glyph;
  2189. }
  2190. return glyph;
  2191. },
  2192. charsToGlyphs: function Font_charsToGlyphs(chars) {
  2193. var charsCache = this.charsCache;
  2194. var glyphs, glyph, charcode;
  2195. if (charsCache) {
  2196. glyphs = charsCache[chars];
  2197. if (glyphs) {
  2198. return glyphs;
  2199. }
  2200. }
  2201. if (!charsCache) {
  2202. charsCache = this.charsCache = Object.create(null);
  2203. }
  2204. glyphs = [];
  2205. var charsCacheKey = chars;
  2206. var i = 0,
  2207. ii;
  2208. if (this.cMap) {
  2209. var c = Object.create(null);
  2210. while (i < chars.length) {
  2211. this.cMap.readCharCode(chars, i, c);
  2212. charcode = c.charcode;
  2213. var length = c.length;
  2214. i += length;
  2215. var isSpace = length === 1 && chars.charCodeAt(i - 1) === 0x20;
  2216. glyph = this.charToGlyph(charcode, isSpace);
  2217. glyphs.push(glyph);
  2218. }
  2219. } else {
  2220. for (i = 0, ii = chars.length; i < ii; ++i) {
  2221. charcode = chars.charCodeAt(i);
  2222. glyph = this.charToGlyph(charcode, charcode === 0x20);
  2223. glyphs.push(glyph);
  2224. }
  2225. }
  2226. return charsCache[charsCacheKey] = glyphs;
  2227. },
  2228. get glyphCacheValues() {
  2229. return Object.values(this.glyphCache);
  2230. }
  2231. };
  2232. return Font;
  2233. }();
  2234. exports.Font = Font;
  2235. var ErrorFont = function ErrorFontClosure() {
  2236. function ErrorFont(error) {
  2237. this.error = error;
  2238. this.loadedName = "g_font_error";
  2239. this.missingFile = true;
  2240. }
  2241. ErrorFont.prototype = {
  2242. charsToGlyphs: function ErrorFont_charsToGlyphs() {
  2243. return [];
  2244. },
  2245. exportData(extraProperties = false) {
  2246. return {
  2247. error: this.error
  2248. };
  2249. }
  2250. };
  2251. return ErrorFont;
  2252. }();
  2253. exports.ErrorFont = ErrorFont;
  2254. function type1FontGlyphMapping(properties, builtInEncoding, glyphNames) {
  2255. var charCodeToGlyphId = Object.create(null);
  2256. var glyphId, charCode, baseEncoding;
  2257. var isSymbolicFont = !!(properties.flags & FontFlags.Symbolic);
  2258. if (properties.baseEncodingName) {
  2259. baseEncoding = (0, _encodings.getEncoding)(properties.baseEncodingName);
  2260. for (charCode = 0; charCode < baseEncoding.length; charCode++) {
  2261. glyphId = glyphNames.indexOf(baseEncoding[charCode]);
  2262. if (glyphId >= 0) {
  2263. charCodeToGlyphId[charCode] = glyphId;
  2264. } else {
  2265. charCodeToGlyphId[charCode] = 0;
  2266. }
  2267. }
  2268. } else if (isSymbolicFont) {
  2269. for (charCode in builtInEncoding) {
  2270. charCodeToGlyphId[charCode] = builtInEncoding[charCode];
  2271. }
  2272. } else {
  2273. baseEncoding = _encodings.StandardEncoding;
  2274. for (charCode = 0; charCode < baseEncoding.length; charCode++) {
  2275. glyphId = glyphNames.indexOf(baseEncoding[charCode]);
  2276. if (glyphId >= 0) {
  2277. charCodeToGlyphId[charCode] = glyphId;
  2278. } else {
  2279. charCodeToGlyphId[charCode] = 0;
  2280. }
  2281. }
  2282. }
  2283. var differences = properties.differences,
  2284. glyphsUnicodeMap;
  2285. if (differences) {
  2286. for (charCode in differences) {
  2287. var glyphName = differences[charCode];
  2288. glyphId = glyphNames.indexOf(glyphName);
  2289. if (glyphId === -1) {
  2290. if (!glyphsUnicodeMap) {
  2291. glyphsUnicodeMap = (0, _glyphlist.getGlyphsUnicode)();
  2292. }
  2293. var standardGlyphName = recoverGlyphName(glyphName, glyphsUnicodeMap);
  2294. if (standardGlyphName !== glyphName) {
  2295. glyphId = glyphNames.indexOf(standardGlyphName);
  2296. }
  2297. }
  2298. if (glyphId >= 0) {
  2299. charCodeToGlyphId[charCode] = glyphId;
  2300. } else {
  2301. charCodeToGlyphId[charCode] = 0;
  2302. }
  2303. }
  2304. }
  2305. return charCodeToGlyphId;
  2306. }
  2307. var Type1Font = function Type1FontClosure() {
  2308. function findBlock(streamBytes, signature, startIndex) {
  2309. var streamBytesLength = streamBytes.length;
  2310. var signatureLength = signature.length;
  2311. var scanLength = streamBytesLength - signatureLength;
  2312. var i = startIndex,
  2313. j,
  2314. found = false;
  2315. while (i < scanLength) {
  2316. j = 0;
  2317. while (j < signatureLength && streamBytes[i + j] === signature[j]) {
  2318. j++;
  2319. }
  2320. if (j >= signatureLength) {
  2321. i += j;
  2322. while (i < streamBytesLength && (0, _core_utils.isWhiteSpace)(streamBytes[i])) {
  2323. i++;
  2324. }
  2325. found = true;
  2326. break;
  2327. }
  2328. i++;
  2329. }
  2330. return {
  2331. found,
  2332. length: i
  2333. };
  2334. }
  2335. function getHeaderBlock(stream, suggestedLength) {
  2336. var EEXEC_SIGNATURE = [0x65, 0x65, 0x78, 0x65, 0x63];
  2337. var streamStartPos = stream.pos;
  2338. var headerBytes, headerBytesLength, block;
  2339. try {
  2340. headerBytes = stream.getBytes(suggestedLength);
  2341. headerBytesLength = headerBytes.length;
  2342. } catch (ex) {
  2343. if (ex instanceof _core_utils.MissingDataException) {
  2344. throw ex;
  2345. }
  2346. }
  2347. if (headerBytesLength === suggestedLength) {
  2348. block = findBlock(headerBytes, EEXEC_SIGNATURE, suggestedLength - 2 * EEXEC_SIGNATURE.length);
  2349. if (block.found && block.length === suggestedLength) {
  2350. return {
  2351. stream: new _stream.Stream(headerBytes),
  2352. length: suggestedLength
  2353. };
  2354. }
  2355. }
  2356. (0, _util.warn)('Invalid "Length1" property in Type1 font -- trying to recover.');
  2357. stream.pos = streamStartPos;
  2358. var SCAN_BLOCK_LENGTH = 2048;
  2359. var actualLength;
  2360. while (true) {
  2361. var scanBytes = stream.peekBytes(SCAN_BLOCK_LENGTH);
  2362. block = findBlock(scanBytes, EEXEC_SIGNATURE, 0);
  2363. if (block.length === 0) {
  2364. break;
  2365. }
  2366. stream.pos += block.length;
  2367. if (block.found) {
  2368. actualLength = stream.pos - streamStartPos;
  2369. break;
  2370. }
  2371. }
  2372. stream.pos = streamStartPos;
  2373. if (actualLength) {
  2374. return {
  2375. stream: new _stream.Stream(stream.getBytes(actualLength)),
  2376. length: actualLength
  2377. };
  2378. }
  2379. (0, _util.warn)('Unable to recover "Length1" property in Type1 font -- using as is.');
  2380. return {
  2381. stream: new _stream.Stream(stream.getBytes(suggestedLength)),
  2382. length: suggestedLength
  2383. };
  2384. }
  2385. function getEexecBlock(stream, suggestedLength) {
  2386. var eexecBytes = stream.getBytes();
  2387. return {
  2388. stream: new _stream.Stream(eexecBytes),
  2389. length: eexecBytes.length
  2390. };
  2391. }
  2392. function Type1Font(name, file, properties) {
  2393. var PFB_HEADER_SIZE = 6;
  2394. var headerBlockLength = properties.length1;
  2395. var eexecBlockLength = properties.length2;
  2396. var pfbHeader = file.peekBytes(PFB_HEADER_SIZE);
  2397. var pfbHeaderPresent = pfbHeader[0] === 0x80 && pfbHeader[1] === 0x01;
  2398. if (pfbHeaderPresent) {
  2399. file.skip(PFB_HEADER_SIZE);
  2400. headerBlockLength = pfbHeader[5] << 24 | pfbHeader[4] << 16 | pfbHeader[3] << 8 | pfbHeader[2];
  2401. }
  2402. var headerBlock = getHeaderBlock(file, headerBlockLength);
  2403. var headerBlockParser = new _type1_parser.Type1Parser(headerBlock.stream, false, SEAC_ANALYSIS_ENABLED);
  2404. headerBlockParser.extractFontHeader(properties);
  2405. if (pfbHeaderPresent) {
  2406. pfbHeader = file.getBytes(PFB_HEADER_SIZE);
  2407. eexecBlockLength = pfbHeader[5] << 24 | pfbHeader[4] << 16 | pfbHeader[3] << 8 | pfbHeader[2];
  2408. }
  2409. var eexecBlock = getEexecBlock(file, eexecBlockLength);
  2410. var eexecBlockParser = new _type1_parser.Type1Parser(eexecBlock.stream, true, SEAC_ANALYSIS_ENABLED);
  2411. var data = eexecBlockParser.extractFontProgram(properties);
  2412. for (const key in data.properties) {
  2413. properties[key] = data.properties[key];
  2414. }
  2415. var charstrings = data.charstrings;
  2416. var type2Charstrings = this.getType2Charstrings(charstrings);
  2417. var subrs = this.getType2Subrs(data.subrs);
  2418. this.charstrings = charstrings;
  2419. this.data = this.wrap(name, type2Charstrings, this.charstrings, subrs, properties);
  2420. this.seacs = this.getSeacs(data.charstrings);
  2421. }
  2422. Type1Font.prototype = {
  2423. get numGlyphs() {
  2424. return this.charstrings.length + 1;
  2425. },
  2426. getCharset: function Type1Font_getCharset() {
  2427. var charset = [".notdef"];
  2428. var charstrings = this.charstrings;
  2429. for (var glyphId = 0; glyphId < charstrings.length; glyphId++) {
  2430. charset.push(charstrings[glyphId].glyphName);
  2431. }
  2432. return charset;
  2433. },
  2434. getGlyphMapping: function Type1Font_getGlyphMapping(properties) {
  2435. var charstrings = this.charstrings;
  2436. if (properties.composite) {
  2437. const charCodeToGlyphId = Object.create(null);
  2438. for (let glyphId = 0, charstringsLen = charstrings.length; glyphId < charstringsLen; glyphId++) {
  2439. const charCode = properties.cMap.charCodeOf(glyphId);
  2440. charCodeToGlyphId[charCode] = glyphId + 1;
  2441. }
  2442. return charCodeToGlyphId;
  2443. }
  2444. var glyphNames = [".notdef"],
  2445. glyphId;
  2446. for (glyphId = 0; glyphId < charstrings.length; glyphId++) {
  2447. glyphNames.push(charstrings[glyphId].glyphName);
  2448. }
  2449. var encoding = properties.builtInEncoding;
  2450. if (encoding) {
  2451. var builtInEncoding = Object.create(null);
  2452. for (var charCode in encoding) {
  2453. glyphId = glyphNames.indexOf(encoding[charCode]);
  2454. if (glyphId >= 0) {
  2455. builtInEncoding[charCode] = glyphId;
  2456. }
  2457. }
  2458. }
  2459. return type1FontGlyphMapping(properties, builtInEncoding, glyphNames);
  2460. },
  2461. hasGlyphId: function Type1Font_hasGlyphID(id) {
  2462. if (id < 0 || id >= this.numGlyphs) {
  2463. return false;
  2464. }
  2465. if (id === 0) {
  2466. return true;
  2467. }
  2468. var glyph = this.charstrings[id - 1];
  2469. return glyph.charstring.length > 0;
  2470. },
  2471. getSeacs: function Type1Font_getSeacs(charstrings) {
  2472. var i, ii;
  2473. var seacMap = [];
  2474. for (i = 0, ii = charstrings.length; i < ii; i++) {
  2475. var charstring = charstrings[i];
  2476. if (charstring.seac) {
  2477. seacMap[i + 1] = charstring.seac;
  2478. }
  2479. }
  2480. return seacMap;
  2481. },
  2482. getType2Charstrings: function Type1Font_getType2Charstrings(type1Charstrings) {
  2483. var type2Charstrings = [];
  2484. for (var i = 0, ii = type1Charstrings.length; i < ii; i++) {
  2485. type2Charstrings.push(type1Charstrings[i].charstring);
  2486. }
  2487. return type2Charstrings;
  2488. },
  2489. getType2Subrs: function Type1Font_getType2Subrs(type1Subrs) {
  2490. var bias = 0;
  2491. var count = type1Subrs.length;
  2492. if (count < 1133) {
  2493. bias = 107;
  2494. } else if (count < 33769) {
  2495. bias = 1131;
  2496. } else {
  2497. bias = 32768;
  2498. }
  2499. var type2Subrs = [];
  2500. var i;
  2501. for (i = 0; i < bias; i++) {
  2502. type2Subrs.push([0x0b]);
  2503. }
  2504. for (i = 0; i < count; i++) {
  2505. type2Subrs.push(type1Subrs[i]);
  2506. }
  2507. return type2Subrs;
  2508. },
  2509. wrap: function Type1Font_wrap(name, glyphs, charstrings, subrs, properties) {
  2510. var cff = new _cff_parser.CFF();
  2511. cff.header = new _cff_parser.CFFHeader(1, 0, 4, 4);
  2512. cff.names = [name];
  2513. var topDict = new _cff_parser.CFFTopDict();
  2514. topDict.setByName("version", 391);
  2515. topDict.setByName("Notice", 392);
  2516. topDict.setByName("FullName", 393);
  2517. topDict.setByName("FamilyName", 394);
  2518. topDict.setByName("Weight", 395);
  2519. topDict.setByName("Encoding", null);
  2520. topDict.setByName("FontMatrix", properties.fontMatrix);
  2521. topDict.setByName("FontBBox", properties.bbox);
  2522. topDict.setByName("charset", null);
  2523. topDict.setByName("CharStrings", null);
  2524. topDict.setByName("Private", null);
  2525. cff.topDict = topDict;
  2526. var strings = new _cff_parser.CFFStrings();
  2527. strings.add("Version 0.11");
  2528. strings.add("See original notice");
  2529. strings.add(name);
  2530. strings.add(name);
  2531. strings.add("Medium");
  2532. cff.strings = strings;
  2533. cff.globalSubrIndex = new _cff_parser.CFFIndex();
  2534. var count = glyphs.length;
  2535. var charsetArray = [".notdef"];
  2536. var i, ii;
  2537. for (i = 0; i < count; i++) {
  2538. const glyphName = charstrings[i].glyphName;
  2539. const index = _cff_parser.CFFStandardStrings.indexOf(glyphName);
  2540. if (index === -1) {
  2541. strings.add(glyphName);
  2542. }
  2543. charsetArray.push(glyphName);
  2544. }
  2545. cff.charset = new _cff_parser.CFFCharset(false, 0, charsetArray);
  2546. var charStringsIndex = new _cff_parser.CFFIndex();
  2547. charStringsIndex.add([0x8b, 0x0e]);
  2548. for (i = 0; i < count; i++) {
  2549. charStringsIndex.add(glyphs[i]);
  2550. }
  2551. cff.charStrings = charStringsIndex;
  2552. var privateDict = new _cff_parser.CFFPrivateDict();
  2553. privateDict.setByName("Subrs", null);
  2554. var fields = ["BlueValues", "OtherBlues", "FamilyBlues", "FamilyOtherBlues", "StemSnapH", "StemSnapV", "BlueShift", "BlueFuzz", "BlueScale", "LanguageGroup", "ExpansionFactor", "ForceBold", "StdHW", "StdVW"];
  2555. for (i = 0, ii = fields.length; i < ii; i++) {
  2556. var field = fields[i];
  2557. if (!(field in properties.privateData)) {
  2558. continue;
  2559. }
  2560. var value = properties.privateData[field];
  2561. if (Array.isArray(value)) {
  2562. for (var j = value.length - 1; j > 0; j--) {
  2563. value[j] -= value[j - 1];
  2564. }
  2565. }
  2566. privateDict.setByName(field, value);
  2567. }
  2568. cff.topDict.privateDict = privateDict;
  2569. var subrIndex = new _cff_parser.CFFIndex();
  2570. for (i = 0, ii = subrs.length; i < ii; i++) {
  2571. subrIndex.add(subrs[i]);
  2572. }
  2573. privateDict.subrsIndex = subrIndex;
  2574. var compiler = new _cff_parser.CFFCompiler(cff);
  2575. return compiler.compile();
  2576. }
  2577. };
  2578. return Type1Font;
  2579. }();
  2580. var CFFFont = function CFFFontClosure() {
  2581. function CFFFont(file, properties) {
  2582. this.properties = properties;
  2583. var parser = new _cff_parser.CFFParser(file, properties, SEAC_ANALYSIS_ENABLED);
  2584. this.cff = parser.parse();
  2585. this.cff.duplicateFirstGlyph();
  2586. var compiler = new _cff_parser.CFFCompiler(this.cff);
  2587. this.seacs = this.cff.seacs;
  2588. try {
  2589. this.data = compiler.compile();
  2590. } catch (e) {
  2591. (0, _util.warn)("Failed to compile font " + properties.loadedName);
  2592. this.data = file;
  2593. }
  2594. }
  2595. CFFFont.prototype = {
  2596. get numGlyphs() {
  2597. return this.cff.charStrings.count;
  2598. },
  2599. getCharset: function CFFFont_getCharset() {
  2600. return this.cff.charset.charset;
  2601. },
  2602. getGlyphMapping: function CFFFont_getGlyphMapping() {
  2603. var cff = this.cff;
  2604. var properties = this.properties;
  2605. var charsets = cff.charset.charset;
  2606. var charCodeToGlyphId;
  2607. var glyphId;
  2608. if (properties.composite) {
  2609. charCodeToGlyphId = Object.create(null);
  2610. let charCode;
  2611. if (cff.isCIDFont) {
  2612. for (glyphId = 0; glyphId < charsets.length; glyphId++) {
  2613. var cid = charsets[glyphId];
  2614. charCode = properties.cMap.charCodeOf(cid);
  2615. charCodeToGlyphId[charCode] = glyphId;
  2616. }
  2617. } else {
  2618. for (glyphId = 0; glyphId < cff.charStrings.count; glyphId++) {
  2619. charCode = properties.cMap.charCodeOf(glyphId);
  2620. charCodeToGlyphId[charCode] = glyphId;
  2621. }
  2622. }
  2623. return charCodeToGlyphId;
  2624. }
  2625. var encoding = cff.encoding ? cff.encoding.encoding : null;
  2626. charCodeToGlyphId = type1FontGlyphMapping(properties, encoding, charsets);
  2627. return charCodeToGlyphId;
  2628. },
  2629. hasGlyphId: function CFFFont_hasGlyphID(id) {
  2630. return this.cff.hasGlyphId(id);
  2631. }
  2632. };
  2633. return CFFFont;
  2634. }();