Skip to content

Commit 8147102

Browse files
committed
fix(text): first fix of multiline small width
1 parent 3cf0c79 commit 8147102

1 file changed

Lines changed: 24 additions & 15 deletions

File tree

src/shapes.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,17 @@ export class Text extends Shape {
151151
]
152152
}
153153

154+
private computeFontSize(text: string, defaultFontSize: number, context: CanvasRenderingContext2D): number {
155+
const defaultTextWidth = context.measureText(text).width;
156+
if (defaultTextWidth >= this.boundingBox.size.x) return defaultFontSize * this.boundingBox.size.x / defaultTextWidth;
157+
return defaultFontSize
158+
}
159+
154160
private automaticFontSize(context: CanvasRenderingContext2D): number {
155161
let fontsize = Math.min(this.boundingBox.size.y ?? Number.POSITIVE_INFINITY, this.fontsize ?? Number.POSITIVE_INFINITY);
156162
if (fontsize == Number.POSITIVE_INFINITY) fontsize = C.DEFAULT_FONTSIZE;
157163
context.font = Text.buildFont(this.style, fontsize, this.font);
158-
const defaultTextWidth = context.measureText(this.text).width;
159-
if (defaultTextWidth >= this.boundingBox.size.x) fontsize = fontsize * this.boundingBox.size.x / defaultTextWidth;
160-
return fontsize
164+
return this.computeFontSize(this.text, fontsize, context)
161165
}
162166

163167
private setRectOffsetX(): number {
@@ -268,10 +272,11 @@ export class Text extends Shape {
268272

269273
private multiLineWrite(writtenText: string[], middleFactor: number, context: CanvasRenderingContext2D): void {
270274
const nRows = writtenText.length - 1;
275+
const fontsize = Math.max(this.fontsize, 1);
271276
writtenText.forEach((row, index) => {
272277
["top", "hanging"].includes(this.baseline)
273-
? context.fillText(index == 0 ? Text.capitalize(row) : row, 0, index * this.fontsize + this.offset)
274-
: context.fillText(index == 0 ? Text.capitalize(row) : row, 0, (index - nRows / middleFactor) * this.fontsize + this.offset);
278+
? context.fillText(index == 0 ? Text.capitalize(row) : row, 0, index * fontsize + this.offset)
279+
: context.fillText(index == 0 ? Text.capitalize(row) : row, 0, (index - nRows / middleFactor) * fontsize + this.offset);
275280
});
276281
}
277282

@@ -335,7 +340,7 @@ export class Text extends Shape {
335340
if (oneRowLength < this.boundingBox.size.x) {
336341
return [[this.text.trimStart()], fontsize > this.boundingBox.size.y ? this.boundingBox.size.y ?? fontsize : fontsize];
337342
}
338-
if (!this.boundingBox.size.y) return [this.fixedFontSplit(context), fontsize];
343+
if (!this.boundingBox.size.y) return this.fixedFontSplit(fontsize, context)
339344
return this.autoFontSplit(fontsize, context);
340345
}
341346

@@ -377,23 +382,27 @@ export class Text extends Shape {
377382
return [row + this.words[pickedWords], pickedWords + 1]
378383
}
379384

380-
private computeNewRow(context: CanvasRenderingContext2D, pickedWords: number, rows: string[]): [string[], number] {
385+
private computeNewRow(context: CanvasRenderingContext2D, pickedWords: number, rows: string[], fontsize: number): [string[], number, number] {
381386
let newRow = '';
382387
while (context.measureText(newRow).width < this.boundingBox.size.x && pickedWords < this.words.length) {
383-
if (this.isTextTooWide(context, newRow + this.words[pickedWords]) && newRow != '') break
384-
else [newRow, pickedWords] = this.addPickedWordToRow(newRow, pickedWords);
388+
if (this.isTextTooWide(context, newRow + this.words[pickedWords])) {
389+
if (newRow != '') break
390+
else {
391+
fontsize = this.computeFontSize(newRow + this.words[pickedWords], fontsize, context);
392+
context.font = Text.buildFont(this.style, fontsize, this.font);
393+
}
394+
} else [newRow, pickedWords] = this.addPickedWordToRow(newRow, pickedWords);
385395
}
386396
if (newRow.length != 0) rows.push(newRow);
387-
return [rows, pickedWords]
397+
return [rows, pickedWords, fontsize]
388398
}
389399

390-
private fixedFontSplit(context: CanvasRenderingContext2D): string[] {
400+
private fixedFontSplit(fontsize: number, context: CanvasRenderingContext2D): [string[], number] {
391401
let rows: string[] = [];
392402
let pickedWords = 0;
393-
while (pickedWords < this.words.length) [rows, pickedWords] = this.computeNewRow(context, pickedWords, rows);
394-
return rows
403+
while (pickedWords < this.words.length) [rows, pickedWords, fontsize] = this.computeNewRow(context, pickedWords, rows, fontsize);
404+
return [rows, fontsize]
395405
}
396-
397406
private cleanStartAllRows(rows: string[]): string[] { return rows.map(row => row.trimStart()) }
398407

399408
private checkWordsLength(context: CanvasRenderingContext2D): boolean {
@@ -405,7 +414,7 @@ export class Text extends Shape {
405414

406415
private computeTextHeight(fontsize: number, textHeight: number, rows: string[], context: CanvasRenderingContext2D): [string[], number] {
407416
if (this.checkWordsLength(context)) {
408-
rows = this.fixedFontSplit(context);
417+
rows = this.fixedFontSplit(fontsize, context)[0];
409418
textHeight = fontsize * rows.length;
410419
}
411420
return [rows, textHeight]

0 commit comments

Comments
 (0)