Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 69 additions & 33 deletions src/modules/acroform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Comment on lines +220 to +224
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this looks a bit fishy to me. Does this work for a text like "abc \n def"? Wouldn't it be better to split first into lines and then into words?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, I will add new test soon. Also I reproduced your example and it works:

image

I think that current solve is easier to implement in your code. In my opinion, your implementation will require more new code than my. So it will be harder to find hypothetical new bugs.


var fontSize = maxFontSize; // The Starting fontSize (The Maximum)
var lineSpacing = 2;
Expand All @@ -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;
Expand All @@ -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
Expand 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
Expand Down Expand Up @@ -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 = "";
Expand Down
Binary file added test/reference/textfieldMultiline.pdf
Binary file not shown.
Binary file added test/reference/textfieldMultilineSmallForm.pdf
Binary file not shown.
36 changes: 36 additions & 0 deletions test/specs/acroform.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,42 @@ describe("Module: Acroform Integration Test", function() {
comparePdf(doc.output(), "password.pdf", "acroform");
});

it("Check multiline text", function() {
var doc = new jsPDF({
orientation: "p",
unit: "mm",
format: "a4",
floatPrecision: 2
});
doc.text("TextField:", 10, 145);
var textField = new TextField();
textField.Rect = [50, 140, 100, 100];
textField.multiline = true;
textField.V = "A\nB\nC";

doc.addField(textField);

comparePdf(doc.output(), "textfieldMultiline.pdf", "acroform");
});

it("Check multiline text in small form", function() {
var doc = new jsPDF({
orientation: "p",
unit: "mm",
format: "a4",
floatPrecision: 2
});
doc.text("TextField:", 10, 145);
var textField = new TextField();
textField.Rect = [50, 140, 100, 15];
textField.multiline = true;
textField.V = "A\nLong line Long line Long line Long line Long line \nC";

doc.addField(textField);

comparePdf(doc.output(), "textfieldMultilineSmallForm.pdf", "acroform");
});

it("should add a RadioGroup Cross", function() {
var doc = new jsPDF({
orientation: "p",
Expand Down