Skip to content

Commit 92da914

Browse files
authored
Merge pull request #7481 from processing/fix/webgl-text-align
Fix WebGL text alignment + add tests
2 parents 1db55f6 + 7ad57cf commit 92da914

File tree

148 files changed

+281
-232
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

148 files changed

+281
-232
lines changed

src/type/text2d.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -518,12 +518,16 @@ function text2d(p5, fn) {
518518

519519
// adjust the bounding boxes based on horiz. text alignment
520520
if (lines.length > 1) {
521-
boxes.forEach(bb => bb.x += this._xAlignOffset(textAlign, width));
521+
// Call the 2D mode version: the WebGL mode version does additional
522+
// alignment adjustments to account for how WebGL renders text.
523+
boxes.forEach(bb => bb.x += p5.Renderer2D.prototype._xAlignOffset.call(this, textAlign, width));
522524
}
523525

524526
// adjust the bounding boxes based on vert. text alignment
525527
if (typeof height !== 'undefined') {
526-
this._yAlignOffset(boxes, height);
528+
// Call the 2D mode version: the WebGL mode version does additional
529+
// alignment adjustments to account for how WebGL renders text.
530+
p5.Renderer2D.prototype._yAlignOffset.call(this, boxes, height);
527531
}
528532

529533
// get the bounds for the text block
@@ -1224,11 +1228,11 @@ function text2d(p5, fn) {
12241228
case fn.LEFT:
12251229
adjustedX = x;
12261230
break;
1227-
case fn._CTX_MIDDLE:
1228-
adjustedX = x + (adjustedW - widths[i]) / 2;
1231+
case fn.CENTER:
1232+
adjustedX = x + (adjustedW - widths[i]) / 2 - adjustedW / 2 + (width || 0) / 2;
12291233
break;
12301234
case fn.RIGHT:
1231-
adjustedX = x + adjustedW - widths[i];
1235+
adjustedX = x + adjustedW - widths[i] - adjustedW + (width || 0);
12321236
break;
12331237
case fn.END:
12341238
throw new Error('textBounds: END not yet supported for textAlign');
@@ -1255,10 +1259,10 @@ function text2d(p5, fn) {
12551259
case fn.BASELINE:
12561260
break;
12571261
case fn._CTX_MIDDLE:
1258-
yOff = -totalHeight / 2 + textSize;
1262+
yOff = -totalHeight / 2 + textSize + (height || 0) / 2;
12591263
break;
12601264
case fn.BOTTOM:
1261-
yOff = -(totalHeight - textSize);
1265+
yOff = -(totalHeight - textSize) + (height || 0);
12621266
break;
12631267
default:
12641268
console.warn(`${textBaseline} is not supported in WebGL mode.`); // FES?

src/webgl/text.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ function text(p5, fn){
665665
console.log(
666666
'WEBGL: only Opentype (.otf) and Truetype (.ttf) fonts are supported'
667667
);
668-
return p;
668+
return;
669669
}
670670

671671
this.push(); // fix to #803

0 commit comments

Comments
 (0)