pdfread.js 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. /*
  2. ArozOS Office - reading a PDF (OfficePdfRead)
  3. =============================================
  4. The front half of every "Import PDF": it turns each page of a PDF into
  5. plain data an app can rebuild a document from - the text as runs of
  6. glyphs with their font, size, colour and place, the pictures as
  7. canvases, the vector drawing as shapes. It knows nothing about any app's
  8. document model; Docs reflows the result into paragraphs
  9. (docs/docs_pdfimport.js), and Slides could place it on slides.
  10. The parsing is pdf.js (common/lib/pdfjs, Apache-2.0), loaded on demand
  11. the first time a PDF is imported - it is several MB and nothing else in
  12. the suite needs it. Only the parser is used, not its renderer: the
  13. page's operator list (the content stream pdf.js has already decoded,
  14. fonts and colour spaces resolved) is walked here with the graphics and
  15. text state a PDF reader keeps (CTM, text matrix, Tc / Tw / Tz / Ts,
  16. fill and stroke colour, clip box), so every glyph comes out with its
  17. real position, size and colour. getTextContent() would be simpler but
  18. says nothing about colour, and its runs do not line up with the
  19. drawing operators that carry it.
  20. Coordinates are PDF points with the origin at the top-left of the page
  21. (pdf.js' viewport at scale 1), y growing downwards.
  22. OfficePdfRead.open(bytes, { password: fn(cb) }) -> Promise<doc>
  23. doc.numPages
  24. doc.page(n) -> Promise<page> // n counts from 1
  25. doc.close()
  26. page = {
  27. width, height, // pt
  28. runs: [{ text, x0, x1, top, bottom, base, size, font, color,
  29. invisible, angle, spaceBefore, letterSpacing,
  30. vertical }],
  31. images: [{ x0, y0, x1, y1, canvas() -> Promise<canvas|null>,
  32. mask }],
  33. shapes: [{ x0, y0, x1, y1, fill, stroke, lw, rect, curve,
  34. segs: [[x0, y0, x1, y1], ...], alpha }],
  35. links: [{ x0, y0, x1, y1, url }],
  36. render(box, scale) -> Promise<canvas> // a region, rasterized
  37. release()
  38. }
  39. A run's font is { family, bold, italic, mono, serif, symbol, raw }:
  40. family is a real family name (subset prefix and style suffix taken
  41. off, "TimesNewRomanPS-BoldMT" -> "Times New Roman"), or "" when the
  42. PDF only names it "F1".
  43. */
  44. (function () {
  45. "use strict";
  46. var BASE = (function () {
  47. var s = document.currentScript && document.currentScript.src;
  48. return s ? s.replace(/[^\/]*$/, "") : "../common/";
  49. })();
  50. var LIB = BASE + "lib/pdfjs/";
  51. var libPromise = null;
  52. function loadLib() {
  53. if (window.pdfjsLib) return Promise.resolve(window.pdfjsLib);
  54. if (libPromise) return libPromise;
  55. libPromise = new Promise(function (resolve, reject) {
  56. var s = document.createElement("script");
  57. s.src = LIB + "pdf.js";
  58. s.onload = function () {
  59. var lib = window.pdfjsLib;
  60. if (!lib) { reject(new Error("the PDF reader did not load")); return; }
  61. lib.GlobalWorkerOptions.workerSrc = LIB + "pdf.worker.js";
  62. resolve(lib);
  63. };
  64. s.onerror = function () {
  65. libPromise = null;
  66. reject(new Error("the PDF reader could not be loaded"));
  67. };
  68. document.head.appendChild(s);
  69. });
  70. return libPromise;
  71. }
  72. /* ---------------- matrices (pdf.js / canvas order) ---------------- */
  73. function mul(m1, m2) {
  74. return [
  75. m1[0] * m2[0] + m1[2] * m2[1],
  76. m1[1] * m2[0] + m1[3] * m2[1],
  77. m1[0] * m2[2] + m1[2] * m2[3],
  78. m1[1] * m2[2] + m1[3] * m2[3],
  79. m1[0] * m2[4] + m1[2] * m2[5] + m1[4],
  80. m1[1] * m2[4] + m1[3] * m2[5] + m1[5]
  81. ];
  82. }
  83. function apply(m, x, y) {
  84. return [m[0] * x + m[2] * y + m[4], m[1] * x + m[3] * y + m[5]];
  85. }
  86. function boxOf(pts) {
  87. var b = [Infinity, Infinity, -Infinity, -Infinity];
  88. for (var i = 0; i < pts.length; i++) {
  89. var p = pts[i];
  90. if (p[0] < b[0]) b[0] = p[0];
  91. if (p[1] < b[1]) b[1] = p[1];
  92. if (p[0] > b[2]) b[2] = p[0];
  93. if (p[1] > b[3]) b[3] = p[1];
  94. }
  95. return b;
  96. }
  97. function unitBox(m) {
  98. return boxOf([apply(m, 0, 0), apply(m, 1, 0), apply(m, 0, 1), apply(m, 1, 1)]);
  99. }
  100. function intersect(a, b) {
  101. if (!a) return b;
  102. if (!b) return a;
  103. return [Math.max(a[0], b[0]), Math.max(a[1], b[1]), Math.min(a[2], b[2]), Math.min(a[3], b[3])];
  104. }
  105. function emptyBox(b) { return !b || b[2] - b[0] <= 0.01 || b[3] - b[1] <= 0.01; }
  106. /* ---------------- fonts ---------------- */
  107. // the families a PDF names most, by their name with the spaces taken out
  108. var KNOWN = {};
  109. [
  110. "Arial", "Arial Narrow", "Arial Black", "Arial Rounded MT Bold", "Calibri", "Calibri Light",
  111. "Cambria", "Cambria Math", "Candara", "Consolas", "Constantia", "Corbel", "Courier New",
  112. "Georgia", "Garamond", "Helvetica", "Helvetica Neue", "Segoe UI", "Segoe UI Light",
  113. "Segoe UI Semibold", "Segoe UI Symbol", "Tahoma", "Times New Roman", "Trebuchet MS",
  114. "Verdana", "Book Antiqua", "Bookman Old Style", "Century Gothic", "Century Schoolbook",
  115. "Comic Sans MS", "Franklin Gothic Book", "Franklin Gothic Medium", "Gill Sans",
  116. "Gill Sans MT", "Lucida Console", "Lucida Sans", "Lucida Sans Unicode", "Palatino Linotype",
  117. "Palatino", "Impact", "Aptos", "Aptos Display",
  118. "Microsoft YaHei", "Microsoft JhengHei", "Microsoft JhengHei UI", "SimSun", "SimHei",
  119. "NSimSun", "KaiTi", "FangSong", "DengXian", "PMingLiU", "MingLiU", "DFKai-SB",
  120. "MS Gothic", "MS Mincho", "MS PGothic", "MS PMincho", "MS UI Gothic", "Meiryo", "Meiryo UI",
  121. "Yu Gothic", "Yu Mincho", "Malgun Gothic", "Gulim", "Batang", "Dotum", "Gungsuh",
  122. "PingFang TC", "PingFang SC", "PingFang HK", "Hiragino Sans", "Heiti TC", "Heiti SC",
  123. "Noto Sans", "Noto Serif", "Noto Sans Mono", "Noto Sans TC", "Noto Sans SC", "Noto Sans JP",
  124. "Noto Sans KR", "Noto Serif TC", "Noto Serif SC", "Noto Serif JP", "Noto Serif KR",
  125. "Roboto", "Roboto Mono", "Open Sans", "Lato", "Montserrat", "Raleway", "Poppins",
  126. "Source Sans Pro", "Source Serif Pro", "Source Code Pro", "Inter", "Ubuntu",
  127. "Liberation Sans", "Liberation Serif", "Liberation Mono", "DejaVu Sans", "DejaVu Serif",
  128. "DejaVu Sans Mono", "Carlito", "Caladea", "Symbol", "Wingdings", "Webdings"
  129. ].forEach(function (f) { KNOWN[f.toLowerCase().replace(/[^a-z0-9]/g, "")] = f; });
  130. // names a PDF uses for a family it is not called by
  131. var ALIAS = {
  132. "timesroman": "Times New Roman", "times": "Times New Roman", "timesnewromanps": "Times New Roman",
  133. "courier": "Courier New", "couriernewps": "Courier New", "arialunicodems": "Arial",
  134. "arialmt": "Arial", "helveticaneuelt": "Helvetica Neue",
  135. "notosanscjktc": "Noto Sans TC", "notosanscjksc": "Noto Sans SC", "notosanscjkjp": "Noto Sans JP",
  136. "notosanscjkkr": "Noto Sans KR", "notosanscjkhk": "Noto Sans TC",
  137. "notoserifcjktc": "Noto Serif TC", "notoserifcjksc": "Noto Serif SC",
  138. "notoserifcjkjp": "Noto Serif JP", "notoserifcjkkr": "Noto Serif KR",
  139. "sourcehansanstc": "Noto Sans TC", "sourcehansanssc": "Noto Sans SC",
  140. "sourcehansansjp": "Noto Sans JP", "sourcehansanskr": "Noto Sans KR",
  141. "sourcehansans": "Noto Sans JP", "sourcehanserif": "Noto Serif JP",
  142. "msgothic": "MS Gothic", "msmincho": "MS Mincho", "mspgothic": "MS PGothic",
  143. "mspmincho": "MS PMincho", "dfkaisb": "DFKai-SB", "dfkaishusbestdbf": "DFKai-SB",
  144. "stsong": "SimSun", "stheiti": "SimHei", "zapfdingbats": "ZapfDingbats",
  145. "zapfdingbatsitc": "ZapfDingbats"
  146. };
  147. var STYLE_WORD = /^(bold|italic|oblique|regular|roman|light|medium|semibold|semi|demibold|demi|black|heavy|thin|extralight|ultralight|extrabold|ultrabold|book|normal|condensed|cond|narrow|it|bd|bi|w\d|mt|psmt|ps|std|pro|lt)+$/i;
  148. // "ABCDEF+TimesNewRomanPS-BoldItalicMT" -> { base: "Times New Roman", style: "BoldItalic" }
  149. function splitFontName(raw) {
  150. var n = String(raw || "");
  151. // a name stated in UTF-8 reaches us as its bytes
  152. if (/[\u0080-\u00FF]/.test(n) && !/[\u0100-\uFFFF]/.test(n)) {
  153. try { n = decodeURIComponent(escape(n)); } catch (e) { /* keep it */ }
  154. }
  155. n = n.replace(/^[A-Z]{6}\+/, "");
  156. var style = "";
  157. var comma = n.indexOf(",");
  158. if (comma > 0) {
  159. style = n.substring(comma + 1);
  160. n = n.substring(0, comma);
  161. } else {
  162. var dash = n.lastIndexOf("-");
  163. if (dash > 0 && STYLE_WORD.test(n.substring(dash + 1))) {
  164. style = n.substring(dash + 1);
  165. n = n.substring(0, dash);
  166. }
  167. }
  168. // TrueType's PostScript decorations
  169. n = n.replace(/(PSMT|PS|MT)$/, "");
  170. // a style glued on without a dash ("ArialBold")
  171. var glued = n.match(/^(.*[a-z])((?:Bold|Italic|Oblique|Regular|Light|Medium|Semibold|SemiBold|Black)+)$/);
  172. if (glued) {
  173. n = glued[1];
  174. style = glued[2] + style;
  175. }
  176. return { base: n, style: style };
  177. }
  178. function familyOf(raw, info) {
  179. var parts = splitFontName(raw);
  180. var key = parts.base.toLowerCase().replace(/[^a-z0-9]/g, "");
  181. var fam = KNOWN[key] || ALIAS[key] || "";
  182. if (!fam && /^(notosanscjk|sourcehansans)/.test(key)) fam = "Noto Sans TC";
  183. if (!fam) {
  184. // a PDF that only says "F1" or "T1_0" names no family at all
  185. var junk = !parts.base || /^(cidfont\+)?(f|t|tt|c|font|ff)?[\d_]*$/i.test(parts.base) ||
  186. !/[A-Za-z]{3}/.test(parts.base);
  187. if (!junk) {
  188. fam = parts.base.replace(/[_-]+/g, " ")
  189. .replace(/([a-z])([A-Z])/g, "$1 $2")
  190. .replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2")
  191. .replace(/\s+/g, " ").trim();
  192. }
  193. }
  194. // Word's own light families
  195. if (/light/i.test(parts.style) && (fam === "Calibri" || fam === "Segoe UI")) fam += " Light";
  196. return { family: fam, style: parts.style };
  197. }
  198. function fontInfo(f) {
  199. var raw = (f && f.name) || "";
  200. var fam = familyOf(raw);
  201. var all = raw + " " + fam.style;
  202. var bold = !!(f && (f.bold || f.black)) ||
  203. /bold|black|heavy|demi|semibold|extrabold|ultrabold|w[6-9](?![0-9])/i.test(fam.style || raw.replace(/^[A-Z]{6}\+/, ""));
  204. var italic = !!(f && f.italic) || /italic|oblique|kursiv/i.test(all) || /-It$|-BoldIt$/.test(raw);
  205. // fonts whose letters are pictures (Symbol itself is mapped to real
  206. // Greek and maths by pdf.js already)
  207. var symbol = /dingbat|wingding|webding|marlett/i.test(raw);
  208. var asc = f && typeof f.ascent === "number" ? f.ascent : 0.85;
  209. var desc = f && typeof f.descent === "number" ? -f.descent : 0.2;
  210. if (!(asc > 0.4 && asc < 1.4)) asc = 0.85;
  211. if (!(desc >= 0 && desc < 0.6)) desc = 0.2;
  212. return {
  213. family: fam.family,
  214. bold: bold,
  215. italic: italic,
  216. mono: !!(f && f.isMonospace) || /courier|consolas|mono|menlo|monaco|inconsolata|lucida ?console|code/i.test(raw),
  217. serif: !!(f && f.isSerifFont),
  218. symbol: symbol,
  219. raw: raw,
  220. asc: asc,
  221. desc: desc
  222. };
  223. }
  224. /* Symbol fonts (Wingdings, Symbol) put a bullet where a letter or a
  225. private-use code point would be; the reflowed text wants the bullet
  226. itself, in a character every font has. */
  227. var SYMBOL_CHARS = {
  228. 0x6C: 0x25CF, 0x6E: 0x25A0, 0x6F: 0x25A1, 0x71: 0x2751, 0x75: 0x25C6, 0x76: 0x2756,
  229. 0xA7: 0x25AA, 0xA8: 0x25A1, 0xB7: 0x2022, 0xD8: 0x27A2, 0xE0: 0x27A2, 0xE8: 0x2794,
  230. 0xF0: 0x21E8, 0xFC: 0x2713, 0xFB: 0x2717, 0xFE: 0x2611, 0x9F: 0x2022
  231. };
  232. var BULLET = 0x2022;
  233. function cleanText(s, info) {
  234. if (!s) return "";
  235. var out = "";
  236. for (var i = 0; i < s.length; i++) {
  237. var c = s.charCodeAt(i);
  238. if (c >= 0xD800 && c <= 0xDBFF && i + 1 < s.length) {
  239. out += s.charAt(i) + s.charAt(i + 1);
  240. i++;
  241. continue;
  242. }
  243. // a symbol font's code points, moved into the private use area
  244. var pua = c >= 0xF000 && c <= 0xF0FF;
  245. if (pua) c -= 0xF000;
  246. if (info.symbol || pua) {
  247. if (c === 0x20) { out += " "; continue; }
  248. out += String.fromCharCode(SYMBOL_CHARS[c] || BULLET);
  249. continue;
  250. }
  251. if (c < 0x20 || (c >= 0x7F && c < 0xA0) || c === 0xAD) continue;
  252. if (c >= 0xE000 && c <= 0xF8FF) { out += String.fromCharCode(BULLET); continue; }
  253. if (c >= 0xFB00 && c <= 0xFB06) {
  254. var ch = String.fromCharCode(c);
  255. out += ch.normalize ? ch.normalize("NFKC") : ch;
  256. continue;
  257. }
  258. out += String.fromCharCode(c);
  259. }
  260. return out;
  261. }
  262. /* ---------------- images ---------------- */
  263. function imageDataCanvas(img, maskColor) {
  264. if (!img) return null;
  265. var w = img.width, h = img.height;
  266. if (!(w > 0 && h > 0) || w * h > 40000000) return null;
  267. var cv = document.createElement("canvas");
  268. cv.width = w;
  269. cv.height = h;
  270. var ctx = cv.getContext("2d");
  271. // a decoded bitmap (newer pdf.js) draws as it is
  272. if (!img.data || (typeof HTMLElement === "function" && img instanceof HTMLElement) ||
  273. (typeof ImageBitmap === "function" && img instanceof ImageBitmap)) {
  274. try { ctx.drawImage(img.bitmap || img, 0, 0); return cv; } catch (e) { return null; }
  275. }
  276. var out = ctx.createImageData(w, h);
  277. var d = out.data, src = img.data;
  278. var x, y, p, s;
  279. if (maskColor) {
  280. // a stencil: a clear bit is painted in the fill colour
  281. var rowBytes = (w + 7) >> 3;
  282. for (y = 0; y < h; y++) {
  283. for (x = 0; x < w; x++) {
  284. var on = !(src[y * rowBytes + (x >> 3)] & (128 >> (x & 7)));
  285. p = (y * w + x) * 4;
  286. d[p] = maskColor[0]; d[p + 1] = maskColor[1]; d[p + 2] = maskColor[2];
  287. d[p + 3] = on ? 255 : 0;
  288. }
  289. }
  290. } else if (img.kind === 1) {
  291. var rb = (w + 7) >> 3;
  292. for (y = 0; y < h; y++) {
  293. for (x = 0; x < w; x++) {
  294. var v = (src[y * rb + (x >> 3)] & (128 >> (x & 7))) ? 255 : 0;
  295. p = (y * w + x) * 4;
  296. d[p] = d[p + 1] = d[p + 2] = v;
  297. d[p + 3] = 255;
  298. }
  299. }
  300. } else if (img.kind === 2) {
  301. for (p = 0, s = 0; p < d.length; p += 4, s += 3) {
  302. d[p] = src[s]; d[p + 1] = src[s + 1]; d[p + 2] = src[s + 2]; d[p + 3] = 255;
  303. }
  304. } else {
  305. d.set(src.subarray ? src.subarray(0, d.length) : src);
  306. }
  307. ctx.putImageData(out, 0, 0);
  308. // whether the picture has any transparency, which decides how it is
  309. // stored: an opaque one can be a JPEG
  310. var opaque = !maskColor;
  311. if (opaque && img.kind !== 1 && img.kind !== 2) {
  312. for (p = 3; p < d.length; p += 4) if (d[p] < 255) { opaque = false; break; }
  313. }
  314. cv._opaque = opaque;
  315. return cv;
  316. }
  317. /* The picture as it shows on the page: drawn through its own matrix
  318. (so a rotated or mirrored image comes out as it looks) and cut to
  319. the clip box, at about its own resolution. */
  320. function placedCanvas(src, rec) {
  321. var vis = rec.vis;
  322. var M = rec.m;
  323. var w = rec.sub ? rec.sub[2] : src.width, h = rec.sub ? rec.sub[3] : src.height;
  324. var dx = Math.hypot(M[0], M[1]) || 1, dy = Math.hypot(M[2], M[3]) || 1;
  325. var density = Math.max(w / dx, h / dy);
  326. // never above the picture's own resolution, nor past 216 dpi
  327. density = Math.max(1, Math.min(3, density));
  328. var bw = vis[2] - vis[0], bh = vis[3] - vis[1];
  329. var maxSide = 4096;
  330. if (bw * density > maxSide) density = maxSide / bw;
  331. if (bh * density > maxSide) density = maxSide / bh;
  332. var cw = Math.max(1, Math.round(bw * density)), ch = Math.max(1, Math.round(bh * density));
  333. var cv = document.createElement("canvas");
  334. cv.width = cw;
  335. cv.height = ch;
  336. var ctx = cv.getContext("2d");
  337. ctx.imageSmoothingEnabled = true;
  338. try { ctx.imageSmoothingQuality = "high"; } catch (e) { }
  339. // an opaque picture on white, so its resampled edge stays opaque too
  340. if (src._opaque) {
  341. ctx.fillStyle = "#ffffff";
  342. ctx.fillRect(0, 0, cw, ch);
  343. cv._opaque = true;
  344. }
  345. ctx.setTransform(density, 0, 0, density, -vis[0] * density, -vis[1] * density);
  346. ctx.transform(M[0], M[1], M[2], M[3], M[4], M[5]);
  347. ctx.transform(1 / w, 0, 0, -1 / h, 0, 1);
  348. if (rec.sub) ctx.drawImage(src, rec.sub[0], rec.sub[1], w, h, 0, 0, w, h);
  349. else ctx.drawImage(src, 0, 0);
  350. return cv;
  351. }
  352. function objOf(page, id) {
  353. return new Promise(function (resolve) {
  354. var store = String(id).indexOf("g_") === 0 ? page.commonObjs : page.objs;
  355. var done = false;
  356. var t = setTimeout(function () { if (!done) { done = true; resolve(null); } }, 8000);
  357. try {
  358. store.get(id, function (data) {
  359. if (done) return;
  360. done = true;
  361. clearTimeout(t);
  362. resolve(data);
  363. });
  364. } catch (e) {
  365. clearTimeout(t);
  366. resolve(null);
  367. }
  368. });
  369. }
  370. /* ---------------- the operator list ---------------- */
  371. function State() {
  372. this.ctm = [1, 0, 0, 1, 0, 0];
  373. this.fill = [0, 0, 0];
  374. this.stroke = [0, 0, 0];
  375. this.fillPattern = false;
  376. this.fillAlpha = 1;
  377. this.strokeAlpha = 1;
  378. this.lineWidth = 1;
  379. this.clip = null;
  380. this.font = null;
  381. this.fontRef = null;
  382. this.fontSize = 0;
  383. this.fontDirection = 1;
  384. this.charSpacing = 0;
  385. this.wordSpacing = 0;
  386. this.hScale = 1;
  387. this.leading = 0;
  388. this.rise = 0;
  389. this.render = 0;
  390. this.tm = [1, 0, 0, 1, 0, 0];
  391. this.x = 0; this.y = 0; this.lineX = 0; this.lineY = 0;
  392. }
  393. State.prototype.clone = function () {
  394. var s = new State();
  395. for (var k in this) {
  396. if (!Object.prototype.hasOwnProperty.call(this, k)) continue;
  397. var v = this[k];
  398. s[k] = Array.isArray(v) ? v.slice() : v;
  399. }
  400. return s;
  401. };
  402. var MAX_SHAPES = 30000;
  403. var MAX_RUNS = 60000;
  404. function interpret(lib, page, opList, viewport) {
  405. var OPS = lib.OPS;
  406. var st = new State();
  407. st.ctm = viewport.transform.slice();
  408. var stack = [];
  409. var runs = [], images = [], shapes = [];
  410. var fontCache = {};
  411. // where the last space glyph ended: a writer that draws each word
  412. // (or a picture of a character) by itself draws the space between
  413. // them as text of its own, and the next thing drawn is after a space
  414. var lastSpace = null;
  415. function afterSpace(x, y, size) {
  416. var s = lastSpace;
  417. if (!s) return false;
  418. lastSpace = null;
  419. return x - s.x > -s.size * 0.3 && x - s.x < s.size * 0.8 && Math.abs(y - s.y) < Math.max(s.size, size) * 0.6;
  420. }
  421. var path = []; // [{pts, closed, rect, curve}]
  422. var pendingClip = false;
  423. var skipDepth = 0; // inside an annotation's appearance
  424. // keyed by the font's own reference: pdf.js gives every font it draws
  425. // with one system face (Helvetica and Helvetica-Bold alike) the same
  426. // loadedName
  427. function info(f, ref) {
  428. if (!ref) return fontInfo(f);
  429. if (!fontCache[ref]) fontCache[ref] = fontInfo(f);
  430. return fontCache[ref];
  431. }
  432. function setFont(name, size) {
  433. var f = null;
  434. try { f = page.commonObjs.get(name); } catch (e) { f = null; }
  435. st.font = f;
  436. st.fontRef = name;
  437. if (size < 0) { size = -size; st.fontDirection = -1; } else st.fontDirection = 1;
  438. st.fontSize = size;
  439. }
  440. function moveText(x, y) {
  441. st.x = st.lineX += x;
  442. st.y = st.lineY += y;
  443. }
  444. function showText(glyphs) {
  445. var font = st.font;
  446. if (!font || !st.fontSize || !glyphs) return;
  447. var fi = info(font, st.fontRef);
  448. var size = st.fontSize;
  449. var fm = font.fontMatrix || [0.001, 0, 0, 0.001, 0, 0];
  450. var type3 = !!font.isType3Font;
  451. var vertical = !!font.vertical;
  452. var Th = st.hScale * st.fontDirection;
  453. var spacingDir = vertical ? 1 : -1;
  454. var M = mul(st.ctm, st.tm);
  455. var sizeDev = size * Math.hypot(M[2], M[3]);
  456. var xv = Math.hypot(M[0], M[1]) || 1;
  457. var angle = Math.atan2(M[1], M[0] * (st.fontDirection < 0 ? -1 : 1)) * 180 / Math.PI;
  458. var mode = st.render & 3;
  459. var invisible = mode === 3 || (mode !== 1 && st.fillAlpha === 0);
  460. var color = (mode === 1) ? st.stroke : (st.fillPattern ? [0, 0, 0] : st.fill);
  461. var em = size; // text space units per em
  462. var x = 0;
  463. var cur = null;
  464. var breakPending = false, spaceSeen = false;
  465. function close() {
  466. if (!cur) return;
  467. var c = st.clip;
  468. // text a clip box hides is not on the page
  469. var hidden = c && (cur.x1 < c[0] - 1 || cur.x0 > c[2] + 1 || cur.bottom < c[1] - 1 || cur.top > c[3] + 1);
  470. if (cur.text && !hidden && runs.length < MAX_RUNS) runs.push(cur);
  471. cur = null;
  472. }
  473. for (var i = 0; i < glyphs.length; i++) {
  474. var g = glyphs[i];
  475. if (typeof g === "number") {
  476. var adv = spacingDir * g * size / 1000;
  477. x += adv;
  478. // a kerning offset wider than a thin space is a word gap
  479. if (adv > em * 0.18) breakPending = true;
  480. else if (adv < -em * 0.5) breakPending = true;
  481. continue;
  482. }
  483. if (!g) continue;
  484. var spacing = (g.isSpace ? st.wordSpacing : 0) + st.charSpacing;
  485. var w = g.width || 0;
  486. var gw;
  487. if (type3) gw = (w * fm[0] + 0 * fm[2]) * size;
  488. else gw = w * size * fm[0];
  489. if (vertical) {
  490. var vm = g.vmetric || font.defaultVMetrics;
  491. gw = vm ? -vm[0] * size * fm[0] : gw;
  492. }
  493. var tx = st.x + x * Th, ty = st.y + st.rise;
  494. var text = g.unicode || "";
  495. var isSpace = g.isSpace || text === " " || text === "\u00A0" || text === "\u3000";
  496. if (isSpace) {
  497. breakPending = true;
  498. spaceSeen = true;
  499. if (!vertical) {
  500. var se = apply(M, tx + gw * Th, ty);
  501. lastSpace = { x: se[0], y: se[1], size: sizeDev };
  502. }
  503. } else {
  504. var t = cleanText(text, fi);
  505. if (t) {
  506. var o = vertical ? apply(M, tx, st.y - x) : apply(M, tx, ty);
  507. var e = vertical ? o : apply(M, tx + gw * Th, ty);
  508. if (cur && breakPending) close();
  509. if (!cur) {
  510. if (afterSpace(o[0], o[1], sizeDev)) spaceSeen = true;
  511. cur = {
  512. text: "", x0: o[0], x1: e[0], base: o[1], size: sizeDev,
  513. top: o[1] - fi.asc * sizeDev, bottom: o[1] + fi.desc * sizeDev,
  514. font: fi, color: color.slice(0, 3), invisible: invisible,
  515. angle: angle, spaceBefore: spaceSeen, vertical: vertical,
  516. letterSpacing: Math.abs(st.charSpacing * xv) > sizeDev * 0.08 ? st.charSpacing * xv : 0,
  517. fakeBold: (st.render & 3) === 2
  518. };
  519. }
  520. cur.text += t;
  521. if (vertical) {
  522. cur.x0 = Math.min(cur.x0, o[0] - sizeDev / 2);
  523. cur.x1 = Math.max(cur.x1, o[0] + sizeDev / 2);
  524. cur.bottom = Math.max(cur.bottom, o[1] + sizeDev);
  525. } else {
  526. cur.x0 = Math.min(cur.x0, o[0], e[0]);
  527. cur.x1 = Math.max(cur.x1, o[0], e[0]);
  528. }
  529. breakPending = false;
  530. spaceSeen = false;
  531. }
  532. }
  533. var charW;
  534. if (type3) charW = gw + spacing;
  535. else if (vertical) charW = gw - spacing * st.fontDirection;
  536. else charW = gw + spacing * st.fontDirection;
  537. x += charW;
  538. }
  539. close();
  540. if (vertical) st.y -= x;
  541. else st.x += x * Th;
  542. }
  543. function addPath(ops, args) {
  544. var j = 0, sub = null;
  545. function point(px, py) { return apply(st.ctm, px, py); }
  546. for (var k = 0; k < ops.length; k++) {
  547. switch (ops[k]) {
  548. case OPS.rectangle:
  549. var rx = args[j++], ry = args[j++], rw = args[j++], rh = args[j++];
  550. var axis = Math.abs(st.ctm[1]) < 1e-6 && Math.abs(st.ctm[2]) < 1e-6;
  551. path.push({
  552. pts: [point(rx, ry), point(rx + rw, ry), point(rx + rw, ry + rh), point(rx, ry + rh)],
  553. closed: true, rect: axis, curve: false
  554. });
  555. sub = null;
  556. break;
  557. case OPS.moveTo:
  558. sub = { pts: [point(args[j], args[j + 1])], closed: false, rect: false, curve: false };
  559. path.push(sub);
  560. j += 2;
  561. break;
  562. case OPS.lineTo:
  563. if (!sub) { sub = { pts: [], closed: false, rect: false, curve: false }; path.push(sub); }
  564. sub.pts.push(point(args[j], args[j + 1]));
  565. j += 2;
  566. break;
  567. case OPS.curveTo:
  568. if (!sub) { sub = { pts: [], closed: false, rect: false, curve: false }; path.push(sub); }
  569. sub.pts.push(point(args[j], args[j + 1]), point(args[j + 2], args[j + 3]), point(args[j + 4], args[j + 5]));
  570. sub.curve = true;
  571. j += 6;
  572. break;
  573. case OPS.curveTo2:
  574. case OPS.curveTo3:
  575. if (!sub) { sub = { pts: [], closed: false, rect: false, curve: false }; path.push(sub); }
  576. sub.pts.push(point(args[j], args[j + 1]), point(args[j + 2], args[j + 3]));
  577. sub.curve = true;
  578. j += 4;
  579. break;
  580. case OPS.closePath:
  581. if (sub) sub.closed = true;
  582. break;
  583. }
  584. }
  585. }
  586. // what a painted path looks like, for the importer's purposes
  587. function paint(kind) {
  588. var p = path;
  589. path = [];
  590. var all = [];
  591. var curve = false;
  592. p.forEach(function (s) {
  593. all = all.concat(s.pts);
  594. if (s.curve) curve = true;
  595. });
  596. if (pendingClip) {
  597. if (all.length) st.clip = intersect(st.clip, boxOf(all));
  598. pendingClip = false;
  599. }
  600. if (!kind || !all.length || skipDepth > 0 || shapes.length >= MAX_SHAPES) return;
  601. var b = boxOf(all);
  602. var vis = intersect(b, st.clip);
  603. // (a rule is a box with no height, which still shows)
  604. if (vis[2] < vis[0] || vis[3] < vis[1]) return;
  605. var scale = (Math.hypot(st.ctm[0], st.ctm[1]) + Math.hypot(st.ctm[2], st.ctm[3])) / 2;
  606. var lw = Math.max(0.25, st.lineWidth * scale);
  607. var segs = null;
  608. if (!curve && all.length <= 64) {
  609. segs = [];
  610. p.forEach(function (s) {
  611. for (var i = 1; i < s.pts.length; i++) {
  612. segs.push([s.pts[i - 1][0], s.pts[i - 1][1], s.pts[i][0], s.pts[i][1]]);
  613. }
  614. if (s.closed && s.pts.length > 2) {
  615. segs.push([s.pts[s.pts.length - 1][0], s.pts[s.pts.length - 1][1], s.pts[0][0], s.pts[0][1]]);
  616. }
  617. });
  618. }
  619. var oneRect = p.length === 1 && p[0].rect;
  620. if (!oneRect && p.length === 1 && !curve && p[0].pts.length >= 4 && p[0].pts.length <= 5) {
  621. // a rectangle drawn as four lines
  622. var q = p[0].pts;
  623. if (q.length === 5 && Math.abs(q[4][0] - q[0][0]) < 0.01 && Math.abs(q[4][1] - q[0][1]) < 0.01) q = q.slice(0, 4);
  624. if (q.length === 4) {
  625. var ok = true;
  626. for (var i = 0; i < 4; i++) {
  627. var a = q[i], c = q[(i + 1) % 4];
  628. if (Math.abs(a[0] - c[0]) > 0.05 && Math.abs(a[1] - c[1]) > 0.05) ok = false;
  629. }
  630. oneRect = ok;
  631. }
  632. }
  633. shapes.push({
  634. x0: vis[0], y0: vis[1], x1: vis[2], y1: vis[3],
  635. fill: (kind === "F" || kind === "B") ? (st.fillPattern ? "pattern" : st.fill.slice(0, 3)) : null,
  636. stroke: (kind === "S" || kind === "B") ? st.stroke.slice(0, 3) : null,
  637. lw: lw,
  638. alpha: kind === "S" ? st.strokeAlpha : st.fillAlpha,
  639. rect: !!oneRect,
  640. curve: curve,
  641. count: all.length,
  642. segs: segs
  643. });
  644. }
  645. function addImage(rec) {
  646. if (skipDepth > 0) return;
  647. var b = unitBox(rec.m);
  648. var vis = intersect(b, st.clip);
  649. if (emptyBox(vis)) return;
  650. rec.vis = vis;
  651. // a picture standing in for a character after a space (its
  652. // baseline is near its bottom)
  653. rec.spaceBefore = afterSpace(b[0], b[3], b[3] - b[1]);
  654. images.push(rec);
  655. }
  656. var fnArray = opList.fnArray, argsArray = opList.argsArray;
  657. for (var i = 0; i < fnArray.length; i++) {
  658. var fn = fnArray[i], args = argsArray[i];
  659. switch (fn) {
  660. case OPS.beginAnnotation: skipDepth++; break;
  661. case OPS.endAnnotation: if (skipDepth > 0) skipDepth--; break;
  662. case OPS.save: stack.push(st); st = st.clone(); break;
  663. case OPS.restore: if (stack.length) st = stack.pop(); break;
  664. case OPS.transform: st.ctm = mul(st.ctm, args); break;
  665. case OPS.setLineWidth: st.lineWidth = args[0]; break;
  666. case OPS.setGState:
  667. (args[0] || []).forEach(function (kv) {
  668. if (!kv) return;
  669. if (kv[0] === "LW") st.lineWidth = kv[1];
  670. else if (kv[0] === "Font" && kv[1]) setFont(kv[1][0], kv[1][1]);
  671. else if (kv[0] === "ca") st.fillAlpha = kv[1];
  672. else if (kv[0] === "CA") st.strokeAlpha = kv[1];
  673. });
  674. break;
  675. case OPS.setFillRGBColor: st.fill = [args[0], args[1], args[2]]; st.fillPattern = false; break;
  676. case OPS.setStrokeRGBColor: st.stroke = [args[0], args[1], args[2]]; break;
  677. case OPS.setFillColorN: st.fillPattern = true; break;
  678. case OPS.beginText:
  679. st.tm = [1, 0, 0, 1, 0, 0];
  680. st.x = st.y = st.lineX = st.lineY = 0;
  681. break;
  682. case OPS.setCharSpacing: st.charSpacing = args[0]; break;
  683. case OPS.setWordSpacing: st.wordSpacing = args[0]; break;
  684. case OPS.setHScale: st.hScale = args[0] / 100; break;
  685. case OPS.setLeading: st.leading = -args[0]; break;
  686. case OPS.setFont: setFont(args[0], args[1]); break;
  687. case OPS.setTextRenderingMode: st.render = args[0]; break;
  688. case OPS.setTextRise: st.rise = args[0]; break;
  689. case OPS.moveText: moveText(args[0], args[1]); break;
  690. case OPS.setLeadingMoveText: st.leading = args[1]; moveText(args[0], args[1]); break;
  691. case OPS.setTextMatrix:
  692. st.tm = [args[0], args[1], args[2], args[3], args[4], args[5]];
  693. st.x = st.y = st.lineX = st.lineY = 0;
  694. break;
  695. case OPS.nextLine: moveText(0, st.leading); break;
  696. case OPS.showText:
  697. case OPS.showSpacedText:
  698. if (!skipDepth) showText(args[0]);
  699. break;
  700. case OPS.nextLineShowText:
  701. moveText(0, st.leading);
  702. if (!skipDepth) showText(args[0]);
  703. break;
  704. case OPS.nextLineSetSpacingShowText:
  705. st.wordSpacing = args[0];
  706. st.charSpacing = args[1];
  707. moveText(0, st.leading);
  708. if (!skipDepth) showText(args[2]);
  709. break;
  710. case OPS.constructPath: addPath(args[0], args[1]); break;
  711. case OPS.clip:
  712. case OPS.eoClip: pendingClip = true; break;
  713. case OPS.stroke:
  714. case OPS.closeStroke: paint("S"); break;
  715. case OPS.fill:
  716. case OPS.eoFill: paint("F"); break;
  717. case OPS.fillStroke:
  718. case OPS.eoFillStroke:
  719. case OPS.closeFillStroke:
  720. case OPS.closeEOFillStroke: paint("B"); break;
  721. case OPS.endPath: paint(null); break;
  722. case OPS.shadingFill:
  723. if (st.clip && !skipDepth && shapes.length < MAX_SHAPES) {
  724. shapes.push({
  725. x0: st.clip[0], y0: st.clip[1], x1: st.clip[2], y1: st.clip[3],
  726. fill: "pattern", stroke: null, lw: 0, alpha: st.fillAlpha,
  727. rect: false, curve: true, count: 8, segs: null
  728. });
  729. }
  730. break;
  731. case OPS.paintFormXObjectBegin:
  732. stack.push(st);
  733. st = st.clone();
  734. if (Array.isArray(args[0]) && args[0].length === 6) st.ctm = mul(st.ctm, args[0]);
  735. if (Array.isArray(args[1]) && args[1].length === 4) {
  736. var bb = args[1];
  737. st.clip = intersect(st.clip, boxOf([apply(st.ctm, bb[0], bb[1]), apply(st.ctm, bb[2], bb[1]),
  738. apply(st.ctm, bb[0], bb[3]), apply(st.ctm, bb[2], bb[3])]));
  739. }
  740. break;
  741. case OPS.paintFormXObjectEnd: if (stack.length) st = stack.pop(); break;
  742. case OPS.beginGroup: stack.push(st); st = st.clone(); break;
  743. case OPS.endGroup: if (stack.length) st = stack.pop(); break;
  744. case OPS.paintImageXObject:
  745. addImage({ id: args[0], m: st.ctm.slice(), clip: st.clip });
  746. break;
  747. case OPS.paintImageXObjectRepeat:
  748. for (var r = 0; r + 1 < (args[3] || []).length; r += 2) {
  749. addImage({ id: args[0], m: mul(st.ctm, [args[1], 0, 0, args[2], args[3][r], args[3][r + 1]]) });
  750. }
  751. break;
  752. case OPS.paintInlineImageXObject:
  753. addImage({ data: args[0], m: st.ctm.slice() });
  754. break;
  755. case OPS.paintInlineImageXObjectGroup:
  756. (args[1] || []).forEach(function (en) {
  757. addImage({ data: args[0], m: mul(st.ctm, en.transform), sub: [en.x, en.y, en.w, en.h] });
  758. });
  759. break;
  760. case OPS.paintImageMaskXObject:
  761. addImage({ data: args[0], m: st.ctm.slice(), mask: st.fill.slice(0, 3) });
  762. break;
  763. case OPS.paintImageMaskXObjectGroup:
  764. (args[0] || []).forEach(function (im) {
  765. addImage({ data: im, m: mul(st.ctm, im.transform), mask: st.fill.slice(0, 3) });
  766. });
  767. break;
  768. case OPS.paintImageMaskXObjectRepeat:
  769. var pos = args[5] || [];
  770. for (var q = 0; q + 1 < pos.length; q += 2) {
  771. addImage({
  772. data: args[0], mask: st.fill.slice(0, 3),
  773. m: mul(st.ctm, [args[1], args[2] || 0, args[3] || 0, args[4], pos[q], pos[q + 1]])
  774. });
  775. }
  776. break;
  777. case OPS.paintSolidColorImageMask:
  778. path = [{
  779. pts: [apply(st.ctm, 0, 0), apply(st.ctm, 1, 0), apply(st.ctm, 1, 1), apply(st.ctm, 0, 1)],
  780. closed: true, rect: true, curve: false
  781. }];
  782. paint("F");
  783. break;
  784. }
  785. }
  786. return { runs: runs, images: images, shapes: shapes };
  787. }
  788. /* ---------------- documents and pages ---------------- */
  789. /* pdf.js hands a font to the page only once the browser has loaded its
  790. face, which can be after the operator list has arrived; the walk
  791. needs every font the page sets, or their text would be lost */
  792. function fontsReady(lib, page, opList) {
  793. var OPS = lib.OPS;
  794. var names = {};
  795. var fnArray = opList.fnArray, argsArray = opList.argsArray;
  796. for (var i = 0; i < fnArray.length; i++) {
  797. if (fnArray[i] === OPS.setFont) {
  798. names[argsArray[i][0]] = true;
  799. } else if (fnArray[i] === OPS.setGState) {
  800. (argsArray[i][0] || []).forEach(function (kv) {
  801. if (kv && kv[0] === "Font" && kv[1]) names[kv[1][0]] = true;
  802. });
  803. }
  804. }
  805. return Promise.all(Object.keys(names).map(function (id) {
  806. return new Promise(function (resolve) {
  807. var done = false;
  808. var t = setTimeout(function () { if (!done) { done = true; resolve(); } }, 10000);
  809. try {
  810. page.commonObjs.get(id, function () {
  811. if (done) return;
  812. done = true;
  813. clearTimeout(t);
  814. resolve();
  815. });
  816. } catch (e) {
  817. clearTimeout(t);
  818. resolve();
  819. }
  820. });
  821. }));
  822. }
  823. function readPage(lib, pdf, n) {
  824. return pdf.getPage(n).then(function (page) {
  825. var vp = page.getViewport({ scale: 1 });
  826. return Promise.all([
  827. page.getOperatorList(),
  828. page.getAnnotations().catch(function () { return []; })
  829. ]).then(function (res) {
  830. return fontsReady(lib, page, res[0]).then(function () { return res; });
  831. }).then(function (res) {
  832. var parsed = interpret(lib, page, res[0], vp);
  833. var links = [];
  834. (res[1] || []).forEach(function (a) {
  835. if (!a || a.subtype !== "Link" || !a.rect || !/^(https?:|mailto:)/i.test(a.url || "")) return;
  836. var r = vp.convertToViewportRectangle(a.rect);
  837. links.push({
  838. x0: Math.min(r[0], r[2]), y0: Math.min(r[1], r[3]),
  839. x1: Math.max(r[0], r[2]), y1: Math.max(r[1], r[3]), url: a.url
  840. });
  841. });
  842. var images = parsed.images.map(function (rec) {
  843. return {
  844. x0: rec.vis[0], y0: rec.vis[1], x1: rec.vis[2], y1: rec.vis[3],
  845. mask: !!rec.mask,
  846. spaceBefore: !!rec.spaceBefore,
  847. canvas: function () {
  848. var get = rec.data ? Promise.resolve(rec.data) : objOf(page, rec.id);
  849. return get.then(function (img) {
  850. var src = imageDataCanvas(img, rec.mask);
  851. return src ? placedCanvas(src, rec) : null;
  852. }).catch(function () { return null; });
  853. }
  854. };
  855. });
  856. return {
  857. number: n,
  858. width: vp.width,
  859. height: vp.height,
  860. runs: parsed.runs,
  861. images: images,
  862. shapes: parsed.shapes,
  863. links: links,
  864. // a region of the page as the PDF draws it
  865. render: function (box, scale) {
  866. scale = scale || 2;
  867. var bw = Math.max(1, box[2] - box[0]), bh = Math.max(1, box[3] - box[1]);
  868. var cv = document.createElement("canvas");
  869. cv.width = Math.max(1, Math.round(bw * scale));
  870. cv.height = Math.max(1, Math.round(bh * scale));
  871. var ctx = cv.getContext("2d");
  872. ctx.fillStyle = "#ffffff";
  873. ctx.fillRect(0, 0, cv.width, cv.height);
  874. var v = page.getViewport({ scale: scale, offsetX: -box[0] * scale, offsetY: -box[1] * scale });
  875. // "print": the display intent paces itself on animation
  876. // frames, which a hidden tab never gets
  877. return page.render({ canvasContext: ctx, viewport: v, intent: "print" }).promise.then(function () { return cv; });
  878. },
  879. release: function () { try { page.cleanup(); } catch (e) { } }
  880. };
  881. });
  882. });
  883. }
  884. function open(bytes, opts) {
  885. opts = opts || {};
  886. return loadLib().then(function (lib) {
  887. var data = bytes instanceof Uint8Array ? bytes.slice() : new Uint8Array(bytes);
  888. var task = lib.getDocument({
  889. data: data,
  890. cMapUrl: LIB + "cmaps/",
  891. cMapPacked: true,
  892. isEvalSupported: false
  893. });
  894. var cancelled = false;
  895. if (opts.password) {
  896. task.onPassword = function (update, reason) {
  897. opts.password(reason === lib.PasswordResponses.INCORRECT_PASSWORD, function (pw) {
  898. if (pw === null || pw === undefined) {
  899. cancelled = true;
  900. task.destroy();
  901. return;
  902. }
  903. update(pw);
  904. });
  905. };
  906. }
  907. return task.promise.then(function (pdf) {
  908. return {
  909. numPages: pdf.numPages,
  910. page: function (n) { return readPage(lib, pdf, n); },
  911. close: function () { try { pdf.destroy(); } catch (e) { } }
  912. };
  913. }, function (err) {
  914. var msg = (err && err.message) || String(err);
  915. if (err && err.name === "PasswordException") msg = "the PDF is password protected";
  916. if (cancelled || /destroyed/i.test(msg)) msg = "cancelled";
  917. if (err && err.name === "InvalidPDFException") msg = "this is not a valid PDF file";
  918. throw new Error(msg);
  919. });
  920. });
  921. }
  922. window.OfficePdfRead = {
  923. open: open,
  924. load: loadLib,
  925. familyOf: function (raw) { return familyOf(raw).family; },
  926. // exposed for tests
  927. _splitFontName: splitFontName,
  928. _cleanText: cleanText
  929. };
  930. })();