Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set the dimensions of the various layers at their creation #15770

Merged
merged 1 commit into from
Dec 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 3 additions & 18 deletions src/display/annotation_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
DOMSVGFactory,
getFilenameFromUrl,
PDFDateString,
setLayerDimensions,
} from "./display_utils.js";
import { AnnotationStorage } from "./annotation_storage.js";
import { ColorConverters } from "../shared/scripting_utils.js";
Expand Down Expand Up @@ -2593,7 +2594,6 @@ class AnnotationLayer {
static render(parameters) {
const { annotations, div, viewport, accessibilityManager } = parameters;

this.#setDimensions(div, viewport);
let zIndex = 0;

for (const data of annotations) {
Expand Down Expand Up @@ -2660,6 +2660,7 @@ class AnnotationLayer {
}

this.#setAnnotationCanvasMap(div, parameters.annotationCanvasMap);
setLayerDimensions(div, viewport);
}

/**
Expand All @@ -2672,27 +2673,11 @@ class AnnotationLayer {
static update(parameters) {
const { annotationCanvasMap, div, viewport } = parameters;

this.#setDimensions(div, viewport);
this.#setAnnotationCanvasMap(div, annotationCanvasMap);
setLayerDimensions(div, { rotation: viewport.rotation });
div.hidden = false;
}

/**
* @param {HTMLDivElement} div
* @param {PageViewport} viewport
*/
static #setDimensions(div, { width, height, rotation }) {
const { style } = div;

const flipOrientation = rotation % 180 !== 0,
widthStr = Math.floor(width) + "px",
heightStr = Math.floor(height) + "px";

style.width = flipOrientation ? heightStr : widthStr;
style.height = flipOrientation ? widthStr : heightStr;
div.setAttribute("data-main-rotation", rotation);
}

static #setAnnotationCanvasMap(div, annotationCanvasMap) {
if (!annotationCanvasMap) {
return;
Expand Down
43 changes: 43 additions & 0 deletions src/display/display_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,48 @@ function getCurrentTransformInverse(ctx) {
return [a, b, c, d, e, f];
}

/**
* @param {HTMLDivElement} div
* @param {PageViewport} viewport
* @param {boolean} mustFlip
* @param {boolean} mustRotate
*/
function setLayerDimensions(
div,
viewport,
mustFlip = false,
mustRotate = true
) {
const { viewBox, rotation } = viewport;
if (viewBox) {
const [pageLLx, pageLLy, pageURx, pageURy] = viewBox;
const pageWidth = pageURx - pageLLx;
const pageHeight = pageURy - pageLLy;
const { style } = div;

// TODO: Investigate if it could be interesting to use the css round
// function (https://developer.mozilla.org/en-US/docs/Web/CSS/round):
// const widthStr =
// `round(down, var(--scale-factor) * ${pageWidth}px, 1px)`;
// const heightStr =
// `round(down, var(--scale-factor) * ${pageHeight}px, 1px)`;
const widthStr = `calc(var(--scale-factor) * ${pageWidth}px)`;
const heightStr = `calc(var(--scale-factor) * ${pageHeight}px)`;

if (!mustFlip || rotation % 180 === 0) {
style.width = widthStr;
style.height = heightStr;
} else {
style.width = heightStr;
style.height = widthStr;
}
}

if (mustRotate) {
div.setAttribute("data-main-rotation", rotation);
}
}

export {
AnnotationPrefix,
deprecated,
Expand All @@ -636,5 +678,6 @@ export {
PDFDateString,
PixelsPerInch,
RenderingCancelledException,
setLayerDimensions,
StatTimer,
};
38 changes: 15 additions & 23 deletions src/display/editor/annotation_editor_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
/** @typedef {import("./editor.js").AnnotationEditor} AnnotationEditor */
// eslint-disable-next-line max-len
/** @typedef {import("./tools.js").AnnotationEditorUIManager} AnnotationEditorUIManager */
/** @typedef {import("../display_utils.js").PageViewport} PageViewport */
// eslint-disable-next-line max-len
/** @typedef {import("../../web/text_accessibility.js").TextAccessibilityManager} TextAccessibilityManager */
/** @typedef {import("../../web/interfaces").IL10n} IL10n */
Expand All @@ -24,6 +25,7 @@ import { AnnotationEditorType, FeatureTest } from "../../shared/util.js";
import { bindEvents } from "./tools.js";
import { FreeTextEditor } from "./freetext.js";
import { InkEditor } from "./ink.js";
import { setLayerDimensions } from "../display_utils.js";

/**
* @typedef {Object} AnnotationEditorLayerOptions
Expand All @@ -36,6 +38,11 @@ import { InkEditor } from "./ink.js";
* @property {IL10n} l10n
*/

/**
* @typedef {Object} RenderEditorLayerOptions
* @property {PageViewport} viewport
*/

/**
* Manage all the different editors on a page.
*/
Expand Down Expand Up @@ -529,12 +536,12 @@ class AnnotationEditorLayer {

/**
* Render the main editor.
* @param {Object} parameters
* @param {RenderEditorLayerOptions} parameters
*/
render(parameters) {
this.viewport = parameters.viewport;
render({ viewport }) {
this.viewport = viewport;
setLayerDimensions(this.div, viewport);
bindEvents(this, this.div, ["dragover", "drop"]);
this.setDimensions();
for (const editor of this.#uiManager.getEditors(this.pageIndex)) {
this.add(editor);
}
Expand All @@ -543,16 +550,16 @@ class AnnotationEditorLayer {

/**
* Update the main editor.
* @param {Object} parameters
* @param {RenderEditorLayerOptions} parameters
*/
update(parameters) {
update({ viewport }) {
// Editors have their dimensions/positions in percent so to avoid any
// issues (see #15582), we must commit the current one before changing
// the viewport.
this.#uiManager.commitOrRemove();

this.viewport = parameters.viewport;
this.setDimensions();
this.viewport = viewport;
setLayerDimensions(this.div, { rotation: viewport.rotation });
this.updateMode();
}

Expand All @@ -567,21 +574,6 @@ class AnnotationEditorLayer {

return [width, height];
}

/**
* Set the dimensions of the main div.
*/
setDimensions() {
const { width, height, rotation } = this.viewport;

const flipOrientation = rotation % 180 !== 0,
widthStr = Math.floor(width) + "px",
heightStr = Math.floor(height) + "px";

this.div.style.width = flipOrientation ? heightStr : widthStr;
this.div.style.height = flipOrientation ? widthStr : heightStr;
this.div.setAttribute("data-main-rotation", rotation);
}
}

export { AnnotationEditorLayer };
26 changes: 3 additions & 23 deletions src/display/text_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
FeatureTest,
Util,
} from "../shared/util.js";
import { deprecated } from "./display_utils.js";
import { deprecated, setLayerDimensions } from "./display_utils.js";

/**
* Text layer render parameters.
Expand Down Expand Up @@ -330,7 +330,7 @@ class TextLayerRenderTask {
this._pageWidth = pageURx - pageLLx;
this._pageHeight = pageURy - pageLLy;

setTextLayerDimensions(container, viewport);
setLayerDimensions(container, viewport);

// Always clean-up the temporary canvas once rendering is no longer pending.
this._capability.promise
Expand Down Expand Up @@ -485,7 +485,7 @@ function updateTextLayer({
mustRescale = true,
}) {
if (mustRotate) {
setTextLayerDimensions(container, { rotation: viewport.rotation });
setLayerDimensions(container, { rotation: viewport.rotation });
}

if (mustRescale) {
Expand All @@ -507,24 +507,4 @@ function updateTextLayer({
}
}

/**
* @param {HTMLDivElement} div
* @param {import("./display_utils").PageViewport} viewport
*/
function setTextLayerDimensions(div, viewport) {
if (!viewport.viewBox) {
div.setAttribute("data-main-rotation", viewport.rotation);
return;
}

const [pageLLx, pageLLy, pageURx, pageURy] = viewport.viewBox;
const pageWidth = pageURx - pageLLx;
const pageHeight = pageURy - pageLLy;
const { style } = div;

style.width = `calc(var(--scale-factor) * ${pageWidth}px)`;
style.height = `calc(var(--scale-factor) * ${pageHeight}px)`;
div.setAttribute("data-main-rotation", viewport.rotation);
}

export { renderTextLayer, TextLayerRenderTask, updateTextLayer };
2 changes: 2 additions & 0 deletions src/pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import {
PDFDateString,
PixelsPerInch,
RenderingCancelledException,
setLayerDimensions,
} from "./display/display_utils.js";
import { renderTextLayer, updateTextLayer } from "./display/text_layer.js";
import { AnnotationEditorLayer } from "./display/editor/annotation_editor_layer.js";
Expand Down Expand Up @@ -141,6 +142,7 @@ export {
PixelsPerInch,
RenderingCancelledException,
renderTextLayer,
setLayerDimensions,
shadow,
SVGGraphics,
UnexpectedResponseException,
Expand Down
12 changes: 6 additions & 6 deletions web/annotation_editor_layer_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ class AnnotationEditorLayerBuilder {
}

// Create an AnnotationEditor layer div
this.div = document.createElement("div");
this.div.className = "annotationEditorLayer";
this.div.tabIndex = 0;
this.pageDiv.append(this.div);
const div = (this.div = document.createElement("div"));
div.className = "annotationEditorLayer";
div.tabIndex = 0;
this.pageDiv.append(div);

this.annotationEditorLayer = new AnnotationEditorLayer({
uiManager: this.#uiManager,
div: this.div,
div,
accessibilityManager: this.accessibilityManager,
pageIndex: this.pdfPage.pageNumber - 1,
l10n: this.l10n,
Expand All @@ -88,7 +88,7 @@ class AnnotationEditorLayerBuilder {

const parameters = {
viewport: clonedViewport,
div: this.div,
div,
annotations: null,
intent,
};
Expand Down
3 changes: 1 addition & 2 deletions web/annotation_layer_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,9 @@ class AnnotationLayerBuilder {
} else {
// Create an annotation layer div and render the annotations
// if there is at least one annotation.
this.div = document.createElement("div");
this.div = parameters.div = document.createElement("div");
this.div.className = "annotationLayer";
this.pageDiv.append(this.div);
parameters.div = this.div;

AnnotationLayer.render(parameters);
this.l10n.translate(this.div);
Expand Down
20 changes: 14 additions & 6 deletions web/pdf_page_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
createPromiseCapability,
PixelsPerInch,
RenderingCancelledException,
setLayerDimensions,
SVGGraphics,
} from "pdfjs-lib";
import {
Expand Down Expand Up @@ -179,15 +180,15 @@ class PDFPageView {

const div = document.createElement("div");
div.className = "page";
div.style.width = Math.round(this.viewport.width) + "px";
div.style.height = Math.round(this.viewport.height) + "px";
div.setAttribute("data-page-number", this.id);
div.setAttribute("role", "region");
this.l10n.get("page_landmark", { page: this.id }).then(msg => {
div.setAttribute("aria-label", msg);
});
this.div = div;

this.#setDimensions();

container?.append(div);

if (
Expand Down Expand Up @@ -219,6 +220,16 @@ class PDFPageView {
}
}

#setDimensions() {
const { div, viewport } = this;
setLayerDimensions(
div,
viewport,
/* mustFlip = */ true,
/* mustRotate = */ false
);
}

setPdfPage(pdfPage) {
this.pdfPage = pdfPage;
this.pdfPageRotate = pdfPage.rotate;
Expand Down Expand Up @@ -400,9 +411,8 @@ class PDFPageView {
});
this.renderingState = RenderingStates.INITIAL;

this.#setDimensions();
const div = this.div;
div.style.width = Math.round(this.viewport.width) + "px";
div.style.height = Math.round(this.viewport.height) + "px";

const childNodes = div.childNodes,
zoomLayerNode = (keepZoomLayer && this.zoomLayer) || null,
Expand Down Expand Up @@ -724,8 +734,6 @@ class PDFPageView {
// Wrap the canvas so that if it has a CSS transform for high DPI the
// overflow will be hidden in Firefox.
const canvasWrapper = document.createElement("div");
canvasWrapper.style.width = div.style.width;
canvasWrapper.style.height = div.style.height;
canvasWrapper.classList.add("canvasWrapper");

if (this.textLayer) {
Expand Down
2 changes: 2 additions & 0 deletions web/pdf_viewer.css
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@

.pdfViewer .canvasWrapper {
overflow: hidden;
width: 100%;
height: 100%;
}

.pdfViewer .page {
Expand Down