diff --git a/jpdfium/src/test/java/stirling/software/jpdfium/fonts/FontNormalizerTest.java b/jpdfium/src/test/java/stirling/software/jpdfium/fonts/FontNormalizerTest.java index b383372a9..12dfca6ac 100644 --- a/jpdfium/src/test/java/stirling/software/jpdfium/fonts/FontNormalizerTest.java +++ b/jpdfium/src/test/java/stirling/software/jpdfium/fonts/FontNormalizerTest.java @@ -1,15 +1,43 @@ package stirling.software.jpdfium.fonts; +import org.apache.pdfbox.Loader; +import org.apache.pdfbox.cos.COSArray; +import org.apache.pdfbox.cos.COSBase; +import org.apache.pdfbox.cos.COSDictionary; +import org.apache.pdfbox.cos.COSFloat; +import org.apache.pdfbox.cos.COSInteger; +import org.apache.pdfbox.cos.COSName; +import org.apache.pdfbox.cos.COSNumber; +import org.apache.pdfbox.cos.COSStream; import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.pdmodel.PDPage; +import org.apache.pdfbox.pdmodel.PDPageContentStream; +import org.apache.pdfbox.pdmodel.PDResources; +import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget; +import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream; +import org.apache.pdfbox.pdmodel.common.PDRectangle; +import org.apache.pdfbox.pdmodel.common.PDStream; +import org.apache.pdfbox.pdmodel.font.PDFont; +import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont; +import org.apache.pdfbox.pdmodel.font.encoding.WinAnsiEncoding; +import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject; +import org.apache.pdfbox.text.PDFTextStripper; import org.junit.jupiter.api.Test; +import stirling.software.jpdfium.PdfDocument; +import stirling.software.jpdfium.PdfPage; import stirling.software.jpdfium.exception.JPDFiumException; import stirling.software.jpdfium.exception.PdfCorruptException; import stirling.software.jpdfium.model.FontType; import stirling.software.jpdfium.panama.NativeRuntime; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.nio.ByteBuffer; import java.nio.ByteOrder; +import java.nio.charset.StandardCharsets; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -23,13 +51,13 @@ /** * Tests for {@link FontNormalizer}. * - *
The stub library returns default/empty results, so these tests verify - * the Java-layer parsing, record construction, and API correctness. + *
The stub library returns default/empty results, so the Java-layer tests
+ * verify parsing, record construction, and API correctness. The normalized-page
+ * regression tests need the real qpdf + FreeType bridge and are integration-gated.
*/
class FontNormalizerTest {
private static final byte[] EMPTY_BYTES = new byte[0];
-
@Test
void resultRecordProperties() {
var result = new FontNormalizer.Result(10, 3, 2, 1, 4);
@@ -105,7 +133,665 @@ void subsetRejectsCorruptFontWithoutGlyphs() {
() -> FontNormalizer.subset(corrupt, new int[] {0x41}, true));
}
- private static byte[] liberationSansBytes() throws Exception {
+ @Test
+ void fixToUnicodeGeneratesCmapForEmbeddedTrueType() throws Exception {
+ assumeTrue(NativeRuntime.isFull(), "needs real native library");
+ byte[] pdf = embeddedTrueTypePdf(true, false);
+ assertFalse(hasToUnicodeStream(pdf), "fixture must start without /ToUnicode");
+
+ try (PdfDocument doc = PdfDocument.open(pdf)) {
+ int fixed = FontNormalizer.fixToUnicode(doc, 0);
+ assertTrue(fixed >= 1, "expected at least one repaired /ToUnicode, got " + fixed);
+ byte[] repaired = doc.saveBytes();
+ assertTrue(hasToUnicodeStream(repaired),
+ "repaired document must carry a non-empty /ToUnicode stream");
+
+ try (PDDocument loaded = Loader.loadPDF(repaired)) {
+ PDFont font = firstFont(loaded);
+ assertNotNull(font, "repaired document must still expose the font");
+ assertEquals("H", font.toUnicode('H'));
+ assertEquals("Hello World", new PDFTextStripper().getText(loaded).trim());
+ }
+ }
+ }
+
+ @Test
+ void repairWidthsRebuildsMissingWidthsForEmbeddedTrueType() throws Exception {
+ assumeTrue(NativeRuntime.isFull(), "needs real native library");
+ byte[] pdf = embeddedTrueTypePdf(false, true);
+
+ try (PdfDocument doc = PdfDocument.open(pdf)) {
+ int repaired = FontNormalizer.repairWidths(doc, 0);
+ assertTrue(repaired >= 1, "expected at least one repaired /Widths, got " + repaired);
+ byte[] repairedBytes = doc.saveBytes();
+ assertTrue(hasWidthsArray(repairedBytes),
+ "repaired document must carry a /Widths array");
+
+ try (PDDocument loaded = Loader.loadPDF(repairedBytes)) {
+ COSDictionary font = firstFontDictionary(loaded);
+ assertNotNull(font);
+ COSArray widths = (COSArray) font.getDictionaryObject(COSName.WIDTHS);
+ assertNotNull(widths);
+ int firstChar = font.getInt(COSName.FIRST_CHAR);
+ assertEquals(722, widths.getInt(72 - firstChar),
+ "Liberation Sans 'H' advance width must be 722");
+ }
+ }
+ }
+
+ @Test
+ void repairWidthsLeavesMatchingRealValuedWidthsUnchanged() throws Exception {
+ assumeTrue(NativeRuntime.isFull(), "needs real native library");
+ byte[] repaired;
+ try (PdfDocument doc = PdfDocument.open(embeddedTrueTypePdf(false, true))) {
+ assertTrue(FontNormalizer.repairWidths(doc, 0) >= 1,
+ "fixture must first gain repaired widths");
+ repaired = doc.saveBytes();
+ }
+
+ try (PdfDocument doc = PdfDocument.open(withFloatWidths(repaired))) {
+ assertEquals(0, FontNormalizer.repairWidths(doc, 0),
+ "real-valued widths within one glyph unit must not be rewritten");
+ byte[] unchanged = doc.saveBytes();
+ try (PDDocument loaded = Loader.loadPDF(unchanged)) {
+ COSArray widths = (COSArray) firstFontDictionary(loaded)
+ .getDictionaryObject(COSName.WIDTHS);
+ assertNotNull(widths);
+ boolean fractional = false;
+ for (int i = 0; i < widths.size(); i++) {
+ if (widths.get(i) instanceof COSFloat value
+ && value.floatValue() != Math.rint(value.floatValue())) {
+ fractional = true;
+ break;
+ }
+ }
+ assertTrue(fractional, "fractional producer width must survive the repair pass");
+ }
+ }
+ }
+
+ @Test
+ void fixToUnicodeReplacesBoilerplateOnlyCMap() throws Exception {
+ assumeTrue(NativeRuntime.isFull(), "needs real native library");
+ byte[] pdf = withBoilerplateToUnicode(embeddedTrueTypePdf(false, false));
+ assertTrue(hasToUnicodeStream(pdf),
+ "fixture must start with a boilerplate /ToUnicode stream");
+
+ try (PdfDocument doc = PdfDocument.open(pdf)) {
+ assertEquals(1, FontNormalizer.fixToUnicode(doc, 0),
+ "a /ToUnicode stream with no bfchar/bfrange entries must be regenerated");
+ byte[] repaired = doc.saveBytes();
+
+ try (PDDocument loaded = Loader.loadPDF(repaired)) {
+ assertEquals("Hello World", new PDFTextStripper().getText(loaded).trim(),
+ "the regenerated CMap must keep text extraction working");
+ }
+ }
+ }
+
+ @Test
+ void fixToUnicodeKeepsMappingAfterEmptyCmapSection() throws Exception {
+ assumeTrue(NativeRuntime.isFull(), "needs real native library");
+ byte[] pdf = withMultiSectionToUnicode(embeddedTrueTypePdf(false, false));
+
+ try (PdfDocument doc = PdfDocument.open(pdf)) {
+ assertEquals(0, FontNormalizer.fixToUnicode(doc, 0),
+ "an empty first bfchar section must not hide a later mapping");
+ }
+ }
+
+ @Test
+ void repairWidthsPreservesProducerWidthForUnresolvedCode() throws Exception {
+ assumeTrue(NativeRuntime.isFull(), "needs real native library");
+ byte[] pdf = unresolvedWidthPdf();
+
+ try (PdfDocument doc = PdfDocument.open(pdf)) {
+ assertEquals(1, FontNormalizer.repairWidths(doc, 0),
+ "the resolved 'H' width must be repaired");
+ byte[] repaired = doc.saveBytes();
+
+ try (PDDocument loaded = Loader.loadPDF(repaired)) {
+ COSDictionary font = firstFontDictionary(loaded);
+ assertNotNull(font);
+ COSArray widths = (COSArray) font.getDictionaryObject(COSName.WIDTHS);
+ assertNotNull(widths);
+ int firstChar = font.getInt(COSName.FIRST_CHAR);
+ assertEquals(777, widths.getInt(65 - firstChar),
+ "/.notdef leaves code 65 unresolved; its producer width must survive");
+ assertEquals(722, widths.getInt(72 - firstChar),
+ "resolved 'H' must take Liberation Sans' 722-unit advance");
+ }
+ }
+ }
+
+ @Test
+ void repairWidthsIncludesZeroWidthCidGlyphWhenDwIsNonZero() throws Exception {
+ assumeTrue(NativeRuntime.isFull(), "needs real native library");
+ byte[] pdf = cidTrueTypePdf();
+
+ try (PdfDocument doc = PdfDocument.open(pdf)) {
+ assertEquals(1, FontNormalizer.repairWidths(doc, 0),
+ "a CID font without /W must gain one from the embedded program");
+ byte[] repaired = doc.saveBytes();
+
+ try (PDDocument loaded = Loader.loadPDF(repaired)) {
+ COSDictionary descendant = cidDescendant(loaded);
+ assertNotNull(descendant);
+ assertNotNull(descendant.getDictionaryObject(COSName.W),
+ "CID font must gain /W");
+ Integer width = cidWidth(descendant, 1);
+ assertNotNull(width,
+ "/W must spell out the zero-advance CID instead of inheriting /DW");
+ assertEquals(0, width.intValue(),
+ "/DW 1000 must not hide the zero-width glyph");
+ }
+ }
+ }
+
+ @Test
+ void perPageRepairsRejectNegativePageIndex() throws Exception {
+ try (PdfDocument doc = PdfDocument.open(embeddedTrueTypePdf(false, false))) {
+ assertThrows(IllegalArgumentException.class, () -> FontNormalizer.fixToUnicode(doc, -1));
+ assertThrows(IllegalArgumentException.class, () -> FontNormalizer.repairWidths(doc, -2));
+ }
+ }
+
+ @Test
+ void fixToUnicodeMapsWinAnsiHighRangeAndDifferences() throws Exception {
+ assumeTrue(NativeRuntime.isFull(), "needs real native library");
+ byte[] pdf = winAnsiDifferencesPdf();
+ assertFalse(hasToUnicodeStream(pdf), "fixture must start without /ToUnicode");
+
+ try (PdfDocument doc = PdfDocument.open(pdf)) {
+ assertEquals(1, FontNormalizer.fixToUnicode(doc, 0),
+ "expected one repaired /ToUnicode");
+ byte[] repaired = doc.saveBytes();
+ try (PDDocument loaded = Loader.loadPDF(repaired)) {
+ PDFont font = firstFont(loaded);
+ assertNotNull(font);
+ assertEquals("\u20AC", font.toUnicode(128),
+ "/Differences /Euro must map to the Euro sign");
+ assertEquals("\u2022", font.toUnicode(129),
+ "/Differences /bullet must map to the bullet");
+ }
+ }
+ }
+
+ @Test
+ void normalizePageRepairsToUnicodeAndWidthsInOnePass() throws Exception {
+ assumeTrue(NativeRuntime.isFull(), "needs real native library");
+ byte[] pdf = embeddedTrueTypePdf(true, true);
+
+ try (PdfDocument doc = PdfDocument.open(pdf)) {
+ FontNormalizer.Result result = FontNormalizer.normalizePage(doc, 0);
+ assertTrue(result.fontsProcessed() >= 1,
+ "expected at least one embedded font, got " + result.fontsProcessed());
+ assertTrue(result.toUnicodeFixed() >= 1,
+ "expected /ToUnicode repair, got " + result.toUnicodeFixed());
+ assertTrue(result.widthsRepaired() >= 1,
+ "expected width repair, got " + result.widthsRepaired());
+
+ byte[] repaired = doc.saveBytes();
+ assertTrue(hasToUnicodeStream(repaired), "/ToUnicode missing after normalizePage");
+ assertTrue(hasWidthsArray(repaired), "/Widths missing after normalizePage");
+ }
+ }
+
+ @Test
+ void normalizeAllRepairsCidToUnicodeAndCompactWidthsIdempotently() throws Exception {
+ assumeTrue(NativeRuntime.isFull(), "needs real native library");
+ byte[] pdf = cidTrueTypePdf();
+ assertFalse(hasToUnicodeStream(pdf), "fixture must start without /ToUnicode");
+
+ try (PdfDocument doc = PdfDocument.open(pdf)) {
+ FontNormalizer.Result first = FontNormalizer.normalizeAll(doc);
+ assertEquals(1, first.toUnicodeFixed(), "CID font must gain /ToUnicode");
+ assertEquals(1, first.widthsRepaired(), "CID font must gain a compact /W");
+
+ byte[] repaired = doc.saveBytes();
+ assertTrue(hasToUnicodeStream(repaired),
+ "repaired CID document must carry a non-empty /ToUnicode stream");
+ try (PDDocument loaded = Loader.loadPDF(repaired)) {
+ COSDictionary descendant = cidDescendant(loaded);
+ assertNotNull(descendant);
+ COSArray widths = (COSArray) descendant.getDictionaryObject(COSName.W);
+ assertNotNull(widths, "CID font must gain /W");
+ assertTrue(widths.size() > 0, "/W must not be empty");
+ assertTrue(widths.getObject(0) instanceof COSInteger,
+ "compact /W must start with a CID, not a per-glyph array");
+ PDFont font = firstFont(loaded);
+ assertEquals("A", font.toUnicode(0x24),
+ "generated CID /ToUnicode must map CID 36 to A in Liberation Sans");
+ }
+
+ FontNormalizer.Result second = FontNormalizer.normalizeAll(doc);
+ assertEquals(0, second.toUnicodeFixed(),
+ "a second normalizeAll pass must not rewrite /ToUnicode");
+ assertEquals(0, second.widthsRepaired(),
+ "a second normalizeAll pass must not rewrite the compact /W");
+ }
+ }
+
+ @Test
+ void normalizePageFindsFontsInsideFormXObjectAndAnnotationAppearance() throws Exception {
+ assumeTrue(NativeRuntime.isFull(), "needs real native library");
+ byte[] pdf = formXObjectFontPdf();
+
+ try (PdfDocument doc = PdfDocument.open(pdf)) {
+ FontNormalizer.Result result = FontNormalizer.normalizePage(doc, 0);
+ assertEquals(2, result.toUnicodeFixed(),
+ "fonts inside a /Form XObject and an /AP /N stream must get /ToUnicode");
+ assertEquals(2, result.widthsRepaired(),
+ "fonts inside a /Form XObject and an /AP /N stream must get repaired /Widths");
+
+ byte[] repaired = doc.saveBytes();
+ String raw = new String(repaired, StandardCharsets.ISO_8859_1);
+ assertTrue(raw.contains("/ToUnicode"),
+ "repaired fonts must carry a /ToUnicode stream");
+ assertTrue(raw.contains("/Widths"), "repaired fonts must carry a /Widths array");
+ }
+ }
+
+ @Test
+ void normalizePageInvalidatesOpenPageWhenRepairRewritesDocument() throws Exception {
+ assumeTrue(NativeRuntime.isFull(), "needs real native library");
+ try (PdfDocument doc = PdfDocument.open(embeddedTrueTypePdf(false, true));
+ PdfPage page = doc.page(0)) {
+ FontNormalizer.Result result = FontNormalizer.normalizePage(doc, 0);
+ assertTrue(result.widthsRepaired() >= 1, "fixture must repair widths");
+ assertThrows(IllegalStateException.class, page::rotation,
+ "an open page must be stale after the document is rewritten");
+ }
+ }
+
+ /**
+ * Builds a one-page PDF whose text uses an embedded simple TrueType font.
+ * Optionally removes the font dictionary entries that the normalizer repairs.
+ */
+ private static byte[] embeddedTrueTypePdf(boolean stripToUnicode, boolean stripWidths)
+ throws IOException {
+ try (PDDocument doc = new PDDocument()) {
+ PDPage page = new PDPage(PDRectangle.LETTER);
+ doc.addPage(page);
+
+ PDTrueTypeFont font;
+ try (InputStream in = PDDocument.class.getResourceAsStream(
+ "/org/apache/pdfbox/resources/ttf/LiberationSans-Regular.ttf")) {
+ assertNotNull(in, "PDFBox no longer bundles LiberationSans");
+ font = PDTrueTypeFont.load(doc, in, WinAnsiEncoding.INSTANCE);
+ }
+
+ try (PDPageContentStream content = new PDPageContentStream(doc, page)) {
+ content.beginText();
+ content.setFont(font, 12);
+ content.newLineAtOffset(72, 700);
+ content.showText("Hello World");
+ content.endText();
+ }
+
+ COSDictionary dictionary = font.getCOSObject();
+ if (stripToUnicode) dictionary.removeItem(COSName.TO_UNICODE);
+ if (stripWidths) {
+ dictionary.removeItem(COSName.WIDTHS);
+ dictionary.removeItem(COSName.FIRST_CHAR);
+ dictionary.removeItem(COSName.LAST_CHAR);
+ }
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ doc.save(out);
+ return out.toByteArray();
+ }
+ }
+
+ /**
+ * Rewrites every /Widths entry as a {@link COSFloat} without changing its
+ * numeric value, to exercise the native real-value tolerance path.
+ */
+ private static byte[] withFloatWidths(byte[] pdf) throws IOException {
+ try (PDDocument doc = Loader.loadPDF(pdf)) {
+ COSDictionary font = firstFontDictionary(doc);
+ assertNotNull(font, "fixture must contain a font");
+ COSArray widths = (COSArray) font.getDictionaryObject(COSName.WIDTHS);
+ assertNotNull(widths, "fixture must contain /Widths");
+ for (int i = 0; i < widths.size(); i++) {
+ float value = ((COSNumber) widths.get(i)).floatValue();
+ widths.set(i, new COSFloat(value + 0.25f));
+ }
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ doc.save(out);
+ return out.toByteArray();
+ }
+ }
+
+ /**
+ * Builds a PDF whose embedded TrueType font uses WinAnsi with a
+ * {@code /Differences} array for the 0x80/0x81 high range.
+ */
+ private static byte[] winAnsiDifferencesPdf() throws IOException {
+ try (PDDocument doc = Loader.loadPDF(embeddedTrueTypePdf(true, false))) {
+ COSDictionary font = firstFontDictionary(doc);
+ assertNotNull(font, "fixture must contain a font");
+
+ COSDictionary encoding = new COSDictionary();
+ encoding.setItem(COSName.BASE_ENCODING, COSName.WIN_ANSI_ENCODING);
+ COSArray differences = new COSArray();
+ differences.add(COSInteger.get(128));
+ differences.add(COSName.getPDFName("Euro"));
+ differences.add(COSInteger.get(129));
+ differences.add(COSName.getPDFName("bullet"));
+ encoding.setItem(COSName.DIFFERENCES, differences);
+ font.setItem(COSName.ENCODING, encoding);
+ font.removeItem(COSName.TO_UNICODE);
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ doc.save(out);
+ return out.toByteArray();
+ }
+ }
+
+ /**
+ * Replaces the font's {@code /ToUnicode} with the CMap prologue only, the shape a
+ * broken producer emits when it never wrote any {@code beginbfchar} or
+ * {@code beginbfrange} section.
+ */
+ private static byte[] withMultiSectionToUnicode(byte[] pdf) throws IOException {
+ try (PDDocument doc = Loader.loadPDF(pdf)) {
+ COSDictionary font = firstFontDictionary(doc);
+ assertNotNull(font, "fixture must contain a font");
+ COSStream toUnicode = new COSStream();
+ try (OutputStream out = toUnicode.createOutputStream()) {
+ out.write("""
+ /CIDInit /ProcSet findresource begin
+ 12 dict begin
+ begincmap
+ /CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def
+ /CMapName /Adobe-Identity-UCS def
+ /CMapType 2 def
+ 1 begincodespacerange
+ <00>