From f5a19b03d7dd0934a0cf6ecfa31449d803578157 Mon Sep 17 00:00:00 2001 From: ST-1580 Date: Fri, 22 Oct 2021 11:03:42 +0300 Subject: [PATCH] Support for text with newlines in Acroform TextFields (#3298) --- src/modules/acroform.js | 102 ++++++++++++------ test/reference/textfieldMultiline.pdf | Bin 0 -> 3650 bytes .../reference/textfieldMultilineSmallForm.pdf | Bin 0 -> 3746 bytes test/specs/acroform.spec.js | 36 +++++++ 4 files changed, 105 insertions(+), 33 deletions(-) create mode 100644 test/reference/textfieldMultiline.pdf create mode 100644 test/reference/textfieldMultilineSmallForm.pdf diff --git a/src/modules/acroform.js b/src/modules/acroform.js index d333eb28d..d440d51f6 100644 --- a/src/modules/acroform.js +++ b/src/modules/acroform.js @@ -217,6 +217,11 @@ var calculateX = function(formObject, text) { : text; // split into array of words var textSplit = text.split(" "); + if (formObject.multiline) { + textSplit = textSplit.map(word => word.split("\n")); + } else { + textSplit = textSplit.map(word => [word]); + } var fontSize = maxFontSize; // The Starting fontSize (The Maximum) var lineSpacing = 2; @@ -229,7 +234,7 @@ var calculateX = function(formObject, text) { var isSmallerThanWidth = function(i, lastLine, fontSize) { if (i + 1 < textSplit.length) { - var tmp = lastLine + " " + textSplit[i + 1]; + var tmp = lastLine + " " + textSplit[i + 1][0]; var TextWidth = calculateFontSpace(tmp, formObject, fontSize).width; var FieldWidth = width - 2 * borderPadding; return TextWidth <= FieldWidth; @@ -253,6 +258,7 @@ var calculateX = function(formObject, text) { var firstWordInLine = 0, lastWordInLine = 0; var lastLength; + var currWord = 0; if (fontSize <= 0) { // In case, the Text doesn't fit at all @@ -269,51 +275,81 @@ var calculateX = function(formObject, text) { var lastLine = ""; var lineCount = 0; - Line: for (var i in textSplit) { + Line: for (var i = 0; i < textSplit.length; i++) { if (textSplit.hasOwnProperty(i)) { - lastLine += textSplit[i] + " "; - // Remove last blank - lastLine = - lastLine.substr(lastLine.length - 1) == " " - ? lastLine.substr(0, lastLine.length - 1) - : lastLine; - var key = parseInt(i); - var nextLineIsSmaller = isSmallerThanWidth(key, lastLine, fontSize); - var isLastWord = i >= textSplit.length - 1; - if (nextLineIsSmaller && !isLastWord) { - lastLine += " "; - continue; // Line - } else if (!nextLineIsSmaller && !isLastWord) { - if (!formObject.multiline) { + let isWithNewLine = false; + if (textSplit[i].length !== 1 && currWord !== textSplit[i].length - 1) { + if ( + (textHeight + lineSpacing) * (lineCount + 2) + lineSpacing > + height + ) { continue FontSize; + } + + lastLine += textSplit[i][currWord]; + isWithNewLine = true; + lastWordInLine = i; + i--; + } else { + lastLine += textSplit[i][currWord] + " "; + lastLine = + lastLine.substr(lastLine.length - 1) == " " + ? lastLine.substr(0, lastLine.length - 1) + : lastLine; + var key = parseInt(i); + var nextLineIsSmaller = isSmallerThanWidth(key, lastLine, fontSize); + var isLastWord = i >= textSplit.length - 1; + + if (nextLineIsSmaller && !isLastWord) { + lastLine += " "; + currWord = 0; + continue; // Line + } else if (!nextLineIsSmaller && !isLastWord) { + if (!formObject.multiline) { + continue FontSize; + } else { + if ( + (textHeight + lineSpacing) * (lineCount + 2) + lineSpacing > + height + ) { + // If the Text is higher than the + // FieldObject + continue FontSize; + } + lastWordInLine = key; + // go on + } + } else if (isLastWord) { + lastWordInLine = key; } else { if ( + formObject.multiline && (textHeight + lineSpacing) * (lineCount + 2) + lineSpacing > - height + height ) { - // If the Text is higher than the - // FieldObject + // If the Text is higher than the FieldObject continue FontSize; } - lastWordInLine = key; - // go on - } - } else if (isLastWord) { - lastWordInLine = key; - } else { - if ( - formObject.multiline && - (textHeight + lineSpacing) * (lineCount + 2) + lineSpacing > height - ) { - // If the Text is higher than the FieldObject - continue FontSize; } } + // Remove last blank var line = ""; for (var x = firstWordInLine; x <= lastWordInLine; x++) { - line += textSplit[x] + " "; + var currLine = textSplit[x]; + if (formObject.multiline) { + if (x === lastWordInLine) { + line += currLine[currWord] + " "; + currWord = (currWord + 1) % currLine.length; + continue; + } + if (x === firstWordInLine) { + line += currLine[currLine.length - 1] + " "; + continue; + } + } + line += currLine[0] + " "; } // Remove last blank @@ -347,7 +383,7 @@ var calculateX = function(formObject, text) { // Reset for next iteration step lastLength = 0; - firstWordInLine = lastWordInLine + 1; + firstWordInLine = isWithNewLine ? lastWordInLine : lastWordInLine + 1; lineCount++; lastLine = ""; diff --git a/test/reference/textfieldMultiline.pdf b/test/reference/textfieldMultiline.pdf new file mode 100644 index 0000000000000000000000000000000000000000..a78ceb310e7c7770280583cb73567d58e0409b84 GIT binary patch literal 3650 zcmcIn+iu%N5PjdTm=_~93X!|Zs{+G-NSTchr*dVYDQe__l2#54iF8P+wfjx^g8WST zDV_5;EJQc89Ija!MBCs4?i(;lx2BU!W9FDwgW?3 zm}eDwEWy?uPEMH078IEN*9kE5TxHkQXYe?~^|C5dvS!?HJot-o_?ZPUGa~`Qg?Pz? zZ`cr@u%!^ubDJB2kCl-Z)rM5n#BP%AF``g2V$ttBH6*StKp+n zYK33fDQ41Z2)_qRgguL;C#9k*FyE=wpQ=jdNe3zd2=O`1=J_JcuEG46W}~c3TLada z7G)KFP71Ijws_L~k&ecvX{zDtgAamywfONF2x8!IV?0^^U@}{!Up6==9vbfl;UKoR zW!vIUXF)*hFB9sv=kW+}D5k2|0}9)z{1ggJ?0q#ucPiIYVwZ+Cf&CHCouSX?u-``L z4*c)u5vOZa4yO4!$@b6;{fWKHMIC=nbK&K=ScgBWl2tn2L;Ww$#%=xkN+1-fqKpRiCb?te{<9kQ?fs(!vybx9Q6$e>9SSO zlUtRI=H$E8mZ%G=caUvXD}2vkAL*mRqP)ch6b`{f<9}Xy37ULquxF zHY92v+XT1!HsSaD35)38IcxWG77mK+-F+VK)hqDbrMGYBV!J(#AyLBjY*P08$?sq# zZMT=R^f|WrP7J?`RaGSE3N;4Zdzb0oia0l?d0v6;J-h+VvSkjsz3I;wuHZOuBhR-Y sKN|b)Xlyx=$3IP!>`%Q?g0$-%`a>IutX2Y>(b*T2jG4*B$k9Ut5A{jCQ3BDvNK zzYCpJAn@&k*%Mv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