Skip to content

Commit 8d148fb

Browse files
authored
Merge pull request #8014 from dhowe/fixTo#7984
Fix to#7984
2 parents 09b7e5e + c5fa1a0 commit 8d148fb

File tree

24 files changed

+70
-22
lines changed

24 files changed

+70
-22
lines changed

src/type/textCore.js

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,23 +1905,15 @@ function textCore(p5, fn) {
19051905
let boxes = lines.map((line, i) => this[type].bind(this)
19061906
(line, x, y + i * textLeading));
19071907

1908-
// adjust the bounding boxes based on horiz. text alignment
1909-
if (lines.length > 1) {
1910-
// When width is not provided (e.g., fontBounds path), fall back to the widest line.
1911-
const maxWidth = boxes.reduce((m, b) => Math.max(m, b.w || 0), 0);
1912-
1913-
boxes.forEach((bb) => {
1914-
const w = (width ?? maxWidth);
1915-
bb.x += p5.Renderer2D.prototype._xAlignOffset.call(this, textAlign, w);
1916-
});
1908+
if (lines.length > 1 && typeof width !== 'undefined') { // fix for #7984
1909+
// adjust the bounding boxes for horizontal text alignment in 2d
1910+
// the WebGL mode version does additional alignment adjustments
1911+
boxes.forEach(bb => bb.x += p5.Renderer2D.prototype._xAlignOffset.call(this, textAlign, width));
19171912
}
19181913

1919-
// adjust the bounding boxes based on vert. text alignment
1920-
if (typeof height !== 'undefined') {
1921-
// Call the 2D mode version: the WebGL mode version does additional
1922-
// alignment adjustments to account for how WebGL renders text.
1923-
p5.Renderer2D.prototype._yAlignOffset.call(this, boxes, height);
1924-
}
1914+
// adjust the bounding boxes for vertical text alignment in 2d
1915+
// the WebGL mode version does additional alignment adjustments
1916+
p5.Renderer2D.prototype._yAlignOffset.call(this, boxes, height || 0); // fix for #7984
19251917

19261918
// get the bounds for the text block
19271919
let bounds = boxes[0];
@@ -1936,12 +1928,12 @@ function textCore(p5, fn) {
19361928
}
19371929
}
19381930

1939-
if (0 && opts?.ignoreRectMode) boxes.forEach((b, i) => { // draw bounds for debugging
1931+
if (0 && opts?.ignoreRectMode) { // draw bounds for debugging
19401932
let ss = context.strokeStyle;
19411933
context.strokeStyle = 'green';
19421934
context.strokeRect(bounds.x, bounds.y, bounds.w, bounds.h);
19431935
context.strokeStyle = ss;
1944-
});
1936+
}
19451937

19461938
context.textBaseline = setBaseline; // restore baseline
19471939

@@ -2633,7 +2625,7 @@ function textCore(p5, fn) {
26332625
}
26342626

26352627
if (p5.RendererGL) {
2636-
p5.RendererGL.prototype.textCanvas = function() {
2628+
p5.RendererGL.prototype.textCanvas = function () {
26372629
if (!this._textCanvas) {
26382630
this._textCanvas = document.createElement('canvas');
26392631
this._textCanvas.width = 1;
@@ -2644,15 +2636,15 @@ function textCore(p5, fn) {
26442636
}
26452637
return this._textCanvas;
26462638
};
2647-
p5.RendererGL.prototype.textDrawingContext = function() {
2639+
p5.RendererGL.prototype.textDrawingContext = function () {
26482640
if (!this._textDrawingContext) {
26492641
const textCanvas = this.textCanvas();
26502642
this._textDrawingContext = textCanvas.getContext('2d');
26512643
}
26522644
return this._textDrawingContext;
26532645
};
26542646
const oldRemove = p5.RendererGL.prototype.remove;
2655-
p5.RendererGL.prototype.remove = function() {
2647+
p5.RendererGL.prototype.remove = function () {
26562648
if (this._textCanvas) {
26572649
this._textCanvas.parentElement.removeChild(this._textCanvas);
26582650
}

test/manual-test-examples/type/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@
546546

547547

548548
let manualLineBreaks = function (p) {
549-
let x, y, s, showBB = 0;
549+
let x, y, s, showBB = false;
550550

551551
p.setup = function () {
552552
p.createCanvas(240 * 5, 400);
@@ -623,7 +623,7 @@
623623

624624
x += 580, s = 'CENTER TOP\nis easy.';
625625
p.fill(0) && p.noStroke() && p.textAlign(p.CENTER, p.TOP) && p.text(s, x, y);
626-
if (showBB) sp.noFill() && p.stroke(0) && p.rect(...Object.values(p.textBounds(s, x, y)));
626+
if (showBB) p.noFill() && p.stroke(0) && p.rect(...Object.values(p.textBounds(s, x, y)));
627627

628628
x += 600, s = 'RIGHT TOP\nis easy.';
629629
p.fill(0) && p.noStroke() && p.textAlign(p.RIGHT, p.TOP) && p.text(s, x, y);

test/unit/visual/cases/typography.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,56 @@ visualSuite('Typography', function () {
403403
});
404404
}
405405
);
406+
407+
visualTest(
408+
'all alignments with multi-line manual text, no box dimensions',
409+
async function (p5, screenshot) {
410+
const alignments = [
411+
{ alignX: p5.LEFT, alignY: p5.TOP },
412+
{ alignX: p5.CENTER, alignY: p5.TOP },
413+
{ alignX: p5.RIGHT, alignY: p5.TOP },
414+
{ alignX: p5.LEFT, alignY: p5.CENTER },
415+
{ alignX: p5.CENTER, alignY: p5.CENTER },
416+
{ alignX: p5.RIGHT, alignY: p5.CENTER },
417+
{ alignX: p5.LEFT, alignY: p5.BOTTOM },
418+
{ alignX: p5.CENTER, alignY: p5.BOTTOM },
419+
{ alignX: p5.RIGHT, alignY: p5.BOTTOM }
420+
];
421+
422+
p5.createCanvas(150, 100, mode === 'webgl' ? p5.WEBGL : undefined);
423+
if (mode === 'webgl') p5.translate(-p5.width/2, -p5.height/2);
424+
p5.textSize(20);
425+
426+
const font = await p5.loadFont(
427+
'/unit/assets/Inconsolata-Bold.ttf'
428+
);
429+
p5.textFont(font);
430+
431+
let xPos = 20;
432+
let yPos = 20;
433+
434+
alignments.forEach((alignment, i) => {
435+
p5.background(255);
436+
p5.push();
437+
p5.textAlign(alignment.alignX, alignment.alignY);
438+
439+
p5.fill(0);
440+
p5.noStroke();
441+
p5.text('Line 1\nLine 2\nLine 3', xPos, yPos);
442+
const bb = p5.textBounds(
443+
'Line 1\nLine 2\nLine 3',
444+
xPos,
445+
yPos
446+
);
447+
p5.noFill();
448+
p5.stroke('red');
449+
p5.rect(bb.x, bb.y, bb.w, bb.h);
450+
p5.pop();
451+
452+
screenshot();
453+
});
454+
}
455+
);
406456
});
407457
}
408458
});
2.64 KB
Loading
2.46 KB
Loading
1.65 KB
Loading
2.54 KB
Loading
2.39 KB
Loading
1.55 KB
Loading
1.95 KB
Loading

0 commit comments

Comments
 (0)