Skip to content

Commit f73031d

Browse files
authored
Merge pull request #383 from Dessia-tech/fix/text_patch
fix(text): hotfix on small scaled text position
2 parents 016b062 + 5da3341 commit f73031d

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [0.22.6]
9+
### Fix
10+
- Fix bug on small scaled text position
811
## [0.22.5]
912
### Fix
1013
- Fix global bug on RemoteFigure.resize methods, fixing a browser crasher bug

src/shapes.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)