@@ -152,7 +152,11 @@ export class Text extends Shape {
152152 }
153153
154154 private computeFontSize ( text : string , defaultFontSize : number , context : CanvasRenderingContext2D ) : number {
155- const defaultTextWidth = context . measureText ( text ) . width ;
155+ let defaultTextWidth = 0 ;
156+ if ( defaultFontSize < 1 ) {
157+ context . font = Text . buildFont ( this . style , 1 , this . font ) ;
158+ defaultTextWidth = context . measureText ( text ) . width * defaultFontSize ;
159+ } else defaultTextWidth = context . measureText ( text ) . width ;
156160 if ( defaultTextWidth >= this . boundingBox . size . x ) return defaultFontSize * this . boundingBox . size . x / defaultTextWidth ;
157161 return defaultFontSize
158162 }
@@ -386,12 +390,9 @@ export class Text extends Shape {
386390 let newRow = '' ;
387391 while ( context . measureText ( newRow ) . width < this . boundingBox . size . x && pickedWords < this . words . length ) {
388392 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 ) ;
393+ if ( newRow != '' ) break
394+ }
395+ [ newRow , pickedWords ] = this . addPickedWordToRow ( newRow , pickedWords ) ;
395396 }
396397 if ( newRow . length != 0 ) rows . push ( newRow ) ;
397398 return [ rows , pickedWords , fontsize ]
@@ -400,7 +401,13 @@ export class Text extends Shape {
400401 private fixedFontSplit ( fontsize : number , context : CanvasRenderingContext2D ) : [ string [ ] , number ] {
401402 let rows : string [ ] = [ ] ;
402403 let pickedWords = 0 ;
403- while ( pickedWords < this . words . length ) [ rows , pickedWords , fontsize ] = this . computeNewRow ( context , pickedWords , rows , fontsize ) ;
404+ while ( pickedWords < this . words . length ) {
405+ [ rows , pickedWords , fontsize ] = this . computeNewRow ( context , pickedWords , rows , fontsize ) ;
406+ if ( this . isTextTooWide ( context , rows [ rows . length - 1 ] ) ) {
407+ fontsize = this . computeFontSize ( rows [ rows . length - 1 ] , fontsize , context ) ;
408+ context . font = Text . buildFont ( this . style , fontsize , this . font ) ;
409+ }
410+ }
404411 return [ rows , fontsize ]
405412 }
406413 private cleanStartAllRows ( rows : string [ ] ) : string [ ] { return rows . map ( row => row . trimStart ( ) ) }
0 commit comments