Skip to content

Commit

Permalink
Merge pull request #19196 from Snuffleupagus/issue-19176
Browse files Browse the repository at this point in the history
Take the `userUnit` into account in the `PageViewport` class (issue 19176)
  • Loading branch information
Snuffleupagus authored Dec 9, 2024
2 parents 6177bb5 + c6e3fc4 commit 5dc2d25
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1408,6 +1408,7 @@ class PDFPageProxy {
} = {}) {
return new PageViewport({
viewBox: this.view,
userUnit: this.userUnit,
scale,
rotation,
offsetX,
Expand Down
19 changes: 14 additions & 5 deletions src/display/display_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ async function fetchData(url, type = "text") {
* @typedef {Object} PageViewportParameters
* @property {Array<number>} viewBox - The xMin, yMin, xMax and
* yMax coordinates.
* @property {number} userUnit - The size of units.
* @property {number} scale - The scale of the viewport.
* @property {number} rotation - The rotation, in degrees, of the viewport.
* @property {number} [offsetX] - The horizontal, i.e. x-axis, offset. The
Expand Down Expand Up @@ -116,18 +117,22 @@ class PageViewport {
*/
constructor({
viewBox,
userUnit,
scale,
rotation,
offsetX = 0,
offsetY = 0,
dontFlip = false,
}) {
this.viewBox = viewBox;
this.userUnit = userUnit;
this.scale = scale;
this.rotation = rotation;
this.offsetX = offsetX;
this.offsetY = offsetY;

scale *= userUnit; // Take the userUnit into account.

// creating transform to convert pdf coordinate system to the normal
// canvas like coordinates taking in account scale and rotation
const centerX = (viewBox[2] + viewBox[0]) / 2;
Expand Down Expand Up @@ -208,12 +213,14 @@ class PageViewport {
* @type {Object}
*/
get rawDims() {
const { viewBox } = this;
const { userUnit, viewBox } = this;
const dims = viewBox.map(x => x * userUnit);

return shadow(this, "rawDims", {
pageWidth: viewBox[2] - viewBox[0],
pageHeight: viewBox[3] - viewBox[1],
pageX: viewBox[0],
pageY: viewBox[1],
pageWidth: dims[2] - dims[0],
pageHeight: dims[3] - dims[1],
pageX: dims[0],
pageY: dims[1],
});
}

Expand All @@ -231,6 +238,7 @@ class PageViewport {
} = {}) {
return new PageViewport({
viewBox: this.viewBox.slice(),
userUnit: this.userUnit,
scale,
rotation,
offsetX,
Expand Down Expand Up @@ -514,6 +522,7 @@ function getXfaPageViewport(xfaPage, { scale = 1, rotation = 0 }) {

return new PageViewport({
viewBox,
userUnit: 1,
scale,
rotation,
});
Expand Down
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@
!poppler-67295-0.pdf
!poppler-85140-0.pdf
!issue15012.pdf
!issue19176.pdf
!issue15150.pdf
!poppler-395-0-fuzzed.pdf
!issue14165.pdf
Expand Down
Binary file added test/pdfs/issue19176.pdf
Binary file not shown.
7 changes: 7 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2734,6 +2734,13 @@
"type": "eq",
"about": "TrueType font with (0, 1) cmap."
},
{
"id": "issue19176",
"file": "pdfs/issue19176.pdf",
"md5": "c307418412a72997671518fa978439e0",
"rounds": 1,
"type": "eq"
},
{
"id": "issue4801",
"file": "pdfs/issue4801.pdf",
Expand Down
34 changes: 33 additions & 1 deletion test/unit/api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3081,10 +3081,21 @@ describe("api", function () {
expect(page.ref).toEqual({ num: 15, gen: 0 });
});

it("gets userUnit", function () {
it("gets default userUnit", function () {
expect(page.userUnit).toEqual(1.0);
});

it("gets non-default userUnit", async function () {
const loadingTask = getDocument(buildGetDocumentParams("issue19176.pdf"));

const pdfDoc = await loadingTask.promise;
const pdfPage = await pdfDoc.getPage(1);

expect(pdfPage.userUnit).toEqual(72);

await loadingTask.destroy();
});

it("gets view", function () {
expect(page.view).toEqual([0, 0, 595.28, 841.89]);
});
Expand Down Expand Up @@ -3116,13 +3127,34 @@ describe("api", function () {
expect(viewport instanceof PageViewport).toEqual(true);

expect(viewport.viewBox).toEqual(page.view);
expect(viewport.userUnit).toEqual(page.userUnit);
expect(viewport.scale).toEqual(1.5);
expect(viewport.rotation).toEqual(90);
expect(viewport.transform).toEqual([0, 1.5, 1.5, 0, 0, 0]);
expect(viewport.width).toEqual(1262.835);
expect(viewport.height).toEqual(892.92);
});

it("gets viewport with non-default userUnit", async function () {
const loadingTask = getDocument(buildGetDocumentParams("issue19176.pdf"));

const pdfDoc = await loadingTask.promise;
const pdfPage = await pdfDoc.getPage(1);

const viewport = pdfPage.getViewport({ scale: 1 });
expect(viewport instanceof PageViewport).toEqual(true);

expect(viewport.viewBox).toEqual(pdfPage.view);
expect(viewport.userUnit).toEqual(pdfPage.userUnit);
expect(viewport.scale).toEqual(1);
expect(viewport.rotation).toEqual(0);
expect(viewport.transform).toEqual([72, 0, 0, -72, 0, 792]);
expect(viewport.width).toEqual(612);
expect(viewport.height).toEqual(792);

await loadingTask.destroy();
});

it('gets viewport with "offsetX/offsetY" arguments', function () {
const viewport = page.getViewport({
scale: 1,
Expand Down

0 comments on commit 5dc2d25

Please sign in to comment.