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
24 changes: 17 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}

Expand Down Expand Up @@ -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;
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, 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
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}

Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@ public record Result(
public Result(byte[] pdf, List<String> embedded, List<String> unresolved) {
this(pdf, embedded, unresolved, List.of());
}

/**
* Reject a result that still has unembedded fonts.
*
* <p>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. */
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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) {
Expand All @@ -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;
Expand Down
Loading
Loading