From a24b30b110193e3065ce1f9c1045413aec9424bf Mon Sep 17 00:00:00 2001 From: Adam Greenan Date: Wed, 11 Dec 2024 13:35:24 +0000 Subject: [PATCH] Add calculation for letter spacing to better align with css letter-spacing properties --- src/shapes/Text.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/shapes/Text.ts b/src/shapes/Text.ts index dbd3b948..7097ca08 100644 --- a/src/shapes/Text.ts +++ b/src/shapes/Text.ts @@ -263,7 +263,7 @@ export class Text extends Shape { var lineTranslateY = 0; var obj = textArr[n], text = obj.text, - width = obj.width, + width = obj.width + letterSpacing, lastLine = obj.lastInParagraph, spacesNumber, oneWord, @@ -310,7 +310,7 @@ export class Text extends Shape { spacesNumber = text.split(' ').length - 1; oneWord = spacesNumber === 0; lineWidth = - align === JUSTIFY && !lastLine ? totalWidth - padding * 2 : width; + align === JUSTIFY && !lastLine ? totalWidth - padding * 2 : width; context.lineTo( lineTranslateX + Math.round(lineWidth), translateY + lineTranslateY + yOffset @@ -383,7 +383,8 @@ export class Text extends Shape { return isAuto ? this.getTextWidth() + this.padding() * 2 : this.attrs.width; } getHeight() { - const isAuto = this.attrs.height === AUTO || this.attrs.height === undefined; + const isAuto = + this.attrs.height === AUTO || this.attrs.height === undefined; return isAuto ? this.fontSize() * this.textArr.length * this.lineHeight() + this.padding() * 2 @@ -501,7 +502,9 @@ export class Text extends Shape { this.textArr = []; getDummyContext().font = this._getContextFont(); - const additionalWidth = shouldAddEllipsis ? this._getTextWidth(ELLIPSIS) : 0; + const additionalWidth = shouldAddEllipsis + ? this._getTextWidth(ELLIPSIS) + : 0; for (let i = 0, max = lines.length; i < max; ++i) { let line = lines[i];