font_renderer.js 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.FontRendererFactory = void 0;
  27. var _util = require("../shared/util");
  28. var _cff_parser = require("./cff_parser");
  29. var _glyphlist = require("./glyphlist");
  30. var _encodings = require("./encodings");
  31. var _stream = require("./stream");
  32. function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  33. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  34. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  35. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  36. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
  37. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  38. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  39. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  40. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  41. var FontRendererFactory = function FontRendererFactoryClosure() {
  42. function getLong(data, offset) {
  43. return data[offset] << 24 | data[offset + 1] << 16 | data[offset + 2] << 8 | data[offset + 3];
  44. }
  45. function getUshort(data, offset) {
  46. return data[offset] << 8 | data[offset + 1];
  47. }
  48. function parseCmap(data, start, end) {
  49. var offset = getUshort(data, start + 2) === 1 ? getLong(data, start + 8) : getLong(data, start + 16);
  50. var format = getUshort(data, start + offset);
  51. var ranges, p, i;
  52. if (format === 4) {
  53. getUshort(data, start + offset + 2);
  54. var segCount = getUshort(data, start + offset + 6) >> 1;
  55. p = start + offset + 14;
  56. ranges = [];
  57. for (i = 0; i < segCount; i++, p += 2) {
  58. ranges[i] = {
  59. end: getUshort(data, p)
  60. };
  61. }
  62. p += 2;
  63. for (i = 0; i < segCount; i++, p += 2) {
  64. ranges[i].start = getUshort(data, p);
  65. }
  66. for (i = 0; i < segCount; i++, p += 2) {
  67. ranges[i].idDelta = getUshort(data, p);
  68. }
  69. for (i = 0; i < segCount; i++, p += 2) {
  70. var idOffset = getUshort(data, p);
  71. if (idOffset === 0) {
  72. continue;
  73. }
  74. ranges[i].ids = [];
  75. for (var j = 0, jj = ranges[i].end - ranges[i].start + 1; j < jj; j++) {
  76. ranges[i].ids[j] = getUshort(data, p + idOffset);
  77. idOffset += 2;
  78. }
  79. }
  80. return ranges;
  81. } else if (format === 12) {
  82. getLong(data, start + offset + 4);
  83. var groups = getLong(data, start + offset + 12);
  84. p = start + offset + 16;
  85. ranges = [];
  86. for (i = 0; i < groups; i++) {
  87. ranges.push({
  88. start: getLong(data, p),
  89. end: getLong(data, p + 4),
  90. idDelta: getLong(data, p + 8) - getLong(data, p)
  91. });
  92. p += 12;
  93. }
  94. return ranges;
  95. }
  96. throw new _util.FormatError("unsupported cmap: ".concat(format));
  97. }
  98. function parseCff(data, start, end, seacAnalysisEnabled) {
  99. var properties = {};
  100. var parser = new _cff_parser.CFFParser(new _stream.Stream(data, start, end - start), properties, seacAnalysisEnabled);
  101. var cff = parser.parse();
  102. return {
  103. glyphs: cff.charStrings.objects,
  104. subrs: cff.topDict.privateDict && cff.topDict.privateDict.subrsIndex && cff.topDict.privateDict.subrsIndex.objects,
  105. gsubrs: cff.globalSubrIndex && cff.globalSubrIndex.objects,
  106. isCFFCIDFont: cff.isCIDFont,
  107. fdSelect: cff.fdSelect,
  108. fdArray: cff.fdArray
  109. };
  110. }
  111. function parseGlyfTable(glyf, loca, isGlyphLocationsLong) {
  112. var itemSize, itemDecode;
  113. if (isGlyphLocationsLong) {
  114. itemSize = 4;
  115. itemDecode = function fontItemDecodeLong(data, offset) {
  116. return data[offset] << 24 | data[offset + 1] << 16 | data[offset + 2] << 8 | data[offset + 3];
  117. };
  118. } else {
  119. itemSize = 2;
  120. itemDecode = function fontItemDecode(data, offset) {
  121. return data[offset] << 9 | data[offset + 1] << 1;
  122. };
  123. }
  124. var glyphs = [];
  125. var startOffset = itemDecode(loca, 0);
  126. for (var j = itemSize; j < loca.length; j += itemSize) {
  127. var endOffset = itemDecode(loca, j);
  128. glyphs.push(glyf.subarray(startOffset, endOffset));
  129. startOffset = endOffset;
  130. }
  131. return glyphs;
  132. }
  133. function lookupCmap(ranges, unicode) {
  134. var code = unicode.codePointAt(0),
  135. gid = 0;
  136. var l = 0,
  137. r = ranges.length - 1;
  138. while (l < r) {
  139. var c = l + r + 1 >> 1;
  140. if (code < ranges[c].start) {
  141. r = c - 1;
  142. } else {
  143. l = c;
  144. }
  145. }
  146. if (ranges[l].start <= code && code <= ranges[l].end) {
  147. gid = ranges[l].idDelta + (ranges[l].ids ? ranges[l].ids[code - ranges[l].start] : code) & 0xFFFF;
  148. }
  149. return {
  150. charCode: code,
  151. glyphId: gid
  152. };
  153. }
  154. function compileGlyf(code, cmds, font) {
  155. function moveTo(x, y) {
  156. cmds.push({
  157. cmd: 'moveTo',
  158. args: [x, y]
  159. });
  160. }
  161. function lineTo(x, y) {
  162. cmds.push({
  163. cmd: 'lineTo',
  164. args: [x, y]
  165. });
  166. }
  167. function quadraticCurveTo(xa, ya, x, y) {
  168. cmds.push({
  169. cmd: 'quadraticCurveTo',
  170. args: [xa, ya, x, y]
  171. });
  172. }
  173. var i = 0;
  174. var numberOfContours = (code[i] << 24 | code[i + 1] << 16) >> 16;
  175. var flags;
  176. var x = 0,
  177. y = 0;
  178. i += 10;
  179. if (numberOfContours < 0) {
  180. do {
  181. flags = code[i] << 8 | code[i + 1];
  182. var glyphIndex = code[i + 2] << 8 | code[i + 3];
  183. i += 4;
  184. var arg1, arg2;
  185. if (flags & 0x01) {
  186. arg1 = (code[i] << 24 | code[i + 1] << 16) >> 16;
  187. arg2 = (code[i + 2] << 24 | code[i + 3] << 16) >> 16;
  188. i += 4;
  189. } else {
  190. arg1 = code[i++];
  191. arg2 = code[i++];
  192. }
  193. if (flags & 0x02) {
  194. x = arg1;
  195. y = arg2;
  196. } else {
  197. x = 0;
  198. y = 0;
  199. }
  200. var scaleX = 1,
  201. scaleY = 1,
  202. scale01 = 0,
  203. scale10 = 0;
  204. if (flags & 0x08) {
  205. scaleX = scaleY = (code[i] << 24 | code[i + 1] << 16) / 1073741824;
  206. i += 2;
  207. } else if (flags & 0x40) {
  208. scaleX = (code[i] << 24 | code[i + 1] << 16) / 1073741824;
  209. scaleY = (code[i + 2] << 24 | code[i + 3] << 16) / 1073741824;
  210. i += 4;
  211. } else if (flags & 0x80) {
  212. scaleX = (code[i] << 24 | code[i + 1] << 16) / 1073741824;
  213. scale01 = (code[i + 2] << 24 | code[i + 3] << 16) / 1073741824;
  214. scale10 = (code[i + 4] << 24 | code[i + 5] << 16) / 1073741824;
  215. scaleY = (code[i + 6] << 24 | code[i + 7] << 16) / 1073741824;
  216. i += 8;
  217. }
  218. var subglyph = font.glyphs[glyphIndex];
  219. if (subglyph) {
  220. cmds.push({
  221. cmd: 'save'
  222. });
  223. cmds.push({
  224. cmd: 'transform',
  225. args: [scaleX, scale01, scale10, scaleY, x, y]
  226. });
  227. compileGlyf(subglyph, cmds, font);
  228. cmds.push({
  229. cmd: 'restore'
  230. });
  231. }
  232. } while (flags & 0x20);
  233. } else {
  234. var endPtsOfContours = [];
  235. var j, jj;
  236. for (j = 0; j < numberOfContours; j++) {
  237. endPtsOfContours.push(code[i] << 8 | code[i + 1]);
  238. i += 2;
  239. }
  240. var instructionLength = code[i] << 8 | code[i + 1];
  241. i += 2 + instructionLength;
  242. var numberOfPoints = endPtsOfContours[endPtsOfContours.length - 1] + 1;
  243. var points = [];
  244. while (points.length < numberOfPoints) {
  245. flags = code[i++];
  246. var repeat = 1;
  247. if (flags & 0x08) {
  248. repeat += code[i++];
  249. }
  250. while (repeat-- > 0) {
  251. points.push({
  252. flags: flags
  253. });
  254. }
  255. }
  256. for (j = 0; j < numberOfPoints; j++) {
  257. switch (points[j].flags & 0x12) {
  258. case 0x00:
  259. x += (code[i] << 24 | code[i + 1] << 16) >> 16;
  260. i += 2;
  261. break;
  262. case 0x02:
  263. x -= code[i++];
  264. break;
  265. case 0x12:
  266. x += code[i++];
  267. break;
  268. }
  269. points[j].x = x;
  270. }
  271. for (j = 0; j < numberOfPoints; j++) {
  272. switch (points[j].flags & 0x24) {
  273. case 0x00:
  274. y += (code[i] << 24 | code[i + 1] << 16) >> 16;
  275. i += 2;
  276. break;
  277. case 0x04:
  278. y -= code[i++];
  279. break;
  280. case 0x24:
  281. y += code[i++];
  282. break;
  283. }
  284. points[j].y = y;
  285. }
  286. var startPoint = 0;
  287. for (i = 0; i < numberOfContours; i++) {
  288. var endPoint = endPtsOfContours[i];
  289. var contour = points.slice(startPoint, endPoint + 1);
  290. if (contour[0].flags & 1) {
  291. contour.push(contour[0]);
  292. } else if (contour[contour.length - 1].flags & 1) {
  293. contour.unshift(contour[contour.length - 1]);
  294. } else {
  295. var p = {
  296. flags: 1,
  297. x: (contour[0].x + contour[contour.length - 1].x) / 2,
  298. y: (contour[0].y + contour[contour.length - 1].y) / 2
  299. };
  300. contour.unshift(p);
  301. contour.push(p);
  302. }
  303. moveTo(contour[0].x, contour[0].y);
  304. for (j = 1, jj = contour.length; j < jj; j++) {
  305. if (contour[j].flags & 1) {
  306. lineTo(contour[j].x, contour[j].y);
  307. } else if (contour[j + 1].flags & 1) {
  308. quadraticCurveTo(contour[j].x, contour[j].y, contour[j + 1].x, contour[j + 1].y);
  309. j++;
  310. } else {
  311. quadraticCurveTo(contour[j].x, contour[j].y, (contour[j].x + contour[j + 1].x) / 2, (contour[j].y + contour[j + 1].y) / 2);
  312. }
  313. }
  314. startPoint = endPoint + 1;
  315. }
  316. }
  317. }
  318. function compileCharString(code, cmds, font, glyphId) {
  319. var stack = [];
  320. var x = 0,
  321. y = 0;
  322. var stems = 0;
  323. function moveTo(x, y) {
  324. cmds.push({
  325. cmd: 'moveTo',
  326. args: [x, y]
  327. });
  328. }
  329. function lineTo(x, y) {
  330. cmds.push({
  331. cmd: 'lineTo',
  332. args: [x, y]
  333. });
  334. }
  335. function bezierCurveTo(x1, y1, x2, y2, x, y) {
  336. cmds.push({
  337. cmd: 'bezierCurveTo',
  338. args: [x1, y1, x2, y2, x, y]
  339. });
  340. }
  341. function parse(code) {
  342. var i = 0;
  343. while (i < code.length) {
  344. var stackClean = false;
  345. var v = code[i++];
  346. var xa, xb, ya, yb, y1, y2, y3, n, subrCode;
  347. switch (v) {
  348. case 1:
  349. stems += stack.length >> 1;
  350. stackClean = true;
  351. break;
  352. case 3:
  353. stems += stack.length >> 1;
  354. stackClean = true;
  355. break;
  356. case 4:
  357. y += stack.pop();
  358. moveTo(x, y);
  359. stackClean = true;
  360. break;
  361. case 5:
  362. while (stack.length > 0) {
  363. x += stack.shift();
  364. y += stack.shift();
  365. lineTo(x, y);
  366. }
  367. break;
  368. case 6:
  369. while (stack.length > 0) {
  370. x += stack.shift();
  371. lineTo(x, y);
  372. if (stack.length === 0) {
  373. break;
  374. }
  375. y += stack.shift();
  376. lineTo(x, y);
  377. }
  378. break;
  379. case 7:
  380. while (stack.length > 0) {
  381. y += stack.shift();
  382. lineTo(x, y);
  383. if (stack.length === 0) {
  384. break;
  385. }
  386. x += stack.shift();
  387. lineTo(x, y);
  388. }
  389. break;
  390. case 8:
  391. while (stack.length > 0) {
  392. xa = x + stack.shift();
  393. ya = y + stack.shift();
  394. xb = xa + stack.shift();
  395. yb = ya + stack.shift();
  396. x = xb + stack.shift();
  397. y = yb + stack.shift();
  398. bezierCurveTo(xa, ya, xb, yb, x, y);
  399. }
  400. break;
  401. case 10:
  402. n = stack.pop();
  403. subrCode = null;
  404. if (font.isCFFCIDFont) {
  405. var fdIndex = font.fdSelect.getFDIndex(glyphId);
  406. if (fdIndex >= 0 && fdIndex < font.fdArray.length) {
  407. var fontDict = font.fdArray[fdIndex],
  408. subrs = void 0;
  409. if (fontDict.privateDict && fontDict.privateDict.subrsIndex) {
  410. subrs = fontDict.privateDict.subrsIndex.objects;
  411. }
  412. if (subrs) {
  413. var numSubrs = subrs.length;
  414. n += numSubrs < 1240 ? 107 : numSubrs < 33900 ? 1131 : 32768;
  415. subrCode = subrs[n];
  416. }
  417. } else {
  418. (0, _util.warn)('Invalid fd index for glyph index.');
  419. }
  420. } else {
  421. subrCode = font.subrs[n + font.subrsBias];
  422. }
  423. if (subrCode) {
  424. parse(subrCode);
  425. }
  426. break;
  427. case 11:
  428. return;
  429. case 12:
  430. v = code[i++];
  431. switch (v) {
  432. case 34:
  433. xa = x + stack.shift();
  434. xb = xa + stack.shift();
  435. y1 = y + stack.shift();
  436. x = xb + stack.shift();
  437. bezierCurveTo(xa, y, xb, y1, x, y1);
  438. xa = x + stack.shift();
  439. xb = xa + stack.shift();
  440. x = xb + stack.shift();
  441. bezierCurveTo(xa, y1, xb, y, x, y);
  442. break;
  443. case 35:
  444. xa = x + stack.shift();
  445. ya = y + stack.shift();
  446. xb = xa + stack.shift();
  447. yb = ya + stack.shift();
  448. x = xb + stack.shift();
  449. y = yb + stack.shift();
  450. bezierCurveTo(xa, ya, xb, yb, x, y);
  451. xa = x + stack.shift();
  452. ya = y + stack.shift();
  453. xb = xa + stack.shift();
  454. yb = ya + stack.shift();
  455. x = xb + stack.shift();
  456. y = yb + stack.shift();
  457. bezierCurveTo(xa, ya, xb, yb, x, y);
  458. stack.pop();
  459. break;
  460. case 36:
  461. xa = x + stack.shift();
  462. y1 = y + stack.shift();
  463. xb = xa + stack.shift();
  464. y2 = y1 + stack.shift();
  465. x = xb + stack.shift();
  466. bezierCurveTo(xa, y1, xb, y2, x, y2);
  467. xa = x + stack.shift();
  468. xb = xa + stack.shift();
  469. y3 = y2 + stack.shift();
  470. x = xb + stack.shift();
  471. bezierCurveTo(xa, y2, xb, y3, x, y);
  472. break;
  473. case 37:
  474. var x0 = x,
  475. y0 = y;
  476. xa = x + stack.shift();
  477. ya = y + stack.shift();
  478. xb = xa + stack.shift();
  479. yb = ya + stack.shift();
  480. x = xb + stack.shift();
  481. y = yb + stack.shift();
  482. bezierCurveTo(xa, ya, xb, yb, x, y);
  483. xa = x + stack.shift();
  484. ya = y + stack.shift();
  485. xb = xa + stack.shift();
  486. yb = ya + stack.shift();
  487. x = xb;
  488. y = yb;
  489. if (Math.abs(x - x0) > Math.abs(y - y0)) {
  490. x += stack.shift();
  491. } else {
  492. y += stack.shift();
  493. }
  494. bezierCurveTo(xa, ya, xb, yb, x, y);
  495. break;
  496. default:
  497. throw new _util.FormatError("unknown operator: 12 ".concat(v));
  498. }
  499. break;
  500. case 14:
  501. if (stack.length >= 4) {
  502. var achar = stack.pop();
  503. var bchar = stack.pop();
  504. y = stack.pop();
  505. x = stack.pop();
  506. cmds.push({
  507. cmd: 'save'
  508. });
  509. cmds.push({
  510. cmd: 'translate',
  511. args: [x, y]
  512. });
  513. var cmap = lookupCmap(font.cmap, String.fromCharCode(font.glyphNameMap[_encodings.StandardEncoding[achar]]));
  514. compileCharString(font.glyphs[cmap.glyphId], cmds, font, cmap.glyphId);
  515. cmds.push({
  516. cmd: 'restore'
  517. });
  518. cmap = lookupCmap(font.cmap, String.fromCharCode(font.glyphNameMap[_encodings.StandardEncoding[bchar]]));
  519. compileCharString(font.glyphs[cmap.glyphId], cmds, font, cmap.glyphId);
  520. }
  521. return;
  522. case 18:
  523. stems += stack.length >> 1;
  524. stackClean = true;
  525. break;
  526. case 19:
  527. stems += stack.length >> 1;
  528. i += stems + 7 >> 3;
  529. stackClean = true;
  530. break;
  531. case 20:
  532. stems += stack.length >> 1;
  533. i += stems + 7 >> 3;
  534. stackClean = true;
  535. break;
  536. case 21:
  537. y += stack.pop();
  538. x += stack.pop();
  539. moveTo(x, y);
  540. stackClean = true;
  541. break;
  542. case 22:
  543. x += stack.pop();
  544. moveTo(x, y);
  545. stackClean = true;
  546. break;
  547. case 23:
  548. stems += stack.length >> 1;
  549. stackClean = true;
  550. break;
  551. case 24:
  552. while (stack.length > 2) {
  553. xa = x + stack.shift();
  554. ya = y + stack.shift();
  555. xb = xa + stack.shift();
  556. yb = ya + stack.shift();
  557. x = xb + stack.shift();
  558. y = yb + stack.shift();
  559. bezierCurveTo(xa, ya, xb, yb, x, y);
  560. }
  561. x += stack.shift();
  562. y += stack.shift();
  563. lineTo(x, y);
  564. break;
  565. case 25:
  566. while (stack.length > 6) {
  567. x += stack.shift();
  568. y += stack.shift();
  569. lineTo(x, y);
  570. }
  571. xa = x + stack.shift();
  572. ya = y + stack.shift();
  573. xb = xa + stack.shift();
  574. yb = ya + stack.shift();
  575. x = xb + stack.shift();
  576. y = yb + stack.shift();
  577. bezierCurveTo(xa, ya, xb, yb, x, y);
  578. break;
  579. case 26:
  580. if (stack.length % 2) {
  581. x += stack.shift();
  582. }
  583. while (stack.length > 0) {
  584. xa = x;
  585. ya = y + stack.shift();
  586. xb = xa + stack.shift();
  587. yb = ya + stack.shift();
  588. x = xb;
  589. y = yb + stack.shift();
  590. bezierCurveTo(xa, ya, xb, yb, x, y);
  591. }
  592. break;
  593. case 27:
  594. if (stack.length % 2) {
  595. y += stack.shift();
  596. }
  597. while (stack.length > 0) {
  598. xa = x + stack.shift();
  599. ya = y;
  600. xb = xa + stack.shift();
  601. yb = ya + stack.shift();
  602. x = xb + stack.shift();
  603. y = yb;
  604. bezierCurveTo(xa, ya, xb, yb, x, y);
  605. }
  606. break;
  607. case 28:
  608. stack.push((code[i] << 24 | code[i + 1] << 16) >> 16);
  609. i += 2;
  610. break;
  611. case 29:
  612. n = stack.pop() + font.gsubrsBias;
  613. subrCode = font.gsubrs[n];
  614. if (subrCode) {
  615. parse(subrCode);
  616. }
  617. break;
  618. case 30:
  619. while (stack.length > 0) {
  620. xa = x;
  621. ya = y + stack.shift();
  622. xb = xa + stack.shift();
  623. yb = ya + stack.shift();
  624. x = xb + stack.shift();
  625. y = yb + (stack.length === 1 ? stack.shift() : 0);
  626. bezierCurveTo(xa, ya, xb, yb, x, y);
  627. if (stack.length === 0) {
  628. break;
  629. }
  630. xa = x + stack.shift();
  631. ya = y;
  632. xb = xa + stack.shift();
  633. yb = ya + stack.shift();
  634. y = yb + stack.shift();
  635. x = xb + (stack.length === 1 ? stack.shift() : 0);
  636. bezierCurveTo(xa, ya, xb, yb, x, y);
  637. }
  638. break;
  639. case 31:
  640. while (stack.length > 0) {
  641. xa = x + stack.shift();
  642. ya = y;
  643. xb = xa + stack.shift();
  644. yb = ya + stack.shift();
  645. y = yb + stack.shift();
  646. x = xb + (stack.length === 1 ? stack.shift() : 0);
  647. bezierCurveTo(xa, ya, xb, yb, x, y);
  648. if (stack.length === 0) {
  649. break;
  650. }
  651. xa = x;
  652. ya = y + stack.shift();
  653. xb = xa + stack.shift();
  654. yb = ya + stack.shift();
  655. x = xb + stack.shift();
  656. y = yb + (stack.length === 1 ? stack.shift() : 0);
  657. bezierCurveTo(xa, ya, xb, yb, x, y);
  658. }
  659. break;
  660. default:
  661. if (v < 32) {
  662. throw new _util.FormatError("unknown operator: ".concat(v));
  663. }
  664. if (v < 247) {
  665. stack.push(v - 139);
  666. } else if (v < 251) {
  667. stack.push((v - 247) * 256 + code[i++] + 108);
  668. } else if (v < 255) {
  669. stack.push(-(v - 251) * 256 - code[i++] - 108);
  670. } else {
  671. stack.push((code[i] << 24 | code[i + 1] << 16 | code[i + 2] << 8 | code[i + 3]) / 65536);
  672. i += 4;
  673. }
  674. break;
  675. }
  676. if (stackClean) {
  677. stack.length = 0;
  678. }
  679. }
  680. }
  681. parse(code);
  682. }
  683. var NOOP = [];
  684. var CompiledFont =
  685. /*#__PURE__*/
  686. function () {
  687. function CompiledFont(fontMatrix) {
  688. _classCallCheck(this, CompiledFont);
  689. if (this.constructor === CompiledFont) {
  690. (0, _util.unreachable)('Cannot initialize CompiledFont.');
  691. }
  692. this.fontMatrix = fontMatrix;
  693. this.compiledGlyphs = Object.create(null);
  694. this.compiledCharCodeToGlyphId = Object.create(null);
  695. }
  696. _createClass(CompiledFont, [{
  697. key: "getPathJs",
  698. value: function getPathJs(unicode) {
  699. var cmap = lookupCmap(this.cmap, unicode);
  700. var fn = this.compiledGlyphs[cmap.glyphId];
  701. if (!fn) {
  702. fn = this.compileGlyph(this.glyphs[cmap.glyphId], cmap.glyphId);
  703. this.compiledGlyphs[cmap.glyphId] = fn;
  704. }
  705. if (this.compiledCharCodeToGlyphId[cmap.charCode] === undefined) {
  706. this.compiledCharCodeToGlyphId[cmap.charCode] = cmap.glyphId;
  707. }
  708. return fn;
  709. }
  710. }, {
  711. key: "compileGlyph",
  712. value: function compileGlyph(code, glyphId) {
  713. if (!code || code.length === 0 || code[0] === 14) {
  714. return NOOP;
  715. }
  716. var fontMatrix = this.fontMatrix;
  717. if (this.isCFFCIDFont) {
  718. var fdIndex = this.fdSelect.getFDIndex(glyphId);
  719. if (fdIndex >= 0 && fdIndex < this.fdArray.length) {
  720. var fontDict = this.fdArray[fdIndex];
  721. fontMatrix = fontDict.getByName('FontMatrix') || _util.FONT_IDENTITY_MATRIX;
  722. } else {
  723. (0, _util.warn)('Invalid fd index for glyph index.');
  724. }
  725. }
  726. var cmds = [];
  727. cmds.push({
  728. cmd: 'save'
  729. });
  730. cmds.push({
  731. cmd: 'transform',
  732. args: fontMatrix.slice()
  733. });
  734. cmds.push({
  735. cmd: 'scale',
  736. args: ['size', '-size']
  737. });
  738. this.compileGlyphImpl(code, cmds, glyphId);
  739. cmds.push({
  740. cmd: 'restore'
  741. });
  742. return cmds;
  743. }
  744. }, {
  745. key: "compileGlyphImpl",
  746. value: function compileGlyphImpl() {
  747. (0, _util.unreachable)('Children classes should implement this.');
  748. }
  749. }, {
  750. key: "hasBuiltPath",
  751. value: function hasBuiltPath(unicode) {
  752. var cmap = lookupCmap(this.cmap, unicode);
  753. return this.compiledGlyphs[cmap.glyphId] !== undefined && this.compiledCharCodeToGlyphId[cmap.charCode] !== undefined;
  754. }
  755. }]);
  756. return CompiledFont;
  757. }();
  758. var TrueTypeCompiled =
  759. /*#__PURE__*/
  760. function (_CompiledFont) {
  761. _inherits(TrueTypeCompiled, _CompiledFont);
  762. function TrueTypeCompiled(glyphs, cmap, fontMatrix) {
  763. var _this;
  764. _classCallCheck(this, TrueTypeCompiled);
  765. _this = _possibleConstructorReturn(this, _getPrototypeOf(TrueTypeCompiled).call(this, fontMatrix || [0.000488, 0, 0, 0.000488, 0, 0]));
  766. _this.glyphs = glyphs;
  767. _this.cmap = cmap;
  768. return _this;
  769. }
  770. _createClass(TrueTypeCompiled, [{
  771. key: "compileGlyphImpl",
  772. value: function compileGlyphImpl(code, cmds) {
  773. compileGlyf(code, cmds, this);
  774. }
  775. }]);
  776. return TrueTypeCompiled;
  777. }(CompiledFont);
  778. var Type2Compiled =
  779. /*#__PURE__*/
  780. function (_CompiledFont2) {
  781. _inherits(Type2Compiled, _CompiledFont2);
  782. function Type2Compiled(cffInfo, cmap, fontMatrix, glyphNameMap) {
  783. var _this2;
  784. _classCallCheck(this, Type2Compiled);
  785. _this2 = _possibleConstructorReturn(this, _getPrototypeOf(Type2Compiled).call(this, fontMatrix || [0.001, 0, 0, 0.001, 0, 0]));
  786. _this2.glyphs = cffInfo.glyphs;
  787. _this2.gsubrs = cffInfo.gsubrs || [];
  788. _this2.subrs = cffInfo.subrs || [];
  789. _this2.cmap = cmap;
  790. _this2.glyphNameMap = glyphNameMap || (0, _glyphlist.getGlyphsUnicode)();
  791. _this2.gsubrsBias = _this2.gsubrs.length < 1240 ? 107 : _this2.gsubrs.length < 33900 ? 1131 : 32768;
  792. _this2.subrsBias = _this2.subrs.length < 1240 ? 107 : _this2.subrs.length < 33900 ? 1131 : 32768;
  793. _this2.isCFFCIDFont = cffInfo.isCFFCIDFont;
  794. _this2.fdSelect = cffInfo.fdSelect;
  795. _this2.fdArray = cffInfo.fdArray;
  796. return _this2;
  797. }
  798. _createClass(Type2Compiled, [{
  799. key: "compileGlyphImpl",
  800. value: function compileGlyphImpl(code, cmds, glyphId) {
  801. compileCharString(code, cmds, this, glyphId);
  802. }
  803. }]);
  804. return Type2Compiled;
  805. }(CompiledFont);
  806. return {
  807. create: function FontRendererFactory_create(font, seacAnalysisEnabled) {
  808. var data = new Uint8Array(font.data);
  809. var cmap, glyf, loca, cff, indexToLocFormat, unitsPerEm;
  810. var numTables = getUshort(data, 4);
  811. for (var i = 0, p = 12; i < numTables; i++, p += 16) {
  812. var tag = (0, _util.bytesToString)(data.subarray(p, p + 4));
  813. var offset = getLong(data, p + 8);
  814. var length = getLong(data, p + 12);
  815. switch (tag) {
  816. case 'cmap':
  817. cmap = parseCmap(data, offset, offset + length);
  818. break;
  819. case 'glyf':
  820. glyf = data.subarray(offset, offset + length);
  821. break;
  822. case 'loca':
  823. loca = data.subarray(offset, offset + length);
  824. break;
  825. case 'head':
  826. unitsPerEm = getUshort(data, offset + 18);
  827. indexToLocFormat = getUshort(data, offset + 50);
  828. break;
  829. case 'CFF ':
  830. cff = parseCff(data, offset, offset + length, seacAnalysisEnabled);
  831. break;
  832. }
  833. }
  834. if (glyf) {
  835. var fontMatrix = !unitsPerEm ? font.fontMatrix : [1 / unitsPerEm, 0, 0, 1 / unitsPerEm, 0, 0];
  836. return new TrueTypeCompiled(parseGlyfTable(glyf, loca, indexToLocFormat), cmap, fontMatrix);
  837. }
  838. return new Type2Compiled(cff, cmap, font.fontMatrix, font.glyphNameMap);
  839. }
  840. };
  841. }();
  842. exports.FontRendererFactory = FontRendererFactory;