fonts.js 98 KB

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