|
| 1 | +package com.itextpdf.pdfua.checkers; |
| 2 | + |
| 3 | +import com.itextpdf.io.font.PdfEncodings; |
| 4 | +import com.itextpdf.kernel.geom.Rectangle; |
| 5 | +import com.itextpdf.kernel.pdf.PdfName; |
| 6 | +import com.itextpdf.kernel.pdf.PdfPage; |
| 7 | +import com.itextpdf.kernel.pdf.PdfString; |
| 8 | +import com.itextpdf.kernel.pdf.PdfUAConformance; |
| 9 | +import com.itextpdf.kernel.pdf.annot.PdfAnnotation; |
| 10 | +import com.itextpdf.kernel.pdf.annot.PdfTextAnnotation; |
| 11 | +import com.itextpdf.pdfua.UaValidationTestFramework; |
| 12 | +import com.itextpdf.pdfua.exceptions.PdfUAExceptionMessageConstants; |
| 13 | +import com.itextpdf.test.ExtendedITextTest; |
| 14 | +import com.itextpdf.test.TestUtil; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.util.Arrays; |
| 18 | +import java.util.List; |
| 19 | +import org.junit.jupiter.api.BeforeAll; |
| 20 | +import org.junit.jupiter.api.BeforeEach; |
| 21 | +import org.junit.jupiter.api.Tag; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.params.ParameterizedTest; |
| 24 | +import org.junit.jupiter.params.provider.MethodSource; |
| 25 | + |
| 26 | +@Tag("IntegrationTest") |
| 27 | +public class PdfUAStringTest extends ExtendedITextTest { |
| 28 | + private static final String DESTINATION_FOLDER = TestUtil.getOutputPath() + "/pdfua/PdfUAStringTest/"; |
| 29 | + private static final Rectangle RECTANGLE = new Rectangle(100, 100, 100, 100); |
| 30 | + |
| 31 | + private UaValidationTestFramework framework; |
| 32 | + |
| 33 | + @BeforeAll |
| 34 | + public static void before() { |
| 35 | + createOrClearDestinationFolder(DESTINATION_FOLDER); |
| 36 | + } |
| 37 | + |
| 38 | + @BeforeEach |
| 39 | + public void setUp() { |
| 40 | + framework = new UaValidationTestFramework(DESTINATION_FOLDER, false); |
| 41 | + } |
| 42 | + |
| 43 | + public static List<Integer> privateUseAreaSymbols() { |
| 44 | + return Arrays.asList(0xE004, 0xF0009, 0x10FFFA); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void validValueWithDocEncodingTest() throws IOException { |
| 49 | + framework.addBeforeGenerationHook(document -> { |
| 50 | + document.addNewPage(); |
| 51 | + PdfString pdfString = new PdfString("value", PdfEncodings.PDF_DOC_ENCODING); |
| 52 | + document.getCatalog().put(PdfName.Lang, pdfString); |
| 53 | + }); |
| 54 | + framework.assertBothValid("validValueWithDocEncoding", PdfUAConformance.PDF_UA_2); |
| 55 | + } |
| 56 | + |
| 57 | + @ParameterizedTest |
| 58 | + @MethodSource("privateUseAreaSymbols") |
| 59 | + public void puaValueWithDocEncodingTest(Integer puaSymbol) throws IOException { |
| 60 | + String filename = "puaValueWithDocEncoding_" + getPuaValueName(puaSymbol); |
| 61 | + framework.addBeforeGenerationHook(document -> { |
| 62 | + PdfString pdfString = new PdfString("hello_" + new String(Character.toChars((int) puaSymbol)), PdfEncodings.PDF_DOC_ENCODING); |
| 63 | + PdfPage page = document.addNewPage(); |
| 64 | + PdfAnnotation textAnnotation = new PdfTextAnnotation(RECTANGLE).setContents(pdfString); |
| 65 | + page.addAnnotation(textAnnotation); |
| 66 | + }); |
| 67 | + framework.assertITextFail(filename, PdfUAExceptionMessageConstants.TEXT_STRING_USES_UNICODE_PUA, PdfUAConformance.PDF_UA_2); |
| 68 | + // In this particular case validators which reopen the document cannot identify the problem, and strictly speaking PDF document is valid. |
| 69 | + // Since PDFDocEncoding doesn't have enough space to allocate this Unicode PUA symbol, it is simply not present in the resulting file. |
| 70 | + // Even though the file is valid, there was clearly an attempt to create human-readable PdfString with Unicode PUA, that's why we fail. |
| 71 | + framework.assertVeraPdfValid(filename, PdfUAConformance.PDF_UA_2); |
| 72 | + } |
| 73 | + |
| 74 | + @ParameterizedTest |
| 75 | + @MethodSource("privateUseAreaSymbols") |
| 76 | + public void puaValueWithUTF8Test(Integer puaSymbol) throws IOException { |
| 77 | + String filename = "puaValueWithUTF8_" + getPuaValueName(puaSymbol); |
| 78 | + framework.addBeforeGenerationHook(document -> { |
| 79 | + PdfString pdfString = new PdfString("hello_" + new String(Character.toChars((int) puaSymbol)), PdfEncodings.UTF8); |
| 80 | + PdfPage page = document.addNewPage(); |
| 81 | + PdfAnnotation textAnnotation = new PdfTextAnnotation(RECTANGLE).setSubject(pdfString); |
| 82 | + page.addAnnotation(textAnnotation); |
| 83 | + }); |
| 84 | + framework.assertITextFail(filename, PdfUAExceptionMessageConstants.TEXT_STRING_USES_UNICODE_PUA, PdfUAConformance.PDF_UA_2); |
| 85 | + // VeraPdf doesn't fail because they mistakenly don't check all the PdfString entries in the document. |
| 86 | + framework.assertVeraPdfValid(filename, PdfUAConformance.PDF_UA_2); |
| 87 | + } |
| 88 | + |
| 89 | + @ParameterizedTest |
| 90 | + @MethodSource("privateUseAreaSymbols") |
| 91 | + public void puaValueWithUTF16Test(Integer puaSymbol) throws IOException { |
| 92 | + String filename = "puaValueWithUTF16_" + getPuaValueName(puaSymbol); |
| 93 | + framework.addBeforeGenerationHook(document -> { |
| 94 | + PdfString pdfString = new PdfString("hello_" + new String(Character.toChars((int) puaSymbol)), PdfEncodings.UNICODE_BIG); |
| 95 | + PdfPage page = document.addNewPage(); |
| 96 | + PdfAnnotation textAnnotation = new PdfTextAnnotation(RECTANGLE).setSubject(pdfString); |
| 97 | + page.addAnnotation(textAnnotation); |
| 98 | + }); |
| 99 | + framework.assertITextFail(filename, PdfUAExceptionMessageConstants.TEXT_STRING_USES_UNICODE_PUA, PdfUAConformance.PDF_UA_2); |
| 100 | + // VeraPdf doesn't fail because they mistakenly don't check all the PdfString entries in the document. |
| 101 | + framework.assertVeraPdfValid(filename, PdfUAConformance.PDF_UA_2); |
| 102 | + } |
| 103 | + |
| 104 | + @ParameterizedTest |
| 105 | + @MethodSource("privateUseAreaSymbols") |
| 106 | + public void puaValueWithUTF16UnmarkedTest(Integer puaSymbol) throws IOException { |
| 107 | + String filename = "puaValueWithUTF16Unmarked_" + getPuaValueName(puaSymbol); |
| 108 | + framework.addBeforeGenerationHook(document -> { |
| 109 | + PdfString pdfString = new PdfString("hello_" + new String(Character.toChars((int) puaSymbol)), PdfEncodings.UNICODE_BIG_UNMARKED); |
| 110 | + PdfPage page = document.addNewPage(); |
| 111 | + PdfAnnotation textAnnotation = new PdfTextAnnotation(RECTANGLE).setSubject(pdfString); |
| 112 | + page.addAnnotation(textAnnotation); |
| 113 | + }); |
| 114 | + framework.assertBothValid(filename, PdfUAConformance.PDF_UA_2); |
| 115 | + } |
| 116 | + |
| 117 | + @ParameterizedTest |
| 118 | + @MethodSource("privateUseAreaSymbols") |
| 119 | + public void puaValueInLangTest(Integer puaSymbol) throws IOException { |
| 120 | + String filename = "puaValueInLang_" + getPuaValueName(puaSymbol); |
| 121 | + framework.addBeforeGenerationHook(document -> { |
| 122 | + PdfString pdfString = new PdfString("hello_" + new String(Character.toChars((int) puaSymbol)), PdfEncodings.UTF8); |
| 123 | + document.addNewPage(); |
| 124 | + document.getCatalog().setLang(pdfString); |
| 125 | + }); |
| 126 | + // This test is only needed to reproduce veraPdf failure. |
| 127 | + // For now, we only were able to reproduce it when lang entry in catalog dictionary contains PUA. |
| 128 | + // However, iText logic fails earlier, because Lang entry must contain valid language identifier. |
| 129 | + framework.assertBothFail(filename, PdfUAExceptionMessageConstants.DOCUMENT_SHALL_CONTAIN_VALID_LANG_ENTRY, PdfUAConformance.PDF_UA_2); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void puaValueWithTest() throws IOException { |
| 134 | + framework.addBeforeGenerationHook(document -> { |
| 135 | + document.addNewPage(); |
| 136 | + PdfString pdfString = new PdfString(new String(Character.toChars(0xE005)), PdfEncodings.WINANSI); |
| 137 | + document.getCatalog().put(PdfName.Lang, pdfString); |
| 138 | + }); |
| 139 | + framework.assertBothFail("puaValueWithUTF16", PdfUAConformance.PDF_UA_2); |
| 140 | + } |
| 141 | + |
| 142 | + private static String getPuaValueName(Integer puaSymbol) { |
| 143 | + switch (puaSymbol) { |
| 144 | + case 0xE004: |
| 145 | + return "PrivateArea"; |
| 146 | + case 0xF0009: |
| 147 | + return "SupplementaryPrivateAreaA"; |
| 148 | + case 0x10FFFA: |
| 149 | + return "SupplementaryPrivateAreaB"; |
| 150 | + } |
| 151 | + return null; |
| 152 | + } |
| 153 | +} |
0 commit comments