Skip to content

Commit

Permalink
Merge pull request #383 from Dessia-tech/fix/text_patch
Browse files Browse the repository at this point in the history
fix(text): hotfix on small scaled text position
  • Loading branch information
Tanguylo authored May 16, 2024
2 parents 016b062 + 5da3341 commit f73031d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.22.6]
### Fix
- Fix bug on small scaled text position
## [0.22.5]
### Fix
- Fix global bug on RemoteFigure.resize methods, fixing a browser crasher bug
Expand Down
21 changes: 18 additions & 3 deletions src/shapes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,21 @@ export class Text extends Shape {
]
}

private computeFontSize(text: string, defaultFontSize: number, context: CanvasRenderingContext2D): number {
let defaultTextWidth = 0;
if (defaultFontSize < 1) {
context.font = Text.buildFont(this.style, 1, this.font);
defaultTextWidth = context.measureText(text).width * defaultFontSize;
} else defaultTextWidth = context.measureText(text).width;
if (defaultTextWidth >= this.boundingBox.size.x) return defaultFontSize * this.boundingBox.size.x / defaultTextWidth;
return defaultFontSize
}

private automaticFontSize(context: CanvasRenderingContext2D): number {
let fontsize = Math.min(this.boundingBox.size.y ?? Number.POSITIVE_INFINITY, this.fontsize ?? Number.POSITIVE_INFINITY);
if (fontsize == Number.POSITIVE_INFINITY) fontsize = DEFAULT_FONTSIZE;
context.font = Text.buildFont(this.style, fontsize, this.font);
if (context.measureText(this.text).width >= this.boundingBox.size.x) fontsize = fontsize * this.boundingBox.size.x / context.measureText(this.text).width;
return fontsize
return this.computeFontSize(this.text, fontsize, context)
}

private setRectOffsetX(): number {
Expand Down Expand Up @@ -203,7 +212,13 @@ export class Text extends Shape {
context.resetTransform();
context.translate(origin.x, origin.y);
context.rotate(Math.PI / 180 * this.orientation);
if (this.isScaled) context.scale(Math.abs(contextMatrix.a), Math.abs(contextMatrix.d));
if (this.isScaled) {
if (this.fontsize < 1) {
context.scale(Math.abs(contextMatrix.a * this.fontsize), Math.abs(contextMatrix.d * this.fontsize));
context.font = Text.buildFont(this.style, 1, this.font);
}
else context.scale(Math.abs(contextMatrix.a), Math.abs(contextMatrix.d));
}
this.write(writtenText, context);
context.restore();
}
Expand Down

0 comments on commit f73031d

Please sign in to comment.