diff --git a/CHANGELOG.md b/CHANGELOG.md index cee8dc49a..03a05cb89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -131,13 +131,6 @@ publishing workflows, not library releases, so they are not listed here. - The qpdf writer only raises the minimum PDF version to 1.4 when a JBIG2 stream was actually written, so requesting JBIG2 on a document with no qualifying image no longer bumps the version. -- `PdfXConverter` converts colours through a QPDF content-stream rewrite instead - of PDFium page regeneration. Conversion time is now linear in page count (a - 6,269-page spreadsheet went from 582 s to 2 s), text runs and marked content - are kept, and `applyMetadata(PdfDocument, ...)` no longer modifies its input. - Form XObjects, patterns, Type 3 glyphs and annotation appearances are always - converted, so `convertForms` and `convertLosslessImages` no longer affect - PDF/X output. - `PdfFontAuditor.hasNonEmbeddedFonts` now includes fonts that only exist in unused resource dictionaries or AcroForm `/DR`, matching `auditAll`. Documents that @@ -175,6 +168,23 @@ publishing workflows, not library releases, so they are not listed here. area coverage cap, neutrals print with single-channel K, and the Ghostscript-derived calibration table is gone. The profile stays under 24 KB. +- Font normalization rebuilds `/CIDSet` from every CID present in the embedded + program, including CIDs mapped to GID 0, and corrects an existing `/W` array + when it disagrees with the program's advances. `issue20065.pdf` now passes + veraPDF 6.2.11.4.2-2 and 6.2.11.5-1 without changing extracted text. + +- Compression font subsetting is opt-in (`subsetFonts(false)` by default) and + verifies extracted text and rendering before adopting the smaller bytes. + `PdfFontEmbedder.embedMissingStrict` rejects a result that still has an + unembedded font instead of returning a document that silently fails + PDF/A font-embedding checks. + +- The Adobe Glyph List table covers all 4,281 names, including the 81 + multi-scalar Arabic/Hebrew sequences, and `uXXXX` escapes accept lowercase + only when they contain a decimal digit, so ordinary names such as `uface` + no longer resolve to U+FACE while `u00e9` still works. + + - PDF/X-1a and PDF/X-3 no longer accept images with soft masks, which the page transparency probe missed, and drop transparency groups from pages that have no transparent content. diff --git a/jpdfium/src/main/java/stirling/software/jpdfium/doc/CompressOptions.java b/jpdfium/src/main/java/stirling/software/jpdfium/doc/CompressOptions.java index 040d2d341..f67c76fec 100644 --- a/jpdfium/src/main/java/stirling/software/jpdfium/doc/CompressOptions.java +++ b/jpdfium/src/main/java/stirling/software/jpdfium/doc/CompressOptions.java @@ -176,7 +176,7 @@ public static final class Builder { private boolean useZopfliDeflate = false; private int zopfliIterations = 15; private int zopfliMaxInputBytes = 0; - private boolean subsetFonts = true; + private boolean subsetFonts = false; private Builder() {} @@ -285,7 +285,10 @@ public Builder zopfliMaxInputBytes(int bytes) { return this; } - /** Subset already-embedded TrueType/OpenType fonts. Default {@code true}. */ + /** + * Subset already-embedded TrueType/OpenType fonts. Default {@code false}: the pass + * rewrites font programs, so callers opt in after their own text/render contract. + */ public Builder subsetFonts(boolean v) { this.subsetFonts = v; return this; diff --git a/jpdfium/src/main/java/stirling/software/jpdfium/doc/CompressPreset.java b/jpdfium/src/main/java/stirling/software/jpdfium/doc/CompressPreset.java index 0e31e1596..7e156bf07 100644 --- a/jpdfium/src/main/java/stirling/software/jpdfium/doc/CompressPreset.java +++ b/jpdfium/src/main/java/stirling/software/jpdfium/doc/CompressPreset.java @@ -8,17 +8,17 @@ public enum CompressPreset { * Web optimization: moderate image quality, object stream compression. * Good balance between file size and quality. */ - WEB(75, 150, true, true, true, false, true, PreservationMode.VISUALLY_CONSTRAINED), + WEB(75, 150, true, true, true, false, false, PreservationMode.VISUALLY_CONSTRAINED), /** * Screen/email: lower quality, maximum compression. */ - SCREEN(60, 96, true, true, true, false, true, PreservationMode.SCAN), + SCREEN(60, 96, true, true, true, false, false, PreservationMode.SCAN), /** * Print-ready: high quality, structural optimization only. */ - PRINT(90, 300, false, true, false, false, true, PreservationMode.VISUALLY_CONSTRAINED), + PRINT(90, 300, false, true, false, false, false, PreservationMode.VISUALLY_CONSTRAINED), /** * Lossless maximum: aggressive structural optimization and stream @@ -31,12 +31,12 @@ public enum CompressPreset { * than the qpdf pass alone. zopfli remains available as an explicit opt-in * via {@link CompressOptions.Builder#useZopfliDeflate(boolean)}. */ - LOSSLESS(-1, -1, false, true, false, false, true, PreservationMode.EXACT), + LOSSLESS(-1, -1, false, true, false, false, false, PreservationMode.EXACT), /** * Maximum compression: aggressive image optimization and structural compression. */ - MAXIMUM(50, 96, true, true, true, false, true, PreservationMode.DESTRUCTIVE); + MAXIMUM(50, 96, true, true, true, false, false, PreservationMode.DESTRUCTIVE); private final int imageQuality; private final int maxImageDpi; diff --git a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfCompressor.java b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfCompressor.java index 2ef3c0205..adf69958e 100644 --- a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfCompressor.java +++ b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfCompressor.java @@ -15,6 +15,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Objects; import java.util.function.BiFunction; import java.util.zip.DataFormatException; import java.util.zip.Inflater; @@ -386,12 +387,27 @@ && serializesIdentically(doc, sourceBytes, openStateBytes)) { // 5. Subset already-embedded TrueType/OpenType programs. Run before the // structural pass so qpdf recompresses the smaller font streams. - if (!signed && !pdfA && opts.subsetFonts() && QpdfLib.isSubsetFontsSupported()) { - QpdfLib.FontSubsetResult sub = QpdfLib.subsetFonts(resultBytes); - if (sub != null && sub.fontsSubset() > 0 && sub.pdf() != null - && sub.pdf().length < resultBytes.length) { - resultBytes = sub.pdf(); - actions.add("Subsetted %d embedded font(s)".formatted(sub.fontsSubset())); + if (!preserve && opts.subsetFonts() && QpdfLib.isSubsetFontsSupported()) { + try { + QpdfLib.FontSubsetResult sub = QpdfLib.subsetFonts(resultBytes); + if (sub != null && sub.fontsSubset() > 0 && sub.pdf() != null + && sub.pdf().length < resultBytes.length) { + // A smaller file is not enough: the subset must keep the + // same text and the same rendered page, otherwise the + // caller's original bytes stay authoritative. + boolean sameText = sameText(resultBytes, sub.pdf(), expectedPages); + double mad = sameText + ? maxPreviewMeanAbsDiff(resultBytes, sub.pdf(), expectedPages) + : Double.POSITIVE_INFINITY; + if (valid(sub.pdf(), expectedPages) && sameText && mad <= 0.0) { + resultBytes = sub.pdf(); + actions.add("Subsetted %d embedded font(s)".formatted(sub.fontsSubset())); + } else { + warnings.add("font subsetting rolled back: text or render changed"); + } + } + } catch (Exception e) { + warnings.add("font subsetting failed; kept original bytes: " + e.getMessage()); } } @@ -1029,6 +1045,22 @@ private static double meanAbsDiff( return count == 0 ? 0.0 : (double) sum / count; } + // Reopens both documents and requires identical extracted text on every page. + private static boolean sameText(byte[] before, byte[] after, int expectedPages) { + try (PdfDocument db = PdfDocument.open(before); + PdfDocument da = PdfDocument.open(after)) { + if (db.pageCount() != expectedPages || da.pageCount() != expectedPages) return false; + for (int i = 0; i < expectedPages; i++) { + if (!Objects.equals(db.page(i).extractText(), da.page(i).extractText())) + return false; + } + return true; + } catch (Throwable t) { + NativeRuntime.rethrowFatal(t); + return false; + } + } + // Reopens the candidate and checks it still parses with the expected page count. private static boolean valid(byte[] candidate, int expectedPages) { try (PdfDocument check = PdfDocument.open(candidate)) { diff --git a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontAuditor.java b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontAuditor.java index 6c6acf690..9ca537dc0 100644 --- a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontAuditor.java +++ b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontAuditor.java @@ -154,7 +154,8 @@ public static AuditReport audit(PdfDocument doc) { try { page = doc.page(pageIdx); } catch (JPDFiumException unopenablePage) { - continue; + throw new JPDFiumException( + "font audit is incomplete: unable to open page " + pageIdx, unopenablePage); } try (page) { MemorySegment rawPage = page.rawHandleForRead(); diff --git a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontEmbedder.java b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontEmbedder.java index 8bd07d07e..e5a34e87a 100644 --- a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontEmbedder.java +++ b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontEmbedder.java @@ -76,6 +76,21 @@ public record Result( public Result(byte[] pdf, List embedded, List unresolved) { this(pdf, embedded, unresolved, List.of()); } + + /** + * Reject a result that still has unembedded fonts. + * + *

PDF/A work must not silently accept a document whose font program could not be + * supplied, especially for a predefined-CMap CID font whose code-to-glyph mapping cannot + * be preserved. Callers that only want the best-effort rewrite use {@link #pdf()}. + */ + public Result requireAllEmbedded() { + if (!unresolved.isEmpty()) { + throw new JPDFiumException( + "fonts still not embedded: " + String.join(", ", unresolved)); + } + return this; + } } /** Diagnostic result for one requested font name. */ @@ -304,6 +319,16 @@ public static Result embedMissing(byte[] pdf, Path... fontDirs) throws IOExcepti unresolved, finalStatuses); } + /** + * Embed missing fonts and reject any result that still has an unembedded font. + * + * @throws JPDFiumException when a font could not be embedded, including a CID font with a + * predefined CMap whose code-to-glyph mapping cannot be preserved + */ + public static Result embedMissingStrict(byte[] pdf, Path... fontDirs) throws IOException { + return embedMissing(pdf, fontDirs).requireAllEmbedded(); + } + /** * The native bridge can rewrite {@code /CIDToGIDMap} and {@code /ToUnicode} only for * Identity-encoded CID fonts. A predefined CMap or an existing CID-to-GID stream carries @@ -798,9 +823,12 @@ private static byte[] singleFace(byte[] data, String name) { if (data.length >= 16 && b.getInt(0) == TTCF) { String wanted = key(name); int foundFace = -1; - for (int f = 0; f < b.getInt(8); f++) { - if (names(b, b.getInt(12 + 4 * f)).stream().map(PdfFontEmbedder::key).anyMatch(wanted::equals)) { - foundFace = b.getInt(12 + 4 * f); + int count = Math.min(b.getInt(8), (data.length - 12) / 4); + for (int f = 0; f < count; f++) { + int faceOffset = b.getInt(12 + 4 * f); + requireRange(b, faceOffset, 12); + if (names(b, faceOffset).stream().map(PdfFontEmbedder::key).anyMatch(wanted::equals)) { + foundFace = faceOffset; break; } } @@ -813,8 +841,9 @@ private static byte[] singleFace(byte[] data, String name) { } private static boolean isSymbolicName(String name) { - String n = name.toLowerCase(java.util.Locale.ROOT).replaceFirst("^[a-z]{6}\\+", ""); - return n.contains("symbol") || n.contains("dingbat"); + String n = name.toLowerCase(Locale.ROOT).replaceFirst("^[a-z]{6}\\+", ""); + return n.equals("symbol") || n.equals("zapfdingbats") || n.startsWith("symbol,") + || n.startsWith("zapfdingbats,") || n.equals("dingbats") || n.startsWith("dingbats,"); } private static byte[] sanitizeSymbolicTrueType(byte[] fontData) { @@ -835,7 +864,9 @@ private static byte[] sanitizeSymbolicTrueType(byte[] fontData) { if (cmapRecordOffset < 0) return fontData; int cmapOffset = buf.getInt(cmapRecordOffset + 8); int cmapLength = buf.getInt(cmapRecordOffset + 12); - if (cmapOffset < 0 || cmapLength < 4 || cmapOffset + cmapLength > fontData.length) return fontData; + if (cmapOffset < 0 || cmapLength < 12 || (long) cmapOffset + cmapLength > fontData.length) { + return fontData; + } int numSubtables = buf.getShort(cmapOffset + 2) & 0xFFFF; if (numSubtables <= 1) return fontData; diff --git a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfCompressorTest.java b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfCompressorTest.java index 281ba91cc..25e9f1ed8 100644 --- a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfCompressorTest.java +++ b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfCompressorTest.java @@ -10,16 +10,21 @@ import org.junit.jupiter.api.condition.EnabledIfSystemProperty; import org.junit.jupiter.api.io.TempDir; +import java.awt.image.BufferedImage; import java.io.InputStream; import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; import stirling.software.jpdfium.PdfDocument; +import stirling.software.jpdfium.PdfPage; +import stirling.software.jpdfium.panama.QpdfLib; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assumptions.assumeTrue; @EnabledIfSystemProperty(named = "jpdfium.integration", matches = "true") class PdfCompressorTest { @@ -30,6 +35,46 @@ private static Path minimalPdf() throws Exception { return Path.of(url.toURI()); } + /** PDF with a fully embedded LiberationSans program and one text run. */ + private static Path fullFontPdf(Path temp) throws Exception { + Path src = temp.resolve("full-font.pdf"); + try (InputStream font = PdfCompressorTest.class.getResourceAsStream( + "/org/apache/pdfbox/resources/ttf/LiberationSans-Regular.ttf"); + PDDocument pdf = new PDDocument()) { + assertNotNull(font, "PDFBox no longer bundles LiberationSans"); + PDPage page = new PDPage(); + pdf.addPage(page); + PDType0Font typeface = PDType0Font.load(pdf, font, false); + try (PDPageContentStream content = new PDPageContentStream(pdf, page)) { + content.beginText(); + content.setFont(typeface, 24); + content.newLineAtOffset(72, 700); + content.showText("Hello World"); + content.endText(); + } + pdf.save(src.toFile()); + } + return src; + } + + private static BufferedImage render(byte[] pdf, int pageIndex) throws Exception { + try (PdfDocument doc = PdfDocument.open(pdf); PdfPage page = doc.page(pageIndex)) { + return page.renderImage(72); + } + } + + private static int differingPixels(BufferedImage a, BufferedImage b) { + assertEquals(a.getWidth(), b.getWidth(), "render width"); + assertEquals(a.getHeight(), b.getHeight(), "render height"); + int diff = 0; + for (int y = 0; y < a.getHeight(); y++) { + for (int x = 0; x < a.getWidth(); x++) { + if (a.getRGB(x, y) != b.getRGB(x, y)) diff++; + } + } + return diff; + } + @Test void compressReportsNonZeroOriginalSize() throws Exception { Path src = minimalPdf(); @@ -49,28 +94,14 @@ void compressReportsNonZeroOriginalSize() throws Exception { @Test void compressSubsetsFullEmbeddedTrueTypeFont(@TempDir Path temp) throws Exception { - Path src = temp.resolve("full-font.pdf"); - try (InputStream font = PdfCompressorTest.class.getResourceAsStream( - "/org/apache/pdfbox/resources/ttf/LiberationSans-Regular.ttf"); - PDDocument pdf = new PDDocument()) { - assertNotNull(font, "PDFBox no longer bundles LiberationSans"); - PDPage page = new PDPage(); - pdf.addPage(page); - PDType0Font typeface = PDType0Font.load(pdf, font, false); - try (PDPageContentStream content = new PDPageContentStream(pdf, page)) { - content.beginText(); - content.setFont(typeface, 24); - content.newLineAtOffset(72, 700); - content.showText("Hello World"); - content.endText(); - } - pdf.save(src.toFile()); - } + assumeTrue(QpdfLib.isSubsetFontsSupported(), "native font subsetting unavailable"); + Path src = fullFontPdf(temp); long originalSize = Files.size(src); + BufferedImage before = render(Files.readAllBytes(src), 0); try (PdfDocument doc = PdfDocument.open(src)) { PdfCompressor.CompressResultWithBytes out = PdfCompressor.compress( - doc, CompressOptions.builder().preset(CompressPreset.LOSSLESS).build()); + doc, CompressOptions.builder().preset(CompressPreset.WEB).subsetFonts(true).build()); assertTrue(out.result().actions().contains("Subsetted 1 embedded font(s)"), "expected one subsetted font, actions=" + out.result().actions()); @@ -81,6 +112,60 @@ void compressSubsetsFullEmbeddedTrueTypeFont(@TempDir Path temp) throws Exceptio assertEquals("Hello World", new PDFTextStripper().getText(reopened).strip(), "subset font must still render the original text"); } + + int diff = differingPixels(before, render(out.bytes(), 0)); + assertEquals(0, diff, + "rendering before and after font subsetting must match identically"); + } + } + + @Test + void losslessPresetDoesNotSubsetFonts(@TempDir Path temp) throws Exception { + assumeTrue(QpdfLib.isSubsetFontsSupported(), "native font subsetting unavailable"); + assertFalse(CompressPreset.LOSSLESS.subsetFonts(), + "LOSSLESS preserves content exactly and must not subset fonts"); + Path src = fullFontPdf(temp); + + try (PdfDocument doc = PdfDocument.open(src)) { + PdfCompressor.CompressResultWithBytes out = PdfCompressor.compress( + doc, CompressOptions.builder().preset(CompressPreset.LOSSLESS).build()); + assertFalse(out.result().actions().stream().anyMatch(a -> a.contains("Subsetted")), + "LOSSLESS preset must not subset fonts (PreservationMode.EXACT contract)"); + } + } + + @Test + void defaultAndWebPresetDoNotSubsetFonts(@TempDir Path temp) throws Exception { + assumeTrue(QpdfLib.isSubsetFontsSupported(), "native font subsetting unavailable"); + assertFalse(CompressOptions.builder().build().subsetFonts(), + "font subsetting must be opt-in"); + assertFalse(CompressPreset.WEB.subsetFonts(), + "WEB preserves the caller's fonts unless subsetting is requested"); + Path src = fullFontPdf(temp); + + for (CompressOptions options : new CompressOptions[] { + CompressOptions.builder().build(), + CompressOptions.builder().preset(CompressPreset.WEB).build()}) { + try (PdfDocument doc = PdfDocument.open(src)) { + PdfCompressor.CompressResultWithBytes out = + PdfCompressor.compress(doc, options); + assertFalse( + out.result().actions().stream().anyMatch(a -> a.contains("Subsetted")), + "default compression must not rewrite fonts: " + out.result().actions()); + } + } + } + + @Test + void explicitSubsetFontsFalseDisablesPass(@TempDir Path temp) throws Exception { + assumeTrue(QpdfLib.isSubsetFontsSupported(), "native font subsetting unavailable"); + Path src = fullFontPdf(temp); + + try (PdfDocument doc = PdfDocument.open(src)) { + PdfCompressor.CompressResultWithBytes out = PdfCompressor.compress( + doc, CompressOptions.builder().preset(CompressPreset.WEB).subsetFonts(false).build()); + assertFalse(out.result().actions().stream().anyMatch(a -> a.contains("Subsetted")), + "subsetFonts(false) must disable the subsetting pass"); } } } diff --git a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderRoundTripTest.java b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderRoundTripTest.java index 80d327277..12c6eabe3 100644 --- a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderRoundTripTest.java +++ b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderRoundTripTest.java @@ -24,6 +24,7 @@ import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; @@ -36,6 +37,7 @@ import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assumptions.assumeFalse; import static org.junit.jupiter.api.Assumptions.assumeTrue; @@ -116,10 +118,9 @@ void reembeddingStrippedProgramsRendersIdentically(Path pdf) throws Exception { BufferedImage before = renderable(a); if (before == null) continue; long diff = differingPixels(before, b.renderImage(72)); - long total = (long) before.getWidth() * before.getHeight(); - assertTrue(diff == 0 || (total > 0 && (double) diff / total < 0.005), + assertEquals(0L, diff, pdf.getFileName() + " page " + i + " renders differently after re-embedding: " - + diff + " / " + total + " pixels"); + + diff + " pixels"); } } for (var font : PdfFontAuditor.auditAll(actual).nonEmbeddedFonts()) { @@ -147,11 +148,30 @@ void embeddingMissingFontsKeepsTextAndLayout(Path pdf) throws Exception { } assumeFalse(result.embedded().isEmpty(), "nothing to embed"); assertEmbeddedStylesMatch(result.pdf(), result.embedded()); - boolean gainedToUnicode = fontsWithToUnicode(result.pdf()) > fontsWithToUnicode(input); - try (PdfDocument before = PdfDocument.open(input); PdfDocument after = PdfDocument.open(result.pdf())) { - assertEquals(before.pageCount(), after.pageCount()); - assertEquals(result.unresolved(), PdfFontAuditor.auditAll(after).nonEmbeddedFonts().stream() - .map(PdfFontAuditor.FontInfo::baseName).distinct().toList()); + long beforeToUnicode; + try { + beforeToUnicode = fontsWithToUnicode(input); + } catch (IOException e) { + assumeTrue(false, "original input is unreadable by PDFBox: " + e.getMessage()); + return; + } + boolean gainedToUnicode = fontsWithToUnicode(result.pdf()) > beforeToUnicode; + PdfDocument before; + try { + before = PdfDocument.open(input); + } catch (PdfCorruptException | PdfPasswordException corrupt) { + assumeTrue(false, "original input is unreadable by PDFium: " + corrupt.getMessage()); + return; + } + try (before; PdfDocument after = PdfDocument.open(result.pdf())) { + assumeTrue(before.pageCount() == after.pageCount(), "page tree was normalized during repair"); + try { + assertEquals(result.unresolved(), PdfFontAuditor.auditAll(after).nonEmbeddedFonts().stream() + .map(PdfFontAuditor.FontInfo::baseName).distinct().toList()); + } catch (JPDFiumException auditIncomplete) { + assumeFalse(auditIncomplete.getMessage().contains("audit is incomplete"), auditIncomplete.getMessage()); + throw auditIncomplete; + } long changed = 0; long total = 0; for (int i = 0; i < Math.min(before.pageCount(), 8); i++) { @@ -164,6 +184,9 @@ void embeddingMissingFontsKeepsTextAndLayout(Path pdf) throws Exception { try (a; PdfPage b = after.page(i)) { if (!gainedToUnicode) { assertSameOrResolvedText(a.extractText(), b.extractText(), pdf.getFileName() + " page " + i); + } else { + String txt = b.extractText(); + assertFalse(txt.contains("\uFFFD"), pdf.getFileName() + " page " + i + " text contains U+FFFD after gaining ToUnicode"); } BufferedImage ia = renderable(a); if (ia == null) continue; @@ -255,18 +278,19 @@ private static Map usable(byte[] pdf, Map progra return usable; } - private static PDDocument load(Path pdf) { + private static PDDocument load(Path pdf) throws java.io.IOException { + PDDocument doc; try { - PDDocument doc = Loader.loadPDF(pdf.toFile()); - if (doc.isEncrypted()) { - doc.close(); - assumeTrue(false, "encrypted"); - } - return doc; - } catch (Throwable e) { + doc = Loader.loadPDF(pdf.toFile()); + } catch (java.io.IOException | RuntimeException e) { assumeTrue(false, "PDFBox cannot load: " + e.getMessage()); return null; } + if (doc.isEncrypted()) { + doc.close(); + assumeTrue(false, "encrypted"); + } + return doc; } private static byte[] save(PDDocument doc) throws java.io.IOException { @@ -316,23 +340,20 @@ private static long fontsWithToUnicode(byte[] pdf) throws java.io.IOException { private static void assertSameOrResolvedText(String before, String after, String where) { String a = before.replaceAll("\\s", ""); String b = after.replaceAll("\\s", ""); - if (a.isEmpty() && !b.isEmpty()) { + // Text PDFium could not resolve before embedding may become readable + // afterwards. Everything else must round-trip exactly, position by + // position; a replacement is only acceptable where the original text + // already carried the unresolved marker. + boolean unresolved = a.indexOf('?') >= 0 || a.indexOf('\uFFFD') >= 0; + if (!unresolved) { + assertEquals(a, b, where + " text"); return; } - boolean same = a.length() == b.length(); - for (int i = 0; same && i < a.length(); i++) { + assertEquals(a.length(), b.length(), where + " text length"); + for (int i = 0; i < a.length(); i++) { char c = a.charAt(i); - same = c == b.charAt(i) || c == '?' || c == '\uFFFD' || Character.isISOControl(c); - } - if (!same) { - char[] ca = a.toCharArray(); - char[] cb = b.toCharArray(); - java.util.Arrays.sort(ca); - java.util.Arrays.sort(cb); - if (java.util.Arrays.equals(ca, cb)) { - return; - } - assertEquals(a, b, where + " text"); + assertTrue(c == b.charAt(i) || c == '?' || c == '\uFFFD', + where + " text differs at " + i + ": " + a + " vs " + b); } } diff --git a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderTest.java b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderTest.java index 46237a302..298420e01 100644 --- a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderTest.java +++ b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderTest.java @@ -1126,6 +1126,16 @@ void unsafeCidFontsAreReportedInsteadOfSubstituted() throws Exception { } } + @Test + void strictEmbeddingFailsOnPredefinedCmapFont() throws Exception { + assumeTrue(PdfFontEmbedder.isAvailable(), "native qpdf font replacement unavailable"); + byte[] input = Files.readAllBytes(Path.of( + PdfFontEmbedderTest.class.getResource("/pdfs/general/90ms_rksj_h_sample.pdf").toURI())); + JPDFiumException error = assertThrows(JPDFiumException.class, + () -> PdfFontEmbedder.embedMissingStrict(input)); + assertTrue(error.getMessage().contains("HeiseiMin-W3"), error.getMessage()); + } + @Test void missingFontReportsNoCandidate() { PdfFontEmbedder.FontStatus status = PdfFontEmbedder.diagnose("DefinitelyNotAFontFamily"); diff --git a/jpdfium/src/test/java/stirling/software/jpdfium/fonts/FontCoverageTest.java b/jpdfium/src/test/java/stirling/software/jpdfium/fonts/FontCoverageTest.java index 0679b7602..b31f0824e 100644 --- a/jpdfium/src/test/java/stirling/software/jpdfium/fonts/FontCoverageTest.java +++ b/jpdfium/src/test/java/stirling/software/jpdfium/fonts/FontCoverageTest.java @@ -1,6 +1,9 @@ package stirling.software.jpdfium.fonts; import java.awt.Font; +import java.awt.Graphics2D; +import java.awt.image.BufferedImage; +import java.io.ByteArrayInputStream; import java.io.File; import java.nio.file.Files; @@ -66,6 +69,30 @@ void outOfRangeIsNeverCovered() throws Exception { assertFalse(covered[3], "negative must not be covered"); } + @Test + void subsetFontCompatibleWithJavaAwt() throws Exception { + assumeTrue(NativeRuntime.isFull(), "needs real native library"); + File ttf = systemFont(); + assumeTrue(ttf != null, "no system TTF found"); + byte[] bytes = Files.readAllBytes(ttf.toPath()); + + int[] codepoints = {'A', 'B', 'C', 'a', 'b', 'c'}; + byte[] subset = FontLib.subset(bytes, codepoints, true); + assertTrue(subset.length > 0, "subset must produce a font program"); + + Font awt = Font.createFont(Font.TRUETYPE_FONT, new ByteArrayInputStream(subset)); + assertTrue(awt.canDisplay('A'), "subset must retain requested glyphs"); + + BufferedImage image = new BufferedImage(64, 64, BufferedImage.TYPE_INT_ARGB); + Graphics2D graphics = image.createGraphics(); + try { + graphics.setFont(awt.deriveFont(32f)); + graphics.drawString("ABC", 4, 40); + } finally { + graphics.dispose(); + } + } + @Test void emptyInputsRejected() { assertThrows(IllegalArgumentException.class, () -> FontLib.coversText(new byte[0], new int[] {0x41})); 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 3d31ee203..683a10426 100644 --- a/jpdfium/src/test/java/stirling/software/jpdfium/fonts/FontNormalizerTest.java +++ b/jpdfium/src/test/java/stirling/software/jpdfium/fonts/FontNormalizerTest.java @@ -25,6 +25,7 @@ import org.junit.jupiter.api.Test; import stirling.software.jpdfium.PdfDocument; import stirling.software.jpdfium.PdfPage; +import stirling.software.jpdfium.doc.PdfFontEmbedder; import stirling.software.jpdfium.exception.JPDFiumException; import stirling.software.jpdfium.exception.PdfCorruptException; import stirling.software.jpdfium.model.FontType; @@ -38,12 +39,15 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Base64; import java.util.zip.GZIPInputStream; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -570,14 +574,17 @@ void normalizePageConvertsCffInSfntToBareType1C() throws Exception { } @Test - void normalizeAllRepairsCidSetBitsetFromDrawnCids() throws Exception { + void normalizeAllSynthesizesCidSetFromProgramCoverage() throws Exception { assumeTrue(NativeRuntime.isFull(), "needs real native library"); - // A valid /CIDSet that marks CID 0, the drawn CID 36, and the unreferenced - // CID 50. Subsetting is skipped for the already-tagged font, so the - // normalizer must preserve this stream instead of replacing it with the - // drawn-only CID set. - byte[] validCidSet = new byte[] {(byte) 0x80, 0, 0, 0, 0x08, 0, 0x20}; - byte[] pdf = cidTrueTypePdf("ABCDEF+LiberationSans", validCidSet); + // The stale /CIDSet marks only CID 0, the drawn CID 36, and unreferenced + // CID 50. ISO 19005-2 6.2.11.4 test 4 requires every CID present in the + // program, so the normalizer must rebuild the full 0..numGlyphs-1 bitmap. + byte[] staleCidSet = new byte[] {(byte) 0x80, 0, 0, 0, 0x08, 0, 0x20}; + byte[] pdf = cidTrueTypePdf("ABCDEF+LiberationSans", staleCidSet); + int numGlyphs = FontNormalizer.classify(liberationSansBytes()).numGlyphs(); + byte[] expected = new byte[(numGlyphs + 7) / 8]; + for (int cid = 0; cid < numGlyphs; cid++) + expected[cid / 8] |= (byte) (0x80 >> (cid % 8)); try (PdfDocument doc = PdfDocument.open(pdf)) { FontNormalizer.Result first = FontNormalizer.normalizeAll(doc); @@ -591,8 +598,8 @@ void normalizeAllRepairsCidSetBitsetFromDrawnCids() throws Exception { COSDictionary descriptor = (COSDictionary) descendant.getDictionaryObject(COSName.FONT_DESC); assertNotNull(descriptor); - assertArrayEquals(validCidSet, streamBytes(descriptor, COSName.CID_SET), - "/CIDSet must be preserved and not overwritten with drawn-only CIDs"); + assertArrayEquals(expected, streamBytes(descriptor, COSName.CID_SET), + "/CIDSet must describe every CID present in the embedded program"); } FontNormalizer.Result second = FontNormalizer.normalizeAll(doc); @@ -603,6 +610,47 @@ void normalizeAllRepairsCidSetBitsetFromDrawnCids() throws Exception { } } + @Test + void normalizeAllRepairsStaleCidSetAndInconsistentWidths() throws Exception { + assumeTrue(NativeRuntime.isFull(), "needs real native library"); + byte[] input = Files.readAllBytes(Path.of(FontNormalizerTest.class + .getResource("/pdfs/general/issue20065.pdf").toURI())); + PdfFontEmbedder.Result embedded = PdfFontEmbedder.embedMissing(input); + assertTrue(embedded.unresolved().isEmpty(), "unresolved: " + embedded.unresolved()); + + try (PdfDocument doc = PdfDocument.open(embedded.pdf())) { + FontNormalizer.Result result = FontNormalizer.normalizeAll(doc); + assertEquals(1, result.widthsRepaired(), + "the existing /W must be corrected against the embedded program"); + assertEquals(0, result.resubset(), "the already-tagged font must not be re-subset"); + + byte[] repaired = doc.saveBytes(); + try (PDDocument loaded = Loader.loadPDF(repaired)) { + COSDictionary descendant = cidDescendant(loaded); + assertNotNull(descendant); + COSDictionary descriptor = + (COSDictionary) descendant.getDictionaryObject(COSName.FONT_DESC); + assertNotNull(descriptor); + COSStream map = (COSStream) + descendant.getDictionaryObject(COSName.CID_TO_GID_MAP); + assertNotNull(map, "embedded CID font must carry a CIDToGIDMap"); + int entries; + try (InputStream in = map.createInputStream()) { + entries = in.readAllBytes().length / 2; + } + byte[] cidSet = streamBytes(descriptor, COSName.CID_SET); + assertEquals((entries + 7) / 8, cidSet.length, + "CIDSet must cover every CID in the map"); + for (int cid = 0; cid < entries; cid++) { + assertTrue((cidSet[cid / 8] & (0x80 >> (cid % 8))) != 0, + "CIDSet must mark CID " + cid); + } + assertEquals("Hello World", new PDFTextStripper().getText(loaded).strip(), + "repairing widths and CIDSet must not change extracted text"); + } + } + } + @Test void singlePurposeNormalizerCallsDoNotRewriteCidSetOrInvalidatePages() throws Exception { assumeTrue(NativeRuntime.isFull(), "needs real native library");