Skip to content

Commit 522a2ca

Browse files
committed
Update centering code
1 parent adc1064 commit 522a2ca

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/type/textCore.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2519,14 +2519,18 @@ function textCore(p5, fn) {
25192519
};
25202520

25212521
Renderer.prototype._middleAlignOffset = function() {
2522+
const { textFont, textSize } = this.states;
2523+
const font = textFont?.font;
25222524
const ctx = this.textDrawingContext();
25232525
const metrics = ctx.measureText('X');
2524-
return (
2525-
(metrics.fontBoundingBoxDescent +
2526-
(metrics.alphabeticBaseline || -metrics.fontBoundingBoxAscent / 2)) /
2527-
2 -
2528-
(metrics.fontBoundingBoxAscent - metrics.fontBoundingBoxDescent) / 2
2529-
);
2526+
let sCapHeight = (font?.data || {})['OS/2']?.sCapHeight;
2527+
if (sCapHeight) {
2528+
const unitsPerEm = font.data.head.unitsPerEm;
2529+
sCapHeight *= textSize / unitsPerEm;
2530+
} else {
2531+
sCapHeight = metrics.fontBoundingBoxAscent;
2532+
}
2533+
return metrics.alphabeticBaseline + sCapHeight / 2;
25302534
};
25312535

25322536
if (p5.Renderer2D) {

0 commit comments

Comments
 (0)