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 @@ -36,6 +36,7 @@ public final class CompressOptions {
private final boolean useZopfliDeflate; // Rust: zopfli post-processing pass
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 CompressOptions(Builder b) {
this.imageQuality = b.imageQuality;
Expand All @@ -54,6 +55,7 @@ private CompressOptions(Builder b) {
this.useZopfliDeflate = b.useZopfliDeflate;
this.zopfliIterations = b.zopfliIterations;
this.zopfliMaxInputBytes = b.zopfliMaxInputBytes;
this.subsetFonts = b.subsetFonts;
}

public int imageQuality() { return imageQuality; }
Expand Down Expand Up @@ -135,6 +137,11 @@ private CompressOptions(Builder b) {
* files. Only used when {@link #useZopfliDeflate()} is {@code true}.
*/
public int zopfliMaxInputBytes() { return zopfliMaxInputBytes; }
/**
* Whether to subset already-embedded TrueType/OpenType fonts in-place.
* Default: {@code true}.
Comment thread
balazs-szucs marked this conversation as resolved.
Resolved
*/
public boolean subsetFonts() { return subsetFonts; }

/**
* @deprecated the option was removed; the lossless path no longer recompresses
Expand Down Expand Up @@ -169,6 +176,7 @@ public static final class Builder {
private boolean useZopfliDeflate = false;
private int zopfliIterations = 15;
private int zopfliMaxInputBytes = 0;
private boolean subsetFonts = true;
Comment thread
balazs-szucs marked this conversation as resolved.
Resolved

private Builder() {}

Expand Down Expand Up @@ -231,6 +239,7 @@ public Builder preset(CompressPreset preset) {
this.removeMetadata = preset.removeMetadata();
this.preservationMode = preset.preservationMode();
this.useZopfliDeflate = preset.useZopfliDeflate();
this.subsetFonts = preset.subsetFonts();
return this;
}

Expand Down Expand Up @@ -276,6 +285,12 @@ public Builder zopfliMaxInputBytes(int bytes) {
return this;
}

/** Subset already-embedded TrueType/OpenType fonts. Default {@code true}. */
public Builder subsetFonts(boolean v) {
this.subsetFonts = 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 @@ -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, PreservationMode.VISUALLY_CONSTRAINED),
WEB(75, 150, true, true, true, false, true, PreservationMode.VISUALLY_CONSTRAINED),

/**
* Screen/email: lower quality, maximum compression.
*/
SCREEN(60, 96, true, true, true, false, PreservationMode.SCAN),
SCREEN(60, 96, true, true, true, false, true, PreservationMode.SCAN),

/**
* Print-ready: high quality, structural optimization only.
*/
PRINT(90, 300, false, true, false, false, PreservationMode.VISUALLY_CONSTRAINED),
PRINT(90, 300, false, true, false, false, true, PreservationMode.VISUALLY_CONSTRAINED),

/**
* Lossless maximum: aggressive structural optimization and stream
Expand All @@ -31,30 +31,32 @@ 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, PreservationMode.EXACT),
LOSSLESS(-1, -1, false, true, false, false, true, PreservationMode.EXACT),

/**
* Maximum compression: aggressive image optimization and structural compression.
*/
MAXIMUM(50, 96, true, true, true, false, PreservationMode.DESTRUCTIVE);
MAXIMUM(50, 96, true, true, true, false, true, PreservationMode.DESTRUCTIVE);

private final int imageQuality;
private final int maxImageDpi;
private final boolean convertPngToJpeg;
private final boolean optimizeStreams;
private final boolean removeMetadata;
private final boolean useZopfliDeflate;
private final boolean subsetFonts;
private final PreservationMode preservationMode;

CompressPreset(int imageQuality, int maxImageDpi, boolean convertPngToJpeg,
boolean optimizeStreams, boolean removeMetadata, boolean useZopfliDeflate,
PreservationMode preservationMode) {
boolean subsetFonts, PreservationMode preservationMode) {
this.imageQuality = imageQuality;
this.maxImageDpi = maxImageDpi;
this.convertPngToJpeg = convertPngToJpeg;
this.optimizeStreams = optimizeStreams;
this.removeMetadata = removeMetadata;
this.useZopfliDeflate = useZopfliDeflate;
this.subsetFonts = subsetFonts;
this.preservationMode = preservationMode;
}

Expand All @@ -64,6 +66,7 @@ public enum CompressPreset {
public boolean optimizeStreams() { return optimizeStreams; }
public boolean removeMetadata() { return removeMetadata; }
public boolean useZopfliDeflate() { return useZopfliDeflate; }
public boolean subsetFonts() { return subsetFonts; }
public PreservationMode preservationMode() { return preservationMode; }

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import stirling.software.jpdfium.PdfDocument;
import stirling.software.jpdfium.PdfPage;
import stirling.software.jpdfium.panama.NativeRuntime;
import stirling.software.jpdfium.panama.QpdfLib;
import stirling.software.jpdfium.panama.RustBridgeBindings;

import java.awt.image.BufferedImage;
Expand Down Expand Up @@ -383,7 +384,18 @@ && serializesIdentically(doc, sourceBytes, openStateBytes)) {
}
boolean streamsOptimized = false;

// 5. qpdf pass: recompress streams, generate object/xref streams.
// 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()));
}
}

// 6. qpdf pass: recompress streams, generate object/xref streams.
// Only adopt when strictly smaller so small PDFs are never inflated.
if (!signed && opts.optimizeStreams()) {
// PDF/A-1 forbids object streams and cross-reference streams, so a
Expand Down Expand Up @@ -432,7 +444,7 @@ && serializesIdentically(doc, sourceBytes, openStateBytes)) {
}
}

// 6. Rust/zopfli post-processing pass (optional opt-in).
// 7. Rust/zopfli post-processing pass (optional opt-in).
if (!signed && opts.useZopfliDeflate()) {
if (opts.zopfliMaxInputBytes() <= 0
|| resultBytes.length <= opts.zopfliMaxInputBytes()) {
Expand All @@ -457,7 +469,7 @@ && serializesIdentically(doc, sourceBytes, openStateBytes)) {
}
}

// 7. Monotonic guarantee: never return more than the live document. Fall back
// 8. Monotonic guarantee: never return more than the live document. Fall back
// to the in-memory snapshot, never the open-time bytes, so an edit survives.
if (resultBytes.length > originalSize) {
if (resultBytes.length > openStateBytes.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,13 @@ public static AuditReport audit(PdfDocument doc) {
Map<String, FontBuilder> fontMap = new LinkedHashMap<>();

for (int pageIdx = 0; pageIdx < doc.pageCount(); pageIdx++) {
try (PdfPage page = doc.page(pageIdx)) {
PdfPage page;
try {
page = doc.page(pageIdx);
} catch (JPDFiumException unopenablePage) {
continue;
}
try (page) {
Comment thread
balazs-szucs marked this conversation as resolved.
Resolved
MemorySegment rawPage = page.rawHandleForRead();
int objCount;
try {
Expand Down Expand Up @@ -340,6 +346,7 @@ static List<String> unembeddedNames(byte[] pdf) {
* when the native inventory is unavailable.
*/
static List<FontResource> unembeddedResources(byte[] pdf) {
if (pdf == null || pdf.length == 0) return List.of();
if (QpdfLib.isFontInventorySupported()) {
String json = QpdfLib.inspectFonts(pdf);
if (json != null) {
Expand Down Expand Up @@ -393,6 +400,7 @@ private static List<FontResource> auditFallback(PdfDocument doc) {
}

static List<FontResource> inventoryBytes(byte[] pdf) {
if (pdf == null || pdf.length == 0) return List.of();
String json = QpdfLib.inspectFonts(pdf);
return json == null ? List.of() : parseInventory(json);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ public static byte[] replace(Path input, Map<String, byte[]> fonts) throws IOExc
* directories when none are given.
*/
public static Result embedMissing(byte[] pdf, Path... fontDirs) throws IOException {
if (pdf == null || pdf.length == 0) {
return new Result(pdf == null ? new byte[0] : pdf, List.of(), List.of(), List.of());
}
List<PdfFontAuditor.FontResource> resources = PdfFontAuditor.unembeddedResources(pdf);
Set<String> names = new LinkedHashSet<>();
Map<String, Boolean> substitutable = new HashMap<>();
Expand All @@ -245,7 +248,16 @@ public static Result embedMissing(byte[] pdf, Path... fontDirs) throws IOExcepti
continue;
}
String key = key(name);
Face face = bundled == null ? null : resolve(bundled, key);
Face face = bundled == null ? null : bundled.get(key);
if (face != null && !face.embeddable()) face = null;
if (face == null) {
if (index == null) index = indexFor(fontDirs);
face = index.get(key);
if (face != null && !face.embeddable()) face = null;
}
if (face == null && bundled != null) {
face = resolve(bundled, key);
}
if (face == null) {
if (index == null) index = indexFor(fontDirs);
face = resolve(index, key);
Expand All @@ -266,7 +278,7 @@ public static Result embedMissing(byte[] pdf, Path... fontDirs) throws IOExcepti
"embedding restricted by fsType " + fsType));
continue;
}
found.put(name, data);
found.put(name, singleFace(data, name));
statuses.add(new FontStatus(name, source(face), fsType, true, "embedded"));
}
byte[] out = found.isEmpty() ? null : QpdfLib.embedFonts(pdf, found, true);
Expand Down Expand Up @@ -327,14 +339,25 @@ private static boolean isAscii(String value) {
public static FontStatus diagnose(String baseName, Path... fontDirs) {
String key = key(baseName);
if (fontDirs.length == 0) {
Face bundledFace = resolve(bundledFaces(), key);
if (bundledFace != null) {
Face bundledFace = bundledFaces().get(key);
if (bundledFace != null && bundledFace.embeddable()) {
return new FontStatus(baseName, source(bundledFace), bundledFace.fsType(), true,
"available");
}
}
Map<String, Face> index = indexFor(fontDirs);
Face face = resolve(index, key);
Face face = index.get(key);
if (face != null && face.embeddable()) {
return new FontStatus(baseName, source(face), face.fsType(), true, "available");
}
if (fontDirs.length == 0) {
Face bundledMetric = resolve(bundledFaces(), key);
if (bundledMetric != null) {
return new FontStatus(baseName, source(bundledMetric), bundledMetric.fsType(), true,
"available");
}
}
face = resolve(index, key);
if (face != null) {
return new FontStatus(baseName, source(face), face.fsType(), true, "available");
}
Expand Down Expand Up @@ -397,7 +420,7 @@ private static Map<String, byte[]> programsFor(
if (face == null) continue;
byte[] data = program(face);
if (!embeddableProgram(programFsType(face, data))) continue;
found.put(name, data);
found.put(name, singleFace(data, name));
}
return found;
}
Expand Down Expand Up @@ -488,9 +511,14 @@ private static Face lookup(Map<String, Face> index, String key, boolean embeddab
String style = styleOf(key.substring(family.length()));
for (String member : group) {
Face face = index.get(member + style);
if (face == null && !style.isEmpty()) face = index.get(member);
if (face != null && (!embeddableOnly || face.embeddable())) return face;
}
if (!style.isEmpty()) {
for (String member : group) {
Face face = index.get(member);
if (face != null && (!embeddableOnly || face.embeddable())) return face;
}
}
}
return null;
}
Expand Down Expand Up @@ -766,14 +794,94 @@ private static void cacheProgram(Face key, byte[] data) {

private static byte[] singleFace(byte[] data, String name) {
ByteBuffer b = ByteBuffer.wrap(data);
if (data.length < 16 || b.getInt(0) != TTCF) return data;
String wanted = key(name);
for (int f = 0; f < b.getInt(8); f++) {
if (names(b, b.getInt(12 + 4 * f)).stream().map(PdfFontEmbedder::key).anyMatch(wanted::equals)) {
return extractFace(data, b.getInt(12 + 4 * f));
byte[] face = data;
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);
break;
}
}
face = extractFace(data, foundFace >= 0 ? foundFace : b.getInt(12));
}
if (isSymbolicName(name)) {
face = sanitizeSymbolicTrueType(face);
}
return face;
}

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");
}
Comment thread
balazs-szucs marked this conversation as resolved.
Resolved

private static byte[] sanitizeSymbolicTrueType(byte[] fontData) {
if (fontData.length < 12) return fontData;
ByteBuffer buf = ByteBuffer.wrap(fontData);
int tag = buf.getInt(0);
if (tag != 0x00010000 && tag != 0x74727565) return fontData;
int numTables = buf.getShort(4) & 0xFFFF;
if (numTables < 1 || 12 + numTables * 16 > fontData.length) return fontData;
int cmapRecordOffset = -1;
for (int i = 0; i < numTables; i++) {
int rec = 12 + i * 16;
if (buf.getInt(rec) == 0x636D6170) {
cmapRecordOffset = rec;
break;
}
}
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;

int numSubtables = buf.getShort(cmapOffset + 2) & 0xFFFF;
if (numSubtables <= 1) return fontData;

int selectedSubtableOffset = -1;
for (int j = 0; j < numSubtables; j++) {
int subRec = cmapOffset + 4 + j * 8;
if (subRec + 8 > cmapOffset + cmapLength) break;
int platformId = buf.getShort(subRec) & 0xFFFF;
int encodingId = buf.getShort(subRec + 2) & 0xFFFF;
if (platformId == 3 && encodingId == 0) {
return fontData;
}
if (platformId == 1 && encodingId == 0) {
selectedSubtableOffset = subRec;
} else if (selectedSubtableOffset < 0 && platformId == 0) {
selectedSubtableOffset = subRec;
}
}
if (selectedSubtableOffset < 0) {
selectedSubtableOffset = cmapOffset + 4;
Comment thread
balazs-szucs marked this conversation as resolved.
Resolved
}

byte[] modified = fontData.clone();
ByteBuffer out = ByteBuffer.wrap(modified);
out.putShort(cmapOffset + 2, (short) 1);
for (int b = 0; b < 8; b++) {
modified[cmapOffset + 4 + b] = fontData[selectedSubtableOffset + b];
}

int checksum = 0;
int nWords = (cmapLength + 3) / 4;
for (int w = 0; w < nWords; w++) {
int off = cmapOffset + w * 4;
if (off + 4 <= modified.length) {
checksum += out.getInt(off);
} else {
int last = 0;
for (int b = 0; b < modified.length - off; b++) {
last |= (modified[off + b] & 0xFF) << (24 - 8 * b);
}
checksum += last;
}
}
return extractFace(data, b.getInt(12));
out.putInt(cmapRecordOffset + 4, checksum);
return modified;
}

private static byte[] extractFace(byte[] collection, int face) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public final class PdfOptimizer {
public static final int CCITT_G4 = 0x200;
/** Re-encode 1-bit DeviceGray images as JBIG2 when smaller. */
public static final int JBIG2 = 0x400;
/** Subset already-embedded TrueType/OpenType fonts in-place. */
public static final int SUBSET_FONTS = 0x800;

public static final int OBJECT_STREAMS_DISABLE = 0;
public static final int OBJECT_STREAMS_PRESERVE = 1;
Expand Down
Loading
Loading