Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Position text relative to baseline #2109

Merged
merged 2 commits into from
Jul 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions src/render/canvas/canvas-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,13 @@ export class CanvasRenderer {
}
}

renderTextWithLetterSpacing(text: TextBounds, letterSpacing: number) {
renderTextWithLetterSpacing(text: TextBounds, letterSpacing: number, baseline: number) {
if (letterSpacing === 0) {
this.ctx.fillText(text.text, text.bounds.left, text.bounds.top + text.bounds.height);
this.ctx.fillText(text.text, text.bounds.left, text.bounds.top + baseline);
} else {
const letters = toCodePoints(text.text).map(i => fromCodePoint(i));
letters.reduce((left, letter) => {
this.ctx.fillText(letter, left, text.bounds.top + text.bounds.height);
this.ctx.fillText(letter, left, text.bounds.top + baseline);

return left + this.ctx.measureText(letter).width;
}, text.bounds.left);
Expand All @@ -173,10 +173,13 @@ export class CanvasRenderer {
const [font, fontFamily, fontSize] = this.createFontStyle(styles);

this.ctx.font = font;
this.ctx.textBaseline = 'alphabetic';

const {baseline, middle} = this.fontMetrics.getMetrics(fontFamily, fontSize);

text.textBounds.forEach(text => {
this.ctx.fillStyle = asString(styles.color);
this.renderTextWithLetterSpacing(text, styles.letterSpacing);
this.renderTextWithLetterSpacing(text, styles.letterSpacing, baseline);
const textShadows: TextShadow = styles.textShadow;

if (textShadows.length && text.text.trim().length) {
Expand Down Expand Up @@ -206,7 +209,6 @@ export class CanvasRenderer {
// Draws a line at the baseline of the font
// TODO As some browsers display the line as more than 1px if the font-size is big,
// need to take that into account both in position and size
const {baseline} = this.fontMetrics.getMetrics(fontFamily, fontSize);
this.ctx.fillRect(
text.bounds.left,
Math.round(text.bounds.top + baseline),
Expand All @@ -220,7 +222,6 @@ export class CanvasRenderer {
break;
case TEXT_DECORATION_LINE.LINE_THROUGH:
// TODO try and find exact position for line-through
const {middle} = this.fontMetrics.getMetrics(fontFamily, fontSize);
this.ctx.fillRect(
text.bounds.left,
Math.ceil(text.bounds.top + middle),
Expand Down Expand Up @@ -363,7 +364,10 @@ export class CanvasRenderer {
}

if (isTextInputElement(container) && container.value.length) {
[this.ctx.font] = this.createFontStyle(styles);
const [fontFamily, fontSize] = this.createFontStyle(styles);
const {baseline} = this.fontMetrics.getMetrics(fontFamily, fontSize);

this.ctx.font = fontFamily;
this.ctx.fillStyle = asString(styles.color);

this.ctx.textBaseline = 'middle';
Expand Down Expand Up @@ -393,7 +397,11 @@ export class CanvasRenderer {
]);

this.ctx.clip();
this.renderTextWithLetterSpacing(new TextBounds(container.value, textBounds), styles.letterSpacing);
this.renderTextWithLetterSpacing(
new TextBounds(container.value, textBounds),
styles.letterSpacing,
baseline
);
this.ctx.restore();
this.ctx.textBaseline = 'bottom';
this.ctx.textAlign = 'left';
Expand All @@ -413,7 +421,9 @@ export class CanvasRenderer {
}
}
} else if (paint.listValue && container.styles.listStyleType !== LIST_STYLE_TYPE.NONE) {
[this.ctx.font] = this.createFontStyle(styles);
const [fontFamily, fontSize] = this.createFontStyle(styles);

this.ctx.font = fontFamily;
this.ctx.fillStyle = asString(styles.color);

this.ctx.textBaseline = 'middle';
Expand All @@ -425,7 +435,13 @@ export class CanvasRenderer {
computeLineHeight(styles.lineHeight, styles.fontSize.number) / 2 + 1
);

this.renderTextWithLetterSpacing(new TextBounds(paint.listValue, bounds), styles.letterSpacing);
const {baseline} = this.fontMetrics.getMetrics(fontFamily, fontSize);

this.renderTextWithLetterSpacing(
new TextBounds(paint.listValue, bounds),
styles.letterSpacing,
baseline
);
this.ctx.textBaseline = 'bottom';
this.ctx.textAlign = 'left';
}
Expand Down