display_utils.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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.StatTimer = exports.RenderingCancelledException = exports.PixelsPerInch = exports.PageViewport = exports.PDFDateString = exports.DOMStandardFontDataFactory = exports.DOMSVGFactory = exports.DOMCanvasFactory = exports.DOMCMapReaderFactory = void 0;
  27. exports.binarySearchFirstItem = binarySearchFirstItem;
  28. exports.deprecated = deprecated;
  29. exports.getColorValues = getColorValues;
  30. exports.getFilenameFromUrl = getFilenameFromUrl;
  31. exports.getPdfFilenameFromUrl = getPdfFilenameFromUrl;
  32. exports.getRGB = getRGB;
  33. exports.getXfaPageViewport = getXfaPageViewport;
  34. exports.isDataScheme = isDataScheme;
  35. exports.isPdfFile = isPdfFile;
  36. exports.isValidFetchUrl = isValidFetchUrl;
  37. exports.loadScript = loadScript;
  38. var _base_factory = require("./base_factory.js");
  39. var _util = require("../shared/util.js");
  40. const SVG_NS = "http://www.w3.org/2000/svg";
  41. class PixelsPerInch {
  42. static CSS = 96.0;
  43. static PDF = 72.0;
  44. static PDF_TO_CSS_UNITS = this.CSS / this.PDF;
  45. }
  46. exports.PixelsPerInch = PixelsPerInch;
  47. class DOMCanvasFactory extends _base_factory.BaseCanvasFactory {
  48. constructor({
  49. ownerDocument = globalThis.document
  50. } = {}) {
  51. super();
  52. this._document = ownerDocument;
  53. }
  54. _createCanvas(width, height) {
  55. const canvas = this._document.createElement("canvas");
  56. canvas.width = width;
  57. canvas.height = height;
  58. return canvas;
  59. }
  60. }
  61. exports.DOMCanvasFactory = DOMCanvasFactory;
  62. async function fetchData(url, asTypedArray = false) {
  63. if (isValidFetchUrl(url, document.baseURI)) {
  64. const response = await fetch(url);
  65. if (!response.ok) {
  66. throw new Error(response.statusText);
  67. }
  68. return asTypedArray ? new Uint8Array(await response.arrayBuffer()) : (0, _util.stringToBytes)(await response.text());
  69. }
  70. return new Promise((resolve, reject) => {
  71. const request = new XMLHttpRequest();
  72. request.open("GET", url, true);
  73. if (asTypedArray) {
  74. request.responseType = "arraybuffer";
  75. }
  76. request.onreadystatechange = () => {
  77. if (request.readyState !== XMLHttpRequest.DONE) {
  78. return;
  79. }
  80. if (request.status === 200 || request.status === 0) {
  81. let data;
  82. if (asTypedArray && request.response) {
  83. data = new Uint8Array(request.response);
  84. } else if (!asTypedArray && request.responseText) {
  85. data = (0, _util.stringToBytes)(request.responseText);
  86. }
  87. if (data) {
  88. resolve(data);
  89. return;
  90. }
  91. }
  92. reject(new Error(request.statusText));
  93. };
  94. request.send(null);
  95. });
  96. }
  97. class DOMCMapReaderFactory extends _base_factory.BaseCMapReaderFactory {
  98. _fetchData(url, compressionType) {
  99. return fetchData(url, this.isCompressed).then(data => {
  100. return {
  101. cMapData: data,
  102. compressionType
  103. };
  104. });
  105. }
  106. }
  107. exports.DOMCMapReaderFactory = DOMCMapReaderFactory;
  108. class DOMStandardFontDataFactory extends _base_factory.BaseStandardFontDataFactory {
  109. _fetchData(url) {
  110. return fetchData(url, true);
  111. }
  112. }
  113. exports.DOMStandardFontDataFactory = DOMStandardFontDataFactory;
  114. class DOMSVGFactory extends _base_factory.BaseSVGFactory {
  115. _createSVG(type) {
  116. return document.createElementNS(SVG_NS, type);
  117. }
  118. }
  119. exports.DOMSVGFactory = DOMSVGFactory;
  120. class PageViewport {
  121. constructor({
  122. viewBox,
  123. scale,
  124. rotation,
  125. offsetX = 0,
  126. offsetY = 0,
  127. dontFlip = false
  128. }) {
  129. this.viewBox = viewBox;
  130. this.scale = scale;
  131. this.rotation = rotation;
  132. this.offsetX = offsetX;
  133. this.offsetY = offsetY;
  134. const centerX = (viewBox[2] + viewBox[0]) / 2;
  135. const centerY = (viewBox[3] + viewBox[1]) / 2;
  136. let rotateA, rotateB, rotateC, rotateD;
  137. rotation %= 360;
  138. if (rotation < 0) {
  139. rotation += 360;
  140. }
  141. switch (rotation) {
  142. case 180:
  143. rotateA = -1;
  144. rotateB = 0;
  145. rotateC = 0;
  146. rotateD = 1;
  147. break;
  148. case 90:
  149. rotateA = 0;
  150. rotateB = 1;
  151. rotateC = 1;
  152. rotateD = 0;
  153. break;
  154. case 270:
  155. rotateA = 0;
  156. rotateB = -1;
  157. rotateC = -1;
  158. rotateD = 0;
  159. break;
  160. case 0:
  161. rotateA = 1;
  162. rotateB = 0;
  163. rotateC = 0;
  164. rotateD = -1;
  165. break;
  166. default:
  167. throw new Error("PageViewport: Invalid rotation, must be a multiple of 90 degrees.");
  168. }
  169. if (dontFlip) {
  170. rotateC = -rotateC;
  171. rotateD = -rotateD;
  172. }
  173. let offsetCanvasX, offsetCanvasY;
  174. let width, height;
  175. if (rotateA === 0) {
  176. offsetCanvasX = Math.abs(centerY - viewBox[1]) * scale + offsetX;
  177. offsetCanvasY = Math.abs(centerX - viewBox[0]) * scale + offsetY;
  178. width = Math.abs(viewBox[3] - viewBox[1]) * scale;
  179. height = Math.abs(viewBox[2] - viewBox[0]) * scale;
  180. } else {
  181. offsetCanvasX = Math.abs(centerX - viewBox[0]) * scale + offsetX;
  182. offsetCanvasY = Math.abs(centerY - viewBox[1]) * scale + offsetY;
  183. width = Math.abs(viewBox[2] - viewBox[0]) * scale;
  184. height = Math.abs(viewBox[3] - viewBox[1]) * scale;
  185. }
  186. this.transform = [rotateA * scale, rotateB * scale, rotateC * scale, rotateD * scale, offsetCanvasX - rotateA * scale * centerX - rotateC * scale * centerY, offsetCanvasY - rotateB * scale * centerX - rotateD * scale * centerY];
  187. this.width = width;
  188. this.height = height;
  189. }
  190. clone({
  191. scale = this.scale,
  192. rotation = this.rotation,
  193. offsetX = this.offsetX,
  194. offsetY = this.offsetY,
  195. dontFlip = false
  196. } = {}) {
  197. return new PageViewport({
  198. viewBox: this.viewBox.slice(),
  199. scale,
  200. rotation,
  201. offsetX,
  202. offsetY,
  203. dontFlip
  204. });
  205. }
  206. convertToViewportPoint(x, y) {
  207. return _util.Util.applyTransform([x, y], this.transform);
  208. }
  209. convertToViewportRectangle(rect) {
  210. const topLeft = _util.Util.applyTransform([rect[0], rect[1]], this.transform);
  211. const bottomRight = _util.Util.applyTransform([rect[2], rect[3]], this.transform);
  212. return [topLeft[0], topLeft[1], bottomRight[0], bottomRight[1]];
  213. }
  214. convertToPdfPoint(x, y) {
  215. return _util.Util.applyInverseTransform([x, y], this.transform);
  216. }
  217. }
  218. exports.PageViewport = PageViewport;
  219. class RenderingCancelledException extends _util.BaseException {
  220. constructor(msg, type) {
  221. super(msg, "RenderingCancelledException");
  222. this.type = type;
  223. }
  224. }
  225. exports.RenderingCancelledException = RenderingCancelledException;
  226. function isDataScheme(url) {
  227. const ii = url.length;
  228. let i = 0;
  229. while (i < ii && url[i].trim() === "") {
  230. i++;
  231. }
  232. return url.substring(i, i + 5).toLowerCase() === "data:";
  233. }
  234. function isPdfFile(filename) {
  235. return typeof filename === "string" && /\.pdf$/i.test(filename);
  236. }
  237. function getFilenameFromUrl(url) {
  238. const anchor = url.indexOf("#");
  239. const query = url.indexOf("?");
  240. const end = Math.min(anchor > 0 ? anchor : url.length, query > 0 ? query : url.length);
  241. return url.substring(url.lastIndexOf("/", end) + 1, end);
  242. }
  243. function getPdfFilenameFromUrl(url, defaultFilename = "document.pdf") {
  244. if (typeof url !== "string") {
  245. return defaultFilename;
  246. }
  247. if (isDataScheme(url)) {
  248. (0, _util.warn)('getPdfFilenameFromUrl: ignore "data:"-URL for performance reasons.');
  249. return defaultFilename;
  250. }
  251. const reURI = /^(?:(?:[^:]+:)?\/\/[^/]+)?([^?#]*)(\?[^#]*)?(#.*)?$/;
  252. const reFilename = /[^/?#=]+\.pdf\b(?!.*\.pdf\b)/i;
  253. const splitURI = reURI.exec(url);
  254. let suggestedFilename = reFilename.exec(splitURI[1]) || reFilename.exec(splitURI[2]) || reFilename.exec(splitURI[3]);
  255. if (suggestedFilename) {
  256. suggestedFilename = suggestedFilename[0];
  257. if (suggestedFilename.includes("%")) {
  258. try {
  259. suggestedFilename = reFilename.exec(decodeURIComponent(suggestedFilename))[0];
  260. } catch (ex) {}
  261. }
  262. }
  263. return suggestedFilename || defaultFilename;
  264. }
  265. class StatTimer {
  266. constructor() {
  267. this.started = Object.create(null);
  268. this.times = [];
  269. }
  270. time(name) {
  271. if (name in this.started) {
  272. (0, _util.warn)(`Timer is already running for ${name}`);
  273. }
  274. this.started[name] = Date.now();
  275. }
  276. timeEnd(name) {
  277. if (!(name in this.started)) {
  278. (0, _util.warn)(`Timer has not been started for ${name}`);
  279. }
  280. this.times.push({
  281. name,
  282. start: this.started[name],
  283. end: Date.now()
  284. });
  285. delete this.started[name];
  286. }
  287. toString() {
  288. const outBuf = [];
  289. let longest = 0;
  290. for (const time of this.times) {
  291. const name = time.name;
  292. if (name.length > longest) {
  293. longest = name.length;
  294. }
  295. }
  296. for (const time of this.times) {
  297. const duration = time.end - time.start;
  298. outBuf.push(`${time.name.padEnd(longest)} ${duration}ms\n`);
  299. }
  300. return outBuf.join("");
  301. }
  302. }
  303. exports.StatTimer = StatTimer;
  304. function isValidFetchUrl(url, baseUrl) {
  305. try {
  306. const {
  307. protocol
  308. } = baseUrl ? new URL(url, baseUrl) : new URL(url);
  309. return protocol === "http:" || protocol === "https:";
  310. } catch (ex) {
  311. return false;
  312. }
  313. }
  314. function loadScript(src, removeScriptElement = false) {
  315. return new Promise((resolve, reject) => {
  316. const script = document.createElement("script");
  317. script.src = src;
  318. script.onload = function (evt) {
  319. if (removeScriptElement) {
  320. script.remove();
  321. }
  322. resolve(evt);
  323. };
  324. script.onerror = function () {
  325. reject(new Error(`Cannot load script at: ${script.src}`));
  326. };
  327. (document.head || document.documentElement).append(script);
  328. });
  329. }
  330. function deprecated(details) {
  331. console.log("Deprecated API usage: " + details);
  332. }
  333. let pdfDateStringRegex;
  334. class PDFDateString {
  335. static toDateObject(input) {
  336. if (!input || typeof input !== "string") {
  337. return null;
  338. }
  339. if (!pdfDateStringRegex) {
  340. pdfDateStringRegex = new RegExp("^D:" + "(\\d{4})" + "(\\d{2})?" + "(\\d{2})?" + "(\\d{2})?" + "(\\d{2})?" + "(\\d{2})?" + "([Z|+|-])?" + "(\\d{2})?" + "'?" + "(\\d{2})?" + "'?");
  341. }
  342. const matches = pdfDateStringRegex.exec(input);
  343. if (!matches) {
  344. return null;
  345. }
  346. const year = parseInt(matches[1], 10);
  347. let month = parseInt(matches[2], 10);
  348. month = month >= 1 && month <= 12 ? month - 1 : 0;
  349. let day = parseInt(matches[3], 10);
  350. day = day >= 1 && day <= 31 ? day : 1;
  351. let hour = parseInt(matches[4], 10);
  352. hour = hour >= 0 && hour <= 23 ? hour : 0;
  353. let minute = parseInt(matches[5], 10);
  354. minute = minute >= 0 && minute <= 59 ? minute : 0;
  355. let second = parseInt(matches[6], 10);
  356. second = second >= 0 && second <= 59 ? second : 0;
  357. const universalTimeRelation = matches[7] || "Z";
  358. let offsetHour = parseInt(matches[8], 10);
  359. offsetHour = offsetHour >= 0 && offsetHour <= 23 ? offsetHour : 0;
  360. let offsetMinute = parseInt(matches[9], 10) || 0;
  361. offsetMinute = offsetMinute >= 0 && offsetMinute <= 59 ? offsetMinute : 0;
  362. if (universalTimeRelation === "-") {
  363. hour += offsetHour;
  364. minute += offsetMinute;
  365. } else if (universalTimeRelation === "+") {
  366. hour -= offsetHour;
  367. minute -= offsetMinute;
  368. }
  369. return new Date(Date.UTC(year, month, day, hour, minute, second));
  370. }
  371. }
  372. exports.PDFDateString = PDFDateString;
  373. function getXfaPageViewport(xfaPage, {
  374. scale = 1,
  375. rotation = 0
  376. }) {
  377. const {
  378. width,
  379. height
  380. } = xfaPage.attributes.style;
  381. const viewBox = [0, 0, parseInt(width), parseInt(height)];
  382. return new PageViewport({
  383. viewBox,
  384. scale,
  385. rotation
  386. });
  387. }
  388. function getRGB(color) {
  389. if (color.startsWith("#")) {
  390. const colorRGB = parseInt(color.slice(1), 16);
  391. return [(colorRGB & 0xff0000) >> 16, (colorRGB & 0x00ff00) >> 8, colorRGB & 0x0000ff];
  392. }
  393. if (color.startsWith("rgb(")) {
  394. return color.slice(4, -1).split(",").map(x => parseInt(x));
  395. }
  396. if (color.startsWith("rgba(")) {
  397. return color.slice(5, -1).split(",").map(x => parseInt(x)).slice(0, 3);
  398. }
  399. (0, _util.warn)(`Not a valid color format: "${color}"`);
  400. return [0, 0, 0];
  401. }
  402. function getColorValues(colors) {
  403. const span = document.createElement("span");
  404. span.style.visibility = "hidden";
  405. document.body.append(span);
  406. for (const name of colors.keys()) {
  407. span.style.color = name;
  408. const computedColor = window.getComputedStyle(span).color;
  409. colors.set(name, getRGB(computedColor));
  410. }
  411. span.remove();
  412. }
  413. function binarySearchFirstItem(items, condition, start = 0) {
  414. let minIndex = start;
  415. let maxIndex = items.length - 1;
  416. if (maxIndex < 0 || !condition(items[maxIndex])) {
  417. return items.length;
  418. }
  419. if (condition(items[minIndex])) {
  420. return minIndex;
  421. }
  422. while (minIndex < maxIndex) {
  423. const currentIndex = minIndex + maxIndex >> 1;
  424. const currentItem = items[currentIndex];
  425. if (condition(currentItem)) {
  426. maxIndex = currentIndex;
  427. } else {
  428. minIndex = currentIndex + 1;
  429. }
  430. }
  431. return minIndex;
  432. }