Skip to content

Commit 279f2be

Browse files
committed
Fixed line-through rendering when letter spacing is used
1 parent 638974e commit 279f2be

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/shapes/Text.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,9 @@ export class Text extends Shape<TextConfig> {
318318
context.stroke();
319319
context.restore();
320320
}
321-
// draw line-through above the text content
321+
322+
// store the starting x position for line-through which is drawn after text
323+
const lineThroughStartX = lineTranslateX;
322324

323325
// As `letterSpacing` isn't supported on Safari, we use this polyfill.
324326
// The exception is for RTL text, which we rely on native as it cannot
@@ -380,14 +382,15 @@ export class Text extends Shape<TextConfig> {
380382

381383
context.fillStrokeShape(this);
382384
}
385+
383386
// draw line-through above the text content
384387
if (shouldLineThrough) {
385388
context.save();
386389
context.beginPath();
387390
const yOffset = !Konva.legacyTextRendering
388391
? -Math.round(fontSize / 4)
389392
: 0;
390-
const x = align === JUSTIFY ? 0 : lineTranslateX;
393+
const x = lineThroughStartX;
391394
context.moveTo(x, translateY + lineTranslateY + yOffset);
392395
const lineWidth =
393396
align === JUSTIFY && !lastLine ? totalWidth - padding * 2 : width;
@@ -402,6 +405,7 @@ export class Text extends Shape<TextConfig> {
402405
context.stroke();
403406
context.restore();
404407
}
408+
405409
context.restore();
406410
if (textArrLen > 1) {
407411
translateY += lineHeightPx;

test/unit/Text-test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1966,4 +1966,41 @@ describe('Text', function () {
19661966
);
19671967
}
19681968
});
1969+
1970+
it('text decoration with letterSpacing and lineHeight', function () {
1971+
var stage = addStage();
1972+
var layer = new Konva.Layer();
1973+
1974+
var text = new Konva.Text({
1975+
x: 10,
1976+
y: 10,
1977+
text: 'hello\nworld',
1978+
fontSize: 40,
1979+
fill: 'red',
1980+
letterSpacing: 5,
1981+
lineHeight: 2,
1982+
textDecoration: 'underline line-through',
1983+
});
1984+
1985+
layer.add(text);
1986+
stage.add(layer);
1987+
1988+
var trace = layer.getContext().getTrace(false, true);
1989+
if (Konva._renderBackend === 'web') {
1990+
assert.equal(
1991+
trace,
1992+
'clearRect(0,0,578,200);save();transform(1,0,0,1,10,10);font=normal normal 40px Arial;textBaseline=alphabetic;textAlign=left;translate(0,0);save();save();beginPath();moveTo(0,64);lineTo(110,64);stroke();restore();fillStyle=red;fillText(h,0,54);fillStyle=red;fillText(e,27,54);fillStyle=red;fillText(l,54,54);fillStyle=red;fillText(l,68,54);fillStyle=red;fillText(o,82,54);save();beginPath();moveTo(0,44);lineTo(110,44);stroke();restore();restore();save();save();beginPath();moveTo(0,144);lineTo(121,144);stroke();restore();fillStyle=red;fillText(w,0,134);fillStyle=red;fillText(o,33,134);fillStyle=red;fillText(r,61,134);fillStyle=red;fillText(l,79,134);fillStyle=red;fillText(d,93,134);save();beginPath();moveTo(0,124);lineTo(121,124);stroke();restore();restore();restore();'
1993+
);
1994+
} else if (Konva._renderBackend === 'node-canvas') {
1995+
assert.equal(
1996+
trace,
1997+
'clearRect(0,0,578,200);save();transform(1,0,0,1,10,10);font=normal normal 40px Arial;textBaseline=alphabetic;textAlign=left;translate(0,0);save();save();beginPath();moveTo(0,64);lineTo(110,64);stroke();restore();fillStyle=red;fillText(h,0,54);fillStyle=red;fillText(e,27,54);fillStyle=red;fillText(l,54,54);fillStyle=red;fillText(l,68,54);fillStyle=red;fillText(o,82,54);save();beginPath();moveTo(0,44);lineTo(110,44);stroke();restore();restore();save();save();beginPath();moveTo(0,144);lineTo(121,144);stroke();restore();fillStyle=red;fillText(w,0,134);fillStyle=red;fillText(o,33,134);fillStyle=red;fillText(r,61,134);fillStyle=red;fillText(l,79,134);fillStyle=red;fillText(d,93,134);save();beginPath();moveTo(0,124);lineTo(121,124);stroke();restore();restore();restore();'
1998+
);
1999+
} else {
2000+
assert.equal(
2001+
trace,
2002+
'clearRect(0,0,578,200);save();transform(1,0,0,1,10,10);font=normal normal 40px Arial;textBaseline=alphabetic;textAlign=left;translate(0,0);save();save();beginPath();moveTo(0,63);lineTo(110,63);stroke();restore();fillStyle=red;fillText(h,0,53);fillStyle=red;fillText(e,27,53);fillStyle=red;fillText(l,54,53);fillStyle=red;fillText(l,68,53);fillStyle=red;fillText(o,82,53);save();beginPath();moveTo(0,43);lineTo(110,43);stroke();restore();restore();save();save();beginPath();moveTo(0,143);lineTo(121,143);stroke();restore();fillStyle=red;fillText(w,0,133);fillStyle=red;fillText(o,33,133);fillStyle=red;fillText(r,61,133);fillStyle=red;fillText(l,79,133);fillStyle=red;fillText(d,93,133);save();beginPath();moveTo(0,123);lineTo(121,123);stroke();restore();restore();restore();'
2003+
);
2004+
}
2005+
});
19692006
});

0 commit comments

Comments
 (0)