Skip to content

Commit d508087

Browse files
Samuel HuylebroeckFederico Frascarelli
authored andcommitted
Catch failed newline glyph retrieval
Handle missing newline through FontEncoding add test for type 3 font DEVSIX-2393
1 parent 7b09bf0 commit d508087

File tree

6 files changed

+52
-3
lines changed

6 files changed

+52
-3
lines changed

forms/src/test/java/com/itextpdf/forms/PdfFormFieldTest.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -777,8 +777,6 @@ private void fillAcroForm(PdfDocument pdfDocument, String text){
777777
}
778778

779779
@Test
780-
//DEVSIX-2393
781-
//TODO change cmp file after fix
782780
public void multilineFormFieldNewLineTest() throws IOException, InterruptedException {
783781
String testName = "multilineFormFieldNewLineTest";
784782
String outPdf = destinationFolder + testName + ".pdf";
@@ -803,6 +801,30 @@ public void multilineFormFieldNewLineTest() throws IOException, InterruptedExcep
803801
}
804802
}
805803

804+
@Test
805+
public void multilineFormFieldNewLineFontType3Test() throws IOException, InterruptedException {
806+
String testName = "multilineFormFieldNewLineFontType3Test";
807+
808+
String outPdf = destinationFolder + testName + ".pdf";
809+
String cmpPdf = sourceFolder + "cmp_"+ testName + ".pdf";
810+
String srcPdf = sourceFolder + testName + ".pdf";
811+
812+
PdfWriter writer = new PdfWriter(outPdf);
813+
PdfReader reader = new PdfReader(srcPdf);
814+
PdfDocument pdfDoc = new PdfDocument(reader, writer);
815+
816+
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
817+
PdfTextFormField info = (PdfTextFormField) form.getField("info");
818+
info.setValue("A\n\nE");
819+
820+
pdfDoc.close();
821+
CompareTool compareTool = new CompareTool();
822+
String errorMessage = compareTool.compareByContent(outPdf, cmpPdf, destinationFolder, "diff_");
823+
if (errorMessage != null) {
824+
Assert.fail(errorMessage);
825+
}
826+
}
827+
806828
@Test
807829
public void fillFormWithSameEmptyObjsForAppearance() throws IOException, InterruptedException {
808830
String outPdf = destinationFolder + "fillFormWithSameEmptyObjsForAppearance.pdf";

io/src/main/java/com/itextpdf/io/font/FontEncoding.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ public int convertToByte(int unicode) {
218218
* @return {@code true} if {@code ch} could be encoded.
219219
*/
220220
public boolean canEncode(int unicode) {
221-
return unicodeToCode.containsKey(unicode) || TextUtil.isNonPrintable(unicode);
221+
return unicodeToCode.containsKey(unicode) || TextUtil.isNonPrintable(unicode) || TextUtil.isNewLine(unicode);
222222
}
223223

224224
/**

io/src/main/java/com/itextpdf/io/util/TextUtil.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,35 @@ public static String charToString(char ch) {
221221
return String.valueOf(ch);
222222
}
223223

224+
/**
225+
* Check if a glyph is a newline by checking if it's unicode value is a newline
226+
*
227+
* @param glyph glyph to check
228+
* @return True if the glyph represents a newline, false otherwise
229+
*/
224230
public static boolean isNewLine(Glyph glyph) {
225231
int unicode = glyph.getUnicode();
232+
return isNewLine(unicode);
233+
}
234+
235+
/**
236+
* Check if a character is a newline by checking if it's integer value is a newline in unicode
237+
*
238+
* @param c character to check
239+
* @return True if the character represents a newline, false otherwise
240+
*/
241+
public static boolean isNewLine(char c) {
242+
int unicode = (int) c;
243+
return isNewLine(unicode);
244+
}
245+
246+
/**
247+
* Check if a character is a newline by checking if it's integer value is a newline in unicode
248+
*
249+
* @param unicode unicode value to check
250+
* @return True if the character represents a newline, false otherwise
251+
*/
252+
public static boolean isNewLine(int unicode) {
226253
return unicode == '\n' || unicode == '\r';
227254
}
228255

0 commit comments

Comments
 (0)