Skip to content

Commit 7ae5a0f

Browse files
Merge pull request #16731 from Snuffleupagus/rm-useOnlyCssZoom
[api-minor] Replace the `useOnlyCssZoom` option with `maxCanvasPixels = 0` instead (PR 16729 follow-up)
2 parents 2a77f08 + 0ee2a35 commit 7ae5a0f

File tree

5 files changed

+51
-47
lines changed

5 files changed

+51
-47
lines changed

examples/mobile-viewer/viewer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ if (!pdfjsLib.getDocument || !pdfjsViewer.PDFViewer) {
2020
alert("Please build the pdfjs-dist library using\n `gulp dist-install`");
2121
}
2222

23-
const USE_ONLY_CSS_ZOOM = true;
23+
const MAX_CANVAS_PIXELS = 0; // CSS-only zooming.
2424
const TEXT_LAYER_MODE = 0; // DISABLE
2525
const MAX_IMAGE_SIZE = 1024 * 1024;
2626
const CMAP_URL = "../../node_modules/pdfjs-dist/cmaps/";
@@ -363,7 +363,7 @@ const PDFViewerApplication = {
363363
eventBus,
364364
linkService,
365365
l10n: this.l10n,
366-
useOnlyCssZoom: USE_ONLY_CSS_ZOOM,
366+
maxCanvasPixels: MAX_CANVAS_PIXELS,
367367
textLayerMode: TEXT_LAYER_MODE,
368368
});
369369
this.pdfViewer = pdfViewer;

web/app.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,6 @@ const PDFViewerApplication = {
522522
annotationEditorMode,
523523
imageResourcesPath: AppOptions.get("imageResourcesPath"),
524524
enablePrintAutoRotate: AppOptions.get("enablePrintAutoRotate"),
525-
useOnlyCssZoom: AppOptions.get("useOnlyCssZoom"),
526525
isOffscreenCanvasSupported,
527526
maxCanvasPixels: AppOptions.get("maxCanvasPixels"),
528527
enablePermissions: AppOptions.get("enablePermissions"),

web/app_options.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,6 @@ const defaultOptions = {
189189
value: 1,
190190
kind: OptionKind.VIEWER + OptionKind.PREFERENCE,
191191
},
192-
useOnlyCssZoom: {
193-
/** @type {boolean} */
194-
value: false,
195-
kind: OptionKind.VIEWER,
196-
},
197192
viewerCssTheme: {
198193
/** @type {number} */
199194
value: typeof PDFJSDev !== "undefined" && PDFJSDev.test("CHROME") ? 2 : 0,

web/pdf_page_view.js

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,11 @@ import { XfaLayerBuilder } from "./xfa_layer_builder.js";
7171
* The default value is `AnnotationMode.ENABLE_FORMS`.
7272
* @property {string} [imageResourcesPath] - Path for image resources, mainly
7373
* for annotation icons. Include trailing slash.
74-
* @property {boolean} [useOnlyCssZoom] - Enables CSS only zooming. The default
75-
* value is `false`.
7674
* @property {boolean} [isOffscreenCanvasSupported] - Allows to use an
7775
* OffscreenCanvas if needed.
7876
* @property {number} [maxCanvasPixels] - The maximum supported canvas size in
79-
* total pixels, i.e. width * height. Use -1 for no limit. The default value
80-
* is 4096 * 4096 (16 mega-pixels).
77+
* total pixels, i.e. width * height. Use `-1` for no limit, or `0` for
78+
* CSS-only zooming. The default value is 4096 * 4096 (16 mega-pixels).
8179
* @property {Object} [pageColors] - Overwrites background and foreground colors
8280
* with user defined ones in order to improve readability in high contrast
8381
* mode.
@@ -112,6 +110,8 @@ const DEFAULT_LAYER_PROPERTIES = () => {
112110
class PDFPageView {
113111
#annotationMode = AnnotationMode.ENABLE_FORMS;
114112

113+
#hasRestrictedScaling = false;
114+
115115
#layerProperties = null;
116116

117117
#loadingId = null;
@@ -151,15 +151,13 @@ class PDFPageView {
151151
this.pdfPageRotate = defaultViewport.rotation;
152152
this._optionalContentConfigPromise =
153153
options.optionalContentConfigPromise || null;
154-
this.hasRestrictedScaling = false;
155154
this.#textLayerMode = options.textLayerMode ?? TextLayerMode.ENABLE;
156155
this.#annotationMode =
157156
options.annotationMode ?? AnnotationMode.ENABLE_FORMS;
158157
this.imageResourcesPath = options.imageResourcesPath || "";
159-
this.useOnlyCssZoom = options.useOnlyCssZoom || false;
160158
this.isOffscreenCanvasSupported =
161159
options.isOffscreenCanvasSupported ?? true;
162-
this.maxCanvasPixels = options.maxCanvasPixels || MAX_CANVAS_PIXELS;
160+
this.maxCanvasPixels = options.maxCanvasPixels ?? MAX_CANVAS_PIXELS;
163161
this.pageColors = options.pageColors || null;
164162

165163
this.eventBus = options.eventBus;
@@ -171,6 +169,13 @@ class PDFPageView {
171169
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
172170
this._isStandalone = !this.renderingQueue?.hasViewer();
173171
this._container = container;
172+
173+
if (options.useOnlyCssZoom) {
174+
console.error(
175+
"useOnlyCssZoom was removed, please use `maxCanvasPixels = 0` instead."
176+
);
177+
this.maxCanvasPixels = 0;
178+
}
174179
}
175180

176181
this._annotationCanvasMap = null;
@@ -586,23 +591,25 @@ class PDFPageView {
586591
this._container?.style.setProperty("--scale-factor", this.viewport.scale);
587592
}
588593

589-
let isScalingRestricted = false;
590-
if (this.canvas && this.maxCanvasPixels > 0) {
591-
const { width, height } = this.viewport;
592-
const { sx, sy } = this.outputScale;
593-
if (
594-
((Math.floor(width) * sx) | 0) * ((Math.floor(height) * sy) | 0) >
595-
this.maxCanvasPixels
596-
) {
597-
isScalingRestricted = true;
594+
if (this.canvas) {
595+
let onlyCssZoom = false;
596+
if (this.#hasRestrictedScaling) {
597+
if (
598+
(typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) &&
599+
this.maxCanvasPixels === 0
600+
) {
601+
onlyCssZoom = true;
602+
} else if (this.maxCanvasPixels > 0) {
603+
const { width, height } = this.viewport;
604+
const { sx, sy } = this.outputScale;
605+
onlyCssZoom =
606+
((Math.floor(width) * sx) | 0) * ((Math.floor(height) * sy) | 0) >
607+
this.maxCanvasPixels;
608+
}
598609
}
599-
}
600-
const onlyCssZoom =
601-
this.useOnlyCssZoom || (this.hasRestrictedScaling && isScalingRestricted);
602-
const postponeDrawing =
603-
!onlyCssZoom && drawingDelay >= 0 && drawingDelay < 1000;
610+
const postponeDrawing =
611+
!onlyCssZoom && drawingDelay >= 0 && drawingDelay < 1000;
604612

605-
if (this.canvas) {
606613
if (postponeDrawing || onlyCssZoom) {
607614
if (
608615
postponeDrawing &&
@@ -921,25 +928,25 @@ class PDFPageView {
921928
const ctx = canvas.getContext("2d", { alpha: false });
922929
const outputScale = (this.outputScale = new OutputScale());
923930

924-
if (this.useOnlyCssZoom) {
925-
const actualSizeViewport = viewport.clone({
926-
scale: PixelsPerInch.PDF_TO_CSS_UNITS,
927-
});
931+
if (
932+
(typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) &&
933+
this.maxCanvasPixels === 0
934+
) {
935+
const invScale = 1 / this.scale;
928936
// Use a scale that makes the canvas have the originally intended size
929937
// of the page.
930-
outputScale.sx *= actualSizeViewport.width / width;
931-
outputScale.sy *= actualSizeViewport.height / height;
932-
}
933-
934-
if (this.maxCanvasPixels > 0) {
938+
outputScale.sx *= invScale;
939+
outputScale.sy *= invScale;
940+
this.#hasRestrictedScaling = true;
941+
} else if (this.maxCanvasPixels > 0) {
935942
const pixelsInViewport = width * height;
936943
const maxScale = Math.sqrt(this.maxCanvasPixels / pixelsInViewport);
937944
if (outputScale.sx > maxScale || outputScale.sy > maxScale) {
938945
outputScale.sx = maxScale;
939946
outputScale.sy = maxScale;
940-
this.hasRestrictedScaling = true;
947+
this.#hasRestrictedScaling = true;
941948
} else {
942-
this.hasRestrictedScaling = false;
949+
this.#hasRestrictedScaling = false;
943950
}
944951
}
945952
const sfx = approximateFraction(outputScale.sx);

web/pdf_viewer.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,11 @@ function isValidAnnotationEditorMode(mode) {
108108
* mainly for annotation icons. Include trailing slash.
109109
* @property {boolean} [enablePrintAutoRotate] - Enables automatic rotation of
110110
* landscape pages upon printing. The default is `false`.
111-
* @property {boolean} [useOnlyCssZoom] - Enables CSS only zooming. The default
112-
* value is `false`.
113111
* @property {boolean} [isOffscreenCanvasSupported] - Allows to use an
114112
* OffscreenCanvas if needed.
115113
* @property {number} [maxCanvasPixels] - The maximum supported canvas size in
116-
* total pixels, i.e. width * height. Use -1 for no limit. The default value
117-
* is 4096 * 4096 (16 mega-pixels).
114+
* total pixels, i.e. width * height. Use `-1` for no limit, or `0` for
115+
* CSS-only zooming. The default value is 4096 * 4096 (16 mega-pixels).
118116
* @property {IL10n} [l10n] - Localization service.
119117
* @property {boolean} [enablePermissions] - Enables PDF document permissions,
120118
* when they exist. The default value is `false`.
@@ -274,8 +272,14 @@ class PDFViewer {
274272
this.enablePrintAutoRotate = options.enablePrintAutoRotate || false;
275273
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
276274
this.removePageBorders = options.removePageBorders || false;
275+
276+
if (options.useOnlyCssZoom) {
277+
console.error(
278+
"useOnlyCssZoom was removed, please use `maxCanvasPixels = 0` instead."
279+
);
280+
options.maxCanvasPixels = 0;
281+
}
277282
}
278-
this.useOnlyCssZoom = options.useOnlyCssZoom || false;
279283
this.isOffscreenCanvasSupported =
280284
options.isOffscreenCanvasSupported ?? true;
281285
this.maxCanvasPixels = options.maxCanvasPixels;
@@ -894,7 +898,6 @@ class PDFViewer {
894898
textLayerMode,
895899
annotationMode,
896900
imageResourcesPath: this.imageResourcesPath,
897-
useOnlyCssZoom: this.useOnlyCssZoom,
898901
isOffscreenCanvasSupported: this.isOffscreenCanvasSupported,
899902
maxCanvasPixels: this.maxCanvasPixels,
900903
pageColors: this.pageColors,

0 commit comments

Comments
 (0)