Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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; }
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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() {}

Expand Down Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
Expand All @@ -155,14 +155,25 @@ 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);
}
Semaphore held = acquireSlotOrThrow("font subsetting");
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -219,4 +249,38 @@ 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 = taggedFullFontPdf(temp);
long fullSize = 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.WEB).subsetFonts(true).build());
assertFalse(out.result().actions().stream().anyMatch(a -> a.contains("Subsetted")),
"a tagged font must not be re-subsetted by default");
}

try (PdfDocument doc = PdfDocument.open(src)) {
PdfCompressor.CompressResultWithBytes out = PdfCompressor.compress(
doc, CompressOptions.builder().preset(CompressPreset.WEB)
.subsetFonts(true).reSubsetTaggedFonts(true).build());
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");
}
}
}
6 changes: 3 additions & 3 deletions native/bridge/include/jpdfium.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
34 changes: 23 additions & 11 deletions native/bridge/src/jpdfium_qpdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,10 @@
}
}

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) {
Expand Down Expand Up @@ -3466,23 +3469,30 @@
// Inspect a single font resource and subset its embedded SFNT stream when safe.
bool subsetEmbeddedFontResource(QPDF& qpdf, FT_Library lib, QPDFObjectHandle& font,
std::map<QPDFObjGen, int> const& descriptorUseCount,
std::map<QPDFObjGen, int> const& streamUseCount) {
std::map<QPDFObjGen, int> const& streamUseCount,
bool subsetTagged = false) {
auto descendants = font.getKey("/DescendantFonts");
bool cid = descendants.isArray() && descendants.getArrayNItems() > 0 &&
descendants.getArrayItem(0).isDictionary();
auto target = cid ? descendants.getArrayItem(0) : font;
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");
std::string baseName =
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;
Expand Down Expand Up @@ -3527,148 +3537,150 @@
: 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");
return field.hasKey("/ByteRange") || (value.isDictionary() && value.hasKey("/ByteRange"));
};

// Collect top-level font dictionaries across all objects and resource dictionaries.
std::vector<QPDFObjectHandle> fonts;
auto collectFromResources = [&](QPDFObjectHandle const& res) {
auto fontDict = res.isDictionary() ? res.getKey("/Font") : QPDFObjectHandle();
if (!fontDict.isDictionary()) return;
for (auto const& [name, font] : fontDict.getDictAsMap())
if (font.isDictionary()) fonts.push_back(font);
};
for (auto& object : qpdf.getAllObjects()) {
auto dict = object.isStream() ? object.getDict() : object;
if (!dict.isDictionary()) continue;
if (hasSignatureValue(dict)) return 0;
auto type = dict.getKey("/Type");
auto subtypeName = dict.getKey("/Subtype");
bool cidDescendant = subtypeName.isName() && (subtypeName.getName() == "/CIDFontType0" ||
subtypeName.getName() == "/CIDFontType2");
if (type.isName() && type.getName() == "/Font" && !cidDescendant) fonts.push_back(dict);
collectFromResources(dict.getKey("/Resources"));
}

// Refuse to modify signed documents discovered via the AcroForm field tree.
auto acroForm = qpdf.getRoot().getKey("/AcroForm");
std::vector<QPDFObjectHandle> pendingFields{acroForm};
std::set<QPDFObjGen> visitedFields;
while (!pendingFields.empty()) {
QPDFObjectHandle field = pendingFields.back();
pendingFields.pop_back();
if (!field.isDictionary()) continue;
if (field.isIndirect() && !visitedFields.insert(field.getObjGen()).second) continue;
if (hasSignatureValue(field)) return 0;
for (char const* key : {"/Fields", "/Kids"}) {
QPDFObjectHandle children = field.getKey(key);
if (!children.isArray()) continue;
for (auto& child : children.getArrayAsVector()) pendingFields.push_back(child);
}
}
// Fonts reachable from /AcroForm /DR may be used for interactive text entry
// in form fields that never reference them from a page /Resources. They are
// never subset candidates: collect them separately so their descriptors and
// programs can be marked as shared (see below) and excluded from uniqueFonts.
std::vector<QPDFObjectHandle> formFonts;
std::set<QPDFObjGen> visitedFormResources;
std::vector<QPDFObjectHandle> pendingFormResources{
acroForm.isDictionary() ? acroForm.getKey("/DR") : QPDFObjectHandle()};
while (!pendingFormResources.empty()) {
QPDFObjectHandle res = pendingFormResources.back();
pendingFormResources.pop_back();
if (!res.isDictionary()) continue;
if (res.isIndirect() && !visitedFormResources.insert(res.getObjGen()).second) continue;
auto fontDict = res.getKey("/Font");
if (fontDict.isDictionary()) {
for (auto const& [name, font] : fontDict.getDictAsMap()) {
if (!font.isDictionary()) continue;
formFonts.push_back(font);
auto descendants = font.getKey("/DescendantFonts");
if (!descendants.isArray()) continue;
for (auto& descendant : descendants.getArrayAsVector())
if (descendant.isDictionary()) formFonts.push_back(descendant);
}
}
auto xobjects = res.getKey("/XObject");
if (xobjects.isDictionary()) {
for (auto const& [name, xobject] : xobjects.getDictAsMap()) {
if (!xobject.isStream()) continue;
pendingFormResources.push_back(xobject.getDict().getKey("/Resources"));
}
}
}
std::set<QPDFObjGen> formFontObjects;
for (auto const& font : formFonts)
if (font.isIndirect()) formFontObjects.insert(font.getObjGen());

// Deduplicate indirect font dictionaries and count shared descriptors/streams.
std::set<QPDFObjGen> seenFonts;
std::vector<QPDFObjectHandle> uniqueFonts;
for (auto const& font : fonts) {
if (font.isIndirect() && !seenFonts.insert(font.getObjGen()).second) continue;
if (font.isIndirect() && formFontObjects.contains(font.getObjGen())) continue;
uniqueFonts.push_back(font);
}

std::map<QPDFObjGen, int> descriptorUseCount;
std::map<QPDFObjGen, int> streamUseCount;
for (auto const& font : uniqueFonts) {
auto descendants = font.getKey("/DescendantFonts");
bool cid = descendants.isArray() && descendants.getArrayNItems() > 0 &&
descendants.getArrayItem(0).isDictionary();
auto descriptor = (cid ? descendants.getArrayItem(0) : font).getKey("/FontDescriptor");
if (descriptor.isIndirect()) ++descriptorUseCount[descriptor.getObjGen()];
if (!descriptor.isDictionary()) continue;
auto stream = descriptor.getKey("/FontFile2");
if (!stream.isStream()) {
auto ff3 = descriptor.getKey("/FontFile3");
if (ff3.isStream() && ff3.getDict().getKey("/Subtype").isNameAndEquals("/OpenType"))
stream = ff3;
}
if (stream.isIndirect()) ++streamUseCount[stream.getObjGen()];
}

// Sentinel counts keep AcroForm /DR fonts, descriptors and programs out of
// subsetting even when a page /Resources also references them. Subsetting
// form fonts would break interactive entry for characters not yet drawn.
for (auto const& font : formFonts) {
auto descriptor = font.getKey("/FontDescriptor");
if (!descriptor.isDictionary()) continue;
if (descriptor.isIndirect()) descriptorUseCount[descriptor.getObjGen()] = 999999;
for (char const* key : {"/FontFile", "/FontFile2", "/FontFile3"}) {
auto stream = descriptor.getKey(key);
if (stream.isStream() && stream.isIndirect())
streamUseCount[stream.getObjGen()] = 999999;
}
}

FT_Library lib = nullptr;
struct LibraryGuard {
FT_Library& library;
~LibraryGuard() {
if (library) FT_Done_FreeType(library);
}
} libraryGuard{lib};
if (FT_Init_FreeType(&lib)) return 0;

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
}
}
return subsetCount;
}

Check notice

Code scanning / CodeQL

Block with too many statements Note

Block with too many statements (4 complex statements in the block).

QPDFObjectHandle newDescriptor(QPDF& qpdf, QPDFObjectHandle font, FT_Face face, bool symbolic) {
auto scaled = [&](long v) {
Expand Down Expand Up @@ -4600,8 +4612,8 @@
}

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;
Expand All @@ -4610,7 +4622,7 @@
auto qpdf = QPDF::create();
qpdf->processMemoryFile("subset-in", reinterpret_cast<const char*>(input),
static_cast<size_t>(inputLen));
int count = subsetEmbeddedFonts(*qpdf);
int count = subsetEmbeddedFonts(*qpdf, subsetTagged != 0);
*fontsSubset = count;
if (count <= 0) return count == 0 ? 1 : -1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
Loading