1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-19 06:38:07 +02:00

Add an OutputScale static method to get the devicePixelRatio

Currently we lookup the `devicePixelRatio`, with fallback handling, in a number of spots in the code-base.
Rather than duplicating code we can instead add a new static method in the `OutputScale` class, since that one is now exposed in the API.
This commit is contained in:
Jonas Jenwald 2025-03-12 14:03:51 +01:00
parent e4795f639c
commit 319d239f41
7 changed files with 25 additions and 17 deletions

View file

@ -30,6 +30,7 @@ import {
import { import {
getCurrentTransform, getCurrentTransform,
getCurrentTransformInverse, getCurrentTransformInverse,
OutputScale,
PixelsPerInch, PixelsPerInch,
} from "./display_utils.js"; } from "./display_utils.js";
import { import {
@ -811,7 +812,7 @@ function getImageSmoothingEnabled(transform, interpolate) {
scale[0] = Math.fround(scale[0]); scale[0] = Math.fround(scale[0]);
scale[1] = Math.fround(scale[1]); scale[1] = Math.fround(scale[1]);
const actualScale = Math.fround( const actualScale = Math.fround(
(globalThis.devicePixelRatio || 1) * PixelsPerInch.PDF_TO_CSS_UNITS OutputScale.pixelRatio * PixelsPerInch.PDF_TO_CSS_UNITS
); );
return scale[0] <= actualScale && scale[1] <= actualScale; return scale[0] <= actualScale && scale[1] <= actualScale;
} }

View file

@ -620,7 +620,7 @@ function setLayerDimensions(
*/ */
class OutputScale { class OutputScale {
constructor() { constructor() {
const pixelRatio = window.devicePixelRatio || 1; const { pixelRatio } = OutputScale;
/** /**
* @type {number} Horizontal scale. * @type {number} Horizontal scale.
@ -673,6 +673,10 @@ class OutputScale {
} }
return false; return false;
} }
static get pixelRatio() {
return globalThis.devicePixelRatio || 1;
}
} }
// See https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types // See https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types

View file

@ -23,7 +23,7 @@ import {
Util, Util,
warn, warn,
} from "../shared/util.js"; } from "../shared/util.js";
import { setLayerDimensions } from "./display_utils.js"; import { OutputScale, setLayerDimensions } from "./display_utils.js";
/** /**
* @typedef {Object} TextLayerParameters * @typedef {Object} TextLayerParameters
@ -115,7 +115,7 @@ class TextLayer {
} }
this.#container = this.#rootContainer = container; this.#container = this.#rootContainer = container;
this.#scale = viewport.scale * (globalThis.devicePixelRatio || 1); this.#scale = viewport.scale * OutputScale.pixelRatio;
this.#rotation = viewport.rotation; this.#rotation = viewport.rotation;
this.#layoutTextParams = { this.#layoutTextParams = {
div: null, div: null,
@ -205,7 +205,7 @@ class TextLayer {
* @returns {undefined} * @returns {undefined}
*/ */
update({ viewport, onBefore = null }) { update({ viewport, onBefore = null }) {
const scale = viewport.scale * (globalThis.devicePixelRatio || 1); const scale = viewport.scale * OutputScale.pixelRatio;
const rotation = viewport.rotation; const rotation = viewport.rotation;
if (rotation !== this.#rotation) { if (rotation !== this.#rotation) {

View file

@ -13,7 +13,7 @@
* limitations under the License. * limitations under the License.
*/ */
import { stopEvent } from "./display_utils.js"; import { OutputScale, stopEvent } from "./display_utils.js";
class TouchManager { class TouchManager {
#container; #container;
@ -75,7 +75,7 @@ class TouchManager {
// The properties TouchEvent::screenX/Y are in screen CSS pixels: // The properties TouchEvent::screenX/Y are in screen CSS pixels:
// https://developer.mozilla.org/en-US/docs/Web/API/Touch/screenX#examples // https://developer.mozilla.org/en-US/docs/Web/API/Touch/screenX#examples
// MIN_TOUCH_DISTANCE_TO_PINCH is in CSS pixels. // MIN_TOUCH_DISTANCE_TO_PINCH is in CSS pixels.
return 35 / (window.devicePixelRatio || 1); return 35 / OutputScale.pixelRatio;
} }
#onTouchStart(evt) { #onTouchStart(evt) {

View file

@ -20,6 +20,7 @@ const {
DrawLayer, DrawLayer,
getDocument, getDocument,
GlobalWorkerOptions, GlobalWorkerOptions,
OutputScale,
PixelsPerInch, PixelsPerInch,
shadow, shadow,
TextLayer, TextLayer,
@ -876,7 +877,7 @@ class Driver {
page => { page => {
// Default to creating the test images at the devices pixel ratio, // Default to creating the test images at the devices pixel ratio,
// unless the test explicitly specifies an output scale. // unless the test explicitly specifies an output scale.
const outputScale = task.outputScale || window.devicePixelRatio; const outputScale = task.outputScale || OutputScale.pixelRatio;
let viewport = page.getViewport({ let viewport = page.getViewport({
scale: PixelsPerInch.PDF_TO_CSS_UNITS, scale: PixelsPerInch.PDF_TO_CSS_UNITS,
}); });

View file

@ -50,6 +50,7 @@ import {
InvalidPDFException, InvalidPDFException,
isDataScheme, isDataScheme,
isPdfFile, isPdfFile,
OutputScale,
PDFWorker, PDFWorker,
ResponseException, ResponseException,
shadow, shadow,
@ -2091,7 +2092,7 @@ const PDFViewerApplication = {
pdfViewer.refresh(); pdfViewer.refresh();
} }
const mediaQueryList = window.matchMedia( const mediaQueryList = window.matchMedia(
`(resolution: ${window.devicePixelRatio || 1}dppx)` `(resolution: ${OutputScale.pixelRatio}dppx)`
); );
mediaQueryList.addEventListener("change", addWindowResolutionChange, { mediaQueryList.addEventListener("change", addWindowResolutionChange, {
once: true, once: true,

View file

@ -14,6 +14,7 @@
*/ */
import { BasePDFPageView } from "./base_pdf_page_view.js"; import { BasePDFPageView } from "./base_pdf_page_view.js";
import { OutputScale } from "pdfjs-lib";
import { RenderingStates } from "./ui_utils.js"; import { RenderingStates } from "./ui_utils.js";
/** @typedef {import("./interfaces").IRenderableView} IRenderableView */ /** @typedef {import("./interfaces").IRenderableView} IRenderableView */
@ -153,7 +154,7 @@ class PDFPageDetailView extends BasePDFPageView {
// but we can reduce it to make sure that we stay within the maxCanvasPixels // but we can reduce it to make sure that we stay within the maxCanvasPixels
// limit. // limit.
const visiblePixels = const visiblePixels =
visibleWidth * visibleHeight * (window.devicePixelRatio || 1) ** 2; visibleWidth * visibleHeight * OutputScale.pixelRatio ** 2;
const maxDetailToVisibleLinearRatio = Math.sqrt( const maxDetailToVisibleLinearRatio = Math.sqrt(
maxCanvasPixels / visiblePixels maxCanvasPixels / visiblePixels
); );
@ -228,18 +229,18 @@ class PDFPageDetailView extends BasePDFPageView {
const area = this.#detailArea; const area = this.#detailArea;
const { devicePixelRatio = 1 } = window; const { pixelRatio } = OutputScale;
const transform = [ const transform = [
devicePixelRatio, pixelRatio,
0, 0,
0, 0,
devicePixelRatio, pixelRatio,
-area.minX * devicePixelRatio, -area.minX * pixelRatio,
-area.minY * devicePixelRatio, -area.minY * pixelRatio,
]; ];
canvas.width = area.width * devicePixelRatio; canvas.width = area.width * pixelRatio;
canvas.height = area.height * devicePixelRatio; canvas.height = area.height * pixelRatio;
const { style } = canvas; const { style } = canvas;
style.width = `${(area.width * 100) / width}%`; style.width = `${(area.width * 100) / width}%`;
style.height = `${(area.height * 100) / height}%`; style.height = `${(area.height * 100) / height}%`;