Skip to content

Commit d287796

Browse files
authored
Merge pull request #8320 from processing/fix/font-align
Better visual centering of text
2 parents 43fd2c0 + 52c4266 commit d287796

File tree

18 files changed

+71
-2
lines changed

18 files changed

+71
-2
lines changed

src/type/textCore.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2518,6 +2518,21 @@ function textCore(p5, fn) {
25182518
return this._pInst;
25192519
};
25202520

2521+
Renderer.prototype._middleAlignOffset = function() {
2522+
const { textFont, textSize } = this.states;
2523+
const font = textFont?.font;
2524+
const ctx = this.textDrawingContext();
2525+
const metrics = ctx.measureText('X');
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;
2534+
};
2535+
25212536
if (p5.Renderer2D) {
25222537
p5.Renderer2D.prototype.textCanvas = function () {
25232538
return this.canvas;
@@ -2607,7 +2622,7 @@ function textCore(p5, fn) {
26072622
case fn.BASELINE:
26082623
break;
26092624
case textCoreConstants._CTX_MIDDLE:
2610-
yOff = ydiff / 2;
2625+
yOff = ydiff / 2 + this._middleAlignOffset();
26112626
break;
26122627
case fn.BOTTOM:
26132628
yOff = ydiff;
@@ -2712,7 +2727,7 @@ function textCore(p5, fn) {
27122727
case fn.BASELINE:
27132728
break;
27142729
case textCoreConstants._CTX_MIDDLE:
2715-
yOff = (-totalHeight + textSize + (height || 0)) / 2 + this._verticalAlignFont();
2730+
yOff = (-totalHeight + textSize + (height || 0)) / 2 + this._verticalAlignFont() + this._middleAlignOffset();
27162731
break;
27172732
case fn.BOTTOM:
27182733
yOff = -(totalHeight - textSize) + (height || 0);

test/unit/visual/cases/typography.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,36 @@ visualSuite('Typography', function () {
145145
});
146146
});
147147

148+
visualSuite('vertical centering', function() {
149+
const fonts = {
150+
Inter: 'https://fonts.gstatic.com/s/inter/v20/UcCO3FwrK3iLTeHuS_nVMrMxCp50SjIw2boKoduKmMEVuLyfMZhrib2Bg-4.ttf',
151+
Raleway: 'https://fonts.gstatic.com/s/raleway/v36/1Ptxg8zYS_SKggPN4iEgvnHyvveLxVvaooCPNLA3JC9c.ttf',
152+
'Inknut Antiqua': 'https://fonts.gstatic.com/s/inknutantiqua/v16/Y4GRYax7VC4ot_qNB4nYpBdaKU2_xbj5bBoIYJNf.ttf',
153+
Oswald: 'https://fonts.gstatic.com/s/oswald/v57/TK3_WkUHHAIjg75cFRf3bXL8LICs1_FvgUFoZAaRliE.ttf',
154+
};
155+
for (const mode of ['2d', 'webgl']) {
156+
for (const fontName in fonts) {
157+
visualTest(`${fontName} in ${mode} mode`, async function (p5, screenshot) {
158+
p5.createCanvas(200, 50, mode === 'webgl' ? p5.WEBGL : p5.P2D);
159+
p5.background(255);
160+
const font = await p5.loadFont(fonts[fontName]);
161+
if (mode === '2d') p5.translate(p5.width/2, p5.height/2);
162+
p5.textFont(font);
163+
164+
p5.stroke('red');
165+
p5.line(-p5.width/2, 0, p5.width/2, 0);
166+
167+
p5.noStroke();
168+
p5.fill(0);
169+
p5.textSize(20);
170+
p5.textAlign(p5.CENTER, p5.CENTER);
171+
p5.text('Vertical align', 0, 0);
172+
screenshot();
173+
});
174+
}
175+
}
176+
});
177+
148178
visualSuite('textAlign', function () {
149179
for (const mode of ['2d', 'webgl']) {
150180
visualSuite(`${mode} mode`, () => {
3.85 KB
Loading
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"numScreenshots": 1
3+
}
3.67 KB
Loading
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"numScreenshots": 1
3+
}
3.21 KB
Loading
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"numScreenshots": 1
3+
}
3 KB
Loading
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"numScreenshots": 1
3+
}

0 commit comments

Comments
 (0)