Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions test/unit/util_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import {
string32,
stringToBytes,
stringToPDFString,
Util,
} from "../../src/shared/util.js";
import { DOMMatrix, DOMPoint } from "@napi-rs/canvas";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test cannot run because of this line.
Can you check it works for you locally in running gulp unittest ?

Copy link
Contributor Author

@maettuu maettuu Sep 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, this is a native node module, therefore the crash with the gulp unittest.
If we move the test to a browser test file where these modules are available by default this could fix the issue. Which file would fit best? Alternatively, we could use a different pure-JS library such as "transformation-matrix", but not sure whether you want to add something like that to the repo requirements just because of this test.
Lmk what you think is best :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


describe("util", function () {
describe("BaseException", function () {
Expand Down Expand Up @@ -258,4 +260,28 @@ describe("util", function () {
expect(uuid.length).toBeGreaterThanOrEqual(32);
});
});

describe("applyTransform", function () {
it("should apply transformations correctly without subarray creation", async function () {
const cases = [
{ transform: [1, 0, 0, 1, 10, 20], points: [5, 5, 10, 10] }, // translation
{ transform: [0.5, 0.3, -0.3, 0.5, -5, 5], points: [0, 0, 1, 1] }, // scale+shear+translate
{ transform: [2, 0, 0, 2, 0, 0], points: [-2, 3, 4, -5] }, // simple doubling
];

for (const { transform, points: original } of cases) {
const M = new DOMMatrix(transform);
const pts = original.slice();

const p0 = M.transformPoint(new DOMPoint(original[0], original[1]));
const p1 = M.transformPoint(new DOMPoint(original[2], original[3]));
const expected = [p0.x, p0.y, p1.x, p1.y];

Util.applyTransform(pts, transform, 0);
Util.applyTransform(pts, transform, 2);

expect(pts).toEqual(expected);
}
});
});
});