fonts.js 84 KB

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