Skip to content

Commit

Permalink
[api-minor] Replace the useOnlyCssZoom option with `maxCanvasPixels…
Browse files Browse the repository at this point in the history
… = 0` instead (PR 16729 follow-up)

Given that the `useOnlyCssZoom` option is essentially just a special-case of the `maxCanvasPixels` functionality, we can combine the two options in order to simplify the overall implementation.
Note that the `useOnlyCssZoom` functionality was only ever used, by default, in the PDF Viewer for the B2G/FirefoxOS project (which was abandoned years ago).
  • Loading branch information
Snuffleupagus committed Jul 24, 2023
1 parent 71f113b commit c017598
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 47 deletions.
1 change: 0 additions & 1 deletion web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,6 @@ const PDFViewerApplication = {
annotationEditorMode,
imageResourcesPath: AppOptions.get("imageResourcesPath"),
enablePrintAutoRotate: AppOptions.get("enablePrintAutoRotate"),
useOnlyCssZoom: AppOptions.get("useOnlyCssZoom"),
isOffscreenCanvasSupported,
maxCanvasPixels: AppOptions.get("maxCanvasPixels"),
enablePermissions: AppOptions.get("enablePermissions"),
Expand Down
5 changes: 0 additions & 5 deletions web/app_options.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,6 @@ const defaultOptions = {
value: 1,
kind: OptionKind.VIEWER + OptionKind.PREFERENCE,
},
useOnlyCssZoom: {
/** @type {boolean} */
value: false,
kind: OptionKind.VIEWER,
},
viewerCssTheme: {
/** @type {number} */
value: typeof PDFJSDev !== "undefined" && PDFJSDev.test("CHROME") ? 2 : 0,
Expand Down
70 changes: 35 additions & 35 deletions web/pdf_page_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,11 @@ import { XfaLayerBuilder } from "./xfa_layer_builder.js";
* The default value is `AnnotationMode.ENABLE_FORMS`.
* @property {string} [imageResourcesPath] - Path for image resources, mainly
* for annotation icons. Include trailing slash.
* @property {boolean} [useOnlyCssZoom] - Enables CSS only zooming. The default
* value is `false`.
* @property {boolean} [isOffscreenCanvasSupported] - Allows to use an
* OffscreenCanvas if needed.
* @property {number} [maxCanvasPixels] - The maximum supported canvas size in
* total pixels, i.e. width * height. Use -1 for no limit. The default value
* is 4096 * 4096 (16 mega-pixels).
* total pixels, i.e. width * height. Use `-1` for no limit, and `0` for
* CSS-only zooming. The default value is 4096 * 4096 (16 mega-pixels).
* @property {Object} [pageColors] - Overwrites background and foreground colors
* with user defined ones in order to improve readability in high contrast
* mode.
Expand Down Expand Up @@ -112,6 +110,8 @@ const DEFAULT_LAYER_PROPERTIES = () => {
class PDFPageView {
#annotationMode = AnnotationMode.ENABLE_FORMS;

#hasRestrictedScaling = false;

#layerProperties = null;

#loadingId = null;
Expand Down Expand Up @@ -151,15 +151,13 @@ class PDFPageView {
this.pdfPageRotate = defaultViewport.rotation;
this._optionalContentConfigPromise =
options.optionalContentConfigPromise || null;
this.hasRestrictedScaling = false;
this.#textLayerMode = options.textLayerMode ?? TextLayerMode.ENABLE;
this.#annotationMode =
options.annotationMode ?? AnnotationMode.ENABLE_FORMS;
this.imageResourcesPath = options.imageResourcesPath || "";
this.useOnlyCssZoom = options.useOnlyCssZoom || false;
this.isOffscreenCanvasSupported =
options.isOffscreenCanvasSupported ?? true;
this.maxCanvasPixels = options.maxCanvasPixels || MAX_CANVAS_PIXELS;
this.maxCanvasPixels = options.maxCanvasPixels ?? MAX_CANVAS_PIXELS;
this.pageColors = options.pageColors || null;

this.eventBus = options.eventBus;
Expand All @@ -171,6 +169,12 @@ class PDFPageView {
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
this._isStandalone = !this.renderingQueue?.hasViewer();
this._container = container;

if (options.useOnlyCssZoom) {
console.error(
"useOnlyCssZoom was removed, please use `maxCanvasPixels = 0` instead."
);
}
}

this._annotationCanvasMap = null;
Expand Down Expand Up @@ -586,23 +590,22 @@ class PDFPageView {
this._container?.style.setProperty("--scale-factor", this.viewport.scale);
}

let isScalingRestricted = false;
if (this.canvas && this.maxCanvasPixels > 0) {
const { width, height } = this.viewport;
const { sx, sy } = this.outputScale;
if (
((Math.floor(width) * sx) | 0) * ((Math.floor(height) * sy) | 0) >
this.maxCanvasPixels
) {
isScalingRestricted = true;
if (this.canvas) {
let onlyCssZoom = false;
if (this.#hasRestrictedScaling) {
if (this.maxCanvasPixels === 0) {
onlyCssZoom = true;
} else if (this.maxCanvasPixels > 0) {
const { width, height } = this.viewport;
const { sx, sy } = this.outputScale;
onlyCssZoom =
((Math.floor(width) * sx) | 0) * ((Math.floor(height) * sy) | 0) >
this.maxCanvasPixels;
}
}
}
const onlyCssZoom =
this.useOnlyCssZoom || (this.hasRestrictedScaling && isScalingRestricted);
const postponeDrawing =
!onlyCssZoom && drawingDelay >= 0 && drawingDelay < 1000;
const postponeDrawing =
!onlyCssZoom && drawingDelay >= 0 && drawingDelay < 1000;

if (this.canvas) {
if (postponeDrawing || onlyCssZoom) {
if (
postponeDrawing &&
Expand Down Expand Up @@ -821,7 +824,7 @@ class PDFPageView {
console.error("Must be in new state before drawing");
this.reset(); // Ensure that we reset all state to prevent issues.
}
const { div, l10n, pageColors, pdfPage, viewport } = this;
const { div, l10n, maxCanvasPixels, pageColors, pdfPage, viewport } = this;

if (!pdfPage) {
this.renderingState = RenderingStates.FINISHED;
Expand Down Expand Up @@ -921,25 +924,22 @@ class PDFPageView {
const ctx = canvas.getContext("2d", { alpha: false });
const outputScale = (this.outputScale = new OutputScale());

if (this.useOnlyCssZoom) {
const actualSizeViewport = viewport.clone({
scale: PixelsPerInch.PDF_TO_CSS_UNITS,
});
if (maxCanvasPixels === 0) {
const invScale = 1 / this.scale;
// Use a scale that makes the canvas have the originally intended size
// of the page.
outputScale.sx *= actualSizeViewport.width / width;
outputScale.sy *= actualSizeViewport.height / height;
}

if (this.maxCanvasPixels > 0) {
outputScale.sx *= invScale;
outputScale.sy *= invScale;
this.#hasRestrictedScaling = true;
} else if (maxCanvasPixels > 0) {
const pixelsInViewport = width * height;
const maxScale = Math.sqrt(this.maxCanvasPixels / pixelsInViewport);
const maxScale = Math.sqrt(maxCanvasPixels / pixelsInViewport);
if (outputScale.sx > maxScale || outputScale.sy > maxScale) {
outputScale.sx = maxScale;
outputScale.sy = maxScale;
this.hasRestrictedScaling = true;
this.#hasRestrictedScaling = true;
} else {
this.hasRestrictedScaling = false;
this.#hasRestrictedScaling = false;
}
}
const sfx = approximateFraction(outputScale.sx);
Expand Down
14 changes: 8 additions & 6 deletions web/pdf_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,11 @@ function isValidAnnotationEditorMode(mode) {
* mainly for annotation icons. Include trailing slash.
* @property {boolean} [enablePrintAutoRotate] - Enables automatic rotation of
* landscape pages upon printing. The default is `false`.
* @property {boolean} [useOnlyCssZoom] - Enables CSS only zooming. The default
* value is `false`.
* @property {boolean} [isOffscreenCanvasSupported] - Allows to use an
* OffscreenCanvas if needed.
* @property {number} [maxCanvasPixels] - The maximum supported canvas size in
* total pixels, i.e. width * height. Use -1 for no limit. The default value
* is 4096 * 4096 (16 mega-pixels).
* total pixels, i.e. width * height. Use `-1` for no limit, and `0` for
* CSS-only zooming. The default value is 4096 * 4096 (16 mega-pixels).
* @property {IL10n} [l10n] - Localization service.
* @property {boolean} [enablePermissions] - Enables PDF document permissions,
* when they exist. The default value is `false`.
Expand Down Expand Up @@ -274,8 +272,13 @@ class PDFViewer {
this.enablePrintAutoRotate = options.enablePrintAutoRotate || false;
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
this.removePageBorders = options.removePageBorders || false;

if (options.useOnlyCssZoom) {
console.error(
"useOnlyCssZoom was removed, please use `maxCanvasPixels = 0` instead."
);
}
}
this.useOnlyCssZoom = options.useOnlyCssZoom || false;
this.isOffscreenCanvasSupported =
options.isOffscreenCanvasSupported ?? true;
this.maxCanvasPixels = options.maxCanvasPixels;
Expand Down Expand Up @@ -894,7 +897,6 @@ class PDFViewer {
textLayerMode,
annotationMode,
imageResourcesPath: this.imageResourcesPath,
useOnlyCssZoom: this.useOnlyCssZoom,
isOffscreenCanvasSupported: this.isOffscreenCanvasSupported,
maxCanvasPixels: this.maxCanvasPixels,
pageColors: this.pageColors,
Expand Down

0 comments on commit c017598

Please sign in to comment.