Skip to content

Commit 23ed35b

Browse files
committed
adds tests for #1410
1 parent 1fabe53 commit 23ed35b

File tree

8 files changed

+114
-11
lines changed

8 files changed

+114
-11
lines changed

src/core/p5.Renderer2D.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,7 +1105,7 @@ p5.Renderer2D.prototype.text = function (str, x, y, maxWidth, maxHeight) {
11051105
case constants.BOTTOM:
11061106
y += (maxHeight - totalHeight);
11071107
break;
1108-
case constants._CTX_MIDDLE:
1108+
case constants._CTX_MIDDLE: // CENTER?
11091109
y += (maxHeight - totalHeight) / 2;
11101110
break;
11111111
case constants.BASELINE:
@@ -1139,8 +1139,8 @@ p5.Renderer2D.prototype.text = function (str, x, y, maxWidth, maxHeight) {
11391139
}
11401140
}
11411141
else {
1142-
//offset to account for centering multiple lines of text
1143-
var offset = ((cars.length)*0.5-0.5)*p.textLeading();
1142+
// offset to account for vertically centering multiple lines of text
1143+
var offset = ((cars.length * 0.5) - 0.5) * p.textLeading(); // see #1410
11441144

11451145
for (jj = 0; jj < cars.length; jj++) {
11461146

14.4 KB
Loading
13 KB
Loading
13.7 KB
Loading
13.5 KB
Loading

test/manual-test-examples/p5.Font/custom/index.html

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,16 @@
2626
<img src="textAlignSketch.png" width="240" height="160"></img>
2727
</div>
2828
<div id='textLeadingSketch'>
29-
<img src="textLeadingSketch.png" width="240" height="160"></img>
29+
<img src="LEFT.TOP.lead.png" width="400" height="200"></img>
30+
</div>
31+
<div id='textLeadingSketch2'>
32+
<img src="LEFT.CENTER.lead.png" width="400" height="200"></img>
33+
</div>
34+
<div id='textLeadingSketch3'>
35+
<img src="LEFT.BL.lead.png" width="400" height="200"></img>
36+
</div>
37+
<div id='textLeadingSketch4'>
38+
<img src="LEFT.BOTTOM.lead.png" width="400" height="200"></img>
3039
</div>
3140
<div id='textSizeSketch'>
3241
<img src="textSizeSketch.png" width="240" height="160"></img>

test/manual-test-examples/p5.Font/custom/sketch.js

Lines changed: 101 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -273,27 +273,120 @@ var textAlignSketch = function(p) {
273273
};
274274
};
275275

276+
276277
var textLeadingSketch = function(p) {
277278
var font;
278279
p.preload = function() {
279280
font = p.loadFont("../SourceSansPro-Regular.otf");
280281
};
281282
p.setup = function() {
282-
p.createCanvas(240, 160);
283+
p.createCanvas(400, 200);
284+
p.textFont(font);
285+
p.fill(0);
286+
p.textSize(12);
287+
288+
p.line(0,100,p.width,100);
289+
p.textAlign(p.LEFT, p.TOP);
290+
p.strokeWeight(0);
291+
292+
var s10 = 'LEFT/TOP@10px',
293+
s20 = s10.replace('1','2'),
294+
s30 = s10.replace('1','3');
295+
296+
p.textLeading(10); // Set leading to 10
297+
p.text(s10+'\n'+s10+'\n'+s10, 10, 100);
298+
p.textLeading(20); // Set leading to 20
299+
p.text(s20+'\n'+s20+'\n'+s20, 140, 100);
300+
p.textLeading(30); // Set leading to 30
301+
p.text(s30+'\n'+s30+'\n'+s30, 270, 100);
302+
};
303+
};
304+
305+
var textLeadingSketch2 = function(p) {
306+
var font;
307+
p.preload = function() {
308+
font = p.loadFont("../SourceSansPro-Regular.otf");
309+
};
310+
p.setup = function() {
311+
p.createCanvas(400, 200);
312+
p.textFont(font);
313+
p.fill(0);
314+
p.textSize(12);
315+
316+
p.line(0,100,p.width,100);
317+
p.textAlign(p.LEFT, p.CENTER);
318+
p.strokeWeight(0);
319+
320+
var s10 = 'LEFT/CENTER@10px',
321+
s20 = s10.replace('1','2'),
322+
s30 = s10.replace('1','3');
323+
324+
p.textLeading(10); // Set leading to 10
325+
p.text(s10+'\n'+s10+'\n'+s10, 10, 100);
326+
p.textLeading(20); // Set leading to 20
327+
p.text(s20+'\n'+s20+'\n'+s20, 140, 100);
328+
p.textLeading(30); // Set leading to 30
329+
p.text(s30+'\n'+s30+'\n'+s30, 270, 100);
330+
};
331+
};
332+
333+
var textLeadingSketch3 = function(p) {
334+
var font;
335+
p.preload = function() {
336+
font = p.loadFont("../SourceSansPro-Regular.otf");
337+
};
338+
p.setup = function() {
339+
p.createCanvas(400, 200);
283340
p.textFont(font);
284341
p.fill(0);
342+
p.textSize(12);
343+
344+
p.line(0,100,p.width,100);
345+
p.textAlign(p.LEFT, p.BASELINE);
285346
p.strokeWeight(0);
347+
348+
var s10 = 'LEFT/BASELINE@10px',
349+
s20 = s10.replace('1','2'),
350+
s30 = s10.replace('1','3');
351+
352+
p.textLeading(10); // Set leading to 10
353+
p.text(s10+'\n'+s10+'\n'+s10, 10, 100);
354+
p.textLeading(20); // Set leading to 20
355+
p.text(s20+'\n'+s20+'\n'+s20, 140, 100);
356+
p.textLeading(30); // Set leading to 30
357+
p.text(s30+'\n'+s30+'\n'+s30, 270, 100);
358+
};
359+
};
360+
361+
var textLeadingSketch4 = function(p) {
362+
var font;
363+
p.preload = function() {
364+
font = p.loadFont("../SourceSansPro-Regular.otf");
365+
};
366+
p.setup = function() {
367+
p.createCanvas(400, 200);
368+
p.textFont(font);
369+
p.fill(0);
286370
p.textSize(12);
287-
//leadig
371+
372+
p.line(0,100,p.width,100);
373+
p.textAlign(p.LEFT, p.BOTTOM);
374+
p.strokeWeight(0);
375+
376+
var s10 = 'LEFT/BOTTOM@10px',
377+
s20 = s10.replace('1','2'),
378+
s30 = s10.replace('1','3');
379+
288380
p.textLeading(10); // Set leading to 10
289-
p.text("Leading10px\nLeading10px\nLeading10px", 10, 30);
381+
p.text(s10+'\n'+s10+'\n'+s10, 10, 100);
290382
p.textLeading(20); // Set leading to 20
291-
p.text("Leading20px\nLeading20px\nLeading20px", 90, 30);
383+
p.text(s20+'\n'+s20+'\n'+s20, 140, 100);
292384
p.textLeading(30); // Set leading to 30
293-
p.text("Leading30px\nLeading30px\nLeading30px", 170, 30);
385+
p.text(s30+'\n'+s30+'\n'+s30, 270, 100);
294386
};
295387
};
296388

389+
297390
var textSizeSketch = function(p) {
298391
var font;
299392
p.preload = function() {
@@ -310,8 +403,6 @@ var textSizeSketch = function(p) {
310403
p.text("Font Size 14", 10, 60);
311404
p.textSize(16);
312405
p.text("Font Size 16", 10, 90);
313-
p.textSize(32);
314-
p.text("Font Size 32", 10, 130);
315406
};
316407
};
317408

@@ -603,6 +694,9 @@ new p5(textLineSketch, 'textLineSketch');
603694
new p5(textWrapSketch, 'textWrapSketch');
604695
new p5(textAlignSketch, 'textAlignSketch');
605696
new p5(textLeadingSketch, 'textLeadingSketch');
697+
new p5(textLeadingSketch2, 'textLeadingSketch2');
698+
new p5(textLeadingSketch3, 'textLeadingSketch3');
699+
new p5(textLeadingSketch4, 'textLeadingSketch4');
606700
new p5(textSizeSketch, 'textSizeSketch');
607701
new p5(textBoundsSketch, 'textBoundsSketch');
608702
new p5(textStyleSketch, 'textStyleSketch');
-1.88 KB
Binary file not shown.

0 commit comments

Comments
 (0)