fonts.js 97 KB

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