2
0

fonts.js 99 KB

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