2
0

font_renderer.js 24 KB

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