@@ -1217,7 +1217,41 @@ class Renderer2D extends p5.Renderer {
12171217 p . push ( ) ; // fix to #803
12181218
12191219 if ( ! this . _isOpenType ( ) ) {
1220- // a system/browser font
1220+ if (
1221+ this . _textAlign === constants . JUSTIFIED &&
1222+ this . _justifyActive &&
1223+ ! this . _justifyIsLastLine &&
1224+ this . _justifyWidth > 0
1225+ ) {
1226+ const words = line . split ( / \s + / ) . filter ( s => s . length > 0 ) ;
1227+ if ( words . length > 1 ) {
1228+ const widths = words . map ( s => this . textWidth ( s ) ) ;
1229+ const sum = widths . reduce ( ( a , b ) => a + b , 0 ) ;
1230+ const extra = this . _justifyWidth - sum ;
1231+ if ( extra > 0 ) {
1232+ const gap = extra / ( words . length - 1 ) ;
1233+ let cx = x ;
1234+ if ( this . _doStroke && this . _strokeSet ) {
1235+ for ( let i = 0 ; i < words . length ; i ++ ) {
1236+ this . drawingContext . strokeText ( words [ i ] , cx , y ) ;
1237+ cx += widths [ i ] + ( i < words . length - 1 ? gap : 0 ) ;
1238+ }
1239+ }
1240+ cx = x ;
1241+ if ( ! this . _clipping && this . _doFill ) {
1242+ if ( ! this . _fillSet ) {
1243+ this . _setFill ( constants . _DEFAULT_TEXT_FILL ) ;
1244+ }
1245+ for ( let i = 0 ; i < words . length ; i ++ ) {
1246+ this . drawingContext . fillText ( words [ i ] , cx , y ) ;
1247+ cx += widths [ i ] + ( i < words . length - 1 ? gap : 0 ) ;
1248+ }
1249+ }
1250+ p . pop ( ) ;
1251+ return p ;
1252+ }
1253+ }
1254+ }
12211255
12221256 // no stroke unless specified by user
12231257 if ( this . _doStroke && this . _strokeSet ) {
@@ -1272,7 +1306,10 @@ class Renderer2D extends p5.Renderer {
12721306 this . drawingContext . font = `${ this . _textStyle || 'normal' } ${ this . _textSize ||
12731307 12 } px ${ fontNameString } `;
12741308
1275- this . drawingContext . textAlign = this . _textAlign ;
1309+ const _ta = this . _textAlign === constants . JUSTIFIED
1310+ ? constants . LEFT
1311+ : this . _textAlign ;
1312+ this . drawingContext . textAlign = _ta ;
12761313 if ( this . _textBaseline === constants . CENTER ) {
12771314 this . drawingContext . textBaseline = constants . _CTX_MIDDLE ;
12781315 } else {
0 commit comments