@@ -141,12 +141,21 @@ export class Text extends Shape {
141141 ]
142142 }
143143
144+ private computeFontSize ( text : string , defaultFontSize : number , context : CanvasRenderingContext2D ) : number {
145+ let defaultTextWidth = 0 ;
146+ if ( defaultFontSize < 1 ) {
147+ context . font = Text . buildFont ( this . style , 1 , this . font ) ;
148+ defaultTextWidth = context . measureText ( text ) . width * defaultFontSize ;
149+ } else defaultTextWidth = context . measureText ( text ) . width ;
150+ if ( defaultTextWidth >= this . boundingBox . size . x ) return defaultFontSize * this . boundingBox . size . x / defaultTextWidth ;
151+ return defaultFontSize
152+ }
153+
144154 private automaticFontSize ( context : CanvasRenderingContext2D ) : number {
145155 let fontsize = Math . min ( this . boundingBox . size . y ?? Number . POSITIVE_INFINITY , this . fontsize ?? Number . POSITIVE_INFINITY ) ;
146156 if ( fontsize == Number . POSITIVE_INFINITY ) fontsize = DEFAULT_FONTSIZE ;
147157 context . font = Text . buildFont ( this . style , fontsize , this . font ) ;
148- if ( context . measureText ( this . text ) . width >= this . boundingBox . size . x ) fontsize = fontsize * this . boundingBox . size . x / context . measureText ( this . text ) . width ;
149- return fontsize
158+ return this . computeFontSize ( this . text , fontsize , context )
150159 }
151160
152161 private setRectOffsetX ( ) : number {
@@ -203,7 +212,13 @@ export class Text extends Shape {
203212 context . resetTransform ( ) ;
204213 context . translate ( origin . x , origin . y ) ;
205214 context . rotate ( Math . PI / 180 * this . orientation ) ;
206- if ( this . isScaled ) context . scale ( Math . abs ( contextMatrix . a ) , Math . abs ( contextMatrix . d ) ) ;
215+ if ( this . isScaled ) {
216+ if ( this . fontsize < 1 ) {
217+ context . scale ( Math . abs ( contextMatrix . a * this . fontsize ) , Math . abs ( contextMatrix . d * this . fontsize ) ) ;
218+ context . font = Text . buildFont ( this . style , 1 , this . font ) ;
219+ }
220+ else context . scale ( Math . abs ( contextMatrix . a ) , Math . abs ( contextMatrix . d ) ) ;
221+ }
207222 this . write ( writtenText , context ) ;
208223 context . restore ( ) ;
209224 }
0 commit comments