diff --git a/src/display/api.js b/src/display/api.js index 1feeb72e5975e..b688971509126 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -67,6 +67,7 @@ import { MathClamp } from "../shared/math_clamp.js"; import { Metadata } from "./metadata.js"; import { ObjectHandler } from "./object_handler.js"; import { OptionalContentConfig } from "./optional_content_config.js"; +import { OutputScale } from "./output_scale.js"; import { PagesMapper } from "./pages_mapper.js"; import { PageViewport } from "./page_viewport.js"; import { PDFDataTransportStream } from "./transport_stream.js"; @@ -3523,7 +3524,7 @@ class InternalRenderTask { this.objs, this.canvasFactory, this.filterFactory, - { optionalContentConfig }, + { optionalContentConfig, pixelRatio: OutputScale.pixelRatio }, this.annotationCanvasMap, this.pageColors, dependencyTracker, diff --git a/src/display/canvas.js b/src/display/canvas.js index 21e06e6c0c21c..b9337213a67e5 100644 --- a/src/display/canvas.js +++ b/src/display/canvas.js @@ -43,7 +43,6 @@ import { getCurrentTransformInverse, getRGBA, makePathFromDrawOPS, - OutputScale, PixelsPerInch, } from "./display_utils.js"; import { @@ -610,11 +609,7 @@ class CanvasGraphics { objs, canvasFactory, filterFactory, - { - optionalContentConfig, - markedContentStack = null, - pixelRatio = OutputScale.pixelRatio, - }, + { optionalContentConfig, markedContentStack = null, pixelRatio }, annotationCanvasMap, pageColors, dependencyTracker, diff --git a/src/display/display_utils.js b/src/display/display_utils.js index 6d4fcc3ec789d..4548f87186738 100644 --- a/src/display/display_utils.js +++ b/src/display/display_utils.js @@ -100,91 +100,6 @@ function getCurrentTransformInverse(ctx) { return [a, b, c, d, e, f]; } -/** - * Scale factors for the canvas, necessary with HiDPI displays. - */ -class OutputScale { - constructor() { - const { pixelRatio } = OutputScale; - - /** - * @type {number} Horizontal scale. - */ - this.sx = pixelRatio; - - /** - * @type {number} Vertical scale. - */ - this.sy = pixelRatio; - } - - /** - * @type {boolean} Returns `true` when scaling is required, `false` otherwise. - */ - get scaled() { - return this.sx !== 1 || this.sy !== 1; - } - - /** - * @type {boolean} Returns `true` when scaling is symmetric, - * `false` otherwise. - */ - get symmetric() { - return this.sx === this.sy; - } - - /** - * @returns {boolean} Returns `true` if scaling was limited, - * `false` otherwise. - */ - limitCanvas(width, height, maxPixels, maxDim, capAreaFactor = -1) { - if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("WORKER_THREAD")) { - return false; - } - let maxAreaScale = Infinity, - maxWidthScale = Infinity, - maxHeightScale = Infinity; - - maxPixels = OutputScale.capPixels(maxPixels, capAreaFactor); - if (maxPixels > 0) { - maxAreaScale = Math.sqrt(maxPixels / (width * height)); - } - if (maxDim > 0) { - maxWidthScale = maxDim / width; - maxHeightScale = maxDim / height; - } - const maxScale = Math.min(maxAreaScale, maxWidthScale, maxHeightScale); - - if (this.sx > maxScale || this.sy > maxScale) { - this.sx = maxScale; - this.sy = maxScale; - return true; - } - return false; - } - - static get pixelRatio() { - return globalThis.devicePixelRatio || 1; - } - - static capPixels(maxPixels, capAreaFactor) { - if ( - (typeof PDFJSDev === "undefined" || !PDFJSDev.test("WORKER_THREAD")) && - capAreaFactor >= 0 - ) { - const winPixels = Math.ceil( - (typeof PDFJSDev !== "undefined" && PDFJSDev.test("TESTING") - ? window.innerWidth * window.innerHeight - : window.screen.availWidth * window.screen.availHeight) * - this.pixelRatio ** 2 * - (1 + capAreaFactor / 100) - ); - return maxPixels > 0 ? Math.min(maxPixels, winPixels) : winPixels; - } - return maxPixels; - } -} - function applyOpacity(color, opacity) { opacity = MathClamp(opacity ?? 1, 0, 1); const white = 255 * (1 - opacity); @@ -403,7 +318,6 @@ export { getRGB, getRGBA, makePathFromDrawOPS, - OutputScale, PixelsPerInch, RenderingCancelledException, }; diff --git a/src/display/editor/stamp.js b/src/display/editor/stamp.js index 9fd9399cecc5e..9b90defc6ce67 100644 --- a/src/display/editor/stamp.js +++ b/src/display/editor/stamp.js @@ -15,8 +15,9 @@ import { AnnotationEditorType, AnnotationPrefix } from "../../shared/util.js"; import { ColorScheme, SupportedImageMimeTypes } from "../dom_utils.js"; -import { OutputScale, PixelsPerInch } from "../display_utils.js"; import { AnnotationEditor } from "./editor.js"; +import { OutputScale } from "../output_scale.js"; +import { PixelsPerInch } from "../display_utils.js"; import { StampAnnotationElement } from "../annotation_layer.js"; /** diff --git a/src/display/output_scale.js b/src/display/output_scale.js new file mode 100644 index 0000000000000..6e35015195522 --- /dev/null +++ b/src/display/output_scale.js @@ -0,0 +1,95 @@ +/* Copyright 2015 Mozilla Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Scale factors for the canvas, necessary with HiDPI displays. + */ +class OutputScale { + constructor() { + const { pixelRatio } = OutputScale; + + /** + * @type {number} Horizontal scale. + */ + this.sx = pixelRatio; + + /** + * @type {number} Vertical scale. + */ + this.sy = pixelRatio; + } + + /** + * @type {boolean} Returns `true` when scaling is required, `false` otherwise. + */ + get scaled() { + return this.sx !== 1 || this.sy !== 1; + } + + /** + * @type {boolean} Returns `true` when scaling is symmetric, + * `false` otherwise. + */ + get symmetric() { + return this.sx === this.sy; + } + + /** + * @returns {boolean} Returns `true` if scaling was limited, + * `false` otherwise. + */ + limitCanvas(width, height, maxPixels, maxDim, capAreaFactor = -1) { + let maxAreaScale = Infinity, + maxWidthScale = Infinity, + maxHeightScale = Infinity; + + maxPixels = OutputScale.capPixels(maxPixels, capAreaFactor); + if (maxPixels > 0) { + maxAreaScale = Math.sqrt(maxPixels / (width * height)); + } + if (maxDim > 0) { + maxWidthScale = maxDim / width; + maxHeightScale = maxDim / height; + } + const maxScale = Math.min(maxAreaScale, maxWidthScale, maxHeightScale); + + if (this.sx > maxScale || this.sy > maxScale) { + this.sx = maxScale; + this.sy = maxScale; + return true; + } + return false; + } + + static get pixelRatio() { + return globalThis.devicePixelRatio || 1; + } + + static capPixels(maxPixels, capAreaFactor) { + if (capAreaFactor >= 0) { + const winPixels = Math.ceil( + (typeof PDFJSDev !== "undefined" && PDFJSDev.test("TESTING") + ? window.innerWidth * window.innerHeight + : window.screen.availWidth * window.screen.availHeight) * + this.pixelRatio ** 2 * + (1 + capAreaFactor / 100) + ); + return maxPixels > 0 ? Math.min(maxPixels, winPixels) : winPixels; + } + return maxPixels; + } +} + +export { OutputScale }; diff --git a/src/display/renderer_worker_proxy.js b/src/display/renderer_worker_proxy.js index 49603e2112cd6..e28800f1f9d0f 100644 --- a/src/display/renderer_worker_proxy.js +++ b/src/display/renderer_worker_proxy.js @@ -23,7 +23,7 @@ import { BBoxReader } from "./canvas_dependency_tracker.js"; import { getWorkerSrc } from "./api_utils.js"; import { GlobalWorkerOptions } from "./worker_options.js"; import { MessageHandler } from "../shared/message_handler.js"; -import { OutputScale } from "./display_utils.js"; +import { OutputScale } from "./output_scale.js"; import { setAnnotationCanvasName } from "./canvas.js"; const PORT_CONNECTION_TIMEOUT = 5000; // ms diff --git a/src/display/text_layer.js b/src/display/text_layer.js index 5ea12e864d11b..a66efd43e3394 100644 --- a/src/display/text_layer.js +++ b/src/display/text_layer.js @@ -29,7 +29,7 @@ import { warn, } from "../shared/util.js"; import { FontLoader } from "./font_loader.js"; -import { OutputScale } from "./display_utils.js"; +import { OutputScale } from "./output_scale.js"; import { setLayerDimensions } from "./dom_utils.js"; /** diff --git a/src/display/touch_manager.js b/src/display/touch_manager.js index 4b8e7a421f6f5..b765176217147 100644 --- a/src/display/touch_manager.js +++ b/src/display/touch_manager.js @@ -13,7 +13,7 @@ * limitations under the License. */ -import { OutputScale } from "./display_utils.js"; +import { OutputScale } from "./output_scale.js"; import { stopEvent } from "./dom_utils.js"; function preventDefault(evt) { diff --git a/src/pdf.js b/src/pdf.js index 7d01be989ee9b..a823c302d82e9 100644 --- a/src/pdf.js +++ b/src/pdf.js @@ -55,7 +55,6 @@ import { findContrastColor, getRGB, getRGBA, - OutputScale, PixelsPerInch, RenderingCancelledException, } from "./display/display_utils.js"; @@ -90,6 +89,7 @@ import { GlobalWorkerOptions } from "./display/worker_options.js"; import { HighlightOutliner } from "./display/editor/drawers/highlight.js"; import { isValidExplicitDest } from "./display/api_utils.js"; import { MathClamp } from "./shared/math_clamp.js"; +import { OutputScale } from "./display/output_scale.js"; import { SignatureExtractor } from "./display/editor/drawers/signaturedraw.js"; import { TextLayer } from "./display/text_layer.js"; import { TextLayerImages } from "./display/text_layer_images.js"; diff --git a/test/unit/pdf_spec.js b/test/unit/pdf_spec.js index 560b2e1a7f799..7f609e8d23654 100644 --- a/test/unit/pdf_spec.js +++ b/test/unit/pdf_spec.js @@ -46,7 +46,6 @@ import { findContrastColor, getRGB, getRGBA, - OutputScale, PixelsPerInch, RenderingCancelledException, } from "../../src/display/display_utils.js"; @@ -80,6 +79,7 @@ import { DrawLayer } from "../../src/display/draw_layer.js"; import { GlobalWorkerOptions } from "../../src/display/worker_options.js"; import { isValidExplicitDest } from "../../src/display/api_utils.js"; import { MathClamp } from "../../src/shared/math_clamp.js"; +import { OutputScale } from "../../src/display/output_scale.js"; import { SignatureExtractor } from "../../src/display/editor/drawers/signaturedraw.js"; import { TextLayer } from "../../src/display/text_layer.js"; import { TextLayerImages } from "../../src/display/text_layer_images.js"; diff --git a/test/unit/text_layer_spec.js b/test/unit/text_layer_spec.js index bb9f2f9fd857e..7cbb18f41c675 100644 --- a/test/unit/text_layer_spec.js +++ b/test/unit/text_layer_spec.js @@ -17,7 +17,7 @@ import { AbortException, isNodeJS } from "../../src/shared/util.js"; import { DEFAULT_FONT_SIZE, TextLayer } from "../../src/display/text_layer.js"; import { buildGetDocumentParams } from "./test_utils.js"; import { getDocument } from "../../src/display/api.js"; -import { OutputScale } from "../../src/display/display_utils.js"; +import { OutputScale } from "../../src/display/output_scale.js"; describe("textLayer", function () { it("creates textLayer from ReadableStream", async function () {