@@ -233,6 +233,8 @@ p5.Renderer.prototype.text = function(str, x, y, maxWidth, maxHeight) {
233
233
let chars ;
234
234
let shiftedY ;
235
235
let finalMaxHeight = Number . MAX_VALUE ;
236
+ // fix for #5785 (top of bounding box)
237
+ let finalMinHeight = y ;
236
238
237
239
if ( ! ( this . _doFill || this . _doStroke ) ) {
238
240
return ;
@@ -263,29 +265,48 @@ p5.Renderer.prototype.text = function(str, x, y, maxWidth, maxHeight) {
263
265
break ;
264
266
}
265
267
266
- let baselineHacked = false ;
267
268
if ( typeof maxHeight !== 'undefined' ) {
268
269
if ( this . _rectMode === constants . CENTER ) {
269
270
y -= maxHeight / 2 ;
270
271
}
271
272
273
+ let originalY = y ;
274
+ let ascent = p . textAscent ( ) ;
275
+
272
276
switch ( this . _textBaseline ) {
273
277
case constants . BOTTOM :
274
278
shiftedY = y + maxHeight ;
275
279
y = Math . max ( shiftedY , y ) ;
280
+ // fix for #5785 (top of bounding box)
281
+ finalMinHeight += ascent ;
276
282
break ;
277
283
case constants . CENTER :
278
284
shiftedY = y + maxHeight / 2 ;
279
285
y = Math . max ( shiftedY , y ) ;
280
- break ;
281
- case constants . BASELINE :
282
- baselineHacked = true ;
283
- this . _textBaseline = constants . TOP ;
286
+ // fix for #5785 (top of bounding box)
287
+ finalMinHeight += ascent / 2 ;
284
288
break ;
285
289
}
286
290
287
291
// remember the max-allowed y-position for any line (fix to #928)
288
- finalMaxHeight = y + maxHeight - p . textAscent ( ) ;
292
+ finalMaxHeight = y + maxHeight - ascent ;
293
+
294
+ // fix for #5785 (bottom of bounding box)
295
+ if ( this . _textBaseline === constants . CENTER ) {
296
+ finalMaxHeight = originalY + maxHeight - ascent / 2 ;
297
+ }
298
+ } else {
299
+ // no text-height specified, show warning for BOTTOM / CENTER
300
+ if ( this . _textBaseline === constants . BOTTOM ) {
301
+ return console . warn (
302
+ 'textAlign(*, BOTTOM) requires x, y, width and height'
303
+ ) ;
304
+ }
305
+ if ( this . _textBaseline === constants . CENTER ) {
306
+ return console . warn (
307
+ 'textAlign(*, CENTER) requires x, y, width and height'
308
+ ) ;
309
+ }
289
310
}
290
311
291
312
// Render lines of text according to settings of textWrap
@@ -310,10 +331,9 @@ p5.Renderer.prototype.text = function(str, x, y, maxWidth, maxHeight) {
310
331
}
311
332
312
333
let offset = 0 ;
313
- const vAlign = p . textAlign ( ) . vertical ;
314
- if ( vAlign === constants . CENTER ) {
334
+ if ( this . _textBaseline === constants . CENTER ) {
315
335
offset = ( nlines . length - 1 ) * p . textLeading ( ) / 2 ;
316
- } else if ( vAlign === constants . BOTTOM ) {
336
+ } else if ( this . _textBaseline === constants . BOTTOM ) {
317
337
offset = ( nlines . length - 1 ) * p . textLeading ( ) ;
318
338
}
319
339
@@ -324,18 +344,29 @@ p5.Renderer.prototype.text = function(str, x, y, maxWidth, maxHeight) {
324
344
testLine = `${ line + words [ wordIndex ] } ` + ' ' ;
325
345
testWidth = this . textWidth ( testLine ) ;
326
346
if ( testWidth > maxWidth && line . length > 0 ) {
327
- this . _renderText ( p , line . trim ( ) , x , y - offset , finalMaxHeight ) ;
347
+ this . _renderText (
348
+ p ,
349
+ line . trim ( ) ,
350
+ x ,
351
+ y - offset ,
352
+ finalMaxHeight ,
353
+ finalMinHeight
354
+ ) ;
328
355
line = `${ words [ wordIndex ] } ` + ' ' ;
329
356
y += p . textLeading ( ) ;
330
357
} else {
331
358
line = testLine ;
332
359
}
333
360
}
334
- this . _renderText ( p , line . trim ( ) , x , y - offset , finalMaxHeight ) ;
361
+ this . _renderText (
362
+ p ,
363
+ line . trim ( ) ,
364
+ x ,
365
+ y - offset ,
366
+ finalMaxHeight ,
367
+ finalMinHeight
368
+ ) ;
335
369
y += p . textLeading ( ) ;
336
- if ( baselineHacked ) {
337
- this . _textBaseline = constants . BASELINE ;
338
- }
339
370
}
340
371
} else {
341
372
let nlines = [ ] ;
@@ -356,10 +387,9 @@ p5.Renderer.prototype.text = function(str, x, y, maxWidth, maxHeight) {
356
387
357
388
nlines . push ( line ) ;
358
389
let offset = 0 ;
359
- const vAlign = p . textAlign ( ) . vertical ;
360
- if ( vAlign === constants . CENTER ) {
390
+ if ( this . _textBaseline === constants . CENTER ) {
361
391
offset = ( nlines . length - 1 ) * p . textLeading ( ) / 2 ;
362
- } else if ( vAlign === constants . BOTTOM ) {
392
+ } else if ( this . _textBaseline === constants . BOTTOM ) {
363
393
offset = ( nlines . length - 1 ) * p . textLeading ( ) ;
364
394
}
365
395
@@ -374,33 +404,49 @@ p5.Renderer.prototype.text = function(str, x, y, maxWidth, maxHeight) {
374
404
if ( testWidth <= maxWidth ) {
375
405
line += chars [ charIndex ] ;
376
406
} else if ( testWidth > maxWidth && line . length > 0 ) {
377
- this . _renderText ( p , line . trim ( ) , x , y - offset , finalMaxHeight ) ;
407
+ this . _renderText (
408
+ p ,
409
+ line . trim ( ) ,
410
+ x ,
411
+ y - offset ,
412
+ finalMaxHeight ,
413
+ finalMinHeight
414
+ ) ;
378
415
y += p . textLeading ( ) ;
379
416
line = `${ chars [ charIndex ] } ` ;
380
417
}
381
418
}
382
419
}
383
- this . _renderText ( p , line . trim ( ) , x , y - offset , finalMaxHeight ) ;
420
+ this . _renderText (
421
+ p ,
422
+ line . trim ( ) ,
423
+ x ,
424
+ y - offset ,
425
+ finalMaxHeight ,
426
+ finalMinHeight
427
+ ) ;
384
428
y += p . textLeading ( ) ;
385
-
386
- if ( baselineHacked ) {
387
- this . _textBaseline = constants . BASELINE ;
388
- }
389
429
}
390
430
} else {
391
431
// Offset to account for vertically centering multiple lines of text - no
392
432
// need to adjust anything for vertical align top or baseline
393
433
let offset = 0 ;
394
- const vAlign = p . textAlign ( ) . vertical ;
395
- if ( vAlign === constants . CENTER ) {
434
+ if ( this . _textBaseline === constants . CENTER ) {
396
435
offset = ( lines . length - 1 ) * p . textLeading ( ) / 2 ;
397
- } else if ( vAlign === constants . BOTTOM ) {
436
+ } else if ( this . _textBaseline === constants . BOTTOM ) {
398
437
offset = ( lines . length - 1 ) * p . textLeading ( ) ;
399
438
}
400
439
401
440
// Renders lines of text at any line breaks present in the original string
402
441
for ( let i = 0 ; i < lines . length ; i ++ ) {
403
- this . _renderText ( p , lines [ i ] , x , y - offset , finalMaxHeight ) ;
442
+ this . _renderText (
443
+ p ,
444
+ lines [ i ] ,
445
+ x ,
446
+ y - offset ,
447
+ finalMaxHeight ,
448
+ finalMinHeight
449
+ ) ;
404
450
y += p . textLeading ( ) ;
405
451
}
406
452
}
0 commit comments