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 @@ -139,7 +139,8 @@ private CompressOptions(Builder b) {
public int zopfliMaxInputBytes() { return zopfliMaxInputBytes; }
/**
* Whether to subset already-embedded TrueType/OpenType fonts in-place.
* Default: {@code true}.
* Default: {@code false}. This pass rewrites font programs, so callers opt
* in after deciding their text/render contract.
*/
public boolean subsetFonts() { return subsetFonts; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,18 @@ public static boolean inventoryAvailable() {
* @return fonts in file order, or an empty list when the native build cannot inventory
*/
public static List<FontResource> inventory(PdfDocument doc) {
// Prefer the original file so a large file-backed document is never
// serialized just to list its font resources.
Path sourcePath = doc.sourcePath();
if (!doc.isContentModified()
&& sourcePath != null
&& QpdfLib.isFileFontInventorySupported()) {
String json = QpdfLib.inspectFonts(sourcePath);
if (json != null) {
List<FontResource> parsed = parseInventory(json);
if (!parsed.isEmpty()) return parsed;
}
}
// sourceBytes() is the open-time snapshot. After an in-memory edit it no longer
// describes the document, so inventory the serialized current state instead.
byte[] source = doc.sourceBytes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,21 +245,23 @@ public static Result embedMissing(byte[] pdf, Path... fontDirs) throws IOExcepti
}
List<PdfFontAuditor.FontResource> resources = PdfFontAuditor.unembeddedResources(pdf);
Set<String> names = new LinkedHashSet<>();
Map<String, Boolean> substitutable = new HashMap<>();
Map<String, String> blockers = new HashMap<>();
for (PdfFontAuditor.FontResource resource : resources) {
names.add(resource.baseName());
substitutable.merge(resource.baseName(), safeToSubstitute(resource), Boolean::logicalAnd);
String blocker = substitutionBlocker(resource);
if (blocker != null) blockers.putIfAbsent(resource.baseName(), blocker);
}
List<String> missing = List.copyOf(names);
if (missing.isEmpty()) return new Result(pdf, List.of(), List.of(), List.of());
Map<String, Face> bundled = fontDirs.length == 0 ? bundledFaces() : null;
Map<String, Face> index = null;
Map<String, byte[]> found = new LinkedHashMap<>();
Map<Face, byte[]> operationPrograms = new HashMap<>();
List<FontStatus> statuses = new ArrayList<>(missing.size());
for (String name : missing) {
if (!substitutable.getOrDefault(name, true)) {
statuses.add(new FontStatus(name, "", -1, false,
"CID encoding or glyph order cannot be verified safely"));
String blocker = blockers.get(name);
if (blocker != null) {
statuses.add(new FontStatus(name, "", -1, false, blocker));
continue;
}
String key = key(name);
Expand All @@ -286,7 +288,7 @@ public static Result embedMissing(byte[] pdf, Path... fontDirs) throws IOExcepti
"embedding restricted by fsType " + restricted.fsType()));
continue;
}
byte[] data = program(face);
byte[] data = program(face, operationPrograms);
int fsType = programFsType(face, data);
if (!embeddableProgram(fsType)) {
statuses.add(new FontStatus(name, source(face), fsType, false,
Expand Down Expand Up @@ -335,19 +337,49 @@ public static Result embedMissingStrict(byte[] pdf, Path... fontDirs) throws IOE
* an ordering that a substitute program does not share; embedding it would keep the
* document renderable but change its extracted text.
*/
private static boolean safeToSubstitute(PdfFontAuditor.FontResource resource) {
private static String substitutionBlocker(PdfFontAuditor.FontResource resource) {
if (!resource.hasToUnicode() && !isAscii(resource.baseName())) {
// Legacy CJK producers put GBK/Shift-JIS bytes in /BaseFont and rely on the
// original font's glyph order. A substitute would change both text extraction
// and the drawn glyphs because there is no /ToUnicode to preserve.
return false;
return "legacy non-ASCII /BaseFont without /ToUnicode cannot be mapped safely";
}
if (!resource.cid()) {
String encoding = resource.encoding();
if (!encoding.isEmpty() && !"custom".equals(encoding)
&& !"WinAnsiEncoding".equals(encoding)
&& !"MacRomanEncoding".equals(encoding)
&& !"StandardEncoding".equals(encoding)) {
return "unknown simple-font encoding " + encoding
+ " cannot be mapped without the original glyph names";
}
String normalized = key(resource.baseName());
boolean symbolicFamily = normalized.startsWith("symbol")
|| normalized.startsWith("standardsymbol")
|| normalized.startsWith("zapfdingbats")
|| normalized.startsWith("dingbats");
if (symbolicFamily && resource.encoding().isEmpty()) {
// A built-in Symbol/ZapfDingbats encoding maps codes through the
// producer's glyph names, not through Unicode. A substitute
// program without the same glyph-name table would silently
// change every character, so leave it unresolved.
return "built-in symbolic encoding cannot be preserved by a substitute program";
}
return null;
}
if (!resource.cid()) return true;
boolean identityEncoding = "Identity-H".equals(resource.encoding())
|| "Identity-V".equals(resource.encoding());
if (!identityEncoding) {
return "CID encoding: predefined CMap " + resource.encoding()
+ " cannot be rewritten without a verified code-to-glyph mapping";
}
boolean identityMap = resource.cidToGidMap().isEmpty()
|| "Identity".equals(resource.cidToGidMap());
return identityEncoding && identityMap;
if (!identityMap) {
return "CIDToGIDMap " + resource.cidToGidMap()
+ " carries a glyph order a substitute program does not share";
}
return null;
}

private static boolean isAscii(String value) {
Expand Down Expand Up @@ -414,17 +446,18 @@ public static FileResult embedMissingTo(Path input, Path output, Path... fontDir
List<PdfFontAuditor.FontResource> resources =
PdfFontAuditor.unembeddedResources(input);
Set<String> names = new LinkedHashSet<>();
Map<String, Boolean> substitutable = new HashMap<>();
Map<String, String> blockers = new HashMap<>();
for (PdfFontAuditor.FontResource resource : resources) {
names.add(resource.baseName());
substitutable.merge(resource.baseName(), safeToSubstitute(resource), Boolean::logicalAnd);
String blocker = substitutionBlocker(resource);
if (blocker != null) blockers.putIfAbsent(resource.baseName(), blocker);
}
List<String> missing = List.copyOf(names);
if (missing.isEmpty()) {
Files.copy(input, output, StandardCopyOption.REPLACE_EXISTING);
return new FileResult(List.of(), List.of());
}
Map<String, byte[]> found = programsFor(missing, substitutable, fontDirs);
Map<String, byte[]> found = programsFor(missing, blockers, fontDirs);
if (found.isEmpty() || !QpdfLib.embedFontsToFile(input, output, found, true)) {
Files.copy(input, output, StandardCopyOption.REPLACE_EXISTING);
return new FileResult(List.of(), missing);
Expand All @@ -435,15 +468,16 @@ public static FileResult embedMissingTo(Path input, Path output, Path... fontDir
}

private static Map<String, byte[]> programsFor(
List<String> missing, Map<String, Boolean> substitutable, Path... fontDirs)
List<String> missing, Map<String, String> blockers, Path... fontDirs)
throws IOException {
Map<String, Face> index = indexFor(fontDirs);
Map<String, byte[]> found = new LinkedHashMap<>();
Map<Face, byte[]> operationPrograms = new HashMap<>();
for (String name : missing) {
if (!substitutable.getOrDefault(name, true)) continue;
if (blockers.containsKey(name)) continue;
Face face = resolve(index, key(name));
if (face == null) continue;
byte[] data = program(face);
byte[] data = program(face, operationPrograms);
if (!embeddableProgram(programFsType(face, data))) continue;
found.put(name, singleFace(data, name));
}
Expand Down Expand Up @@ -663,8 +697,12 @@ private static void indexFile(Path path, Map<String, Face> index) {
return;
}
boolean collection = b.getInt(0) == TTCF;
for (int f = 0, n = collection ? b.getInt(8) : 1; f < n; f++) {
int faceCount = collection
? Math.max(0, Math.min(b.getInt(8), (b.capacity() - 12) / 4))
: 1;
for (int f = 0; f < faceCount; f++) {
int offset = collection ? b.getInt(12 + 4 * f) : 0;
if (offset < 0 || offset > b.capacity() - 12) continue;
int fsType = fsType(b, offset);
// Restricted-license embedding and bitmap-only embedding are both unusable
// for a program substitution that must keep outlines, but the face stays in
Expand Down Expand Up @@ -730,18 +768,37 @@ int record = face + 12 + 16 * i;
/** fsType of the bytes actually selected for embedding, not the cached index entry. */
private static int programFsType(Face face, byte[] data) {
if (face.resource() != null) return face.fsType();
if (isType1Program(data)) {
// Type 1 containers have no OS/2 table and therefore no fsType.
// Treat them as embeddable; the native writer validates the program
// before installing it.
return 0;
}
try {
return data.length < 12 ? -1 : fsType(ByteBuffer.wrap(data), 0);
} catch (RuntimeException malformed) {
return -1;
}
}

private static boolean isType1Program(byte[] data) {
if (data.length >= 2 && (data[0] & 0xFF) == 0x80 && (data[1] & 0xFF) == 0x01) {
return true; // PFB segment header (little-endian 0x8001).
}
int probe = Math.min(data.length, 8192);
String header = new String(data, 0, probe, StandardCharsets.ISO_8859_1);
return header.startsWith("%!PS-AdobeFont")
|| header.startsWith("%!FontType1")
|| (header.startsWith("%!PS-Adobe-")
&& (header.contains("/FontType 1") || header.contains("eexec")));
}

private static boolean embeddableProgram(int fsType) {
return fsType >= 0 && (fsType & 0xF) != 2 && (fsType & 0x0200) == 0;
}

private static byte[] program(Face face) throws IOException {
private static byte[] program(Face face, Map<Face, byte[]> operationPrograms)
throws IOException {
// A replaced font file invalidates the cached face even when the path and
// TTC offset are unchanged; resolve the current key before looking up.
Face key = face;
Expand All @@ -759,6 +816,10 @@ private static byte[] program(Face face) throws IOException {
face.fsType());
}
}
if (operationPrograms != null) {
byte[] cached = operationPrograms.get(key);
if (cached != null) return cached;
}
synchronized (PROGRAMS) {
byte[] cached = PROGRAMS.get(key);
if (cached != null) return cached;
Expand All @@ -779,6 +840,7 @@ private static byte[] program(Face face) throws IOException {
}
}
}
if (operationPrograms != null) operationPrograms.put(key, data);
synchronized (PROGRAMS) {
byte[] raced = PROGRAMS.get(key);
if (raced != null) return raced;
Expand All @@ -788,6 +850,7 @@ private static byte[] program(Face face) throws IOException {
}
byte[] all = Files.readAllBytes(face.path());
byte[] data = face.offset() < 0 ? all : extractFace(all, face.offset());
if (operationPrograms != null) operationPrograms.put(key, data);
if (data.length > MAX_PROGRAM_BYTES) {
return data;
}
Expand Down Expand Up @@ -823,7 +886,7 @@ private static byte[] singleFace(byte[] data, String name) {
if (data.length >= 16 && b.getInt(0) == TTCF) {
String wanted = key(name);
int foundFace = -1;
int count = Math.min(b.getInt(8), (data.length - 12) / 4);
int count = Math.max(0, 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ private FontNormalizer() {}
*
* <p>Finds font dictionaries through the page resources (including nested
* form XObjects, annotation appearances, and patterns), repairs a missing or
* empty {@code /ToUnicode} map from the embedded program's cmap, and rebuilds
* empty {@code /ToUnicode} map from the embedded program's cmap, and repairs
* simple-font {@code /Widths} or CID {@code /W} entries from FreeType's
* unscaled advance widths. Both repairs share one qpdf load/write cycle.
* unscaled advance widths. Missing or malformed simple widths are rebuilt;
* positive producer widths are preserved. Both repairs share one qpdf
* load/write cycle.
*
* <p>Fonts without an embedded program are skipped, since there is no cmap or
* advance-width source to read. Use
Expand Down Expand Up @@ -165,9 +167,10 @@ private static int fixToUnicodeInternal(PdfDocument doc, int pageIndex) {
* <p>For simple fonts ({@code /TrueType}, {@code /Type1}, {@code /MMType1})
* the native bridge recomputes {@code /FirstChar}, {@code /LastChar}, and
* {@code /Widths} from each glyph's unscaled horizontal advance, normalised to
* 1000 units per em. Existing widths are replaced only when the array is
* missing, has the wrong length, or disagrees with the program. CID fonts
* without a {@code /W} array gain a compact GID-keyed array. Fonts with no
* 1000 units per em. Missing, non-numeric, or non-positive widths are
* repaired; positive producer widths are preserved because they may encode
* deliberate tracking or justification. CID fonts without a {@code /W} array
* gain entries for drawn CIDs plus zero-advance glyphs. Fonts with no
* readable embedded program are skipped; embed them with
* {@link stirling.software.jpdfium.doc.PdfFontEmbedder} first.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont;
import org.apache.pdfbox.pdmodel.font.encoding.WinAnsiEncoding;
import org.apache.pdfbox.text.PDFTextStripper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
Expand Down Expand Up @@ -119,6 +121,55 @@ void compressSubsetsFullEmbeddedTrueTypeFont(@TempDir Path temp) throws Exceptio
}
}

private static Path simpleTrueTypePdf(Path temp) throws Exception {
Path src = temp.resolve("simple-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);
PDTrueTypeFont typeface = PDTrueTypeFont.load(
pdf, font, WinAnsiEncoding.INSTANCE);
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;
}

@Test
void compressSubsetsSimpleTrueTypeFont(@TempDir Path temp) throws Exception {
assumeTrue(QpdfLib.isSubsetFontsSupported(), "native font subsetting unavailable");
Path src = simpleTrueTypePdf(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.WEB).subsetFonts(true).build());
assertTrue(out.result().actions().contains("Subsetted 1 embedded font(s)"),
"expected one subsetted simple font, actions=" + out.result().actions());
assertTrue(out.bytes().length < originalSize / 2,
"simple subset output should be less than half the source: "
+ out.bytes().length + " vs " + originalSize);
assertTrue(out.bytes().length < originalSize / 10,
"compact simple subsets should approach service output, not retain "
+ "every glyph slot: " + out.bytes().length + " vs " + originalSize);
try (PDDocument reopened = Loader.loadPDF(out.bytes())) {
assertEquals("Hello World", new PDFTextStripper().getText(reopened).strip(),
"the simple subset must keep the original text");
}
assertEquals(0, differingPixels(before, render(out.bytes(), 0)),
"simple subsetting must render identically");
}
}

@Test
void losslessPresetDoesNotSubsetFonts(@TempDir Path temp) throws Exception {
assumeTrue(QpdfLib.isSubsetFontsSupported(), "native font subsetting unavailable");
Expand Down
Loading
Loading