@@ -217,6 +217,11 @@ var calculateX = function(formObject, text) {
217
217
: text ;
218
218
// split into array of words
219
219
var textSplit = text . split ( " " ) ;
220
+ if ( formObject . multiline ) {
221
+ textSplit = textSplit . map ( word => word . split ( "\n" ) ) ;
222
+ } else {
223
+ textSplit = textSplit . map ( word => [ word ] ) ;
224
+ }
220
225
221
226
var fontSize = maxFontSize ; // The Starting fontSize (The Maximum)
222
227
var lineSpacing = 2 ;
@@ -229,7 +234,7 @@ var calculateX = function(formObject, text) {
229
234
230
235
var isSmallerThanWidth = function ( i , lastLine , fontSize ) {
231
236
if ( i + 1 < textSplit . length ) {
232
- var tmp = lastLine + " " + textSplit [ i + 1 ] ;
237
+ var tmp = lastLine + " " + textSplit [ i + 1 ] [ 0 ] ;
233
238
var TextWidth = calculateFontSpace ( tmp , formObject , fontSize ) . width ;
234
239
var FieldWidth = width - 2 * borderPadding ;
235
240
return TextWidth <= FieldWidth ;
@@ -253,6 +258,7 @@ var calculateX = function(formObject, text) {
253
258
var firstWordInLine = 0 ,
254
259
lastWordInLine = 0 ;
255
260
var lastLength ;
261
+ var currWord = 0 ;
256
262
257
263
if ( fontSize <= 0 ) {
258
264
// In case, the Text doesn't fit at all
@@ -269,51 +275,81 @@ var calculateX = function(formObject, text) {
269
275
270
276
var lastLine = "" ;
271
277
var lineCount = 0 ;
272
- Line: for ( var i in textSplit ) {
278
+ Line: for ( var i = 0 ; i < textSplit . length ; i ++ ) {
273
279
if ( textSplit . hasOwnProperty ( i ) ) {
274
- lastLine += textSplit [ i ] + " " ;
275
- // Remove last blank
276
- lastLine =
277
- lastLine . substr ( lastLine . length - 1 ) == " "
278
- ? lastLine . substr ( 0 , lastLine . length - 1 )
279
- : lastLine ;
280
- var key = parseInt ( i ) ;
281
- var nextLineIsSmaller = isSmallerThanWidth ( key , lastLine , fontSize ) ;
282
- var isLastWord = i >= textSplit . length - 1 ;
283
- if ( nextLineIsSmaller && ! isLastWord ) {
284
- lastLine += " " ;
285
- continue ; // Line
286
- } else if ( ! nextLineIsSmaller && ! isLastWord ) {
287
- if ( ! formObject . multiline ) {
280
+ let isWithNewLine = false ;
281
+ if ( textSplit [ i ] . length !== 1 && currWord !== textSplit [ i ] . length - 1 ) {
282
+ if (
283
+ ( textHeight + lineSpacing ) * ( lineCount + 2 ) + lineSpacing >
284
+ height
285
+ ) {
288
286
continue FontSize;
287
+ }
288
+
289
+ lastLine += textSplit [ i ] [ currWord ] ;
290
+ isWithNewLine = true ;
291
+ lastWordInLine = i ;
292
+ i -- ;
293
+ } else {
294
+ lastLine += textSplit [ i ] [ currWord ] + " " ;
295
+ lastLine =
296
+ lastLine . substr ( lastLine . length - 1 ) == " "
297
+ ? lastLine . substr ( 0 , lastLine . length - 1 )
298
+ : lastLine ;
299
+ var key = parseInt ( i ) ;
300
+ var nextLineIsSmaller = isSmallerThanWidth ( key , lastLine , fontSize ) ;
301
+ var isLastWord = i >= textSplit . length - 1 ;
302
+
303
+ if ( nextLineIsSmaller && ! isLastWord ) {
304
+ lastLine += " " ;
305
+ currWord = 0 ;
306
+ continue ; // Line
307
+ } else if ( ! nextLineIsSmaller && ! isLastWord ) {
308
+ if ( ! formObject . multiline ) {
309
+ continue FontSize;
310
+ } else {
311
+ if (
312
+ ( textHeight + lineSpacing ) * ( lineCount + 2 ) + lineSpacing >
313
+ height
314
+ ) {
315
+ // If the Text is higher than the
316
+ // FieldObject
317
+ continue FontSize;
318
+ }
319
+ lastWordInLine = key ;
320
+ // go on
321
+ }
322
+ } else if ( isLastWord ) {
323
+ lastWordInLine = key ;
289
324
} else {
290
325
if (
326
+ formObject . multiline &&
291
327
( textHeight + lineSpacing ) * ( lineCount + 2 ) + lineSpacing >
292
- height
328
+ height
293
329
) {
294
- // If the Text is higher than the
295
- // FieldObject
330
+ // If the Text is higher than the FieldObject
296
331
continue FontSize;
297
332
}
298
- lastWordInLine = key ;
299
- // go on
300
- }
301
- } else if ( isLastWord ) {
302
- lastWordInLine = key ;
303
- } else {
304
- if (
305
- formObject . multiline &&
306
- ( textHeight + lineSpacing ) * ( lineCount + 2 ) + lineSpacing > height
307
- ) {
308
- // If the Text is higher than the FieldObject
309
- continue FontSize;
310
333
}
311
334
}
335
+ // Remove last blank
312
336
313
337
var line = "" ;
314
338
315
339
for ( var x = firstWordInLine ; x <= lastWordInLine ; x ++ ) {
316
- line += textSplit [ x ] + " " ;
340
+ var currLine = textSplit [ x ] ;
341
+ if ( formObject . multiline ) {
342
+ if ( x === lastWordInLine ) {
343
+ line += currLine [ currWord ] + " " ;
344
+ currWord = ( currWord + 1 ) % currLine . length ;
345
+ continue ;
346
+ }
347
+ if ( x === firstWordInLine ) {
348
+ line += currLine [ currLine . length - 1 ] + " " ;
349
+ continue ;
350
+ }
351
+ }
352
+ line += currLine [ 0 ] + " " ;
317
353
}
318
354
319
355
// Remove last blank
@@ -347,7 +383,7 @@ var calculateX = function(formObject, text) {
347
383
348
384
// Reset for next iteration step
349
385
lastLength = 0 ;
350
- firstWordInLine = lastWordInLine + 1 ;
386
+ firstWordInLine = isWithNewLine ? lastWordInLine : lastWordInLine + 1 ;
351
387
lineCount ++ ;
352
388
353
389
lastLine = "" ;
0 commit comments