Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly render the glyph outline when it has a stroke pattern #19361

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions src/core/font_renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ function lookupCmap(ranges, unicode) {

function compileGlyf(code, cmds, font) {
function moveTo(x, y) {
if (firstPoint) {
// Close the current subpath in adding a straight line to the first point.
cmds.add("L", firstPoint);
}
firstPoint = [x, y];
cmds.add("M", [x, y]);
}
function lineTo(x, y) {
Expand All @@ -182,6 +187,7 @@ function compileGlyf(code, cmds, font) {
let i = 0;
const numberOfContours = getInt16(code, i);
let flags;
let firstPoint = null;
let x = 0,
y = 0;
i += 10;
Expand Down Expand Up @@ -350,6 +356,11 @@ function compileGlyf(code, cmds, font) {

function compileCharString(charStringCode, cmds, font, glyphId) {
function moveTo(x, y) {
if (firstPoint) {
// Close the current subpath in adding a straight line to the first point.
cmds.add("L", firstPoint);
}
firstPoint = [x, y];
cmds.add("M", [x, y]);
}
function lineTo(x, y) {
Expand All @@ -363,6 +374,7 @@ function compileCharString(charStringCode, cmds, font, glyphId) {
let x = 0,
y = 0;
let stems = 0;
let firstPoint = null;

function parse(code) {
let i = 0;
Expand Down
19 changes: 17 additions & 2 deletions src/display/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -2043,12 +2043,13 @@ class CanvasGraphics {
ctx.save();
ctx.translate(x, y);
ctx.scale(fontSize, -fontSize);
let currentTransform;
if (
fillStrokeMode === TextRenderingMode.FILL ||
fillStrokeMode === TextRenderingMode.FILL_STROKE
) {
if (patternFillTransform) {
const currentTransform = ctx.getTransform();
currentTransform = ctx.getTransform();
ctx.setTransform(...patternFillTransform);
ctx.fill(
this.#getScaledPath(path, currentTransform, patternFillTransform)
Expand All @@ -2062,8 +2063,22 @@ class CanvasGraphics {
fillStrokeMode === TextRenderingMode.FILL_STROKE
) {
if (patternStrokeTransform) {
const currentTransform = ctx.getTransform();
currentTransform ||= ctx.getTransform();
ctx.setTransform(...patternStrokeTransform);
const { a, b, c, d } = currentTransform;
const invPatternTransform = Util.inverseTransform(
patternStrokeTransform
);
const transf = Util.transform(
[a, b, c, d, 0, 0],
invPatternTransform
);
const [sx, sy] = Util.singularValueDecompose2dScale(transf);

// Cancel the pattern scaling of the line width.
// If sx and sy are different, unfortunately we can't do anything and
// we'll have a rendering bug.
ctx.lineWidth *= Math.max(sx, sy) / fontSize;
ctx.stroke(
this.#getScaledPath(path, currentTransform, patternStrokeTransform)
);
Expand Down
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -696,3 +696,4 @@
!issue18911.pdf
!issue19207.pdf
!issue19239.pdf
!issue19360.pdf
Binary file added test/pdfs/issue19360.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 @@ -11286,5 +11286,12 @@
"firstPage": 2,
"lastPage": 2,
"type": "eq"
},
{
"id": "issue19360",
"file": "pdfs/issue19360.pdf",
"md5": "b2de376f7e96fa2b6afc00dac016c40a",
"rounds": 1,
"type": "eq"
}
]
Loading