Files
pdf.js/test/unit/pattern_spec.js
Calixte Denizet b29564d94c Fix function-based shading scaled by the CTM
A function-based shading was sampled at one step per user-space unit of
its own matrix, so a unit domain scaled up by the CTM, a form matrix or
a pattern matrix was drawn with a single cell.
The sampling grid is now computed from the transform the shading is
painted with, and the generated meshes are cached per grid size.

The setFillColorN/setStrokeColorN cases are merged so the base transform
is only passed in one place.

The software mesh rasterizer now clamps triangles to the canvas, so
pixels on the right edge no longer wrap into the next row.

It fixes #22616.
2026-11-28 16:36:15 +01:00

224 lines
7.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* Copyright 2026 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Dict, Name } from "../../src/core/primitives.js";
import {
GlobalColorSpaceCache,
LocalColorSpaceCache,
} from "../../src/core/image_utils.js";
import { compilePatternInfo } from "../../src/core/obj_bin_transform_core.js";
import { IDENTITY_MATRIX } from "../../src/core/core_utils.js";
import { Pattern } from "../../src/core/pattern.js";
import { PatternInfo } from "../../src/display/obj_bin_transform_display.js";
describe("pattern", function () {
describe("FunctionBasedShading", function () {
function createFunctionBasedShading({
colorSpace = "DeviceRGB",
domain = [0, 1, 0, 1],
matrix = [2, 0, 0, 3, 10, 20],
background = null,
fn = (src, srcOffset, dest, destOffset) => {
dest[destOffset] = src[srcOffset];
dest[destOffset + 1] = src[srcOffset + 1];
dest[destOffset + 2] = 0;
},
numInputs = 2,
numOutputs = 3,
} = {}) {
const dict = new Dict();
dict.set("ShadingType", 1);
if (Array.isArray(colorSpace)) {
// `setIfName` only accepts a name, and would silently drop an Array.
dict.set("ColorSpace", colorSpace);
} else {
dict.setIfName("ColorSpace", colorSpace);
}
dict.set("Domain", domain);
dict.set("Matrix", matrix);
if (background) {
dict.set("Background", background);
}
dict.set("Function", {
fn: Object.assign(fn, { numInputs, numOutputs }),
});
const pdfFunctionFactory = {
create: fnObj => fnObj.fn,
};
const xref = {
fetchIfRef: obj => obj,
};
return Pattern.parseShading(
dict,
xref,
/* res = */ null,
pdfFunctionFactory,
new GlobalColorSpaceCache(),
new LocalColorSpaceCache()
);
}
function getMeshIR(shading) {
return shading.getIR(shading.getSamplingGrid(IDENTITY_MATRIX));
}
it("must convert Type 1 shading into packed mesh IR", async function () {
const shading = await createFunctionBasedShading();
const ir = getMeshIR(shading);
expect(ir[0]).toEqual("Mesh");
expect(ir[1]).toEqual(1);
// Vertices are pre-expanded: 3×4 lattice →
// 6 quads → 12 triangles → 36 vertices
expect(ir[2]).toBeInstanceOf(Float32Array);
expect(ir[2].length).toEqual(72); // 36 vertices × 2 coords
expect(ir[3]).toBeInstanceOf(Uint8Array);
expect(ir[3].length).toEqual(144); // 36 vertices × 4 bytes
expect(ir[4]).toEqual(36); // vertexCount
expect(ir[5]).toEqual([10, 20, 12, 23]);
expect(ir[6]).toBeNull();
expect(ir[7]).toBeNull();
});
it("must bound the color component buffer while batching", async function () {
const numComps = 32;
let batchStarts = 0;
let componentBufferLength = 0;
const colorSpace = [
Name.get("DeviceN"),
Array.from({ length: numComps }, (_, i) => Name.get(`Colorant${i}`)),
Name.get("DeviceRGB"),
{
fn(src, srcOffset, dest, destOffset) {
dest[destOffset] = src[srcOffset];
dest[destOffset + 1] = src[srcOffset + 1];
dest[destOffset + 2] = src[srcOffset + 2];
},
},
];
const shading = await createFunctionBasedShading({
colorSpace,
matrix: [64, 0, 0, 64, 0, 0],
fn(src, srcOffset, dest, destOffset) {
if (destOffset === 0) {
batchStarts++;
componentBufferLength = dest.length;
}
dest[destOffset] = src[srcOffset];
dest[destOffset + 1] = src[srcOffset + 1];
dest[destOffset + 2] = 0.5;
dest.fill(0, destOffset + 3, destOffset + numComps);
},
numOutputs: numComps,
});
const [, , , colors] = getMeshIR(shading);
const totalVertices = (64 + 1) ** 2;
expect(batchStarts).toBeGreaterThan(1);
expect(componentBufferLength).toBeLessThan(totalVertices * numComps);
expect(Array.from(colors.subarray(0, 4))).toEqual([0, 0, 128, 0]);
});
it("must keep mesh colors intact through binary serialization", async function () {
const shading = await createFunctionBasedShading({
background: [0.25, 0.5, 0.75],
});
const ir = getMeshIR(shading);
const buffer = compilePatternInfo(ir);
const reconstructedIR = new PatternInfo(buffer).getIR();
expect(reconstructedIR[0]).toEqual("Mesh");
expect(reconstructedIR[1]).toEqual(1);
expect(Array.from(reconstructedIR[2])).toEqual(Array.from(ir[2]));
expect(Array.from(reconstructedIR[3])).toEqual(Array.from(ir[3]));
expect(Array.from(reconstructedIR[7])).toEqual([64, 128, 191]);
});
it("must sample the upper and right edges half a step inside", async function () {
const shading = await createFunctionBasedShading({
colorSpace: "DeviceGray",
fn(src, srcOffset, dest, destOffset) {
dest[destOffset] =
src[srcOffset] === 1 || src[srcOffset + 1] === 1 ? 1 : 0;
},
numOutputs: 1,
});
const [, , , colors] = getMeshIR(shading);
expect(colors.length).toEqual(144);
expect(Array.from(colors)).toEqual(new Array(144).fill(0));
});
it("must ignore a function with the wrong dimensions", async function () {
expect(
(await createFunctionBasedShading({ numInputs: 1 })).getIR()
).toEqual(["Dummy"]);
expect(
(await createFunctionBasedShading({ numOutputs: 1 })).getIR()
).toEqual(["Dummy"]);
});
});
describe("RadialAxialShading", function () {
function createAxialShading({ numInputs, numOutputs }) {
const dict = new Dict();
dict.set("ShadingType", 2);
dict.setIfName("ColorSpace", "DeviceRGB");
dict.set("Coords", [0, 0, 100, 0]);
dict.set("Extend", [true, true]);
dict.set("Function", {
fn: Object.assign(
(src, srcOffset, dest, destOffset) => {
dest.fill(src[srcOffset], destOffset, destOffset + numOutputs);
},
{ numInputs, numOutputs }
),
});
return Pattern.parseShading(
dict,
/* xref = */ { fetchIfRef: obj => obj },
/* res = */ null,
/* pdfFunctionFactory = */ { create: fnObj => fnObj.fn },
new GlobalColorSpaceCache(),
new LocalColorSpaceCache()
);
}
it("must accept a 1-in, n-out function", async function () {
const ir = (
await createAxialShading({ numInputs: 1, numOutputs: 3 })
).getIR();
expect(ir[0]).toEqual("RadialAxial");
expect(ir[3]).toEqual([
[0, "#000000"],
[1, "#ffffff"],
]);
});
it("must ignore a function with the wrong dimensions", async function () {
expect(
(await createAxialShading({ numInputs: 18, numOutputs: 3 })).getIR()
).toEqual(["Dummy"]);
expect(
(await createAxialShading({ numInputs: 1, numOutputs: 1 })).getIR()
).toEqual(["Dummy"]);
});
});
});