Files
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

90 lines
2.6 KiB
JavaScript

/* 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 { captureCalls, processPdf } from "./capture_utils.js";
import {
GlobalColorSpaceCache,
LocalColorSpaceCache,
} from "../../src/core/image_utils.js";
import { BaseStream } from "../../src/core/base_stream.js";
import { IDENTITY_MATRIX } from "../../src/core/core_utils.js";
import { Pattern } from "../../src/core/pattern.js";
import { PDFFunctionFactory } from "../../src/core/function.js";
class ShadingInput {
#resources;
#shading;
#xref;
constructor(shading, xref, resources) {
this.#shading = shading;
this.#xref = xref;
this.#resources = resources;
}
get dict() {
const shading = this.#shading;
return shading instanceof BaseStream ? shading.dict : shading;
}
async getIR() {
const shading = this.#shading;
if (shading instanceof BaseStream) {
shading.reset();
}
const pattern = await Pattern.parseShading(
shading,
this.#xref,
this.#resources,
new PDFFunctionFactory({ xref: this.#xref }),
new GlobalColorSpaceCache(),
new LocalColorSpaceCache()
);
return pattern.getIR(pattern.getSamplingGrid?.(IDENTITY_MATRIX));
}
}
/**
* @param {string} filename
* @param {Function} [filter] - Predicate on the shading dictionary.
* @param {object} [options] - See `processPdf`.
* @returns {Promise<Array<ShadingInput>>}
*/
async function captureShadings(filename, filter = () => true, options) {
const shadings = await captureCalls(
Pattern,
"parseShading",
() => processPdf(filename, { ...options, operatorList: true }),
(target, [shading, xref, resources]) => {
const input = new ShadingInput(shading, xref, resources);
return filter(input.dict) ? input : undefined;
},
{ before: true }
);
if (shadings.length === 0) {
throw new Error(`No matching shading in "${filename}".`);
}
for (const input of shadings) {
if ((await input.getIR())[0] === "Dummy") {
throw new Error(`Shading in "${filename}" can't be replayed.`);
}
}
return shadings;
}
export { captureShadings };