Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions src/core/p5.Renderer2D.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
Expand Down
17 changes: 17 additions & 0 deletions src/type/textCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down
20 changes: 20 additions & 0 deletions test/unit/type/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading