Merge pull request #14578 from Snuffleupagus/rm-backingStorePixelRatio

Remove the `backingStorePixelRatio`-part of the `getOutputScale` helper function
This commit is contained in:
Tim van der Meij 2022-02-19 14:54:30 +01:00 committed by GitHub
commit 8e234a16d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 28 deletions

View File

@ -40,7 +40,7 @@ import {
import { import {
approximateFraction, approximateFraction,
DEFAULT_SCALE, DEFAULT_SCALE,
getOutputScale, OutputScale,
RendererType, RendererType,
RenderingStates, RenderingStates,
roundToDivide, roundToDivide,
@ -806,8 +806,7 @@ class PDFPageView {
} }
const ctx = canvas.getContext("2d", { alpha: false }); const ctx = canvas.getContext("2d", { alpha: false });
const outputScale = getOutputScale(ctx); const outputScale = (this.outputScale = new OutputScale());
this.outputScale = outputScale;
if (this.useOnlyCssZoom) { if (this.useOnlyCssZoom) {
const actualSizeViewport = viewport.clone({ const actualSizeViewport = viewport.clone({
@ -817,7 +816,6 @@ class PDFPageView {
// of the page. // of the page.
outputScale.sx *= actualSizeViewport.width / viewport.width; outputScale.sx *= actualSizeViewport.width / viewport.width;
outputScale.sy *= actualSizeViewport.height / viewport.height; outputScale.sy *= actualSizeViewport.height / viewport.height;
outputScale.scaled = true;
} }
if (this.maxCanvasPixels > 0) { if (this.maxCanvasPixels > 0) {
@ -826,7 +824,6 @@ class PDFPageView {
if (outputScale.sx > maxScale || outputScale.sy > maxScale) { if (outputScale.sx > maxScale || outputScale.sy > maxScale) {
outputScale.sx = maxScale; outputScale.sx = maxScale;
outputScale.sy = maxScale; outputScale.sy = maxScale;
outputScale.scaled = true;
this.hasRestrictedScaling = true; this.hasRestrictedScaling = true;
} else { } else {
this.hasRestrictedScaling = false; this.hasRestrictedScaling = false;
@ -844,9 +841,9 @@ class PDFPageView {
this.paintedViewportMap.set(canvas, viewport); this.paintedViewportMap.set(canvas, viewport);
// Rendering area // Rendering area
const transform = !outputScale.scaled const transform = outputScale.scaled
? null ? [outputScale.sx, 0, 0, outputScale.sy, 0, 0]
: [outputScale.sx, 0, 0, outputScale.sy, 0, 0]; : null;
const renderContext = { const renderContext = {
canvasContext: ctx, canvasContext: ctx,
transform, transform,

View File

@ -19,7 +19,7 @@
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
/** @typedef {import("./pdf_rendering_queue").PDFRenderingQueue} PDFRenderingQueue */ /** @typedef {import("./pdf_rendering_queue").PDFRenderingQueue} PDFRenderingQueue */
import { getOutputScale, RenderingStates } from "./ui_utils.js"; import { OutputScale, RenderingStates } from "./ui_utils.js";
import { RenderingCancelledException } from "pdfjs-lib"; import { RenderingCancelledException } from "pdfjs-lib";
const DRAW_UPSCALE_FACTOR = 2; // See comment in `PDFThumbnailView.draw` below. const DRAW_UPSCALE_FACTOR = 2; // See comment in `PDFThumbnailView.draw` below.
@ -233,7 +233,7 @@ class PDFThumbnailView {
canvas.mozOpaque = true; canvas.mozOpaque = true;
} }
const ctx = canvas.getContext("2d", { alpha: false }); const ctx = canvas.getContext("2d", { alpha: false });
const outputScale = getOutputScale(ctx); const outputScale = new OutputScale();
canvas.width = (upscaleFactor * this.canvasWidth * outputScale.sx) | 0; canvas.width = (upscaleFactor * this.canvasWidth * outputScale.sx) | 0;
canvas.height = (upscaleFactor * this.canvasHeight * outputScale.sy) | 0; canvas.height = (upscaleFactor * this.canvasHeight * outputScale.sy) | 0;

View File

@ -78,24 +78,29 @@ const SpreadMode = {
const AutoPrintRegExp = /\bprint\s*\(/; const AutoPrintRegExp = /\bprint\s*\(/;
/** /**
* Returns scale factor for the canvas. It makes sense for the HiDPI displays. * Scale factors for the canvas, necessary with HiDPI displays.
* @returns {Object} The object with horizontal (sx) and vertical (sy)
* scales. The scaled property is set to false if scaling is
* not required, true otherwise.
*/ */
function getOutputScale(ctx) { class OutputScale {
const devicePixelRatio = window.devicePixelRatio || 1; constructor() {
const backingStoreRatio = const pixelRatio = window.devicePixelRatio || 1;
ctx.webkitBackingStorePixelRatio ||
ctx.mozBackingStorePixelRatio || /**
ctx.backingStorePixelRatio || * @type {number} Horizontal scale.
1; */
const pixelRatio = devicePixelRatio / backingStoreRatio; this.sx = pixelRatio;
return {
sx: pixelRatio, /**
sy: pixelRatio, * @type {number} Vertical scale.
scaled: pixelRatio !== 1, */
}; this.sy = pixelRatio;
}
/**
* @type {boolean} Returns `true` when scaling is required, `false` otherwise.
*/
get scaled() {
return this.sx !== 1 || this.sy !== 1;
}
} }
/** /**
@ -840,7 +845,6 @@ export {
DEFAULT_SCALE_DELTA, DEFAULT_SCALE_DELTA,
DEFAULT_SCALE_VALUE, DEFAULT_SCALE_VALUE,
getActiveOrFocusedElement, getActiveOrFocusedElement,
getOutputScale,
getPageSizeInches, getPageSizeInches,
getVisibleElements, getVisibleElements,
isPortraitOrientation, isPortraitOrientation,
@ -853,6 +857,7 @@ export {
noContextMenuHandler, noContextMenuHandler,
normalizeWheelEventDelta, normalizeWheelEventDelta,
normalizeWheelEventDirection, normalizeWheelEventDirection,
OutputScale,
parseQueryString, parseQueryString,
PresentationModeState, PresentationModeState,
ProgressBar, ProgressBar,