Skip to content
Merged
Changes from 5 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
19 changes: 17 additions & 2 deletions src/type/textCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -2518,6 +2518,21 @@ function textCore(p5, fn) {
return this._pInst;
};

Renderer.prototype._middleAlignOffset = function() {
const { textFont, textSize } = this.states;
const font = textFont?.font;
const ctx = this.textDrawingContext();
const metrics = ctx.measureText('X');
let sCapHeight = (font?.data || {})['OS/2']?.sCapHeight;
if (sCapHeight) {
const unitsPerEm = font.data.head.unitsPerEm;
sCapHeight *= textSize / unitsPerEm;
} else {
sCapHeight = metrics.fontBoundingBoxAscent;
}
return metrics.alphabeticBaseline + sCapHeight / 2;
};

if (p5.Renderer2D) {
p5.Renderer2D.prototype.textCanvas = function () {
return this.canvas;
Expand Down Expand Up @@ -2607,7 +2622,7 @@ function textCore(p5, fn) {
case fn.BASELINE:
break;
case textCoreConstants._CTX_MIDDLE:
yOff = ydiff / 2;
yOff = ydiff / 2 + this._middleAlignOffset();
break;
case fn.BOTTOM:
yOff = ydiff;
Expand Down Expand Up @@ -2712,7 +2727,7 @@ function textCore(p5, fn) {
case fn.BASELINE:
break;
case textCoreConstants._CTX_MIDDLE:
yOff = (-totalHeight + textSize + (height || 0)) / 2 + this._verticalAlignFont();
yOff = (-totalHeight + textSize + (height || 0)) / 2 + this._verticalAlignFont() + this._middleAlignOffset();
break;
case fn.BOTTOM:
yOff = -(totalHeight - textSize) + (height || 0);
Expand Down
Loading