@@ -233,6 +233,8 @@ p5.Renderer.prototype.text = function(str, x, y, maxWidth, maxHeight) {
233233 let chars ;
234234 let shiftedY ;
235235 let finalMaxHeight = Number . MAX_VALUE ;
236+ // fix for #5785 (top of bounding box)
237+ let finalMinHeight = y ;
236238
237239 if ( ! ( this . _doFill || this . _doStroke ) ) {
238240 return ;
@@ -263,29 +265,48 @@ p5.Renderer.prototype.text = function(str, x, y, maxWidth, maxHeight) {
263265 break ;
264266 }
265267
266- let baselineHacked = false ;
267268 if ( typeof maxHeight !== 'undefined' ) {
268269 if ( this . _rectMode === constants . CENTER ) {
269270 y -= maxHeight / 2 ;
270271 }
271272
273+ let originalY = y ;
274+ let ascent = p . textAscent ( ) ;
275+
272276 switch ( this . _textBaseline ) {
273277 case constants . BOTTOM :
274278 shiftedY = y + maxHeight ;
275279 y = Math . max ( shiftedY , y ) ;
280+ // fix for #5785 (top of bounding box)
281+ finalMinHeight += ascent ;
276282 break ;
277283 case constants . CENTER :
278284 shiftedY = y + maxHeight / 2 ;
279285 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 ;
284288 break ;
285289 }
286290
287291 // 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+ }
289310 }
290311
291312 // Render lines of text according to settings of textWrap
@@ -310,10 +331,9 @@ p5.Renderer.prototype.text = function(str, x, y, maxWidth, maxHeight) {
310331 }
311332
312333 let offset = 0 ;
313- const vAlign = p . textAlign ( ) . vertical ;
314- if ( vAlign === constants . CENTER ) {
334+ if ( this . _textBaseline === constants . CENTER ) {
315335 offset = ( nlines . length - 1 ) * p . textLeading ( ) / 2 ;
316- } else if ( vAlign === constants . BOTTOM ) {
336+ } else if ( this . _textBaseline === constants . BOTTOM ) {
317337 offset = ( nlines . length - 1 ) * p . textLeading ( ) ;
318338 }
319339
@@ -324,18 +344,29 @@ p5.Renderer.prototype.text = function(str, x, y, maxWidth, maxHeight) {
324344 testLine = `${ line + words [ wordIndex ] } ` + ' ' ;
325345 testWidth = this . textWidth ( testLine ) ;
326346 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+ ) ;
328355 line = `${ words [ wordIndex ] } ` + ' ' ;
329356 y += p . textLeading ( ) ;
330357 } else {
331358 line = testLine ;
332359 }
333360 }
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+ ) ;
335369 y += p . textLeading ( ) ;
336- if ( baselineHacked ) {
337- this . _textBaseline = constants . BASELINE ;
338- }
339370 }
340371 } else {
341372 let nlines = [ ] ;
@@ -356,10 +387,9 @@ p5.Renderer.prototype.text = function(str, x, y, maxWidth, maxHeight) {
356387
357388 nlines . push ( line ) ;
358389 let offset = 0 ;
359- const vAlign = p . textAlign ( ) . vertical ;
360- if ( vAlign === constants . CENTER ) {
390+ if ( this . _textBaseline === constants . CENTER ) {
361391 offset = ( nlines . length - 1 ) * p . textLeading ( ) / 2 ;
362- } else if ( vAlign === constants . BOTTOM ) {
392+ } else if ( this . _textBaseline === constants . BOTTOM ) {
363393 offset = ( nlines . length - 1 ) * p . textLeading ( ) ;
364394 }
365395
@@ -374,33 +404,49 @@ p5.Renderer.prototype.text = function(str, x, y, maxWidth, maxHeight) {
374404 if ( testWidth <= maxWidth ) {
375405 line += chars [ charIndex ] ;
376406 } 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+ ) ;
378415 y += p . textLeading ( ) ;
379416 line = `${ chars [ charIndex ] } ` ;
380417 }
381418 }
382419 }
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+ ) ;
384428 y += p . textLeading ( ) ;
385-
386- if ( baselineHacked ) {
387- this . _textBaseline = constants . BASELINE ;
388- }
389429 }
390430 } else {
391431 // Offset to account for vertically centering multiple lines of text - no
392432 // need to adjust anything for vertical align top or baseline
393433 let offset = 0 ;
394- const vAlign = p . textAlign ( ) . vertical ;
395- if ( vAlign === constants . CENTER ) {
434+ if ( this . _textBaseline === constants . CENTER ) {
396435 offset = ( lines . length - 1 ) * p . textLeading ( ) / 2 ;
397- } else if ( vAlign === constants . BOTTOM ) {
436+ } else if ( this . _textBaseline === constants . BOTTOM ) {
398437 offset = ( lines . length - 1 ) * p . textLeading ( ) ;
399438 }
400439
401440 // Renders lines of text at any line breaks present in the original string
402441 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+ ) ;
404450 y += p . textLeading ( ) ;
405451 }
406452 }
0 commit comments