@@ -141,12 +141,21 @@ export class Text extends Shape {
141
141
]
142
142
}
143
143
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
+
144
154
private automaticFontSize ( context : CanvasRenderingContext2D ) : number {
145
155
let fontsize = Math . min ( this . boundingBox . size . y ?? Number . POSITIVE_INFINITY , this . fontsize ?? Number . POSITIVE_INFINITY ) ;
146
156
if ( fontsize == Number . POSITIVE_INFINITY ) fontsize = DEFAULT_FONTSIZE ;
147
157
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 )
150
159
}
151
160
152
161
private setRectOffsetX ( ) : number {
@@ -203,7 +212,13 @@ export class Text extends Shape {
203
212
context . resetTransform ( ) ;
204
213
context . translate ( origin . x , origin . y ) ;
205
214
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
+ }
207
222
this . write ( writtenText , context ) ;
208
223
context . restore ( ) ;
209
224
}
0 commit comments