font_renderer.js 25 KB

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