Reduce duplication when computing the maximum canvas pixels

*This is something that occurred to me when reviewing the latest PDF.js update in mozilla-central.*

Currently we duplicate essentially the same code in both the `OutputScale.prototype.limitCanvas` and `PDFPageDetailView.prototype.update` methods, which seems unnecessary, and to avoid that we introduce a new `OutputScale.capPixels` method that is used to compute the maximum canvas pixels.
This commit is contained in:
Jonas Jenwald 2025-05-10 15:42:29 +02:00
parent ca05a0dbfc
commit d5c534fb83
2 changed files with 12 additions and 28 deletions

View File

@ -665,13 +665,7 @@ class OutputScale {
maxWidthScale = Infinity, maxWidthScale = Infinity,
maxHeightScale = Infinity; maxHeightScale = Infinity;
if (capAreaFactor >= 0) { maxPixels = OutputScale.capPixels(maxPixels, capAreaFactor);
const cappedWindowArea = OutputScale.getCappedWindowArea(capAreaFactor);
maxPixels =
maxPixels > 0
? Math.min(maxPixels, cappedWindowArea)
: cappedWindowArea;
}
if (maxPixels > 0) { if (maxPixels > 0) {
maxAreaScale = Math.sqrt(maxPixels / (width * height)); maxAreaScale = Math.sqrt(maxPixels / (width * height));
} }
@ -693,21 +687,18 @@ class OutputScale {
return globalThis.devicePixelRatio || 1; return globalThis.devicePixelRatio || 1;
} }
static getCappedWindowArea(capAreaFactor) { static capPixels(maxPixels, capAreaFactor) {
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("TESTING")) { if (capAreaFactor >= 0) {
return Math.ceil( const winPixels = Math.ceil(
window.innerWidth * (typeof PDFJSDev !== "undefined" && PDFJSDev.test("TESTING")
window.innerHeight * ? window.innerWidth * window.innerHeight
: window.screen.availWidth * window.screen.availHeight) *
this.pixelRatio ** 2 * this.pixelRatio ** 2 *
(1 + capAreaFactor / 100) (1 + capAreaFactor / 100)
); );
return maxPixels > 0 ? Math.min(maxPixels, winPixels) : winPixels;
} }
return Math.ceil( return maxPixels;
window.screen.availWidth *
window.screen.availHeight *
this.pixelRatio ** 2 *
(1 + capAreaFactor / 100)
);
} }
} }

View File

@ -140,18 +140,10 @@ class PDFPageDetailView extends BasePDFPageView {
return; return;
} }
const { viewport, capCanvasAreaFactor } = this.pageView; const { viewport, maxCanvasPixels, capCanvasAreaFactor } = this.pageView;
const visibleWidth = visibleArea.maxX - visibleArea.minX; const visibleWidth = visibleArea.maxX - visibleArea.minX;
const visibleHeight = visibleArea.maxY - visibleArea.minY; const visibleHeight = visibleArea.maxY - visibleArea.minY;
let { maxCanvasPixels } = this.pageView;
if (capCanvasAreaFactor >= 0) {
maxCanvasPixels = Math.min(
maxCanvasPixels,
OutputScale.getCappedWindowArea(capCanvasAreaFactor)
);
}
// "overflowScale" represents which percentage of the width and of the // "overflowScale" represents which percentage of the width and of the
// height the detail area extends outside of the visible area. We want to // height the detail area extends outside of the visible area. We want to
@ -164,7 +156,8 @@ class PDFPageDetailView extends BasePDFPageView {
const visiblePixels = const visiblePixels =
visibleWidth * visibleHeight * OutputScale.pixelRatio ** 2; visibleWidth * visibleHeight * OutputScale.pixelRatio ** 2;
const maxDetailToVisibleLinearRatio = Math.sqrt( const maxDetailToVisibleLinearRatio = Math.sqrt(
maxCanvasPixels / visiblePixels OutputScale.capPixels(maxCanvasPixels, capCanvasAreaFactor) /
visiblePixels
); );
const maxOverflowScale = (maxDetailToVisibleLinearRatio - 1) / 2; const maxOverflowScale = (maxDetailToVisibleLinearRatio - 1) / 2;
let overflowScale = Math.min(1, maxOverflowScale); let overflowScale = Math.min(1, maxOverflowScale);