123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- import { renderTextLayer, updateTextLayer } from "pdfjs-lib";
- class TextLayerBuilder {
- #rotation = 0;
- #scale = 0;
- #textContentSource = null;
- constructor({
- highlighter = null,
- accessibilityManager = null,
- isOffscreenCanvasSupported = true,
- }) {
- this.textContentItemsStr = [];
- this.renderingDone = false;
- this.textDivs = [];
- this.textDivProperties = new WeakMap();
- this.textLayerRenderTask = null;
- this.highlighter = highlighter;
- this.accessibilityManager = accessibilityManager;
- this.isOffscreenCanvasSupported = isOffscreenCanvasSupported;
- this.div = document.createElement("div");
- this.div.className = "textLayer";
- this.hide();
- }
- #finishRendering() {
- this.renderingDone = true;
- const endOfContent = document.createElement("div");
- endOfContent.className = "endOfContent";
- this.div.append(endOfContent);
- this.#bindMouse();
- }
- get numTextDivs() {
- return this.textDivs.length;
- }
-
- async render(viewport) {
- if (!this.#textContentSource) {
- throw new Error('No "textContentSource" parameter specified.');
- }
- const scale = viewport.scale * (globalThis.devicePixelRatio || 1);
- const { rotation } = viewport;
- if (this.renderingDone) {
- const mustRotate = rotation !== this.#rotation;
- const mustRescale = scale !== this.#scale;
- if (mustRotate || mustRescale) {
- this.hide();
- updateTextLayer({
- container: this.div,
- viewport,
- textDivs: this.textDivs,
- textDivProperties: this.textDivProperties,
- isOffscreenCanvasSupported: this.isOffscreenCanvasSupported,
- mustRescale,
- mustRotate,
- });
- this.#scale = scale;
- this.#rotation = rotation;
- }
- this.show();
- return;
- }
- this.cancel();
- this.highlighter?.setTextMapping(this.textDivs, this.textContentItemsStr);
- this.accessibilityManager?.setTextMapping(this.textDivs);
- this.textLayerRenderTask = renderTextLayer({
- textContentSource: this.#textContentSource,
- container: this.div,
- viewport,
- textDivs: this.textDivs,
- textDivProperties: this.textDivProperties,
- textContentItemsStr: this.textContentItemsStr,
- isOffscreenCanvasSupported: this.isOffscreenCanvasSupported,
- });
- await this.textLayerRenderTask.promise;
- this.#finishRendering();
- this.#scale = scale;
- this.#rotation = rotation;
- this.show();
- this.accessibilityManager?.enable();
- }
- hide() {
- if (!this.div.hidden) {
-
-
- this.highlighter?.disable();
- this.div.hidden = true;
- }
- }
- show() {
- if (this.div.hidden && this.renderingDone) {
- this.div.hidden = false;
- this.highlighter?.enable();
- }
- }
-
- cancel() {
- if (this.textLayerRenderTask) {
- this.textLayerRenderTask.cancel();
- this.textLayerRenderTask = null;
- }
- this.highlighter?.disable();
- this.accessibilityManager?.disable();
- this.textContentItemsStr.length = 0;
- this.textDivs.length = 0;
- this.textDivProperties = new WeakMap();
- }
-
- setTextContentSource(source) {
- this.cancel();
- this.#textContentSource = source;
- }
-
- #bindMouse() {
- const { div } = this;
- div.addEventListener("mousedown", evt => {
- const end = div.querySelector(".endOfContent");
- if (!end) {
- return;
- }
- if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) {
-
-
-
-
- let adjustTop = evt.target !== div;
- if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
- adjustTop &&=
- getComputedStyle(end).getPropertyValue("-moz-user-select") !==
- "none";
- }
- if (adjustTop) {
- const divBounds = div.getBoundingClientRect();
- const r = Math.max(0, (evt.pageY - divBounds.top) / divBounds.height);
- end.style.top = (r * 100).toFixed(2) + "%";
- }
- }
- end.classList.add("active");
- });
- div.addEventListener("mouseup", () => {
- const end = div.querySelector(".endOfContent");
- if (!end) {
- return;
- }
- if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) {
- end.style.top = "";
- }
- end.classList.remove("active");
- });
- }
- }
- export { TextLayerBuilder };
|