Skip to content

Commit

Permalink
fix: correctly align text vertically (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
justusmattern27 authored Sep 23, 2024
1 parent 59bc0d0 commit 8d7a728
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions packages/2d/src/lib/components/TxtLeaf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ export class TxtLeaf extends Shape {
await document.fonts?.ready;
this.applyText(context);
context.font = this.styles.font;
context.textBaseline = 'bottom';
if ('letterSpacing' in context) {
context.letterSpacing = `${this.letterSpacing()}px`;
}
const fontOffset = context.measureText('').fontBoundingBoxAscent;

const parentRect = this.element.getBoundingClientRect();
const {width, height} = this.size();
Expand All @@ -84,7 +86,7 @@ export class TxtLeaf extends Shape {
const rangeRect = range.getBoundingClientRect();

const x = width / -2 + rangeRect.left - parentRect.left;
const y = height / -2 + rangeRect.top - parentRect.top;
const y = height / -2 + rangeRect.top - parentRect.top + fontOffset;

if (lineRect.y === y) {
lineRect.width += rangeRect.width;
Expand All @@ -107,9 +109,7 @@ export class TxtLeaf extends Shape {
text: string,
box: BBox,
) {
const y = box.y + box.height / 2;
context.save();
context.textBaseline = 'middle';
const y = box.y;
text = text.replace(/\s+/g, ' ');
if (this.lineWidth() <= 0) {
context.fillText(text, box.x, y);
Expand All @@ -120,7 +120,6 @@ export class TxtLeaf extends Shape {
context.fillText(text, box.x, y);
context.strokeText(text, box.x, y);
}
context.restore();
}

protected override getCacheBBox(): BBox {
Expand Down

0 comments on commit 8d7a728

Please sign in to comment.