@@ -289,27 +289,87 @@ p5.Renderer.prototype.text = function(str, x, y, maxWidth, maxHeight) {
289
289
// Render lines of text according to settings of textWrap
290
290
// Splits lines at spaces, for loop adds one word + space at a time and tests length with next word added
291
291
if ( textWrapStyle === constants . WORD ) {
292
+ // ADDED-WORD /////////////////////////////////////////////////////////
293
+ let nlines = [ ] ;
292
294
for ( let lineIndex = 0 ; lineIndex < lines . length ; lineIndex ++ ) {
293
295
line = '' ;
294
296
words = lines [ lineIndex ] . split ( ' ' ) ;
295
297
for ( let wordIndex = 0 ; wordIndex < words . length ; wordIndex ++ ) {
296
298
testLine = `${ line + words [ wordIndex ] } ` + ' ' ;
297
299
testWidth = this . textWidth ( testLine ) ;
298
300
if ( testWidth > maxWidth && line . length > 0 ) {
299
- this . _renderText ( p , line , x , y , finalMaxHeight ) ;
301
+ //this._renderText(p, line, x, y, finalMaxHeight);
302
+ nlines . push ( line ) ;
303
+ line = `${ words [ wordIndex ] } ` + ' ' ;
304
+ //y += p.textLeading();
305
+ } else {
306
+ line = testLine ;
307
+ }
308
+ }
309
+ //this._renderText(p, line, x, y, finalMaxHeight);
310
+ nlines . push ( line ) ;
311
+ //y += p.textLeading();
312
+ }
313
+ //console.log('WORD: ', nlines);
314
+ let offset = 0 ;
315
+
316
+ const vAlign = p . textAlign ( ) . vertical ;
317
+ if ( vAlign === constants . CENTER ) {
318
+ offset = ( nlines . length - 1 ) * p . textLeading ( ) / 2 ;
319
+ } else if ( vAlign === constants . BOTTOM ) {
320
+ offset = ( nlines . length - 1 ) * p . textLeading ( ) ;
321
+ }
322
+ ////////////////////////////////////////////////////////////////////////
323
+
324
+ for ( let lineIndex = 0 ; lineIndex < lines . length ; lineIndex ++ ) {
325
+ line = '' ;
326
+ words = lines [ lineIndex ] . split ( ' ' ) ;
327
+ for ( let wordIndex = 0 ; wordIndex < words . length ; wordIndex ++ ) {
328
+ testLine = `${ line + words [ wordIndex ] } ` + ' ' ;
329
+ testWidth = this . textWidth ( testLine ) ;
330
+ if ( testWidth > maxWidth && line . length > 0 ) {
331
+ this . _renderText ( p , line . trim ( ) , x , y - offset , finalMaxHeight ) ;
300
332
line = `${ words [ wordIndex ] } ` + ' ' ;
301
333
y += p . textLeading ( ) ;
302
334
} else {
303
335
line = testLine ;
304
336
}
305
337
}
306
- this . _renderText ( p , line , x , y , finalMaxHeight ) ;
338
+ this . _renderText ( p , line . trim ( ) , x , y - offset , finalMaxHeight ) ;
307
339
y += p . textLeading ( ) ;
308
340
if ( baselineHacked ) {
309
341
this . _textBaseline = constants . BASELINE ;
310
342
}
311
343
}
312
344
} else {
345
+ // ADDED-CHAR /////////////////////////////////////////////////////////
346
+ let nlines = [ ] ;
347
+ for ( let lineIndex = 0 ; lineIndex < lines . length ; lineIndex ++ ) {
348
+ line = '' ;
349
+ chars = lines [ lineIndex ] . split ( '' ) ;
350
+ for ( let charIndex = 0 ; charIndex < chars . length ; charIndex ++ ) {
351
+ testLine = `${ line + chars [ charIndex ] } ` ;
352
+ testWidth = this . textWidth ( testLine ) ;
353
+ if ( testWidth <= maxWidth ) {
354
+ line += chars [ charIndex ] ;
355
+ } else if ( testWidth > maxWidth && line . length > 0 ) {
356
+ nlines . push ( line ) ;
357
+ line = `${ chars [ charIndex ] } ` ;
358
+ }
359
+ }
360
+ }
361
+ nlines . push ( line ) ;
362
+ console . log ( 'CHAR: ' , nlines ) ;
363
+ let offset = 0 ;
364
+
365
+ const vAlign = p . textAlign ( ) . vertical ;
366
+ if ( vAlign === constants . CENTER ) {
367
+ offset = ( nlines . length - 1 ) * p . textLeading ( ) / 2 ;
368
+ } else if ( vAlign === constants . BOTTOM ) {
369
+ offset = ( nlines . length - 1 ) * p . textLeading ( ) ;
370
+ }
371
+ ////////////////////////////////////////////////////////////////////////
372
+
313
373
// Splits lines at characters, for loop adds one char at a time and tests length with next char added
314
374
for ( let lineIndex = 0 ; lineIndex < lines . length ; lineIndex ++ ) {
315
375
line = '' ;
@@ -320,13 +380,13 @@ p5.Renderer.prototype.text = function(str, x, y, maxWidth, maxHeight) {
320
380
if ( testWidth <= maxWidth ) {
321
381
line += chars [ charIndex ] ;
322
382
} else if ( testWidth > maxWidth && line . length > 0 ) {
323
- this . _renderText ( p , line , x , y , finalMaxHeight ) ;
383
+ this . _renderText ( p , line . trim ( ) , x , y - offset , finalMaxHeight ) ;
324
384
y += p . textLeading ( ) ;
325
385
line = `${ chars [ charIndex ] } ` ;
326
386
}
327
387
}
328
388
}
329
- this . _renderText ( p , line , x , y , finalMaxHeight ) ;
389
+ this . _renderText ( p , line . trim ( ) , x , y - offset , finalMaxHeight ) ;
330
390
y += p . textLeading ( ) ;
331
391
332
392
if ( baselineHacked ) {
0 commit comments