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
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,20 @@ publishing workflows, not library releases, so they are not listed here.
exactly like `font.ttf`, and the scan no longer allocates a regex per file
name.

- qpdf font embedding, subsetting and the PDF/X writer compress their output
at zlib level 9. The level is process-global state in qpdf, so those passes
hold the exclusive writer lock for the whole write: other threads' merge,
split or optimize work in the same process waits for a font pass instead of
interleaving with it.

- Measured end to end on one arm64 driver (7 repetitions of identical
commands) across the unreleased rework: embedding the 7-font TAMReview
sample costs p50 60→129 ms and repairing a damaged 3.1 MB document p50
84→121 ms, while compression presets are unchanged (185→184 ms and 82→81 ms
p50). The embed pass now builds a document-wide font inventory, normalizes
and verifies; the numbers are recorded so later changes can tell
optimization from regression.

### Fixed

- The bundled `JPDFiumCompactCmyk.icc` fallback is now generated only from the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import java.io.StringReader;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
Expand Down Expand Up @@ -277,19 +279,22 @@ public static Result embedMissing(byte[] pdf, Path... fontDirs) throws IOExcepti
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;
for (String candidate : nameKeys(name)) {
face = index.get(candidate);
if (face != null && face.embeddable()) break;
face = null;
}
}
if (face == null && bundled != null) {
face = resolve(bundled, key);
face = resolveName(bundled, name);
}
if (face == null) {
if (index == null) index = indexFor(fontDirs);
face = resolve(index, key);
face = resolveName(index, name);
}
if (face == null) {
if (index == null) index = indexFor(fontDirs);
Face restricted = restricted(index, key);
Face restricted = restrictedName(index, name);
statuses.add(restricted == null
? new FontStatus(name, "", -1, false, "no matching font")
: new FontStatus(name, source(restricted), restricted.fsType(), false,
Expand Down Expand Up @@ -336,18 +341,24 @@ public static Result embedMissing(byte[] pdf, Path... fontDirs) throws IOExcepti
* predefined CMap whose code-to-glyph mapping cannot be preserved
*/
public static Result embedMissingStrict(byte[] pdf, Path... fontDirs) throws IOException {
if (!QpdfLib.isFontInventorySupported()) {
throw new JPDFiumException(
"strict font embedding requires the native font inventory");
}
return embedMissing(pdf, fontDirs).requireAllEmbedded();
}

/**
* The native bridge can rewrite {@code /CIDToGIDMap} and {@code /ToUnicode} for
* Identity-encoded CID fonts and for predefined CJK CMaps (Adobe-Japan1 {@code 90ms-RKSJ-H},
* Adobe-Korea1 {@code KSC-EUC-H}, Adobe-GB1 {@code GB-EUC-H}), whose code-to-CID and
* {@code 90ms-RKSJ-V}, {@code 90msp-RKSJ-H}, {@code 90msp-RKSJ-V}, Adobe-Korea1 {@code KSC-EUC-H},
* {@code KSC-EUC-V}, Adobe-GB1 {@code GB-EUC-H}, {@code GB-EUC-V}), whose code-to-CID and
* CID-to-Unicode mappings are bundled. Other predefined CMaps or an existing CID-to-GID stream carry an ordering that a substitute program does not
* share; embedding them would keep the document renderable but change its text.
*/
private static String substitutionBlocker(PdfFontAuditor.FontResource resource) {
if (!resource.hasToUnicode() && !isAscii(resource.baseName())) {
boolean supportedCjk = isSupportedCjkPredefinedCMap(resource.encoding());
if (!resource.hasToUnicode() && !isAscii(resource.baseName()) && !supportedCjk) {
// 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.
Expand Down Expand Up @@ -378,9 +389,7 @@ private static String substitutionBlocker(PdfFontAuditor.FontResource resource)
}
boolean identityEncoding = "Identity-H".equals(resource.encoding())
|| "Identity-V".equals(resource.encoding())
|| "90ms-RKSJ-H".equals(resource.encoding())
|| "KSC-EUC-H".equals(resource.encoding())
|| "GB-EUC-H".equals(resource.encoding());
|| supportedCjk;
if (!identityEncoding) {
return "CID encoding: predefined CMap " + resource.encoding()
+ " cannot be rewritten without a verified code-to-glyph mapping";
Expand All @@ -394,6 +403,17 @@ private static String substitutionBlocker(PdfFontAuditor.FontResource resource)
return null;
}

private static boolean isSupportedCjkPredefinedCMap(String encoding) {
return "90ms-RKSJ-H".equals(encoding)
|| "90ms-RKSJ-V".equals(encoding)
|| "90msp-RKSJ-H".equals(encoding)
|| "90msp-RKSJ-V".equals(encoding)
|| "KSC-EUC-H".equals(encoding)
|| "KSC-EUC-V".equals(encoding)
|| "GB-EUC-H".equals(encoding)
|| "GB-EUC-V".equals(encoding);
}

private static boolean isAscii(String value) {
for (int i = 0; i < value.length(); i++) {
if (value.charAt(i) > 0x7F) return false;
Expand Down Expand Up @@ -426,11 +446,11 @@ public static FontStatus diagnose(String baseName, Path... fontDirs) {
"available");
}
}
face = resolve(index, key);
face = resolveName(index, baseName);
if (face != null) {
return new FontStatus(baseName, source(face), face.fsType(), true, "available");
}
Face restricted = restricted(index, key);
Face restricted = restrictedName(index, baseName);
return restricted == null
? new FontStatus(baseName, "", -1, false, "no matching font")
: new FontStatus(baseName, source(restricted), restricted.fsType(), false,
Expand Down Expand Up @@ -487,7 +507,7 @@ private static Map<String, byte[]> programsFor(
Map<Face, byte[]> operationPrograms = new HashMap<>();
for (String name : missing) {
if (blockers.containsKey(name)) continue;
Face face = resolve(index, key(name));
Face face = resolveName(index, name);
if (face == null) continue;
byte[] data = program(face, operationPrograms);
if (!embeddableProgram(programFsType(face, data))) continue;
Expand All @@ -506,6 +526,43 @@ public static Result embedMissing(Path input, Path... fontDirs) throws IOExcepti
return embedMissing(Files.readAllBytes(input), fontDirs);
}

private static Face resolveName(Map<String, Face> index, String name) {
for (String candidate : nameKeys(name)) {
Face face = resolve(index, candidate);
if (face != null) return face;
}
return null;
}

private static Face restrictedName(Map<String, Face> index, String name) {
for (String candidate : nameKeys(name)) {
Face face = restricted(index, candidate);
if (face != null) return face;
}
return null;
}

static List<String> nameKeys(String name) {
List<String> keys = new ArrayList<>(2);
String direct = key(name);
if (!direct.isEmpty()) keys.add(direct);
boolean latin1 = !name.isEmpty() && name.chars().allMatch(c -> c <= 0xFF);
if (latin1) {
try {
String decoded = StandardCharsets.UTF_8.newDecoder()
.onMalformedInput(CodingErrorAction.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPORT)
.decode(ByteBuffer.wrap(name.getBytes(StandardCharsets.ISO_8859_1)))
.toString();
String decodedKey = key(decoded);
if (!decodedKey.isEmpty() && !keys.contains(decodedKey)) keys.add(decodedKey);
} catch (CharacterCodingException ignored) {
// Not a UTF-8 byte sequence after the Latin-1 round trip.
}
}
return keys;
}

static String key(String name) {
int start = 0;
if (name.length() > 7 && name.charAt(6) == '+') {
Expand Down Expand Up @@ -983,6 +1040,8 @@ private static byte[] sanitizeSymbolicTrueType(byte[] fontData) {
if (subRec + 8 > cmapOffset + cmapLength) break;
int platformId = buf.getShort(subRec) & 0xFFFF;
int encodingId = buf.getShort(subRec + 2) & 0xFFFF;
int subtableOffset = buf.getInt(subRec + 4);
if (subtableOffset < 0 || subtableOffset > cmapLength - 4) continue;
if (platformId == 3 && encodingId == 0) {
return fontData;
}
Expand All @@ -996,6 +1055,8 @@ private static byte[] sanitizeSymbolicTrueType(byte[] fontData) {
selectedSubtableOffset = cmapOffset + 4;
}
if (selectedSubtableOffset + 8 > fontData.length) return fontData;
int selectedOffset = buf.getInt(selectedSubtableOffset + 4);
if (selectedOffset < 0 || selectedOffset > cmapLength - 4) return fontData;

byte[] modified = fontData.clone();
ByteBuffer out = ByteBuffer.wrap(modified);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,15 @@ private static int parseIntInPlace(String s, int start, int end) {
i++;
}
if (i >= end) throw new NumberFormatException("empty number");
int result = 0;
long limit = negative ? 2_147_483_648L : 2_147_483_647L;
long result = 0;
for (; i < end; i++) {
char c = s.charAt(i);
if (c < '0' || c > '9') throw new NumberFormatException("not a digit");
result = result * 10 + (c - '0');
if (result > limit) throw new NumberFormatException("integer overflow");
}
return negative ? -result : result;
return (int) (negative ? -result : result);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,19 @@ public static int intField(String json, String key) {
int length = json.length();
boolean negative = json.charAt(index) == '-';
if (negative) index++;
int value = 0;
long limit = negative ? 2_147_483_648L : 2_147_483_647L;
long value = 0;
boolean digits = false;
while (index < length) {
char c = json.charAt(index);
if (c < '0' || c > '9') break;
value = value * 10 + (c - '0');
if (value > limit) throw new NumberFormatException("integer overflow");
digits = true;
index++;
}
if (!digits) return 0;
return negative ? -value : value;
return (int) (negative ? -value : value);
}

/** Extract a long field from a single JSON object string. Returns 0 if missing. */
Expand All @@ -108,12 +110,21 @@ public static long longField(String json, String key) {
while (index < length) {
char c = json.charAt(index);
if (c < '0' || c > '9') break;
value = value * 10L + (c - '0');
int digit = c - '0';
if (negative) {
if (value < (Long.MIN_VALUE + digit) / 10)
throw new NumberFormatException("long overflow");
value = value * 10L - digit;
} else {
if (value > (Long.MAX_VALUE - digit) / 10)
throw new NumberFormatException("long overflow");
value = value * 10L + digit;
}
digits = true;
index++;
}
if (!digits) return 0L;
return negative ? -value : value;
return value;
}

/** Extract a boolean field from a single JSON object string. Returns false if missing. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,9 +401,10 @@ private static void assertSameOrResolvedText(
i++;
}
}
while (matched < a.length()
while (matched < a.length() && i < b.length()
&& (a.charAt(matched) == '?' || a.charAt(matched) == '\uFFFD')) {
matched++;
i++;
}
assertEquals(a.length(), matched, where + " lost resolved text: " + a + " vs " + b);
}
Expand Down
Loading
Loading