diff --git a/src/core/p5.Renderer2D.js b/src/core/p5.Renderer2D.js index aca53a7ff1..7e2fd180aa 100644 --- a/src/core/p5.Renderer2D.js +++ b/src/core/p5.Renderer2D.js @@ -953,6 +953,9 @@ class Renderer2D extends Renderer { break; case textCoreConstants.END: throw new Error('textBounds: END not yet supported for textAlign'); + default: + adjustedX = x; + break; } lineData[i] = { text: lines[i], x: adjustedX, y: y + i * textLeading }; } diff --git a/src/type/textCore.js b/src/type/textCore.js index a72854c2c6..1241f668e3 100644 --- a/src/type/textCore.js +++ b/src/type/textCore.js @@ -1500,6 +1500,14 @@ function textCore(p5, fn) { // horizontal value as separate argument if (typeof h === 'string' || h instanceof String) { + // unknown values fall back to LEFT, so both renderers stay in sync + const validHorizontal = [ + fn.LEFT, fn.CENTER, fn.RIGHT, + textCoreConstants.START, textCoreConstants.END + ]; + if (!validHorizontal.includes(h)) { + h = fn.LEFT; + } this.states.setValue('textAlign', h); } @@ -1508,6 +1516,15 @@ function textCore(p5, fn) { if (v === fn.CENTER) { v = textCoreConstants._CTX_MIDDLE; } + // unknown values fall back to BASELINE, so both renderers stay in sync + const validVertical = [ + fn.TOP, fn.BOTTOM, fn.BASELINE, + textCoreConstants._CTX_MIDDLE, + textCoreConstants.HANGING, textCoreConstants.IDEOGRAPHIC + ]; + if (!validVertical.includes(v)) { + v = fn.BASELINE; + } this.states.setValue('textBaseline', v); } diff --git a/test/unit/type/attributes.js b/test/unit/type/attributes.js index 0315deac40..2b098ac961 100644 --- a/test/unit/type/attributes.js +++ b/test/unit/type/attributes.js @@ -25,6 +25,26 @@ suite('Typography Attributes', function () { }); }); + suite('textAlign', function () { + test('keeps valid horizontal and vertical values', function () { + myp5.textAlign(myp5.RIGHT, myp5.BOTTOM); + assert.strictEqual(myp5.textAlign().horizontal, myp5.RIGHT); + assert.strictEqual(myp5.textAlign().vertical, myp5.BOTTOM); + }); + test('falls back to LEFT for an unknown horizontal value', function () { + myp5.textAlign(myp5.RIGHT); + myp5.textAlign(myp5.TOP); + assert.strictEqual(myp5.textAlign().horizontal, myp5.LEFT); + assert.strictEqual(myp5.drawingContext.textAlign, myp5.LEFT); + }); + test('falls back to BASELINE for an unknown vertical value', function () { + myp5.textAlign(myp5.LEFT, myp5.TOP); + myp5.textAlign(myp5.LEFT, myp5.LEFT); + assert.strictEqual(myp5.textAlign().vertical, myp5.BASELINE); + assert.strictEqual(myp5.drawingContext.textBaseline, myp5.BASELINE); + }); + }); + suite('textSize', function () { test('sets and gets the text size', function () { myp5.textSize(24);