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

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

View file

@ -14,6 +14,7 @@
*/
import { BasePDFPageView } from "./base_pdf_page_view.js";
import { OutputScale } from "pdfjs-lib";
import { RenderingStates } from "./ui_utils.js";
/** @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
// limit.
const visiblePixels =
visibleWidth * visibleHeight * (window.devicePixelRatio || 1) ** 2;
visibleWidth * visibleHeight * OutputScale.pixelRatio ** 2;
const maxDetailToVisibleLinearRatio = Math.sqrt(
maxCanvasPixels / visiblePixels
);
@ -228,18 +229,18 @@ class PDFPageDetailView extends BasePDFPageView {
const area = this.#detailArea;
const { devicePixelRatio = 1 } = window;
const { pixelRatio } = OutputScale;
const transform = [
devicePixelRatio,
pixelRatio,
0,
0,
devicePixelRatio,
-area.minX * devicePixelRatio,
-area.minY * devicePixelRatio,
pixelRatio,
-area.minX * pixelRatio,
-area.minY * pixelRatio,
];
canvas.width = area.width * devicePixelRatio;
canvas.height = area.height * devicePixelRatio;
canvas.width = area.width * pixelRatio;
canvas.height = area.height * pixelRatio;
const { style } = canvas;
style.width = `${(area.width * 100) / width}%`;
style.height = `${(area.height * 100) / height}%`;