From db4b85c02d5b4a08147ff4aa63fbd622089060d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Sz=C3=BCcs?= Date: Thu, 12 Nov 2026 04:10:16 +0100 Subject: [PATCH 1/4] Register the tagged-subset ABI in reachability metadata --- .../com.stirling/jpdfium/reachability-metadata.json | 11 +++++++++++ .../jpdfium/1.2.0/reachability-metadata.json | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/jpdfium/src/main/resources/META-INF/native-image/com.stirling/jpdfium/reachability-metadata.json b/jpdfium/src/main/resources/META-INF/native-image/com.stirling/jpdfium/reachability-metadata.json index e702d6d19..a09631924 100644 --- a/jpdfium/src/main/resources/META-INF/native-image/com.stirling/jpdfium/reachability-metadata.json +++ b/jpdfium/src/main/resources/META-INF/native-image/com.stirling/jpdfium/reachability-metadata.json @@ -252,6 +252,17 @@ "returnType": "jint", "parameterTypes": ["void*", "void*", "jint", "void*", "jint", "jint", "jint", "void*", "jint"] }, + { + "returnType": "jint", + "parameterTypes": [ + "void*", + "jlong", + "jint", + "void*", + "void*", + "void*" + ] + }, { "returnType": "jint", "parameterTypes": [ diff --git a/reachability-metadata/metadata/com.stirling/jpdfium/1.2.0/reachability-metadata.json b/reachability-metadata/metadata/com.stirling/jpdfium/1.2.0/reachability-metadata.json index e702d6d19..a09631924 100644 --- a/reachability-metadata/metadata/com.stirling/jpdfium/1.2.0/reachability-metadata.json +++ b/reachability-metadata/metadata/com.stirling/jpdfium/1.2.0/reachability-metadata.json @@ -252,6 +252,17 @@ "returnType": "jint", "parameterTypes": ["void*", "void*", "jint", "void*", "jint", "jint", "jint", "void*", "jint"] }, + { + "returnType": "jint", + "parameterTypes": [ + "void*", + "jlong", + "jint", + "void*", + "void*", + "void*" + ] + }, { "returnType": "jint", "parameterTypes": [ From 0e61383ccee90df162ebde3ff842c6a17a27786b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Sz=C3=BCcs?= Date: Thu, 12 Nov 2026 12:37:32 +0100 Subject: [PATCH 2/4] Support opt-in tagged font subsetting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Balázs Szücs --- .../software/jpdfium/doc/CompressOptions.java | 17 ++++++++++ .../software/jpdfium/doc/PdfCompressor.java | 9 +++-- .../software/jpdfium/panama/QpdfLib.java | 15 ++++++-- .../jpdfium/doc/PdfCompressorTest.java | 30 ++++++++++++++++ native/bridge/include/jpdfium.h | 4 +-- native/bridge/src/jpdfium_qpdf.cpp | 34 +++++++++++++------ 6 files changed, 91 insertions(+), 18 deletions(-) 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 2decda640..3d3b13837 100644 --- a/jpdfium/src/main/java/stirling/software/jpdfium/doc/CompressOptions.java +++ b/jpdfium/src/main/java/stirling/software/jpdfium/doc/CompressOptions.java @@ -37,6 +37,7 @@ public final class CompressOptions { private final int zopfliIterations; // Rust: zopfli iteration count private final int zopfliMaxInputBytes; // Rust: skip zopfli above this size, 0 = unlimited private final boolean subsetFonts; // Subset already-embedded TrueType/OpenType fonts + private final boolean reSubsetTaggedFonts; // Re-subset fonts that already carry a subset tag private CompressOptions(Builder b) { this.imageQuality = b.imageQuality; @@ -56,6 +57,7 @@ private CompressOptions(Builder b) { this.zopfliIterations = b.zopfliIterations; this.zopfliMaxInputBytes = b.zopfliMaxInputBytes; this.subsetFonts = b.subsetFonts; + this.reSubsetTaggedFonts = b.reSubsetTaggedFonts; } public int imageQuality() { return imageQuality; } @@ -143,6 +145,11 @@ private CompressOptions(Builder b) { * in after deciding their text/render contract. */ public boolean subsetFonts() { return subsetFonts; } + /** + * Whether to re-subset fonts that already carry a subset tag (e.g. {@code ABCDEF+FontName}). + * Default: {@code false}. + */ + public boolean reSubsetTaggedFonts() { return reSubsetTaggedFonts; } /** * @deprecated the option was removed; the lossless path no longer recompresses @@ -178,6 +185,7 @@ public static final class Builder { private int zopfliIterations = 15; private int zopfliMaxInputBytes = 0; private boolean subsetFonts = false; + private boolean reSubsetTaggedFonts = false; private Builder() {} @@ -295,6 +303,15 @@ public Builder subsetFonts(boolean v) { return this; } + /** + * Whether to re-subset fonts that already carry a subset tag (e.g. {@code ABCDEF+FontName}). + * Default {@code false}. + */ + public Builder reSubsetTaggedFonts(boolean v) { + this.reSubsetTaggedFonts = v; + return this; + } + /** * @deprecated removed option; no-op retained for binary compatibility. */ 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 adf69958e..df60936a4 100644 --- a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfCompressor.java +++ b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfCompressor.java @@ -389,7 +389,8 @@ && serializesIdentically(doc, sourceBytes, openStateBytes)) { // structural pass so qpdf recompresses the smaller font streams. if (!preserve && opts.subsetFonts() && QpdfLib.isSubsetFontsSupported()) { try { - QpdfLib.FontSubsetResult sub = QpdfLib.subsetFonts(resultBytes); + QpdfLib.FontSubsetResult sub = QpdfLib.subsetFonts(resultBytes, + opts.reSubsetTaggedFonts()); 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 @@ -1051,8 +1052,10 @@ private static boolean sameText(byte[] before, byte[] after, int expectedPages) 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; + try (PdfPage beforePage = db.page(i); PdfPage afterPage = da.page(i)) { + if (!Objects.equals(beforePage.extractText(), afterPage.extractText())) + return false; + } } return true; } catch (Throwable t) { diff --git a/jpdfium/src/main/java/stirling/software/jpdfium/panama/QpdfLib.java b/jpdfium/src/main/java/stirling/software/jpdfium/panama/QpdfLib.java index 74a382973..fd61c2039 100644 --- a/jpdfium/src/main/java/stirling/software/jpdfium/panama/QpdfLib.java +++ b/jpdfium/src/main/java/stirling/software/jpdfium/panama/QpdfLib.java @@ -138,7 +138,7 @@ public static int maxConcurrency() { private static final MethodHandle SUBSET_FONTS_HANDLE = Symbols.downcallOptionalUnguarded( "jpdfium_qpdf_subset_fonts", - FunctionDescriptor.of(JAVA_INT, ADDRESS, JAVA_LONG, ADDRESS, ADDRESS, ADDRESS)); + FunctionDescriptor.of(JAVA_INT, ADDRESS, JAVA_LONG, JAVA_INT, ADDRESS, ADDRESS, ADDRESS)); /** Result of an in-place embedded-font subsetting pass. */ public record FontSubsetResult(byte[] pdf, int fontsSubset) {} @@ -155,6 +155,17 @@ public static boolean isSubsetFontsSupported() { * {@code pdf == null} and {@code fontsSubset == 0} when nothing needed subsetting */ public static FontSubsetResult subsetFonts(byte[] input) { + return subsetFonts(input, false); + } + + /** + * Subset already-embedded TrueType/OpenType fonts in-place. + * + * @param subsetTagged whether to re-subset fonts that already carry a subset prefix + * @return the rewritten PDF and the number of subsetted fonts, or a result with + * {@code pdf == null} and {@code fontsSubset == 0} when nothing needed subsetting + */ + public static FontSubsetResult subsetFonts(byte[] input, boolean subsetTagged) { if (SUBSET_FONTS_HANDLE == null || input == null || input.length == 0) { return new FontSubsetResult(null, 0); } @@ -162,7 +173,7 @@ public static FontSubsetResult subsetFonts(byte[] input) { try (QpdfCall call = new QpdfCall()) { MemorySegment count = call.arena.allocate(JAVA_INT); int rc = (int) SUBSET_FONTS_HANDLE.invokeExact(call.copyBytes(input), - (long) input.length, call.outPtr, call.outLen, count); + (long) input.length, subsetTagged ? 1 : 0, call.outPtr, call.outLen, count); int fonts = count.get(JAVA_INT, 0); if (rc == 1) return new FontSubsetResult(null, 0); if (rc == 0) return new FontSubsetResult(call.copyAndFree("qpdfSubsetFonts"), fonts); 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 789a37a0d..c817d7bcb 100644 --- a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfCompressorTest.java +++ b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfCompressorTest.java @@ -219,4 +219,34 @@ void explicitSubsetFontsFalseDisablesPass(@TempDir Path temp) throws Exception { "subsetFonts(false) must disable the subsetting pass"); } } + + @Test + void taggedFontSubsettingIsStrictlyOptIn(@TempDir Path temp) throws Exception { + assumeTrue(QpdfLib.isSubsetFontsSupported(), "native font subsetting unavailable"); + assertFalse(CompressOptions.builder().build().reSubsetTaggedFonts(), + "tagged font re-subsetting must default to false"); + Path src = fullFontPdf(temp); + byte[] firstPass; + try (PdfDocument doc = PdfDocument.open(src)) { + PdfCompressor.CompressResultWithBytes out = PdfCompressor.compress( + doc, CompressOptions.builder().preset(CompressPreset.WEB).subsetFonts(true).build()); + assertTrue(out.result().actions().stream().anyMatch(a -> a.contains("Subsetted")), + "first pass must subset untagged font"); + firstPass = out.bytes(); + } + + try (PdfDocument doc = PdfDocument.open(firstPass)) { + PdfCompressor.CompressResultWithBytes out = PdfCompressor.compress( + doc, CompressOptions.builder().preset(CompressPreset.WEB).subsetFonts(true).build()); + assertFalse(out.result().actions().stream().anyMatch(a -> a.contains("Subsetted")), + "already-tagged font must not be re-subsetted by default"); + } + + try (PdfDocument doc = PdfDocument.open(firstPass)) { + PdfCompressor.CompressResultWithBytes out = PdfCompressor.compress( + doc, CompressOptions.builder().preset(CompressPreset.WEB) + .subsetFonts(true).reSubsetTaggedFonts(true).build()); + assertNotNull(out.bytes()); + } + } } diff --git a/native/bridge/include/jpdfium.h b/native/bridge/include/jpdfium.h index 94a5db625..3b97449e0 100644 --- a/native/bridge/include/jpdfium.h +++ b/native/bridge/include/jpdfium.h @@ -682,8 +682,8 @@ JPDFIUM_EXPORT int32_t jpdfium_qpdf_embed_fonts_file(const char* in_path, const // RETAIN_GIDS. Returns 0 when at least one font was subsetted (*output populated), // 1 when no font needed subsetting (*output is NULL), negative on error. JPDFIUM_EXPORT int32_t jpdfium_qpdf_subset_fonts(const uint8_t* input, int64_t inputLen, - uint8_t** output, int64_t* outputLen, - int32_t* fontsSubset); + int32_t subsetTagged, uint8_t** output, + int64_t* outputLen, int32_t* fontsSubset); // In-process qpdf structural sanitization (FFM, no CLI). // Scrubs metadata/info/structure, JavaScript actions, embedded files, AcroForm diff --git a/native/bridge/src/jpdfium_qpdf.cpp b/native/bridge/src/jpdfium_qpdf.cpp index 43b3e508f..2e78534f7 100644 --- a/native/bridge/src/jpdfium_qpdf.cpp +++ b/native/bridge/src/jpdfium_qpdf.cpp @@ -614,7 +614,10 @@ void clampPatternSteps(QPDF& pdf) { } } -int subsetEmbeddedFonts(QPDF& qpdf); +// Subset every embedded TrueType/OpenType program that the document references. +// When subsetTagged is false, fonts that already carry a subset tag are skipped. +// Defined below with the font helpers. +int subsetEmbeddedFonts(QPDF& qpdf, bool subsetTagged = false); int32_t applyDocumentOptions(QPDF& pdf, const OptimizeOptions& options) { clampPatternSteps(pdf); if (options.flags & JPDFIUM_QPDF_QDF) { @@ -3466,7 +3469,8 @@ bool subsetEmbeddedSimpleFont(QPDF& qpdf, FT_Library lib, QPDFObjectHandle& font // Inspect a single font resource and subset its embedded SFNT stream when safe. bool subsetEmbeddedFontResource(QPDF& qpdf, FT_Library lib, QPDFObjectHandle& font, std::map const& descriptorUseCount, - std::map const& streamUseCount) { + std::map const& streamUseCount, + bool subsetTagged = false) { auto descendants = font.getKey("/DescendantFonts"); bool cid = descendants.isArray() && descendants.getArrayNItems() > 0 && descendants.getArrayItem(0).isDictionary(); @@ -3474,7 +3478,8 @@ bool subsetEmbeddedFontResource(QPDF& qpdf, FT_Library lib, QPDFObjectHandle& fo auto descriptor = target.getKey("/FontDescriptor"); if (!descriptor.isDictionary()) return false; - // Skip fonts that are already subset-tagged or share a descriptor/stream with another font. + // Skip fonts that are already subset-tagged (unless enabled) or share a + // descriptor/stream with another font. auto baseFont = target.getKey("/BaseFont"); if (!baseFont.isName()) baseFont = font.getKey("/BaseFont"); auto fontBaseFont = font.getKey("/BaseFont"); @@ -3482,7 +3487,12 @@ bool subsetEmbeddedFontResource(QPDF& qpdf, FT_Library lib, QPDFObjectHandle& fo baseFont.isName() ? stripPdfNamePrefix(baseFont.getName()) : std::string(); std::string parentName = fontBaseFont.isName() ? stripPdfNamePrefix(fontBaseFont.getName()) : std::string(); - if (hasSubsetTag(baseName) || hasSubsetTag(parentName)) return false; + if (!subsetTagged) { + if (hasSubsetTag(baseName) || hasSubsetTag(parentName)) return false; + } else { + if (hasSubsetTag(baseName)) baseName = baseName.substr(7); + if (hasSubsetTag(parentName)) parentName = parentName.substr(7); + } if (descriptor.isIndirect()) { auto it = descriptorUseCount.find(descriptor.getObjGen()); if (it != descriptorUseCount.end() && it->second > 1) return false; @@ -3527,9 +3537,10 @@ bool subsetEmbeddedFontResource(QPDF& qpdf, FT_Library lib, QPDFObjectHandle& fo : subsetEmbeddedSimpleFont(qpdf, lib, font, target, descriptor, program, baseName); } -// Subset every embedded TrueType/OpenType program that the document references -// and that is not already subset-tagged. Returns the number of fonts rewritten. -int subsetEmbeddedFonts(QPDF& qpdf) { +// Subset every embedded TrueType/OpenType program that the document references. +// When subsetTagged is false, fonts that already carry a subset tag are skipped. +// Returns the number of fonts rewritten. +int subsetEmbeddedFonts(QPDF& qpdf, bool subsetTagged) { auto hasSignatureValue = [](QPDFObjectHandle const& field) { if (!field.isDictionary()) return false; auto value = field.getKey("/V"); @@ -3661,7 +3672,8 @@ int subsetEmbeddedFonts(QPDF& qpdf) { int subsetCount = 0; for (auto& font : uniqueFonts) { try { - if (subsetEmbeddedFontResource(qpdf, lib, font, descriptorUseCount, streamUseCount)) + if (subsetEmbeddedFontResource(qpdf, lib, font, descriptorUseCount, streamUseCount, + subsetTagged)) ++subsetCount; } catch (...) { // Isolate failure to this specific font and continue @@ -4600,8 +4612,8 @@ JPDFIUM_EXPORT int32_t jpdfium_qpdf_embed_fonts_file(const char* in_path, const } JPDFIUM_EXPORT int32_t jpdfium_qpdf_subset_fonts(const uint8_t* input, int64_t inputLen, - uint8_t** output, int64_t* outputLen, - int32_t* fontsSubset) { + int32_t subsetTagged, uint8_t** output, + int64_t* outputLen, int32_t* fontsSubset) { if (!input || inputLen <= 0 || !output || !outputLen || !fontsSubset) return -1; *output = nullptr; *outputLen = 0; @@ -4610,7 +4622,7 @@ JPDFIUM_EXPORT int32_t jpdfium_qpdf_subset_fonts(const uint8_t* input, int64_t i auto qpdf = QPDF::create(); qpdf->processMemoryFile("subset-in", reinterpret_cast(input), static_cast(inputLen)); - int count = subsetEmbeddedFonts(*qpdf); + int count = subsetEmbeddedFonts(*qpdf, subsetTagged != 0); *fontsSubset = count; if (count <= 0) return count == 0 ? 1 : -1; From 42996259084c7cadb7f87872599967a8f051968d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Sz=C3=BCcs?= Date: Thu, 12 Nov 2026 15:18:34 +0100 Subject: [PATCH 3/4] Bump the ABI for tagged subsetting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Balázs Szücs --- .../main/java/stirling/software/jpdfium/panama/JpdfiumLib.java | 2 +- native/bridge/include/jpdfium.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/jpdfium/src/main/java/stirling/software/jpdfium/panama/JpdfiumLib.java b/jpdfium/src/main/java/stirling/software/jpdfium/panama/JpdfiumLib.java index 894b41356..d69a63dc2 100644 --- a/jpdfium/src/main/java/stirling/software/jpdfium/panama/JpdfiumLib.java +++ b/jpdfium/src/main/java/stirling/software/jpdfium/panama/JpdfiumLib.java @@ -155,7 +155,7 @@ public static long abiQuery(int query) { } /** Expected bridge ABI version (mirrors {@code JPDFIUM_ABI_VERSION}). */ - public static final int EXPECTED_ABI_VERSION = 2; + public static final int EXPECTED_ABI_VERSION = 3; /** Expected native pointer width: all supported targets are 64-bit. */ public static final long EXPECTED_PTR_SIZE = 8; /** Expected {@code sizeof(FS_RECTF)}: four floats, no padding. */ diff --git a/native/bridge/include/jpdfium.h b/native/bridge/include/jpdfium.h index 3b97449e0..778401104 100644 --- a/native/bridge/include/jpdfium.h +++ b/native/bridge/include/jpdfium.h @@ -53,7 +53,7 @@ extern "C" { // combinations handshake on JPDFIUM_ABI_VERSION and verify platform-sensitive // layout facts (pointer width, PDFium struct geometry) instead of trusting // handwritten FFM patches. Bump the version on any ABI-breaking change. -#define JPDFIUM_ABI_VERSION (2) +#define JPDFIUM_ABI_VERSION (3) // Query ids for jpdfium_abi_query. Unknown ids return -1. #define JPDFIUM_ABI_QUERY_PTR_SIZE (0) From c71efbac25fe7d6b95b6d29bd6200bee45bad98e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Sz=C3=BCcs?= Date: Thu, 12 Nov 2026 15:18:34 +0100 Subject: [PATCH 4/4] Test opt-in tagging with a full font program MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Balázs Szücs --- .../jpdfium/doc/PdfCompressorTest.java | 60 +++++++++++++++---- 1 file changed, 47 insertions(+), 13 deletions(-) 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 c817d7bcb..56c8fa73a 100644 --- a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfCompressorTest.java +++ b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfCompressorTest.java @@ -1,6 +1,10 @@ package stirling.software.jpdfium.doc; import org.apache.pdfbox.Loader; +import org.apache.pdfbox.cos.COSArray; +import org.apache.pdfbox.cos.COSDictionary; +import org.apache.pdfbox.cos.COSName; +import org.apache.pdfbox.cos.COSObject; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageContentStream; @@ -59,6 +63,32 @@ private static Path fullFontPdf(Path temp) throws Exception { return src; } + + /** Full embedded program relabelled as a producer subset, so it has removable glyphs. */ + private static Path taggedFullFontPdf(Path temp) throws Exception { + Path tagged = temp.resolve("tagged-full-font.pdf"); + try (PDDocument pdf = Loader.loadPDF(fullFontPdf(temp).toFile())) { + for (COSObject object : pdf.getDocument().getObjectsByType(COSName.FONT)) { + if (!(object.getObject() instanceof COSDictionary font)) continue; + COSDictionary target = font; + if (font.getDictionaryObject(COSName.DESCENDANT_FONTS) instanceof COSArray kids + && kids.size() > 0 + && kids.getObject(0) instanceof COSDictionary kid) { + target = kid; + } + COSName base = target.getCOSName(COSName.BASE_FONT); + if (base == null || base.getName().startsWith("ABCDEF+")) continue; + COSName taggedName = COSName.getPDFName("ABCDEF+" + base.getName()); + target.setItem(COSName.BASE_FONT, taggedName); + font.setItem(COSName.BASE_FONT, taggedName); + COSDictionary descriptor = target.getCOSDictionary(COSName.FONT_DESC); + if (descriptor != null) descriptor.setItem(COSName.FONT_NAME, taggedName); + } + pdf.save(tagged.toFile()); + } + return tagged; + } + 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); @@ -225,28 +255,32 @@ void taggedFontSubsettingIsStrictlyOptIn(@TempDir Path temp) throws Exception { assumeTrue(QpdfLib.isSubsetFontsSupported(), "native font subsetting unavailable"); assertFalse(CompressOptions.builder().build().reSubsetTaggedFonts(), "tagged font re-subsetting must default to false"); - Path src = fullFontPdf(temp); - byte[] firstPass; - try (PdfDocument doc = PdfDocument.open(src)) { - PdfCompressor.CompressResultWithBytes out = PdfCompressor.compress( - doc, CompressOptions.builder().preset(CompressPreset.WEB).subsetFonts(true).build()); - assertTrue(out.result().actions().stream().anyMatch(a -> a.contains("Subsetted")), - "first pass must subset untagged font"); - firstPass = out.bytes(); - } + Path src = taggedFullFontPdf(temp); + long fullSize = Files.size(src); + BufferedImage before = render(Files.readAllBytes(src), 0); - try (PdfDocument doc = PdfDocument.open(firstPass)) { + try (PdfDocument doc = PdfDocument.open(src)) { PdfCompressor.CompressResultWithBytes out = PdfCompressor.compress( doc, CompressOptions.builder().preset(CompressPreset.WEB).subsetFonts(true).build()); assertFalse(out.result().actions().stream().anyMatch(a -> a.contains("Subsetted")), - "already-tagged font must not be re-subsetted by default"); + "a tagged font must not be re-subsetted by default"); } - try (PdfDocument doc = PdfDocument.open(firstPass)) { + try (PdfDocument doc = PdfDocument.open(src)) { PdfCompressor.CompressResultWithBytes out = PdfCompressor.compress( doc, CompressOptions.builder().preset(CompressPreset.WEB) .subsetFonts(true).reSubsetTaggedFonts(true).build()); - assertNotNull(out.bytes()); + assertTrue(out.result().actions().contains("Subsetted 1 embedded font(s)"), + "opt-in must re-subset the tagged full program, actions=" + out.result().actions()); + assertTrue(out.bytes().length < fullSize / 2, + "re-subsetting must shrink the full program: " + out.bytes().length + + " vs " + fullSize); + try (PDDocument reopened = Loader.loadPDF(out.bytes())) { + assertEquals("Hello World", new PDFTextStripper().getText(reopened).strip(), + "re-subset font must keep the original text"); + } + assertEquals(0, differingPixels(before, render(out.bytes(), 0)), + "re-subset font must render identically"); } } }