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

Avoid to have a white line around the canvas

The canvas must have the same dims as the page in order to avoid to see the page
background.
This commit is contained in:
Calixte Denizet 2024-09-05 17:05:22 +02:00
parent 77c7ec6927
commit 68332ec236
8 changed files with 146 additions and 7 deletions

View file

@ -33,6 +33,7 @@ import {
} from "pdfjs-lib";
import {
approximateFraction,
calcRound,
DEFAULT_SCALE,
floorToDivide,
OutputScale,
@ -127,6 +128,10 @@ class PDFPageView {
#previousRotation = null;
#scaleRoundX = 1;
#scaleRoundY = 1;
#renderError = null;
#renderingState = RenderingStates.INITIAL;
@ -1039,11 +1044,27 @@ class PDFPageView {
const sfx = approximateFraction(outputScale.sx);
const sfy = approximateFraction(outputScale.sy);
canvas.width = floorToDivide(width * outputScale.sx, sfx[0]);
canvas.height = floorToDivide(height * outputScale.sy, sfy[0]);
const { style } = canvas;
style.width = floorToDivide(width, sfx[1]) + "px";
style.height = floorToDivide(height, sfy[1]) + "px";
const canvasWidth = (canvas.width = floorToDivide(
calcRound(width * outputScale.sx),
sfx[0]
));
const canvasHeight = (canvas.height = floorToDivide(
calcRound(height * outputScale.sy),
sfy[0]
));
const pageWidth = floorToDivide(calcRound(width), sfx[1]);
const pageHeight = floorToDivide(calcRound(height), sfy[1]);
outputScale.sx = canvasWidth / pageWidth;
outputScale.sy = canvasHeight / pageHeight;
if (this.#scaleRoundX !== sfx[1]) {
div.style.setProperty("--scale-round-x", `${sfx[1]}px`);
this.#scaleRoundX = sfx[1];
}
if (this.#scaleRoundY !== sfy[1]) {
div.style.setProperty("--scale-round-y", `${sfy[1]}px`);
this.#scaleRoundY = sfy[1];
}
// Add the viewport so it's known what it was originally drawn with.
this.#viewportMap.set(canvas, viewport);

View file

@ -83,6 +83,8 @@
canvas {
margin: 0;
display: block;
width: 100%;
height: 100%;
&[hidden] {
display: none;
@ -101,6 +103,9 @@
}
.pdfViewer .page {
--scale-round-x: 1px;
--scale-round-y: 1px;
direction: ltr;
width: 816px;
height: 1056px;

View file

@ -862,6 +862,25 @@ function toggleExpandedBtn(button, toggle, view = null) {
view?.classList.toggle("hidden", !toggle);
}
// In Firefox, the css calc function uses f32 precision but the Chrome or Safari
// are using f64 one. So in order to have the same rendering in all browsers, we
// need to use the right precision in order to have correct dimensions.
const calcRound =
typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")
? Math.fround
: (function () {
if (
typeof PDFJSDev !== "undefined" &&
PDFJSDev.test("LIB") &&
typeof document === "undefined"
) {
return x => x;
}
const e = document.createElement("div");
e.style.width = "round(down, calc(1.6666666666666665 * 792px), 1px)";
return e.style.width === "calc(1320px)" ? Math.fround : x => x;
})();
export {
animationStarted,
apiPageLayoutToViewerModes,
@ -870,6 +889,7 @@ export {
AutoPrintRegExp,
backtrackBeforeAllVisibleElements, // only exported for testing
binarySearchFirstItem,
calcRound,
CursorTool,
DEFAULT_SCALE,
DEFAULT_SCALE_DELTA,