opentype.js

Read and write OpenType fonts using JavaScript. http://opentype.js.org/

このスクリプトは単体で利用できません。右のようなメタデータを含むスクリプトから、ライブラリとして読み込まれます: // @require https://update.greasyfork.org/scripts/20469/130870/opentypejs.js

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください。
  1. (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.opentype = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  2. var TINF_OK = 0;
  3. var TINF_DATA_ERROR = -3;
  4.  
  5. function Tree() {
  6. this.table = new Uint16Array(16); /* table of code length counts */
  7. this.trans = new Uint16Array(288); /* code -> symbol translation table */
  8. }
  9.  
  10. function Data(source, dest) {
  11. this.source = source;
  12. this.sourceIndex = 0;
  13. this.tag = 0;
  14. this.bitcount = 0;
  15. this.dest = dest;
  16. this.destLen = 0;
  17. this.ltree = new Tree(); /* dynamic length/symbol tree */
  18. this.dtree = new Tree(); /* dynamic distance tree */
  19. }
  20.  
  21. /* --------------------------------------------------- *
  22. * -- uninitialized global data (static structures) -- *
  23. * --------------------------------------------------- */
  24.  
  25. var sltree = new Tree();
  26. var sdtree = new Tree();
  27.  
  28. /* extra bits and base tables for length codes */
  29. var length_bits = new Uint8Array(30);
  30. var length_base = new Uint16Array(30);
  31.  
  32. /* extra bits and base tables for distance codes */
  33. var dist_bits = new Uint8Array(30);
  34. var dist_base = new Uint16Array(30);
  35.  
  36. /* special ordering of code length codes */
  37. var clcidx = new Uint8Array([
  38. 16, 17, 18, 0, 8, 7, 9, 6,
  39. 10, 5, 11, 4, 12, 3, 13, 2,
  40. 14, 1, 15
  41. ]);
  42.  
  43. /* used by tinf_decode_trees, avoids allocations every call */
  44. var code_tree = new Tree();
  45. var lengths = new Uint8Array(288 + 32);
  46.  
  47. /* ----------------------- *
  48. * -- utility functions -- *
  49. * ----------------------- */
  50.  
  51. /* build extra bits and base tables */
  52. function tinf_build_bits_base(bits, base, delta, first) {
  53. var i, sum;
  54.  
  55. /* build bits table */
  56. for (i = 0; i < delta; ++i) bits[i] = 0;
  57. for (i = 0; i < 30 - delta; ++i) bits[i + delta] = i / delta | 0;
  58.  
  59. /* build base table */
  60. for (sum = first, i = 0; i < 30; ++i) {
  61. base[i] = sum;
  62. sum += 1 << bits[i];
  63. }
  64. }
  65.  
  66. /* build the fixed huffman trees */
  67. function tinf_build_fixed_trees(lt, dt) {
  68. var i;
  69.  
  70. /* build fixed length tree */
  71. for (i = 0; i < 7; ++i) lt.table[i] = 0;
  72.  
  73. lt.table[7] = 24;
  74. lt.table[8] = 152;
  75. lt.table[9] = 112;
  76.  
  77. for (i = 0; i < 24; ++i) lt.trans[i] = 256 + i;
  78. for (i = 0; i < 144; ++i) lt.trans[24 + i] = i;
  79. for (i = 0; i < 8; ++i) lt.trans[24 + 144 + i] = 280 + i;
  80. for (i = 0; i < 112; ++i) lt.trans[24 + 144 + 8 + i] = 144 + i;
  81.  
  82. /* build fixed distance tree */
  83. for (i = 0; i < 5; ++i) dt.table[i] = 0;
  84.  
  85. dt.table[5] = 32;
  86.  
  87. for (i = 0; i < 32; ++i) dt.trans[i] = i;
  88. }
  89.  
  90. /* given an array of code lengths, build a tree */
  91. var offs = new Uint16Array(16);
  92.  
  93. function tinf_build_tree(t, lengths, off, num) {
  94. var i, sum;
  95.  
  96. /* clear code length count table */
  97. for (i = 0; i < 16; ++i) t.table[i] = 0;
  98.  
  99. /* scan symbol lengths, and sum code length counts */
  100. for (i = 0; i < num; ++i) t.table[lengths[off + i]]++;
  101.  
  102. t.table[0] = 0;
  103.  
  104. /* compute offset table for distribution sort */
  105. for (sum = 0, i = 0; i < 16; ++i) {
  106. offs[i] = sum;
  107. sum += t.table[i];
  108. }
  109.  
  110. /* create code->symbol translation table (symbols sorted by code) */
  111. for (i = 0; i < num; ++i) {
  112. if (lengths[off + i]) t.trans[offs[lengths[off + i]]++] = i;
  113. }
  114. }
  115.  
  116. /* ---------------------- *
  117. * -- decode functions -- *
  118. * ---------------------- */
  119.  
  120. /* get one bit from source stream */
  121. function tinf_getbit(d) {
  122. /* check if tag is empty */
  123. if (!d.bitcount--) {
  124. /* load next tag */
  125. d.tag = d.source[d.sourceIndex++];
  126. d.bitcount = 7;
  127. }
  128.  
  129. /* shift bit out of tag */
  130. var bit = d.tag & 1;
  131. d.tag >>>= 1;
  132.  
  133. return bit;
  134. }
  135.  
  136. /* read a num bit value from a stream and add base */
  137. function tinf_read_bits(d, num, base) {
  138. if (!num)
  139. return base;
  140.  
  141. while (d.bitcount < 24) {
  142. d.tag |= d.source[d.sourceIndex++] << d.bitcount;
  143. d.bitcount += 8;
  144. }
  145.  
  146. var val = d.tag & (0xffff >>> (16 - num));
  147. d.tag >>>= num;
  148. d.bitcount -= num;
  149. return val + base;
  150. }
  151.  
  152. /* given a data stream and a tree, decode a symbol */
  153. function tinf_decode_symbol(d, t) {
  154. while (d.bitcount < 24) {
  155. d.tag |= d.source[d.sourceIndex++] << d.bitcount;
  156. d.bitcount += 8;
  157. }
  158. var sum = 0, cur = 0, len = 0;
  159. var tag = d.tag;
  160.  
  161. /* get more bits while code value is above sum */
  162. do {
  163. cur = 2 * cur + (tag & 1);
  164. tag >>>= 1;
  165. ++len;
  166.  
  167. sum += t.table[len];
  168. cur -= t.table[len];
  169. } while (cur >= 0);
  170. d.tag = tag;
  171. d.bitcount -= len;
  172.  
  173. return t.trans[sum + cur];
  174. }
  175.  
  176. /* given a data stream, decode dynamic trees from it */
  177. function tinf_decode_trees(d, lt, dt) {
  178. var hlit, hdist, hclen;
  179. var i, num, length;
  180.  
  181. /* get 5 bits HLIT (257-286) */
  182. hlit = tinf_read_bits(d, 5, 257);
  183.  
  184. /* get 5 bits HDIST (1-32) */
  185. hdist = tinf_read_bits(d, 5, 1);
  186.  
  187. /* get 4 bits HCLEN (4-19) */
  188. hclen = tinf_read_bits(d, 4, 4);
  189.  
  190. for (i = 0; i < 19; ++i) lengths[i] = 0;
  191.  
  192. /* read code lengths for code length alphabet */
  193. for (i = 0; i < hclen; ++i) {
  194. /* get 3 bits code length (0-7) */
  195. var clen = tinf_read_bits(d, 3, 0);
  196. lengths[clcidx[i]] = clen;
  197. }
  198.  
  199. /* build code length tree */
  200. tinf_build_tree(code_tree, lengths, 0, 19);
  201.  
  202. /* decode code lengths for the dynamic trees */
  203. for (num = 0; num < hlit + hdist;) {
  204. var sym = tinf_decode_symbol(d, code_tree);
  205.  
  206. switch (sym) {
  207. case 16:
  208. /* copy previous code length 3-6 times (read 2 bits) */
  209. var prev = lengths[num - 1];
  210. for (length = tinf_read_bits(d, 2, 3); length; --length) {
  211. lengths[num++] = prev;
  212. }
  213. break;
  214. case 17:
  215. /* repeat code length 0 for 3-10 times (read 3 bits) */
  216. for (length = tinf_read_bits(d, 3, 3); length; --length) {
  217. lengths[num++] = 0;
  218. }
  219. break;
  220. case 18:
  221. /* repeat code length 0 for 11-138 times (read 7 bits) */
  222. for (length = tinf_read_bits(d, 7, 11); length; --length) {
  223. lengths[num++] = 0;
  224. }
  225. break;
  226. default:
  227. /* values 0-15 represent the actual code lengths */
  228. lengths[num++] = sym;
  229. break;
  230. }
  231. }
  232.  
  233. /* build dynamic trees */
  234. tinf_build_tree(lt, lengths, 0, hlit);
  235. tinf_build_tree(dt, lengths, hlit, hdist);
  236. }
  237.  
  238. /* ----------------------------- *
  239. * -- block inflate functions -- *
  240. * ----------------------------- */
  241.  
  242. /* given a stream and two trees, inflate a block of data */
  243. function tinf_inflate_block_data(d, lt, dt) {
  244. while (1) {
  245. var sym = tinf_decode_symbol(d, lt);
  246.  
  247. /* check for end of block */
  248. if (sym === 256) {
  249. return TINF_OK;
  250. }
  251.  
  252. if (sym < 256) {
  253. d.dest[d.destLen++] = sym;
  254. } else {
  255. var length, dist, offs;
  256. var i;
  257.  
  258. sym -= 257;
  259.  
  260. /* possibly get more bits from length code */
  261. length = tinf_read_bits(d, length_bits[sym], length_base[sym]);
  262.  
  263. dist = tinf_decode_symbol(d, dt);
  264.  
  265. /* possibly get more bits from distance code */
  266. offs = d.destLen - tinf_read_bits(d, dist_bits[dist], dist_base[dist]);
  267.  
  268. /* copy match */
  269. for (i = offs; i < offs + length; ++i) {
  270. d.dest[d.destLen++] = d.dest[i];
  271. }
  272. }
  273. }
  274. }
  275.  
  276. /* inflate an uncompressed block of data */
  277. function tinf_inflate_uncompressed_block(d) {
  278. var length, invlength;
  279. var i;
  280. /* unread from bitbuffer */
  281. while (d.bitcount > 8) {
  282. d.sourceIndex--;
  283. d.bitcount -= 8;
  284. }
  285.  
  286. /* get length */
  287. length = d.source[d.sourceIndex + 1];
  288. length = 256 * length + d.source[d.sourceIndex];
  289.  
  290. /* get one's complement of length */
  291. invlength = d.source[d.sourceIndex + 3];
  292. invlength = 256 * invlength + d.source[d.sourceIndex + 2];
  293.  
  294. /* check length */
  295. if (length !== (~invlength & 0x0000ffff))
  296. return TINF_DATA_ERROR;
  297.  
  298. d.sourceIndex += 4;
  299.  
  300. /* copy block */
  301. for (i = length; i; --i)
  302. d.dest[d.destLen++] = d.source[d.sourceIndex++];
  303.  
  304. /* make sure we start next block on a byte boundary */
  305. d.bitcount = 0;
  306.  
  307. return TINF_OK;
  308. }
  309.  
  310. /* inflate stream from source to dest */
  311. function tinf_uncompress(source, dest) {
  312. var d = new Data(source, dest);
  313. var bfinal, res;
  314.  
  315. do {
  316. /* read final block flag */
  317. bfinal = tinf_getbit(d);
  318.  
  319. /* read block type (2 bits) */
  320. btype = tinf_read_bits(d, 2, 0);
  321.  
  322. /* decompress block */
  323. switch (btype) {
  324. case 0:
  325. /* decompress uncompressed block */
  326. res = tinf_inflate_uncompressed_block(d);
  327. break;
  328. case 1:
  329. /* decompress block with fixed huffman trees */
  330. res = tinf_inflate_block_data(d, sltree, sdtree);
  331. break;
  332. case 2:
  333. /* decompress block with dynamic huffman trees */
  334. tinf_decode_trees(d, d.ltree, d.dtree);
  335. res = tinf_inflate_block_data(d, d.ltree, d.dtree);
  336. break;
  337. default:
  338. res = TINF_DATA_ERROR;
  339. }
  340.  
  341. if (res !== TINF_OK)
  342. throw new Error('Data error');
  343.  
  344. } while (!bfinal);
  345.  
  346. if (d.destLen < d.dest.length) {
  347. if (typeof d.dest.slice === 'function')
  348. return d.dest.slice(0, d.destLen);
  349. else
  350. return d.dest.subarray(0, d.destLen);
  351. }
  352. return d.dest;
  353. }
  354.  
  355. /* -------------------- *
  356. * -- initialization -- *
  357. * -------------------- */
  358.  
  359. /* build fixed huffman trees */
  360. tinf_build_fixed_trees(sltree, sdtree);
  361.  
  362. /* build extra bits and base tables */
  363. tinf_build_bits_base(length_bits, length_base, 4, 3);
  364. tinf_build_bits_base(dist_bits, dist_base, 2, 1);
  365.  
  366. /* fix a special case */
  367. length_bits[28] = 0;
  368. length_base[28] = 258;
  369.  
  370. module.exports = tinf_uncompress;
  371.  
  372. },{}],2:[function(require,module,exports){
  373. // Run-time checking of preconditions.
  374.  
  375. 'use strict';
  376.  
  377. // Precondition function that checks if the given predicate is true.
  378. // If not, it will throw an error.
  379. exports.argument = function(predicate, message) {
  380. if (!predicate) {
  381. throw new Error(message);
  382. }
  383. };
  384.  
  385. // Precondition function that checks if the given assertion is true.
  386. // If not, it will throw an error.
  387. exports.assert = exports.argument;
  388.  
  389. },{}],3:[function(require,module,exports){
  390. // Drawing utility functions.
  391.  
  392. 'use strict';
  393.  
  394. // Draw a line on the given context from point `x1,y1` to point `x2,y2`.
  395. function line(ctx, x1, y1, x2, y2) {
  396. ctx.beginPath();
  397. ctx.moveTo(x1, y1);
  398. ctx.lineTo(x2, y2);
  399. ctx.stroke();
  400. }
  401.  
  402. exports.line = line;
  403.  
  404. },{}],4:[function(require,module,exports){
  405. // Glyph encoding
  406.  
  407. 'use strict';
  408.  
  409. var cffStandardStrings = [
  410. '.notdef', 'space', 'exclam', 'quotedbl', 'numbersign', 'dollar', 'percent', 'ampersand', 'quoteright',
  411. 'parenleft', 'parenright', 'asterisk', 'plus', 'comma', 'hyphen', 'period', 'slash', 'zero', 'one', 'two',
  412. 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'colon', 'semicolon', 'less', 'equal', 'greater',
  413. 'question', 'at', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
  414. 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'bracketleft', 'backslash', 'bracketright', 'asciicircum', 'underscore',
  415. 'quoteleft', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
  416. 'u', 'v', 'w', 'x', 'y', 'z', 'braceleft', 'bar', 'braceright', 'asciitilde', 'exclamdown', 'cent', 'sterling',
  417. 'fraction', 'yen', 'florin', 'section', 'currency', 'quotesingle', 'quotedblleft', 'guillemotleft',
  418. 'guilsinglleft', 'guilsinglright', 'fi', 'fl', 'endash', 'dagger', 'daggerdbl', 'periodcentered', 'paragraph',
  419. 'bullet', 'quotesinglbase', 'quotedblbase', 'quotedblright', 'guillemotright', 'ellipsis', 'perthousand',
  420. 'questiondown', 'grave', 'acute', 'circumflex', 'tilde', 'macron', 'breve', 'dotaccent', 'dieresis', 'ring',
  421. 'cedilla', 'hungarumlaut', 'ogonek', 'caron', 'emdash', 'AE', 'ordfeminine', 'Lslash', 'Oslash', 'OE',
  422. 'ordmasculine', 'ae', 'dotlessi', 'lslash', 'oslash', 'oe', 'germandbls', 'onesuperior', 'logicalnot', 'mu',
  423. 'trademark', 'Eth', 'onehalf', 'plusminus', 'Thorn', 'onequarter', 'divide', 'brokenbar', 'degree', 'thorn',
  424. 'threequarters', 'twosuperior', 'registered', 'minus', 'eth', 'multiply', 'threesuperior', 'copyright',
  425. 'Aacute', 'Acircumflex', 'Adieresis', 'Agrave', 'Aring', 'Atilde', 'Ccedilla', 'Eacute', 'Ecircumflex',
  426. 'Edieresis', 'Egrave', 'Iacute', 'Icircumflex', 'Idieresis', 'Igrave', 'Ntilde', 'Oacute', 'Ocircumflex',
  427. 'Odieresis', 'Ograve', 'Otilde', 'Scaron', 'Uacute', 'Ucircumflex', 'Udieresis', 'Ugrave', 'Yacute',
  428. 'Ydieresis', 'Zcaron', 'aacute', 'acircumflex', 'adieresis', 'agrave', 'aring', 'atilde', 'ccedilla', 'eacute',
  429. 'ecircumflex', 'edieresis', 'egrave', 'iacute', 'icircumflex', 'idieresis', 'igrave', 'ntilde', 'oacute',
  430. 'ocircumflex', 'odieresis', 'ograve', 'otilde', 'scaron', 'uacute', 'ucircumflex', 'udieresis', 'ugrave',
  431. 'yacute', 'ydieresis', 'zcaron', 'exclamsmall', 'Hungarumlautsmall', 'dollaroldstyle', 'dollarsuperior',
  432. 'ampersandsmall', 'Acutesmall', 'parenleftsuperior', 'parenrightsuperior', '266 ff', 'onedotenleader',
  433. 'zerooldstyle', 'oneoldstyle', 'twooldstyle', 'threeoldstyle', 'fouroldstyle', 'fiveoldstyle', 'sixoldstyle',
  434. 'sevenoldstyle', 'eightoldstyle', 'nineoldstyle', 'commasuperior', 'threequartersemdash', 'periodsuperior',
  435. 'questionsmall', 'asuperior', 'bsuperior', 'centsuperior', 'dsuperior', 'esuperior', 'isuperior', 'lsuperior',
  436. 'msuperior', 'nsuperior', 'osuperior', 'rsuperior', 'ssuperior', 'tsuperior', 'ff', 'ffi', 'ffl',
  437. 'parenleftinferior', 'parenrightinferior', 'Circumflexsmall', 'hyphensuperior', 'Gravesmall', 'Asmall',
  438. 'Bsmall', 'Csmall', 'Dsmall', 'Esmall', 'Fsmall', 'Gsmall', 'Hsmall', 'Ismall', 'Jsmall', 'Ksmall', 'Lsmall',
  439. 'Msmall', 'Nsmall', 'Osmall', 'Psmall', 'Qsmall', 'Rsmall', 'Ssmall', 'Tsmall', 'Usmall', 'Vsmall', 'Wsmall',
  440. 'Xsmall', 'Ysmall', 'Zsmall', 'colonmonetary', 'onefitted', 'rupiah', 'Tildesmall', 'exclamdownsmall',
  441. 'centoldstyle', 'Lslashsmall', 'Scaronsmall', 'Zcaronsmall', 'Dieresissmall', 'Brevesmall', 'Caronsmall',
  442. 'Dotaccentsmall', 'Macronsmall', 'figuredash', 'hypheninferior', 'Ogoneksmall', 'Ringsmall', 'Cedillasmall',
  443. 'questiondownsmall', 'oneeighth', 'threeeighths', 'fiveeighths', 'seveneighths', 'onethird', 'twothirds',
  444. 'zerosuperior', 'foursuperior', 'fivesuperior', 'sixsuperior', 'sevensuperior', 'eightsuperior', 'ninesuperior',
  445. 'zeroinferior', 'oneinferior', 'twoinferior', 'threeinferior', 'fourinferior', 'fiveinferior', 'sixinferior',
  446. 'seveninferior', 'eightinferior', 'nineinferior', 'centinferior', 'dollarinferior', 'periodinferior',
  447. 'commainferior', 'Agravesmall', 'Aacutesmall', 'Acircumflexsmall', 'Atildesmall', 'Adieresissmall',
  448. 'Aringsmall', 'AEsmall', 'Ccedillasmall', 'Egravesmall', 'Eacutesmall', 'Ecircumflexsmall', 'Edieresissmall',
  449. 'Igravesmall', 'Iacutesmall', 'Icircumflexsmall', 'Idieresissmall', 'Ethsmall', 'Ntildesmall', 'Ogravesmall',
  450. 'Oacutesmall', 'Ocircumflexsmall', 'Otildesmall', 'Odieresissmall', 'OEsmall', 'Oslashsmall', 'Ugravesmall',
  451. 'Uacutesmall', 'Ucircumflexsmall', 'Udieresissmall', 'Yacutesmall', 'Thornsmall', 'Ydieresissmall', '001.000',
  452. '001.001', '001.002', '001.003', 'Black', 'Bold', 'Book', 'Light', 'Medium', 'Regular', 'Roman', 'Semibold'];
  453.  
  454. var cffStandardEncoding = [
  455. '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
  456. '', '', '', '', 'space', 'exclam', 'quotedbl', 'numbersign', 'dollar', 'percent', 'ampersand', 'quoteright',
  457. 'parenleft', 'parenright', 'asterisk', 'plus', 'comma', 'hyphen', 'period', 'slash', 'zero', 'one', 'two',
  458. 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'colon', 'semicolon', 'less', 'equal', 'greater',
  459. 'question', 'at', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
  460. 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'bracketleft', 'backslash', 'bracketright', 'asciicircum', 'underscore',
  461. 'quoteleft', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
  462. 'u', 'v', 'w', 'x', 'y', 'z', 'braceleft', 'bar', 'braceright', 'asciitilde', '', '', '', '', '', '', '', '',
  463. '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
  464. 'exclamdown', 'cent', 'sterling', 'fraction', 'yen', 'florin', 'section', 'currency', 'quotesingle',
  465. 'quotedblleft', 'guillemotleft', 'guilsinglleft', 'guilsinglright', 'fi', 'fl', '', 'endash', 'dagger',
  466. 'daggerdbl', 'periodcentered', '', 'paragraph', 'bullet', 'quotesinglbase', 'quotedblbase', 'quotedblright',
  467. 'guillemotright', 'ellipsis', 'perthousand', '', 'questiondown', '', 'grave', 'acute', 'circumflex', 'tilde',
  468. 'macron', 'breve', 'dotaccent', 'dieresis', '', 'ring', 'cedilla', '', 'hungarumlaut', 'ogonek', 'caron',
  469. 'emdash', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'AE', '', 'ordfeminine', '', '', '',
  470. '', 'Lslash', 'Oslash', 'OE', 'ordmasculine', '', '', '', '', '', 'ae', '', '', '', 'dotlessi', '', '',
  471. 'lslash', 'oslash', 'oe', 'germandbls'];
  472.  
  473. var cffExpertEncoding = [
  474. '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
  475. '', '', '', '', 'space', 'exclamsmall', 'Hungarumlautsmall', '', 'dollaroldstyle', 'dollarsuperior',
  476. 'ampersandsmall', 'Acutesmall', 'parenleftsuperior', 'parenrightsuperior', 'twodotenleader', 'onedotenleader',
  477. 'comma', 'hyphen', 'period', 'fraction', 'zerooldstyle', 'oneoldstyle', 'twooldstyle', 'threeoldstyle',
  478. 'fouroldstyle', 'fiveoldstyle', 'sixoldstyle', 'sevenoldstyle', 'eightoldstyle', 'nineoldstyle', 'colon',
  479. 'semicolon', 'commasuperior', 'threequartersemdash', 'periodsuperior', 'questionsmall', '', 'asuperior',
  480. 'bsuperior', 'centsuperior', 'dsuperior', 'esuperior', '', '', 'isuperior', '', '', 'lsuperior', 'msuperior',
  481. 'nsuperior', 'osuperior', '', '', 'rsuperior', 'ssuperior', 'tsuperior', '', 'ff', 'fi', 'fl', 'ffi', 'ffl',
  482. 'parenleftinferior', '', 'parenrightinferior', 'Circumflexsmall', 'hyphensuperior', 'Gravesmall', 'Asmall',
  483. 'Bsmall', 'Csmall', 'Dsmall', 'Esmall', 'Fsmall', 'Gsmall', 'Hsmall', 'Ismall', 'Jsmall', 'Ksmall', 'Lsmall',
  484. 'Msmall', 'Nsmall', 'Osmall', 'Psmall', 'Qsmall', 'Rsmall', 'Ssmall', 'Tsmall', 'Usmall', 'Vsmall', 'Wsmall',
  485. 'Xsmall', 'Ysmall', 'Zsmall', 'colonmonetary', 'onefitted', 'rupiah', 'Tildesmall', '', '', '', '', '', '', '',
  486. '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
  487. 'exclamdownsmall', 'centoldstyle', 'Lslashsmall', '', '', 'Scaronsmall', 'Zcaronsmall', 'Dieresissmall',
  488. 'Brevesmall', 'Caronsmall', '', 'Dotaccentsmall', '', '', 'Macronsmall', '', '', 'figuredash', 'hypheninferior',
  489. '', '', 'Ogoneksmall', 'Ringsmall', 'Cedillasmall', '', '', '', 'onequarter', 'onehalf', 'threequarters',
  490. 'questiondownsmall', 'oneeighth', 'threeeighths', 'fiveeighths', 'seveneighths', 'onethird', 'twothirds', '',
  491. '', 'zerosuperior', 'onesuperior', 'twosuperior', 'threesuperior', 'foursuperior', 'fivesuperior',
  492. 'sixsuperior', 'sevensuperior', 'eightsuperior', 'ninesuperior', 'zeroinferior', 'oneinferior', 'twoinferior',
  493. 'threeinferior', 'fourinferior', 'fiveinferior', 'sixinferior', 'seveninferior', 'eightinferior',
  494. 'nineinferior', 'centinferior', 'dollarinferior', 'periodinferior', 'commainferior', 'Agravesmall',
  495. 'Aacutesmall', 'Acircumflexsmall', 'Atildesmall', 'Adieresissmall', 'Aringsmall', 'AEsmall', 'Ccedillasmall',
  496. 'Egravesmall', 'Eacutesmall', 'Ecircumflexsmall', 'Edieresissmall', 'Igravesmall', 'Iacutesmall',
  497. 'Icircumflexsmall', 'Idieresissmall', 'Ethsmall', 'Ntildesmall', 'Ogravesmall', 'Oacutesmall',
  498. 'Ocircumflexsmall', 'Otildesmall', 'Odieresissmall', 'OEsmall', 'Oslashsmall', 'Ugravesmall', 'Uacutesmall',
  499. 'Ucircumflexsmall', 'Udieresissmall', 'Yacutesmall', 'Thornsmall', 'Ydieresissmall'];
  500.  
  501. var standardNames = [
  502. '.notdef', '.null', 'nonmarkingreturn', 'space', 'exclam', 'quotedbl', 'numbersign', 'dollar', 'percent',
  503. 'ampersand', 'quotesingle', 'parenleft', 'parenright', 'asterisk', 'plus', 'comma', 'hyphen', 'period', 'slash',
  504. 'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'colon', 'semicolon', 'less',
  505. 'equal', 'greater', 'question', 'at', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
  506. 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'bracketleft', 'backslash', 'bracketright',
  507. 'asciicircum', 'underscore', 'grave', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  508. 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'braceleft', 'bar', 'braceright', 'asciitilde',
  509. 'Adieresis', 'Aring', 'Ccedilla', 'Eacute', 'Ntilde', 'Odieresis', 'Udieresis', 'aacute', 'agrave',
  510. 'acircumflex', 'adieresis', 'atilde', 'aring', 'ccedilla', 'eacute', 'egrave', 'ecircumflex', 'edieresis',
  511. 'iacute', 'igrave', 'icircumflex', 'idieresis', 'ntilde', 'oacute', 'ograve', 'ocircumflex', 'odieresis',
  512. 'otilde', 'uacute', 'ugrave', 'ucircumflex', 'udieresis', 'dagger', 'degree', 'cent', 'sterling', 'section',
  513. 'bullet', 'paragraph', 'germandbls', 'registered', 'copyright', 'trademark', 'acute', 'dieresis', 'notequal',
  514. 'AE', 'Oslash', 'infinity', 'plusminus', 'lessequal', 'greaterequal', 'yen', 'mu', 'partialdiff', 'summation',
  515. 'product', 'pi', 'integral', 'ordfeminine', 'ordmasculine', 'Omega', 'ae', 'oslash', 'questiondown',
  516. 'exclamdown', 'logicalnot', 'radical', 'florin', 'approxequal', 'Delta', 'guillemotleft', 'guillemotright',
  517. 'ellipsis', 'nonbreakingspace', 'Agrave', 'Atilde', 'Otilde', 'OE', 'oe', 'endash', 'emdash', 'quotedblleft',
  518. 'quotedblright', 'quoteleft', 'quoteright', 'divide', 'lozenge', 'ydieresis', 'Ydieresis', 'fraction',
  519. 'currency', 'guilsinglleft', 'guilsinglright', 'fi', 'fl', 'daggerdbl', 'periodcentered', 'quotesinglbase',
  520. 'quotedblbase', 'perthousand', 'Acircumflex', 'Ecircumflex', 'Aacute', 'Edieresis', 'Egrave', 'Iacute',
  521. 'Icircumflex', 'Idieresis', 'Igrave', 'Oacute', 'Ocircumflex', 'apple', 'Ograve', 'Uacute', 'Ucircumflex',
  522. 'Ugrave', 'dotlessi', 'circumflex', 'tilde', 'macron', 'breve', 'dotaccent', 'ring', 'cedilla', 'hungarumlaut',
  523. 'ogonek', 'caron', 'Lslash', 'lslash', 'Scaron', 'scaron', 'Zcaron', 'zcaron', 'brokenbar', 'Eth', 'eth',
  524. 'Yacute', 'yacute', 'Thorn', 'thorn', 'minus', 'multiply', 'onesuperior', 'twosuperior', 'threesuperior',
  525. 'onehalf', 'onequarter', 'threequarters', 'franc', 'Gbreve', 'gbreve', 'Idotaccent', 'Scedilla', 'scedilla',
  526. 'Cacute', 'cacute', 'Ccaron', 'ccaron', 'dcroat'];
  527.  
  528. // This is the encoding used for fonts created from scratch.
  529. // It loops through all glyphs and finds the appropriate unicode value.
  530. // Since it's linear time, other encodings will be faster.
  531. function DefaultEncoding(font) {
  532. this.font = font;
  533. }
  534.  
  535. DefaultEncoding.prototype.charToGlyphIndex = function(c) {
  536. var code = c.charCodeAt(0);
  537. var glyphs = this.font.glyphs;
  538. if (glyphs) {
  539. for (var i = 0; i < glyphs.length; i += 1) {
  540. var glyph = glyphs.get(i);
  541. for (var j = 0; j < glyph.unicodes.length; j += 1) {
  542. if (glyph.unicodes[j] === code) {
  543. return i;
  544. }
  545. }
  546. }
  547. } else {
  548. return null;
  549. }
  550. };
  551.  
  552. function CmapEncoding(cmap) {
  553. this.cmap = cmap;
  554. }
  555.  
  556. CmapEncoding.prototype.charToGlyphIndex = function(c) {
  557. return this.cmap.glyphIndexMap[c.charCodeAt(0)] || 0;
  558. };
  559.  
  560. function CffEncoding(encoding, charset) {
  561. this.encoding = encoding;
  562. this.charset = charset;
  563. }
  564.  
  565. CffEncoding.prototype.charToGlyphIndex = function(s) {
  566. var code = s.charCodeAt(0);
  567. var charName = this.encoding[code];
  568. return this.charset.indexOf(charName);
  569. };
  570.  
  571. function GlyphNames(post) {
  572. var i;
  573. switch (post.version) {
  574. case 1:
  575. this.names = exports.standardNames.slice();
  576. break;
  577. case 2:
  578. this.names = new Array(post.numberOfGlyphs);
  579. for (i = 0; i < post.numberOfGlyphs; i++) {
  580. if (post.glyphNameIndex[i] < exports.standardNames.length) {
  581. this.names[i] = exports.standardNames[post.glyphNameIndex[i]];
  582. } else {
  583. this.names[i] = post.names[post.glyphNameIndex[i] - exports.standardNames.length];
  584. }
  585. }
  586.  
  587. break;
  588. case 2.5:
  589. this.names = new Array(post.numberOfGlyphs);
  590. for (i = 0; i < post.numberOfGlyphs; i++) {
  591. this.names[i] = exports.standardNames[i + post.glyphNameIndex[i]];
  592. }
  593.  
  594. break;
  595. case 3:
  596. this.names = [];
  597. break;
  598. }
  599. }
  600.  
  601. GlyphNames.prototype.nameToGlyphIndex = function(name) {
  602. return this.names.indexOf(name);
  603. };
  604.  
  605. GlyphNames.prototype.glyphIndexToName = function(gid) {
  606. return this.names[gid];
  607. };
  608.  
  609. function addGlyphNames(font) {
  610. var glyph;
  611. var glyphIndexMap = font.tables.cmap.glyphIndexMap;
  612. var charCodes = Object.keys(glyphIndexMap);
  613.  
  614. for (var i = 0; i < charCodes.length; i += 1) {
  615. var c = charCodes[i];
  616. var glyphIndex = glyphIndexMap[c];
  617. glyph = font.glyphs.get(glyphIndex);
  618. glyph.addUnicode(parseInt(c));
  619. }
  620.  
  621. for (i = 0; i < font.glyphs.length; i += 1) {
  622. glyph = font.glyphs.get(i);
  623. if (font.cffEncoding) {
  624. glyph.name = font.cffEncoding.charset[i];
  625. } else {
  626. glyph.name = font.glyphNames.glyphIndexToName(i);
  627. }
  628. }
  629. }
  630.  
  631. exports.cffStandardStrings = cffStandardStrings;
  632. exports.cffStandardEncoding = cffStandardEncoding;
  633. exports.cffExpertEncoding = cffExpertEncoding;
  634. exports.standardNames = standardNames;
  635. exports.DefaultEncoding = DefaultEncoding;
  636. exports.CmapEncoding = CmapEncoding;
  637. exports.CffEncoding = CffEncoding;
  638. exports.GlyphNames = GlyphNames;
  639. exports.addGlyphNames = addGlyphNames;
  640.  
  641. },{}],5:[function(require,module,exports){
  642. // The Font object
  643.  
  644. 'use strict';
  645.  
  646. var path = require('./path');
  647. var sfnt = require('./tables/sfnt');
  648. var encoding = require('./encoding');
  649. var glyphset = require('./glyphset');
  650. var util = require('./util');
  651.  
  652. // A Font represents a loaded OpenType font file.
  653. // It contains a set of glyphs and methods to draw text on a drawing context,
  654. // or to get a path representing the text.
  655. function Font(options) {
  656. options = options || {};
  657.  
  658. if (!options.empty) {
  659. // Check that we've provided the minimum set of names.
  660. util.checkArgument(options.familyName, 'When creating a new Font object, familyName is required.');
  661. util.checkArgument(options.styleName, 'When creating a new Font object, styleName is required.');
  662. util.checkArgument(options.unitsPerEm, 'When creating a new Font object, unitsPerEm is required.');
  663. util.checkArgument(options.ascender, 'When creating a new Font object, ascender is required.');
  664. util.checkArgument(options.descender, 'When creating a new Font object, descender is required.');
  665. util.checkArgument(options.descender < 0, 'Descender should be negative (e.g. -512).');
  666.  
  667. // OS X will complain if the names are empty, so we put a single space everywhere by default.
  668. this.names = {
  669. fontFamily: {en: options.familyName || ' '},
  670. fontSubfamily: {en: options.styleName || ' '},
  671. fullName: {en: options.fullName || options.familyName + ' ' + options.styleName},
  672. postScriptName: {en: options.postScriptName || options.familyName + options.styleName},
  673. designer: {en: options.designer || ' '},
  674. designerURL: {en: options.designerURL || ' '},
  675. manufacturer: {en: options.manufacturer || ' '},
  676. manufacturerURL: {en: options.manufacturerURL || ' '},
  677. license: {en: options.license || ' '},
  678. licenseURL: {en: options.licenseURL || ' '},
  679. version: {en: options.version || 'Version 0.1'},
  680. description: {en: options.description || ' '},
  681. copyright: {en: options.copyright || ' '},
  682. trademark: {en: options.trademark || ' '}
  683. };
  684. this.unitsPerEm = options.unitsPerEm || 1000;
  685. this.ascender = options.ascender;
  686. this.descender = options.descender;
  687. }
  688.  
  689. this.supported = true; // Deprecated: parseBuffer will throw an error if font is not supported.
  690. this.glyphs = new glyphset.GlyphSet(this, options.glyphs || []);
  691. this.encoding = new encoding.DefaultEncoding(this);
  692. this.tables = {};
  693. }
  694.  
  695. // Check if the font has a glyph for the given character.
  696. Font.prototype.hasChar = function(c) {
  697. return this.encoding.charToGlyphIndex(c) !== null;
  698. };
  699.  
  700. // Convert the given character to a single glyph index.
  701. // Note that this function assumes that there is a one-to-one mapping between
  702. // the given character and a glyph; for complex scripts this might not be the case.
  703. Font.prototype.charToGlyphIndex = function(s) {
  704. return this.encoding.charToGlyphIndex(s);
  705. };
  706.  
  707. // Convert the given character to a single Glyph object.
  708. // Note that this function assumes that there is a one-to-one mapping between
  709. // the given character and a glyph; for complex scripts this might not be the case.
  710. Font.prototype.charToGlyph = function(c) {
  711. var glyphIndex = this.charToGlyphIndex(c);
  712. var glyph = this.glyphs.get(glyphIndex);
  713. if (!glyph) {
  714. // .notdef
  715. glyph = this.glyphs.get(0);
  716. }
  717.  
  718. return glyph;
  719. };
  720.  
  721. // Convert the given text to a list of Glyph objects.
  722. // Note that there is no strict one-to-one mapping between characters and
  723. // glyphs, so the list of returned glyphs can be larger or smaller than the
  724. // length of the given string.
  725. Font.prototype.stringToGlyphs = function(s) {
  726. var glyphs = [];
  727. for (var i = 0; i < s.length; i += 1) {
  728. var c = s[i];
  729. glyphs.push(this.charToGlyph(c));
  730. }
  731.  
  732. return glyphs;
  733. };
  734.  
  735. Font.prototype.nameToGlyphIndex = function(name) {
  736. return this.glyphNames.nameToGlyphIndex(name);
  737. };
  738.  
  739. Font.prototype.nameToGlyph = function(name) {
  740. var glyphIndex = this.nametoGlyphIndex(name);
  741. var glyph = this.glyphs.get(glyphIndex);
  742. if (!glyph) {
  743. // .notdef
  744. glyph = this.glyphs.get(0);
  745. }
  746.  
  747. return glyph;
  748. };
  749.  
  750. Font.prototype.glyphIndexToName = function(gid) {
  751. if (!this.glyphNames.glyphIndexToName) {
  752. return '';
  753. }
  754.  
  755. return this.glyphNames.glyphIndexToName(gid);
  756. };
  757.  
  758. // Retrieve the value of the kerning pair between the left glyph (or its index)
  759. // and the right glyph (or its index). If no kerning pair is found, return 0.
  760. // The kerning value gets added to the advance width when calculating the spacing
  761. // between glyphs.
  762. Font.prototype.getKerningValue = function(leftGlyph, rightGlyph) {
  763. leftGlyph = leftGlyph.index || leftGlyph;
  764. rightGlyph = rightGlyph.index || rightGlyph;
  765. var gposKerning = this.getGposKerningValue;
  766. return gposKerning ? gposKerning(leftGlyph, rightGlyph) :
  767. (this.kerningPairs[leftGlyph + ',' + rightGlyph] || 0);
  768. };
  769.  
  770. // Helper function that invokes the given callback for each glyph in the given text.
  771. // The callback gets `(glyph, x, y, fontSize, options)`.
  772. Font.prototype.forEachGlyph = function(text, x, y, fontSize, options, callback) {
  773. x = x !== undefined ? x : 0;
  774. y = y !== undefined ? y : 0;
  775. fontSize = fontSize !== undefined ? fontSize : 72;
  776. options = options || {};
  777. var kerning = options.kerning === undefined ? true : options.kerning;
  778. var fontScale = 1 / this.unitsPerEm * fontSize;
  779. var glyphs = this.stringToGlyphs(text);
  780. for (var i = 0; i < glyphs.length; i += 1) {
  781. var glyph = glyphs[i];
  782. callback(glyph, x, y, fontSize, options);
  783. if (glyph.advanceWidth) {
  784. x += glyph.advanceWidth * fontScale;
  785. }
  786.  
  787. if (kerning && i < glyphs.length - 1) {
  788. var kerningValue = this.getKerningValue(glyph, glyphs[i + 1]);
  789. x += kerningValue * fontScale;
  790. }
  791. }
  792. };
  793.  
  794. // Create a Path object that represents the given text.
  795. //
  796. // text - The text to create.
  797. // x - Horizontal position of the beginning of the text. (default: 0)
  798. // y - Vertical position of the *baseline* of the text. (default: 0)
  799. // fontSize - Font size in pixels. We scale the glyph units by `1 / unitsPerEm * fontSize`. (default: 72)
  800. // Options is an optional object that contains:
  801. // - kerning - Whether to take kerning information into account. (default: true)
  802. //
  803. // Returns a Path object.
  804. Font.prototype.getPath = function(text, x, y, fontSize, options) {
  805. var fullPath = new path.Path();
  806. this.forEachGlyph(text, x, y, fontSize, options, function(glyph, gX, gY, gFontSize) {
  807. var glyphPath = glyph.getPath(gX, gY, gFontSize);
  808. fullPath.extend(glyphPath);
  809. });
  810.  
  811. return fullPath;
  812. };
  813.  
  814. // Create an array of Path objects that represent the glyps of a given text.
  815. //
  816. // text - The text to create.
  817. // x - Horizontal position of the beginning of the text. (default: 0)
  818. // y - Vertical position of the *baseline* of the text. (default: 0)
  819. // fontSize - Font size in pixels. We scale the glyph units by `1 / unitsPerEm * fontSize`. (default: 72)
  820. // Options is an optional object that contains:
  821. // - kerning - Whether to take kerning information into account. (default: true)
  822. //
  823. // Returns an array of Path objects.
  824. Font.prototype.getPaths = function(text, x, y, fontSize, options) {
  825. var glyphPaths = [];
  826. this.forEachGlyph(text, x, y, fontSize, options, function(glyph, gX, gY, gFontSize) {
  827. var glyphPath = glyph.getPath(gX, gY, gFontSize);
  828. glyphPaths.push(glyphPath);
  829. });
  830.  
  831. return glyphPaths;
  832. };
  833.  
  834. // Draw the text on the given drawing context.
  835. //
  836. // ctx - A 2D drawing context, like Canvas.
  837. // text - The text to create.
  838. // x - Horizontal position of the beginning of the text. (default: 0)
  839. // y - Vertical position of the *baseline* of the text. (default: 0)
  840. // fontSize - Font size in pixels. We scale the glyph units by `1 / unitsPerEm * fontSize`. (default: 72)
  841. // Options is an optional object that contains:
  842. // - kerning - Whether to take kerning information into account. (default: true)
  843. Font.prototype.draw = function(ctx, text, x, y, fontSize, options) {
  844. this.getPath(text, x, y, fontSize, options).draw(ctx);
  845. };
  846.  
  847. // Draw the points of all glyphs in the text.
  848. // On-curve points will be drawn in blue, off-curve points will be drawn in red.
  849. //
  850. // ctx - A 2D drawing context, like Canvas.
  851. // text - The text to create.
  852. // x - Horizontal position of the beginning of the text. (default: 0)
  853. // y - Vertical position of the *baseline* of the text. (default: 0)
  854. // fontSize - Font size in pixels. We scale the glyph units by `1 / unitsPerEm * fontSize`. (default: 72)
  855. // Options is an optional object that contains:
  856. // - kerning - Whether to take kerning information into account. (default: true)
  857. Font.prototype.drawPoints = function(ctx, text, x, y, fontSize, options) {
  858. this.forEachGlyph(text, x, y, fontSize, options, function(glyph, gX, gY, gFontSize) {
  859. glyph.drawPoints(ctx, gX, gY, gFontSize);
  860. });
  861. };
  862.  
  863. // Draw lines indicating important font measurements for all glyphs in the text.
  864. // Black lines indicate the origin of the coordinate system (point 0,0).
  865. // Blue lines indicate the glyph bounding box.
  866. // Green line indicates the advance width of the glyph.
  867. //
  868. // ctx - A 2D drawing context, like Canvas.
  869. // text - The text to create.
  870. // x - Horizontal position of the beginning of the text. (default: 0)
  871. // y - Vertical position of the *baseline* of the text. (default: 0)
  872. // fontSize - Font size in pixels. We scale the glyph units by `1 / unitsPerEm * fontSize`. (default: 72)
  873. // Options is an optional object that contains:
  874. // - kerning - Whether to take kerning information into account. (default: true)
  875. Font.prototype.drawMetrics = function(ctx, text, x, y, fontSize, options) {
  876. this.forEachGlyph(text, x, y, fontSize, options, function(glyph, gX, gY, gFontSize) {
  877. glyph.drawMetrics(ctx, gX, gY, gFontSize);
  878. });
  879. };
  880.  
  881. Font.prototype.getEnglishName = function(name) {
  882. var translations = this.names[name];
  883. if (translations) {
  884. return translations.en;
  885. }
  886. };
  887.  
  888. // Validate
  889. Font.prototype.validate = function() {
  890. var warnings = [];
  891. var _this = this;
  892.  
  893. function assert(predicate, message) {
  894. if (!predicate) {
  895. warnings.push(message);
  896. }
  897. }
  898.  
  899. function assertNamePresent(name) {
  900. var englishName = _this.getEnglishName(name);
  901. assert(englishName && englishName.trim().length > 0,
  902. 'No English ' + name + ' specified.');
  903. }
  904.  
  905. // Identification information
  906. assertNamePresent('fontFamily');
  907. assertNamePresent('weightName');
  908. assertNamePresent('manufacturer');
  909. assertNamePresent('copyright');
  910. assertNamePresent('version');
  911.  
  912. // Dimension information
  913. assert(this.unitsPerEm > 0, 'No unitsPerEm specified.');
  914. };
  915.  
  916. // Convert the font object to a SFNT data structure.
  917. // This structure contains all the necessary tables and metadata to create a binary OTF file.
  918. Font.prototype.toTables = function() {
  919. return sfnt.fontToTable(this);
  920. };
  921.  
  922. Font.prototype.toBuffer = function() {
  923. console.warn('Font.toBuffer is deprecated. Use Font.toArrayBuffer instead.');
  924. return this.toArrayBuffer();
  925. };
  926.  
  927. Font.prototype.toArrayBuffer = function() {
  928. var sfntTable = this.toTables();
  929. var bytes = sfntTable.encode();
  930. var buffer = new ArrayBuffer(bytes.length);
  931. var intArray = new Uint8Array(buffer);
  932. for (var i = 0; i < bytes.length; i++) {
  933. intArray[i] = bytes[i];
  934. }
  935.  
  936. return buffer;
  937. };
  938.  
  939. // Initiate a download of the OpenType font.
  940. Font.prototype.download = function() {
  941. var familyName = this.getEnglishName('fontFamily');
  942. var styleName = this.getEnglishName('fontSubfamily');
  943. var fileName = familyName.replace(/\s/g, '') + '-' + styleName + '.otf';
  944. var arrayBuffer = this.toArrayBuffer();
  945.  
  946. if (util.isBrowser()) {
  947. window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
  948. window.requestFileSystem(window.TEMPORARY, arrayBuffer.byteLength, function(fs) {
  949. fs.root.getFile(fileName, {create: true}, function(fileEntry) {
  950. fileEntry.createWriter(function(writer) {
  951. var dataView = new DataView(arrayBuffer);
  952. var blob = new Blob([dataView], {type: 'font/opentype'});
  953. writer.write(blob);
  954.  
  955. writer.addEventListener('writeend', function() {
  956. // Navigating to the file will download it.
  957. location.href = fileEntry.toURL();
  958. }, false);
  959. });
  960. });
  961. },
  962. function(err) {
  963. throw err;
  964. });
  965. } else {
  966. var fs = require('fs');
  967. var buffer = util.arrayBufferToNodeBuffer(arrayBuffer);
  968. fs.writeFileSync(fileName, buffer);
  969. }
  970. };
  971.  
  972. exports.Font = Font;
  973.  
  974. },{"./encoding":4,"./glyphset":7,"./path":10,"./tables/sfnt":27,"./util":29,"fs":undefined}],6:[function(require,module,exports){
  975. // The Glyph object
  976.  
  977. 'use strict';
  978.  
  979. var check = require('./check');
  980. var draw = require('./draw');
  981. var path = require('./path');
  982.  
  983. function getPathDefinition(glyph, path) {
  984. var _path = path || { commands: [] };
  985. return {
  986. configurable: true,
  987.  
  988. get: function() {
  989. if (typeof _path === 'function') {
  990. _path = _path();
  991. }
  992.  
  993. return _path;
  994. },
  995.  
  996. set: function(p) {
  997. _path = p;
  998. }
  999. };
  1000. }
  1001.  
  1002. // A Glyph is an individual mark that often corresponds to a character.
  1003. // Some glyphs, such as ligatures, are a combination of many characters.
  1004. // Glyphs are the basic building blocks of a font.
  1005. //
  1006. // The `Glyph` class contains utility methods for drawing the path and its points.
  1007. function Glyph(options) {
  1008. // By putting all the code on a prototype function (which is only declared once)
  1009. // we reduce the memory requirements for larger fonts by some 2%
  1010. this.bindConstructorValues(options);
  1011. }
  1012.  
  1013. Glyph.prototype.bindConstructorValues = function(options) {
  1014. this.index = options.index || 0;
  1015.  
  1016. // These three values cannnot be deferred for memory optimization:
  1017. this.name = options.name || null;
  1018. this.unicode = options.unicode || undefined;
  1019. this.unicodes = options.unicodes || options.unicode !== undefined ? [options.unicode] : [];
  1020.  
  1021. // But by binding these values only when necessary, we reduce can
  1022. // the memory requirements by almost 3% for larger fonts.
  1023. if (options.xMin) {
  1024. this.xMin = options.xMin;
  1025. }
  1026.  
  1027. if (options.yMin) {
  1028. this.yMin = options.yMin;
  1029. }
  1030.  
  1031. if (options.xMax) {
  1032. this.xMax = options.xMax;
  1033. }
  1034.  
  1035. if (options.yMax) {
  1036. this.yMax = options.yMax;
  1037. }
  1038.  
  1039. if (options.advanceWidth) {
  1040. this.advanceWidth = options.advanceWidth;
  1041. }
  1042.  
  1043. // The path for a glyph is the most memory intensive, and is bound as a value
  1044. // with a getter/setter to ensure we actually do path parsing only once the
  1045. // path is actually needed by anything.
  1046. Object.defineProperty(this, 'path', getPathDefinition(this, options.path));
  1047. };
  1048.  
  1049. Glyph.prototype.addUnicode = function(unicode) {
  1050. if (this.unicodes.length === 0) {
  1051. this.unicode = unicode;
  1052. }
  1053.  
  1054. this.unicodes.push(unicode);
  1055. };
  1056.  
  1057. // Convert the glyph to a Path we can draw on a drawing context.
  1058. //
  1059. // x - Horizontal position of the glyph. (default: 0)
  1060. // y - Vertical position of the *baseline* of the glyph. (default: 0)
  1061. // fontSize - Font size, in pixels (default: 72).
  1062. Glyph.prototype.getPath = function(x, y, fontSize) {
  1063. x = x !== undefined ? x : 0;
  1064. y = y !== undefined ? y : 0;
  1065. fontSize = fontSize !== undefined ? fontSize : 72;
  1066. var scale = 1 / this.path.unitsPerEm * fontSize;
  1067. var p = new path.Path();
  1068. var commands = this.path.commands;
  1069. for (var i = 0; i < commands.length; i += 1) {
  1070. var cmd = commands[i];
  1071. if (cmd.type === 'M') {
  1072. p.moveTo(x + (cmd.x * scale), y + (-cmd.y * scale));
  1073. } else if (cmd.type === 'L') {
  1074. p.lineTo(x + (cmd.x * scale), y + (-cmd.y * scale));
  1075. } else if (cmd.type === 'Q') {
  1076. p.quadraticCurveTo(x + (cmd.x1 * scale), y + (-cmd.y1 * scale),
  1077. x + (cmd.x * scale), y + (-cmd.y * scale));
  1078. } else if (cmd.type === 'C') {
  1079. p.curveTo(x + (cmd.x1 * scale), y + (-cmd.y1 * scale),
  1080. x + (cmd.x2 * scale), y + (-cmd.y2 * scale),
  1081. x + (cmd.x * scale), y + (-cmd.y * scale));
  1082. } else if (cmd.type === 'Z') {
  1083. p.closePath();
  1084. }
  1085. }
  1086.  
  1087. return p;
  1088. };
  1089.  
  1090. // Split the glyph into contours.
  1091. // This function is here for backwards compatibility, and to
  1092. // provide raw access to the TrueType glyph outlines.
  1093. Glyph.prototype.getContours = function() {
  1094. if (this.points === undefined) {
  1095. return [];
  1096. }
  1097.  
  1098. var contours = [];
  1099. var currentContour = [];
  1100. for (var i = 0; i < this.points.length; i += 1) {
  1101. var pt = this.points[i];
  1102. currentContour.push(pt);
  1103. if (pt.lastPointOfContour) {
  1104. contours.push(currentContour);
  1105. currentContour = [];
  1106. }
  1107. }
  1108.  
  1109. check.argument(currentContour.length === 0, 'There are still points left in the current contour.');
  1110. return contours;
  1111. };
  1112.  
  1113. // Calculate the xMin/yMin/xMax/yMax/lsb/rsb for a Glyph.
  1114. Glyph.prototype.getMetrics = function() {
  1115. var commands = this.path.commands;
  1116. var xCoords = [];
  1117. var yCoords = [];
  1118. for (var i = 0; i < commands.length; i += 1) {
  1119. var cmd = commands[i];
  1120. if (cmd.type !== 'Z') {
  1121. xCoords.push(cmd.x);
  1122. yCoords.push(cmd.y);
  1123. }
  1124.  
  1125. if (cmd.type === 'Q' || cmd.type === 'C') {
  1126. xCoords.push(cmd.x1);
  1127. yCoords.push(cmd.y1);
  1128. }
  1129.  
  1130. if (cmd.type === 'C') {
  1131. xCoords.push(cmd.x2);
  1132. yCoords.push(cmd.y2);
  1133. }
  1134. }
  1135.  
  1136. var metrics = {
  1137. xMin: Math.min.apply(null, xCoords),
  1138. yMin: Math.min.apply(null, yCoords),
  1139. xMax: Math.max.apply(null, xCoords),
  1140. yMax: Math.max.apply(null, yCoords),
  1141. leftSideBearing: 0
  1142. };
  1143.  
  1144. if (!isFinite(metrics.xMin)) {
  1145. metrics.xMin = 0;
  1146. }
  1147.  
  1148. if (!isFinite(metrics.xMax)) {
  1149. metrics.xMax = this.advanceWidth;
  1150. }
  1151.  
  1152. if (!isFinite(metrics.yMin)) {
  1153. metrics.yMin = 0;
  1154. }
  1155.  
  1156. if (!isFinite(metrics.yMax)) {
  1157. metrics.yMax = 0;
  1158. }
  1159.  
  1160. metrics.rightSideBearing = this.advanceWidth - metrics.leftSideBearing - (metrics.xMax - metrics.xMin);
  1161. return metrics;
  1162. };
  1163.  
  1164. // Draw the glyph on the given context.
  1165. //
  1166. // ctx - The drawing context.
  1167. // x - Horizontal position of the glyph. (default: 0)
  1168. // y - Vertical position of the *baseline* of the glyph. (default: 0)
  1169. // fontSize - Font size, in pixels (default: 72).
  1170. Glyph.prototype.draw = function(ctx, x, y, fontSize) {
  1171. this.getPath(x, y, fontSize).draw(ctx);
  1172. };
  1173.  
  1174. // Draw the points of the glyph.
  1175. // On-curve points will be drawn in blue, off-curve points will be drawn in red.
  1176. //
  1177. // ctx - The drawing context.
  1178. // x - Horizontal position of the glyph. (default: 0)
  1179. // y - Vertical position of the *baseline* of the glyph. (default: 0)
  1180. // fontSize - Font size, in pixels (default: 72).
  1181. Glyph.prototype.drawPoints = function(ctx, x, y, fontSize) {
  1182.  
  1183. function drawCircles(l, x, y, scale) {
  1184. var PI_SQ = Math.PI * 2;
  1185. ctx.beginPath();
  1186. for (var j = 0; j < l.length; j += 1) {
  1187. ctx.moveTo(x + (l[j].x * scale), y + (l[j].y * scale));
  1188. ctx.arc(x + (l[j].x * scale), y + (l[j].y * scale), 2, 0, PI_SQ, false);
  1189. }
  1190.  
  1191. ctx.closePath();
  1192. ctx.fill();
  1193. }
  1194.  
  1195. x = x !== undefined ? x : 0;
  1196. y = y !== undefined ? y : 0;
  1197. fontSize = fontSize !== undefined ? fontSize : 24;
  1198. var scale = 1 / this.path.unitsPerEm * fontSize;
  1199.  
  1200. var blueCircles = [];
  1201. var redCircles = [];
  1202. var path = this.path;
  1203. for (var i = 0; i < path.commands.length; i += 1) {
  1204. var cmd = path.commands[i];
  1205. if (cmd.x !== undefined) {
  1206. blueCircles.push({x: cmd.x, y: -cmd.y});
  1207. }
  1208.  
  1209. if (cmd.x1 !== undefined) {
  1210. redCircles.push({x: cmd.x1, y: -cmd.y1});
  1211. }
  1212.  
  1213. if (cmd.x2 !== undefined) {
  1214. redCircles.push({x: cmd.x2, y: -cmd.y2});
  1215. }
  1216. }
  1217.  
  1218. ctx.fillStyle = 'blue';
  1219. drawCircles(blueCircles, x, y, scale);
  1220. ctx.fillStyle = 'red';
  1221. drawCircles(redCircles, x, y, scale);
  1222. };
  1223.  
  1224. // Draw lines indicating important font measurements.
  1225. // Black lines indicate the origin of the coordinate system (point 0,0).
  1226. // Blue lines indicate the glyph bounding box.
  1227. // Green line indicates the advance width of the glyph.
  1228. //
  1229. // ctx - The drawing context.
  1230. // x - Horizontal position of the glyph. (default: 0)
  1231. // y - Vertical position of the *baseline* of the glyph. (default: 0)
  1232. // fontSize - Font size, in pixels (default: 72).
  1233. Glyph.prototype.drawMetrics = function(ctx, x, y, fontSize) {
  1234. var scale;
  1235. x = x !== undefined ? x : 0;
  1236. y = y !== undefined ? y : 0;
  1237. fontSize = fontSize !== undefined ? fontSize : 24;
  1238. scale = 1 / this.path.unitsPerEm * fontSize;
  1239. ctx.lineWidth = 1;
  1240.  
  1241. // Draw the origin
  1242. ctx.strokeStyle = 'black';
  1243. draw.line(ctx, x, -10000, x, 10000);
  1244. draw.line(ctx, -10000, y, 10000, y);
  1245.  
  1246. // This code is here due to memory optimization: by not using
  1247. // defaults in the constructor, we save a notable amount of memory.
  1248. var xMin = this.xMin || 0;
  1249. var yMin = this.yMin || 0;
  1250. var xMax = this.xMax || 0;
  1251. var yMax = this.yMax || 0;
  1252. var advanceWidth = this.advanceWidth || 0;
  1253.  
  1254. // Draw the glyph box
  1255. ctx.strokeStyle = 'blue';
  1256. draw.line(ctx, x + (xMin * scale), -10000, x + (xMin * scale), 10000);
  1257. draw.line(ctx, x + (xMax * scale), -10000, x + (xMax * scale), 10000);
  1258. draw.line(ctx, -10000, y + (-yMin * scale), 10000, y + (-yMin * scale));
  1259. draw.line(ctx, -10000, y + (-yMax * scale), 10000, y + (-yMax * scale));
  1260.  
  1261. // Draw the advance width
  1262. ctx.strokeStyle = 'green';
  1263. draw.line(ctx, x + (advanceWidth * scale), -10000, x + (advanceWidth * scale), 10000);
  1264. };
  1265.  
  1266. exports.Glyph = Glyph;
  1267.  
  1268. },{"./check":2,"./draw":3,"./path":10}],7:[function(require,module,exports){
  1269. // The GlyphSet object
  1270.  
  1271. 'use strict';
  1272.  
  1273. var _glyph = require('./glyph');
  1274.  
  1275. // A GlyphSet represents all glyphs available in the font, but modelled using
  1276. // a deferred glyph loader, for retrieving glyphs only once they are absolutely
  1277. // necessary, to keep the memory footprint down.
  1278. function GlyphSet(font, glyphs) {
  1279. this.font = font;
  1280. this.glyphs = {};
  1281. if (Array.isArray(glyphs)) {
  1282. for (var i = 0; i < glyphs.length; i++) {
  1283. this.glyphs[i] = glyphs[i];
  1284. }
  1285. }
  1286.  
  1287. this.length = (glyphs && glyphs.length) || 0;
  1288. }
  1289.  
  1290. GlyphSet.prototype.get = function(index) {
  1291. if (typeof this.glyphs[index] === 'function') {
  1292. this.glyphs[index] = this.glyphs[index]();
  1293. }
  1294.  
  1295. return this.glyphs[index];
  1296. };
  1297.  
  1298. GlyphSet.prototype.push = function(index, loader) {
  1299. this.glyphs[index] = loader;
  1300. this.length++;
  1301. };
  1302.  
  1303. function glyphLoader(font, index) {
  1304. return new _glyph.Glyph({index: index, font: font});
  1305. }
  1306.  
  1307. /**
  1308. * Generate a stub glyph that can be filled with all metadata *except*
  1309. * the "points" and "path" properties, which must be loaded only once
  1310. * the glyph's path is actually requested for text shaping.
  1311. */
  1312.  
  1313. function ttfGlyphLoader(font, index, parseGlyph, data, position, buildPath) {
  1314. return function() {
  1315. var glyph = new _glyph.Glyph({index: index, font: font});
  1316.  
  1317. glyph.path = function() {
  1318. parseGlyph(glyph, data, position);
  1319. var path = buildPath(font.glyphs, glyph);
  1320. path.unitsPerEm = font.unitsPerEm;
  1321. return path;
  1322. };
  1323.  
  1324. return glyph;
  1325. };
  1326. }
  1327.  
  1328. function cffGlyphLoader(font, index, parseCFFCharstring, charstring) {
  1329. return function() {
  1330. var glyph = new _glyph.Glyph({index: index, font: font});
  1331.  
  1332. glyph.path = function() {
  1333. var path = parseCFFCharstring(font, glyph, charstring);
  1334. path.unitsPerEm = font.unitsPerEm;
  1335. return path;
  1336. };
  1337.  
  1338. return glyph;
  1339. };
  1340. }
  1341.  
  1342. exports.GlyphSet = GlyphSet;
  1343. exports.glyphLoader = glyphLoader;
  1344. exports.ttfGlyphLoader = ttfGlyphLoader;
  1345. exports.cffGlyphLoader = cffGlyphLoader;
  1346.  
  1347. },{"./glyph":6}],8:[function(require,module,exports){
  1348. // opentype.js
  1349. // https://github.com/nodebox/opentype.js
  1350. // (c) 2015 Frederik De Bleser
  1351. // opentype.js may be freely distributed under the MIT license.
  1352.  
  1353. /* global DataView, Uint8Array, XMLHttpRequest */
  1354.  
  1355. 'use strict';
  1356.  
  1357. var inflate = require('tiny-inflate');
  1358.  
  1359. var encoding = require('./encoding');
  1360. var _font = require('./font');
  1361. var glyph = require('./glyph');
  1362. var parse = require('./parse');
  1363. var path = require('./path');
  1364. var util = require('./util');
  1365.  
  1366. var cmap = require('./tables/cmap');
  1367. var cff = require('./tables/cff');
  1368. var fvar = require('./tables/fvar');
  1369. var glyf = require('./tables/glyf');
  1370. var gpos = require('./tables/gpos');
  1371. var head = require('./tables/head');
  1372. var hhea = require('./tables/hhea');
  1373. var hmtx = require('./tables/hmtx');
  1374. var kern = require('./tables/kern');
  1375. var ltag = require('./tables/ltag');
  1376. var loca = require('./tables/loca');
  1377. var maxp = require('./tables/maxp');
  1378. var _name = require('./tables/name');
  1379. var os2 = require('./tables/os2');
  1380. var post = require('./tables/post');
  1381.  
  1382. // File loaders /////////////////////////////////////////////////////////
  1383.  
  1384. function loadFromFile(path, callback) {
  1385. var fs = require('fs');
  1386. fs.readFile(path, function(err, buffer) {
  1387. if (err) {
  1388. return callback(err.message);
  1389. }
  1390.  
  1391. callback(null, util.nodeBufferToArrayBuffer(buffer));
  1392. });
  1393. }
  1394.  
  1395. function loadFromUrl(url, callback) {
  1396. var request = new XMLHttpRequest();
  1397. request.open('get', url, true);
  1398. request.responseType = 'arraybuffer';
  1399. request.onload = function() {
  1400. if (request.status !== 200) {
  1401. return callback('Font could not be loaded: ' + request.statusText);
  1402. }
  1403.  
  1404. return callback(null, request.response);
  1405. };
  1406.  
  1407. request.send();
  1408. }
  1409.  
  1410. // Table Directory Entries //////////////////////////////////////////////
  1411.  
  1412. function parseOpenTypeTableEntries(data, numTables) {
  1413. var tableEntries = [];
  1414. var p = 12;
  1415. for (var i = 0; i < numTables; i += 1) {
  1416. var tag = parse.getTag(data, p);
  1417. var offset = parse.getULong(data, p + 8);
  1418. tableEntries.push({tag: tag, offset: offset, compression: false});
  1419. p += 16;
  1420. }
  1421.  
  1422. return tableEntries;
  1423. }
  1424.  
  1425. function parseWOFFTableEntries(data, numTables) {
  1426. var tableEntries = [];
  1427. var p = 44; // offset to the first table directory entry.
  1428. for (var i = 0; i < numTables; i += 1) {
  1429. var tag = parse.getTag(data, p);
  1430. var offset = parse.getULong(data, p + 4);
  1431. var compLength = parse.getULong(data, p + 8);
  1432. var origLength = parse.getULong(data, p + 12);
  1433. var compression;
  1434. if (compLength < origLength) {
  1435. compression = 'WOFF';
  1436. } else {
  1437. compression = false;
  1438. }
  1439.  
  1440. tableEntries.push({tag: tag, offset: offset, compression: compression,
  1441. compressedLength: compLength, originalLength: origLength});
  1442. p += 20;
  1443. }
  1444.  
  1445. return tableEntries;
  1446. }
  1447.  
  1448. function uncompressTable(data, tableEntry) {
  1449. if (tableEntry.compression === 'WOFF') {
  1450. var inBuffer = new Uint8Array(data.buffer, tableEntry.offset + 2, tableEntry.compressedLength - 2);
  1451. var outBuffer = new Uint8Array(tableEntry.originalLength);
  1452. inflate(inBuffer, outBuffer);
  1453. if (outBuffer.byteLength !== tableEntry.originalLength) {
  1454. throw new Error('Decompression error: ' + tableEntry.tag + ' decompressed length doesn\'t match recorded length');
  1455. }
  1456.  
  1457. var view = new DataView(outBuffer.buffer, 0);
  1458. return {data: view, offset: 0};
  1459. } else {
  1460. return {data: data, offset: tableEntry.offset};
  1461. }
  1462. }
  1463.  
  1464. // Public API ///////////////////////////////////////////////////////////
  1465.  
  1466. // Parse the OpenType file data (as an ArrayBuffer) and return a Font object.
  1467. // Throws an error if the font could not be parsed.
  1468. function parseBuffer(buffer) {
  1469. var indexToLocFormat;
  1470. var ltagTable;
  1471.  
  1472. // Since the constructor can also be called to create new fonts from scratch, we indicate this
  1473. // should be an empty font that we'll fill with our own data.
  1474. var font = new _font.Font({empty: true});
  1475.  
  1476. // OpenType fonts use big endian byte ordering.
  1477. // We can't rely on typed array view types, because they operate with the endianness of the host computer.
  1478. // Instead we use DataViews where we can specify endianness.
  1479. var data = new DataView(buffer, 0);
  1480. var numTables;
  1481. var tableEntries = [];
  1482. var signature = parse.getTag(data, 0);
  1483. if (signature === String.fromCharCode(0, 1, 0, 0)) {
  1484. font.outlinesFormat = 'truetype';
  1485. numTables = parse.getUShort(data, 4);
  1486. tableEntries = parseOpenTypeTableEntries(data, numTables);
  1487. } else if (signature === 'OTTO') {
  1488. font.outlinesFormat = 'cff';
  1489. numTables = parse.getUShort(data, 4);
  1490. tableEntries = parseOpenTypeTableEntries(data, numTables);
  1491. } else if (signature === 'wOFF') {
  1492. var flavor = parse.getTag(data, 4);
  1493. if (flavor === String.fromCharCode(0, 1, 0, 0)) {
  1494. font.outlinesFormat = 'truetype';
  1495. } else if (flavor === 'OTTO') {
  1496. font.outlinesFormat = 'cff';
  1497. } else {
  1498. throw new Error('Unsupported OpenType flavor ' + signature);
  1499. }
  1500.  
  1501. numTables = parse.getUShort(data, 12);
  1502. tableEntries = parseWOFFTableEntries(data, numTables);
  1503. } else {
  1504. throw new Error('Unsupported OpenType signature ' + signature);
  1505. }
  1506.  
  1507. var cffTableEntry;
  1508. var fvarTableEntry;
  1509. var glyfTableEntry;
  1510. var gposTableEntry;
  1511. var hmtxTableEntry;
  1512. var kernTableEntry;
  1513. var locaTableEntry;
  1514. var nameTableEntry;
  1515.  
  1516. for (var i = 0; i < numTables; i += 1) {
  1517. var tableEntry = tableEntries[i];
  1518. var table;
  1519. switch (tableEntry.tag) {
  1520. case 'cmap':
  1521. table = uncompressTable(data, tableEntry);
  1522. font.tables.cmap = cmap.parse(table.data, table.offset);
  1523. font.encoding = new encoding.CmapEncoding(font.tables.cmap);
  1524. break;
  1525. case 'fvar':
  1526. fvarTableEntry = tableEntry;
  1527. break;
  1528. case 'head':
  1529. table = uncompressTable(data, tableEntry);
  1530. font.tables.head = head.parse(table.data, table.offset);
  1531. font.unitsPerEm = font.tables.head.unitsPerEm;
  1532. indexToLocFormat = font.tables.head.indexToLocFormat;
  1533. break;
  1534. case 'hhea':
  1535. table = uncompressTable(data, tableEntry);
  1536. font.tables.hhea = hhea.parse(table.data, table.offset);
  1537. font.ascender = font.tables.hhea.ascender;
  1538. font.descender = font.tables.hhea.descender;
  1539. font.numberOfHMetrics = font.tables.hhea.numberOfHMetrics;
  1540. break;
  1541. case 'hmtx':
  1542. hmtxTableEntry = tableEntry;
  1543. break;
  1544. case 'ltag':
  1545. table = uncompressTable(data, tableEntry);
  1546. ltagTable = ltag.parse(table.data, table.offset);
  1547. break;
  1548. case 'maxp':
  1549. table = uncompressTable(data, tableEntry);
  1550. font.tables.maxp = maxp.parse(table.data, table.offset);
  1551. font.numGlyphs = font.tables.maxp.numGlyphs;
  1552. break;
  1553. case 'name':
  1554. nameTableEntry = tableEntry;
  1555. break;
  1556. case 'OS/2':
  1557. table = uncompressTable(data, tableEntry);
  1558. font.tables.os2 = os2.parse(table.data, table.offset);
  1559. break;
  1560. case 'post':
  1561. table = uncompressTable(data, tableEntry);
  1562. font.tables.post = post.parse(table.data, table.offset);
  1563. font.glyphNames = new encoding.GlyphNames(font.tables.post);
  1564. break;
  1565. case 'glyf':
  1566. glyfTableEntry = tableEntry;
  1567. break;
  1568. case 'loca':
  1569. locaTableEntry = tableEntry;
  1570. break;
  1571. case 'CFF ':
  1572. cffTableEntry = tableEntry;
  1573. break;
  1574. case 'kern':
  1575. kernTableEntry = tableEntry;
  1576. break;
  1577. case 'GPOS':
  1578. gposTableEntry = tableEntry;
  1579. break;
  1580. }
  1581. }
  1582.  
  1583. var nameTable = uncompressTable(data, nameTableEntry);
  1584. font.tables.name = _name.parse(nameTable.data, nameTable.offset, ltagTable);
  1585. font.names = font.tables.name;
  1586.  
  1587. if (glyfTableEntry && locaTableEntry) {
  1588. var shortVersion = indexToLocFormat === 0;
  1589. var locaTable = uncompressTable(data, locaTableEntry);
  1590. var locaOffsets = loca.parse(locaTable.data, locaTable.offset, font.numGlyphs, shortVersion);
  1591. var glyfTable = uncompressTable(data, glyfTableEntry);
  1592. font.glyphs = glyf.parse(glyfTable.data, glyfTable.offset, locaOffsets, font);
  1593. } else if (cffTableEntry) {
  1594. var cffTable = uncompressTable(data, cffTableEntry);
  1595. cff.parse(cffTable.data, cffTable.offset, font);
  1596. } else {
  1597. throw new Error('Font doesn\'t contain TrueType or CFF outlines.');
  1598. }
  1599.  
  1600. var hmtxTable = uncompressTable(data, hmtxTableEntry);
  1601. hmtx.parse(hmtxTable.data, hmtxTable.offset, font.numberOfHMetrics, font.numGlyphs, font.glyphs);
  1602. encoding.addGlyphNames(font);
  1603.  
  1604. if (kernTableEntry) {
  1605. var kernTable = uncompressTable(data, kernTableEntry);
  1606. font.kerningPairs = kern.parse(kernTable.data, kernTable.offset);
  1607. } else {
  1608. font.kerningPairs = {};
  1609. }
  1610.  
  1611. if (gposTableEntry) {
  1612. var gposTable = uncompressTable(data, gposTableEntry);
  1613. gpos.parse(gposTable.data, gposTable.offset, font);
  1614. }
  1615.  
  1616. if (fvarTableEntry) {
  1617. var fvarTable = uncompressTable(data, fvarTableEntry);
  1618. font.tables.fvar = fvar.parse(fvarTable.data, fvarTable.offset, font.names);
  1619. }
  1620.  
  1621. return font;
  1622. }
  1623.  
  1624. // Asynchronously load the font from a URL or a filesystem. When done, call the callback
  1625. // with two arguments `(err, font)`. The `err` will be null on success,
  1626. // the `font` is a Font object.
  1627. //
  1628. // We use the node.js callback convention so that
  1629. // opentype.js can integrate with frameworks like async.js.
  1630. function load(url, callback) {
  1631. var isNode = typeof window === 'undefined';
  1632. var loadFn = isNode ? loadFromFile : loadFromUrl;
  1633. loadFn(url, function(err, arrayBuffer) {
  1634. if (err) {
  1635. return callback(err);
  1636. }
  1637.  
  1638. var font = parseBuffer(arrayBuffer);
  1639. return callback(null, font);
  1640. });
  1641. }
  1642.  
  1643. // Syncronously load the font from a URL or file.
  1644. // When done, return the font object or throw an error.
  1645. function loadSync(url) {
  1646. var fs = require('fs');
  1647. var buffer = fs.readFileSync(url);
  1648. return parseBuffer(util.nodeBufferToArrayBuffer(buffer));
  1649. }
  1650.  
  1651. exports._parse = parse;
  1652. exports.Font = _font.Font;
  1653. exports.Glyph = glyph.Glyph;
  1654. exports.Path = path.Path;
  1655. exports.parse = parseBuffer;
  1656. exports.load = load;
  1657. exports.loadSync = loadSync;
  1658.  
  1659. },{"./encoding":4,"./font":5,"./glyph":6,"./parse":9,"./path":10,"./tables/cff":12,"./tables/cmap":13,"./tables/fvar":14,"./tables/glyf":15,"./tables/gpos":16,"./tables/head":17,"./tables/hhea":18,"./tables/hmtx":19,"./tables/kern":20,"./tables/loca":21,"./tables/ltag":22,"./tables/maxp":23,"./tables/name":24,"./tables/os2":25,"./tables/post":26,"./util":29,"fs":undefined,"tiny-inflate":1}],9:[function(require,module,exports){
  1660. // Parsing utility functions
  1661.  
  1662. 'use strict';
  1663.  
  1664. // Retrieve an unsigned byte from the DataView.
  1665. exports.getByte = function getByte(dataView, offset) {
  1666. return dataView.getUint8(offset);
  1667. };
  1668.  
  1669. exports.getCard8 = exports.getByte;
  1670.  
  1671. // Retrieve an unsigned 16-bit short from the DataView.
  1672. // The value is stored in big endian.
  1673. exports.getUShort = function(dataView, offset) {
  1674. return dataView.getUint16(offset, false);
  1675. };
  1676.  
  1677. exports.getCard16 = exports.getUShort;
  1678.  
  1679. // Retrieve a signed 16-bit short from the DataView.
  1680. // The value is stored in big endian.
  1681. exports.getShort = function(dataView, offset) {
  1682. return dataView.getInt16(offset, false);
  1683. };
  1684.  
  1685. // Retrieve an unsigned 32-bit long from the DataView.
  1686. // The value is stored in big endian.
  1687. exports.getULong = function(dataView, offset) {
  1688. return dataView.getUint32(offset, false);
  1689. };
  1690.  
  1691. // Retrieve a 32-bit signed fixed-point number (16.16) from the DataView.
  1692. // The value is stored in big endian.
  1693. exports.getFixed = function(dataView, offset) {
  1694. var decimal = dataView.getInt16(offset, false);
  1695. var fraction = dataView.getUint16(offset + 2, false);
  1696. return decimal + fraction / 65535;
  1697. };
  1698.  
  1699. // Retrieve a 4-character tag from the DataView.
  1700. // Tags are used to identify tables.
  1701. exports.getTag = function(dataView, offset) {
  1702. var tag = '';
  1703. for (var i = offset; i < offset + 4; i += 1) {
  1704. tag += String.fromCharCode(dataView.getInt8(i));
  1705. }
  1706.  
  1707. return tag;
  1708. };
  1709.  
  1710. // Retrieve an offset from the DataView.
  1711. // Offsets are 1 to 4 bytes in length, depending on the offSize argument.
  1712. exports.getOffset = function(dataView, offset, offSize) {
  1713. var v = 0;
  1714. for (var i = 0; i < offSize; i += 1) {
  1715. v <<= 8;
  1716. v += dataView.getUint8(offset + i);
  1717. }
  1718.  
  1719. return v;
  1720. };
  1721.  
  1722. // Retrieve a number of bytes from start offset to the end offset from the DataView.
  1723. exports.getBytes = function(dataView, startOffset, endOffset) {
  1724. var bytes = [];
  1725. for (var i = startOffset; i < endOffset; i += 1) {
  1726. bytes.push(dataView.getUint8(i));
  1727. }
  1728.  
  1729. return bytes;
  1730. };
  1731.  
  1732. // Convert the list of bytes to a string.
  1733. exports.bytesToString = function(bytes) {
  1734. var s = '';
  1735. for (var i = 0; i < bytes.length; i += 1) {
  1736. s += String.fromCharCode(bytes[i]);
  1737. }
  1738.  
  1739. return s;
  1740. };
  1741.  
  1742. var typeOffsets = {
  1743. byte: 1,
  1744. uShort: 2,
  1745. short: 2,
  1746. uLong: 4,
  1747. fixed: 4,
  1748. longDateTime: 8,
  1749. tag: 4
  1750. };
  1751.  
  1752. // A stateful parser that changes the offset whenever a value is retrieved.
  1753. // The data is a DataView.
  1754. function Parser(data, offset) {
  1755. this.data = data;
  1756. this.offset = offset;
  1757. this.relativeOffset = 0;
  1758. }
  1759.  
  1760. Parser.prototype.parseByte = function() {
  1761. var v = this.data.getUint8(this.offset + this.relativeOffset);
  1762. this.relativeOffset += 1;
  1763. return v;
  1764. };
  1765.  
  1766. Parser.prototype.parseChar = function() {
  1767. var v = this.data.getInt8(this.offset + this.relativeOffset);
  1768. this.relativeOffset += 1;
  1769. return v;
  1770. };
  1771.  
  1772. Parser.prototype.parseCard8 = Parser.prototype.parseByte;
  1773.  
  1774. Parser.prototype.parseUShort = function() {
  1775. var v = this.data.getUint16(this.offset + this.relativeOffset);
  1776. this.relativeOffset += 2;
  1777. return v;
  1778. };
  1779.  
  1780. Parser.prototype.parseCard16 = Parser.prototype.parseUShort;
  1781. Parser.prototype.parseSID = Parser.prototype.parseUShort;
  1782. Parser.prototype.parseOffset16 = Parser.prototype.parseUShort;
  1783.  
  1784. Parser.prototype.parseShort = function() {
  1785. var v = this.data.getInt16(this.offset + this.relativeOffset);
  1786. this.relativeOffset += 2;
  1787. return v;
  1788. };
  1789.  
  1790. Parser.prototype.parseF2Dot14 = function() {
  1791. var v = this.data.getInt16(this.offset + this.relativeOffset) / 16384;
  1792. this.relativeOffset += 2;
  1793. return v;
  1794. };
  1795.  
  1796. Parser.prototype.parseULong = function() {
  1797. var v = exports.getULong(this.data, this.offset + this.relativeOffset);
  1798. this.relativeOffset += 4;
  1799. return v;
  1800. };
  1801.  
  1802. Parser.prototype.parseFixed = function() {
  1803. var v = exports.getFixed(this.data, this.offset + this.relativeOffset);
  1804. this.relativeOffset += 4;
  1805. return v;
  1806. };
  1807.  
  1808. Parser.prototype.parseOffset16List =
  1809. Parser.prototype.parseUShortList = function(count) {
  1810. var offsets = new Array(count);
  1811. var dataView = this.data;
  1812. var offset = this.offset + this.relativeOffset;
  1813. for (var i = 0; i < count; i++) {
  1814. offsets[i] = exports.getUShort(dataView, offset);
  1815. offset += 2;
  1816. }
  1817.  
  1818. this.relativeOffset += count * 2;
  1819. return offsets;
  1820. };
  1821.  
  1822. Parser.prototype.parseString = function(length) {
  1823. var dataView = this.data;
  1824. var offset = this.offset + this.relativeOffset;
  1825. var string = '';
  1826. this.relativeOffset += length;
  1827. for (var i = 0; i < length; i++) {
  1828. string += String.fromCharCode(dataView.getUint8(offset + i));
  1829. }
  1830.  
  1831. return string;
  1832. };
  1833.  
  1834. Parser.prototype.parseTag = function() {
  1835. return this.parseString(4);
  1836. };
  1837.  
  1838. // LONGDATETIME is a 64-bit integer.
  1839. // JavaScript and unix timestamps traditionally use 32 bits, so we
  1840. // only take the last 32 bits.
  1841. Parser.prototype.parseLongDateTime = function() {
  1842. var v = exports.getULong(this.data, this.offset + this.relativeOffset + 4);
  1843. this.relativeOffset += 8;
  1844. return v;
  1845. };
  1846.  
  1847. Parser.prototype.parseFixed = function() {
  1848. var v = exports.getULong(this.data, this.offset + this.relativeOffset);
  1849. this.relativeOffset += 4;
  1850. return v / 65536;
  1851. };
  1852.  
  1853. Parser.prototype.parseVersion = function() {
  1854. var major = exports.getUShort(this.data, this.offset + this.relativeOffset);
  1855.  
  1856. // How to interpret the minor version is very vague in the spec. 0x5000 is 5, 0x1000 is 1
  1857. // This returns the correct number if minor = 0xN000 where N is 0-9
  1858. var minor = exports.getUShort(this.data, this.offset + this.relativeOffset + 2);
  1859. this.relativeOffset += 4;
  1860. return major + minor / 0x1000 / 10;
  1861. };
  1862.  
  1863. Parser.prototype.skip = function(type, amount) {
  1864. if (amount === undefined) {
  1865. amount = 1;
  1866. }
  1867.  
  1868. this.relativeOffset += typeOffsets[type] * amount;
  1869. };
  1870.  
  1871. exports.Parser = Parser;
  1872.  
  1873. },{}],10:[function(require,module,exports){
  1874. // Geometric objects
  1875.  
  1876. 'use strict';
  1877.  
  1878. // A bézier path containing a set of path commands similar to a SVG path.
  1879. // Paths can be drawn on a context using `draw`.
  1880. function Path() {
  1881. this.commands = [];
  1882. this.fill = 'black';
  1883. this.stroke = null;
  1884. this.strokeWidth = 1;
  1885. }
  1886.  
  1887. Path.prototype.moveTo = function(x, y) {
  1888. this.commands.push({
  1889. type: 'M',
  1890. x: x,
  1891. y: y
  1892. });
  1893. };
  1894.  
  1895. Path.prototype.lineTo = function(x, y) {
  1896. this.commands.push({
  1897. type: 'L',
  1898. x: x,
  1899. y: y
  1900. });
  1901. };
  1902.  
  1903. Path.prototype.curveTo = Path.prototype.bezierCurveTo = function(x1, y1, x2, y2, x, y) {
  1904. this.commands.push({
  1905. type: 'C',
  1906. x1: x1,
  1907. y1: y1,
  1908. x2: x2,
  1909. y2: y2,
  1910. x: x,
  1911. y: y
  1912. });
  1913. };
  1914.  
  1915. Path.prototype.quadTo = Path.prototype.quadraticCurveTo = function(x1, y1, x, y) {
  1916. this.commands.push({
  1917. type: 'Q',
  1918. x1: x1,
  1919. y1: y1,
  1920. x: x,
  1921. y: y
  1922. });
  1923. };
  1924.  
  1925. Path.prototype.close = Path.prototype.closePath = function() {
  1926. this.commands.push({
  1927. type: 'Z'
  1928. });
  1929. };
  1930.  
  1931. // Add the given path or list of commands to the commands of this path.
  1932. Path.prototype.extend = function(pathOrCommands) {
  1933. if (pathOrCommands.commands) {
  1934. pathOrCommands = pathOrCommands.commands;
  1935. }
  1936.  
  1937. Array.prototype.push.apply(this.commands, pathOrCommands);
  1938. };
  1939.  
  1940. // Draw the path to a 2D context.
  1941. Path.prototype.draw = function(ctx) {
  1942. ctx.beginPath();
  1943. for (var i = 0; i < this.commands.length; i += 1) {
  1944. var cmd = this.commands[i];
  1945. if (cmd.type === 'M') {
  1946. ctx.moveTo(cmd.x, cmd.y);
  1947. } else if (cmd.type === 'L') {
  1948. ctx.lineTo(cmd.x, cmd.y);
  1949. } else if (cmd.type === 'C') {
  1950. ctx.bezierCurveTo(cmd.x1, cmd.y1, cmd.x2, cmd.y2, cmd.x, cmd.y);
  1951. } else if (cmd.type === 'Q') {
  1952. ctx.quadraticCurveTo(cmd.x1, cmd.y1, cmd.x, cmd.y);
  1953. } else if (cmd.type === 'Z') {
  1954. ctx.closePath();
  1955. }
  1956. }
  1957.  
  1958. if (this.fill) {
  1959. ctx.fillStyle = this.fill;
  1960. ctx.fill();
  1961. }
  1962.  
  1963. if (this.stroke) {
  1964. ctx.strokeStyle = this.stroke;
  1965. ctx.lineWidth = this.strokeWidth;
  1966. ctx.stroke();
  1967. }
  1968. };
  1969.  
  1970. // Convert the Path to a string of path data instructions
  1971. // See http://www.w3.org/TR/SVG/paths.html#PathData
  1972. // Parameters:
  1973. // - decimalPlaces: The amount of decimal places for floating-point values (default: 2)
  1974. Path.prototype.toPathData = function(decimalPlaces) {
  1975. decimalPlaces = decimalPlaces !== undefined ? decimalPlaces : 2;
  1976.  
  1977. function floatToString(v) {
  1978. if (Math.round(v) === v) {
  1979. return '' + Math.round(v);
  1980. } else {
  1981. return v.toFixed(decimalPlaces);
  1982. }
  1983. }
  1984.  
  1985. function packValues() {
  1986. var s = '';
  1987. for (var i = 0; i < arguments.length; i += 1) {
  1988. var v = arguments[i];
  1989. if (v >= 0 && i > 0) {
  1990. s += ' ';
  1991. }
  1992.  
  1993. s += floatToString(v);
  1994. }
  1995.  
  1996. return s;
  1997. }
  1998.  
  1999. var d = '';
  2000. for (var i = 0; i < this.commands.length; i += 1) {
  2001. var cmd = this.commands[i];
  2002. if (cmd.type === 'M') {
  2003. d += 'M' + packValues(cmd.x, cmd.y);
  2004. } else if (cmd.type === 'L') {
  2005. d += 'L' + packValues(cmd.x, cmd.y);
  2006. } else if (cmd.type === 'C') {
  2007. d += 'C' + packValues(cmd.x1, cmd.y1, cmd.x2, cmd.y2, cmd.x, cmd.y);
  2008. } else if (cmd.type === 'Q') {
  2009. d += 'Q' + packValues(cmd.x1, cmd.y1, cmd.x, cmd.y);
  2010. } else if (cmd.type === 'Z') {
  2011. d += 'Z';
  2012. }
  2013. }
  2014.  
  2015. return d;
  2016. };
  2017.  
  2018. // Convert the path to a SVG <path> element, as a string.
  2019. // Parameters:
  2020. // - decimalPlaces: The amount of decimal places for floating-point values (default: 2)
  2021. Path.prototype.toSVG = function(decimalPlaces) {
  2022. var svg = '<path d="';
  2023. svg += this.toPathData(decimalPlaces);
  2024. svg += '"';
  2025. if (this.fill & this.fill !== 'black') {
  2026. if (this.fill === null) {
  2027. svg += ' fill="none"';
  2028. } else {
  2029. svg += ' fill="' + this.fill + '"';
  2030. }
  2031. }
  2032.  
  2033. if (this.stroke) {
  2034. svg += ' stroke="' + this.stroke + '" stroke-width="' + this.strokeWidth + '"';
  2035. }
  2036.  
  2037. svg += '/>';
  2038. return svg;
  2039. };
  2040.  
  2041. exports.Path = Path;
  2042.  
  2043. },{}],11:[function(require,module,exports){
  2044. // Table metadata
  2045.  
  2046. 'use strict';
  2047.  
  2048. var check = require('./check');
  2049. var encode = require('./types').encode;
  2050. var sizeOf = require('./types').sizeOf;
  2051.  
  2052. function Table(tableName, fields, options) {
  2053. var i;
  2054. for (i = 0; i < fields.length; i += 1) {
  2055. var field = fields[i];
  2056. this[field.name] = field.value;
  2057. }
  2058.  
  2059. this.tableName = tableName;
  2060. this.fields = fields;
  2061. if (options) {
  2062. var optionKeys = Object.keys(options);
  2063. for (i = 0; i < optionKeys.length; i += 1) {
  2064. var k = optionKeys[i];
  2065. var v = options[k];
  2066. if (this[k] !== undefined) {
  2067. this[k] = v;
  2068. }
  2069. }
  2070. }
  2071. }
  2072.  
  2073. Table.prototype.sizeOf = function() {
  2074. var v = 0;
  2075. for (var i = 0; i < this.fields.length; i += 1) {
  2076. var field = this.fields[i];
  2077. var value = this[field.name];
  2078. if (value === undefined) {
  2079. value = field.value;
  2080. }
  2081.  
  2082. if (typeof value.sizeOf === 'function') {
  2083. v += value.sizeOf();
  2084. } else {
  2085. var sizeOfFunction = sizeOf[field.type];
  2086. check.assert(typeof sizeOfFunction === 'function', 'Could not find sizeOf function for field' + field.name);
  2087. v += sizeOfFunction(value);
  2088. }
  2089. }
  2090.  
  2091. return v;
  2092. };
  2093.  
  2094. Table.prototype.encode = function() {
  2095. return encode.TABLE(this);
  2096. };
  2097.  
  2098. exports.Table = Table;
  2099.  
  2100. },{"./check":2,"./types":28}],12:[function(require,module,exports){
  2101. // The `CFF` table contains the glyph outlines in PostScript format.
  2102. // https://www.microsoft.com/typography/OTSPEC/cff.htm
  2103. // http://download.microsoft.com/download/8/0/1/801a191c-029d-4af3-9642-555f6fe514ee/cff.pdf
  2104. // http://download.microsoft.com/download/8/0/1/801a191c-029d-4af3-9642-555f6fe514ee/type2.pdf
  2105.  
  2106. 'use strict';
  2107.  
  2108. var encoding = require('../encoding');
  2109. var glyphset = require('../glyphset');
  2110. var parse = require('../parse');
  2111. var path = require('../path');
  2112. var table = require('../table');
  2113.  
  2114. // Custom equals function that can also check lists.
  2115. function equals(a, b) {
  2116. if (a === b) {
  2117. return true;
  2118. } else if (Array.isArray(a) && Array.isArray(b)) {
  2119. if (a.length !== b.length) {
  2120. return false;
  2121. }
  2122.  
  2123. for (var i = 0; i < a.length; i += 1) {
  2124. if (!equals(a[i], b[i])) {
  2125. return false;
  2126. }
  2127. }
  2128.  
  2129. return true;
  2130. } else {
  2131. return false;
  2132. }
  2133. }
  2134.  
  2135. // Parse a `CFF` INDEX array.
  2136. // An index array consists of a list of offsets, then a list of objects at those offsets.
  2137. function parseCFFIndex(data, start, conversionFn) {
  2138. //var i, objectOffset, endOffset;
  2139. var offsets = [];
  2140. var objects = [];
  2141. var count = parse.getCard16(data, start);
  2142. var i;
  2143. var objectOffset;
  2144. var endOffset;
  2145. if (count !== 0) {
  2146. var offsetSize = parse.getByte(data, start + 2);
  2147. objectOffset = start + ((count + 1) * offsetSize) + 2;
  2148. var pos = start + 3;
  2149. for (i = 0; i < count + 1; i += 1) {
  2150. offsets.push(parse.getOffset(data, pos, offsetSize));
  2151. pos += offsetSize;
  2152. }
  2153.  
  2154. // The total size of the index array is 4 header bytes + the value of the last offset.
  2155. endOffset = objectOffset + offsets[count];
  2156. } else {
  2157. endOffset = start + 2;
  2158. }
  2159.  
  2160. for (i = 0; i < offsets.length - 1; i += 1) {
  2161. var value = parse.getBytes(data, objectOffset + offsets[i], objectOffset + offsets[i + 1]);
  2162. if (conversionFn) {
  2163. value = conversionFn(value);
  2164. }
  2165.  
  2166. objects.push(value);
  2167. }
  2168.  
  2169. return {objects: objects, startOffset: start, endOffset: endOffset};
  2170. }
  2171.  
  2172. // Parse a `CFF` DICT real value.
  2173. function parseFloatOperand(parser) {
  2174. var s = '';
  2175. var eof = 15;
  2176. var lookup = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', 'E', 'E-', null, '-'];
  2177. while (true) {
  2178. var b = parser.parseByte();
  2179. var n1 = b >> 4;
  2180. var n2 = b & 15;
  2181.  
  2182. if (n1 === eof) {
  2183. break;
  2184. }
  2185.  
  2186. s += lookup[n1];
  2187.  
  2188. if (n2 === eof) {
  2189. break;
  2190. }
  2191.  
  2192. s += lookup[n2];
  2193. }
  2194.  
  2195. return parseFloat(s);
  2196. }
  2197.  
  2198. // Parse a `CFF` DICT operand.
  2199. function parseOperand(parser, b0) {
  2200. var b1;
  2201. var b2;
  2202. var b3;
  2203. var b4;
  2204. if (b0 === 28) {
  2205. b1 = parser.parseByte();
  2206. b2 = parser.parseByte();
  2207. return b1 << 8 | b2;
  2208. }
  2209.  
  2210. if (b0 === 29) {
  2211. b1 = parser.parseByte();
  2212. b2 = parser.parseByte();
  2213. b3 = parser.parseByte();
  2214. b4 = parser.parseByte();
  2215. return b1 << 24 | b2 << 16 | b3 << 8 | b4;
  2216. }
  2217.  
  2218. if (b0 === 30) {
  2219. return parseFloatOperand(parser);
  2220. }
  2221.  
  2222. if (b0 >= 32 && b0 <= 246) {
  2223. return b0 - 139;
  2224. }
  2225.  
  2226. if (b0 >= 247 && b0 <= 250) {
  2227. b1 = parser.parseByte();
  2228. return (b0 - 247) * 256 + b1 + 108;
  2229. }
  2230.  
  2231. if (b0 >= 251 && b0 <= 254) {
  2232. b1 = parser.parseByte();
  2233. return -(b0 - 251) * 256 - b1 - 108;
  2234. }
  2235.  
  2236. throw new Error('Invalid b0 ' + b0);
  2237. }
  2238.  
  2239. // Convert the entries returned by `parseDict` to a proper dictionary.
  2240. // If a value is a list of one, it is unpacked.
  2241. function entriesToObject(entries) {
  2242. var o = {};
  2243. for (var i = 0; i < entries.length; i += 1) {
  2244. var key = entries[i][0];
  2245. var values = entries[i][1];
  2246. var value;
  2247. if (values.length === 1) {
  2248. value = values[0];
  2249. } else {
  2250. value = values;
  2251. }
  2252.  
  2253. if (o.hasOwnProperty(key)) {
  2254. throw new Error('Object ' + o + ' already has key ' + key);
  2255. }
  2256.  
  2257. o[key] = value;
  2258. }
  2259.  
  2260. return o;
  2261. }
  2262.  
  2263. // Parse a `CFF` DICT object.
  2264. // A dictionary contains key-value pairs in a compact tokenized format.
  2265. function parseCFFDict(data, start, size) {
  2266. start = start !== undefined ? start : 0;
  2267. var parser = new parse.Parser(data, start);
  2268. var entries = [];
  2269. var operands = [];
  2270. size = size !== undefined ? size : data.length;
  2271.  
  2272. while (parser.relativeOffset < size) {
  2273. var op = parser.parseByte();
  2274.  
  2275. // The first byte for each dict item distinguishes between operator (key) and operand (value).
  2276. // Values <= 21 are operators.
  2277. if (op <= 21) {
  2278. // Two-byte operators have an initial escape byte of 12.
  2279. if (op === 12) {
  2280. op = 1200 + parser.parseByte();
  2281. }
  2282.  
  2283. entries.push([op, operands]);
  2284. operands = [];
  2285. } else {
  2286. // Since the operands (values) come before the operators (keys), we store all operands in a list
  2287. // until we encounter an operator.
  2288. operands.push(parseOperand(parser, op));
  2289. }
  2290. }
  2291.  
  2292. return entriesToObject(entries);
  2293. }
  2294.  
  2295. // Given a String Index (SID), return the value of the string.
  2296. // Strings below index 392 are standard CFF strings and are not encoded in the font.
  2297. function getCFFString(strings, index) {
  2298. if (index <= 390) {
  2299. index = encoding.cffStandardStrings[index];
  2300. } else {
  2301. index = strings[index - 391];
  2302. }
  2303.  
  2304. return index;
  2305. }
  2306.  
  2307. // Interpret a dictionary and return a new dictionary with readable keys and values for missing entries.
  2308. // This function takes `meta` which is a list of objects containing `operand`, `name` and `default`.
  2309. function interpretDict(dict, meta, strings) {
  2310. var newDict = {};
  2311.  
  2312. // Because we also want to include missing values, we start out from the meta list
  2313. // and lookup values in the dict.
  2314. for (var i = 0; i < meta.length; i += 1) {
  2315. var m = meta[i];
  2316. var value = dict[m.op];
  2317. if (value === undefined) {
  2318. value = m.value !== undefined ? m.value : null;
  2319. }
  2320.  
  2321. if (m.type === 'SID') {
  2322. value = getCFFString(strings, value);
  2323. }
  2324.  
  2325. newDict[m.name] = value;
  2326. }
  2327.  
  2328. return newDict;
  2329. }
  2330.  
  2331. // Parse the CFF header.
  2332. function parseCFFHeader(data, start) {
  2333. var header = {};
  2334. header.formatMajor = parse.getCard8(data, start);
  2335. header.formatMinor = parse.getCard8(data, start + 1);
  2336. header.size = parse.getCard8(data, start + 2);
  2337. header.offsetSize = parse.getCard8(data, start + 3);
  2338. header.startOffset = start;
  2339. header.endOffset = start + 4;
  2340. return header;
  2341. }
  2342.  
  2343. var TOP_DICT_META = [
  2344. {name: 'version', op: 0, type: 'SID'},
  2345. {name: 'notice', op: 1, type: 'SID'},
  2346. {name: 'copyright', op: 1200, type: 'SID'},
  2347. {name: 'fullName', op: 2, type: 'SID'},
  2348. {name: 'familyName', op: 3, type: 'SID'},
  2349. {name: 'weight', op: 4, type: 'SID'},
  2350. {name: 'isFixedPitch', op: 1201, type: 'number', value: 0},
  2351. {name: 'italicAngle', op: 1202, type: 'number', value: 0},
  2352. {name: 'underlinePosition', op: 1203, type: 'number', value: -100},
  2353. {name: 'underlineThickness', op: 1204, type: 'number', value: 50},
  2354. {name: 'paintType', op: 1205, type: 'number', value: 0},
  2355. {name: 'charstringType', op: 1206, type: 'number', value: 2},
  2356. {name: 'fontMatrix', op: 1207, type: ['real', 'real', 'real', 'real', 'real', 'real'], value: [0.001, 0, 0, 0.001, 0, 0]},
  2357. {name: 'uniqueId', op: 13, type: 'number'},
  2358. {name: 'fontBBox', op: 5, type: ['number', 'number', 'number', 'number'], value: [0, 0, 0, 0]},
  2359. {name: 'strokeWidth', op: 1208, type: 'number', value: 0},
  2360. {name: 'xuid', op: 14, type: [], value: null},
  2361. {name: 'charset', op: 15, type: 'offset', value: 0},
  2362. {name: 'encoding', op: 16, type: 'offset', value: 0},
  2363. {name: 'charStrings', op: 17, type: 'offset', value: 0},
  2364. {name: 'private', op: 18, type: ['number', 'offset'], value: [0, 0]}
  2365. ];
  2366.  
  2367. var PRIVATE_DICT_META = [
  2368. {name: 'subrs', op: 19, type: 'offset', value: 0},
  2369. {name: 'defaultWidthX', op: 20, type: 'number', value: 0},
  2370. {name: 'nominalWidthX', op: 21, type: 'number', value: 0}
  2371. ];
  2372.  
  2373. // Parse the CFF top dictionary. A CFF table can contain multiple fonts, each with their own top dictionary.
  2374. // The top dictionary contains the essential metadata for the font, together with the private dictionary.
  2375. function parseCFFTopDict(data, strings) {
  2376. var dict = parseCFFDict(data, 0, data.byteLength);
  2377. return interpretDict(dict, TOP_DICT_META, strings);
  2378. }
  2379.  
  2380. // Parse the CFF private dictionary. We don't fully parse out all the values, only the ones we need.
  2381. function parseCFFPrivateDict(data, start, size, strings) {
  2382. var dict = parseCFFDict(data, start, size);
  2383. return interpretDict(dict, PRIVATE_DICT_META, strings);
  2384. }
  2385.  
  2386. // Parse the CFF charset table, which contains internal names for all the glyphs.
  2387. // This function will return a list of glyph names.
  2388. // See Adobe TN #5176 chapter 13, "Charsets".
  2389. function parseCFFCharset(data, start, nGlyphs, strings) {
  2390. var i;
  2391. var sid;
  2392. var count;
  2393. var parser = new parse.Parser(data, start);
  2394.  
  2395. // The .notdef glyph is not included, so subtract 1.
  2396. nGlyphs -= 1;
  2397. var charset = ['.notdef'];
  2398.  
  2399. var format = parser.parseCard8();
  2400. if (format === 0) {
  2401. for (i = 0; i < nGlyphs; i += 1) {
  2402. sid = parser.parseSID();
  2403. charset.push(getCFFString(strings, sid));
  2404. }
  2405. } else if (format === 1) {
  2406. while (charset.length <= nGlyphs) {
  2407. sid = parser.parseSID();
  2408. count = parser.parseCard8();
  2409. for (i = 0; i <= count; i += 1) {
  2410. charset.push(getCFFString(strings, sid));
  2411. sid += 1;
  2412. }
  2413. }
  2414. } else if (format === 2) {
  2415. while (charset.length <= nGlyphs) {
  2416. sid = parser.parseSID();
  2417. count = parser.parseCard16();
  2418. for (i = 0; i <= count; i += 1) {
  2419. charset.push(getCFFString(strings, sid));
  2420. sid += 1;
  2421. }
  2422. }
  2423. } else {
  2424. throw new Error('Unknown charset format ' + format);
  2425. }
  2426.  
  2427. return charset;
  2428. }
  2429.  
  2430. // Parse the CFF encoding data. Only one encoding can be specified per font.
  2431. // See Adobe TN #5176 chapter 12, "Encodings".
  2432. function parseCFFEncoding(data, start, charset) {
  2433. var i;
  2434. var code;
  2435. var enc = {};
  2436. var parser = new parse.Parser(data, start);
  2437. var format = parser.parseCard8();
  2438. if (format === 0) {
  2439. var nCodes = parser.parseCard8();
  2440. for (i = 0; i < nCodes; i += 1) {
  2441. code = parser.parseCard8();
  2442. enc[code] = i;
  2443. }
  2444. } else if (format === 1) {
  2445. var nRanges = parser.parseCard8();
  2446. code = 1;
  2447. for (i = 0; i < nRanges; i += 1) {
  2448. var first = parser.parseCard8();
  2449. var nLeft = parser.parseCard8();
  2450. for (var j = first; j <= first + nLeft; j += 1) {
  2451. enc[j] = code;
  2452. code += 1;
  2453. }
  2454. }
  2455. } else {
  2456. throw new Error('Unknown encoding format ' + format);
  2457. }
  2458.  
  2459. return new encoding.CffEncoding(enc, charset);
  2460. }
  2461.  
  2462. // Take in charstring code and return a Glyph object.
  2463. // The encoding is described in the Type 2 Charstring Format
  2464. // https://www.microsoft.com/typography/OTSPEC/charstr2.htm
  2465. function parseCFFCharstring(font, glyph, code) {
  2466. var c1x;
  2467. var c1y;
  2468. var c2x;
  2469. var c2y;
  2470. var p = new path.Path();
  2471. var stack = [];
  2472. var nStems = 0;
  2473. var haveWidth = false;
  2474. var width = font.defaultWidthX;
  2475. var open = false;
  2476. var x = 0;
  2477. var y = 0;
  2478.  
  2479. function newContour(x, y) {
  2480. if (open) {
  2481. p.closePath();
  2482. }
  2483.  
  2484. p.moveTo(x, y);
  2485. open = true;
  2486. }
  2487.  
  2488. function parseStems() {
  2489. var hasWidthArg;
  2490.  
  2491. // The number of stem operators on the stack is always even.
  2492. // If the value is uneven, that means a width is specified.
  2493. hasWidthArg = stack.length % 2 !== 0;
  2494. if (hasWidthArg && !haveWidth) {
  2495. width = stack.shift() + font.nominalWidthX;
  2496. }
  2497.  
  2498. nStems += stack.length >> 1;
  2499. stack.length = 0;
  2500. haveWidth = true;
  2501. }
  2502.  
  2503. function parse(code) {
  2504. var b1;
  2505. var b2;
  2506. var b3;
  2507. var b4;
  2508. var codeIndex;
  2509. var subrCode;
  2510. var jpx;
  2511. var jpy;
  2512. var c3x;
  2513. var c3y;
  2514. var c4x;
  2515. var c4y;
  2516.  
  2517. var i = 0;
  2518. while (i < code.length) {
  2519. var v = code[i];
  2520. i += 1;
  2521. switch (v) {
  2522. case 1: // hstem
  2523. parseStems();
  2524. break;
  2525. case 3: // vstem
  2526. parseStems();
  2527. break;
  2528. case 4: // vmoveto
  2529. if (stack.length > 1 && !haveWidth) {
  2530. width = stack.shift() + font.nominalWidthX;
  2531. haveWidth = true;
  2532. }
  2533.  
  2534. y += stack.pop();
  2535. newContour(x, y);
  2536. break;
  2537. case 5: // rlineto
  2538. while (stack.length > 0) {
  2539. x += stack.shift();
  2540. y += stack.shift();
  2541. p.lineTo(x, y);
  2542. }
  2543.  
  2544. break;
  2545. case 6: // hlineto
  2546. while (stack.length > 0) {
  2547. x += stack.shift();
  2548. p.lineTo(x, y);
  2549. if (stack.length === 0) {
  2550. break;
  2551. }
  2552.  
  2553. y += stack.shift();
  2554. p.lineTo(x, y);
  2555. }
  2556.  
  2557. break;
  2558. case 7: // vlineto
  2559. while (stack.length > 0) {
  2560. y += stack.shift();
  2561. p.lineTo(x, y);
  2562. if (stack.length === 0) {
  2563. break;
  2564. }
  2565.  
  2566. x += stack.shift();
  2567. p.lineTo(x, y);
  2568. }
  2569.  
  2570. break;
  2571. case 8: // rrcurveto
  2572. while (stack.length > 0) {
  2573. c1x = x + stack.shift();
  2574. c1y = y + stack.shift();
  2575. c2x = c1x + stack.shift();
  2576. c2y = c1y + stack.shift();
  2577. x = c2x + stack.shift();
  2578. y = c2y + stack.shift();
  2579. p.curveTo(c1x, c1y, c2x, c2y, x, y);
  2580. }
  2581.  
  2582. break;
  2583. case 10: // callsubr
  2584. codeIndex = stack.pop() + font.subrsBias;
  2585. subrCode = font.subrs[codeIndex];
  2586. if (subrCode) {
  2587. parse(subrCode);
  2588. }
  2589.  
  2590. break;
  2591. case 11: // return
  2592. return;
  2593. case 12: // flex operators
  2594. v = code[i];
  2595. i += 1;
  2596. switch (v) {
  2597. case 35: // flex
  2598. // |- dx1 dy1 dx2 dy2 dx3 dy3 dx4 dy4 dx5 dy5 dx6 dy6 fd flex (12 35) |-
  2599. c1x = x + stack.shift(); // dx1
  2600. c1y = y + stack.shift(); // dy1
  2601. c2x = c1x + stack.shift(); // dx2
  2602. c2y = c1y + stack.shift(); // dy2
  2603. jpx = c2x + stack.shift(); // dx3
  2604. jpy = c2y + stack.shift(); // dy3
  2605. c3x = jpx + stack.shift(); // dx4
  2606. c3y = jpy + stack.shift(); // dy4
  2607. c4x = c3x + stack.shift(); // dx5
  2608. c4y = c3y + stack.shift(); // dy5
  2609. x = c4x + stack.shift(); // dx6
  2610. y = c4y + stack.shift(); // dy6
  2611. stack.shift(); // flex depth
  2612. p.curveTo(c1x, c1y, c2x, c2y, jpx, jpy);
  2613. p.curveTo(c3x, c3y, c4x, c4y, x, y);
  2614. break;
  2615. case 34: // hflex
  2616. // |- dx1 dx2 dy2 dx3 dx4 dx5 dx6 hflex (12 34) |-
  2617. c1x = x + stack.shift(); // dx1
  2618. c1y = y; // dy1
  2619. c2x = c1x + stack.shift(); // dx2
  2620. c2y = c1y + stack.shift(); // dy2
  2621. jpx = c2x + stack.shift(); // dx3
  2622. jpy = c2y; // dy3
  2623. c3x = jpx + stack.shift(); // dx4
  2624. c3y = c2y; // dy4
  2625. c4x = c3x + stack.shift(); // dx5
  2626. c4y = y; // dy5
  2627. x = c4x + stack.shift(); // dx6
  2628. p.curveTo(c1x, c1y, c2x, c2y, jpx, jpy);
  2629. p.curveTo(c3x, c3y, c4x, c4y, x, y);
  2630. break;
  2631. case 36: // hflex1
  2632. // |- dx1 dy1 dx2 dy2 dx3 dx4 dx5 dy5 dx6 hflex1 (12 36) |-
  2633. c1x = x + stack.shift(); // dx1
  2634. c1y = y + stack.shift(); // dy1
  2635. c2x = c1x + stack.shift(); // dx2
  2636. c2y = c1y + stack.shift(); // dy2
  2637. jpx = c2x + stack.shift(); // dx3
  2638. jpy = c2y; // dy3
  2639. c3x = jpx + stack.shift(); // dx4
  2640. c3y = c2y; // dy4
  2641. c4x = c3x + stack.shift(); // dx5
  2642. c4y = c3y + stack.shift(); // dy5
  2643. x = c4x + stack.shift(); // dx6
  2644. p.curveTo(c1x, c1y, c2x, c2y, jpx, jpy);
  2645. p.curveTo(c3x, c3y, c4x, c4y, x, y);
  2646. break;
  2647. case 37: // flex1
  2648. // |- dx1 dy1 dx2 dy2 dx3 dy3 dx4 dy4 dx5 dy5 d6 flex1 (12 37) |-
  2649. c1x = x + stack.shift(); // dx1
  2650. c1y = y + stack.shift(); // dy1
  2651. c2x = c1x + stack.shift(); // dx2
  2652. c2y = c1y + stack.shift(); // dy2
  2653. jpx = c2x + stack.shift(); // dx3
  2654. jpy = c2y + stack.shift(); // dy3
  2655. c3x = jpx + stack.shift(); // dx4
  2656. c3y = jpy + stack.shift(); // dy4
  2657. c4x = c3x + stack.shift(); // dx5
  2658. c4y = c3y + stack.shift(); // dy5
  2659. if (Math.abs(c4x - x) > Math.abs(c4y - y)) {
  2660. x = c4x + stack.shift();
  2661. } else {
  2662. y = c4y + stack.shift();
  2663. }
  2664.  
  2665. p.curveTo(c1x, c1y, c2x, c2y, jpx, jpy);
  2666. p.curveTo(c3x, c3y, c4x, c4y, x, y);
  2667. break;
  2668. default:
  2669. console.log('Glyph ' + glyph.index + ': unknown operator ' + 1200 + v);
  2670. stack.length = 0;
  2671. }
  2672. break;
  2673. case 14: // endchar
  2674. if (stack.length > 0 && !haveWidth) {
  2675. width = stack.shift() + font.nominalWidthX;
  2676. haveWidth = true;
  2677. }
  2678.  
  2679. if (open) {
  2680. p.closePath();
  2681. open = false;
  2682. }
  2683.  
  2684. break;
  2685. case 18: // hstemhm
  2686. parseStems();
  2687. break;
  2688. case 19: // hintmask
  2689. case 20: // cntrmask
  2690. parseStems();
  2691. i += (nStems + 7) >> 3;
  2692. break;
  2693. case 21: // rmoveto
  2694. if (stack.length > 2 && !haveWidth) {
  2695. width = stack.shift() + font.nominalWidthX;
  2696. haveWidth = true;
  2697. }
  2698.  
  2699. y += stack.pop();
  2700. x += stack.pop();
  2701. newContour(x, y);
  2702. break;
  2703. case 22: // hmoveto
  2704. if (stack.length > 1 && !haveWidth) {
  2705. width = stack.shift() + font.nominalWidthX;
  2706. haveWidth = true;
  2707. }
  2708.  
  2709. x += stack.pop();
  2710. newContour(x, y);
  2711. break;
  2712. case 23: // vstemhm
  2713. parseStems();
  2714. break;
  2715. case 24: // rcurveline
  2716. while (stack.length > 2) {
  2717. c1x = x + stack.shift();
  2718. c1y = y + stack.shift();
  2719. c2x = c1x + stack.shift();
  2720. c2y = c1y + stack.shift();
  2721. x = c2x + stack.shift();
  2722. y = c2y + stack.shift();
  2723. p.curveTo(c1x, c1y, c2x, c2y, x, y);
  2724. }
  2725.  
  2726. x += stack.shift();
  2727. y += stack.shift();
  2728. p.lineTo(x, y);
  2729. break;
  2730. case 25: // rlinecurve
  2731. while (stack.length > 6) {
  2732. x += stack.shift();
  2733. y += stack.shift();
  2734. p.lineTo(x, y);
  2735. }
  2736.  
  2737. c1x = x + stack.shift();
  2738. c1y = y + stack.shift();
  2739. c2x = c1x + stack.shift();
  2740. c2y = c1y + stack.shift();
  2741. x = c2x + stack.shift();
  2742. y = c2y + stack.shift();
  2743. p.curveTo(c1x, c1y, c2x, c2y, x, y);
  2744. break;
  2745. case 26: // vvcurveto
  2746. if (stack.length % 2) {
  2747. x += stack.shift();
  2748. }
  2749.  
  2750. while (stack.length > 0) {
  2751. c1x = x;
  2752. c1y = y + stack.shift();
  2753. c2x = c1x + stack.shift();
  2754. c2y = c1y + stack.shift();
  2755. x = c2x;
  2756. y = c2y + stack.shift();
  2757. p.curveTo(c1x, c1y, c2x, c2y, x, y);
  2758. }
  2759.  
  2760. break;
  2761. case 27: // hhcurveto
  2762. if (stack.length % 2) {
  2763. y += stack.shift();
  2764. }
  2765.  
  2766. while (stack.length > 0) {
  2767. c1x = x + stack.shift();
  2768. c1y = y;
  2769. c2x = c1x + stack.shift();
  2770. c2y = c1y + stack.shift();
  2771. x = c2x + stack.shift();
  2772. y = c2y;
  2773. p.curveTo(c1x, c1y, c2x, c2y, x, y);
  2774. }
  2775.  
  2776. break;
  2777. case 28: // shortint
  2778. b1 = code[i];
  2779. b2 = code[i + 1];
  2780. stack.push(((b1 << 24) | (b2 << 16)) >> 16);
  2781. i += 2;
  2782. break;
  2783. case 29: // callgsubr
  2784. codeIndex = stack.pop() + font.gsubrsBias;
  2785. subrCode = font.gsubrs[codeIndex];
  2786. if (subrCode) {
  2787. parse(subrCode);
  2788. }
  2789.  
  2790. break;
  2791. case 30: // vhcurveto
  2792. while (stack.length > 0) {
  2793. c1x = x;
  2794. c1y = y + stack.shift();
  2795. c2x = c1x + stack.shift();
  2796. c2y = c1y + stack.shift();
  2797. x = c2x + stack.shift();
  2798. y = c2y + (stack.length === 1 ? stack.shift() : 0);
  2799. p.curveTo(c1x, c1y, c2x, c2y, x, y);
  2800. if (stack.length === 0) {
  2801. break;
  2802. }
  2803.  
  2804. c1x = x + stack.shift();
  2805. c1y = y;
  2806. c2x = c1x + stack.shift();
  2807. c2y = c1y + stack.shift();
  2808. y = c2y + stack.shift();
  2809. x = c2x + (stack.length === 1 ? stack.shift() : 0);
  2810. p.curveTo(c1x, c1y, c2x, c2y, x, y);
  2811. }
  2812.  
  2813. break;
  2814. case 31: // hvcurveto
  2815. while (stack.length > 0) {
  2816. c1x = x + stack.shift();
  2817. c1y = y;
  2818. c2x = c1x + stack.shift();
  2819. c2y = c1y + stack.shift();
  2820. y = c2y + stack.shift();
  2821. x = c2x + (stack.length === 1 ? stack.shift() : 0);
  2822. p.curveTo(c1x, c1y, c2x, c2y, x, y);
  2823. if (stack.length === 0) {
  2824. break;
  2825. }
  2826.  
  2827. c1x = x;
  2828. c1y = y + stack.shift();
  2829. c2x = c1x + stack.shift();
  2830. c2y = c1y + stack.shift();
  2831. x = c2x + stack.shift();
  2832. y = c2y + (stack.length === 1 ? stack.shift() : 0);
  2833. p.curveTo(c1x, c1y, c2x, c2y, x, y);
  2834. }
  2835.  
  2836. break;
  2837. default:
  2838. if (v < 32) {
  2839. console.log('Glyph ' + glyph.index + ': unknown operator ' + v);
  2840. } else if (v < 247) {
  2841. stack.push(v - 139);
  2842. } else if (v < 251) {
  2843. b1 = code[i];
  2844. i += 1;
  2845. stack.push((v - 247) * 256 + b1 + 108);
  2846. } else if (v < 255) {
  2847. b1 = code[i];
  2848. i += 1;
  2849. stack.push(-(v - 251) * 256 - b1 - 108);
  2850. } else {
  2851. b1 = code[i];
  2852. b2 = code[i + 1];
  2853. b3 = code[i + 2];
  2854. b4 = code[i + 3];
  2855. i += 4;
  2856. stack.push(((b1 << 24) | (b2 << 16) | (b3 << 8) | b4) / 65536);
  2857. }
  2858. }
  2859. }
  2860. }
  2861.  
  2862. parse(code);
  2863.  
  2864. glyph.advanceWidth = width;
  2865. return p;
  2866. }
  2867.  
  2868. // Subroutines are encoded using the negative half of the number space.
  2869. // See type 2 chapter 4.7 "Subroutine operators".
  2870. function calcCFFSubroutineBias(subrs) {
  2871. var bias;
  2872. if (subrs.length < 1240) {
  2873. bias = 107;
  2874. } else if (subrs.length < 33900) {
  2875. bias = 1131;
  2876. } else {
  2877. bias = 32768;
  2878. }
  2879.  
  2880. return bias;
  2881. }
  2882.  
  2883. // Parse the `CFF` table, which contains the glyph outlines in PostScript format.
  2884. function parseCFFTable(data, start, font) {
  2885. font.tables.cff = {};
  2886. var header = parseCFFHeader(data, start);
  2887. var nameIndex = parseCFFIndex(data, header.endOffset, parse.bytesToString);
  2888. var topDictIndex = parseCFFIndex(data, nameIndex.endOffset);
  2889. var stringIndex = parseCFFIndex(data, topDictIndex.endOffset, parse.bytesToString);
  2890. var globalSubrIndex = parseCFFIndex(data, stringIndex.endOffset);
  2891. font.gsubrs = globalSubrIndex.objects;
  2892. font.gsubrsBias = calcCFFSubroutineBias(font.gsubrs);
  2893.  
  2894. var topDictData = new DataView(new Uint8Array(topDictIndex.objects[0]).buffer);
  2895. var topDict = parseCFFTopDict(topDictData, stringIndex.objects);
  2896. font.tables.cff.topDict = topDict;
  2897.  
  2898. var privateDictOffset = start + topDict['private'][1];
  2899. var privateDict = parseCFFPrivateDict(data, privateDictOffset, topDict['private'][0], stringIndex.objects);
  2900. font.defaultWidthX = privateDict.defaultWidthX;
  2901. font.nominalWidthX = privateDict.nominalWidthX;
  2902.  
  2903. if (privateDict.subrs !== 0) {
  2904. var subrOffset = privateDictOffset + privateDict.subrs;
  2905. var subrIndex = parseCFFIndex(data, subrOffset);
  2906. font.subrs = subrIndex.objects;
  2907. font.subrsBias = calcCFFSubroutineBias(font.subrs);
  2908. } else {
  2909. font.subrs = [];
  2910. font.subrsBias = 0;
  2911. }
  2912.  
  2913. // Offsets in the top dict are relative to the beginning of the CFF data, so add the CFF start offset.
  2914. var charStringsIndex = parseCFFIndex(data, start + topDict.charStrings);
  2915. font.nGlyphs = charStringsIndex.objects.length;
  2916.  
  2917. var charset = parseCFFCharset(data, start + topDict.charset, font.nGlyphs, stringIndex.objects);
  2918. if (topDict.encoding === 0) { // Standard encoding
  2919. font.cffEncoding = new encoding.CffEncoding(encoding.cffStandardEncoding, charset);
  2920. } else if (topDict.encoding === 1) { // Expert encoding
  2921. font.cffEncoding = new encoding.CffEncoding(encoding.cffExpertEncoding, charset);
  2922. } else {
  2923. font.cffEncoding = parseCFFEncoding(data, start + topDict.encoding, charset);
  2924. }
  2925.  
  2926. // Prefer the CMAP encoding to the CFF encoding.
  2927. font.encoding = font.encoding || font.cffEncoding;
  2928.  
  2929. font.glyphs = new glyphset.GlyphSet(font);
  2930. for (var i = 0; i < font.nGlyphs; i += 1) {
  2931. var charString = charStringsIndex.objects[i];
  2932. font.glyphs.push(i, glyphset.cffGlyphLoader(font, i, parseCFFCharstring, charString));
  2933. }
  2934. }
  2935.  
  2936. // Convert a string to a String ID (SID).
  2937. // The list of strings is modified in place.
  2938. function encodeString(s, strings) {
  2939. var sid;
  2940.  
  2941. // Is the string in the CFF standard strings?
  2942. var i = encoding.cffStandardStrings.indexOf(s);
  2943. if (i >= 0) {
  2944. sid = i;
  2945. }
  2946.  
  2947. // Is the string already in the string index?
  2948. i = strings.indexOf(s);
  2949. if (i >= 0) {
  2950. sid = i + encoding.cffStandardStrings.length;
  2951. } else {
  2952. sid = encoding.cffStandardStrings.length + strings.length;
  2953. strings.push(s);
  2954. }
  2955.  
  2956. return sid;
  2957. }
  2958.  
  2959. function makeHeader() {
  2960. return new table.Table('Header', [
  2961. {name: 'major', type: 'Card8', value: 1},
  2962. {name: 'minor', type: 'Card8', value: 0},
  2963. {name: 'hdrSize', type: 'Card8', value: 4},
  2964. {name: 'major', type: 'Card8', value: 1}
  2965. ]);
  2966. }
  2967.  
  2968. function makeNameIndex(fontNames) {
  2969. var t = new table.Table('Name INDEX', [
  2970. {name: 'names', type: 'INDEX', value: []}
  2971. ]);
  2972. t.names = [];
  2973. for (var i = 0; i < fontNames.length; i += 1) {
  2974. t.names.push({name: 'name_' + i, type: 'NAME', value: fontNames[i]});
  2975. }
  2976.  
  2977. return t;
  2978. }
  2979.  
  2980. // Given a dictionary's metadata, create a DICT structure.
  2981. function makeDict(meta, attrs, strings) {
  2982. var m = {};
  2983. for (var i = 0; i < meta.length; i += 1) {
  2984. var entry = meta[i];
  2985. var value = attrs[entry.name];
  2986. if (value !== undefined && !equals(value, entry.value)) {
  2987. if (entry.type === 'SID') {
  2988. value = encodeString(value, strings);
  2989. }
  2990.  
  2991. m[entry.op] = {name: entry.name, type: entry.type, value: value};
  2992. }
  2993. }
  2994.  
  2995. return m;
  2996. }
  2997.  
  2998. // The Top DICT houses the global font attributes.
  2999. function makeTopDict(attrs, strings) {
  3000. var t = new table.Table('Top DICT', [
  3001. {name: 'dict', type: 'DICT', value: {}}
  3002. ]);
  3003. t.dict = makeDict(TOP_DICT_META, attrs, strings);
  3004. return t;
  3005. }
  3006.  
  3007. function makeTopDictIndex(topDict) {
  3008. var t = new table.Table('Top DICT INDEX', [
  3009. {name: 'topDicts', type: 'INDEX', value: []}
  3010. ]);
  3011. t.topDicts = [{name: 'topDict_0', type: 'TABLE', value: topDict}];
  3012. return t;
  3013. }
  3014.  
  3015. function makeStringIndex(strings) {
  3016. var t = new table.Table('String INDEX', [
  3017. {name: 'strings', type: 'INDEX', value: []}
  3018. ]);
  3019. t.strings = [];
  3020. for (var i = 0; i < strings.length; i += 1) {
  3021. t.strings.push({name: 'string_' + i, type: 'STRING', value: strings[i]});
  3022. }
  3023.  
  3024. return t;
  3025. }
  3026.  
  3027. function makeGlobalSubrIndex() {
  3028. // Currently we don't use subroutines.
  3029. return new table.Table('Global Subr INDEX', [
  3030. {name: 'subrs', type: 'INDEX', value: []}
  3031. ]);
  3032. }
  3033.  
  3034. function makeCharsets(glyphNames, strings) {
  3035. var t = new table.Table('Charsets', [
  3036. {name: 'format', type: 'Card8', value: 0}
  3037. ]);
  3038. for (var i = 0; i < glyphNames.length; i += 1) {
  3039. var glyphName = glyphNames[i];
  3040. var glyphSID = encodeString(glyphName, strings);
  3041. t.fields.push({name: 'glyph_' + i, type: 'SID', value: glyphSID});
  3042. }
  3043.  
  3044. return t;
  3045. }
  3046.  
  3047. function glyphToOps(glyph) {
  3048. var ops = [];
  3049. var path = glyph.path;
  3050. ops.push({name: 'width', type: 'NUMBER', value: glyph.advanceWidth});
  3051. var x = 0;
  3052. var y = 0;
  3053. for (var i = 0; i < path.commands.length; i += 1) {
  3054. var dx;
  3055. var dy;
  3056. var cmd = path.commands[i];
  3057. if (cmd.type === 'Q') {
  3058. // CFF only supports bézier curves, so convert the quad to a bézier.
  3059. var _13 = 1 / 3;
  3060. var _23 = 2 / 3;
  3061.  
  3062. // We're going to create a new command so we don't change the original path.
  3063. cmd = {
  3064. type: 'C',
  3065. x: cmd.x,
  3066. y: cmd.y,
  3067. x1: _13 * x + _23 * cmd.x1,
  3068. y1: _13 * y + _23 * cmd.y1,
  3069. x2: _13 * cmd.x + _23 * cmd.x1,
  3070. y2: _13 * cmd.y + _23 * cmd.y1
  3071. };
  3072. }
  3073.  
  3074. if (cmd.type === 'M') {
  3075. dx = Math.round(cmd.x - x);
  3076. dy = Math.round(cmd.y - y);
  3077. ops.push({name: 'dx', type: 'NUMBER', value: dx});
  3078. ops.push({name: 'dy', type: 'NUMBER', value: dy});
  3079. ops.push({name: 'rmoveto', type: 'OP', value: 21});
  3080. x = Math.round(cmd.x);
  3081. y = Math.round(cmd.y);
  3082. } else if (cmd.type === 'L') {
  3083. dx = Math.round(cmd.x - x);
  3084. dy = Math.round(cmd.y - y);
  3085. ops.push({name: 'dx', type: 'NUMBER', value: dx});
  3086. ops.push({name: 'dy', type: 'NUMBER', value: dy});
  3087. ops.push({name: 'rlineto', type: 'OP', value: 5});
  3088. x = Math.round(cmd.x);
  3089. y = Math.round(cmd.y);
  3090. } else if (cmd.type === 'C') {
  3091. var dx1 = Math.round(cmd.x1 - x);
  3092. var dy1 = Math.round(cmd.y1 - y);
  3093. var dx2 = Math.round(cmd.x2 - cmd.x1);
  3094. var dy2 = Math.round(cmd.y2 - cmd.y1);
  3095. dx = Math.round(cmd.x - cmd.x2);
  3096. dy = Math.round(cmd.y - cmd.y2);
  3097. ops.push({name: 'dx1', type: 'NUMBER', value: dx1});
  3098. ops.push({name: 'dy1', type: 'NUMBER', value: dy1});
  3099. ops.push({name: 'dx2', type: 'NUMBER', value: dx2});
  3100. ops.push({name: 'dy2', type: 'NUMBER', value: dy2});
  3101. ops.push({name: 'dx', type: 'NUMBER', value: dx});
  3102. ops.push({name: 'dy', type: 'NUMBER', value: dy});
  3103. ops.push({name: 'rrcurveto', type: 'OP', value: 8});
  3104. x = Math.round(cmd.x);
  3105. y = Math.round(cmd.y);
  3106. }
  3107.  
  3108. // Contours are closed automatically.
  3109.  
  3110. }
  3111.  
  3112. ops.push({name: 'endchar', type: 'OP', value: 14});
  3113. return ops;
  3114. }
  3115.  
  3116. function makeCharStringsIndex(glyphs) {
  3117. var t = new table.Table('CharStrings INDEX', [
  3118. {name: 'charStrings', type: 'INDEX', value: []}
  3119. ]);
  3120.  
  3121. for (var i = 0; i < glyphs.length; i += 1) {
  3122. var glyph = glyphs.get(i);
  3123. var ops = glyphToOps(glyph);
  3124. t.charStrings.push({name: glyph.name, type: 'CHARSTRING', value: ops});
  3125. }
  3126.  
  3127. return t;
  3128. }
  3129.  
  3130. function makePrivateDict(attrs, strings) {
  3131. var t = new table.Table('Private DICT', [
  3132. {name: 'dict', type: 'DICT', value: {}}
  3133. ]);
  3134. t.dict = makeDict(PRIVATE_DICT_META, attrs, strings);
  3135. return t;
  3136. }
  3137.  
  3138. function makeCFFTable(glyphs, options) {
  3139. var t = new table.Table('CFF ', [
  3140. {name: 'header', type: 'TABLE'},
  3141. {name: 'nameIndex', type: 'TABLE'},
  3142. {name: 'topDictIndex', type: 'TABLE'},
  3143. {name: 'stringIndex', type: 'TABLE'},
  3144. {name: 'globalSubrIndex', type: 'TABLE'},
  3145. {name: 'charsets', type: 'TABLE'},
  3146. {name: 'charStringsIndex', type: 'TABLE'},
  3147. {name: 'privateDict', type: 'TABLE'}
  3148. ]);
  3149.  
  3150. var fontScale = 1 / options.unitsPerEm;
  3151. // We use non-zero values for the offsets so that the DICT encodes them.
  3152. // This is important because the size of the Top DICT plays a role in offset calculation,
  3153. // and the size shouldn't change after we've written correct offsets.
  3154. var attrs = {
  3155. version: options.version,
  3156. fullName: options.fullName,
  3157. familyName: options.familyName,
  3158. weight: options.weightName,
  3159. fontBBox: options.fontBBox || [0, 0, 0, 0],
  3160. fontMatrix: [fontScale, 0, 0, fontScale, 0, 0],
  3161. charset: 999,
  3162. encoding: 0,
  3163. charStrings: 999,
  3164. private: [0, 999]
  3165. };
  3166.  
  3167. var privateAttrs = {};
  3168.  
  3169. var glyphNames = [];
  3170. var glyph;
  3171.  
  3172. // Skip first glyph (.notdef)
  3173. for (var i = 1; i < glyphs.length; i += 1) {
  3174. glyph = glyphs.get(i);
  3175. glyphNames.push(glyph.name);
  3176. }
  3177.  
  3178. var strings = [];
  3179.  
  3180. t.header = makeHeader();
  3181. t.nameIndex = makeNameIndex([options.postScriptName]);
  3182. var topDict = makeTopDict(attrs, strings);
  3183. t.topDictIndex = makeTopDictIndex(topDict);
  3184. t.globalSubrIndex = makeGlobalSubrIndex();
  3185. t.charsets = makeCharsets(glyphNames, strings);
  3186. t.charStringsIndex = makeCharStringsIndex(glyphs);
  3187. t.privateDict = makePrivateDict(privateAttrs, strings);
  3188.  
  3189. // Needs to come at the end, to encode all custom strings used in the font.
  3190. t.stringIndex = makeStringIndex(strings);
  3191.  
  3192. var startOffset = t.header.sizeOf() +
  3193. t.nameIndex.sizeOf() +
  3194. t.topDictIndex.sizeOf() +
  3195. t.stringIndex.sizeOf() +
  3196. t.globalSubrIndex.sizeOf();
  3197. attrs.charset = startOffset;
  3198.  
  3199. // We use the CFF standard encoding; proper encoding will be handled in cmap.
  3200. attrs.encoding = 0;
  3201. attrs.charStrings = attrs.charset + t.charsets.sizeOf();
  3202. attrs.private[1] = attrs.charStrings + t.charStringsIndex.sizeOf();
  3203.  
  3204. // Recreate the Top DICT INDEX with the correct offsets.
  3205. topDict = makeTopDict(attrs, strings);
  3206. t.topDictIndex = makeTopDictIndex(topDict);
  3207.  
  3208. return t;
  3209. }
  3210.  
  3211. exports.parse = parseCFFTable;
  3212. exports.make = makeCFFTable;
  3213.  
  3214. },{"../encoding":4,"../glyphset":7,"../parse":9,"../path":10,"../table":11}],13:[function(require,module,exports){
  3215. // The `cmap` table stores the mappings from characters to glyphs.
  3216. // https://www.microsoft.com/typography/OTSPEC/cmap.htm
  3217.  
  3218. 'use strict';
  3219.  
  3220. var check = require('../check');
  3221. var parse = require('../parse');
  3222. var table = require('../table');
  3223.  
  3224. // Parse the `cmap` table. This table stores the mappings from characters to glyphs.
  3225. // There are many available formats, but we only support the Windows format 4.
  3226. // This function returns a `CmapEncoding` object or null if no supported format could be found.
  3227. function parseCmapTable(data, start) {
  3228. var i;
  3229. var cmap = {};
  3230. cmap.version = parse.getUShort(data, start);
  3231. check.argument(cmap.version === 0, 'cmap table version should be 0.');
  3232.  
  3233. // The cmap table can contain many sub-tables, each with their own format.
  3234. // We're only interested in a "platform 3" table. This is a Windows format.
  3235. cmap.numTables = parse.getUShort(data, start + 2);
  3236. var offset = -1;
  3237. for (i = 0; i < cmap.numTables; i += 1) {
  3238. var platformId = parse.getUShort(data, start + 4 + (i * 8));
  3239. var encodingId = parse.getUShort(data, start + 4 + (i * 8) + 2);
  3240. if (platformId === 3 && (encodingId === 1 || encodingId === 0)) {
  3241. offset = parse.getULong(data, start + 4 + (i * 8) + 4);
  3242. break;
  3243. }
  3244. }
  3245.  
  3246. if (offset === -1) {
  3247. // There is no cmap table in the font that we support, so return null.
  3248. // This font will be marked as unsupported.
  3249. return null;
  3250. }
  3251.  
  3252. var p = new parse.Parser(data, start + offset);
  3253. cmap.format = p.parseUShort();
  3254. check.argument(cmap.format === 4, 'Only format 4 cmap tables are supported.');
  3255.  
  3256. // Length in bytes of the sub-tables.
  3257. cmap.length = p.parseUShort();
  3258. cmap.language = p.parseUShort();
  3259.  
  3260. // segCount is stored x 2.
  3261. var segCount;
  3262. cmap.segCount = segCount = p.parseUShort() >> 1;
  3263.  
  3264. // Skip searchRange, entrySelector, rangeShift.
  3265. p.skip('uShort', 3);
  3266.  
  3267. // The "unrolled" mapping from character codes to glyph indices.
  3268. cmap.glyphIndexMap = {};
  3269.  
  3270. var endCountParser = new parse.Parser(data, start + offset + 14);
  3271. var startCountParser = new parse.Parser(data, start + offset + 16 + segCount * 2);
  3272. var idDeltaParser = new parse.Parser(data, start + offset + 16 + segCount * 4);
  3273. var idRangeOffsetParser = new parse.Parser(data, start + offset + 16 + segCount * 6);
  3274. var glyphIndexOffset = start + offset + 16 + segCount * 8;
  3275. for (i = 0; i < segCount - 1; i += 1) {
  3276. var glyphIndex;
  3277. var endCount = endCountParser.parseUShort();
  3278. var startCount = startCountParser.parseUShort();
  3279. var idDelta = idDeltaParser.parseShort();
  3280. var idRangeOffset = idRangeOffsetParser.parseUShort();
  3281. for (var c = startCount; c <= endCount; c += 1) {
  3282. if (idRangeOffset !== 0) {
  3283. // The idRangeOffset is relative to the current position in the idRangeOffset array.
  3284. // Take the current offset in the idRangeOffset array.
  3285. glyphIndexOffset = (idRangeOffsetParser.offset + idRangeOffsetParser.relativeOffset - 2);
  3286.  
  3287. // Add the value of the idRangeOffset, which will move us into the glyphIndex array.
  3288. glyphIndexOffset += idRangeOffset;
  3289.  
  3290. // Then add the character index of the current segment, multiplied by 2 for USHORTs.
  3291. glyphIndexOffset += (c - startCount) * 2;
  3292. glyphIndex = parse.getUShort(data, glyphIndexOffset);
  3293. if (glyphIndex !== 0) {
  3294. glyphIndex = (glyphIndex + idDelta) & 0xFFFF;
  3295. }
  3296. } else {
  3297. glyphIndex = (c + idDelta) & 0xFFFF;
  3298. }
  3299.  
  3300. cmap.glyphIndexMap[c] = glyphIndex;
  3301. }
  3302. }
  3303.  
  3304. return cmap;
  3305. }
  3306.  
  3307. function addSegment(t, code, glyphIndex) {
  3308. t.segments.push({
  3309. end: code,
  3310. start: code,
  3311. delta: -(code - glyphIndex),
  3312. offset: 0
  3313. });
  3314. }
  3315.  
  3316. function addTerminatorSegment(t) {
  3317. t.segments.push({
  3318. end: 0xFFFF,
  3319. start: 0xFFFF,
  3320. delta: 1,
  3321. offset: 0
  3322. });
  3323. }
  3324.  
  3325. function makeCmapTable(glyphs) {
  3326. var i;
  3327. var t = new table.Table('cmap', [
  3328. {name: 'version', type: 'USHORT', value: 0},
  3329. {name: 'numTables', type: 'USHORT', value: 1},
  3330. {name: 'platformID', type: 'USHORT', value: 3},
  3331. {name: 'encodingID', type: 'USHORT', value: 1},
  3332. {name: 'offset', type: 'ULONG', value: 12},
  3333. {name: 'format', type: 'USHORT', value: 4},
  3334. {name: 'length', type: 'USHORT', value: 0},
  3335. {name: 'language', type: 'USHORT', value: 0},
  3336. {name: 'segCountX2', type: 'USHORT', value: 0},
  3337. {name: 'searchRange', type: 'USHORT', value: 0},
  3338. {name: 'entrySelector', type: 'USHORT', value: 0},
  3339. {name: 'rangeShift', type: 'USHORT', value: 0}
  3340. ]);
  3341.  
  3342. t.segments = [];
  3343. for (i = 0; i < glyphs.length; i += 1) {
  3344. var glyph = glyphs.get(i);
  3345. for (var j = 0; j < glyph.unicodes.length; j += 1) {
  3346. addSegment(t, glyph.unicodes[j], i);
  3347. }
  3348.  
  3349. t.segments = t.segments.sort(function(a, b) {
  3350. return a.start - b.start;
  3351. });
  3352. }
  3353.  
  3354. addTerminatorSegment(t);
  3355.  
  3356. var segCount;
  3357. segCount = t.segments.length;
  3358. t.segCountX2 = segCount * 2;
  3359. t.searchRange = Math.pow(2, Math.floor(Math.log(segCount) / Math.log(2))) * 2;
  3360. t.entrySelector = Math.log(t.searchRange / 2) / Math.log(2);
  3361. t.rangeShift = t.segCountX2 - t.searchRange;
  3362.  
  3363. // Set up parallel segment arrays.
  3364. var endCounts = [];
  3365. var startCounts = [];
  3366. var idDeltas = [];
  3367. var idRangeOffsets = [];
  3368. var glyphIds = [];
  3369.  
  3370. for (i = 0; i < segCount; i += 1) {
  3371. var segment = t.segments[i];
  3372. endCounts = endCounts.concat({name: 'end_' + i, type: 'USHORT', value: segment.end});
  3373. startCounts = startCounts.concat({name: 'start_' + i, type: 'USHORT', value: segment.start});
  3374. idDeltas = idDeltas.concat({name: 'idDelta_' + i, type: 'SHORT', value: segment.delta});
  3375. idRangeOffsets = idRangeOffsets.concat({name: 'idRangeOffset_' + i, type: 'USHORT', value: segment.offset});
  3376. if (segment.glyphId !== undefined) {
  3377. glyphIds = glyphIds.concat({name: 'glyph_' + i, type: 'USHORT', value: segment.glyphId});
  3378. }
  3379. }
  3380.  
  3381. t.fields = t.fields.concat(endCounts);
  3382. t.fields.push({name: 'reservedPad', type: 'USHORT', value: 0});
  3383. t.fields = t.fields.concat(startCounts);
  3384. t.fields = t.fields.concat(idDeltas);
  3385. t.fields = t.fields.concat(idRangeOffsets);
  3386. t.fields = t.fields.concat(glyphIds);
  3387.  
  3388. t.length = 14 + // Subtable header
  3389. endCounts.length * 2 +
  3390. 2 + // reservedPad
  3391. startCounts.length * 2 +
  3392. idDeltas.length * 2 +
  3393. idRangeOffsets.length * 2 +
  3394. glyphIds.length * 2;
  3395.  
  3396. return t;
  3397. }
  3398.  
  3399. exports.parse = parseCmapTable;
  3400. exports.make = makeCmapTable;
  3401.  
  3402. },{"../check":2,"../parse":9,"../table":11}],14:[function(require,module,exports){
  3403. // The `fvar` table stores font variation axes and instances.
  3404. // https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6fvar.html
  3405.  
  3406. 'use strict';
  3407.  
  3408. var check = require('../check');
  3409. var parse = require('../parse');
  3410. var table = require('../table');
  3411.  
  3412. function addName(name, names) {
  3413. var nameString = JSON.stringify(name);
  3414. var nameID = 256;
  3415. for (var nameKey in names) {
  3416. var n = parseInt(nameKey);
  3417. if (!n || n < 256) {
  3418. continue;
  3419. }
  3420.  
  3421. if (JSON.stringify(names[nameKey]) === nameString) {
  3422. return n;
  3423. }
  3424.  
  3425. if (nameID <= n) {
  3426. nameID = n + 1;
  3427. }
  3428. }
  3429.  
  3430. names[nameID] = name;
  3431. return nameID;
  3432. }
  3433.  
  3434. function makeFvarAxis(axis, names) {
  3435. var nameID = addName(axis.name, names);
  3436. return new table.Table('fvarAxis', [
  3437. {name: 'tag', type: 'TAG', value: axis.tag},
  3438. {name: 'minValue', type: 'FIXED', value: axis.minValue << 16},
  3439. {name: 'defaultValue', type: 'FIXED', value: axis.defaultValue << 16},
  3440. {name: 'maxValue', type: 'FIXED', value: axis.maxValue << 16},
  3441. {name: 'flags', type: 'USHORT', value: 0},
  3442. {name: 'nameID', type: 'USHORT', value: nameID}
  3443. ]);
  3444. }
  3445.  
  3446. function parseFvarAxis(data, start, names) {
  3447. var axis = {};
  3448. var p = new parse.Parser(data, start);
  3449. axis.tag = p.parseTag();
  3450. axis.minValue = p.parseFixed();
  3451. axis.defaultValue = p.parseFixed();
  3452. axis.maxValue = p.parseFixed();
  3453. p.skip('uShort', 1); // reserved for flags; no values defined
  3454. axis.name = names[p.parseUShort()] || {};
  3455. return axis;
  3456. }
  3457.  
  3458. function makeFvarInstance(inst, axes, names) {
  3459. var nameID = addName(inst.name, names);
  3460. var fields = [
  3461. {name: 'nameID', type: 'USHORT', value: nameID},
  3462. {name: 'flags', type: 'USHORT', value: 0}
  3463. ];
  3464.  
  3465. for (var i = 0; i < axes.length; ++i) {
  3466. var axisTag = axes[i].tag;
  3467. fields.push({
  3468. name: 'axis ' + axisTag,
  3469. type: 'FIXED',
  3470. value: inst.coordinates[axisTag] << 16
  3471. });
  3472. }
  3473.  
  3474. return new table.Table('fvarInstance', fields);
  3475. }
  3476.  
  3477. function parseFvarInstance(data, start, axes, names) {
  3478. var inst = {};
  3479. var p = new parse.Parser(data, start);
  3480. inst.name = names[p.parseUShort()] || {};
  3481. p.skip('uShort', 1); // reserved for flags; no values defined
  3482.  
  3483. inst.coordinates = {};
  3484. for (var i = 0; i < axes.length; ++i) {
  3485. inst.coordinates[axes[i].tag] = p.parseFixed();
  3486. }
  3487.  
  3488. return inst;
  3489. }
  3490.  
  3491. function makeFvarTable(fvar, names) {
  3492. var result = new table.Table('fvar', [
  3493. {name: 'version', type: 'ULONG', value: 0x10000},
  3494. {name: 'offsetToData', type: 'USHORT', value: 0},
  3495. {name: 'countSizePairs', type: 'USHORT', value: 2},
  3496. {name: 'axisCount', type: 'USHORT', value: fvar.axes.length},
  3497. {name: 'axisSize', type: 'USHORT', value: 20},
  3498. {name: 'instanceCount', type: 'USHORT', value: fvar.instances.length},
  3499. {name: 'instanceSize', type: 'USHORT', value: 4 + fvar.axes.length * 4}
  3500. ]);
  3501. result.offsetToData = result.sizeOf();
  3502.  
  3503. for (var i = 0; i < fvar.axes.length; i++) {
  3504. result.fields.push({
  3505. name: 'axis ' + i,
  3506. type: 'TABLE',
  3507. value: makeFvarAxis(fvar.axes[i], names)});
  3508. }
  3509.  
  3510. for (var j = 0; j < fvar.instances.length; j++) {
  3511. result.fields.push({
  3512. name: 'instance ' + j,
  3513. type: 'TABLE',
  3514. value: makeFvarInstance(fvar.instances[j], fvar.axes, names)
  3515. });
  3516. }
  3517.  
  3518. return result;
  3519. }
  3520.  
  3521. function parseFvarTable(data, start, names) {
  3522. var p = new parse.Parser(data, start);
  3523. var tableVersion = p.parseULong();
  3524. check.argument(tableVersion === 0x00010000, 'Unsupported fvar table version.');
  3525. var offsetToData = p.parseOffset16();
  3526. // Skip countSizePairs.
  3527. p.skip('uShort', 1);
  3528. var axisCount = p.parseUShort();
  3529. var axisSize = p.parseUShort();
  3530. var instanceCount = p.parseUShort();
  3531. var instanceSize = p.parseUShort();
  3532.  
  3533. var axes = [];
  3534. for (var i = 0; i < axisCount; i++) {
  3535. axes.push(parseFvarAxis(data, start + offsetToData + i * axisSize, names));
  3536. }
  3537.  
  3538. var instances = [];
  3539. var instanceStart = start + offsetToData + axisCount * axisSize;
  3540. for (var j = 0; j < instanceCount; j++) {
  3541. instances.push(parseFvarInstance(data, instanceStart + j * instanceSize, axes, names));
  3542. }
  3543.  
  3544. return {axes:axes, instances:instances};
  3545. }
  3546.  
  3547. exports.make = makeFvarTable;
  3548. exports.parse = parseFvarTable;
  3549.  
  3550. },{"../check":2,"../parse":9,"../table":11}],15:[function(require,module,exports){
  3551. // The `glyf` table describes the glyphs in TrueType outline format.
  3552. // http://www.microsoft.com/typography/otspec/glyf.htm
  3553.  
  3554. 'use strict';
  3555.  
  3556. var check = require('../check');
  3557. var glyphset = require('../glyphset');
  3558. var parse = require('../parse');
  3559. var path = require('../path');
  3560.  
  3561. // Parse the coordinate data for a glyph.
  3562. function parseGlyphCoordinate(p, flag, previousValue, shortVectorBitMask, sameBitMask) {
  3563. var v;
  3564. if ((flag & shortVectorBitMask) > 0) {
  3565. // The coordinate is 1 byte long.
  3566. v = p.parseByte();
  3567. // The `same` bit is re-used for short values to signify the sign of the value.
  3568. if ((flag & sameBitMask) === 0) {
  3569. v = -v;
  3570. }
  3571.  
  3572. v = previousValue + v;
  3573. } else {
  3574. // The coordinate is 2 bytes long.
  3575. // If the `same` bit is set, the coordinate is the same as the previous coordinate.
  3576. if ((flag & sameBitMask) > 0) {
  3577. v = previousValue;
  3578. } else {
  3579. // Parse the coordinate as a signed 16-bit delta value.
  3580. v = previousValue + p.parseShort();
  3581. }
  3582. }
  3583.  
  3584. return v;
  3585. }
  3586.  
  3587. // Parse a TrueType glyph.
  3588. function parseGlyph(glyph, data, start) {
  3589. var p = new parse.Parser(data, start);
  3590. glyph.numberOfContours = p.parseShort();
  3591. glyph.xMin = p.parseShort();
  3592. glyph.yMin = p.parseShort();
  3593. glyph.xMax = p.parseShort();
  3594. glyph.yMax = p.parseShort();
  3595. var flags;
  3596. var flag;
  3597. if (glyph.numberOfContours > 0) {
  3598. var i;
  3599. // This glyph is not a composite.
  3600. var endPointIndices = glyph.endPointIndices = [];
  3601. for (i = 0; i < glyph.numberOfContours; i += 1) {
  3602. endPointIndices.push(p.parseUShort());
  3603. }
  3604.  
  3605. glyph.instructionLength = p.parseUShort();
  3606. glyph.instructions = [];
  3607. for (i = 0; i < glyph.instructionLength; i += 1) {
  3608. glyph.instructions.push(p.parseByte());
  3609. }
  3610.  
  3611. var numberOfCoordinates = endPointIndices[endPointIndices.length - 1] + 1;
  3612. flags = [];
  3613. for (i = 0; i < numberOfCoordinates; i += 1) {
  3614. flag = p.parseByte();
  3615. flags.push(flag);
  3616. // If bit 3 is set, we repeat this flag n times, where n is the next byte.
  3617. if ((flag & 8) > 0) {
  3618. var repeatCount = p.parseByte();
  3619. for (var j = 0; j < repeatCount; j += 1) {
  3620. flags.push(flag);
  3621. i += 1;
  3622. }
  3623. }
  3624. }
  3625.  
  3626. check.argument(flags.length === numberOfCoordinates, 'Bad flags.');
  3627.  
  3628. if (endPointIndices.length > 0) {
  3629. var points = [];
  3630. var point;
  3631. // X/Y coordinates are relative to the previous point, except for the first point which is relative to 0,0.
  3632. if (numberOfCoordinates > 0) {
  3633. for (i = 0; i < numberOfCoordinates; i += 1) {
  3634. flag = flags[i];
  3635. point = {};
  3636. point.onCurve = !!(flag & 1);
  3637. point.lastPointOfContour = endPointIndices.indexOf(i) >= 0;
  3638. points.push(point);
  3639. }
  3640.  
  3641. var px = 0;
  3642. for (i = 0; i < numberOfCoordinates; i += 1) {
  3643. flag = flags[i];
  3644. point = points[i];
  3645. point.x = parseGlyphCoordinate(p, flag, px, 2, 16);
  3646. px = point.x;
  3647. }
  3648.  
  3649. var py = 0;
  3650. for (i = 0; i < numberOfCoordinates; i += 1) {
  3651. flag = flags[i];
  3652. point = points[i];
  3653. point.y = parseGlyphCoordinate(p, flag, py, 4, 32);
  3654. py = point.y;
  3655. }
  3656. }
  3657.  
  3658. glyph.points = points;
  3659. } else {
  3660. glyph.points = [];
  3661. }
  3662. } else if (glyph.numberOfContours === 0) {
  3663. glyph.points = [];
  3664. } else {
  3665. glyph.isComposite = true;
  3666. glyph.points = [];
  3667. glyph.components = [];
  3668. var moreComponents = true;
  3669. while (moreComponents) {
  3670. flags = p.parseUShort();
  3671. var component = {
  3672. glyphIndex: p.parseUShort(),
  3673. xScale: 1,
  3674. scale01: 0,
  3675. scale10: 0,
  3676. yScale: 1,
  3677. dx: 0,
  3678. dy: 0
  3679. };
  3680. if ((flags & 1) > 0) {
  3681. // The arguments are words
  3682. component.dx = p.parseShort();
  3683. component.dy = p.parseShort();
  3684. } else {
  3685. // The arguments are bytes
  3686. component.dx = p.parseChar();
  3687. component.dy = p.parseChar();
  3688. }
  3689.  
  3690. if ((flags & 8) > 0) {
  3691. // We have a scale
  3692. component.xScale = component.yScale = p.parseF2Dot14();
  3693. } else if ((flags & 64) > 0) {
  3694. // We have an X / Y scale
  3695. component.xScale = p.parseF2Dot14();
  3696. component.yScale = p.parseF2Dot14();
  3697. } else if ((flags & 128) > 0) {
  3698. // We have a 2x2 transformation
  3699. component.xScale = p.parseF2Dot14();
  3700. component.scale01 = p.parseF2Dot14();
  3701. component.scale10 = p.parseF2Dot14();
  3702. component.yScale = p.parseF2Dot14();
  3703. }
  3704.  
  3705. glyph.components.push(component);
  3706. moreComponents = !!(flags & 32);
  3707. }
  3708. }
  3709. }
  3710.  
  3711. // Transform an array of points and return a new array.
  3712. function transformPoints(points, transform) {
  3713. var newPoints = [];
  3714. for (var i = 0; i < points.length; i += 1) {
  3715. var pt = points[i];
  3716. var newPt = {
  3717. x: transform.xScale * pt.x + transform.scale01 * pt.y + transform.dx,
  3718. y: transform.scale10 * pt.x + transform.yScale * pt.y + transform.dy,
  3719. onCurve: pt.onCurve,
  3720. lastPointOfContour: pt.lastPointOfContour
  3721. };
  3722. newPoints.push(newPt);
  3723. }
  3724.  
  3725. return newPoints;
  3726. }
  3727.  
  3728. function getContours(points) {
  3729. var contours = [];
  3730. var currentContour = [];
  3731. for (var i = 0; i < points.length; i += 1) {
  3732. var pt = points[i];
  3733. currentContour.push(pt);
  3734. if (pt.lastPointOfContour) {
  3735. contours.push(currentContour);
  3736. currentContour = [];
  3737. }
  3738. }
  3739.  
  3740. check.argument(currentContour.length === 0, 'There are still points left in the current contour.');
  3741. return contours;
  3742. }
  3743.  
  3744. // Convert the TrueType glyph outline to a Path.
  3745. function getPath(points) {
  3746. var p = new path.Path();
  3747. if (!points) {
  3748. return p;
  3749. }
  3750.  
  3751. var contours = getContours(points);
  3752. for (var i = 0; i < contours.length; i += 1) {
  3753. var contour = contours[i];
  3754. var firstPt = contour[0];
  3755. var lastPt = contour[contour.length - 1];
  3756. var curvePt;
  3757. var realFirstPoint;
  3758. if (firstPt.onCurve) {
  3759. curvePt = null;
  3760. // The first point will be consumed by the moveTo command,
  3761. // so skip it in the loop.
  3762. realFirstPoint = true;
  3763. } else {
  3764. if (lastPt.onCurve) {
  3765. // If the first point is off-curve and the last point is on-curve,
  3766. // start at the last point.
  3767. firstPt = lastPt;
  3768. } else {
  3769. // If both first and last points are off-curve, start at their middle.
  3770. firstPt = { x: (firstPt.x + lastPt.x) / 2, y: (firstPt.y + lastPt.y) / 2 };
  3771. }
  3772.  
  3773. curvePt = firstPt;
  3774. // The first point is synthesized, so don't skip the real first point.
  3775. realFirstPoint = false;
  3776. }
  3777.  
  3778. p.moveTo(firstPt.x, firstPt.y);
  3779.  
  3780. for (var j = realFirstPoint ? 1 : 0; j < contour.length; j += 1) {
  3781. var pt = contour[j];
  3782. var prevPt = j === 0 ? firstPt : contour[j - 1];
  3783. if (prevPt.onCurve && pt.onCurve) {
  3784. // This is a straight line.
  3785. p.lineTo(pt.x, pt.y);
  3786. } else if (prevPt.onCurve && !pt.onCurve) {
  3787. curvePt = pt;
  3788. } else if (!prevPt.onCurve && !pt.onCurve) {
  3789. var midPt = { x: (prevPt.x + pt.x) / 2, y: (prevPt.y + pt.y) / 2 };
  3790. p.quadraticCurveTo(prevPt.x, prevPt.y, midPt.x, midPt.y);
  3791. curvePt = pt;
  3792. } else if (!prevPt.onCurve && pt.onCurve) {
  3793. // Previous point off-curve, this point on-curve.
  3794. p.quadraticCurveTo(curvePt.x, curvePt.y, pt.x, pt.y);
  3795. curvePt = null;
  3796. } else {
  3797. throw new Error('Invalid state.');
  3798. }
  3799. }
  3800.  
  3801. if (firstPt !== lastPt) {
  3802. // Connect the last and first points
  3803. if (curvePt) {
  3804. p.quadraticCurveTo(curvePt.x, curvePt.y, firstPt.x, firstPt.y);
  3805. } else {
  3806. p.lineTo(firstPt.x, firstPt.y);
  3807. }
  3808. }
  3809. }
  3810.  
  3811. p.closePath();
  3812. return p;
  3813. }
  3814.  
  3815. function buildPath(glyphs, glyph) {
  3816. if (glyph.isComposite) {
  3817. for (var j = 0; j < glyph.components.length; j += 1) {
  3818. var component = glyph.components[j];
  3819. var componentGlyph = glyphs.get(component.glyphIndex);
  3820. // Force the ttfGlyphLoader to parse the glyph.
  3821. componentGlyph.getPath();
  3822. if (componentGlyph.points) {
  3823. var transformedPoints = transformPoints(componentGlyph.points, component);
  3824. glyph.points = glyph.points.concat(transformedPoints);
  3825. }
  3826. }
  3827. }
  3828.  
  3829. return getPath(glyph.points);
  3830. }
  3831.  
  3832. // Parse all the glyphs according to the offsets from the `loca` table.
  3833. function parseGlyfTable(data, start, loca, font) {
  3834. var glyphs = new glyphset.GlyphSet(font);
  3835. var i;
  3836.  
  3837. // The last element of the loca table is invalid.
  3838. for (i = 0; i < loca.length - 1; i += 1) {
  3839. var offset = loca[i];
  3840. var nextOffset = loca[i + 1];
  3841. if (offset !== nextOffset) {
  3842. glyphs.push(i, glyphset.ttfGlyphLoader(font, i, parseGlyph, data, start + offset, buildPath));
  3843. } else {
  3844. glyphs.push(i, glyphset.glyphLoader(font, i));
  3845. }
  3846. }
  3847.  
  3848. return glyphs;
  3849. }
  3850.  
  3851. exports.parse = parseGlyfTable;
  3852.  
  3853. },{"../check":2,"../glyphset":7,"../parse":9,"../path":10}],16:[function(require,module,exports){
  3854. // The `GPOS` table contains kerning pairs, among other things.
  3855. // https://www.microsoft.com/typography/OTSPEC/gpos.htm
  3856.  
  3857. 'use strict';
  3858.  
  3859. var check = require('../check');
  3860. var parse = require('../parse');
  3861.  
  3862. // Parse ScriptList and FeatureList tables of GPOS, GSUB, GDEF, BASE, JSTF tables.
  3863. // These lists are unused by now, this function is just the basis for a real parsing.
  3864. function parseTaggedListTable(data, start) {
  3865. var p = new parse.Parser(data, start);
  3866. var n = p.parseUShort();
  3867. var list = [];
  3868. for (var i = 0; i < n; i++) {
  3869. list[p.parseTag()] = { offset: p.parseUShort() };
  3870. }
  3871.  
  3872. return list;
  3873. }
  3874.  
  3875. // Parse a coverage table in a GSUB, GPOS or GDEF table.
  3876. // Format 1 is a simple list of glyph ids,
  3877. // Format 2 is a list of ranges. It is expanded in a list of glyphs, maybe not the best idea.
  3878. function parseCoverageTable(data, start) {
  3879. var p = new parse.Parser(data, start);
  3880. var format = p.parseUShort();
  3881. var count = p.parseUShort();
  3882. if (format === 1) {
  3883. return p.parseUShortList(count);
  3884. }
  3885. else if (format === 2) {
  3886. var coverage = [];
  3887. for (; count--;) {
  3888. var begin = p.parseUShort();
  3889. var end = p.parseUShort();
  3890. var index = p.parseUShort();
  3891. for (var i = begin; i <= end; i++) {
  3892. coverage[index++] = i;
  3893. }
  3894. }
  3895.  
  3896. return coverage;
  3897. }
  3898. }
  3899.  
  3900. // Parse a Class Definition Table in a GSUB, GPOS or GDEF table.
  3901. // Returns a function that gets a class value from a glyph ID.
  3902. function parseClassDefTable(data, start) {
  3903. var p = new parse.Parser(data, start);
  3904. var format = p.parseUShort();
  3905. if (format === 1) {
  3906. // Format 1 specifies a range of consecutive glyph indices, one class per glyph ID.
  3907. var startGlyph = p.parseUShort();
  3908. var glyphCount = p.parseUShort();
  3909. var classes = p.parseUShortList(glyphCount);
  3910. return function(glyphID) {
  3911. return classes[glyphID - startGlyph] || 0;
  3912. };
  3913. }
  3914. else if (format === 2) {
  3915. // Format 2 defines multiple groups of glyph indices that belong to the same class.
  3916. var rangeCount = p.parseUShort();
  3917. var startGlyphs = [];
  3918. var endGlyphs = [];
  3919. var classValues = [];
  3920. for (var i = 0; i < rangeCount; i++) {
  3921. startGlyphs[i] = p.parseUShort();
  3922. endGlyphs[i] = p.parseUShort();
  3923. classValues[i] = p.parseUShort();
  3924. }
  3925.  
  3926. return function(glyphID) {
  3927. var l = 0;
  3928. var r = startGlyphs.length - 1;
  3929. while (l < r) {
  3930. var c = (l + r + 1) >> 1;
  3931. if (glyphID < startGlyphs[c]) {
  3932. r = c - 1;
  3933. } else {
  3934. l = c;
  3935. }
  3936. }
  3937.  
  3938. if (startGlyphs[l] <= glyphID && glyphID <= endGlyphs[l]) {
  3939. return classValues[l] || 0;
  3940. }
  3941.  
  3942. return 0;
  3943. };
  3944. }
  3945. }
  3946.  
  3947. // Parse a pair adjustment positioning subtable, format 1 or format 2
  3948. // The subtable is returned in the form of a lookup function.
  3949. function parsePairPosSubTable(data, start) {
  3950. var p = new parse.Parser(data, start);
  3951. // This part is common to format 1 and format 2 subtables
  3952. var format = p.parseUShort();
  3953. var coverageOffset = p.parseUShort();
  3954. var coverage = parseCoverageTable(data, start + coverageOffset);
  3955. // valueFormat 4: XAdvance only, 1: XPlacement only, 0: no ValueRecord for second glyph
  3956. // Only valueFormat1=4 and valueFormat2=0 is supported.
  3957. var valueFormat1 = p.parseUShort();
  3958. var valueFormat2 = p.parseUShort();
  3959. var value1;
  3960. var value2;
  3961. if (valueFormat1 !== 4 || valueFormat2 !== 0) return;
  3962. var sharedPairSets = {};
  3963. if (format === 1) {
  3964. // Pair Positioning Adjustment: Format 1
  3965. var pairSetCount = p.parseUShort();
  3966. var pairSet = [];
  3967. // Array of offsets to PairSet tables-from beginning of PairPos subtable-ordered by Coverage Index
  3968. var pairSetOffsets = p.parseOffset16List(pairSetCount);
  3969. for (var firstGlyph = 0; firstGlyph < pairSetCount; firstGlyph++) {
  3970. var pairSetOffset = pairSetOffsets[firstGlyph];
  3971. var sharedPairSet = sharedPairSets[pairSetOffset];
  3972. if (!sharedPairSet) {
  3973. // Parse a pairset table in a pair adjustment subtable format 1
  3974. sharedPairSet = {};
  3975. p.relativeOffset = pairSetOffset;
  3976. var pairValueCount = p.parseUShort();
  3977. for (; pairValueCount--;) {
  3978. var secondGlyph = p.parseUShort();
  3979. if (valueFormat1) value1 = p.parseShort();
  3980. if (valueFormat2) value2 = p.parseShort();
  3981. // We only support valueFormat1 = 4 and valueFormat2 = 0,
  3982. // so value1 is the XAdvance and value2 is empty.
  3983. sharedPairSet[secondGlyph] = value1;
  3984. }
  3985. }
  3986.  
  3987. pairSet[coverage[firstGlyph]] = sharedPairSet;
  3988. }
  3989.  
  3990. return function(leftGlyph, rightGlyph) {
  3991. var pairs = pairSet[leftGlyph];
  3992. if (pairs) return pairs[rightGlyph];
  3993. };
  3994. }
  3995. else if (format === 2) {
  3996. // Pair Positioning Adjustment: Format 2
  3997. var classDef1Offset = p.parseUShort();
  3998. var classDef2Offset = p.parseUShort();
  3999. var class1Count = p.parseUShort();
  4000. var class2Count = p.parseUShort();
  4001. var getClass1 = parseClassDefTable(data, start + classDef1Offset);
  4002. var getClass2 = parseClassDefTable(data, start + classDef2Offset);
  4003.  
  4004. // Parse kerning values by class pair.
  4005. var kerningMatrix = [];
  4006. for (var i = 0; i < class1Count; i++) {
  4007. var kerningRow = kerningMatrix[i] = [];
  4008. for (var j = 0; j < class2Count; j++) {
  4009. if (valueFormat1) value1 = p.parseShort();
  4010. if (valueFormat2) value2 = p.parseShort();
  4011. // We only support valueFormat1 = 4 and valueFormat2 = 0,
  4012. // so value1 is the XAdvance and value2 is empty.
  4013. kerningRow[j] = value1;
  4014. }
  4015. }
  4016.  
  4017. // Convert coverage list to a hash
  4018. var covered = {};
  4019. for (i = 0; i < coverage.length; i++) covered[coverage[i]] = 1;
  4020.  
  4021. // Get the kerning value for a specific glyph pair.
  4022. return function(leftGlyph, rightGlyph) {
  4023. if (!covered[leftGlyph]) return;
  4024. var class1 = getClass1(leftGlyph);
  4025. var class2 = getClass2(rightGlyph);
  4026. var kerningRow = kerningMatrix[class1];
  4027.  
  4028. if (kerningRow) {
  4029. return kerningRow[class2];
  4030. }
  4031. };
  4032. }
  4033. }
  4034.  
  4035. // Parse a LookupTable (present in of GPOS, GSUB, GDEF, BASE, JSTF tables).
  4036. function parseLookupTable(data, start) {
  4037. var p = new parse.Parser(data, start);
  4038. var lookupType = p.parseUShort();
  4039. var lookupFlag = p.parseUShort();
  4040. var useMarkFilteringSet = lookupFlag & 0x10;
  4041. var subTableCount = p.parseUShort();
  4042. var subTableOffsets = p.parseOffset16List(subTableCount);
  4043. var table = {
  4044. lookupType: lookupType,
  4045. lookupFlag: lookupFlag,
  4046. markFilteringSet: useMarkFilteringSet ? p.parseUShort() : -1
  4047. };
  4048. // LookupType 2, Pair adjustment
  4049. if (lookupType === 2) {
  4050. var subtables = [];
  4051. for (var i = 0; i < subTableCount; i++) {
  4052. subtables.push(parsePairPosSubTable(data, start + subTableOffsets[i]));
  4053. }
  4054. // Return a function which finds the kerning values in the subtables.
  4055. table.getKerningValue = function(leftGlyph, rightGlyph) {
  4056. for (var i = subtables.length; i--;) {
  4057. var value = subtables[i](leftGlyph, rightGlyph);
  4058. if (value !== undefined) return value;
  4059. }
  4060.  
  4061. return 0;
  4062. };
  4063. }
  4064.  
  4065. return table;
  4066. }
  4067.  
  4068. // Parse the `GPOS` table which contains, among other things, kerning pairs.
  4069. // https://www.microsoft.com/typography/OTSPEC/gpos.htm
  4070. function parseGposTable(data, start, font) {
  4071. var p = new parse.Parser(data, start);
  4072. var tableVersion = p.parseFixed();
  4073. check.argument(tableVersion === 1, 'Unsupported GPOS table version.');
  4074.  
  4075. // ScriptList and FeatureList - ignored for now
  4076. parseTaggedListTable(data, start + p.parseUShort());
  4077. // 'kern' is the feature we are looking for.
  4078. parseTaggedListTable(data, start + p.parseUShort());
  4079.  
  4080. // LookupList
  4081. var lookupListOffset = p.parseUShort();
  4082. p.relativeOffset = lookupListOffset;
  4083. var lookupCount = p.parseUShort();
  4084. var lookupTableOffsets = p.parseOffset16List(lookupCount);
  4085. var lookupListAbsoluteOffset = start + lookupListOffset;
  4086. for (var i = 0; i < lookupCount; i++) {
  4087. var table = parseLookupTable(data, lookupListAbsoluteOffset + lookupTableOffsets[i]);
  4088. if (table.lookupType === 2 && !font.getGposKerningValue) font.getGposKerningValue = table.getKerningValue;
  4089. }
  4090. }
  4091.  
  4092. exports.parse = parseGposTable;
  4093.  
  4094. },{"../check":2,"../parse":9}],17:[function(require,module,exports){
  4095. // The `head` table contains global information about the font.
  4096. // https://www.microsoft.com/typography/OTSPEC/head.htm
  4097.  
  4098. 'use strict';
  4099.  
  4100. var check = require('../check');
  4101. var parse = require('../parse');
  4102. var table = require('../table');
  4103.  
  4104. // Parse the header `head` table
  4105. function parseHeadTable(data, start) {
  4106. var head = {};
  4107. var p = new parse.Parser(data, start);
  4108. head.version = p.parseVersion();
  4109. head.fontRevision = Math.round(p.parseFixed() * 1000) / 1000;
  4110. head.checkSumAdjustment = p.parseULong();
  4111. head.magicNumber = p.parseULong();
  4112. check.argument(head.magicNumber === 0x5F0F3CF5, 'Font header has wrong magic number.');
  4113. head.flags = p.parseUShort();
  4114. head.unitsPerEm = p.parseUShort();
  4115. head.created = p.parseLongDateTime();
  4116. head.modified = p.parseLongDateTime();
  4117. head.xMin = p.parseShort();
  4118. head.yMin = p.parseShort();
  4119. head.xMax = p.parseShort();
  4120. head.yMax = p.parseShort();
  4121. head.macStyle = p.parseUShort();
  4122. head.lowestRecPPEM = p.parseUShort();
  4123. head.fontDirectionHint = p.parseShort();
  4124. head.indexToLocFormat = p.parseShort();
  4125. head.glyphDataFormat = p.parseShort();
  4126. return head;
  4127. }
  4128.  
  4129. function makeHeadTable(options) {
  4130. return new table.Table('head', [
  4131. {name: 'version', type: 'FIXED', value: 0x00010000},
  4132. {name: 'fontRevision', type: 'FIXED', value: 0x00010000},
  4133. {name: 'checkSumAdjustment', type: 'ULONG', value: 0},
  4134. {name: 'magicNumber', type: 'ULONG', value: 0x5F0F3CF5},
  4135. {name: 'flags', type: 'USHORT', value: 0},
  4136. {name: 'unitsPerEm', type: 'USHORT', value: 1000},
  4137. {name: 'created', type: 'LONGDATETIME', value: 0},
  4138. {name: 'modified', type: 'LONGDATETIME', value: 0},
  4139. {name: 'xMin', type: 'SHORT', value: 0},
  4140. {name: 'yMin', type: 'SHORT', value: 0},
  4141. {name: 'xMax', type: 'SHORT', value: 0},
  4142. {name: 'yMax', type: 'SHORT', value: 0},
  4143. {name: 'macStyle', type: 'USHORT', value: 0},
  4144. {name: 'lowestRecPPEM', type: 'USHORT', value: 0},
  4145. {name: 'fontDirectionHint', type: 'SHORT', value: 2},
  4146. {name: 'indexToLocFormat', type: 'SHORT', value: 0},
  4147. {name: 'glyphDataFormat', type: 'SHORT', value: 0}
  4148. ], options);
  4149. }
  4150.  
  4151. exports.parse = parseHeadTable;
  4152. exports.make = makeHeadTable;
  4153.  
  4154. },{"../check":2,"../parse":9,"../table":11}],18:[function(require,module,exports){
  4155. // The `hhea` table contains information for horizontal layout.
  4156. // https://www.microsoft.com/typography/OTSPEC/hhea.htm
  4157.  
  4158. 'use strict';
  4159.  
  4160. var parse = require('../parse');
  4161. var table = require('../table');
  4162.  
  4163. // Parse the horizontal header `hhea` table
  4164. function parseHheaTable(data, start) {
  4165. var hhea = {};
  4166. var p = new parse.Parser(data, start);
  4167. hhea.version = p.parseVersion();
  4168. hhea.ascender = p.parseShort();
  4169. hhea.descender = p.parseShort();
  4170. hhea.lineGap = p.parseShort();
  4171. hhea.advanceWidthMax = p.parseUShort();
  4172. hhea.minLeftSideBearing = p.parseShort();
  4173. hhea.minRightSideBearing = p.parseShort();
  4174. hhea.xMaxExtent = p.parseShort();
  4175. hhea.caretSlopeRise = p.parseShort();
  4176. hhea.caretSlopeRun = p.parseShort();
  4177. hhea.caretOffset = p.parseShort();
  4178. p.relativeOffset += 8;
  4179. hhea.metricDataFormat = p.parseShort();
  4180. hhea.numberOfHMetrics = p.parseUShort();
  4181. return hhea;
  4182. }
  4183.  
  4184. function makeHheaTable(options) {
  4185. return new table.Table('hhea', [
  4186. {name: 'version', type: 'FIXED', value: 0x00010000},
  4187. {name: 'ascender', type: 'FWORD', value: 0},
  4188. {name: 'descender', type: 'FWORD', value: 0},
  4189. {name: 'lineGap', type: 'FWORD', value: 0},
  4190. {name: 'advanceWidthMax', type: 'UFWORD', value: 0},
  4191. {name: 'minLeftSideBearing', type: 'FWORD', value: 0},
  4192. {name: 'minRightSideBearing', type: 'FWORD', value: 0},
  4193. {name: 'xMaxExtent', type: 'FWORD', value: 0},
  4194. {name: 'caretSlopeRise', type: 'SHORT', value: 1},
  4195. {name: 'caretSlopeRun', type: 'SHORT', value: 0},
  4196. {name: 'caretOffset', type: 'SHORT', value: 0},
  4197. {name: 'reserved1', type: 'SHORT', value: 0},
  4198. {name: 'reserved2', type: 'SHORT', value: 0},
  4199. {name: 'reserved3', type: 'SHORT', value: 0},
  4200. {name: 'reserved4', type: 'SHORT', value: 0},
  4201. {name: 'metricDataFormat', type: 'SHORT', value: 0},
  4202. {name: 'numberOfHMetrics', type: 'USHORT', value: 0}
  4203. ], options);
  4204. }
  4205.  
  4206. exports.parse = parseHheaTable;
  4207. exports.make = makeHheaTable;
  4208.  
  4209. },{"../parse":9,"../table":11}],19:[function(require,module,exports){
  4210. // The `hmtx` table contains the horizontal metrics for all glyphs.
  4211. // https://www.microsoft.com/typography/OTSPEC/hmtx.htm
  4212.  
  4213. 'use strict';
  4214.  
  4215. var parse = require('../parse');
  4216. var table = require('../table');
  4217.  
  4218. // Parse the `hmtx` table, which contains the horizontal metrics for all glyphs.
  4219. // This function augments the glyph array, adding the advanceWidth and leftSideBearing to each glyph.
  4220. function parseHmtxTable(data, start, numMetrics, numGlyphs, glyphs) {
  4221. var advanceWidth;
  4222. var leftSideBearing;
  4223. var p = new parse.Parser(data, start);
  4224. for (var i = 0; i < numGlyphs; i += 1) {
  4225. // If the font is monospaced, only one entry is needed. This last entry applies to all subsequent glyphs.
  4226. if (i < numMetrics) {
  4227. advanceWidth = p.parseUShort();
  4228. leftSideBearing = p.parseShort();
  4229. }
  4230.  
  4231. var glyph = glyphs.get(i);
  4232. glyph.advanceWidth = advanceWidth;
  4233. glyph.leftSideBearing = leftSideBearing;
  4234. }
  4235. }
  4236.  
  4237. function makeHmtxTable(glyphs) {
  4238. var t = new table.Table('hmtx', []);
  4239. for (var i = 0; i < glyphs.length; i += 1) {
  4240. var glyph = glyphs.get(i);
  4241. var advanceWidth = glyph.advanceWidth || 0;
  4242. var leftSideBearing = glyph.leftSideBearing || 0;
  4243. t.fields.push({name: 'advanceWidth_' + i, type: 'USHORT', value: advanceWidth});
  4244. t.fields.push({name: 'leftSideBearing_' + i, type: 'SHORT', value: leftSideBearing});
  4245. }
  4246.  
  4247. return t;
  4248. }
  4249.  
  4250. exports.parse = parseHmtxTable;
  4251. exports.make = makeHmtxTable;
  4252.  
  4253. },{"../parse":9,"../table":11}],20:[function(require,module,exports){
  4254. // The `kern` table contains kerning pairs.
  4255. // Note that some fonts use the GPOS OpenType layout table to specify kerning.
  4256. // https://www.microsoft.com/typography/OTSPEC/kern.htm
  4257.  
  4258. 'use strict';
  4259.  
  4260. var check = require('../check');
  4261. var parse = require('../parse');
  4262.  
  4263. // Parse the `kern` table which contains kerning pairs.
  4264. function parseKernTable(data, start) {
  4265. var pairs = {};
  4266. var p = new parse.Parser(data, start);
  4267. var tableVersion = p.parseUShort();
  4268. check.argument(tableVersion === 0, 'Unsupported kern table version.');
  4269. // Skip nTables.
  4270. p.skip('uShort', 1);
  4271. var subTableVersion = p.parseUShort();
  4272. check.argument(subTableVersion === 0, 'Unsupported kern sub-table version.');
  4273. // Skip subTableLength, subTableCoverage
  4274. p.skip('uShort', 2);
  4275. var nPairs = p.parseUShort();
  4276. // Skip searchRange, entrySelector, rangeShift.
  4277. p.skip('uShort', 3);
  4278. for (var i = 0; i < nPairs; i += 1) {
  4279. var leftIndex = p.parseUShort();
  4280. var rightIndex = p.parseUShort();
  4281. var value = p.parseShort();
  4282. pairs[leftIndex + ',' + rightIndex] = value;
  4283. }
  4284.  
  4285. return pairs;
  4286. }
  4287.  
  4288. exports.parse = parseKernTable;
  4289.  
  4290. },{"../check":2,"../parse":9}],21:[function(require,module,exports){
  4291. // The `loca` table stores the offsets to the locations of the glyphs in the font.
  4292. // https://www.microsoft.com/typography/OTSPEC/loca.htm
  4293.  
  4294. 'use strict';
  4295.  
  4296. var parse = require('../parse');
  4297.  
  4298. // Parse the `loca` table. This table stores the offsets to the locations of the glyphs in the font,
  4299. // relative to the beginning of the glyphData table.
  4300. // The number of glyphs stored in the `loca` table is specified in the `maxp` table (under numGlyphs)
  4301. // The loca table has two versions: a short version where offsets are stored as uShorts, and a long
  4302. // version where offsets are stored as uLongs. The `head` table specifies which version to use
  4303. // (under indexToLocFormat).
  4304. function parseLocaTable(data, start, numGlyphs, shortVersion) {
  4305. var p = new parse.Parser(data, start);
  4306. var parseFn = shortVersion ? p.parseUShort : p.parseULong;
  4307. // There is an extra entry after the last index element to compute the length of the last glyph.
  4308. // That's why we use numGlyphs + 1.
  4309. var glyphOffsets = [];
  4310. for (var i = 0; i < numGlyphs + 1; i += 1) {
  4311. var glyphOffset = parseFn.call(p);
  4312. if (shortVersion) {
  4313. // The short table version stores the actual offset divided by 2.
  4314. glyphOffset *= 2;
  4315. }
  4316.  
  4317. glyphOffsets.push(glyphOffset);
  4318. }
  4319.  
  4320. return glyphOffsets;
  4321. }
  4322.  
  4323. exports.parse = parseLocaTable;
  4324.  
  4325. },{"../parse":9}],22:[function(require,module,exports){
  4326. // The `ltag` table stores IETF BCP-47 language tags. It allows supporting
  4327. // languages for which TrueType does not assign a numeric code.
  4328. // https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6ltag.html
  4329. // http://www.w3.org/International/articles/language-tags/
  4330. // http://www.iana.org/assignments/language-subtag-registry/language-subtag-registry
  4331.  
  4332. 'use strict';
  4333.  
  4334. var check = require('../check');
  4335. var parse = require('../parse');
  4336. var table = require('../table');
  4337.  
  4338. function makeLtagTable(tags) {
  4339. var result = new table.Table('ltag', [
  4340. {name: 'version', type: 'ULONG', value: 1},
  4341. {name: 'flags', type: 'ULONG', value: 0},
  4342. {name: 'numTags', type: 'ULONG', value: tags.length}
  4343. ]);
  4344.  
  4345. var stringPool = '';
  4346. var stringPoolOffset = 12 + tags.length * 4;
  4347. for (var i = 0; i < tags.length; ++i) {
  4348. var pos = stringPool.indexOf(tags[i]);
  4349. if (pos < 0) {
  4350. pos = stringPool.length;
  4351. stringPool += tags[i];
  4352. }
  4353.  
  4354. result.fields.push({name: 'offset ' + i, type: 'USHORT', value: stringPoolOffset + pos});
  4355. result.fields.push({name: 'length ' + i, type: 'USHORT', value: tags[i].length});
  4356. }
  4357.  
  4358. result.fields.push({name: 'stringPool', type: 'CHARARRAY', value: stringPool});
  4359. return result;
  4360. }
  4361.  
  4362. function parseLtagTable(data, start) {
  4363. var p = new parse.Parser(data, start);
  4364. var tableVersion = p.parseULong();
  4365. check.argument(tableVersion === 1, 'Unsupported ltag table version.');
  4366. // The 'ltag' specification does not define any flags; skip the field.
  4367. p.skip('uLong', 1);
  4368. var numTags = p.parseULong();
  4369.  
  4370. var tags = [];
  4371. for (var i = 0; i < numTags; i++) {
  4372. var tag = '';
  4373. var offset = start + p.parseUShort();
  4374. var length = p.parseUShort();
  4375. for (var j = offset; j < offset + length; ++j) {
  4376. tag += String.fromCharCode(data.getInt8(j));
  4377. }
  4378.  
  4379. tags.push(tag);
  4380. }
  4381.  
  4382. return tags;
  4383. }
  4384.  
  4385. exports.make = makeLtagTable;
  4386. exports.parse = parseLtagTable;
  4387.  
  4388. },{"../check":2,"../parse":9,"../table":11}],23:[function(require,module,exports){
  4389. // The `maxp` table establishes the memory requirements for the font.
  4390. // We need it just to get the number of glyphs in the font.
  4391. // https://www.microsoft.com/typography/OTSPEC/maxp.htm
  4392.  
  4393. 'use strict';
  4394.  
  4395. var parse = require('../parse');
  4396. var table = require('../table');
  4397.  
  4398. // Parse the maximum profile `maxp` table.
  4399. function parseMaxpTable(data, start) {
  4400. var maxp = {};
  4401. var p = new parse.Parser(data, start);
  4402. maxp.version = p.parseVersion();
  4403. maxp.numGlyphs = p.parseUShort();
  4404. if (maxp.version === 1.0) {
  4405. maxp.maxPoints = p.parseUShort();
  4406. maxp.maxContours = p.parseUShort();
  4407. maxp.maxCompositePoints = p.parseUShort();
  4408. maxp.maxCompositeContours = p.parseUShort();
  4409. maxp.maxZones = p.parseUShort();
  4410. maxp.maxTwilightPoints = p.parseUShort();
  4411. maxp.maxStorage = p.parseUShort();
  4412. maxp.maxFunctionDefs = p.parseUShort();
  4413. maxp.maxInstructionDefs = p.parseUShort();
  4414. maxp.maxStackElements = p.parseUShort();
  4415. maxp.maxSizeOfInstructions = p.parseUShort();
  4416. maxp.maxComponentElements = p.parseUShort();
  4417. maxp.maxComponentDepth = p.parseUShort();
  4418. }
  4419.  
  4420. return maxp;
  4421. }
  4422.  
  4423. function makeMaxpTable(numGlyphs) {
  4424. return new table.Table('maxp', [
  4425. {name: 'version', type: 'FIXED', value: 0x00005000},
  4426. {name: 'numGlyphs', type: 'USHORT', value: numGlyphs}
  4427. ]);
  4428. }
  4429.  
  4430. exports.parse = parseMaxpTable;
  4431. exports.make = makeMaxpTable;
  4432.  
  4433. },{"../parse":9,"../table":11}],24:[function(require,module,exports){
  4434. // The `name` naming table.
  4435. // https://www.microsoft.com/typography/OTSPEC/name.htm
  4436.  
  4437. 'use strict';
  4438.  
  4439. var types = require('../types');
  4440. var decode = types.decode;
  4441. var encode = types.encode;
  4442. var parse = require('../parse');
  4443. var table = require('../table');
  4444.  
  4445. // NameIDs for the name table.
  4446. var nameTableNames = [
  4447. 'copyright', // 0
  4448. 'fontFamily', // 1
  4449. 'fontSubfamily', // 2
  4450. 'uniqueID', // 3
  4451. 'fullName', // 4
  4452. 'version', // 5
  4453. 'postScriptName', // 6
  4454. 'trademark', // 7
  4455. 'manufacturer', // 8
  4456. 'designer', // 9
  4457. 'description', // 10
  4458. 'manufacturerURL', // 11
  4459. 'designerURL', // 12
  4460. 'license', // 13
  4461. 'licenseURL', // 14
  4462. 'reserved', // 15
  4463. 'preferredFamily', // 16
  4464. 'preferredSubfamily', // 17
  4465. 'compatibleFullName', // 18
  4466. 'sampleText', // 19
  4467. 'postScriptFindFontName', // 20
  4468. 'wwsFamily', // 21
  4469. 'wwsSubfamily' // 22
  4470. ];
  4471.  
  4472. var macLanguages = {
  4473. 0: 'en',
  4474. 1: 'fr',
  4475. 2: 'de',
  4476. 3: 'it',
  4477. 4: 'nl',
  4478. 5: 'sv',
  4479. 6: 'es',
  4480. 7: 'da',
  4481. 8: 'pt',
  4482. 9: 'no',
  4483. 10: 'he',
  4484. 11: 'ja',
  4485. 12: 'ar',
  4486. 13: 'fi',
  4487. 14: 'el',
  4488. 15: 'is',
  4489. 16: 'mt',
  4490. 17: 'tr',
  4491. 18: 'hr',
  4492. 19: 'zh-Hant',
  4493. 20: 'ur',
  4494. 21: 'hi',
  4495. 22: 'th',
  4496. 23: 'ko',
  4497. 24: 'lt',
  4498. 25: 'pl',
  4499. 26: 'hu',
  4500. 27: 'es',
  4501. 28: 'lv',
  4502. 29: 'se',
  4503. 30: 'fo',
  4504. 31: 'fa',
  4505. 32: 'ru',
  4506. 33: 'zh',
  4507. 34: 'nl-BE',
  4508. 35: 'ga',
  4509. 36: 'sq',
  4510. 37: 'ro',
  4511. 38: 'cz',
  4512. 39: 'sk',
  4513. 40: 'si',
  4514. 41: 'yi',
  4515. 42: 'sr',
  4516. 43: 'mk',
  4517. 44: 'bg',
  4518. 45: 'uk',
  4519. 46: 'be',
  4520. 47: 'uz',
  4521. 48: 'kk',
  4522. 49: 'az-Cyrl',
  4523. 50: 'az-Arab',
  4524. 51: 'hy',
  4525. 52: 'ka',
  4526. 53: 'mo',
  4527. 54: 'ky',
  4528. 55: 'tg',
  4529. 56: 'tk',
  4530. 57: 'mn-CN',
  4531. 58: 'mn',
  4532. 59: 'ps',
  4533. 60: 'ks',
  4534. 61: 'ku',
  4535. 62: 'sd',
  4536. 63: 'bo',
  4537. 64: 'ne',
  4538. 65: 'sa',
  4539. 66: 'mr',
  4540. 67: 'bn',
  4541. 68: 'as',
  4542. 69: 'gu',
  4543. 70: 'pa',
  4544. 71: 'or',
  4545. 72: 'ml',
  4546. 73: 'kn',
  4547. 74: 'ta',
  4548. 75: 'te',
  4549. 76: 'si',
  4550. 77: 'my',
  4551. 78: 'km',
  4552. 79: 'lo',
  4553. 80: 'vi',
  4554. 81: 'id',
  4555. 82: 'tl',
  4556. 83: 'ms',
  4557. 84: 'ms-Arab',
  4558. 85: 'am',
  4559. 86: 'ti',
  4560. 87: 'om',
  4561. 88: 'so',
  4562. 89: 'sw',
  4563. 90: 'rw',
  4564. 91: 'rn',
  4565. 92: 'ny',
  4566. 93: 'mg',
  4567. 94: 'eo',
  4568. 128: 'cy',
  4569. 129: 'eu',
  4570. 130: 'ca',
  4571. 131: 'la',
  4572. 132: 'qu',
  4573. 133: 'gn',
  4574. 134: 'ay',
  4575. 135: 'tt',
  4576. 136: 'ug',
  4577. 137: 'dz',
  4578. 138: 'jv',
  4579. 139: 'su',
  4580. 140: 'gl',
  4581. 141: 'af',
  4582. 142: 'br',
  4583. 143: 'iu',
  4584. 144: 'gd',
  4585. 145: 'gv',
  4586. 146: 'ga',
  4587. 147: 'to',
  4588. 148: 'el-polyton',
  4589. 149: 'kl',
  4590. 150: 'az',
  4591. 151: 'nn'
  4592. };
  4593.  
  4594. // MacOS language ID → MacOS script ID
  4595. //
  4596. // Note that the script ID is not sufficient to determine what encoding
  4597. // to use in TrueType files. For some languages, MacOS used a modification
  4598. // of a mainstream script. For example, an Icelandic name would be stored
  4599. // with smRoman in the TrueType naming table, but the actual encoding
  4600. // is a special Icelandic version of the normal Macintosh Roman encoding.
  4601. // As another example, Inuktitut uses an 8-bit encoding for Canadian Aboriginal
  4602. // Syllables but MacOS had run out of available script codes, so this was
  4603. // done as a (pretty radical) "modification" of Ethiopic.
  4604. //
  4605. // http://unicode.org/Public/MAPPINGS/VENDORS/APPLE/Readme.txt
  4606. var macLanguageToScript = {
  4607. 0: 0, // langEnglish → smRoman
  4608. 1: 0, // langFrench → smRoman
  4609. 2: 0, // langGerman → smRoman
  4610. 3: 0, // langItalian → smRoman
  4611. 4: 0, // langDutch → smRoman
  4612. 5: 0, // langSwedish → smRoman
  4613. 6: 0, // langSpanish → smRoman
  4614. 7: 0, // langDanish → smRoman
  4615. 8: 0, // langPortuguese → smRoman
  4616. 9: 0, // langNorwegian → smRoman
  4617. 10: 5, // langHebrew → smHebrew
  4618. 11: 1, // langJapanese → smJapanese
  4619. 12: 4, // langArabic → smArabic
  4620. 13: 0, // langFinnish → smRoman
  4621. 14: 6, // langGreek → smGreek
  4622. 15: 0, // langIcelandic → smRoman (modified)
  4623. 16: 0, // langMaltese → smRoman
  4624. 17: 0, // langTurkish → smRoman (modified)
  4625. 18: 0, // langCroatian → smRoman (modified)
  4626. 19: 2, // langTradChinese → smTradChinese
  4627. 20: 4, // langUrdu → smArabic
  4628. 21: 9, // langHindi → smDevanagari
  4629. 22: 21, // langThai → smThai
  4630. 23: 3, // langKorean → smKorean
  4631. 24: 29, // langLithuanian → smCentralEuroRoman
  4632. 25: 29, // langPolish → smCentralEuroRoman
  4633. 26: 29, // langHungarian → smCentralEuroRoman
  4634. 27: 29, // langEstonian → smCentralEuroRoman
  4635. 28: 29, // langLatvian → smCentralEuroRoman
  4636. 29: 0, // langSami → smRoman
  4637. 30: 0, // langFaroese → smRoman (modified)
  4638. 31: 4, // langFarsi → smArabic (modified)
  4639. 32: 7, // langRussian → smCyrillic
  4640. 33: 25, // langSimpChinese → smSimpChinese
  4641. 34: 0, // langFlemish → smRoman
  4642. 35: 0, // langIrishGaelic → smRoman (modified)
  4643. 36: 0, // langAlbanian → smRoman
  4644. 37: 0, // langRomanian → smRoman (modified)
  4645. 38: 29, // langCzech → smCentralEuroRoman
  4646. 39: 29, // langSlovak → smCentralEuroRoman
  4647. 40: 0, // langSlovenian → smRoman (modified)
  4648. 41: 5, // langYiddish → smHebrew
  4649. 42: 7, // langSerbian → smCyrillic
  4650. 43: 7, // langMacedonian → smCyrillic
  4651. 44: 7, // langBulgarian → smCyrillic
  4652. 45: 7, // langUkrainian → smCyrillic (modified)
  4653. 46: 7, // langByelorussian → smCyrillic
  4654. 47: 7, // langUzbek → smCyrillic
  4655. 48: 7, // langKazakh → smCyrillic
  4656. 49: 7, // langAzerbaijani → smCyrillic
  4657. 50: 4, // langAzerbaijanAr → smArabic
  4658. 51: 24, // langArmenian → smArmenian
  4659. 52: 23, // langGeorgian → smGeorgian
  4660. 53: 7, // langMoldavian → smCyrillic
  4661. 54: 7, // langKirghiz → smCyrillic
  4662. 55: 7, // langTajiki → smCyrillic
  4663. 56: 7, // langTurkmen → smCyrillic
  4664. 57: 27, // langMongolian → smMongolian
  4665. 58: 7, // langMongolianCyr → smCyrillic
  4666. 59: 4, // langPashto → smArabic
  4667. 60: 4, // langKurdish → smArabic
  4668. 61: 4, // langKashmiri → smArabic
  4669. 62: 4, // langSindhi → smArabic
  4670. 63: 26, // langTibetan → smTibetan
  4671. 64: 9, // langNepali → smDevanagari
  4672. 65: 9, // langSanskrit → smDevanagari
  4673. 66: 9, // langMarathi → smDevanagari
  4674. 67: 13, // langBengali → smBengali
  4675. 68: 13, // langAssamese → smBengali
  4676. 69: 11, // langGujarati → smGujarati
  4677. 70: 10, // langPunjabi → smGurmukhi
  4678. 71: 12, // langOriya → smOriya
  4679. 72: 17, // langMalayalam → smMalayalam
  4680. 73: 16, // langKannada → smKannada
  4681. 74: 14, // langTamil → smTamil
  4682. 75: 15, // langTelugu → smTelugu
  4683. 76: 18, // langSinhalese → smSinhalese
  4684. 77: 19, // langBurmese → smBurmese
  4685. 78: 20, // langKhmer → smKhmer
  4686. 79: 22, // langLao → smLao
  4687. 80: 30, // langVietnamese → smVietnamese
  4688. 81: 0, // langIndonesian → smRoman
  4689. 82: 0, // langTagalog → smRoman
  4690. 83: 0, // langMalayRoman → smRoman
  4691. 84: 4, // langMalayArabic → smArabic
  4692. 85: 28, // langAmharic → smEthiopic
  4693. 86: 28, // langTigrinya → smEthiopic
  4694. 87: 28, // langOromo → smEthiopic
  4695. 88: 0, // langSomali → smRoman
  4696. 89: 0, // langSwahili → smRoman
  4697. 90: 0, // langKinyarwanda → smRoman
  4698. 91: 0, // langRundi → smRoman
  4699. 92: 0, // langNyanja → smRoman
  4700. 93: 0, // langMalagasy → smRoman
  4701. 94: 0, // langEsperanto → smRoman
  4702. 128: 0, // langWelsh → smRoman (modified)
  4703. 129: 0, // langBasque → smRoman
  4704. 130: 0, // langCatalan → smRoman
  4705. 131: 0, // langLatin → smRoman
  4706. 132: 0, // langQuechua → smRoman
  4707. 133: 0, // langGuarani → smRoman
  4708. 134: 0, // langAymara → smRoman
  4709. 135: 7, // langTatar → smCyrillic
  4710. 136: 4, // langUighur → smArabic
  4711. 137: 26, // langDzongkha → smTibetan
  4712. 138: 0, // langJavaneseRom → smRoman
  4713. 139: 0, // langSundaneseRom → smRoman
  4714. 140: 0, // langGalician → smRoman
  4715. 141: 0, // langAfrikaans → smRoman
  4716. 142: 0, // langBreton → smRoman (modified)
  4717. 143: 28, // langInuktitut → smEthiopic (modified)
  4718. 144: 0, // langScottishGaelic → smRoman (modified)
  4719. 145: 0, // langManxGaelic → smRoman (modified)
  4720. 146: 0, // langIrishGaelicScript → smRoman (modified)
  4721. 147: 0, // langTongan → smRoman
  4722. 148: 6, // langGreekAncient → smRoman
  4723. 149: 0, // langGreenlandic → smRoman
  4724. 150: 0, // langAzerbaijanRoman → smRoman
  4725. 151: 0 // langNynorsk → smRoman
  4726. };
  4727.  
  4728. // While Microsoft indicates a region/country for all its language
  4729. // IDs, we omit the region code if it's equal to the "most likely
  4730. // region subtag" according to Unicode CLDR. For scripts, we omit
  4731. // the subtag if it is equal to the Suppress-Script entry in the
  4732. // IANA language subtag registry for IETF BCP 47.
  4733. //
  4734. // For example, Microsoft states that its language code 0x041A is
  4735. // Croatian in Croatia. We transform this to the BCP 47 language code 'hr'
  4736. // and not 'hr-HR' because Croatia is the default country for Croatian,
  4737. // according to Unicode CLDR. As another example, Microsoft states
  4738. // that 0x101A is Croatian (Latin) in Bosnia-Herzegovina. We transform
  4739. // this to 'hr-BA' and not 'hr-Latn-BA' because Latin is the default script
  4740. // for the Croatian language, according to IANA.
  4741. //
  4742. // http://www.unicode.org/cldr/charts/latest/supplemental/likely_subtags.html
  4743. // http://www.iana.org/assignments/language-subtag-registry/language-subtag-registry
  4744. var windowsLanguages = {
  4745. 0x0436: 'af',
  4746. 0x041C: 'sq',
  4747. 0x0484: 'gsw',
  4748. 0x045E: 'am',
  4749. 0x1401: 'ar-DZ',
  4750. 0x3C01: 'ar-BH',
  4751. 0x0C01: 'ar',
  4752. 0x0801: 'ar-IQ',
  4753. 0x2C01: 'ar-JO',
  4754. 0x3401: 'ar-KW',
  4755. 0x3001: 'ar-LB',
  4756. 0x1001: 'ar-LY',
  4757. 0x1801: 'ary',
  4758. 0x2001: 'ar-OM',
  4759. 0x4001: 'ar-QA',
  4760. 0x0401: 'ar-SA',
  4761. 0x2801: 'ar-SY',
  4762. 0x1C01: 'aeb',
  4763. 0x3801: 'ar-AE',
  4764. 0x2401: 'ar-YE',
  4765. 0x042B: 'hy',
  4766. 0x044D: 'as',
  4767. 0x082C: 'az-Cyrl',
  4768. 0x042C: 'az',
  4769. 0x046D: 'ba',
  4770. 0x042D: 'eu',
  4771. 0x0423: 'be',
  4772. 0x0845: 'bn',
  4773. 0x0445: 'bn-IN',
  4774. 0x201A: 'bs-Cyrl',
  4775. 0x141A: 'bs',
  4776. 0x047E: 'br',
  4777. 0x0402: 'bg',
  4778. 0x0403: 'ca',
  4779. 0x0C04: 'zh-HK',
  4780. 0x1404: 'zh-MO',
  4781. 0x0804: 'zh',
  4782. 0x1004: 'zh-SG',
  4783. 0x0404: 'zh-TW',
  4784. 0x0483: 'co',
  4785. 0x041A: 'hr',
  4786. 0x101A: 'hr-BA',
  4787. 0x0405: 'cs',
  4788. 0x0406: 'da',
  4789. 0x048C: 'prs',
  4790. 0x0465: 'dv',
  4791. 0x0813: 'nl-BE',
  4792. 0x0413: 'nl',
  4793. 0x0C09: 'en-AU',
  4794. 0x2809: 'en-BZ',
  4795. 0x1009: 'en-CA',
  4796. 0x2409: 'en-029',
  4797. 0x4009: 'en-IN',
  4798. 0x1809: 'en-IE',
  4799. 0x2009: 'en-JM',
  4800. 0x4409: 'en-MY',
  4801. 0x1409: 'en-NZ',
  4802. 0x3409: 'en-PH',
  4803. 0x4809: 'en-SG',
  4804. 0x1C09: 'en-ZA',
  4805. 0x2C09: 'en-TT',
  4806. 0x0809: 'en-GB',
  4807. 0x0409: 'en',
  4808. 0x3009: 'en-ZW',
  4809. 0x0425: 'et',
  4810. 0x0438: 'fo',
  4811. 0x0464: 'fil',
  4812. 0x040B: 'fi',
  4813. 0x080C: 'fr-BE',
  4814. 0x0C0C: 'fr-CA',
  4815. 0x040C: 'fr',
  4816. 0x140C: 'fr-LU',
  4817. 0x180C: 'fr-MC',
  4818. 0x100C: 'fr-CH',
  4819. 0x0462: 'fy',
  4820. 0x0456: 'gl',
  4821. 0x0437: 'ka',
  4822. 0x0C07: 'de-AT',
  4823. 0x0407: 'de',
  4824. 0x1407: 'de-LI',
  4825. 0x1007: 'de-LU',
  4826. 0x0807: 'de-CH',
  4827. 0x0408: 'el',
  4828. 0x046F: 'kl',
  4829. 0x0447: 'gu',
  4830. 0x0468: 'ha',
  4831. 0x040D: 'he',
  4832. 0x0439: 'hi',
  4833. 0x040E: 'hu',
  4834. 0x040F: 'is',
  4835. 0x0470: 'ig',
  4836. 0x0421: 'id',
  4837. 0x045D: 'iu',
  4838. 0x085D: 'iu-Latn',
  4839. 0x083C: 'ga',
  4840. 0x0434: 'xh',
  4841. 0x0435: 'zu',
  4842. 0x0410: 'it',
  4843. 0x0810: 'it-CH',
  4844. 0x0411: 'ja',
  4845. 0x044B: 'kn',
  4846. 0x043F: 'kk',
  4847. 0x0453: 'km',
  4848. 0x0486: 'quc',
  4849. 0x0487: 'rw',
  4850. 0x0441: 'sw',
  4851. 0x0457: 'kok',
  4852. 0x0412: 'ko',
  4853. 0x0440: 'ky',
  4854. 0x0454: 'lo',
  4855. 0x0426: 'lv',
  4856. 0x0427: 'lt',
  4857. 0x082E: 'dsb',
  4858. 0x046E: 'lb',
  4859. 0x042F: 'mk',
  4860. 0x083E: 'ms-BN',
  4861. 0x043E: 'ms',
  4862. 0x044C: 'ml',
  4863. 0x043A: 'mt',
  4864. 0x0481: 'mi',
  4865. 0x047A: 'arn',
  4866. 0x044E: 'mr',
  4867. 0x047C: 'moh',
  4868. 0x0450: 'mn',
  4869. 0x0850: 'mn-CN',
  4870. 0x0461: 'ne',
  4871. 0x0414: 'nb',
  4872. 0x0814: 'nn',
  4873. 0x0482: 'oc',
  4874. 0x0448: 'or',
  4875. 0x0463: 'ps',
  4876. 0x0415: 'pl',
  4877. 0x0416: 'pt',
  4878. 0x0816: 'pt-PT',
  4879. 0x0446: 'pa',
  4880. 0x046B: 'qu-BO',
  4881. 0x086B: 'qu-EC',
  4882. 0x0C6B: 'qu',
  4883. 0x0418: 'ro',
  4884. 0x0417: 'rm',
  4885. 0x0419: 'ru',
  4886. 0x243B: 'smn',
  4887. 0x103B: 'smj-NO',
  4888. 0x143B: 'smj',
  4889. 0x0C3B: 'se-FI',
  4890. 0x043B: 'se',
  4891. 0x083B: 'se-SE',
  4892. 0x203B: 'sms',
  4893. 0x183B: 'sma-NO',
  4894. 0x1C3B: 'sms',
  4895. 0x044F: 'sa',
  4896. 0x1C1A: 'sr-Cyrl-BA',
  4897. 0x0C1A: 'sr',
  4898. 0x181A: 'sr-Latn-BA',
  4899. 0x081A: 'sr-Latn',
  4900. 0x046C: 'nso',
  4901. 0x0432: 'tn',
  4902. 0x045B: 'si',
  4903. 0x041B: 'sk',
  4904. 0x0424: 'sl',
  4905. 0x2C0A: 'es-AR',
  4906. 0x400A: 'es-BO',
  4907. 0x340A: 'es-CL',
  4908. 0x240A: 'es-CO',
  4909. 0x140A: 'es-CR',
  4910. 0x1C0A: 'es-DO',
  4911. 0x300A: 'es-EC',
  4912. 0x440A: 'es-SV',
  4913. 0x100A: 'es-GT',
  4914. 0x480A: 'es-HN',
  4915. 0x080A: 'es-MX',
  4916. 0x4C0A: 'es-NI',
  4917. 0x180A: 'es-PA',
  4918. 0x3C0A: 'es-PY',
  4919. 0x280A: 'es-PE',
  4920. 0x500A: 'es-PR',
  4921.  
  4922. // Microsoft has defined two different language codes for
  4923. // “Spanish with modern sorting” and “Spanish with traditional
  4924. // sorting”. This makes sense for collation APIs, and it would be
  4925. // possible to express this in BCP 47 language tags via Unicode
  4926. // extensions (eg., es-u-co-trad is Spanish with traditional
  4927. // sorting). However, for storing names in fonts, the distinction
  4928. // does not make sense, so we give “es” in both cases.
  4929. 0x0C0A: 'es',
  4930. 0x040A: 'es',
  4931.  
  4932. 0x540A: 'es-US',
  4933. 0x380A: 'es-UY',
  4934. 0x200A: 'es-VE',
  4935. 0x081D: 'sv-FI',
  4936. 0x041D: 'sv',
  4937. 0x045A: 'syr',
  4938. 0x0428: 'tg',
  4939. 0x085F: 'tzm',
  4940. 0x0449: 'ta',
  4941. 0x0444: 'tt',
  4942. 0x044A: 'te',
  4943. 0x041E: 'th',
  4944. 0x0451: 'bo',
  4945. 0x041F: 'tr',
  4946. 0x0442: 'tk',
  4947. 0x0480: 'ug',
  4948. 0x0422: 'uk',
  4949. 0x042E: 'hsb',
  4950. 0x0420: 'ur',
  4951. 0x0843: 'uz-Cyrl',
  4952. 0x0443: 'uz',
  4953. 0x042A: 'vi',
  4954. 0x0452: 'cy',
  4955. 0x0488: 'wo',
  4956. 0x0485: 'sah',
  4957. 0x0478: 'ii',
  4958. 0x046A: 'yo'
  4959. };
  4960.  
  4961. // Returns a IETF BCP 47 language code, for example 'zh-Hant'
  4962. // for 'Chinese in the traditional script'.
  4963. function getLanguageCode(platformID, languageID, ltag) {
  4964. switch (platformID) {
  4965. case 0: // Unicode
  4966. if (languageID === 0xFFFF) {
  4967. return 'und';
  4968. } else if (ltag) {
  4969. return ltag[languageID];
  4970. }
  4971.  
  4972. break;
  4973.  
  4974. case 1: // Macintosh
  4975. return macLanguages[languageID];
  4976.  
  4977. case 3: // Windows
  4978. return windowsLanguages[languageID];
  4979. }
  4980.  
  4981. return undefined;
  4982. }
  4983.  
  4984. var utf16 = 'utf-16';
  4985.  
  4986. // MacOS script ID → encoding. This table stores the default case,
  4987. // which can be overridden by macLanguageEncodings.
  4988. var macScriptEncodings = {
  4989. 0: 'macintosh', // smRoman
  4990. 1: 'x-mac-japanese', // smJapanese
  4991. 2: 'x-mac-chinesetrad', // smTradChinese
  4992. 3: 'x-mac-korean', // smKorean
  4993. 6: 'x-mac-greek', // smGreek
  4994. 7: 'x-mac-cyrillic', // smCyrillic
  4995. 9: 'x-mac-devanagai', // smDevanagari
  4996. 10: 'x-mac-gurmukhi', // smGurmukhi
  4997. 11: 'x-mac-gujarati', // smGujarati
  4998. 12: 'x-mac-oriya', // smOriya
  4999. 13: 'x-mac-bengali', // smBengali
  5000. 14: 'x-mac-tamil', // smTamil
  5001. 15: 'x-mac-telugu', // smTelugu
  5002. 16: 'x-mac-kannada', // smKannada
  5003. 17: 'x-mac-malayalam', // smMalayalam
  5004. 18: 'x-mac-sinhalese', // smSinhalese
  5005. 19: 'x-mac-burmese', // smBurmese
  5006. 20: 'x-mac-khmer', // smKhmer
  5007. 21: 'x-mac-thai', // smThai
  5008. 22: 'x-mac-lao', // smLao
  5009. 23: 'x-mac-georgian', // smGeorgian
  5010. 24: 'x-mac-armenian', // smArmenian
  5011. 25: 'x-mac-chinesesimp', // smSimpChinese
  5012. 26: 'x-mac-tibetan', // smTibetan
  5013. 27: 'x-mac-mongolian', // smMongolian
  5014. 28: 'x-mac-ethiopic', // smEthiopic
  5015. 29: 'x-mac-ce', // smCentralEuroRoman
  5016. 30: 'x-mac-vietnamese', // smVietnamese
  5017. 31: 'x-mac-extarabic' // smExtArabic
  5018. };
  5019.  
  5020. // MacOS language ID → encoding. This table stores the exceptional
  5021. // cases, which override macScriptEncodings. For writing MacOS naming
  5022. // tables, we need to emit a MacOS script ID. Therefore, we cannot
  5023. // merge macScriptEncodings into macLanguageEncodings.
  5024. //
  5025. // http://unicode.org/Public/MAPPINGS/VENDORS/APPLE/Readme.txt
  5026. var macLanguageEncodings = {
  5027. 15: 'x-mac-icelandic', // langIcelandic
  5028. 17: 'x-mac-turkish', // langTurkish
  5029. 18: 'x-mac-croatian', // langCroatian
  5030. 24: 'x-mac-ce', // langLithuanian
  5031. 25: 'x-mac-ce', // langPolish
  5032. 26: 'x-mac-ce', // langHungarian
  5033. 27: 'x-mac-ce', // langEstonian
  5034. 28: 'x-mac-ce', // langLatvian
  5035. 30: 'x-mac-icelandic', // langFaroese
  5036. 37: 'x-mac-romanian', // langRomanian
  5037. 38: 'x-mac-ce', // langCzech
  5038. 39: 'x-mac-ce', // langSlovak
  5039. 40: 'x-mac-ce', // langSlovenian
  5040. 143: 'x-mac-inuit', // langInuktitut
  5041. 146: 'x-mac-gaelic' // langIrishGaelicScript
  5042. };
  5043.  
  5044. function getEncoding(platformID, encodingID, languageID) {
  5045. switch (platformID) {
  5046. case 0: // Unicode
  5047. return utf16;
  5048.  
  5049. case 1: // Apple Macintosh
  5050. return macLanguageEncodings[languageID] || macScriptEncodings[encodingID];
  5051.  
  5052. case 3: // Microsoft Windows
  5053. if (encodingID === 1 || encodingID === 10) {
  5054. return utf16;
  5055. }
  5056.  
  5057. break;
  5058. }
  5059.  
  5060. return undefined;
  5061. }
  5062.  
  5063. // Parse the naming `name` table.
  5064. // FIXME: Format 1 additional fields are not supported yet.
  5065. // ltag is the content of the `ltag' table, such as ['en', 'zh-Hans', 'de-CH-1904'].
  5066. function parseNameTable(data, start, ltag) {
  5067. var name = {};
  5068. var p = new parse.Parser(data, start);
  5069. var format = p.parseUShort();
  5070. var count = p.parseUShort();
  5071. var stringOffset = p.offset + p.parseUShort();
  5072. for (var i = 0; i < count; i++) {
  5073. var platformID = p.parseUShort();
  5074. var encodingID = p.parseUShort();
  5075. var languageID = p.parseUShort();
  5076. var nameID = p.parseUShort();
  5077. var property = nameTableNames[nameID] || nameID;
  5078. var byteLength = p.parseUShort();
  5079. var offset = p.parseUShort();
  5080. var language = getLanguageCode(platformID, languageID, ltag);
  5081. var encoding = getEncoding(platformID, encodingID, languageID);
  5082. if (encoding !== undefined && language !== undefined) {
  5083. var text;
  5084. if (encoding === utf16) {
  5085. text = decode.UTF16(data, stringOffset + offset, byteLength);
  5086. } else {
  5087. text = decode.MACSTRING(data, stringOffset + offset, byteLength, encoding);
  5088. }
  5089.  
  5090. if (text) {
  5091. var translations = name[property];
  5092. if (translations === undefined) {
  5093. translations = name[property] = {};
  5094. }
  5095.  
  5096. translations[language] = text;
  5097. }
  5098. }
  5099. }
  5100.  
  5101. var langTagCount = 0;
  5102. if (format === 1) {
  5103. // FIXME: Also handle Microsoft's 'name' table 1.
  5104. langTagCount = p.parseUShort();
  5105. }
  5106.  
  5107. return name;
  5108. }
  5109.  
  5110. // {23: 'foo'} → {'foo': 23}
  5111. // ['bar', 'baz'] → {'bar': 0, 'baz': 1}
  5112. function reverseDict(dict) {
  5113. var result = {};
  5114. for (var key in dict) {
  5115. result[dict[key]] = parseInt(key);
  5116. }
  5117.  
  5118. return result;
  5119. }
  5120.  
  5121. function makeNameRecord(platformID, encodingID, languageID, nameID, length, offset) {
  5122. return new table.Table('NameRecord', [
  5123. {name: 'platformID', type: 'USHORT', value: platformID},
  5124. {name: 'encodingID', type: 'USHORT', value: encodingID},
  5125. {name: 'languageID', type: 'USHORT', value: languageID},
  5126. {name: 'nameID', type: 'USHORT', value: nameID},
  5127. {name: 'length', type: 'USHORT', value: length},
  5128. {name: 'offset', type: 'USHORT', value: offset}
  5129. ]);
  5130. }
  5131.  
  5132. // Finds the position of needle in haystack, or -1 if not there.
  5133. // Like String.indexOf(), but for arrays.
  5134. function findSubArray(needle, haystack) {
  5135. var needleLength = needle.length;
  5136. var limit = haystack.length - needleLength + 1;
  5137.  
  5138. loop:
  5139. for (var pos = 0; pos < limit; pos++) {
  5140. for (; pos < limit; pos++) {
  5141. for (var k = 0; k < needleLength; k++) {
  5142. if (haystack[pos + k] !== needle[k]) {
  5143. continue loop;
  5144. }
  5145. }
  5146.  
  5147. return pos;
  5148. }
  5149. }
  5150.  
  5151. return -1;
  5152. }
  5153.  
  5154. function addStringToPool(s, pool) {
  5155. var offset = findSubArray(s, pool);
  5156. if (offset < 0) {
  5157. offset = pool.length;
  5158. for (var i = 0, len = s.length; i < len; ++i) {
  5159. pool.push(s[i]);
  5160. }
  5161.  
  5162. }
  5163.  
  5164. return offset;
  5165. }
  5166.  
  5167. function makeNameTable(names, ltag) {
  5168. var nameID;
  5169. var nameIDs = [];
  5170.  
  5171. var namesWithNumericKeys = {};
  5172. var nameTableIds = reverseDict(nameTableNames);
  5173. for (var key in names) {
  5174. var id = nameTableIds[key];
  5175. if (id === undefined) {
  5176. id = key;
  5177. }
  5178.  
  5179. nameID = parseInt(id);
  5180. namesWithNumericKeys[nameID] = names[key];
  5181. nameIDs.push(nameID);
  5182. }
  5183.  
  5184. var macLanguageIds = reverseDict(macLanguages);
  5185. var windowsLanguageIds = reverseDict(windowsLanguages);
  5186.  
  5187. var nameRecords = [];
  5188. var stringPool = [];
  5189.  
  5190. for (var i = 0; i < nameIDs.length; i++) {
  5191. nameID = nameIDs[i];
  5192. var translations = namesWithNumericKeys[nameID];
  5193. for (var lang in translations) {
  5194. var text = translations[lang];
  5195.  
  5196. // For MacOS, we try to emit the name in the form that was introduced
  5197. // in the initial version of the TrueType spec (in the late 1980s).
  5198. // However, this can fail for various reasons: the requested BCP 47
  5199. // language code might not have an old-style Mac equivalent;
  5200. // we might not have a codec for the needed character encoding;
  5201. // or the name might contain characters that cannot be expressed
  5202. // in the old-style Macintosh encoding. In case of failure, we emit
  5203. // the name in a more modern fashion (Unicode encoding with BCP 47
  5204. // language tags) that is recognized by MacOS 10.5, released in 2009.
  5205. // If fonts were only read by operating systems, we could simply
  5206. // emit all names in the modern form; this would be much easier.
  5207. // However, there are many applications and libraries that read
  5208. // 'name' tables directly, and these will usually only recognize
  5209. // the ancient form (silently skipping the unrecognized names).
  5210. var macPlatform = 1; // Macintosh
  5211. var macLanguage = macLanguageIds[lang];
  5212. var macScript = macLanguageToScript[macLanguage];
  5213. var macEncoding = getEncoding(macPlatform, macScript, macLanguage);
  5214. var macName = encode.MACSTRING(text, macEncoding);
  5215. if (macName === undefined) {
  5216. macPlatform = 0; // Unicode
  5217. macLanguage = ltag.indexOf(lang);
  5218. if (macLanguage < 0) {
  5219. macLanguage = ltag.length;
  5220. ltag.push(lang);
  5221. }
  5222.  
  5223. macScript = 4; // Unicode 2.0 and later
  5224. macName = encode.UTF16(text);
  5225. }
  5226.  
  5227. var macNameOffset = addStringToPool(macName, stringPool);
  5228. nameRecords.push(makeNameRecord(macPlatform, macScript, macLanguage,
  5229. nameID, macName.length, macNameOffset));
  5230.  
  5231. var winLanguage = windowsLanguageIds[lang];
  5232. if (winLanguage !== undefined) {
  5233. var winName = encode.UTF16(text);
  5234. var winNameOffset = addStringToPool(winName, stringPool);
  5235. nameRecords.push(makeNameRecord(3, 1, winLanguage,
  5236. nameID, winName.length, winNameOffset));
  5237. }
  5238. }
  5239. }
  5240.  
  5241. nameRecords.sort(function(a, b) {
  5242. return ((a.platformID - b.platformID) ||
  5243. (a.encodingID - b.encodingID) ||
  5244. (a.languageID - b.languageID) ||
  5245. (a.nameID - b.nameID));
  5246. });
  5247.  
  5248. var t = new table.Table('name', [
  5249. {name: 'format', type: 'USHORT', value: 0},
  5250. {name: 'count', type: 'USHORT', value: nameRecords.length},
  5251. {name: 'stringOffset', type: 'USHORT', value: 6 + nameRecords.length * 12}
  5252. ]);
  5253.  
  5254. for (var r = 0; r < nameRecords.length; r++) {
  5255. t.fields.push({name: 'record_' + r, type: 'TABLE', value: nameRecords[r]});
  5256. }
  5257.  
  5258. t.fields.push({name: 'strings', type: 'LITERAL', value: stringPool});
  5259. return t;
  5260. }
  5261.  
  5262. exports.parse = parseNameTable;
  5263. exports.make = makeNameTable;
  5264.  
  5265. },{"../parse":9,"../table":11,"../types":28}],25:[function(require,module,exports){
  5266. // The `OS/2` table contains metrics required in OpenType fonts.
  5267. // https://www.microsoft.com/typography/OTSPEC/os2.htm
  5268.  
  5269. 'use strict';
  5270.  
  5271. var parse = require('../parse');
  5272. var table = require('../table');
  5273.  
  5274. var unicodeRanges = [
  5275. {begin: 0x0000, end: 0x007F}, // Basic Latin
  5276. {begin: 0x0080, end: 0x00FF}, // Latin-1 Supplement
  5277. {begin: 0x0100, end: 0x017F}, // Latin Extended-A
  5278. {begin: 0x0180, end: 0x024F}, // Latin Extended-B
  5279. {begin: 0x0250, end: 0x02AF}, // IPA Extensions
  5280. {begin: 0x02B0, end: 0x02FF}, // Spacing Modifier Letters
  5281. {begin: 0x0300, end: 0x036F}, // Combining Diacritical Marks
  5282. {begin: 0x0370, end: 0x03FF}, // Greek and Coptic
  5283. {begin: 0x2C80, end: 0x2CFF}, // Coptic
  5284. {begin: 0x0400, end: 0x04FF}, // Cyrillic
  5285. {begin: 0x0530, end: 0x058F}, // Armenian
  5286. {begin: 0x0590, end: 0x05FF}, // Hebrew
  5287. {begin: 0xA500, end: 0xA63F}, // Vai
  5288. {begin: 0x0600, end: 0x06FF}, // Arabic
  5289. {begin: 0x07C0, end: 0x07FF}, // NKo
  5290. {begin: 0x0900, end: 0x097F}, // Devanagari
  5291. {begin: 0x0980, end: 0x09FF}, // Bengali
  5292. {begin: 0x0A00, end: 0x0A7F}, // Gurmukhi
  5293. {begin: 0x0A80, end: 0x0AFF}, // Gujarati
  5294. {begin: 0x0B00, end: 0x0B7F}, // Oriya
  5295. {begin: 0x0B80, end: 0x0BFF}, // Tamil
  5296. {begin: 0x0C00, end: 0x0C7F}, // Telugu
  5297. {begin: 0x0C80, end: 0x0CFF}, // Kannada
  5298. {begin: 0x0D00, end: 0x0D7F}, // Malayalam
  5299. {begin: 0x0E00, end: 0x0E7F}, // Thai
  5300. {begin: 0x0E80, end: 0x0EFF}, // Lao
  5301. {begin: 0x10A0, end: 0x10FF}, // Georgian
  5302. {begin: 0x1B00, end: 0x1B7F}, // Balinese
  5303. {begin: 0x1100, end: 0x11FF}, // Hangul Jamo
  5304. {begin: 0x1E00, end: 0x1EFF}, // Latin Extended Additional
  5305. {begin: 0x1F00, end: 0x1FFF}, // Greek Extended
  5306. {begin: 0x2000, end: 0x206F}, // General Punctuation
  5307. {begin: 0x2070, end: 0x209F}, // Superscripts And Subscripts
  5308. {begin: 0x20A0, end: 0x20CF}, // Currency Symbol
  5309. {begin: 0x20D0, end: 0x20FF}, // Combining Diacritical Marks For Symbols
  5310. {begin: 0x2100, end: 0x214F}, // Letterlike Symbols
  5311. {begin: 0x2150, end: 0x218F}, // Number Forms
  5312. {begin: 0x2190, end: 0x21FF}, // Arrows
  5313. {begin: 0x2200, end: 0x22FF}, // Mathematical Operators
  5314. {begin: 0x2300, end: 0x23FF}, // Miscellaneous Technical
  5315. {begin: 0x2400, end: 0x243F}, // Control Pictures
  5316. {begin: 0x2440, end: 0x245F}, // Optical Character Recognition
  5317. {begin: 0x2460, end: 0x24FF}, // Enclosed Alphanumerics
  5318. {begin: 0x2500, end: 0x257F}, // Box Drawing
  5319. {begin: 0x2580, end: 0x259F}, // Block Elements
  5320. {begin: 0x25A0, end: 0x25FF}, // Geometric Shapes
  5321. {begin: 0x2600, end: 0x26FF}, // Miscellaneous Symbols
  5322. {begin: 0x2700, end: 0x27BF}, // Dingbats
  5323. {begin: 0x3000, end: 0x303F}, // CJK Symbols And Punctuation
  5324. {begin: 0x3040, end: 0x309F}, // Hiragana
  5325. {begin: 0x30A0, end: 0x30FF}, // Katakana
  5326. {begin: 0x3100, end: 0x312F}, // Bopomofo
  5327. {begin: 0x3130, end: 0x318F}, // Hangul Compatibility Jamo
  5328. {begin: 0xA840, end: 0xA87F}, // Phags-pa
  5329. {begin: 0x3200, end: 0x32FF}, // Enclosed CJK Letters And Months
  5330. {begin: 0x3300, end: 0x33FF}, // CJK Compatibility
  5331. {begin: 0xAC00, end: 0xD7AF}, // Hangul Syllables
  5332. {begin: 0xD800, end: 0xDFFF}, // Non-Plane 0 *
  5333. {begin: 0x10900, end: 0x1091F}, // Phoenicia
  5334. {begin: 0x4E00, end: 0x9FFF}, // CJK Unified Ideographs
  5335. {begin: 0xE000, end: 0xF8FF}, // Private Use Area (plane 0)
  5336. {begin: 0x31C0, end: 0x31EF}, // CJK Strokes
  5337. {begin: 0xFB00, end: 0xFB4F}, // Alphabetic Presentation Forms
  5338. {begin: 0xFB50, end: 0xFDFF}, // Arabic Presentation Forms-A
  5339. {begin: 0xFE20, end: 0xFE2F}, // Combining Half Marks
  5340. {begin: 0xFE10, end: 0xFE1F}, // Vertical Forms
  5341. {begin: 0xFE50, end: 0xFE6F}, // Small Form Variants
  5342. {begin: 0xFE70, end: 0xFEFF}, // Arabic Presentation Forms-B
  5343. {begin: 0xFF00, end: 0xFFEF}, // Halfwidth And Fullwidth Forms
  5344. {begin: 0xFFF0, end: 0xFFFF}, // Specials
  5345. {begin: 0x0F00, end: 0x0FFF}, // Tibetan
  5346. {begin: 0x0700, end: 0x074F}, // Syriac
  5347. {begin: 0x0780, end: 0x07BF}, // Thaana
  5348. {begin: 0x0D80, end: 0x0DFF}, // Sinhala
  5349. {begin: 0x1000, end: 0x109F}, // Myanmar
  5350. {begin: 0x1200, end: 0x137F}, // Ethiopic
  5351. {begin: 0x13A0, end: 0x13FF}, // Cherokee
  5352. {begin: 0x1400, end: 0x167F}, // Unified Canadian Aboriginal Syllabics
  5353. {begin: 0x1680, end: 0x169F}, // Ogham
  5354. {begin: 0x16A0, end: 0x16FF}, // Runic
  5355. {begin: 0x1780, end: 0x17FF}, // Khmer
  5356. {begin: 0x1800, end: 0x18AF}, // Mongolian
  5357. {begin: 0x2800, end: 0x28FF}, // Braille Patterns
  5358. {begin: 0xA000, end: 0xA48F}, // Yi Syllables
  5359. {begin: 0x1700, end: 0x171F}, // Tagalog
  5360. {begin: 0x10300, end: 0x1032F}, // Old Italic
  5361. {begin: 0x10330, end: 0x1034F}, // Gothic
  5362. {begin: 0x10400, end: 0x1044F}, // Deseret
  5363. {begin: 0x1D000, end: 0x1D0FF}, // Byzantine Musical Symbols
  5364. {begin: 0x1D400, end: 0x1D7FF}, // Mathematical Alphanumeric Symbols
  5365. {begin: 0xFF000, end: 0xFFFFD}, // Private Use (plane 15)
  5366. {begin: 0xFE00, end: 0xFE0F}, // Variation Selectors
  5367. {begin: 0xE0000, end: 0xE007F}, // Tags
  5368. {begin: 0x1900, end: 0x194F}, // Limbu
  5369. {begin: 0x1950, end: 0x197F}, // Tai Le
  5370. {begin: 0x1980, end: 0x19DF}, // New Tai Lue
  5371. {begin: 0x1A00, end: 0x1A1F}, // Buginese
  5372. {begin: 0x2C00, end: 0x2C5F}, // Glagolitic
  5373. {begin: 0x2D30, end: 0x2D7F}, // Tifinagh
  5374. {begin: 0x4DC0, end: 0x4DFF}, // Yijing Hexagram Symbols
  5375. {begin: 0xA800, end: 0xA82F}, // Syloti Nagri
  5376. {begin: 0x10000, end: 0x1007F}, // Linear B Syllabary
  5377. {begin: 0x10140, end: 0x1018F}, // Ancient Greek Numbers
  5378. {begin: 0x10380, end: 0x1039F}, // Ugaritic
  5379. {begin: 0x103A0, end: 0x103DF}, // Old Persian
  5380. {begin: 0x10450, end: 0x1047F}, // Shavian
  5381. {begin: 0x10480, end: 0x104AF}, // Osmanya
  5382. {begin: 0x10800, end: 0x1083F}, // Cypriot Syllabary
  5383. {begin: 0x10A00, end: 0x10A5F}, // Kharoshthi
  5384. {begin: 0x1D300, end: 0x1D35F}, // Tai Xuan Jing Symbols
  5385. {begin: 0x12000, end: 0x123FF}, // Cuneiform
  5386. {begin: 0x1D360, end: 0x1D37F}, // Counting Rod Numerals
  5387. {begin: 0x1B80, end: 0x1BBF}, // Sundanese
  5388. {begin: 0x1C00, end: 0x1C4F}, // Lepcha
  5389. {begin: 0x1C50, end: 0x1C7F}, // Ol Chiki
  5390. {begin: 0xA880, end: 0xA8DF}, // Saurashtra
  5391. {begin: 0xA900, end: 0xA92F}, // Kayah Li
  5392. {begin: 0xA930, end: 0xA95F}, // Rejang
  5393. {begin: 0xAA00, end: 0xAA5F}, // Cham
  5394. {begin: 0x10190, end: 0x101CF}, // Ancient Symbols
  5395. {begin: 0x101D0, end: 0x101FF}, // Phaistos Disc
  5396. {begin: 0x102A0, end: 0x102DF}, // Carian
  5397. {begin: 0x1F030, end: 0x1F09F} // Domino Tiles
  5398. ];
  5399.  
  5400. function getUnicodeRange(unicode) {
  5401. for (var i = 0; i < unicodeRanges.length; i += 1) {
  5402. var range = unicodeRanges[i];
  5403. if (unicode >= range.begin && unicode < range.end) {
  5404. return i;
  5405. }
  5406. }
  5407.  
  5408. return -1;
  5409. }
  5410.  
  5411. // Parse the OS/2 and Windows metrics `OS/2` table
  5412. function parseOS2Table(data, start) {
  5413. var os2 = {};
  5414. var p = new parse.Parser(data, start);
  5415. os2.version = p.parseUShort();
  5416. os2.xAvgCharWidth = p.parseShort();
  5417. os2.usWeightClass = p.parseUShort();
  5418. os2.usWidthClass = p.parseUShort();
  5419. os2.fsType = p.parseUShort();
  5420. os2.ySubscriptXSize = p.parseShort();
  5421. os2.ySubscriptYSize = p.parseShort();
  5422. os2.ySubscriptXOffset = p.parseShort();
  5423. os2.ySubscriptYOffset = p.parseShort();
  5424. os2.ySuperscriptXSize = p.parseShort();
  5425. os2.ySuperscriptYSize = p.parseShort();
  5426. os2.ySuperscriptXOffset = p.parseShort();
  5427. os2.ySuperscriptYOffset = p.parseShort();
  5428. os2.yStrikeoutSize = p.parseShort();
  5429. os2.yStrikeoutPosition = p.parseShort();
  5430. os2.sFamilyClass = p.parseShort();
  5431. os2.panose = [];
  5432. for (var i = 0; i < 10; i++) {
  5433. os2.panose[i] = p.parseByte();
  5434. }
  5435.  
  5436. os2.ulUnicodeRange1 = p.parseULong();
  5437. os2.ulUnicodeRange2 = p.parseULong();
  5438. os2.ulUnicodeRange3 = p.parseULong();
  5439. os2.ulUnicodeRange4 = p.parseULong();
  5440. os2.achVendID = String.fromCharCode(p.parseByte(), p.parseByte(), p.parseByte(), p.parseByte());
  5441. os2.fsSelection = p.parseUShort();
  5442. os2.usFirstCharIndex = p.parseUShort();
  5443. os2.usLastCharIndex = p.parseUShort();
  5444. os2.sTypoAscender = p.parseShort();
  5445. os2.sTypoDescender = p.parseShort();
  5446. os2.sTypoLineGap = p.parseShort();
  5447. os2.usWinAscent = p.parseUShort();
  5448. os2.usWinDescent = p.parseUShort();
  5449. if (os2.version >= 1) {
  5450. os2.ulCodePageRange1 = p.parseULong();
  5451. os2.ulCodePageRange2 = p.parseULong();
  5452. }
  5453.  
  5454. if (os2.version >= 2) {
  5455. os2.sxHeight = p.parseShort();
  5456. os2.sCapHeight = p.parseShort();
  5457. os2.usDefaultChar = p.parseUShort();
  5458. os2.usBreakChar = p.parseUShort();
  5459. os2.usMaxContent = p.parseUShort();
  5460. }
  5461.  
  5462. return os2;
  5463. }
  5464.  
  5465. function makeOS2Table(options) {
  5466. return new table.Table('OS/2', [
  5467. {name: 'version', type: 'USHORT', value: 0x0003},
  5468. {name: 'xAvgCharWidth', type: 'SHORT', value: 0},
  5469. {name: 'usWeightClass', type: 'USHORT', value: 0},
  5470. {name: 'usWidthClass', type: 'USHORT', value: 0},
  5471. {name: 'fsType', type: 'USHORT', value: 0},
  5472. {name: 'ySubscriptXSize', type: 'SHORT', value: 650},
  5473. {name: 'ySubscriptYSize', type: 'SHORT', value: 699},
  5474. {name: 'ySubscriptXOffset', type: 'SHORT', value: 0},
  5475. {name: 'ySubscriptYOffset', type: 'SHORT', value: 140},
  5476. {name: 'ySuperscriptXSize', type: 'SHORT', value: 650},
  5477. {name: 'ySuperscriptYSize', type: 'SHORT', value: 699},
  5478. {name: 'ySuperscriptXOffset', type: 'SHORT', value: 0},
  5479. {name: 'ySuperscriptYOffset', type: 'SHORT', value: 479},
  5480. {name: 'yStrikeoutSize', type: 'SHORT', value: 49},
  5481. {name: 'yStrikeoutPosition', type: 'SHORT', value: 258},
  5482. {name: 'sFamilyClass', type: 'SHORT', value: 0},
  5483. {name: 'bFamilyType', type: 'BYTE', value: 0},
  5484. {name: 'bSerifStyle', type: 'BYTE', value: 0},
  5485. {name: 'bWeight', type: 'BYTE', value: 0},
  5486. {name: 'bProportion', type: 'BYTE', value: 0},
  5487. {name: 'bContrast', type: 'BYTE', value: 0},
  5488. {name: 'bStrokeVariation', type: 'BYTE', value: 0},
  5489. {name: 'bArmStyle', type: 'BYTE', value: 0},
  5490. {name: 'bLetterform', type: 'BYTE', value: 0},
  5491. {name: 'bMidline', type: 'BYTE', value: 0},
  5492. {name: 'bXHeight', type: 'BYTE', value: 0},
  5493. {name: 'ulUnicodeRange1', type: 'ULONG', value: 0},
  5494. {name: 'ulUnicodeRange2', type: 'ULONG', value: 0},
  5495. {name: 'ulUnicodeRange3', type: 'ULONG', value: 0},
  5496. {name: 'ulUnicodeRange4', type: 'ULONG', value: 0},
  5497. {name: 'achVendID', type: 'CHARARRAY', value: 'XXXX'},
  5498. {name: 'fsSelection', type: 'USHORT', value: 0},
  5499. {name: 'usFirstCharIndex', type: 'USHORT', value: 0},
  5500. {name: 'usLastCharIndex', type: 'USHORT', value: 0},
  5501. {name: 'sTypoAscender', type: 'SHORT', value: 0},
  5502. {name: 'sTypoDescender', type: 'SHORT', value: 0},
  5503. {name: 'sTypoLineGap', type: 'SHORT', value: 0},
  5504. {name: 'usWinAscent', type: 'USHORT', value: 0},
  5505. {name: 'usWinDescent', type: 'USHORT', value: 0},
  5506. {name: 'ulCodePageRange1', type: 'ULONG', value: 0},
  5507. {name: 'ulCodePageRange2', type: 'ULONG', value: 0},
  5508. {name: 'sxHeight', type: 'SHORT', value: 0},
  5509. {name: 'sCapHeight', type: 'SHORT', value: 0},
  5510. {name: 'usDefaultChar', type: 'USHORT', value: 0},
  5511. {name: 'usBreakChar', type: 'USHORT', value: 0},
  5512. {name: 'usMaxContext', type: 'USHORT', value: 0}
  5513. ], options);
  5514. }
  5515.  
  5516. exports.unicodeRanges = unicodeRanges;
  5517. exports.getUnicodeRange = getUnicodeRange;
  5518. exports.parse = parseOS2Table;
  5519. exports.make = makeOS2Table;
  5520.  
  5521. },{"../parse":9,"../table":11}],26:[function(require,module,exports){
  5522. // The `post` table stores additional PostScript information, such as glyph names.
  5523. // https://www.microsoft.com/typography/OTSPEC/post.htm
  5524.  
  5525. 'use strict';
  5526.  
  5527. var encoding = require('../encoding');
  5528. var parse = require('../parse');
  5529. var table = require('../table');
  5530.  
  5531. // Parse the PostScript `post` table
  5532. function parsePostTable(data, start) {
  5533. var post = {};
  5534. var p = new parse.Parser(data, start);
  5535. var i;
  5536. post.version = p.parseVersion();
  5537. post.italicAngle = p.parseFixed();
  5538. post.underlinePosition = p.parseShort();
  5539. post.underlineThickness = p.parseShort();
  5540. post.isFixedPitch = p.parseULong();
  5541. post.minMemType42 = p.parseULong();
  5542. post.maxMemType42 = p.parseULong();
  5543. post.minMemType1 = p.parseULong();
  5544. post.maxMemType1 = p.parseULong();
  5545. switch (post.version) {
  5546. case 1:
  5547. post.names = encoding.standardNames.slice();
  5548. break;
  5549. case 2:
  5550. post.numberOfGlyphs = p.parseUShort();
  5551. post.glyphNameIndex = new Array(post.numberOfGlyphs);
  5552. for (i = 0; i < post.numberOfGlyphs; i++) {
  5553. post.glyphNameIndex[i] = p.parseUShort();
  5554. }
  5555.  
  5556. post.names = [];
  5557. for (i = 0; i < post.numberOfGlyphs; i++) {
  5558. if (post.glyphNameIndex[i] >= encoding.standardNames.length) {
  5559. var nameLength = p.parseChar();
  5560. post.names.push(p.parseString(nameLength));
  5561. }
  5562. }
  5563.  
  5564. break;
  5565. case 2.5:
  5566. post.numberOfGlyphs = p.parseUShort();
  5567. post.offset = new Array(post.numberOfGlyphs);
  5568. for (i = 0; i < post.numberOfGlyphs; i++) {
  5569. post.offset[i] = p.parseChar();
  5570. }
  5571.  
  5572. break;
  5573. }
  5574. return post;
  5575. }
  5576.  
  5577. function makePostTable() {
  5578. return new table.Table('post', [
  5579. {name: 'version', type: 'FIXED', value: 0x00030000},
  5580. {name: 'italicAngle', type: 'FIXED', value: 0},
  5581. {name: 'underlinePosition', type: 'FWORD', value: 0},
  5582. {name: 'underlineThickness', type: 'FWORD', value: 0},
  5583. {name: 'isFixedPitch', type: 'ULONG', value: 0},
  5584. {name: 'minMemType42', type: 'ULONG', value: 0},
  5585. {name: 'maxMemType42', type: 'ULONG', value: 0},
  5586. {name: 'minMemType1', type: 'ULONG', value: 0},
  5587. {name: 'maxMemType1', type: 'ULONG', value: 0}
  5588. ]);
  5589. }
  5590.  
  5591. exports.parse = parsePostTable;
  5592. exports.make = makePostTable;
  5593.  
  5594. },{"../encoding":4,"../parse":9,"../table":11}],27:[function(require,module,exports){
  5595. // The `sfnt` wrapper provides organization for the tables in the font.
  5596. // It is the top-level data structure in a font.
  5597. // https://www.microsoft.com/typography/OTSPEC/otff.htm
  5598. // Recommendations for creating OpenType Fonts:
  5599. // http://www.microsoft.com/typography/otspec140/recom.htm
  5600.  
  5601. 'use strict';
  5602.  
  5603. var check = require('../check');
  5604. var table = require('../table');
  5605.  
  5606. var cmap = require('./cmap');
  5607. var cff = require('./cff');
  5608. var head = require('./head');
  5609. var hhea = require('./hhea');
  5610. var hmtx = require('./hmtx');
  5611. var ltag = require('./ltag');
  5612. var maxp = require('./maxp');
  5613. var _name = require('./name');
  5614. var os2 = require('./os2');
  5615. var post = require('./post');
  5616.  
  5617. function log2(v) {
  5618. return Math.log(v) / Math.log(2) | 0;
  5619. }
  5620.  
  5621. function computeCheckSum(bytes) {
  5622. while (bytes.length % 4 !== 0) {
  5623. bytes.push(0);
  5624. }
  5625.  
  5626. var sum = 0;
  5627. for (var i = 0; i < bytes.length; i += 4) {
  5628. sum += (bytes[i] << 24) +
  5629. (bytes[i + 1] << 16) +
  5630. (bytes[i + 2] << 8) +
  5631. (bytes[i + 3]);
  5632. }
  5633.  
  5634. sum %= Math.pow(2, 32);
  5635. return sum;
  5636. }
  5637.  
  5638. function makeTableRecord(tag, checkSum, offset, length) {
  5639. return new table.Table('Table Record', [
  5640. {name: 'tag', type: 'TAG', value: tag !== undefined ? tag : ''},
  5641. {name: 'checkSum', type: 'ULONG', value: checkSum !== undefined ? checkSum : 0},
  5642. {name: 'offset', type: 'ULONG', value: offset !== undefined ? offset : 0},
  5643. {name: 'length', type: 'ULONG', value: length !== undefined ? length : 0}
  5644. ]);
  5645. }
  5646.  
  5647. function makeSfntTable(tables) {
  5648. var sfnt = new table.Table('sfnt', [
  5649. {name: 'version', type: 'TAG', value: 'OTTO'},
  5650. {name: 'numTables', type: 'USHORT', value: 0},
  5651. {name: 'searchRange', type: 'USHORT', value: 0},
  5652. {name: 'entrySelector', type: 'USHORT', value: 0},
  5653. {name: 'rangeShift', type: 'USHORT', value: 0}
  5654. ]);
  5655. sfnt.tables = tables;
  5656. sfnt.numTables = tables.length;
  5657. var highestPowerOf2 = Math.pow(2, log2(sfnt.numTables));
  5658. sfnt.searchRange = 16 * highestPowerOf2;
  5659. sfnt.entrySelector = log2(highestPowerOf2);
  5660. sfnt.rangeShift = sfnt.numTables * 16 - sfnt.searchRange;
  5661.  
  5662. var recordFields = [];
  5663. var tableFields = [];
  5664.  
  5665. var offset = sfnt.sizeOf() + (makeTableRecord().sizeOf() * sfnt.numTables);
  5666. while (offset % 4 !== 0) {
  5667. offset += 1;
  5668. tableFields.push({name: 'padding', type: 'BYTE', value: 0});
  5669. }
  5670.  
  5671. for (var i = 0; i < tables.length; i += 1) {
  5672. var t = tables[i];
  5673. check.argument(t.tableName.length === 4, 'Table name' + t.tableName + ' is invalid.');
  5674. var tableLength = t.sizeOf();
  5675. var tableRecord = makeTableRecord(t.tableName, computeCheckSum(t.encode()), offset, tableLength);
  5676. recordFields.push({name: tableRecord.tag + ' Table Record', type: 'TABLE', value: tableRecord});
  5677. tableFields.push({name: t.tableName + ' table', type: 'TABLE', value: t});
  5678. offset += tableLength;
  5679. check.argument(!isNaN(offset), 'Something went wrong calculating the offset.');
  5680. while (offset % 4 !== 0) {
  5681. offset += 1;
  5682. tableFields.push({name: 'padding', type: 'BYTE', value: 0});
  5683. }
  5684. }
  5685.  
  5686. // Table records need to be sorted alphabetically.
  5687. recordFields.sort(function(r1, r2) {
  5688. if (r1.value.tag > r2.value.tag) {
  5689. return 1;
  5690. } else {
  5691. return -1;
  5692. }
  5693. });
  5694.  
  5695. sfnt.fields = sfnt.fields.concat(recordFields);
  5696. sfnt.fields = sfnt.fields.concat(tableFields);
  5697. return sfnt;
  5698. }
  5699.  
  5700. // Get the metrics for a character. If the string has more than one character
  5701. // this function returns metrics for the first available character.
  5702. // You can provide optional fallback metrics if no characters are available.
  5703. function metricsForChar(font, chars, notFoundMetrics) {
  5704. for (var i = 0; i < chars.length; i += 1) {
  5705. var glyphIndex = font.charToGlyphIndex(chars[i]);
  5706. if (glyphIndex > 0) {
  5707. var glyph = font.glyphs.get(glyphIndex);
  5708. return glyph.getMetrics();
  5709. }
  5710. }
  5711.  
  5712. return notFoundMetrics;
  5713. }
  5714.  
  5715. function average(vs) {
  5716. var sum = 0;
  5717. for (var i = 0; i < vs.length; i += 1) {
  5718. sum += vs[i];
  5719. }
  5720.  
  5721. return sum / vs.length;
  5722. }
  5723.  
  5724. // Convert the font object to a SFNT data structure.
  5725. // This structure contains all the necessary tables and metadata to create a binary OTF file.
  5726. function fontToSfntTable(font) {
  5727. var xMins = [];
  5728. var yMins = [];
  5729. var xMaxs = [];
  5730. var yMaxs = [];
  5731. var advanceWidths = [];
  5732. var leftSideBearings = [];
  5733. var rightSideBearings = [];
  5734. var firstCharIndex;
  5735. var lastCharIndex = 0;
  5736. var ulUnicodeRange1 = 0;
  5737. var ulUnicodeRange2 = 0;
  5738. var ulUnicodeRange3 = 0;
  5739. var ulUnicodeRange4 = 0;
  5740.  
  5741. for (var i = 0; i < font.glyphs.length; i += 1) {
  5742. var glyph = font.glyphs.get(i);
  5743. var unicode = glyph.unicode | 0;
  5744. if (firstCharIndex > unicode || firstCharIndex === null) {
  5745. firstCharIndex = unicode;
  5746. }
  5747.  
  5748. if (lastCharIndex < unicode) {
  5749. lastCharIndex = unicode;
  5750. }
  5751.  
  5752. var position = os2.getUnicodeRange(unicode);
  5753. if (position < 32) {
  5754. ulUnicodeRange1 |= 1 << position;
  5755. } else if (position < 64) {
  5756. ulUnicodeRange2 |= 1 << position - 32;
  5757. } else if (position < 96) {
  5758. ulUnicodeRange3 |= 1 << position - 64;
  5759. } else if (position < 123) {
  5760. ulUnicodeRange4 |= 1 << position - 96;
  5761. } else {
  5762. throw new Error('Unicode ranges bits > 123 are reserved for internal usage');
  5763. }
  5764. // Skip non-important characters.
  5765. if (glyph.name === '.notdef') continue;
  5766. var metrics = glyph.getMetrics();
  5767. xMins.push(metrics.xMin);
  5768. yMins.push(metrics.yMin);
  5769. xMaxs.push(metrics.xMax);
  5770. yMaxs.push(metrics.yMax);
  5771. leftSideBearings.push(metrics.leftSideBearing);
  5772. rightSideBearings.push(metrics.rightSideBearing);
  5773. advanceWidths.push(glyph.advanceWidth);
  5774. }
  5775.  
  5776. var globals = {
  5777. xMin: Math.min.apply(null, xMins),
  5778. yMin: Math.min.apply(null, yMins),
  5779. xMax: Math.max.apply(null, xMaxs),
  5780. yMax: Math.max.apply(null, yMaxs),
  5781. advanceWidthMax: Math.max.apply(null, advanceWidths),
  5782. advanceWidthAvg: average(advanceWidths),
  5783. minLeftSideBearing: Math.min.apply(null, leftSideBearings),
  5784. maxLeftSideBearing: Math.max.apply(null, leftSideBearings),
  5785. minRightSideBearing: Math.min.apply(null, rightSideBearings)
  5786. };
  5787. globals.ascender = font.ascender !== undefined ? font.ascender : globals.yMax;
  5788. globals.descender = font.descender !== undefined ? font.descender : globals.yMin;
  5789.  
  5790. var headTable = head.make({
  5791. flags: 3, // 00000011 (baseline for font at y=0; left sidebearing point at x=0)
  5792. unitsPerEm: font.unitsPerEm,
  5793. xMin: globals.xMin,
  5794. yMin: globals.yMin,
  5795. xMax: globals.xMax,
  5796. yMax: globals.yMax,
  5797. lowestRecPPEM: 3
  5798. });
  5799.  
  5800. var hheaTable = hhea.make({
  5801. ascender: globals.ascender,
  5802. descender: globals.descender,
  5803. advanceWidthMax: globals.advanceWidthMax,
  5804. minLeftSideBearing: globals.minLeftSideBearing,
  5805. minRightSideBearing: globals.minRightSideBearing,
  5806. xMaxExtent: globals.maxLeftSideBearing + (globals.xMax - globals.xMin),
  5807. numberOfHMetrics: font.glyphs.length
  5808. });
  5809.  
  5810. var maxpTable = maxp.make(font.glyphs.length);
  5811.  
  5812. var os2Table = os2.make({
  5813. xAvgCharWidth: Math.round(globals.advanceWidthAvg),
  5814. usWeightClass: 500, // Medium FIXME Make this configurable
  5815. usWidthClass: 5, // Medium (normal) FIXME Make this configurable
  5816. usFirstCharIndex: firstCharIndex,
  5817. usLastCharIndex: lastCharIndex,
  5818. ulUnicodeRange1: ulUnicodeRange1,
  5819. ulUnicodeRange2: ulUnicodeRange2,
  5820. ulUnicodeRange3: ulUnicodeRange3,
  5821. ulUnicodeRange4: ulUnicodeRange4,
  5822. fsSelection: 64, // REGULAR
  5823. // See http://typophile.com/node/13081 for more info on vertical metrics.
  5824. // We get metrics for typical characters (such as "x" for xHeight).
  5825. // We provide some fallback characters if characters are unavailable: their
  5826. // ordering was chosen experimentally.
  5827. sTypoAscender: globals.ascender,
  5828. sTypoDescender: globals.descender,
  5829. sTypoLineGap: 0,
  5830. usWinAscent: globals.yMax,
  5831. usWinDescent: Math.abs(globals.yMin),
  5832. ulCodePageRange1: 1, // FIXME: hard-code Latin 1 support for now
  5833. sxHeight: metricsForChar(font, 'xyvw', {yMax: Math.round(globals.ascender / 2)}).yMax,
  5834. sCapHeight: metricsForChar(font, 'HIKLEFJMNTZBDPRAGOQSUVWXY', globals).yMax,
  5835. usDefaultChar: font.hasChar(' ') ? 32 : 0, // Use space as the default character, if available.
  5836. usBreakChar: font.hasChar(' ') ? 32 : 0 // Use space as the break character, if available.
  5837. });
  5838.  
  5839. var hmtxTable = hmtx.make(font.glyphs);
  5840. var cmapTable = cmap.make(font.glyphs);
  5841.  
  5842. var englishFamilyName = font.getEnglishName('fontFamily');
  5843. var englishStyleName = font.getEnglishName('fontSubfamily');
  5844. var englishFullName = englishFamilyName + ' ' + englishStyleName;
  5845. var postScriptName = font.getEnglishName('postScriptName');
  5846. if (!postScriptName) {
  5847. postScriptName = englishFamilyName.replace(/\s/g, '') + '-' + englishStyleName;
  5848. }
  5849.  
  5850. var names = {};
  5851. for (var n in font.names) {
  5852. names[n] = font.names[n];
  5853. }
  5854.  
  5855. if (!names.uniqueID) {
  5856. names.uniqueID = {en: font.getEnglishName('manufacturer') + ':' + englishFullName};
  5857. }
  5858.  
  5859. if (!names.postScriptName) {
  5860. names.postScriptName = {en: postScriptName};
  5861. }
  5862.  
  5863. if (!names.preferredFamily) {
  5864. names.preferredFamily = font.names.fontFamily;
  5865. }
  5866.  
  5867. if (!names.preferredSubfamily) {
  5868. names.preferredSubfamily = font.names.fontSubfamily;
  5869. }
  5870.  
  5871. var languageTags = [];
  5872. var nameTable = _name.make(names, languageTags);
  5873. var ltagTable = (languageTags.length > 0 ? ltag.make(languageTags) : undefined);
  5874.  
  5875. var postTable = post.make();
  5876. var cffTable = cff.make(font.glyphs, {
  5877. version: font.getEnglishName('version'),
  5878. fullName: englishFullName,
  5879. familyName: englishFamilyName,
  5880. weightName: englishStyleName,
  5881. postScriptName: postScriptName,
  5882. unitsPerEm: font.unitsPerEm,
  5883. fontBBox: [0, globals.yMin, globals.ascender, globals.advanceWidthMax]
  5884. });
  5885.  
  5886. // The order does not matter because makeSfntTable() will sort them.
  5887. var tables = [headTable, hheaTable, maxpTable, os2Table, nameTable, cmapTable, postTable, cffTable, hmtxTable];
  5888. if (ltagTable) {
  5889. tables.push(ltagTable);
  5890. }
  5891.  
  5892. var sfntTable = makeSfntTable(tables);
  5893.  
  5894. // Compute the font's checkSum and store it in head.checkSumAdjustment.
  5895. var bytes = sfntTable.encode();
  5896. var checkSum = computeCheckSum(bytes);
  5897. var tableFields = sfntTable.fields;
  5898. var checkSumAdjusted = false;
  5899. for (i = 0; i < tableFields.length; i += 1) {
  5900. if (tableFields[i].name === 'head table') {
  5901. tableFields[i].value.checkSumAdjustment = 0xB1B0AFBA - checkSum;
  5902. checkSumAdjusted = true;
  5903. break;
  5904. }
  5905. }
  5906.  
  5907. if (!checkSumAdjusted) {
  5908. throw new Error('Could not find head table with checkSum to adjust.');
  5909. }
  5910.  
  5911. return sfntTable;
  5912. }
  5913.  
  5914. exports.computeCheckSum = computeCheckSum;
  5915. exports.make = makeSfntTable;
  5916. exports.fontToTable = fontToSfntTable;
  5917.  
  5918. },{"../check":2,"../table":11,"./cff":12,"./cmap":13,"./head":17,"./hhea":18,"./hmtx":19,"./ltag":22,"./maxp":23,"./name":24,"./os2":25,"./post":26}],28:[function(require,module,exports){
  5919. // Data types used in the OpenType font file.
  5920. // All OpenType fonts use Motorola-style byte ordering (Big Endian)
  5921.  
  5922. /* global WeakMap */
  5923.  
  5924. 'use strict';
  5925.  
  5926. var check = require('./check');
  5927.  
  5928. var LIMIT16 = 32768; // The limit at which a 16-bit number switches signs == 2^15
  5929. var LIMIT32 = 2147483648; // The limit at which a 32-bit number switches signs == 2 ^ 31
  5930.  
  5931. var decode = {};
  5932. var encode = {};
  5933. var sizeOf = {};
  5934.  
  5935. // Return a function that always returns the same value.
  5936. function constant(v) {
  5937. return function() {
  5938. return v;
  5939. };
  5940. }
  5941.  
  5942. // OpenType data types //////////////////////////////////////////////////////
  5943.  
  5944. // Convert an 8-bit unsigned integer to a list of 1 byte.
  5945. encode.BYTE = function(v) {
  5946. check.argument(v >= 0 && v <= 255, 'Byte value should be between 0 and 255.');
  5947. return [v];
  5948. };
  5949.  
  5950. sizeOf.BYTE = constant(1);
  5951.  
  5952. // Convert a 8-bit signed integer to a list of 1 byte.
  5953. encode.CHAR = function(v) {
  5954. return [v.charCodeAt(0)];
  5955. };
  5956.  
  5957. sizeOf.CHAR = constant(1);
  5958.  
  5959. // Convert an ASCII string to a list of bytes.
  5960. encode.CHARARRAY = function(v) {
  5961. var b = [];
  5962. for (var i = 0; i < v.length; i += 1) {
  5963. b.push(v.charCodeAt(i));
  5964. }
  5965.  
  5966. return b;
  5967. };
  5968.  
  5969. sizeOf.CHARARRAY = function(v) {
  5970. return v.length;
  5971. };
  5972.  
  5973. // Convert a 16-bit unsigned integer to a list of 2 bytes.
  5974. encode.USHORT = function(v) {
  5975. return [(v >> 8) & 0xFF, v & 0xFF];
  5976. };
  5977.  
  5978. sizeOf.USHORT = constant(2);
  5979.  
  5980. // Convert a 16-bit signed integer to a list of 2 bytes.
  5981. encode.SHORT = function(v) {
  5982. // Two's complement
  5983. if (v >= LIMIT16) {
  5984. v = -(2 * LIMIT16 - v);
  5985. }
  5986.  
  5987. return [(v >> 8) & 0xFF, v & 0xFF];
  5988. };
  5989.  
  5990. sizeOf.SHORT = constant(2);
  5991.  
  5992. // Convert a 24-bit unsigned integer to a list of 3 bytes.
  5993. encode.UINT24 = function(v) {
  5994. return [(v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF];
  5995. };
  5996.  
  5997. sizeOf.UINT24 = constant(3);
  5998.  
  5999. // Convert a 32-bit unsigned integer to a list of 4 bytes.
  6000. encode.ULONG = function(v) {
  6001. return [(v >> 24) & 0xFF, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF];
  6002. };
  6003.  
  6004. sizeOf.ULONG = constant(4);
  6005.  
  6006. // Convert a 32-bit unsigned integer to a list of 4 bytes.
  6007. encode.LONG = function(v) {
  6008. // Two's complement
  6009. if (v >= LIMIT32) {
  6010. v = -(2 * LIMIT32 - v);
  6011. }
  6012.  
  6013. return [(v >> 24) & 0xFF, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF];
  6014. };
  6015.  
  6016. sizeOf.LONG = constant(4);
  6017.  
  6018. encode.FIXED = encode.ULONG;
  6019. sizeOf.FIXED = sizeOf.ULONG;
  6020.  
  6021. encode.FWORD = encode.SHORT;
  6022. sizeOf.FWORD = sizeOf.SHORT;
  6023.  
  6024. encode.UFWORD = encode.USHORT;
  6025. sizeOf.UFWORD = sizeOf.USHORT;
  6026.  
  6027. // FIXME Implement LONGDATETIME
  6028. encode.LONGDATETIME = function() {
  6029. return [0, 0, 0, 0, 0, 0, 0, 0];
  6030. };
  6031.  
  6032. sizeOf.LONGDATETIME = constant(8);
  6033.  
  6034. // Convert a 4-char tag to a list of 4 bytes.
  6035. encode.TAG = function(v) {
  6036. check.argument(v.length === 4, 'Tag should be exactly 4 ASCII characters.');
  6037. return [v.charCodeAt(0),
  6038. v.charCodeAt(1),
  6039. v.charCodeAt(2),
  6040. v.charCodeAt(3)];
  6041. };
  6042.  
  6043. sizeOf.TAG = constant(4);
  6044.  
  6045. // CFF data types ///////////////////////////////////////////////////////////
  6046.  
  6047. encode.Card8 = encode.BYTE;
  6048. sizeOf.Card8 = sizeOf.BYTE;
  6049.  
  6050. encode.Card16 = encode.USHORT;
  6051. sizeOf.Card16 = sizeOf.USHORT;
  6052.  
  6053. encode.OffSize = encode.BYTE;
  6054. sizeOf.OffSize = sizeOf.BYTE;
  6055.  
  6056. encode.SID = encode.USHORT;
  6057. sizeOf.SID = sizeOf.USHORT;
  6058.  
  6059. // Convert a numeric operand or charstring number to a variable-size list of bytes.
  6060. encode.NUMBER = function(v) {
  6061. if (v >= -107 && v <= 107) {
  6062. return [v + 139];
  6063. } else if (v >= 108 && v <= 1131) {
  6064. v = v - 108;
  6065. return [(v >> 8) + 247, v & 0xFF];
  6066. } else if (v >= -1131 && v <= -108) {
  6067. v = -v - 108;
  6068. return [(v >> 8) + 251, v & 0xFF];
  6069. } else if (v >= -32768 && v <= 32767) {
  6070. return encode.NUMBER16(v);
  6071. } else {
  6072. return encode.NUMBER32(v);
  6073. }
  6074. };
  6075.  
  6076. sizeOf.NUMBER = function(v) {
  6077. return encode.NUMBER(v).length;
  6078. };
  6079.  
  6080. // Convert a signed number between -32768 and +32767 to a three-byte value.
  6081. // This ensures we always use three bytes, but is not the most compact format.
  6082. encode.NUMBER16 = function(v) {
  6083. return [28, (v >> 8) & 0xFF, v & 0xFF];
  6084. };
  6085.  
  6086. sizeOf.NUMBER16 = constant(3);
  6087.  
  6088. // Convert a signed number between -(2^31) and +(2^31-1) to a five-byte value.
  6089. // This is useful if you want to be sure you always use four bytes,
  6090. // at the expense of wasting a few bytes for smaller numbers.
  6091. encode.NUMBER32 = function(v) {
  6092. return [29, (v >> 24) & 0xFF, (v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF];
  6093. };
  6094.  
  6095. sizeOf.NUMBER32 = constant(5);
  6096.  
  6097. encode.REAL = function(v) {
  6098. var value = v.toString();
  6099.  
  6100. // Some numbers use an epsilon to encode the value. (e.g. JavaScript will store 0.0000001 as 1e-7)
  6101. // This code converts it back to a number without the epsilon.
  6102. var m = /\.(\d*?)(?:9{5,20}|0{5,20})\d{0,2}(?:e(.+)|$)/.exec(value);
  6103. if (m) {
  6104. var epsilon = parseFloat('1e' + ((m[2] ? +m[2] : 0) + m[1].length));
  6105. value = (Math.round(v * epsilon) / epsilon).toString();
  6106. }
  6107.  
  6108. var nibbles = '';
  6109. var i;
  6110. var ii;
  6111. for (i = 0, ii = value.length; i < ii; i += 1) {
  6112. var c = value[i];
  6113. if (c === 'e') {
  6114. nibbles += value[++i] === '-' ? 'c' : 'b';
  6115. } else if (c === '.') {
  6116. nibbles += 'a';
  6117. } else if (c === '-') {
  6118. nibbles += 'e';
  6119. } else {
  6120. nibbles += c;
  6121. }
  6122. }
  6123.  
  6124. nibbles += (nibbles.length & 1) ? 'f' : 'ff';
  6125. var out = [30];
  6126. for (i = 0, ii = nibbles.length; i < ii; i += 2) {
  6127. out.push(parseInt(nibbles.substr(i, 2), 16));
  6128. }
  6129.  
  6130. return out;
  6131. };
  6132.  
  6133. sizeOf.REAL = function(v) {
  6134. return encode.REAL(v).length;
  6135. };
  6136.  
  6137. encode.NAME = encode.CHARARRAY;
  6138. sizeOf.NAME = sizeOf.CHARARRAY;
  6139.  
  6140. encode.STRING = encode.CHARARRAY;
  6141. sizeOf.STRING = sizeOf.CHARARRAY;
  6142.  
  6143. decode.UTF16 = function(data, offset, numBytes) {
  6144. var codePoints = [];
  6145. var numChars = numBytes / 2;
  6146. for (var j = 0; j < numChars; j++, offset += 2) {
  6147. codePoints[j] = data.getUint16(offset);
  6148. }
  6149.  
  6150. return String.fromCharCode.apply(null, codePoints);
  6151. };
  6152.  
  6153. // Convert a JavaScript string to UTF16-BE.
  6154. encode.UTF16 = function(v) {
  6155. var b = [];
  6156. for (var i = 0; i < v.length; i += 1) {
  6157. var codepoint = v.charCodeAt(i);
  6158. b.push((codepoint >> 8) & 0xFF);
  6159. b.push(codepoint & 0xFF);
  6160. }
  6161.  
  6162. return b;
  6163. };
  6164.  
  6165. sizeOf.UTF16 = function(v) {
  6166. return v.length * 2;
  6167. };
  6168.  
  6169. // Data for converting old eight-bit Macintosh encodings to Unicode.
  6170. // This representation is optimized for decoding; encoding is slower
  6171. // and needs more memory. The assumption is that all opentype.js users
  6172. // want to open fonts, but saving a font will be comperatively rare
  6173. // so it can be more expensive. Keyed by IANA character set name.
  6174. //
  6175. // Python script for generating these strings:
  6176. //
  6177. // s = u''.join([chr(c).decode('mac_greek') for c in range(128, 256)])
  6178. // print(s.encode('utf-8'))
  6179. var eightBitMacEncodings = {
  6180. 'x-mac-croatian': // Python: 'mac_croatian'
  6181. 'ÄÅÇÉÑÖÜáàâäãåçéèêëíìîïñóòôöõúùûü†°¢£§•¶ß®Š™´¨≠ŽØ∞±≤≥∆µ∂∑∏š∫ªºΩžø' +
  6182. '¿¡¬√ƒ≈Ć«Č… ÀÃÕŒœĐ—“”‘’÷◊©⁄€‹›Æ»–·‚„‰ÂćÁčÈÍÎÏÌÓÔđÒÚÛÙıˆ˜¯πË˚¸Êæˇ',
  6183. 'x-mac-cyrillic': // Python: 'mac_cyrillic'
  6184. 'АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ†°Ґ£§•¶І®©™Ђђ≠Ѓѓ∞±≤≥іµґЈЄєЇїЉљЊњ' +
  6185. 'јЅ¬√ƒ≈∆«»… ЋћЌќѕ–—“”‘’÷„ЎўЏџ№Ёёяабвгдежзийклмнопрстуфхцчшщъыьэю',
  6186. 'x-mac-gaelic':
  6187. // http://unicode.org/Public/MAPPINGS/VENDORS/APPLE/GAELIC.TXT
  6188. 'ÄÅÇÉÑÖÜáàâäãåçéèêëíìîïñóòôöõúùûü†°¢£§•¶ß®©™´¨≠ÆØḂ±≤≥ḃĊċḊḋḞḟĠġṀæø' +
  6189. 'ṁṖṗɼƒſṠ«»… ÀÃÕŒœ–—“”‘’ṡẛÿŸṪ€‹›Ŷŷṫ·Ỳỳ⁊ÂÊÁËÈÍÎÏÌÓÔ♣ÒÚÛÙıÝýŴŵẄẅẀẁẂẃ',
  6190. 'x-mac-greek': // Python: 'mac_greek'
  6191. 'Ĺ²É³ÖÜ΅àâä΄¨çéèê룙î‰ôö¦€ùûü†ΓΔΘΛΞΠß®©ΣΪ§≠°·Α±≤≥¥ΒΕΖΗΙΚΜΦΫΨΩ' +
  6192. 'άΝ¬ΟΡ≈Τ«»… ΥΧΆΈœ–―“”‘’÷ΉΊΌΎέήίόΏύαβψδεφγηιξκλμνοπώρστθωςχυζϊϋΐΰ\u00AD',
  6193. 'x-mac-icelandic': // Python: 'mac_iceland'
  6194. 'ÄÅÇÉÑÖÜáàâäãåçéèêëíìîïñóòôöõúùûüÝ°¢£§•¶ß®©™´¨≠ÆØ∞±≤≥¥µ∂∑∏π∫ªºΩæø' +
  6195. '¿¡¬√ƒ≈∆«»… ÀÃÕŒœ–—“”‘’÷◊ÿŸ⁄€ÐðÞþý·‚„‰ÂÊÁËÈÍÎÏÌÓÔÒÚÛÙıˆ˜¯˘˙˚¸˝˛ˇ',
  6196. 'x-mac-inuit':
  6197. // http://unicode.org/Public/MAPPINGS/VENDORS/APPLE/INUIT.TXT
  6198. 'ᐃᐄᐅᐆᐊᐋᐱᐲᐳᐴᐸᐹᑉᑎᑏᑐᑑᑕᑖᑦᑭᑮᑯᑰᑲᑳᒃᒋᒌᒍᒎᒐᒑ°ᒡᒥᒦ•¶ᒧ®©™ᒨᒪᒫᒻᓂᓃᓄᓅᓇᓈᓐᓯᓰᓱᓲᓴᓵᔅᓕᓖᓗ' +
  6199. 'ᓘᓚᓛᓪᔨᔩᔪᔫᔭ… ᔮᔾᕕᕖᕗ–—“”‘’ᕘᕙᕚᕝᕆᕇᕈᕉᕋᕌᕐᕿᖀᖁᖂᖃᖄᖅᖏᖐᖑᖒᖓᖔᖕᙱᙲᙳᙴᙵᙶᖖᖠᖡᖢᖣᖤᖥᖦᕼŁł',
  6200. 'x-mac-ce': // Python: 'mac_latin2'
  6201. 'ÄĀāÉĄÖÜáąČäčĆć鏟ĎíďĒēĖóėôöõúĚěü†°Ę£§•¶ß®©™ę¨≠ģĮįĪ≤≥īĶ∂∑łĻļĽľĹĺŅ' +
  6202. 'ņѬ√ńŇ∆«»… ňŐÕőŌ–—“”‘’÷◊ōŔŕŘ‹›řŖŗŠ‚„šŚśÁŤťÍŽžŪÓÔūŮÚůŰűŲųÝýķŻŁżĢˇ',
  6203. macintosh: // Python: 'mac_roman'
  6204. 'ÄÅÇÉÑÖÜáàâäãåçéèêëíìîïñóòôöõúùûü†°¢£§•¶ß®©™´¨≠ÆØ∞±≤≥¥µ∂∑∏π∫ªºΩæø' +
  6205. '¿¡¬√ƒ≈∆«»… ÀÃÕŒœ–—“”‘’÷◊ÿŸ⁄€‹›fifl‡·‚„‰ÂÊÁËÈÍÎÏÌÓÔÒÚÛÙıˆ˜¯˘˙˚¸˝˛ˇ',
  6206. 'x-mac-romanian': // Python: 'mac_romanian'
  6207. 'ÄÅÇÉÑÖÜáàâäãåçéèêëíìîïñóòôöõúùûü†°¢£§•¶ß®©™´¨≠ĂȘ∞±≤≥¥µ∂∑∏π∫ªºΩăș' +
  6208. '¿¡¬√ƒ≈∆«»… ÀÃÕŒœ–—“”‘’÷◊ÿŸ⁄€‹›Țț‡·‚„‰ÂÊÁËÈÍÎÏÌÓÔÒÚÛÙıˆ˜¯˘˙˚¸˝˛ˇ',
  6209. 'x-mac-turkish': // Python: 'mac_turkish'
  6210. 'ÄÅÇÉÑÖÜáàâäãåçéèêëíìîïñóòôöõúùûü†°¢£§•¶ß®©™´¨≠ÆØ∞±≤≥¥µ∂∑∏π∫ªºΩæø' +
  6211. '¿¡¬√ƒ≈∆«»… ÀÃÕŒœ–—“”‘’÷◊ÿŸĞğİıŞş‡·‚„‰ÂÊÁËÈÍÎÏÌÓÔÒÚÛÙˆ˜¯˘˙˚¸˝˛ˇ'
  6212. };
  6213.  
  6214. // Decodes an old-style Macintosh string. Returns either a Unicode JavaScript
  6215. // string, or 'undefined' if the encoding is unsupported. For example, we do
  6216. // not support Chinese, Japanese or Korean because these would need large
  6217. // mapping tables.
  6218. decode.MACSTRING = function(dataView, offset, dataLength, encoding) {
  6219. var table = eightBitMacEncodings[encoding];
  6220. if (table === undefined) {
  6221. return undefined;
  6222. }
  6223.  
  6224. var result = '';
  6225. for (var i = 0; i < dataLength; i++) {
  6226. var c = dataView.getUint8(offset + i);
  6227. // In all eight-bit Mac encodings, the characters 0x00..0x7F are
  6228. // mapped to U+0000..U+007F; we only need to look up the others.
  6229. if (c <= 0x7F) {
  6230. result += String.fromCharCode(c);
  6231. } else {
  6232. result += table[c & 0x7F];
  6233. }
  6234. }
  6235.  
  6236. return result;
  6237. };
  6238.  
  6239. // Helper function for encode.MACSTRING. Returns a dictionary for mapping
  6240. // Unicode character codes to their 8-bit MacOS equivalent. This table
  6241. // is not exactly a super cheap data structure, but we do not care because
  6242. // encoding Macintosh strings is only rarely needed in typical applications.
  6243. var macEncodingTableCache = typeof WeakMap === 'function' && new WeakMap();
  6244. var macEncodingCacheKeys;
  6245. var getMacEncodingTable = function(encoding) {
  6246. // Since we use encoding as a cache key for WeakMap, it has to be
  6247. // a String object and not a literal. And at least on NodeJS 2.10.1,
  6248. // WeakMap requires that the same String instance is passed for cache hits.
  6249. if (!macEncodingCacheKeys) {
  6250. macEncodingCacheKeys = {};
  6251. for (var e in eightBitMacEncodings) {
  6252. /*jshint -W053 */ // Suppress "Do not use String as a constructor."
  6253. macEncodingCacheKeys[e] = new String(e);
  6254. }
  6255. }
  6256.  
  6257. var cacheKey = macEncodingCacheKeys[encoding];
  6258. if (cacheKey === undefined) {
  6259. return undefined;
  6260. }
  6261.  
  6262. // We can't do "if (cache.has(key)) {return cache.get(key)}" here:
  6263. // since garbage collection may run at any time, it could also kick in
  6264. // between the calls to cache.has() and cache.get(). In that case,
  6265. // we would return 'undefined' even though we do support the encoding.
  6266. if (macEncodingTableCache) {
  6267. var cachedTable = macEncodingTableCache.get(cacheKey);
  6268. if (cachedTable !== undefined) {
  6269. return cachedTable;
  6270. }
  6271. }
  6272.  
  6273. var decodingTable = eightBitMacEncodings[encoding];
  6274. if (decodingTable === undefined) {
  6275. return undefined;
  6276. }
  6277.  
  6278. var encodingTable = {};
  6279. for (var i = 0; i < decodingTable.length; i++) {
  6280. encodingTable[decodingTable.charCodeAt(i)] = i + 0x80;
  6281. }
  6282.  
  6283. if (macEncodingTableCache) {
  6284. macEncodingTableCache.set(cacheKey, encodingTable);
  6285. }
  6286.  
  6287. return encodingTable;
  6288. };
  6289.  
  6290. // Encodes an old-style Macintosh string. Returns a byte array upon success.
  6291. // If the requested encoding is unsupported, or if the input string contains
  6292. // a character that cannot be expressed in the encoding, the function returns
  6293. // 'undefined'.
  6294. encode.MACSTRING = function(str, encoding) {
  6295. var table = getMacEncodingTable(encoding);
  6296. if (table === undefined) {
  6297. return undefined;
  6298. }
  6299.  
  6300. var result = [];
  6301. for (var i = 0; i < str.length; i++) {
  6302. var c = str.charCodeAt(i);
  6303.  
  6304. // In all eight-bit Mac encodings, the characters 0x00..0x7F are
  6305. // mapped to U+0000..U+007F; we only need to look up the others.
  6306. if (c >= 0x80) {
  6307. c = table[c];
  6308. if (c === undefined) {
  6309. // str contains a Unicode character that cannot be encoded
  6310. // in the requested encoding.
  6311. return undefined;
  6312. }
  6313. }
  6314.  
  6315. result.push(c);
  6316. }
  6317.  
  6318. return result;
  6319. };
  6320.  
  6321. sizeOf.MACSTRING = function(str, encoding) {
  6322. var b = encode.MACSTRING(str, encoding);
  6323. if (b !== undefined) {
  6324. return b.length;
  6325. } else {
  6326. return 0;
  6327. }
  6328. };
  6329.  
  6330. // Convert a list of values to a CFF INDEX structure.
  6331. // The values should be objects containing name / type / value.
  6332. encode.INDEX = function(l) {
  6333. var i;
  6334. //var offset, offsets, offsetEncoder, encodedOffsets, encodedOffset, data,
  6335. // dataSize, i, v;
  6336. // Because we have to know which data type to use to encode the offsets,
  6337. // we have to go through the values twice: once to encode the data and
  6338. // calculate the offets, then again to encode the offsets using the fitting data type.
  6339. var offset = 1; // First offset is always 1.
  6340. var offsets = [offset];
  6341. var data = [];
  6342. var dataSize = 0;
  6343. for (i = 0; i < l.length; i += 1) {
  6344. var v = encode.OBJECT(l[i]);
  6345. Array.prototype.push.apply(data, v);
  6346. dataSize += v.length;
  6347. offset += v.length;
  6348. offsets.push(offset);
  6349. }
  6350.  
  6351. if (data.length === 0) {
  6352. return [0, 0];
  6353. }
  6354.  
  6355. var encodedOffsets = [];
  6356. var offSize = (1 + Math.floor(Math.log(dataSize) / Math.log(2)) / 8) | 0;
  6357. var offsetEncoder = [undefined, encode.BYTE, encode.USHORT, encode.UINT24, encode.ULONG][offSize];
  6358. for (i = 0; i < offsets.length; i += 1) {
  6359. var encodedOffset = offsetEncoder(offsets[i]);
  6360. Array.prototype.push.apply(encodedOffsets, encodedOffset);
  6361. }
  6362.  
  6363. return Array.prototype.concat(encode.Card16(l.length),
  6364. encode.OffSize(offSize),
  6365. encodedOffsets,
  6366. data);
  6367. };
  6368.  
  6369. sizeOf.INDEX = function(v) {
  6370. return encode.INDEX(v).length;
  6371. };
  6372.  
  6373. // Convert an object to a CFF DICT structure.
  6374. // The keys should be numeric.
  6375. // The values should be objects containing name / type / value.
  6376. encode.DICT = function(m) {
  6377. var d = [];
  6378. var keys = Object.keys(m);
  6379. var length = keys.length;
  6380.  
  6381. for (var i = 0; i < length; i += 1) {
  6382. // Object.keys() return string keys, but our keys are always numeric.
  6383. var k = parseInt(keys[i], 0);
  6384. var v = m[k];
  6385. // Value comes before the key.
  6386. d = d.concat(encode.OPERAND(v.value, v.type));
  6387. d = d.concat(encode.OPERATOR(k));
  6388. }
  6389.  
  6390. return d;
  6391. };
  6392.  
  6393. sizeOf.DICT = function(m) {
  6394. return encode.DICT(m).length;
  6395. };
  6396.  
  6397. encode.OPERATOR = function(v) {
  6398. if (v < 1200) {
  6399. return [v];
  6400. } else {
  6401. return [12, v - 1200];
  6402. }
  6403. };
  6404.  
  6405. encode.OPERAND = function(v, type) {
  6406. var d = [];
  6407. if (Array.isArray(type)) {
  6408. for (var i = 0; i < type.length; i += 1) {
  6409. check.argument(v.length === type.length, 'Not enough arguments given for type' + type);
  6410. d = d.concat(encode.OPERAND(v[i], type[i]));
  6411. }
  6412. } else {
  6413. if (type === 'SID') {
  6414. d = d.concat(encode.NUMBER(v));
  6415. } else if (type === 'offset') {
  6416. // We make it easy for ourselves and always encode offsets as
  6417. // 4 bytes. This makes offset calculation for the top dict easier.
  6418. d = d.concat(encode.NUMBER32(v));
  6419. } else if (type === 'number') {
  6420. d = d.concat(encode.NUMBER(v));
  6421. } else if (type === 'real') {
  6422. d = d.concat(encode.REAL(v));
  6423. } else {
  6424. throw new Error('Unknown operand type ' + type);
  6425. // FIXME Add support for booleans
  6426. }
  6427. }
  6428.  
  6429. return d;
  6430. };
  6431.  
  6432. encode.OP = encode.BYTE;
  6433. sizeOf.OP = sizeOf.BYTE;
  6434.  
  6435. // memoize charstring encoding using WeakMap if available
  6436. var wmm = typeof WeakMap === 'function' && new WeakMap();
  6437. // Convert a list of CharString operations to bytes.
  6438. encode.CHARSTRING = function(ops) {
  6439. // See encode.MACSTRING for why we don't do "if (wmm && wmm.has(ops))".
  6440. if (wmm) {
  6441. var cachedValue = wmm.get(ops);
  6442. if (cachedValue !== undefined) {
  6443. return cachedValue;
  6444. }
  6445. }
  6446.  
  6447. var d = [];
  6448. var length = ops.length;
  6449.  
  6450. for (var i = 0; i < length; i += 1) {
  6451. var op = ops[i];
  6452. d = d.concat(encode[op.type](op.value));
  6453. }
  6454.  
  6455. if (wmm) {
  6456. wmm.set(ops, d);
  6457. }
  6458.  
  6459. return d;
  6460. };
  6461.  
  6462. sizeOf.CHARSTRING = function(ops) {
  6463. return encode.CHARSTRING(ops).length;
  6464. };
  6465.  
  6466. // Utility functions ////////////////////////////////////////////////////////
  6467.  
  6468. // Convert an object containing name / type / value to bytes.
  6469. encode.OBJECT = function(v) {
  6470. var encodingFunction = encode[v.type];
  6471. check.argument(encodingFunction !== undefined, 'No encoding function for type ' + v.type);
  6472. return encodingFunction(v.value);
  6473. };
  6474.  
  6475. sizeOf.OBJECT = function(v) {
  6476. var sizeOfFunction = sizeOf[v.type];
  6477. check.argument(sizeOfFunction !== undefined, 'No sizeOf function for type ' + v.type);
  6478. return sizeOfFunction(v.value);
  6479. };
  6480.  
  6481. // Convert a table object to bytes.
  6482. // A table contains a list of fields containing the metadata (name, type and default value).
  6483. // The table itself has the field values set as attributes.
  6484. encode.TABLE = function(table) {
  6485. var d = [];
  6486. var length = table.fields.length;
  6487.  
  6488. for (var i = 0; i < length; i += 1) {
  6489. var field = table.fields[i];
  6490. var encodingFunction = encode[field.type];
  6491. check.argument(encodingFunction !== undefined, 'No encoding function for field type ' + field.type);
  6492. var value = table[field.name];
  6493. if (value === undefined) {
  6494. value = field.value;
  6495. }
  6496.  
  6497. var bytes = encodingFunction(value);
  6498. d = d.concat(bytes);
  6499. }
  6500.  
  6501. return d;
  6502. };
  6503.  
  6504. sizeOf.TABLE = function(table) {
  6505. var numBytes = 0;
  6506. var length = table.fields.length;
  6507.  
  6508. for (var i = 0; i < length; i += 1) {
  6509. var field = table.fields[i];
  6510. var sizeOfFunction = sizeOf[field.type];
  6511. check.argument(sizeOfFunction !== undefined, 'No sizeOf function for field type ' + field.type);
  6512. var value = table[field.name];
  6513. if (value === undefined) {
  6514. value = field.value;
  6515. }
  6516.  
  6517. numBytes += sizeOfFunction(value);
  6518. }
  6519.  
  6520. return numBytes;
  6521. };
  6522.  
  6523. // Merge in a list of bytes.
  6524. encode.LITERAL = function(v) {
  6525. return v;
  6526. };
  6527.  
  6528. sizeOf.LITERAL = function(v) {
  6529. return v.length;
  6530. };
  6531.  
  6532. exports.decode = decode;
  6533. exports.encode = encode;
  6534. exports.sizeOf = sizeOf;
  6535.  
  6536. },{"./check":2}],29:[function(require,module,exports){
  6537. 'use strict';
  6538.  
  6539. exports.isBrowser = function() {
  6540. return typeof window !== 'undefined';
  6541. };
  6542.  
  6543. exports.isNode = function() {
  6544. return typeof window === 'undefined';
  6545. };
  6546.  
  6547. exports.nodeBufferToArrayBuffer = function(buffer) {
  6548. var ab = new ArrayBuffer(buffer.length);
  6549. var view = new Uint8Array(ab);
  6550. for (var i = 0; i < buffer.length; ++i) {
  6551. view[i] = buffer[i];
  6552. }
  6553.  
  6554. return ab;
  6555. };
  6556.  
  6557. exports.arrayBufferToNodeBuffer = function(ab) {
  6558. var buffer = new Buffer(ab.byteLength);
  6559. var view = new Uint8Array(ab);
  6560. for (var i = 0; i < buffer.length; ++i) {
  6561. buffer[i] = view[i];
  6562. }
  6563.  
  6564. return buffer;
  6565. };
  6566.  
  6567. exports.checkArgument = function(expression, message) {
  6568. if (!expression) {
  6569. throw message;
  6570. }
  6571. };
  6572.  
  6573. },{}]},{},[8])(8)
  6574. });