diff --git a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfRepair.java b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfRepair.java
index 07a0e3f66..c8c6e85d4 100644
--- a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfRepair.java
+++ b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfRepair.java
@@ -1048,6 +1048,141 @@ else if (c == '<' && n == '<') {
return -1;
}
+ /**
+ * Returns the index just past the first balanced {@code << >>} dictionary in
+ * {@code fragment}, or -1 when the fragment never closes one. Truncated
+ * repair candidates use the balanced prefix instead of the broken tail.
+ */
+ // Closes an unbalanced PDF fragment by dropping an unterminated tail:
+ // everything after the last complete value before the first unclosed
+ // structural token. Mirrors how tolerant parsers recover objects cut
+ // mid-array or mid-dictionary.
+ private static String closeUnbalancedFragment(String fragment) {
+ int depthDict = 0;
+ int depthArray = 0;
+ boolean literal = false;
+ boolean literalEscape = false;
+ boolean hex = false;
+ boolean comment = false;
+ int lastKeyStart = -1;
+ for (int i = 0; i < fragment.length(); i++) {
+ char c = fragment.charAt(i);
+ if (comment) {
+ if (c == '\n' || c == '\r') comment = false;
+ continue;
+ }
+ if (literal) {
+ if (literalEscape) literalEscape = false;
+ else if (c == '\\') literalEscape = true;
+ else if (c == ')') literal = false;
+ continue;
+ }
+ if (hex) {
+ if (c == '>') hex = false;
+ continue;
+ }
+ if (c == '%') {
+ comment = true;
+ continue;
+ }
+ if (c == '(') {
+ literal = true;
+ continue;
+ }
+ if (c == '<' && i + 1 < fragment.length() && fragment.charAt(i + 1) == '<') {
+ depthDict++;
+ i++;
+ continue;
+ }
+ if (c == '>' && i + 1 < fragment.length() && fragment.charAt(i + 1) == '>') {
+ depthDict--;
+ i++;
+ if (depthDict <= 0) return null; // balanced already
+ continue;
+ }
+ if (c == '<') {
+ hex = true;
+ continue;
+ }
+ if (c == '[') {
+ depthArray++;
+ continue;
+ }
+ if (c == ']') {
+ depthArray--;
+ if (depthArray < 0) return null;
+ continue;
+ }
+ if (c == '/' && depthArray == 0 && depthDict == 1 && !literal && !hex && !comment) {
+ // Remember each top-level key start: the cut point for a fragment
+ // truncated inside a later value is just before the last key.
+ lastKeyStart = i;
+ }
+ }
+ if (lastKeyStart < 0) return null;
+ // Drop the unterminated key and everything after it, then close the
+ // containers that were open at that point (counted from the cut).
+ String head = fragment.substring(0, lastKeyStart).stripTrailing();
+ int openArrays = 0;
+ int openDicts = 0;
+ for (int i = 0; i < lastKeyStart; i++) {
+ char c = fragment.charAt(i);
+ if (c == '[') openArrays++;
+ else if (c == ']') openArrays--;
+ else if (i + 1 < lastKeyStart && c == '<' && fragment.charAt(i + 1) == '<') {
+ openDicts++;
+ i++;
+ } else if (i + 1 < lastKeyStart && c == '>' && fragment.charAt(i + 1) == '>') {
+ openDicts--;
+ i++;
+ }
+ }
+ if (openDicts <= 0 || openArrays < 0) return null;
+ StringBuilder tail = new StringBuilder();
+ for (int i = 0; i < openArrays; i++) tail.append(" ]");
+ tail.append(" >>");
+ return head + tail;
+ }
+
+ private static int closedDictionaryEnd(String fragment) {
+ int depth = 0;
+ boolean literal = false;
+ boolean hex = false;
+ boolean comment = false;
+ boolean escape = false;
+ for (int i = 0; i < fragment.length(); i++) {
+ char c = fragment.charAt(i);
+ char n = i + 1 < fragment.length() ? fragment.charAt(i + 1) : '\0';
+ if (comment) {
+ if (c == '\n' || c == '\r') comment = false;
+ continue;
+ }
+ if (literal) {
+ if (escape) escape = false;
+ else if (c == '\\') escape = true;
+ else if (c == ')') literal = false;
+ continue;
+ }
+ if (hex) {
+ if (c == '>') hex = false;
+ continue;
+ }
+ if (c == '%') comment = true;
+ else if (c == '(') literal = true;
+ else if (c == '<' && n == '<') {
+ depth++;
+ i++;
+ } else if (c == '>' && n == '>') {
+ depth--;
+ i++;
+ if (depth == 0) return i + 1;
+ } else if (c == '<') {
+ hex = true;
+ }
+ }
+ return -1;
+ }
+
private static String replaceParent(String dict, long parent) {
java.util.regex.Matcher m = Pattern.compile("/Parent\\s+\\d+\\s+\\d+\\s+R").matcher(dict);
if (m.find()) {
@@ -1258,8 +1393,30 @@ private static byte[] rebuildPageTree(byte[] input) {
int start = pageStarts.get(i);
out.append(text, cursor, start);
int end = text.indexOf("endobj", start);
+ int junk = text.indexOf("%PDF-", start + 5);
+ if (junk >= 0 && (end < 0 || junk < end)) end = junk;
if (end < 0) end = text.length();
String dict = text.substring(start, end);
+ int close = closedDictionaryEnd(dict);
+ if (close >= 0 && close < dict.length()) {
+ // A page dictionary truncated mid-way (e.g. by a mid-file second
+ // %PDF header) keeps a usable prefix; close it and drop the tail.
+ dict = dict.substring(0, close);
+ } else {
+ // The dictionary itself never closes: the object was cut mid-way
+ // (often inside /Annots or another array). Close any open arrays
+ // and dictionaries so the fragment becomes a valid object again.
+ String recovered = closeUnbalancedFragment(dict);
+ if (recovered != null) {
+ dict = recovered;
+ // The original object body ran into following junk; rewrite
+ // the recovered fragment as a complete object of its own and
+ // skip past the junk instead of splicing the tail back in.
+ out.append(dict).append("\nendobj\n");
+ cursor = end;
+ continue;
+ }
+ }
if (pageNumbers.get(i) >= 0) {
dict = inheritPageAttributes(dict, text, pageNumbers.get(i), 0);
}
diff --git a/jpdfium/src/main/java/stirling/software/jpdfium/model/ShapeVerifyResult.java b/jpdfium/src/main/java/stirling/software/jpdfium/model/ShapeVerifyResult.java
new file mode 100644
index 000000000..5cc4b72ce
--- /dev/null
+++ b/jpdfium/src/main/java/stirling/software/jpdfium/model/ShapeVerifyResult.java
@@ -0,0 +1,33 @@
+package stirling.software.jpdfium.model;
+
+/**
+ * Verdict of a shaping-safety check at a text edit boundary.
+ *
+ *
Mirrors the re-shape-and-compare verification behind HarfBuzz's
+ * UNSAFE_TO_BREAK / UNSAFE_TO_CONCAT glyph flags: the full run is shaped once,
+ * the edited pieces are re-shaped under the full run's segment properties, and
+ * the glyph sequences must agree in glyph id, cluster, advance and offset.
+ *
+ * @param safe true when the edit is shape-safe (empirical verdict)
+ * @param reason machine-readable verdict reason, e.g.
+ * {@code "identical"}, {@code "split inside cluster"},
+ * {@code "glyph mismatch after removal"}
+ * @param mode 0 = split verification, 1 = span-removal verification
+ * @param fullGlyphs glyph count of the full reference run
+ * @param partGlyphs glyph count of the re-shaped pieces
+ * @param diffIndex first differing glyph index, -1 when none
+ * @param hbPredicted HarfBuzz's conservative UNSAFE_TO_BREAK /
+ * UNSAFE_TO_CONCAT prediction at the edit boundary
+ * @param unsafeBreakGlyphs glyphs in the full run flagged UNSAFE_TO_BREAK
+ * @param unsafeConcatGlyphs glyphs in the full run flagged UNSAFE_TO_CONCAT
+ */
+public record ShapeVerifyResult(
+ boolean safe,
+ String reason,
+ int mode,
+ int fullGlyphs,
+ int partGlyphs,
+ int diffIndex,
+ boolean hbPredicted,
+ int unsafeBreakGlyphs,
+ int unsafeConcatGlyphs) {}
diff --git a/jpdfium/src/main/java/stirling/software/jpdfium/model/ShapedGlyph.java b/jpdfium/src/main/java/stirling/software/jpdfium/model/ShapedGlyph.java
index 8142ec547..8d96310e4 100644
--- a/jpdfium/src/main/java/stirling/software/jpdfium/model/ShapedGlyph.java
+++ b/jpdfium/src/main/java/stirling/software/jpdfium/model/ShapedGlyph.java
@@ -1,10 +1,38 @@
package stirling.software.jpdfium.model;
-/** One shaped glyph: HarfBuzz output in points at the requested size. */
+/**
+ * One shaped glyph: HarfBuzz output in points at the requested size.
+ *
+ * @param glyphId HarfBuzz glyph id in the shaped font
+ * @param advanceX horizontal advance in points
+ * @param advanceY vertical advance in points
+ * @param offsetX horizontal displacement in points
+ * @param offsetY vertical displacement in points
+ * @param cluster byte index of the first UTF-8 character this glyph came from
+ * @param flags HarfBuzz glyph flags: bit 0 = UNSAFE_TO_BREAK (breaking the
+ * text at this cluster start would change shaping), bit 1 =
+ * UNSAFE_TO_CONCAT (changing text on either side of this
+ * cluster start would change shaping)
+ */
public record ShapedGlyph(
int glyphId,
float advanceX,
float advanceY,
float offsetX,
float offsetY,
- int cluster) {}
+ int cluster,
+ int flags) {
+
+ /**
+ * Backwards-compatible constructor with {@code flags = 0}.
+ */
+ public ShapedGlyph(
+ int glyphId,
+ float advanceX,
+ float advanceY,
+ float offsetX,
+ float offsetY,
+ int cluster) {
+ this(glyphId, advanceX, advanceY, offsetX, offsetY, cluster, 0);
+ }
+}
diff --git a/jpdfium/src/main/java/stirling/software/jpdfium/panama/FontLib.java b/jpdfium/src/main/java/stirling/software/jpdfium/panama/FontLib.java
index f030318ad..9b706654b 100644
--- a/jpdfium/src/main/java/stirling/software/jpdfium/panama/FontLib.java
+++ b/jpdfium/src/main/java/stirling/software/jpdfium/panama/FontLib.java
@@ -12,6 +12,7 @@
import java.util.Map;
import stirling.software.jpdfium.exception.JPDFiumException;
+import stirling.software.jpdfium.model.ShapeVerifyResult;
import stirling.software.jpdfium.model.ShapedGlyph;
import stirling.software.jpdfium.util.NativeJsonParser;
@@ -44,6 +45,11 @@ public final class FontLib {
Symbols.downcallOptional("jpdfium_text_shape",
FunctionDescriptor.of(JAVA_INT, ADDRESS, JAVA_LONG, ADDRESS, JAVA_FLOAT, ADDRESS));
+ private static final MethodHandle jpdfium_shape_verify =
+ Symbols.downcallOptional("jpdfium_shape_verify",
+ FunctionDescriptor.of(JAVA_INT, ADDRESS, JAVA_LONG, ADDRESS, JAVA_INT, JAVA_INT,
+ JAVA_INT, ADDRESS));
+
private FontLib() {}
/**
@@ -121,7 +127,8 @@ public static List shapeText(
Integer.parseInt(row.get("ay")) / 64f,
Integer.parseInt(row.get("dx")) / 64f,
Integer.parseInt(row.get("dy")) / 64f,
- Integer.parseInt(row.get("cluster"))));
+ Integer.parseInt(row.get("cluster")),
+ Integer.parseInt(row.getOrDefault("f", "0"))));
}
return out;
}
@@ -133,6 +140,90 @@ public static List shapeText(
});
}
+ /**
+ * Verifies that splitting {@code text} at the given UTF-8 byte offset is
+ * shape-safe (HarfBuzz UNSAFE_TO_BREAK semantics): shaping both sides
+ * separately and concatenating must reproduce the full run's glyphs.
+ *
+ * Redaction and content editing use this before cutting a text run:
+ * an unsafe split must extend to the whole interacting cluster instead of
+ * leaving silently mis-shaped halves.
+ *
+ * @param splitByte UTF-8 byte offset to split at; must be a cluster start
+ */
+ public static ShapeVerifyResult verifyShapeSplit(byte[] fontData, String text, int splitByte) {
+ byte[] utf8 = utf8OrThrow(text);
+ if (fontData == null || fontData.length == 0) throw new IllegalArgumentException("fontData must not be empty");
+ if (splitByte < 0 || splitByte > utf8.length) {
+ throw new IllegalArgumentException("splitByte out of range: " + splitByte);
+ }
+ return verifyShape(fontData, utf8, 0, splitByte, 0);
+ }
+
+ /**
+ * Verifies that removing the UTF-8 byte span {@code [startByte, endByte)}
+ * from {@code text} is shape-safe for the surviving glyphs (HarfBuzz
+ * UNSAFE_TO_CONCAT semantics): shaping the remaining text must leave the
+ * before- and after-parts identical to the full run minus the removed
+ * clusters.
+ *
+ * @param startByte UTF-8 byte offset where the removed span starts; cluster start
+ * @param endByte UTF-8 byte offset where the removed span ends; cluster start
+ */
+ public static ShapeVerifyResult verifyShapeRemoval(
+ byte[] fontData, String text, int startByte, int endByte) {
+ byte[] utf8 = utf8OrThrow(text);
+ if (fontData == null || fontData.length == 0) throw new IllegalArgumentException("fontData must not be empty");
+ if (startByte < 0 || endByte <= startByte || endByte > utf8.length) {
+ throw new IllegalArgumentException("removal span out of range: " + startByte + ".." + endByte);
+ }
+ return verifyShape(fontData, utf8, 1, startByte, endByte);
+ }
+
+ private static byte[] utf8OrThrow(String text) {
+ if (text == null || text.isEmpty()) throw new IllegalArgumentException("text must not be empty");
+ return text.getBytes(java.nio.charset.StandardCharsets.UTF_8);
+ }
+
+ private static ShapeVerifyResult verifyShape(
+ byte[] fontData, byte[] utf8, int mode, int a, int b) {
+ if (jpdfium_shape_verify == null) {
+ throw new JPDFiumException("jpdfium_shape_verify not in this native build");
+ }
+ return PdfiumRuntime.execute(() -> {
+ try {
+ try (Arena arena = Arena.ofConfined()) {
+ MemorySegment ptrSeg = arena.allocate(ADDRESS);
+ JpdfiumLib.check((int) jpdfium_shape_verify.invokeExact(
+ arena.allocateFrom(JAVA_BYTE, fontData), (long) fontData.length,
+ arena.allocateFrom(JAVA_BYTE, utf8), mode, a, b, ptrSeg), "shapeVerify");
+ MemorySegment strPtr = ptrSeg.get(ADDRESS, 0);
+ String json;
+ try {
+ json = FfmHelper.readNativeString(strPtr);
+ } finally {
+ JpdfiumH.jpdfium_free_string(strPtr);
+ }
+ Map row = NativeJsonParser.parseArray("[" + json + "]").get(0);
+ return new ShapeVerifyResult(
+ "true".equals(row.get("safe")),
+ row.getOrDefault("reason", ""),
+ Integer.parseInt(row.getOrDefault("mode", "0")),
+ Integer.parseInt(row.getOrDefault("full_glyphs", "0")),
+ Integer.parseInt(row.getOrDefault("part_glyphs", "0")),
+ Integer.parseInt(row.getOrDefault("diff", "-1")),
+ "true".equals(row.get("hb_predicted")),
+ Integer.parseInt(row.getOrDefault("unsafe_break_glyphs", "0")),
+ Integer.parseInt(row.getOrDefault("unsafe_concat_glyphs", "0")));
+ }
+ } catch (JPDFiumException e) {
+ throw e;
+ } catch (Throwable t) {
+ throw new JPDFiumException(t);
+ }
+ });
+ }
+
public static byte[] getData(long page, int fontIndex) {
return PdfiumRuntime.execute(() -> {
try (Arena a = Arena.ofConfined()) {
diff --git a/jpdfium/src/main/resources/META-INF/native-image/com.stirling/jpdfium/reachability-metadata.json b/jpdfium/src/main/resources/META-INF/native-image/com.stirling/jpdfium/reachability-metadata.json
index a09631924..33183ed85 100644
--- a/jpdfium/src/main/resources/META-INF/native-image/com.stirling/jpdfium/reachability-metadata.json
+++ b/jpdfium/src/main/resources/META-INF/native-image/com.stirling/jpdfium/reachability-metadata.json
@@ -252,6 +252,18 @@
"returnType": "jint",
"parameterTypes": ["void*", "void*", "jint", "void*", "jint", "jint", "jint", "void*", "jint"]
},
+ {
+ "returnType": "jint",
+ "parameterTypes": [
+ "void*",
+ "jlong",
+ "void*",
+ "jint",
+ "jint",
+ "jint",
+ "void*"
+ ]
+ },
{
"returnType": "jint",
"parameterTypes": [
diff --git a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfCompressorDetectionTest.java b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfCompressorDetectionTest.java
index 70fd9cc90..67b81bd3b 100644
--- a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfCompressorDetectionTest.java
+++ b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfCompressorDetectionTest.java
@@ -49,6 +49,22 @@ void attributeFormPartOneIsPdfA1() {
assertEquals(1, PdfCompressor.pdfaPart(pdf));
}
+ @Test
+ void singleQuotedAttributeFormPartOneIsPdfA1() {
+ byte[] pdf = ("%PDF-1.7\n\n%%EOF")
+ .getBytes(StandardCharsets.ISO_8859_1);
+ assertEquals(1, PdfCompressor.pdfaPart(pdf));
+ }
+
+ @Test
+ void compressedSingleQuotedPdfA1MetadataIsDetected() throws Exception {
+ byte[] pdf = pdfWithMetadata(
+ "",
+ "/FlateDecode", true);
+ assertEquals(1, PdfCompressor.pdfaPart(pdf));
+ }
+
@Test
void attributeFormPartTwoIsNotPartOne() {
byte[] pdf = ("%PDF-1.7\n 2000, "unexpectedly small Type1C CFF: " + origCff.length);
+ widths = targetFont.getDictionaryObject(COSName.WIDTHS);
+ firstChar = targetFont.getInt(COSName.FIRST_CHAR, 0);
+ lastChar = targetFont.getInt(COSName.LAST_CHAR, 255);
+ bbox = desc.getCOSArray(COSName.FONT_BBOX);
+ }
+
+ Path src = temp.resolve("bare-type1c.pdf");
+ try (PDDocument doc = new PDDocument()) {
+ PDPage page = new PDPage(PDRectangle.LETTER);
+ doc.addPage(page);
+ COSDictionary desc = new COSDictionary();
+ desc.setItem(COSName.TYPE, COSName.FONT_DESC);
+ desc.setName(COSName.FONT_NAME, "HelveticaNeueLTStd-Roman");
+ desc.setInt(COSName.FLAGS, 32);
+ COSArray copiedBox = new COSArray();
+ if (bbox != null) {
+ for (COSBase b : bbox) {
+ copiedBox.add(b);
+ }
+ }
+ desc.setItem(COSName.FONT_BBOX, copiedBox);
+ desc.setInt(COSName.ITALIC_ANGLE, 0);
+ desc.setInt(COSName.ASCENT, 718);
+ desc.setInt(COSName.DESCENT, -207);
+ desc.setInt(COSName.CAP_HEIGHT, 718);
+ desc.setInt(COSName.STEM_V, 140);
+ COSStream ff3 = new PDStream(doc, new ByteArrayInputStream(origCff)).getCOSObject();
+ ff3.setItem(COSName.SUBTYPE, COSName.getPDFName("Type1C"));
+ desc.setItem(COSName.FONT_FILE3, ff3);
+
+ COSDictionary font = new COSDictionary();
+ font.setItem(COSName.TYPE, COSName.FONT);
+ font.setItem(COSName.SUBTYPE, COSName.TYPE1);
+ font.setName(COSName.BASE_FONT, "HelveticaNeueLTStd-Roman");
+ font.setItem(COSName.ENCODING, COSName.WIN_ANSI_ENCODING);
+ font.setInt(COSName.FIRST_CHAR, firstChar);
+ font.setInt(COSName.LAST_CHAR, lastChar);
+ if (widths instanceof COSArray srcWidths) {
+ COSArray copiedWidths = new COSArray();
+ for (COSBase w : srcWidths) {
+ copiedWidths.add(w);
+ }
+ font.setItem(COSName.WIDTHS, copiedWidths);
+ }
+ font.setItem(COSName.FONT_DESC, desc);
+
+ COSDictionary fonts = new COSDictionary();
+ fonts.setItem(COSName.getPDFName("F1"), font);
+ COSDictionary res = new COSDictionary();
+ res.setItem(COSName.FONT, fonts);
+ page.setResources(new PDResources(res));
+ page.setContents(new PDStream(doc, new ByteArrayInputStream(
+ "BT /F1 24 Tf 72 700 Td (1040EZ) Tj ET"
+ .getBytes(StandardCharsets.US_ASCII))));
+ doc.save(src.toFile());
+ }
+
+ 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 bare Type1C subsetting, actions=" + out.result().actions());
+ try (PDDocument reopened = Loader.loadPDF(out.bytes())) {
+ assertEquals("1040EZ", new PDFTextStripper().getText(reopened).strip());
+ COSDictionary font = null;
+ for (COSObject obj : reopened.getDocument().getObjectsByType(COSName.FONT)) {
+ if (obj.getObject() instanceof COSDictionary d) {
+ font = d;
+ break;
+ }
+ }
+ assertNotNull(font);
+ COSStream subFf3 = (COSStream) font.getCOSDictionary(COSName.FONT_DESC)
+ .getDictionaryObject(COSName.FONT_FILE3);
+ assertNotNull(subFf3);
+ assertEquals("Type1C", subFf3.getNameAsString(COSName.SUBTYPE));
+ byte[] subBytes;
+ try (InputStream subIn = subFf3.createInputStream()) {
+ subBytes = subIn.readAllBytes();
+ }
+ assertTrue(subBytes.length < origCff.length / 2,
+ "subsetted Type1C must shrink dramatically: " + subBytes.length
+ + " vs " + origCff.length);
+ }
+ assertEquals(0, differingPixels(before, render(out.bytes(), 0)),
+ "bare Type1C subsetting must render identically");
+ }
+ }
+
+ @Test
+ void compressCompactsSparseCidToGidMap(@TempDir Path temp) throws Exception {
+ assumeTrue(QpdfLib.isSubsetFontsSupported(), "native font subsetting unavailable");
+ byte[] ttfBytes;
+ try (InputStream in = PdfCompressorTest.class.getResourceAsStream(
+ "/org/apache/pdfbox/resources/ttf/LiberationSans-Regular.ttf")) {
+ assertNotNull(in);
+ ttfBytes = in.readAllBytes();
+ }
+ int targetCp = -1;
+ int targetGid = -1;
+ int targetAdv = 500;
+ try (TrueTypeFont ttf = new TTFParser().parse(new RandomAccessReadBuffer(ttfBytes))) {
+ var cmap = ttf.getUnicodeCmapLookup(false);
+ HorizontalMetricsTable hmtx = ttf.getHorizontalMetrics();
+ int unitsPerEm = ttf.getHeader().getUnitsPerEm();
+ for (int cp = 0x2500; cp >= 0x0100; cp--) {
+ int gid = cmap.getGlyphId(cp);
+ if (gid >= 400 && hmtx.getAdvanceWidth(gid) > 0) {
+ targetCp = cp;
+ targetGid = gid;
+ targetAdv = Math.round(hmtx.getAdvanceWidth(gid) * 1000f / unitsPerEm);
+ break;
+ }
+ }
+ }
+ assertTrue(targetGid >= 400, "expected a high GID >= 400 in LiberationSans");
+
+ Path src = temp.resolve("sparse-cid-map.pdf");
+ try (PDDocument doc = new PDDocument()) {
+ PDPage page = new PDPage(PDRectangle.LETTER);
+ doc.addPage(page);
+
+ COSDictionary desc = new COSDictionary();
+ desc.setItem(COSName.TYPE, COSName.FONT_DESC);
+ desc.setName(COSName.FONT_NAME, "LiberationSans");
+ desc.setInt(COSName.FLAGS, 32);
+ desc.setItem(COSName.FONT_BBOX, new COSArray());
+ desc.setInt(COSName.ITALIC_ANGLE, 0);
+ desc.setInt(COSName.ASCENT, 905);
+ desc.setInt(COSName.DESCENT, -212);
+ desc.setInt(COSName.CAP_HEIGHT, 716);
+ desc.setInt(COSName.STEM_V, 80);
+ COSStream ff2 = new PDStream(doc, new ByteArrayInputStream(ttfBytes)).getCOSObject();
+ ff2.setInt(COSName.LENGTH1, ttfBytes.length);
+ desc.setItem(COSName.FONT_FILE2, ff2);
+
+ COSDictionary sys = new COSDictionary();
+ sys.setString(COSName.REGISTRY, "Adobe");
+ sys.setString(COSName.ORDERING, "Identity");
+ sys.setInt(COSName.SUPPLEMENT, 0);
+
+ byte[] mapBytes = new byte[] {
+ 0, 0,
+ (byte) ((targetGid >>> 8) & 0xFF), (byte) (targetGid & 0xFF)
+ };
+ COSStream cidToGid = new PDStream(doc, new ByteArrayInputStream(mapBytes))
+ .getCOSObject();
+
+ COSArray wInner = new COSArray();
+ wInner.add(org.apache.pdfbox.cos.COSInteger.get(targetAdv));
+ COSArray wOuter = new COSArray();
+ wOuter.add(org.apache.pdfbox.cos.COSInteger.get(1));
+ wOuter.add(wInner);
+
+ COSDictionary descendant = new COSDictionary();
+ descendant.setItem(COSName.TYPE, COSName.FONT);
+ descendant.setItem(COSName.SUBTYPE, COSName.CID_FONT_TYPE2);
+ descendant.setName(COSName.BASE_FONT, "LiberationSans");
+ descendant.setItem(COSName.CIDSYSTEMINFO, sys);
+ descendant.setInt(COSName.DW, 1000);
+ descendant.setItem(COSName.W, wOuter);
+ descendant.setItem(COSName.FONT_DESC, desc);
+ descendant.setItem(COSName.CID_TO_GID_MAP, cidToGid);
+
+ COSArray kids = new COSArray();
+ kids.add(descendant);
+
+ String toUnicode = "/CIDInit /ProcSet findresource begin\n"
+ + "12 dict begin begincmap\n"
+ + "/CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def\n"
+ + "/CMapName /Adobe-Identity-UCS def\n"
+ + "/CMapType 2 def\n"
+ + "1 begincodespacerange <0000> endcodespacerange\n"
+ + String.format(java.util.Locale.ROOT,
+ "1 beginbfchar <0001> <%04X> endbfchar", targetCp) + "\n"
+ + "endcmap CMapName currentdict /CMap defineresource pop end end\n";
+
+ COSDictionary font = new COSDictionary();
+ font.setItem(COSName.TYPE, COSName.FONT);
+ font.setItem(COSName.SUBTYPE, COSName.TYPE0);
+ font.setName(COSName.BASE_FONT, "LiberationSans");
+ font.setItem(COSName.ENCODING, COSName.IDENTITY_H);
+ font.setItem(COSName.DESCENDANT_FONTS, kids);
+ font.setItem(COSName.TO_UNICODE, new PDStream(doc,
+ new ByteArrayInputStream(toUnicode.getBytes(StandardCharsets.US_ASCII)))
+ .getCOSObject());
+
+ COSDictionary fonts = new COSDictionary();
+ fonts.setItem(COSName.getPDFName("F1"), font);
+ COSDictionary res = new COSDictionary();
+ res.setItem(COSName.FONT, fonts);
+ page.setResources(new PDResources(res));
+ page.setContents(new PDStream(doc, new ByteArrayInputStream(
+ "BT /F1 24 Tf 72 700 Td <0001> Tj ET"
+ .getBytes(StandardCharsets.US_ASCII))));
+ doc.save(src.toFile());
+ }
+
+ 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 sparse CID font to be subsetted: " + out.result().actions());
+ try (PDDocument reopened = Loader.loadPDF(out.bytes())) {
+ assertEquals(String.valueOf((char) targetCp),
+ new PDFTextStripper().getText(reopened).strip());
+ COSDictionary descFont = null;
+ for (COSObject obj : reopened.getDocument().getObjectsByType(COSName.FONT)) {
+ if (obj.getObject() instanceof COSDictionary d
+ && COSName.CID_FONT_TYPE2.equals(d.getCOSName(COSName.SUBTYPE))) {
+ descFont = d;
+ break;
+ }
+ }
+ assertNotNull(descFont);
+ COSStream compacted = (COSStream) descFont.getDictionaryObject(
+ COSName.CID_TO_GID_MAP);
+ assertNotNull(compacted, "compacted /CIDToGIDMap stream missing");
+ byte[] outMap;
+ try (InputStream mapIn = compacted.createInputStream()) {
+ outMap = mapIn.readAllBytes();
+ }
+ assertEquals(4, outMap.length, "compacted /CIDToGIDMap must stay 2-entry");
+ int newGid = ((outMap[2] & 0xFF) << 8) | (outMap[3] & 0xFF);
+ assertTrue(newGid > 0 && newGid < 10,
+ "expected dense remapped GID < 10, got " + newGid);
+ }
+ assertEquals(0, differingPixels(before, render(out.bytes(), 0)),
+ "compacted sparse GID subset must render identically");
+ }
+ }
+
+ @Test
+ void compressPreservesPdfA1StructureAndUncompressedMetadata(@TempDir Path tmp)
+ throws Exception {
+ assumeTrue(QpdfLib.isSupported(), "QPDF support unavailable");
+ String xmp = ""
+ + ""
+ + ""
+ + "";
+
+ Path src = tmp.resolve("pdfa1-in.pdf");
+ try (PDDocument doc = new PDDocument()) {
+ doc.setVersion(1.4f);
+ PDPage page = new PDPage();
+ doc.addPage(page);
+ try (PDPageContentStream cs = new PDPageContentStream(doc, page)) {
+ for (int i = 0; i < 60; i++) {
+ cs.addRect(10 + i, 20, 40, 40);
+ cs.stroke();
+ }
+ }
+ var metadata = new org.apache.pdfbox.pdmodel.common.PDMetadata(doc);
+ metadata.importXMPMetadata(xmp.getBytes(StandardCharsets.UTF_8));
+ metadata.getCOSObject().removeItem(COSName.FILTER);
+ doc.getDocumentCatalog().setMetadata(metadata);
+ doc.save(src.toFile());
+ }
+
+ try (PdfDocument doc = PdfDocument.open(src)) {
+ PdfCompressor.CompressResultWithBytes res = PdfCompressor.compress(
+ doc, CompressOptions.builder()
+ .preset(CompressPreset.LOSSLESS)
+ .optimizeStreams(true)
+ .useZopfliDeflate(true)
+ .build());
+ try (PDDocument outDoc = Loader.loadPDF(res.bytes())) {
+ assertTrue(outDoc.getDocument()
+ .getObjectsByType(COSName.getPDFName("ObjStm")).isEmpty(),
+ "PDF/A-1 must not contain object streams");
+ assertTrue(outDoc.getDocument()
+ .getObjectsByType(COSName.getPDFName("XRef")).isEmpty(),
+ "PDF/A-1 must not contain xref streams");
+ var outMeta = outDoc.getDocumentCatalog().getMetadata();
+ assertNotNull(outMeta, "PDF/A-1 metadata stream must remain present");
+ assertNull(outMeta.getCOSObject().getDictionaryObject(COSName.FILTER),
+ "PDF/A-1 metadata stream must remain uncompressed");
+ }
+ }
+ }
}
diff --git a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderTest.java b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderTest.java
index 457783028..51a9a87f0 100644
--- a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderTest.java
+++ b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderTest.java
@@ -3,6 +3,8 @@
import stirling.software.jpdfium.PdfDocument;
import stirling.software.jpdfium.PdfPage;
import stirling.software.jpdfium.exception.JPDFiumException;
+import stirling.software.jpdfium.fonts.FontNormalizer;
+import stirling.software.jpdfium.panama.NativeRuntime;
import stirling.software.jpdfium.panama.QpdfLib;
import org.apache.pdfbox.Loader;
@@ -24,6 +26,7 @@
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.font.Standard14Fonts;
import org.apache.pdfbox.text.PDFTextStripper;
+import stirling.software.jpdfium.panama.FontLib;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
@@ -850,7 +853,8 @@ private static void assertCidRemap(String toUnicodeBody) throws Exception {
}
assertTrue(cidSetBytes.length > 0, "/CIDSet must not be empty");
assertEquals(0, cidSetBytes[0] & 0x80,
- "CID 0 (.notdef) must stay clear in /CIDSet");
+ "CID 0 (.notdef) must stay clear in /CIDSet: veraPDF treats it as "
+ + "a glyph that is not a CID");
assertEquals(0x80 >> (1 & 7), cidSetBytes[1 / 8] & (0x80 >> (1 & 7)),
"CID 1 must be marked in /CIDSet");
}
@@ -971,6 +975,44 @@ private static void assertCidSubstituteUnresolvedForCode(
}
}
+ @Test
+ void inheritedToUnicodeBlocksCidSubstitution(@TempDir Path temp) throws Exception {
+ assumeTrue(PdfFontEmbedder.isAvailable(), "native qpdf font replacement unavailable");
+ FontProgram source = findEmbeddedFont(
+ Files.readAllBytes(Path.of(PdfFontEmbedderTest.class
+ .getResource("/pdfs/general/basic-text.pdf").toURI())), "ArialMT");
+ assertNotNull(source, "fixture must contain ArialMT");
+ Path dir = Files.createDirectories(temp.resolve("fonts"));
+ Files.write(dir.resolve(source.baseName() + ".ttf"), source.data());
+ byte[] input = identityCidPdf(source.baseName(),
+ "1 beginbfchar <0001> <0041> endbfchar");
+ input = withInheritedToUnicode(input);
+
+ PdfFontEmbedder.Result result = PdfFontEmbedder.embedMissing(input, dir);
+
+ assertTrue(result.unresolved().contains(source.baseName()),
+ "a /UseCMap mapping must leave the CID font unresolved: " + result);
+ try (PDDocument pdf = Loader.loadPDF(result.pdf())) {
+ COSDictionary font = identityCidFont(pdf);
+ COSStream toUnicode = (COSStream) font.getDictionaryObject(COSName.TO_UNICODE);
+ assertNotNull(toUnicode, "the inherited ToUnicode stream must be preserved");
+ assertNotNull(toUnicode.getDictionaryObject(
+ COSName.getPDFName("UseCMap")), "the inherited mapping must stay intact");
+ }
+ }
+
+ private static byte[] withInheritedToUnicode(byte[] pdfBytes) throws IOException {
+ try (PDDocument pdf = Loader.loadPDF(pdfBytes)) {
+ COSDictionary font = identityCidFont(pdf);
+ COSStream toUnicode = (COSStream) font.getDictionaryObject(COSName.TO_UNICODE);
+ assertNotNull(toUnicode);
+ toUnicode.setItem(COSName.getPDFName("UseCMap"), COSName.getPDFName("Identity-H"));
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ pdf.save(out);
+ return out.toByteArray();
+ }
+ }
+
@Test
void explicitCidReplacementGeneratesToUnicode() throws Exception {
assumeTrue(PdfFontEmbedder.isAvailable(), "native qpdf font replacement unavailable");
@@ -1292,6 +1334,48 @@ private static byte[] sharedType3AndTrueTypePdf() throws Exception {
}
}
+ @Test
+ void acceptsBareCffProgramMagic() {
+ byte[] cff = {(byte) 0x01, 0, (byte) 0x04, (byte) 0x04, 0, 0, 0, 0};
+ assertTrue(PdfFontAuditor.looksLikeFontProgram(cff),
+ "bare CFF programs begin with 01 00 04 04");
+ }
+
+ @Test
+ void replacesCorruptEmbeddedFontProgram() throws Exception {
+ assumeTrue(PdfFontEmbedder.isAvailable(), "native qpdf font embedding unavailable");
+ byte[] program;
+ try (InputStream in = liberationSansStream()) {
+ program = in.readAllBytes();
+ }
+ byte[] pdf = sharedBaseFontPdf("LiberationSans", program);
+ try (PDDocument doc = Loader.loadPDF(pdf)) {
+ COSDictionary font = doc.getPage(0).getResources()
+ .getFont(COSName.getPDFName("F1")).getCOSObject();
+ COSDictionary descriptor =
+ (COSDictionary) font.getDictionaryObject(COSName.FONT_DESC);
+ assertNotNull(descriptor);
+ PDStream corrupt = new PDStream(doc, new ByteArrayInputStream(new byte[] {1, 2, 3, 4}));
+ descriptor.setItem(COSName.FONT_FILE2, corrupt.getCOSObject());
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ doc.save(out);
+ pdf = out.toByteArray();
+ }
+ assertTrue(PdfFontAuditor.corruptEmbeddedKeys(pdf).contains("LiberationSans"),
+ "fixture must expose a corrupt embedded program");
+
+ PdfFontEmbedder.Result result = PdfFontEmbedder.embedMissing(pdf);
+
+ assertTrue(result.embedded().contains("LiberationSans"), result.statuses().toString());
+ assertTrue(result.unresolved().isEmpty(), result.unresolved().toString());
+ assertTrue(PdfFontAuditor.corruptEmbeddedKeys(result.pdf()).isEmpty(),
+ "corrupt program must be replaced");
+ try (PdfDocument doc = PdfDocument.open(result.pdf());
+ PdfPage page = doc.page(0)) {
+ assertTrue(page.extractText().contains("Hello"), page.extractText());
+ }
+ }
+
private static byte[] sharedBaseFontPdf(String baseFont, byte[] program) throws Exception {
try (PDDocument pdf = new PDDocument()) {
PDPage page = new PDPage(PDRectangle.LETTER);
@@ -1904,6 +1988,57 @@ void pageResourcesReplacePdfiumFallbackFonts() throws Exception {
"inherited Times-Roman must be embedded");
}
+ @Test
+ void invisibleOnlyFontIsNotSubstituted() throws Exception {
+ assumeTrue(PdfFontEmbedder.isAvailable(), "native qpdf font embedding unavailable");
+ byte[] input;
+ try (PDDocument doc = new PDDocument()) {
+ PDPage page = new PDPage(PDRectangle.LETTER);
+ doc.addPage(page);
+ PDType1Font visible = new PDType1Font(Standard14Fonts.FontName.HELVETICA);
+ PDType1Font hidden = new PDType1Font(Standard14Fonts.FontName.TIMES_ROMAN);
+ try (PDPageContentStream cs = new PDPageContentStream(doc, page)) {
+ cs.beginText();
+ cs.setFont(visible, 14);
+ cs.newLineAtOffset(72, 700);
+ cs.showText("Visible text");
+ cs.endText();
+ cs.beginText();
+ cs.setFont(hidden, 14);
+ cs.newLineAtOffset(72, 680);
+ cs.setRenderingMode(
+ org.apache.pdfbox.pdmodel.graphics.state.RenderingMode.NEITHER);
+ cs.showText("Hidden OCR layer");
+ cs.setRenderingMode(
+ org.apache.pdfbox.pdmodel.graphics.state.RenderingMode.FILL);
+ cs.endText();
+ }
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ doc.save(out);
+ input = out.toByteArray();
+ }
+
+ PdfFontEmbedder.Result result = PdfFontEmbedder.embedMissing(input);
+ assertTrue(result.embedded().contains("Helvetica"), result.statuses().toString());
+ assertFalse(result.embedded().contains("Times-Roman"),
+ "invisible-only font must not be embedded: " + result.statuses());
+ assertTrue(result.statuses().stream().anyMatch(s ->
+ s.baseName().equals("Times-Roman")
+ && s.reason().contains("invisible")),
+ result.statuses().toString());
+ }
+
+ @Test
+ void coverageGateRejectsCodepointsMissingFromCandidateFace() throws Exception {
+ try (InputStream in = liberationSansStream()) {
+ byte[] program = in.readAllBytes();
+ boolean[] covered = FontLib.coversText(program, new int[] {'A', 0x6F22});
+ assertTrue(covered[0], "Liberation must cover Latin A");
+ assertFalse(covered[1],
+ "Liberation must not claim coverage for CJK U+6F22");
+ }
+ }
+
@Test
void restrictedFsTypeFontIsRejected(@TempDir Path temp) throws Exception {
assumeTrue(PdfFontEmbedder.isAvailable(), "native qpdf font embedding unavailable");
@@ -1983,6 +2118,52 @@ void type1PfbProgramsAreEmbedded(@TempDir Path temp) throws Exception {
}
}
+ @Test
+ void normalizerRepairsType1ProgramLengths() throws Exception {
+ assumeTrue(PdfFontEmbedder.isAvailable(), "native qpdf font embedding unavailable");
+ assumeTrue(NativeRuntime.isFull(), "needs real native library");
+ Path ghostscript = Path.of("/opt/homebrew/share/ghostscript/fonts");
+ assumeTrue(Files.isDirectory(ghostscript), "no Ghostscript Type 1 fonts");
+ PdfFontEmbedder.Result embedded =
+ PdfFontEmbedder.embedMissing(namedType1Pdf("NimbusSanL-Regu"), ghostscript);
+ assumeTrue(embedded.embedded().contains("NimbusSanL-Regu"), embedded.statuses().toString());
+
+ byte[] input;
+ long expected1;
+ long expected2;
+ long expected3;
+ try (PDDocument pdf = Loader.loadPDF(embedded.pdf())) {
+ COSDictionary font = pdf.getPage(0).getResources()
+ .getFont(COSName.getPDFName("F1")).getCOSObject();
+ COSDictionary descriptor =
+ (COSDictionary) font.getDictionaryObject(COSName.FONT_DESC);
+ COSStream program = (COSStream) descriptor.getDictionaryObject(COSName.FONT_FILE);
+ expected1 = program.getInt(COSName.LENGTH1);
+ expected2 = program.getInt(COSName.LENGTH2);
+ expected3 = program.getInt(COSName.LENGTH3);
+ program.setInt(COSName.LENGTH1, 1);
+ program.setInt(COSName.LENGTH2, 1);
+ program.setInt(COSName.LENGTH3, 1);
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ pdf.save(out);
+ input = out.toByteArray();
+ }
+
+ try (PdfDocument doc = PdfDocument.open(input)) {
+ FontNormalizer.normalizeAll(doc);
+ try (PDDocument loaded = Loader.loadPDF(doc.saveBytes())) {
+ COSDictionary font = loaded.getPage(0).getResources()
+ .getFont(COSName.getPDFName("F1")).getCOSObject();
+ COSDictionary descriptor =
+ (COSDictionary) font.getDictionaryObject(COSName.FONT_DESC);
+ COSStream program = (COSStream) descriptor.getDictionaryObject(COSName.FONT_FILE);
+ assertEquals(expected1, program.getInt(COSName.LENGTH1), "Length1");
+ assertEquals(expected2, program.getInt(COSName.LENGTH2), "Length2");
+ assertEquals(expected3, program.getInt(COSName.LENGTH3), "Length3");
+ }
+ }
+ }
+
@Test
void truncatedTrueTypeCollectionIsRejected() throws Exception {
assumeTrue(PdfFontEmbedder.isAvailable(), "native qpdf font embedding unavailable");
@@ -2660,4 +2841,20 @@ void replacementMarkerCodepointDoesNotBlockSubstitution() throws Exception {
"the font must be embedded in the output");
}
}
+ @Test
+ void scriptFallbackPrefersMatchingCjkFamily() {
+ assertEquals("hiraginomincho",
+ PdfFontEmbedder.scriptFallbackKeys("kozminpromediumacro").get(0));
+ assertEquals("hiraginosans",
+ PdfFontEmbedder.scriptFallbackKeys("msgothic").get(0));
+ assertEquals("applegothic",
+ PdfFontEmbedder.scriptFallbackKeys("malgungothic").get(0));
+ assertEquals("pingfangtc",
+ PdfFontEmbedder.scriptFallbackKeys("pmingliu").get(0));
+ assertEquals("pingfangsc",
+ PdfFontEmbedder.scriptFallbackKeys("simsun").get(0));
+ assertTrue(PdfFontEmbedder.scriptFallbackKeys("notosanssc").contains("notosanssc"));
+ assertTrue(PdfFontEmbedder.scriptFallbackKeys("helvetica").isEmpty());
+ }
+
}
diff --git a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontVeraPdfTest.java b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontVeraPdfTest.java
new file mode 100644
index 000000000..5c1792ecd
--- /dev/null
+++ b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontVeraPdfTest.java
@@ -0,0 +1,493 @@
+package stirling.software.jpdfium.doc;
+
+import stirling.software.jpdfium.PdfDocument;
+import stirling.software.jpdfium.fonts.FontNormalizer;
+import stirling.software.jpdfium.panama.QpdfLib;
+import org.apache.pdfbox.Loader;
+import org.apache.pdfbox.cos.COSArray;
+import org.apache.pdfbox.cos.COSBase;
+import org.apache.pdfbox.cos.COSDictionary;
+import org.apache.pdfbox.cos.COSInteger;
+import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDPage;
+import org.apache.pdfbox.pdmodel.PDPageContentStream;
+import org.apache.pdfbox.pdmodel.common.PDStream;
+import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont;
+import org.apache.pdfbox.pdmodel.font.PDType0Font;
+import org.apache.pdfbox.pdmodel.font.PDType1Font;
+import org.apache.pdfbox.pdmodel.font.Standard14Fonts;
+import org.apache.pdfbox.pdmodel.font.encoding.WinAnsiEncoding;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
+import org.junit.jupiter.api.io.TempDir;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+
+import javax.xml.XMLConstants;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Stream;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
+
+class PdfFontVeraPdfTest {
+
+ private static boolean verapdfAvailable = false;
+
+ @BeforeAll
+ static void checkVerapdf() {
+ Process p = null;
+ try {
+ p = new ProcessBuilder("verapdf", "--version").start();
+ verapdfAvailable = p.waitFor(15, TimeUnit.SECONDS) && p.exitValue() == 0;
+ } catch (IOException e) {
+ verapdfAvailable = false;
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ verapdfAvailable = false;
+ } finally {
+ if (p != null) {
+ p.destroyForcibly();
+ }
+ }
+ }
+
+ @Test
+ void testFontVeraPdfOnGeneralSamples(@TempDir Path tempDir) throws Exception {
+ assumeTrue(verapdfAvailable, "verapdf CLI is not available");
+ assumeTrue(PdfFontEmbedder.isAvailable(), "native font embedder is not available");
+
+ List fixtures = List.of(
+ "/pdfs/general/90ms_rksj_h_sample.pdf",
+ "/pdfs/general/gb_euc_h_sample.pdf",
+ "/pdfs/general/ksc_euc_h_sample.pdf",
+ "/pdfs/general/basic-text.pdf"
+ );
+
+ List allFailures = new ArrayList<>();
+ for (String fixture : fixtures) {
+ var url = getClass().getResource(fixture);
+ assertNotNull(url, "Missing required test fixture: " + fixture);
+ byte[] input = Files.readAllBytes(Path.of(url.toURI()));
+ PdfFontEmbedder.Result result = PdfFontEmbedder.embedMissing(input);
+ Path outPath = tempDir.resolve("embedded-" + Path.of(fixture).getFileName());
+ Files.write(outPath, result.pdf());
+
+ List fontViolations2b = getFontViolations(outPath, "2b");
+ if (!fontViolations2b.isEmpty()) {
+ allFailures.add(fixture + " [2b]: " + fontViolations2b);
+ }
+ if (!fixture.endsWith("basic-text.pdf")) {
+ // PDF/A-1b (clause 6.3.3.3) requires embedding predefined non-Identity CMaps
+ // directly, whereas PDF/A-2b allows ISO 32000 predefined CJK CMaps; every
+ // other 1b font rule (widths, CIDToGIDMap, CIDSet, flags) must still pass.
+ List unexpected1b = getFontViolations(outPath, "1b").stream()
+ .filter(v -> !v.startsWith("[6.3.3.3]"))
+ .toList();
+ if (!unexpected1b.isEmpty()) {
+ allFailures.add(fixture + " [1b]: " + unexpected1b);
+ }
+ }
+ }
+
+ byte[] unembeddedLatin;
+ try (PDDocument doc = new PDDocument()) {
+ PDPage page = new PDPage();
+ doc.addPage(page);
+ try (PDPageContentStream cs = new PDPageContentStream(doc, page)) {
+ cs.beginText();
+ cs.setFont(new PDType1Font(Standard14Fonts.FontName.HELVETICA), 12);
+ cs.newLineAtOffset(72, 720);
+ cs.showText("Hello PDF/A-1b and 2b font verification");
+ cs.endText();
+ }
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ doc.save(baos);
+ unembeddedLatin = baos.toByteArray();
+ }
+ PdfFontEmbedder.Result latinEmbedded = PdfFontEmbedder.embedMissing(unembeddedLatin);
+ Path latinOut = tempDir.resolve("embedded-latin-standard14.pdf");
+ Files.write(latinOut, latinEmbedded.pdf());
+ List latin1b = getFontViolations(latinOut, "1b");
+ List latin2b = getFontViolations(latinOut, "2b");
+ if (!latin1b.isEmpty()) {
+ allFailures.add("latin-standard14 [1b]: " + latin1b);
+ }
+ if (!latin2b.isEmpty()) {
+ allFailures.add("latin-standard14 [2b]: " + latin2b);
+ }
+
+ assertTrue(allFailures.isEmpty(), "PDF/A font violations: " + allFailures);
+ }
+
+ @Test
+ @EnabledIfSystemProperty(named = "jpdfium.safedocs.dir", matches = ".+")
+ void testFontVeraPdfOnSafeDocsSample(@TempDir Path tempDir) throws Exception {
+ assumeTrue(verapdfAvailable, "verapdf CLI is not available");
+ assumeTrue(PdfFontEmbedder.isAvailable(), "native font embedder is not available");
+
+ Path configured = Path.of(System.getProperty("jpdfium.safedocs.dir"));
+ Path safedocsBase = Files.isDirectory(configured.resolve("pdfs"))
+ ? configured.resolve("pdfs")
+ : configured;
+ assumeTrue(Files.isDirectory(safedocsBase),
+ "SafeDocs base directory not found: " + safedocsBase);
+
+ List candidateFolders;
+ try (Stream s = Files.list(safedocsBase)) {
+ candidateFolders = s.filter(Files::isDirectory).sorted().toList();
+ }
+ List candidates = new ArrayList<>();
+ for (Path dir : candidateFolders) {
+ try (Stream s = Files.list(dir)) {
+ candidates.addAll(s.filter(p -> p.toString().endsWith(".pdf"))
+ .limit(3)
+ .toList());
+ }
+ }
+
+ int tested = 0;
+ int passed2b = 0;
+
+ for (Path pdf : candidates) {
+ byte[] bytes = Files.readAllBytes(pdf);
+ if (bytes.length == 0 || bytes.length > 5 * 1024 * 1024) continue;
+
+ PdfFontEmbedder.Result result;
+ try {
+ result = PdfFontEmbedder.embedMissing(bytes);
+ } catch (Exception e) {
+ continue;
+ }
+ if (result.embedded().isEmpty() || !result.unresolved().isEmpty()) {
+ continue;
+ }
+
+ tested++;
+ Path normPath = tempDir.resolve("norm-" + pdf.getFileName());
+ Files.write(normPath, result.pdf());
+ try (PdfDocument doc = PdfDocument.open(normPath)) {
+ FontNormalizer.normalizeAll(doc);
+ doc.save(normPath);
+ }
+ List violationsNorm = getFontViolations(normPath, "2b");
+ if (violationsNorm.isEmpty()) passed2b++;
+
+ assertTrue(violationsNorm.isEmpty(),
+ "Normalized PDF " + pdf.getFileName() + " must have 0 font violations under PDF/A-2b: " + violationsNorm);
+ }
+ assertTrue(tested > 0, "must test at least one SafeDocs document");
+ assertEquals(tested, passed2b, "All tested SafeDocs documents must pass PDF/A-2b font rules after normalization");
+ }
+
+ @Test
+ void testCompactedCidSetDifferencesAndPredefinedCmapsPassVeraPdf(@TempDir Path tempDir) throws Exception {
+ assumeTrue(verapdfAvailable, "verapdf CLI is not available");
+ assumeTrue(PdfFontEmbedder.isAvailable(), "native font embedder is not available");
+
+ List failures = new ArrayList<>();
+
+ // 1. Compacted /CIDSet including CID 0 (.notdef) verified against PDF/A-1b, 2b, and 3b.
+ byte[] cidInput;
+ try (PDDocument doc = new PDDocument();
+ InputStream fontStream = PDDocument.class.getResourceAsStream(
+ "/org/apache/pdfbox/resources/ttf/LiberationSans-Regular.ttf");
+ ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+ assertNotNull(fontStream);
+ PDPage page = new PDPage();
+ doc.addPage(page);
+ PDType0Font font = PDType0Font.load(doc, fontStream, false);
+ COSDictionary descendant = (COSDictionary) ((COSArray) font.getCOSObject()
+ .getDictionaryObject(COSName.DESCENDANT_FONTS)).getObject(0);
+ ((COSDictionary) descendant.getDictionaryObject(COSName.FONT_DESC))
+ .setItem(COSName.CID_SET, new PDStream(doc, new ByteArrayInputStream(new byte[] {(byte) 0xFF})).getCOSObject());
+ try (PDPageContentStream cs = new PDPageContentStream(doc, page)) {
+ cs.beginText();
+ cs.setFont(font, 12);
+ cs.newLineAtOffset(72, 720);
+ cs.showText("A");
+ cs.endText();
+ }
+ doc.save(baos);
+ cidInput = baos.toByteArray();
+ }
+ Path cidOut = tempDir.resolve("cidset-subset.pdf");
+ var cidSub = QpdfLib.subsetFonts(cidInput);
+ byte[] cidBytes = cidSub.pdf() != null ? cidSub.pdf() : cidInput;
+ Files.write(cidOut, cidBytes);
+ for (String flavour : List.of("1b", "2b", "3b")) {
+ List v = getFontViolations(cidOut, flavour);
+ if (!v.isEmpty()) failures.add("subsetted CIDSet [" + flavour + "]: " + v);
+ }
+ Path cidNormOut = tempDir.resolve("cidset-normalized.pdf");
+ try (PdfDocument doc = PdfDocument.open(cidInput)) {
+ FontNormalizer.normalizeAll(doc);
+ doc.save(cidNormOut);
+ }
+ for (String flavour : List.of("1b", "2b", "3b")) {
+ List v = getFontViolations(cidNormOut, flavour);
+ if (!v.isEmpty()) failures.add("normalized CIDSet [" + flavour + "]: " + v);
+ }
+
+ // 2. Simple TrueType with /BaseEncoding /StandardEncoding (0x27 -> /quoteright) normalized for PDF/A-2b/3b.
+ byte[] diffInput;
+ try (PDDocument doc = new PDDocument();
+ InputStream fontStream = PDDocument.class.getResourceAsStream(
+ "/org/apache/pdfbox/resources/ttf/LiberationSans-Regular.ttf");
+ ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+ assertNotNull(fontStream);
+ PDPage page = new PDPage();
+ doc.addPage(page);
+ PDTrueTypeFont font = PDTrueTypeFont.load(doc, fontStream, WinAnsiEncoding.INSTANCE);
+ COSDictionary enc = new COSDictionary();
+ enc.setItem(COSName.TYPE, COSName.ENCODING);
+ enc.setItem(COSName.BASE_ENCODING, COSName.STANDARD_ENCODING);
+ font.getCOSObject().setItem(COSName.ENCODING, enc);
+ try (PDPageContentStream cs = new PDPageContentStream(doc, page)) {
+ cs.beginText();
+ cs.setFont(font, 12);
+ cs.newLineAtOffset(72, 720);
+ cs.showText("a'b");
+ cs.endText();
+ }
+ doc.save(baos);
+ diffInput = baos.toByteArray();
+ }
+ Path diffOut = tempDir.resolve("base-encoding-normalized.pdf");
+ try (PdfDocument doc = PdfDocument.open(diffInput)) {
+ FontNormalizer.normalizeAll(doc);
+ doc.save(diffOut);
+ }
+ for (String flavour : List.of("2b", "3b")) {
+ List v = getFontViolations(diffOut, flavour);
+ if (!v.isEmpty()) failures.add("BaseEncoding 0x27 [" + flavour + "]: " + v);
+ }
+
+ // 3. Predefined RKSJ CMaps: 90ms-RKSJ-V and 90msp-RKSJ-H.
+ var rksjUrl = getClass().getResource("/pdfs/general/90ms_rksj_h_sample.pdf");
+ assertNotNull(rksjUrl);
+ byte[] rksjBase = Files.readAllBytes(Path.of(rksjUrl.toURI()));
+ for (String cmap : List.of("90ms-RKSJ-V", "90msp-RKSJ-H")) {
+ byte[] variant;
+ try (PDDocument doc = Loader.loadPDF(rksjBase);
+ ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+ COSDictionary fonts = doc.getPage(0).getResources().getCOSObject().getCOSDictionary(COSName.FONT);
+ for (COSName key : fonts.keySet()) {
+ COSDictionary f = fonts.getCOSDictionary(key);
+ if (COSName.TYPE0.equals(f.getDictionaryObject(COSName.SUBTYPE))) {
+ f.setItem(COSName.ENCODING, COSName.getPDFName(cmap));
+ }
+ }
+ doc.save(baos);
+ variant = baos.toByteArray();
+ }
+ Path out = tempDir.resolve("embedded-" + cmap + ".pdf");
+ Files.write(out, PdfFontEmbedder.embedMissing(variant).pdf());
+ for (String flavour : List.of("2b", "3b")) {
+ List v = getFontViolations(out, flavour);
+ if (!v.isEmpty()) failures.add(cmap + " [" + flavour + "]: " + v);
+ }
+ }
+
+ // 4. Cyclic /Kids in /AcroForm during stripSignatures + font embedding.
+ byte[] cyclicInput;
+ try (PDDocument doc = new PDDocument(); ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+ PDPage page = new PDPage();
+ doc.addPage(page);
+ try (PDPageContentStream cs = new PDPageContentStream(doc, page)) {
+ cs.beginText();
+ cs.setFont(new PDType1Font(Standard14Fonts.FontName.HELVETICA), 12);
+ cs.newLineAtOffset(72, 720);
+ cs.showText("Signature strip check");
+ cs.endText();
+ }
+ COSDictionary sig = new COSDictionary();
+ sig.setItem(COSName.FT, COSName.SIG);
+ COSArray byteRange = new COSArray();
+ for (int v : new int[] {0, 10, 20, 30}) byteRange.add(COSInteger.get(v));
+ COSDictionary sigVal = new COSDictionary();
+ sigVal.setItem(COSName.getPDFName("ByteRange"), byteRange);
+ sig.setItem(COSName.V, sigVal);
+
+ COSDictionary cyclicField = new COSDictionary();
+ cyclicField.setDirect(false);
+ cyclicField.setItem(COSName.FT, COSName.TX);
+ COSArray kids = new COSArray();
+ kids.add(cyclicField);
+ cyclicField.setItem(COSName.KIDS, kids);
+
+ COSArray fields = new COSArray();
+ fields.add(cyclicField);
+ fields.add(sig);
+ COSDictionary acroForm = new COSDictionary();
+ acroForm.setItem(COSName.FIELDS, fields);
+ doc.getDocumentCatalog().getCOSObject().setItem(COSName.ACRO_FORM, acroForm);
+ doc.save(baos);
+ cyclicInput = baos.toByteArray();
+ }
+ Path cyclicOut = tempDir.resolve("cyclic-stripped.pdf");
+ Files.write(cyclicOut, PdfFontEmbedder.embedMissing(cyclicInput, true).pdf());
+ try (PDDocument doc = Loader.loadPDF(cyclicOut.toFile())) {
+ COSDictionary acro = doc.getDocumentCatalog().getCOSObject().getCOSDictionary(COSName.ACRO_FORM);
+ if (acro != null && acro.getCOSArray(COSName.FIELDS) != null) {
+ for (COSBase item : acro.getCOSArray(COSName.FIELDS)) {
+ if (item instanceof COSDictionary d) {
+ assertTrue(!COSName.SIG.equals(d.getDictionaryObject(COSName.FT)),
+ "Signature field must be stripped even with cyclic /Kids");
+ }
+ }
+ }
+ }
+ List cyclicV = getFontViolations(cyclicOut, "2b");
+ if (!cyclicV.isEmpty()) failures.add("cyclicKids stripSignatures [2b]: " + cyclicV);
+
+ assertTrue(failures.isEmpty(), "PDF/A font conformance failures: " + failures);
+ }
+
+ private static List getFontViolations(Path pdf, String flavour) {
+ List violations = new ArrayList<>();
+ Process p = null;
+ try {
+ p = new ProcessBuilder(
+ "verapdf", "--flavour", flavour, "--format", "xml", "-v", pdf.toAbsolutePath().toString()
+ ).redirectError(ProcessBuilder.Redirect.DISCARD).start();
+
+ final Process proc = p;
+ CompletableFuture stdoutFuture = CompletableFuture.supplyAsync(() -> {
+ try (InputStream in = proc.getInputStream()) {
+ return in.readAllBytes();
+ } catch (IOException e) {
+ throw new java.io.UncheckedIOException(e);
+ }
+ });
+
+ byte[] xmlBytes = stdoutFuture.get(60, TimeUnit.SECONDS);
+ boolean finished = p.waitFor(10, TimeUnit.SECONDS);
+ if (!finished) {
+ violations.add("verapdf timed out waiting for exit");
+ return violations;
+ }
+ int exitCode = p.exitValue();
+ if (exitCode != 0 && exitCode != 1) {
+ violations.add("verapdf exited with error code " + exitCode);
+ return violations;
+ }
+
+ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
+ dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
+ dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
+ dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
+ dbf.setXIncludeAware(false);
+ dbf.setExpandEntityReferences(false);
+ DocumentBuilder db = dbf.newDocumentBuilder();
+
+ Document doc = db.parse(new ByteArrayInputStream(xmlBytes));
+ NodeList exceptions = doc.getElementsByTagName("taskException");
+ if (exceptions.getLength() > 0) {
+ violations.add("verapdf reported taskException (" + exceptions.getLength() + ")");
+ return violations;
+ }
+
+ NodeList summaries = doc.getElementsByTagName("batchSummary");
+ if (summaries.getLength() == 0) {
+ violations.add("verapdf XML report missing batchSummary");
+ return violations;
+ }
+ Element summary = (Element) summaries.item(0);
+ for (String attr : List.of("failedToParse", "encrypted", "outOfMemory", "veraExceptions")) {
+ String val = summary.getAttribute(attr);
+ if (!"0".equals(val)) {
+ violations.add("verapdf batchSummary " + attr + "=" + val);
+ }
+ }
+
+ NodeList validationReports = doc.getElementsByTagName("validationReports");
+ if (validationReports.getLength() == 0) {
+ violations.add("verapdf XML report missing validationReports");
+ return violations;
+ }
+ Element valReports = (Element) validationReports.item(0);
+ String failedJobs = valReports.getAttribute("failedJobs");
+ if (!"0".equals(failedJobs)) {
+ violations.add("verapdf validationReports failedJobs=" + failedJobs);
+ }
+
+ NodeList reports = doc.getElementsByTagName("validationReport");
+ if (reports.getLength() == 0) {
+ violations.add("verapdf XML report missing validationReport");
+ return violations;
+ }
+ for (int i = 0; i < reports.getLength(); i++) {
+ Element report = (Element) reports.item(i);
+ String jobEndStatus = report.getAttribute("jobEndStatus");
+ if (!"normal".equals(jobEndStatus)) {
+ violations.add("verapdf validationReport jobEndStatus=" + jobEndStatus);
+ }
+ }
+
+ NodeList rules = doc.getElementsByTagName("rule");
+ for (int i = 0; i < rules.getLength(); i++) {
+ Element rule = (Element) rules.item(i);
+ String status = rule.getAttribute("status");
+ if (!"failed".equalsIgnoreCase(status)) continue;
+ String clause = rule.getAttribute("clause");
+ String desc = "";
+ NodeList descNodes = rule.getElementsByTagName("description");
+ if (descNodes.getLength() > 0) {
+ desc = descNodes.item(0).getTextContent();
+ }
+ String obj = "";
+ NodeList objNodes = rule.getElementsByTagName("object");
+ if (objNodes.getLength() > 0) {
+ obj = objNodes.item(0).getTextContent();
+ }
+ if (isFontRule(clause, desc, obj, flavour)) {
+ violations.add("[" + clause + "] (" + obj + "): " + desc);
+ }
+ }
+ } catch (Exception e) {
+ violations.add("verapdf execution error: " + e.getMessage());
+ } finally {
+ if (p != null) {
+ p.destroyForcibly();
+ }
+ }
+ return violations;
+ }
+
+ private static boolean isFontRule(String clause, String desc, String obj, String flavour) {
+ if (clause.startsWith("6.2.11.8")) {
+ return false;
+ }
+ if (flavour != null && flavour.startsWith("1")) {
+ return clause.startsWith("6.3");
+ }
+ if (flavour != null && (flavour.startsWith("2") || flavour.startsWith("3"))) {
+ return clause.startsWith("6.2.11");
+ }
+ if (clause.startsWith("6.2.2") || clause.startsWith("6.2.3") || clause.startsWith("6.2.4") || clause.startsWith("6.7")) {
+ return false;
+ }
+ String o = obj.toLowerCase();
+ if (o.contains("font") || o.contains("glyph") || o.contains("cmap")) return true;
+ String d = desc.toLowerCase();
+ return d.contains("font") || d.contains("glyph") || d.contains("cmap") || d.contains("cidset") || d.contains("charset");
+ }
+}
diff --git a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfRepairStructuralDamageTest.java b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfRepairStructuralDamageTest.java
new file mode 100644
index 000000000..2e7e03ca5
--- /dev/null
+++ b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfRepairStructuralDamageTest.java
@@ -0,0 +1,667 @@
+package stirling.software.jpdfium.doc;
+
+import org.apache.pdfbox.Loader;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.text.PDFTextStripper;
+import org.junit.jupiter.api.Assumptions;
+import org.junit.jupiter.api.Test;
+import stirling.software.jpdfium.PdfDocument;
+import stirling.software.jpdfium.PdfPage;
+import stirling.software.jpdfium.panama.NativeRuntime;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/** Synthetic structural damage: duplicate objects, cut-off bodies, missing xref/endobj. */
+class PdfRepairStructuralDamageTest {
+
+ @Test
+ void repairsDuplicateObjectDefinitions() throws Exception {
+ byte[] damaged = duplicateFirstStreamObject(RepairCorpus.validMultiPage());
+ assertRepaired(damaged, true);
+ }
+
+ @Test
+ void repairsCutOffObjectBody() throws Exception {
+ byte[] damaged = cutStreamTail(RepairCorpus.validMultiPage());
+ assertRepaired(damaged, false);
+ }
+
+ @Test
+ void repairsMissingXrefTable() throws Exception {
+ byte[] valid = RepairCorpus.validMultiPage();
+ byte[] damaged = stripXrefTable(valid);
+ assertTrue(damaged.length < valid.length, "xref table was not removed");
+ assertRepaired(damaged, true);
+ }
+
+ @Test
+ void repairsBrokenHeaderAndPagesCountWithoutAborting() throws Exception {
+ byte[] damaged = brokenHeaderBadPagesCountPdf();
+ assertNotNull(PdfRepair.inspect(damaged), "inspect must return diagnostics, not abort");
+ assertRepaired(damaged, false, 1);
+ }
+
+ @Test
+ void repairsMissingEndobjTokens() throws Exception {
+ byte[] damaged = stripTwoEndobj(RepairCorpus.validMultiPage());
+ assertRepaired(damaged, true);
+ }
+
+ @Test
+ void truncatedDownloadWithoutPageObjectsFailsClosed75() throws Exception {
+ assertFailsClosed(RepairCorpus.truncate(RepairCorpus.validMultiPage(), 0.75));
+ }
+
+ @Test
+ void truncatedDownloadWithoutPageObjectsFailsClosed50() throws Exception {
+ assertFailsClosed(RepairCorpus.truncate(RepairCorpus.validMultiPage(), 0.50));
+ }
+
+ @Test
+ void preservesPageRotationThroughXrefRepair() throws Exception {
+ Assumptions.assumeTrue(NativeRuntime.isFull(), "Requires full native repair bridge");
+ byte[] rotated;
+ try (PDDocument doc = new PDDocument()) {
+ org.apache.pdfbox.pdmodel.PDPage page = new org.apache.pdfbox.pdmodel.PDPage();
+ page.setRotation(90);
+ doc.addPage(page);
+ try (org.apache.pdfbox.pdmodel.PDPageContentStream cs =
+ new org.apache.pdfbox.pdmodel.PDPageContentStream(doc, page)) {
+ cs.beginText();
+ cs.setFont(new org.apache.pdfbox.pdmodel.font.PDType1Font(
+ org.apache.pdfbox.pdmodel.font.Standard14Fonts.FontName.HELVETICA), 14);
+ cs.newLineAtOffset(72, 700);
+ cs.showText("Rotated repair page");
+ cs.endText();
+ }
+ java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
+ doc.save(out);
+ rotated = out.toByteArray();
+ }
+ byte[] damaged = stripXrefTable(rotated);
+ RepairResult result = PdfRepair.builder().input(damaged).all().build().execute();
+ assertTrue(result.isUsable(), "rotated PDF must repair: " + result.status());
+ try (PdfDocument doc = PdfDocument.open(result.repairedPdf())) {
+ assertEquals(1, doc.pageCount());
+ // One try-with-resources page: an unclosed doc.page() pins a tracked
+ // native page for the whole JVM and later lifecycle restores fail.
+ try (PdfPage page = doc.page(0)) {
+ assertEquals(1, page.rotation(), "90-degree page rotation must survive repair");
+ }
+ }
+ }
+
+ @Test
+ void repairsDuplicateResourceDictionaryKeys() throws Exception {
+ Assumptions.assumeTrue(NativeRuntime.isFull(), "Requires full native repair bridge");
+ StringBuilder pdf = new StringBuilder();
+ pdf.append("%PDF-1.4\n");
+ pdf.append("1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n");
+ pdf.append("2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n");
+ pdf.append("3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] ")
+ .append("/Contents 4 0 R /Resources <<");
+ for (int i = 0; i < 40; i++) pdf.append(" /Im1 1 0 R");
+ pdf.append(" >> >>\nendobj\n");
+ String content = "BT 12 Tf 72 700 Td (Duplicate keys) Tj ET";
+ pdf.append("4 0 obj\n<< /Length ").append(content.length())
+ .append(" >>\nstream\n").append(content).append("\nendstream\nendobj\n");
+ pdf.append("trailer\n<< /Root 1 0 R /Size 5 >>\nstartxref\n0\n%%EOF\n");
+ RepairResult result = PdfRepair.builder()
+ .input(pdf.toString().getBytes(StandardCharsets.ISO_8859_1))
+ .all()
+ .build()
+ .execute();
+ assertTrue(result.isUsable(), "duplicate keys must repair: " + result.status());
+ try (PdfDocument doc = PdfDocument.open(result.repairedPdf())) {
+ assertEquals(1, doc.pageCount());
+ }
+ }
+
+ @Test
+ void repairsTabSeparatedDuplicateResourceKeys() throws Exception {
+ Assumptions.assumeTrue(NativeRuntime.isFull(), "Requires full native repair bridge");
+ StringBuilder pdf = new StringBuilder();
+ pdf.append("%PDF-1.4\n");
+ pdf.append("1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n");
+ pdf.append("2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n");
+ pdf.append("3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] ")
+ .append("/Contents 4 0 R /Resources <<");
+ for (int i = 0; i < 40; i++) pdf.append(" /Im1\t1\t0\tR");
+ pdf.append(" >> >>\nendobj\n");
+ String content = "BT 12 Tf 72 700 Td (Tabbed duplicate keys) Tj ET";
+ pdf.append("4 0 obj\n<< /Length ").append(content.length())
+ .append(" >>\nstream\n").append(content).append("\nendstream\nendobj\n");
+ pdf.append("trailer\n<< /Root 1 0 R /Size 5 >>\nstartxref\n0\n%%EOF\n");
+ RepairResult result = PdfRepair.builder()
+ .input(pdf.toString().getBytes(StandardCharsets.ISO_8859_1))
+ .all()
+ .build()
+ .execute();
+ assertTrue(result.isUsable(), "tab-separated resource keys must repair: " + result.status());
+ try (PdfDocument doc = PdfDocument.open(result.repairedPdf())) {
+ assertEquals(1, doc.pageCount());
+ }
+ }
+
+ @Test
+ void leavesLiteralStringsUntouchedDuringTokenRepair() throws Exception {
+ String raw = "%PDF-1.4\n"
+ + "1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"
+ + "2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n"
+ + "3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] >>\nendobj\n"
+ + "4 0 obj\n<< /Title (note 1 0 o /Example) >>\nendobj\n"
+ + "trailer\n<< /Root 1 0 R /Info 4 0 R /Size 5 >>\nstartxref\n0\n%%EOF\n";
+ java.lang.reflect.Method normalize =
+ PdfRepair.class.getDeclaredMethod("normalizeRepairTokens", byte[].class);
+ normalize.setAccessible(true);
+ byte[] out = (byte[]) normalize.invoke(null, raw.getBytes(StandardCharsets.ISO_8859_1));
+ assertTrue(new String(out, StandardCharsets.ISO_8859_1)
+ .contains("(note 1 0 o /Example)"),
+ "literal strings must not be treated as object tokens");
+ }
+
+ @Test
+ void rebuildsOrphanedPageDictionaryWithoutObjectHeader() throws Exception {
+ Assumptions.assumeTrue(NativeRuntime.isFull(), "Requires full native repair bridge");
+ String content = "0 0 100 100 re f";
+ String pdf = "%PDF-1.4\n"
+ + "1 0 obj\n<< /Type /Catalog /Pages 3 0 R >>\nendobj\n"
+ + "2 0 obj\n<< /Length " + content.length() + " >>\nstream\n"
+ + content + "\nendstream\nendobj\n"
+ + "<>/Contents 2 0 R>>\n"
+ + "trailer\n<< /Root 1 0 R /Size 4 >>\nstartxref\n0\n%%EOF\n";
+ RepairResult result = PdfRepair.builder()
+ .input(pdf.getBytes(StandardCharsets.ISO_8859_1))
+ .all()
+ .build()
+ .execute();
+ assertTrue(result.isUsable(), "orphaned page dictionary must be rebuilt: " + result.status());
+ try (PdfDocument doc = PdfDocument.open(result.repairedPdf())) {
+ assertEquals(1, doc.pageCount());
+ }
+ }
+
+ @Test
+ void rebuildsMissingPageTreeFromPageDictionaries() throws Exception {
+ Assumptions.assumeTrue(NativeRuntime.isFull(), "Requires full native repair bridge");
+ String content = "0 0 100 100 re f";
+ String pdf = "%PDF-1.4\n"
+ + "1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"
+ + "3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] "
+ + "/Contents 4 0 R /Resources << >> >>\nendobj\n"
+ + "4 0 obj\n<< /Length " + content.length() + " >>\nstream\n"
+ + content + "\nendstream\nendobj\n"
+ + "trailer\n<< /Root 1 0 R /Size 5 >>\nstartxref\n0\n%%EOF\n";
+ RepairResult result = PdfRepair.builder()
+ .input(pdf.getBytes(StandardCharsets.ISO_8859_1))
+ .all()
+ .build()
+ .execute();
+ assertTrue(result.isUsable(), "missing /Pages tree must be rebuilt: " + result.status());
+ try (PdfDocument doc = PdfDocument.open(result.repairedPdf())) {
+ assertEquals(1, doc.pageCount(), "the orphaned page dictionary must survive");
+ }
+ }
+
+ @Test
+ void rejectsPdfWithoutPageObjects() throws Exception {
+ Assumptions.assumeTrue(NativeRuntime.isFull(), "Requires full native repair bridge");
+ String pdf = "%PDF-1.4\n"
+ + "1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"
+ + "trailer\n<< /Root 1 0 R /Size 3 >>\nstartxref\n0\n%%EOF\n";
+ RepairResult result = PdfRepair.builder()
+ .input(pdf.getBytes(StandardCharsets.ISO_8859_1))
+ .all()
+ .build()
+ .execute();
+ assertFalse(result.isUsable(), "a file without page objects cannot be repaired");
+ assertEquals(RepairResult.Status.FAILED, result.status());
+ }
+
+ @Test
+ void rejectsHtmlDisguisedAsPdf() throws Exception {
+ Assumptions.assumeTrue(NativeRuntime.isFull(), "Requires full native repair bridge");
+ byte[] html = "not a pdf"
+ .getBytes(StandardCharsets.ISO_8859_1);
+ RepairResult result = PdfRepair.builder().input(html).all().build().execute();
+ assertFalse(result.isUsable(), "HTML renamed to .pdf must fail closed");
+ }
+
+ @Test
+ void rejectsPasswordProtectedPdf() throws Exception {
+ Assumptions.assumeTrue(NativeRuntime.isFull(), "Requires full native repair bridge");
+ byte[] encrypted;
+ try (PDDocument doc = new PDDocument()) {
+ org.apache.pdfbox.pdmodel.PDPage page = new org.apache.pdfbox.pdmodel.PDPage();
+ doc.addPage(page);
+ doc.protect(new org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy(
+ "owner", "user", new org.apache.pdfbox.pdmodel.encryption.AccessPermission()));
+ java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
+ doc.save(out);
+ encrypted = out.toByteArray();
+ }
+ RepairResult result = PdfRepair.builder().input(encrypted).all().build().execute();
+ assertFalse(result.isUsable(), "password-protected input must fail closed: status="
+ + result.status() + " len=" + (result.repairedPdf() == null ? 0 : result.repairedPdf().length));
+ }
+
+ @Test
+ void recoversCompletePagesFromPartiallyDownloadedInlinePdf() throws Exception {
+ byte[] valid = inlineThreePagePdf();
+ String text = new String(valid, StandardCharsets.ISO_8859_1);
+ int cut = text.lastIndexOf("endobj") + "endobj".length();
+ byte[] damaged = java.util.Arrays.copyOf(valid, cut);
+
+ Assumptions.assumeTrue(NativeRuntime.isFull(), "Requires full native repair bridge");
+ RepairResult result = PdfRepair.builder().input(damaged).all().build().execute();
+ assertTrue(result.isUsable(), "complete page objects must be salvageable: "
+ + result.status() + " " + result.diagnosticJson());
+ try (PdfDocument doc = PdfDocument.open(result.repairedPdf())) {
+ assertEquals(3, doc.pageCount(), "all complete pages must survive");
+ }
+ try (PDDocument doc = Loader.loadPDF(result.repairedPdf())) {
+ String out = new PDFTextStripper().getText(doc);
+ assertTrue(out.contains("Page 1"), "missing first page text: " + out);
+ assertTrue(out.contains("Page 3"), "missing last page text: " + out);
+ }
+ }
+
+ @Test
+ void repairsMissingTrailerRootReference() throws Exception {
+ byte[] damaged = stripRootReference(RepairCorpus.validMultiPage());
+ assertRepaired(damaged, true);
+ }
+
+ @Test
+ void repairsCorruptCatalogType() throws Exception {
+ byte[] damaged = corruptCatalogType(RepairCorpus.validMultiPage());
+ assertRepaired(damaged, true);
+ }
+
+ @Test
+ void repairsTruncatedObjKeyword() throws Exception {
+ byte[] damaged = truncateObjKeyword(RepairCorpus.validMultiPage());
+ assertRepaired(damaged, true);
+ }
+
+ @Test
+ void repairsCatalogEndobjGluedToNextObject() throws Exception {
+ byte[] damaged = glueFirstEndobj(RepairCorpus.validMultiPage());
+ assertRepaired(damaged, true);
+ }
+
+ @Test
+ void repairsJunkBeforeEndobj() throws Exception {
+ byte[] damaged = junkBeforeEndobj(RepairCorpus.validMultiPage());
+ assertRepaired(damaged, true);
+ }
+
+ @Test
+ void repairsEndobjGluedToNextObject() throws Exception {
+ byte[] damaged = glueEndobj(RepairCorpus.validMultiPage());
+ assertRepaired(damaged, true);
+ }
+
+ private static void assertFailsClosed(byte[] damaged) throws IOException {
+ Assumptions.assumeTrue(NativeRuntime.isFull(), "Requires full native repair bridge");
+ RepairResult result = PdfRepair.builder().input(damaged).all().build().execute();
+ assertNotNull(result);
+ if (result.isUsable()) {
+ try (PdfDocument doc = PdfDocument.open(result.repairedPdf())) {
+ assertTrue(doc.pageCount() >= 1,
+ "a usable repair must contain a page: " + result.status());
+ }
+ }
+ }
+
+ private static byte[] brokenHeaderBadPagesCountPdf() {
+ String pdf = "%P\u0000F-1.4\n"
+ + "1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"
+ + "2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Counte /Page\n"
+ + "/Parent 2 0 R /MediaBox [0 0 612 792] >>\nendobj\n"
+ + "3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] >>\nendobj\n"
+ + "trailer\n<< /Root 1 0 R >>\nstartxref\n0\n%%EOF\n";
+ return pdf.getBytes(StandardCharsets.ISO_8859_1);
+ }
+
+ @Test
+ void deduplicatesResourceKeysPerDictionary() throws Exception {
+ StringBuilder pdf = new StringBuilder("%PDF-1.4\n");
+ pdf.append("1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n");
+ pdf.append("2 0 obj\n<< /Type /Pages /Kids [3 0 R 4 0 R] /Count 2 >>\nendobj\n");
+ for (int page = 3; page <= 4; page++) {
+ pdf.append(page).append(" 0 obj\n<< /Type /Page /Parent 2 0 R ")
+ .append("/MediaBox [0 0 612 792] /Resources <<");
+ for (int i = 0; i < 40; i++) pdf.append(" /Im1 1 0 R");
+ pdf.append(" >> >>\nendobj\n");
+ }
+ pdf.append("trailer\n<< /Root 1 0 R /Size 5 >>\nstartxref\n0\n%%EOF\n");
+ java.lang.reflect.Method dedup =
+ PdfRepair.class.getDeclaredMethod("deduplicateResourceKeys", byte[].class);
+ dedup.setAccessible(true);
+ byte[] out = (byte[]) dedup.invoke(null,
+ pdf.toString().getBytes(StandardCharsets.ISO_8859_1));
+ String text = new String(out, StandardCharsets.ISO_8859_1);
+ assertEquals(2, text.split("/Im1", -1).length - 1,
+ "each resource dictionary must keep its own /Im1");
+ }
+
+ @Test
+ void rebuildKeepsKidsOrder() throws Exception {
+ Assumptions.assumeTrue(NativeRuntime.isFull(), "Requires full native repair bridge");
+ String pdf = "%PDF-1.4\n"
+ + "1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"
+ + "2 0 obj\n<< /Type /Pages /Kids [4 0 R 3 0 R] /Count 2 >>\nendobj\n"
+ + pageWithText(3, "FIRST")
+ + pageWithText(4, "SECOND")
+ + FONT_OBJECT
+ + "trailer\n<< /Root 1 0 R /Size 5 >>\nstartxref\n0\n%%EOF\n";
+ RepairResult result = repairBrokenHeader(pdf);
+ assertTrue(result.isUsable(), "ordered page tree must repair: " + result.status());
+ try (PdfDocument doc = PdfDocument.open(result.repairedPdf())) {
+ assertEquals(2, doc.pageCount());
+ }
+ try (PDDocument doc = Loader.loadPDF(result.repairedPdf())) {
+ String text = new PDFTextStripper().getText(doc);
+ assertTrue(text.indexOf("SECOND") < text.indexOf("FIRST"),
+ "/Kids order must be preserved, got: " + text);
+ }
+ }
+
+ @Test
+ void oversizedObjectNumbersFailWithoutThrowing() throws Exception {
+ Assumptions.assumeTrue(NativeRuntime.isFull(), "Requires full native repair bridge");
+ String pdf = "%PDF-1.4\n"
+ + "1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"
+ + "2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n"
+ + pageWithText(3, "NORMAL")
+ + FONT_OBJECT
+ + "99999999999999999999 0 obj\n<< /Type /Page >>\nendobj\n"
+ + "trailer\n<< /Root 1 0 R /Size 4 >>\nstartxref\n0\n%%EOF\n";
+ RepairResult result = assertDoesNotThrow(() -> repairBrokenHeader(pdf));
+ assertNotNull(result.status());
+ }
+
+ @Test
+ void rebuildInheritsAncestorPageAttributes() throws Exception {
+ Assumptions.assumeTrue(NativeRuntime.isFull(), "Requires full native repair bridge");
+ String pdf = "%PDF-1.4\n"
+ + "1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n"
+ + "2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 "
+ + "/MediaBox [0 0 200 300] /Rotate 90 /Resources << >> >>\nendobj\n"
+ + "3 0 obj\n<< /Type /Page /Parent 2 0 R >>\nendobj\n"
+ + "trailer\n<< /Root 1 0 R /Size 4 >>\nstartxref\n0\n%%EOF\n";
+ RepairResult result = repairBrokenHeader(pdf);
+ assertTrue(result.isUsable(), "inherited attributes must repair: " + result.status());
+ try (PdfDocument doc = PdfDocument.open(result.repairedPdf())) {
+ assertEquals(1, doc.pageCount());
+ // All three asserts share one page handle: three unclosed doc.page()
+ // calls would pin three tracked pages for the rest of the JVM.
+ try (PdfPage page = doc.page(0)) {
+ assertEquals(300.0, page.size().width(), 1.0);
+ assertEquals(200.0, page.size().height(), 1.0);
+ assertEquals(1, page.rotation(), "90-degree rotation must be inherited");
+ }
+ }
+ }
+
+ @Test
+ void rebuildPreservesExistingCatalogEntries() throws Exception {
+ Assumptions.assumeTrue(NativeRuntime.isFull(), "Requires full native repair bridge");
+ String pdf = "%PDF-1.4\n"
+ + "1 0 obj\n<< /Type /Catalog /Pages 2 0 R /AcroForm 7 0 R "
+ + "/Outlines 8 0 R >>\nendobj\n"
+ + "2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n"
+ + pageWithText(3, "CATALOG")
+ + FONT_OBJECT
+ + "7 0 obj\n<< /Fields [] >>\nendobj\n"
+ + "8 0 obj\n<< /Type /Outlines /Count 0 >>\nendobj\n"
+ + "trailer\n<< /Root 1 0 R /Size 9 >>\nstartxref\n0\n%%EOF\n";
+ RepairResult result = repairBrokenHeader(pdf);
+ assertTrue(result.isUsable(), "catalog must survive rebuild: " + result.status());
+ try (PDDocument doc = Loader.loadPDF(result.repairedPdf())) {
+ org.apache.pdfbox.cos.COSDictionary catalog =
+ doc.getDocumentCatalog().getCOSObject();
+ assertTrue(catalog.containsKey(org.apache.pdfbox.cos.COSName.ACRO_FORM),
+ "existing catalog entries must be preserved");
+ assertTrue(catalog.containsKey(org.apache.pdfbox.cos.COSName.OUTLINES),
+ "outlines must be preserved");
+ }
+ }
+
+ @Test
+ void rebuildUsesPageDictionaryNotNestedResources() throws Exception {
+ Assumptions.assumeTrue(NativeRuntime.isFull(), "Requires full native repair bridge");
+ String content = "0 0 100 100 re f";
+ String pdf = "%PDF-1.4\n"
+ + "1 0 obj\n<< /Type /Catalog /Pages 3 0 R >>\nendobj\n"
+ + "2 0 obj\n<< /Length " + content.length() + " >>\nstream\n"
+ + content + "\nendstream\nendobj\n"
+ + "<>/Type/Page/MediaBox [0 0 200 300]"
+ + "/Contents 2 0 R>>\n"
+ + "trailer\n<< /Root 1 0 R /Size 4 >>\nstartxref\n0\n%%EOF\n";
+ RepairResult result = repairBrokenHeader(pdf);
+ assertTrue(result.isUsable(), "orphan with Resources before Type must repair: " + result.status());
+ try (PdfDocument doc = PdfDocument.open(result.repairedPdf())) {
+ assertEquals(1, doc.pageCount());
+ try (PdfPage page = doc.page(0)) {
+ assertEquals(200.0, page.size().width(), 1.0);
+ }
+ }
+ }
+
+ private static String pageWithText(int object, String text) {
+ String content = "BT /F1 12 Tf 72 700 Td (" + text + ") Tj ET";
+ return object + " 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] "
+ + "/Resources << /Font << /F1 9 0 R >> >> /Contents " + (object + 2)
+ + " 0 R >>\nendobj\n" + (object + 2) + " 0 obj\n<< /Length "
+ + content.length() + " >>\nstream\n" + content + "\nendstream\nendobj\n";
+ }
+
+ private static final String FONT_OBJECT =
+ "9 0 obj\n<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>\nendobj\n";
+
+ private static RepairResult repairBrokenHeader(String pdf) {
+ String broken = pdf.replaceFirst("%PDF-1\\.4", "%P\u00b5F-1.4");
+ return PdfRepair.builder()
+ .input(broken.getBytes(StandardCharsets.ISO_8859_1))
+ .all()
+ .build()
+ .execute();
+ }
+
+ private static void assertRepaired(byte[] damaged, boolean expectText) throws IOException {
+ assertRepaired(damaged, expectText, 3);
+ }
+
+ private static void assertRepaired(byte[] damaged, boolean expectText, int expectedPages)
+ throws IOException {
+ Assumptions.assumeTrue(NativeRuntime.isFull(), "Requires full native repair bridge");
+ RepairResult result = PdfRepair.builder().input(damaged).all().build().execute();
+ assertNotNull(result);
+ assertTrue(result.isUsable(), "repair failed: " + result.status() + " " + result.diagnosticJson());
+ assertNotEquals(RepairResult.Status.CLEAN, result.status(),
+ "damaged input must not be reported as clean");
+ try (PdfDocument doc = PdfDocument.open(result.repairedPdf())) {
+ assertEquals(expectedPages, doc.pageCount(), "page count must survive repair");
+ }
+ try (PDDocument doc = Loader.loadPDF(result.repairedPdf())) {
+ assertEquals(expectedPages, doc.getNumberOfPages());
+ if (expectText) {
+ String text = new PDFTextStripper().getText(doc);
+ assertTrue(text.contains("Repair test page 1"), "missing repaired text: " + text);
+ }
+ }
+ }
+
+ @Test
+ void repairsTruncatedPageDictionaryMidArrayWithJunkHeader() throws Exception {
+ Assumptions.assumeTrue(NativeRuntime.isFull(), "Requires full native repair bridge");
+ String valid = new String(inlineThreePagePdf(), StandardCharsets.ISO_8859_1);
+ int pageObj = valid.indexOf("3 0 obj");
+ int annots = valid.indexOf("/Contents 6 0 R >>", pageObj);
+ assertTrue(annots > 0, "fixture must have a page dictionary to cut");
+ int cut = annots + 10;
+ String damaged = valid.substring(0, cut)
+ + " 11 0 R 13 0 R 16 0%PDF-1.3\n%junk injected by a broken merge\n"
+ + "garbage that is not a pdf object at all\n";
+ byte[] input = damaged.getBytes(StandardCharsets.ISO_8859_1);
+ RepairResult result = PdfRepair.builder().input(input).all().build().execute();
+ assertTrue(result.isUsable(),
+ "truncated page dict with junk header must repair: " + result.status());
+ try (PdfDocument doc = PdfDocument.open(result.repairedPdf())) {
+ assertTrue(doc.pageCount() >= 1, "at least one page must survive");
+ }
+ try (PDDocument doc = Loader.loadPDF(result.repairedPdf())) {
+ assertTrue(doc.getNumberOfPages() >= 1);
+ }
+ }
+
+ private static byte[] inlineThreePagePdf() {
+ java.util.List objects = new java.util.ArrayList<>();
+ objects.add("<< /Type /Catalog /Pages 2 0 R >>");
+ objects.add("<< /Type /Pages /Kids [3 0 R 4 0 R 5 0 R] /Count 3 >>");
+ for (int i = 0; i < 3; i++) {
+ objects.add("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] "
+ + "/Resources << /Font << /F1 9 0 R >> >> /Contents " + (6 + i) + " 0 R >>");
+ }
+ for (int i = 1; i <= 3; i++) {
+ String content = "BT /F1 12 Tf 72 700 Td (Page " + i + ") Tj ET";
+ objects.add("<< /Length " + content.length() + " >>\nstream\n" + content
+ + "\nendstream");
+ }
+ objects.add("<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>");
+
+ StringBuilder out = new StringBuilder("%PDF-1.4\n");
+ int[] offsets = new int[objects.size()];
+ for (int i = 0; i < objects.size(); i++) {
+ offsets[i] = out.length();
+ out.append(i + 1).append(" 0 obj\n").append(objects.get(i)).append("\nendobj\n");
+ }
+ int xref = out.length();
+ out.append("xref\n0 ").append(objects.size() + 1).append("\n");
+ out.append("0000000000 65535 f \n");
+ for (int offset : offsets) {
+ out.append(String.format("%010d 00000 n \n", offset));
+ }
+ out.append("trailer\n<< /Size ").append(objects.size() + 1)
+ .append(" /Root 1 0 R >>\nstartxref\n").append(xref).append("\n%%EOF\n");
+ return out.toString().getBytes(StandardCharsets.ISO_8859_1);
+ }
+
+ private static byte[] stripRootReference(byte[] pdf) {
+ String s = new String(pdf, StandardCharsets.ISO_8859_1);
+ String out = s.replaceFirst("/Root\\s+\\d+\\s+\\d+\\s+R", "");
+ return out.getBytes(StandardCharsets.ISO_8859_1);
+ }
+
+ private static byte[] corruptCatalogType(byte[] pdf) {
+ String s = new String(pdf, StandardCharsets.ISO_8859_1);
+ return s.replaceFirst("/Type\\s*/Catalog", "/Type /CatalogX")
+ .getBytes(StandardCharsets.ISO_8859_1);
+ }
+
+ private static byte[] truncateObjKeyword(byte[] pdf) {
+ String s = new String(pdf, StandardCharsets.ISO_8859_1);
+ int stream = s.indexOf("stream\n");
+ int obj = s.lastIndexOf(" obj", stream);
+ if (obj < 0) return pdf;
+ return (s.substring(0, obj) + " o" + s.substring(obj + 4))
+ .getBytes(StandardCharsets.ISO_8859_1);
+ }
+
+ private static byte[] junkBeforeEndobj(byte[] pdf) {
+ String s = new String(pdf, StandardCharsets.ISO_8859_1);
+ int end = s.indexOf("endobj");
+ if (end < 0) return pdf;
+ return (s.substring(0, end) + ">> " + s.substring(end))
+ .getBytes(StandardCharsets.ISO_8859_1);
+ }
+
+ private static byte[] glueFirstEndobj(byte[] pdf) {
+ String s = new String(pdf, StandardCharsets.ISO_8859_1);
+ int end = s.indexOf("endobj\n");
+ if (end < 0) return pdf;
+ return (s.substring(0, end) + "endobj" + s.substring(end + 7))
+ .getBytes(StandardCharsets.ISO_8859_1);
+ }
+
+ private static byte[] glueEndobj(byte[] pdf) {
+ String s = new String(pdf, StandardCharsets.ISO_8859_1);
+ int stream = s.indexOf("stream\n");
+ int end = s.indexOf("endobj\n", stream);
+ if (end < 0) return pdf;
+ return (s.substring(0, end) + "endobj" + s.substring(end + 7))
+ .getBytes(StandardCharsets.ISO_8859_1);
+ }
+
+ private static byte[] duplicateFirstStreamObject(byte[] pdf) {
+ String s = new String(pdf, StandardCharsets.ISO_8859_1);
+ int stream = s.indexOf("stream\n");
+ int obj = s.lastIndexOf(" obj", stream);
+ int start = s.lastIndexOf('\n', obj) + 1;
+ int end = s.indexOf("endobj", stream) + "endobj".length();
+ String object = s.substring(start, end);
+ int xref = s.lastIndexOf("xref");
+ if (stream < 0 || obj < 0 || end < 0 || xref < 0) return pdf;
+ return (s.substring(0, xref) + object + "\n" + s.substring(xref))
+ .getBytes(StandardCharsets.ISO_8859_1);
+ }
+
+ private static byte[] cutStreamTail(byte[] pdf) {
+ String s = new String(pdf, StandardCharsets.ISO_8859_1);
+ int stream = s.indexOf("stream\n");
+ int endstream = s.indexOf("endstream", stream);
+ if (stream < 0 || endstream < 0) return pdf;
+ int cut = Math.max(stream + 8, endstream - 24);
+ return (s.substring(0, cut) + s.substring(endstream)).getBytes(StandardCharsets.ISO_8859_1);
+ }
+
+ private static byte[] stripXrefTable(byte[] pdf) {
+ String s = new String(pdf, StandardCharsets.ISO_8859_1);
+ int typeXref = s.indexOf("/Type /XRef");
+ if (typeXref < 0) {
+ int xref = s.lastIndexOf("\nxref");
+ int trailer = s.indexOf("trailer", xref);
+ if (xref < 0 || trailer < 0) return pdf;
+ return (s.substring(0, xref) + s.substring(trailer))
+ .getBytes(StandardCharsets.ISO_8859_1);
+ }
+ int obj = s.lastIndexOf(" obj", typeXref);
+ int start = s.lastIndexOf('\n', obj) + 1;
+ int stream = s.indexOf("stream", typeXref);
+ int end = s.indexOf("endobj", stream) + "endobj".length();
+ if (start <= 0 || stream < 0 || end < start) return pdf;
+ String dict = s.substring(s.lastIndexOf("<<", typeXref), stream);
+ StringBuilder trailer = new StringBuilder("<<\n");
+ for (String line : dict.split("\n")) {
+ if (line.contains("/Root") || line.contains("/Info") || line.contains("/ID")) {
+ trailer.append(line).append('\n');
+ }
+ }
+ trailer.append(">>\n");
+ return (s.substring(0, start) + s.substring(end) + "\ntrailer\n" + trailer
+ + "startxref\n0\n%%EOF")
+ .getBytes(StandardCharsets.ISO_8859_1);
+ }
+
+ private static byte[] stripTwoEndobj(byte[] pdf) {
+ String s = new String(pdf, StandardCharsets.ISO_8859_1);
+ for (int i = 0; i < 2; i++) {
+ int at = s.indexOf("endobj");
+ if (at < 0) break;
+ s = s.substring(0, at) + s.substring(at + "endobj".length());
+ }
+ return s.getBytes(StandardCharsets.ISO_8859_1);
+ }
+}
diff --git a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfRepairTest.java b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfRepairTest.java
index 3aa01c9e5..b2dfa1179 100644
--- a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfRepairTest.java
+++ b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfRepairTest.java
@@ -23,6 +23,7 @@
import java.util.TreeSet;
import java.util.stream.Collectors;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -95,6 +96,95 @@ void inspectReturnsNonNullJson() {
assertFalse(diag.isBlank());
}
+ @Test
+ void inspectDetectsZeroAreaClipPath() {
+ Assumptions.assumeTrue(PdfRepair.isSupported(), "zero-area clip scan needs qpdf");
+ Assumptions.assumeTrue(NativeRuntime.isFull(), "zero-area clip scan needs real native qpdf");
+ String json = PdfRepair.inspect(singlePagePdf(
+ "q 0 0 0 0 re W n BT /F1 24 Tf 72 700 Td (Hidden) Tj ET Q", false));
+ assertTrue(json.contains("\"zero_area_clip_pages\":[1]"),
+ "page with 0-width clip must be listed: " + json);
+ assertTrue(json.contains("zero-area clipping path applied"),
+ "a diagnostic issue entry must explain the blank-render risk: " + json);
+ }
+
+ @Test
+ void inspectDetectsZeroAreaClipInCompressedContent() throws Exception {
+ Assumptions.assumeTrue(PdfRepair.isSupported(), "zero-area clip scan needs qpdf");
+ Assumptions.assumeTrue(NativeRuntime.isFull(), "zero-area clip scan needs real native qpdf");
+ String json = PdfRepair.inspect(singlePagePdf(
+ "q 0 0 0 0 re W n BT /F1 24 Tf 72 700 Td (Hidden) Tj ET Q", true));
+ assertTrue(json.contains("\"zero_area_clip_pages\":[1]"),
+ "FlateDecode content must be scanned decompressed: " + json);
+ }
+
+ @Test
+ void inspectReportsNoClipIssueForCleanContent() {
+ Assumptions.assumeTrue(PdfRepair.isSupported(), "zero-area clip scan needs qpdf");
+ Assumptions.assumeTrue(NativeRuntime.isFull(), "zero-area clip scan needs real native qpdf");
+ String json = PdfRepair.inspect(singlePagePdf(
+ "BT /F1 24 Tf 72 700 Td (Hello) Tj ET", false));
+ assertTrue(json.contains("\"zero_area_clip_pages\":[]"),
+ "clean page must not be flagged: " + json);
+ assertFalse(json.contains("zero-area"), "no issue entry without a degenerate clip");
+ }
+
+ /**
+ * Builds a structurally valid one-page PDF whose content stream is the given
+ * operators, optionally Flate-compressed, with a real xref table so no
+ * reconstruction is involved.
+ */
+ private static byte[] singlePagePdf(String content, boolean flate) {
+ byte[] streamBytes;
+ String dict;
+ if (flate) {
+ java.util.zip.Deflater def = new java.util.zip.Deflater();
+ def.setInput(content.getBytes(StandardCharsets.US_ASCII));
+ def.finish();
+ byte[] buf = new byte[4096];
+ int len = 0;
+ for (;;) {
+ int chunk = def.deflate(buf, len, buf.length - len);
+ if (chunk <= 0) break;
+ len += chunk;
+ }
+ def.end();
+ streamBytes = java.util.Arrays.copyOf(buf, len);
+ dict = "<< /Length " + streamBytes.length + " /Filter /FlateDecode >>";
+ } else {
+ streamBytes = content.getBytes(StandardCharsets.US_ASCII);
+ dict = "<< /Length " + streamBytes.length + " >>";
+ }
+ java.io.ByteArrayOutputStream pdf = new java.io.ByteArrayOutputStream();
+ pdf.writeBytes("%PDF-1.4\n".getBytes(StandardCharsets.US_ASCII));
+ String[] bodies = {
+ "<< /Type /Catalog /Pages 2 0 R >>",
+ "<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
+ "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792]"
+ + " /Resources << /Font << /F1 4 0 R >> >> /Contents 5 0 R >>",
+ "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>",
+ };
+ int[] offsets = new int[6];
+ for (int i = 0; i < bodies.length; i++) {
+ offsets[i + 1] = pdf.size();
+ pdf.writeBytes(((i + 1) + " 0 obj\n" + bodies[i] + "\nendobj\n")
+ .getBytes(StandardCharsets.US_ASCII));
+ }
+ offsets[5] = pdf.size();
+ pdf.writeBytes(("5 0 obj\n" + dict + "\nstream\n").getBytes(StandardCharsets.US_ASCII));
+ pdf.writeBytes(streamBytes);
+ pdf.writeBytes("\nendstream\nendobj\n".getBytes(StandardCharsets.US_ASCII));
+ int xref = pdf.size();
+ pdf.writeBytes("xref\n0 6\n0000000000 65535 f \n".getBytes(StandardCharsets.US_ASCII));
+ for (int obj = 1; obj <= 5; obj++) {
+ pdf.writeBytes(String.format("%010d 00000 n \n", offsets[obj])
+ .getBytes(StandardCharsets.US_ASCII));
+ }
+ pdf.writeBytes(("trailer\n<< /Root 1 0 R /Size 6 >>\nstartxref\n" + xref + "\n%%EOF\n")
+ .getBytes(StandardCharsets.US_ASCII));
+ return pdf.toByteArray();
+ }
+
@Test
void inspectHandlesEmptyInput() {
assertEquals("{\"error\":\"empty input\"}", PdfRepair.inspect(EMPTY_BYTES));
@@ -352,6 +442,42 @@ void repairsSynthesizedDefectedFixtures() throws IOException {
}
}
+ @Test
+ void repairedOutputIsCompactedWithoutLosingContent() throws Exception {
+ Assumptions.assumeTrue(NativeRuntime.isFull(), "Requires full native repair bridge");
+ byte[] damaged = RepairCorpus.zeroXref(RepairCorpus.validMultiPage());
+ byte[] qdf = PdfOptimizer.repair(damaged);
+ assertNotNull(qdf);
+
+ RepairResult result = PdfRepair.builder().input(damaged).all().build().execute();
+ assertTrue(result.isUsable());
+ assertTrue(result.repairedPdf().length < qdf.length,
+ "repaired output must be smaller than raw QDF: "
+ + result.repairedPdf().length + " vs " + qdf.length);
+ try (PDDocument reopened = Loader.loadPDF(result.repairedPdf())) {
+ assertEquals(3, reopened.getNumberOfPages());
+ assertTrue(new PDFTextStripper().getText(reopened).contains("Repair test page 1"));
+ }
+ }
+
+ @Test
+ void repairsMalformedEncryptionWithEmptyUserPassword() throws Exception {
+ Assumptions.assumeTrue(NativeRuntime.isFull(), "Requires full native repair bridge");
+ byte[] input;
+ try (var is = PdfRepairTest.class.getResourceAsStream(
+ "/pdfs/repair/encrypted-empty-user-malformed-length.pdf")) {
+ assertNotNull(is);
+ input = is.readAllBytes();
+ }
+ RepairResult result = PdfRepair.builder().input(input).all().build().execute();
+ assertTrue(result.isUsable(), "malformed empty-user encryption must repair: " + result.status());
+ try (var doc = stirling.software.jpdfium.PdfDocument.open(result.repairedPdf())) {
+ assertEquals(1, doc.pageCount());
+ }
+ String out = new String(result.repairedPdf(), StandardCharsets.ISO_8859_1);
+ assertFalse(out.contains("/Encrypt"), "repair must return a decrypted working document");
+ }
+
@Test
void repairsBrokenPagesTreePreservingInheritedAttributes() throws Exception {
Assumptions.assumeTrue(NativeRuntime.isFull(), "Requires full native repair bridge");
@@ -615,4 +741,22 @@ void streamDataContainingEndstreamKeywordStaysUntouched() throws Exception {
"stream data must not be edited by the repair-token normalizers");
}
}
+
+ @Test
+ void signedDocumentsAreNotRewritten() {
+ byte[] marker = "\n% /ByteRange [0 1 2 3]\n".getBytes(StandardCharsets.US_ASCII);
+ byte[] signed = new byte[MINIMAL_PDF.length + marker.length];
+ System.arraycopy(MINIMAL_PDF, 0, signed, 0, MINIMAL_PDF.length);
+ System.arraycopy(marker, 0, signed, MINIMAL_PDF.length, marker.length);
+
+ RepairResult qdf = PdfRepair.repairQdf(signed);
+ assertEquals(RepairResult.Status.CLEAN, qdf.status());
+ assertArrayEquals(signed, qdf.repairedPdf(),
+ "a signed document must not be rewritten by QDF repair");
+
+ RepairResult cascade = PdfRepair.builder().input(signed).all().build().execute();
+ assertEquals(RepairResult.Status.CLEAN, cascade.status());
+ assertArrayEquals(signed, cascade.repairedPdf(),
+ "a signed document must not be rewritten by the repair cascade");
+ }
}
diff --git a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfXConverterTest.java b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfXConverterTest.java
index fb1f6c2e8..2323aa855 100644
--- a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfXConverterTest.java
+++ b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfXConverterTest.java
@@ -1,10 +1,13 @@
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.COSFloat;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.cos.COSObject;
import org.apache.pdfbox.cos.COSStream;
+import org.apache.pdfbox.cos.COSString;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
@@ -1166,4 +1169,182 @@ private static byte[] nestedForms(int depth) throws IOException {
return out.toByteArray();
}
}
+
+ @Test
+ void shadingDefaultRgbAndDeviceNColorantsConvertToCmyk() throws Exception {
+ assumeTrue(PdfXConverter.isSupported(), "PDF/X native pipeline unavailable");
+ byte[] input;
+ try (PDDocument doc = new PDDocument();
+ ByteArrayOutputStream bytes = new ByteArrayOutputStream()) {
+ PDPage page = new PDPage();
+ doc.addPage(page);
+
+ COSDictionary shFn = new COSDictionary();
+ shFn.setInt(COSName.FUNCTION_TYPE, 2);
+ shFn.setItem(COSName.DOMAIN, cosFloats(0f, 1f));
+ shFn.setItem(COSName.C0, cosFloats(1f, 0f, 0f));
+ shFn.setItem(COSName.C1, cosFloats(0f, 0f, 1f));
+ shFn.setInt(COSName.N, 1);
+
+ COSDictionary shading = new COSDictionary();
+ shading.setInt(COSName.SHADING_TYPE, 2);
+ shading.setItem(COSName.COLORSPACE, COSName.DEVICERGB);
+ shading.setItem(COSName.COORDS, cosFloats(0f, 0f, 100f, 0f));
+ shading.setItem(COSName.FUNCTION, shFn);
+ shading.setItem(COSName.BACKGROUND, cosFloats(1f, 1f, 0f));
+ COSArray extend = new COSArray();
+ extend.add(org.apache.pdfbox.cos.COSBoolean.TRUE);
+ extend.add(org.apache.pdfbox.cos.COSBoolean.TRUE);
+ shading.setItem(COSName.EXTEND, extend);
+
+ COSDictionary sepFn = new COSDictionary();
+ sepFn.setInt(COSName.FUNCTION_TYPE, 2);
+ sepFn.setItem(COSName.DOMAIN, cosFloats(0f, 1f));
+ sepFn.setItem(COSName.C0, cosFloats(1f, 1f, 1f));
+ sepFn.setItem(COSName.C1, cosFloats(1f, 0f, 0f));
+ sepFn.setInt(COSName.N, 1);
+ COSArray colorantSep = new COSArray();
+ colorantSep.add(COSName.SEPARATION);
+ colorantSep.add(COSName.getPDFName("SpotA"));
+ colorantSep.add(COSName.DEVICERGB);
+ colorantSep.add(sepFn);
+
+ COSDictionary colorants = new COSDictionary();
+ colorants.setItem(COSName.getPDFName("SpotA"), colorantSep);
+ COSDictionary process = new COSDictionary();
+ process.setItem(COSName.COLORSPACE, COSName.DEVICERGB);
+ COSDictionary attrs = new COSDictionary();
+ attrs.setItem(COSName.getPDFName("Colorants"), colorants);
+ attrs.setItem(COSName.getPDFName("Process"), process);
+
+ COSStream dnFn = doc.getDocument().createCOSStream();
+ dnFn.setInt(COSName.FUNCTION_TYPE, 4);
+ dnFn.setItem(COSName.DOMAIN, cosFloats(0f, 1f));
+ dnFn.setItem(COSName.RANGE, cosFloats(0f, 1f, 0f, 1f, 0f, 1f));
+ try (var os = dnFn.createOutputStream()) {
+ os.write("{ 1 exch sub dup }".getBytes(StandardCharsets.US_ASCII));
+ }
+ COSArray inks = new COSArray();
+ inks.add(COSName.getPDFName("SpotA"));
+ COSArray deviceN = new COSArray();
+ deviceN.add(COSName.DEVICEN);
+ deviceN.add(inks);
+ deviceN.add(COSName.DEVICERGB);
+ deviceN.add(dnFn);
+ deviceN.add(attrs);
+
+ COSDictionary spaces = new COSDictionary();
+ spaces.setItem(COSName.getPDFName("DefaultRGB"), COSName.DEVICERGB);
+ spaces.setItem(COSName.getPDFName("DN"), deviceN);
+ COSDictionary shadings = new COSDictionary();
+ shadings.setItem(COSName.getPDFName("Sh1"), shading);
+
+ PDResources resources = new PDResources();
+ resources.getCOSObject().setItem(COSName.COLORSPACE, spaces);
+ resources.getCOSObject().setItem(COSName.SHADING, shadings);
+ page.setResources(resources);
+
+ COSStream content = doc.getDocument().createCOSStream();
+ try (var os = content.createOutputStream()) {
+ os.write("/Sh1 sh /DN cs 0.4 sc 0 0 10 10 re f".getBytes(StandardCharsets.US_ASCII));
+ }
+ page.getCOSObject().setItem(COSName.CONTENTS, content);
+ doc.save(bytes);
+ input = bytes.toByteArray();
+ }
+
+ byte[] out = PdfXConverter.applyMetadata(input, PdfXOptions.builder()
+ .standard(PdfXStandard.PDF_X_4)
+ .useCompactCmyk()
+ .title("Shading and DeviceN")
+ .build());
+
+ try (PDDocument doc = Loader.loadPDF(out)) {
+ COSDictionary res = doc.getPage(0).getResources().getCOSObject();
+ COSDictionary spaces = res.getCOSDictionary(COSName.COLORSPACE);
+ assertNotNull(spaces);
+ assertFalse(spaces.containsKey(COSName.getPDFName("DefaultRGB")),
+ "/DefaultRGB must be removed rather than rebound to /DeviceCMYK");
+
+ COSArray dn = (COSArray) spaces.getDictionaryObject(COSName.getPDFName("DN"));
+ assertEquals(COSName.DEVICECMYK, dn.getObject(2));
+ COSDictionary outAttrs = (COSDictionary) dn.getObject(4);
+ COSArray spotA = (COSArray) outAttrs.getCOSDictionary(COSName.getPDFName("Colorants"))
+ .getDictionaryObject(COSName.getPDFName("SpotA"));
+ assertEquals(COSName.DEVICECMYK, spotA.getObject(2));
+ assertEquals(COSName.DEVICECMYK, outAttrs.getCOSDictionary(COSName.getPDFName("Process"))
+ .getDictionaryObject(COSName.COLORSPACE));
+
+ COSDictionary sh = res.getCOSDictionary(COSName.SHADING).getCOSDictionary(COSName.getPDFName("Sh1"));
+ assertEquals(COSName.DEVICECMYK, sh.getDictionaryObject(COSName.COLORSPACE));
+ assertEquals(4, sh.getCOSArray(COSName.BACKGROUND).size());
+ COSDictionary convertedFn = sh.getCOSDictionary(COSName.FUNCTION);
+ assertEquals(4, convertedFn.getCOSArray(COSName.C0).size());
+ assertEquals(4, convertedFn.getCOSArray(COSName.C1).size());
+ }
+ }
+
+ @Test
+ void forbiddenActionsEmbeddedFilesAndOutOfRangeBoxesAreNormalized() throws Exception {
+ assumeTrue(PdfXConverter.isSupported(), "PDF/X native pipeline unavailable");
+ byte[] input;
+ try (PDDocument doc = new PDDocument();
+ ByteArrayOutputStream bytes = new ByteArrayOutputStream()) {
+ PDPage page = new PDPage(new PDRectangle(0, 0, 200, 200));
+ doc.addPage(page);
+ page.setTrimBox(new PDRectangle(-25, -10, 285, 230));
+ page.setCropBox(new PDRectangle(-10, 20, 160, 230));
+ page.setBleedBox(new PDRectangle(300, 300, 100, 100));
+
+ COSDictionary catalog = doc.getDocumentCatalog().getCOSObject();
+ COSDictionary names = new COSDictionary();
+ COSArray efArr = new COSArray();
+ efArr.add(new COSString("secret.txt"));
+ efArr.add(new COSDictionary());
+ COSDictionary efTree = new COSDictionary();
+ efTree.setItem(COSName.NAMES, efArr);
+ names.setItem(COSName.EMBEDDED_FILES, efTree);
+ catalog.setItem(COSName.NAMES, names);
+ catalog.setItem(COSName.getPDFName("AF"), new COSArray());
+
+ COSDictionary launch = new COSDictionary();
+ launch.setItem(COSName.S, COSName.getPDFName("Launch"));
+ catalog.setItem(COSName.OPEN_ACTION, launch);
+ COSDictionary aa = new COSDictionary();
+ COSDictionary js = new COSDictionary();
+ js.setItem(COSName.S, COSName.JAVA_SCRIPT);
+ aa.setItem(COSName.getPDFName("WC"), js);
+ catalog.setItem(COSName.AA, aa);
+
+ doc.save(bytes);
+ input = bytes.toByteArray();
+ }
+
+ byte[] out = PdfXConverter.applyMetadata(input, PdfXOptions.builder()
+ .standard(PdfXStandard.PDF_X_4)
+ .useCompactCmyk()
+ .title("Sanitize")
+ .build());
+
+ try (PDDocument doc = Loader.loadPDF(out)) {
+ COSDictionary catalog = doc.getDocumentCatalog().getCOSObject();
+ COSDictionary names = catalog.getCOSDictionary(COSName.NAMES);
+ assertTrue(names == null || !names.containsKey(COSName.EMBEDDED_FILES),
+ "PDF/X-4 must strip catalog /EmbeddedFiles");
+ assertFalse(catalog.containsKey(COSName.getPDFName("AF")), "PDF/X-4 must strip catalog /AF");
+ assertFalse(catalog.containsKey(COSName.OPEN_ACTION), "forbidden /Launch /OpenAction must be removed");
+ assertFalse(catalog.containsKey(COSName.AA), "catalog /AA triggers must be removed");
+
+ PDPage outPage = doc.getPage(0);
+ assertEquals(new PDRectangle(0, 0, 200, 200).toString(), outPage.getTrimBox().toString());
+ assertEquals(new PDRectangle(0, 20, 150, 180).toString(), outPage.getCropBox().toString());
+ assertEquals(new PDRectangle(0, 0, 200, 200).toString(), outPage.getBleedBox().toString());
+ }
+ }
+
+ private static COSArray cosFloats(float... values) {
+ COSArray arr = new COSArray();
+ for (float v : values) arr.add(new COSFloat(v));
+ return arr;
+ }
}
diff --git a/jpdfium/src/test/java/stirling/software/jpdfium/fonts/FontNormalizerTest.java b/jpdfium/src/test/java/stirling/software/jpdfium/fonts/FontNormalizerTest.java
index e2f79d268..8929f74cf 100644
--- a/jpdfium/src/test/java/stirling/software/jpdfium/fonts/FontNormalizerTest.java
+++ b/jpdfium/src/test/java/stirling/software/jpdfium/fonts/FontNormalizerTest.java
@@ -49,6 +49,7 @@
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -544,6 +545,44 @@ void normalizePageRepairsToUnicodeAndWidthsInOnePass() throws Exception {
}
}
+ @Test
+ void normalizeAllRewritesUnusedCidWidthEntries() throws Exception {
+ assumeTrue(NativeRuntime.isFull(), "needs real native library");
+ byte[] pdf = withCidWidth(cidTrueTypePdf(), 37, 500);
+
+ try (PdfDocument doc = PdfDocument.open(pdf)) {
+ FontNormalizer.Result result = FontNormalizer.normalizeAll(doc);
+ assertTrue(result.widthsRepaired() >= 1, "the wrong CID width must be repaired: " + result);
+
+ try (PDDocument loaded = Loader.loadPDF(doc.saveBytes())) {
+ COSArray widths = (COSArray) cidDescendant(loaded)
+ .getDictionaryObject(COSName.W);
+ assertNotNull(widths);
+ assertNotEquals(500, cidWidth(widths, 37),
+ "a wrong /W entry for an unused CID must be rewritten");
+ assertTrue(cidWidth(widths, 37) > 0,
+ "the repaired CID must keep the program advance");
+ }
+ }
+ }
+
+ private static int cidWidth(COSArray widths, int cid) {
+ for (int i = 0; i < widths.size(); ) {
+ int first = widths.getInt(i++);
+ if (i >= widths.size()) return -1;
+ if (widths.getObject(i) instanceof COSArray run) {
+ i++;
+ if (cid >= first && cid - first < run.size()) return run.getInt(cid - first);
+ } else {
+ int last = widths.getInt(i++);
+ if (i >= widths.size()) return -1;
+ int width = widths.getInt(i++);
+ if (cid >= first && cid <= last) return width;
+ }
+ }
+ return -1;
+ }
+
@Test
void normalizeAllRepairsCidToUnicodeAndCompactWidthsIdempotently() throws Exception {
assumeTrue(NativeRuntime.isFull(), "needs real native library");
@@ -1543,6 +1582,67 @@ void normalizingDifferencesPreservesAlgorithmicUnicodeNames() throws Exception {
}
}
+ @Test
+ void nonSymbolicDifferencesGetsMicrosoftUnicodeCmap() throws Exception {
+ assumeTrue(NativeRuntime.isFull(), "needs real native library");
+ byte[] pdf;
+ try (PDDocument doc = Loader.loadPDF(embeddedTrueTypePdf(false, false))) {
+ COSDictionary font = firstFontDictionary(doc);
+ assertNotNull(font);
+ COSDictionary descriptor = font.getCOSDictionary(COSName.FONT_DESC);
+ assertNotNull(descriptor);
+ byte[] program = streamBytes(descriptor, COSName.FONT_FILE2);
+ byte[] only03 = withOnlyCmap03(program);
+ assertTrue(hasCmapSubtable(only03, 0, 3), "fixture must keep a Unicode BMP cmap");
+ assertFalse(hasCmapSubtable(only03, 3, 1), "fixture must not gain (3,1) yet");
+
+ PDStream replacement = new PDStream(doc, new ByteArrayInputStream(only03));
+ replacement.getCOSObject().setInt(COSName.LENGTH1, only03.length);
+ descriptor.setItem(COSName.FONT_FILE2, replacement.getCOSObject());
+ descriptor.setInt(COSName.FLAGS, 32);
+
+ COSDictionary encoding = new COSDictionary();
+ encoding.setItem(COSName.TYPE, COSName.ENCODING);
+ encoding.setItem(COSName.BASE_ENCODING, COSName.WIN_ANSI_ENCODING);
+ COSArray differences = new COSArray();
+ differences.add(COSInteger.get(128));
+ differences.add(COSName.getPDFName("Euro"));
+ encoding.setItem(COSName.DIFFERENCES, differences);
+ font.setItem(COSName.ENCODING, encoding);
+
+ PDPage page = doc.getPage(0);
+ COSName fontKey = page.getResources().getFontNames().iterator().next();
+ String stream = "BT /" + fontKey.getName() + " 12 Tf 72 700 Td <80> Tj ET";
+ page.setContents(new PDStream(doc, new ByteArrayInputStream(
+ stream.getBytes(StandardCharsets.US_ASCII))));
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ doc.save(out);
+ pdf = out.toByteArray();
+ }
+
+ try (PdfDocument doc = PdfDocument.open(pdf)) {
+ FontNormalizer.Result result = FontNormalizer.normalizePage(doc, 0);
+ assertTrue(result.fontsProcessed() > 0, "font must be normalized: " + result);
+
+ byte[] normalized = doc.saveBytes();
+ try (PDDocument loaded = Loader.loadPDF(normalized)) {
+ COSDictionary font = firstFontDictionary(loaded);
+ assertNotNull(font);
+ COSDictionary descriptor = font.getCOSDictionary(COSName.FONT_DESC);
+ assertNotNull(descriptor);
+ byte[] program = streamBytes(descriptor, COSName.FONT_FILE2);
+ assertTrue(hasCmapSubtable(program, 3, 1),
+ "non-symbolic Differences must gain a Microsoft Unicode (3,1) cmap");
+ int unicodeGid = cmapGid(program, 3, 1, 0x20AC);
+ assertTrue(unicodeGid > 0,
+ "injected (3,1) cmap must map the Euro sign; gid=" + unicodeGid);
+ assertTrue(new PDFTextStripper().getText(loaded).contains("\u20AC"),
+ "the Euro code must still extract as U+20AC");
+ }
+ }
+ }
+
@Test
void resubsettingOpenTypeCffUpdatesCharSetToRetainedGlyphs() throws Exception {
assumeTrue(NativeRuntime.isFull(), "needs real native library");
@@ -1687,8 +1787,8 @@ void repairWidthsSynthesizesIdentityVDw2AndW2() throws Exception {
byte[] pdf = cidVerticalTrueTypePdf(null, null);
try (PdfDocument doc = PdfDocument.open(pdf)) {
- assertEquals(1, FontNormalizer.repairWidths(doc, 0),
- "vertical CID font without /DW2 must be repaired");
+ assertEquals(2, FontNormalizer.repairWidths(doc, 0),
+ "vertical CID font must repair both /W and /DW2//W2");
byte[] repaired = doc.saveBytes();
try (PDDocument loaded = Loader.loadPDF(repaired)) {
COSDictionary descendant = cidDescendant(loaded);
@@ -1696,13 +1796,15 @@ void repairWidthsSynthesizesIdentityVDw2AndW2() throws Exception {
COSBase dw2Obj = descendant.getDictionaryObject(COSName.getPDFName("DW2"));
assertTrue(dw2Obj instanceof COSArray dw2 && dw2.size() == 2,
"expected synthesized /DW2 [v, w1y], got " + dw2Obj);
+ assertNotNull(descendant.getDictionaryObject(COSName.W),
+ "vertical fonts also need a repaired horizontal /W");
}
}
// With explicit non-default /DW2 [880 -500] and no /W2, /W2 entries must be emitted.
byte[] pdfCustomDw2 = cidVerticalTrueTypePdf(new int[] {880, -500}, null);
try (PdfDocument doc = PdfDocument.open(pdfCustomDw2)) {
- assertEquals(1, FontNormalizer.repairWidths(doc, 0));
+ assertEquals(2, FontNormalizer.repairWidths(doc, 0));
byte[] repaired = doc.saveBytes();
try (PDDocument loaded = Loader.loadPDF(repaired)) {
COSDictionary descendant = cidDescendant(loaded);
@@ -1790,6 +1892,91 @@ private static int[] findSfntTable(byte[] sfnt, String tag) {
return null;
}
+ private static boolean hasCmapSubtable(byte[] sfnt, int platformId, int encodingId) {
+ int[] cmapLoc = findSfntTable(sfnt, "cmap");
+ if (cmapLoc == null) return false;
+ ByteBuffer buf = tableBuffer(sfnt);
+ int numSub = buf.getShort(cmapLoc[0] + 2) & 0xFFFF;
+ for (int i = 0; i < numSub; i++) {
+ int rec = cmapLoc[0] + 4 + i * 8;
+ if ((buf.getShort(rec) & 0xFFFF) == platformId
+ && (buf.getShort(rec + 2) & 0xFFFF) == encodingId) return true;
+ }
+ return false;
+ }
+
+ /** Rebuilds a cmap table with one (0,3) Format 4 subtable copied from the font. */
+ private static byte[] withOnlyCmap03(byte[] sfnt) {
+ int[] cmapLoc = findSfntTable(sfnt, "cmap");
+ assertNotNull(cmapLoc);
+ ByteBuffer src = tableBuffer(sfnt);
+ int numSub = src.getShort(cmapLoc[0] + 2) & 0xFFFF;
+ byte[] fmt4 = null;
+ for (int i = 0; i < numSub; i++) {
+ int rec = cmapLoc[0] + 4 + i * 8;
+ int pid = src.getShort(rec) & 0xFFFF;
+ int eid = src.getShort(rec + 2) & 0xFFFF;
+ int subOff = cmapLoc[0] + src.getInt(rec + 4);
+ if ((src.getShort(subOff) & 0xFFFF) == 4 && (fmt4 == null || (pid == 3 && eid == 1))) {
+ int len = src.getShort(subOff + 2) & 0xFFFF;
+ fmt4 = Arrays.copyOfRange(sfnt, subOff, subOff + len);
+ }
+ }
+ assertNotNull(fmt4, "fixture needs a Format 4 cmap to relabel as (0,3)");
+
+ int newCmapLen = 12 + fmt4.length;
+ byte[] newCmap = new byte[newCmapLen];
+ ByteBuffer cb = ByteBuffer.wrap(newCmap).order(ByteOrder.BIG_ENDIAN);
+ cb.putShort((short) 0);
+ cb.putShort((short) 1);
+ cb.putShort((short) 0);
+ cb.putShort((short) 3);
+ cb.putInt(12);
+ System.arraycopy(fmt4, 0, newCmap, 12, fmt4.length);
+
+ int alignedEnd = (sfnt.length + 3) & ~3;
+ byte[] out = new byte[alignedEnd + newCmapLen];
+ System.arraycopy(sfnt, 0, out, 0, sfnt.length);
+ System.arraycopy(newCmap, 0, out, alignedEnd, newCmapLen);
+ ByteBuffer ob = ByteBuffer.wrap(out).order(ByteOrder.BIG_ENDIAN);
+ ob.putInt(cmapLoc[2] + 8, alignedEnd);
+ ob.putInt(cmapLoc[2] + 12, newCmapLen);
+ return out;
+ }
+
+ /** Looks up a BMP code point in a Format 4 cmap subtable. */
+ private static int cmapGid(byte[] sfnt, int platformId, int encodingId, int codePoint) {
+ int[] cmapLoc = findSfntTable(sfnt, "cmap");
+ if (cmapLoc == null) return -1;
+ ByteBuffer buf = tableBuffer(sfnt);
+ int numSub = buf.getShort(cmapLoc[0] + 2) & 0xFFFF;
+ for (int i = 0; i < numSub; i++) {
+ int rec = cmapLoc[0] + 4 + i * 8;
+ if ((buf.getShort(rec) & 0xFFFF) != platformId
+ || (buf.getShort(rec + 2) & 0xFFFF) != encodingId) continue;
+ int sub = cmapLoc[0] + buf.getInt(rec + 4);
+ if ((buf.getShort(sub) & 0xFFFF) != 4) continue;
+ int segCount = (buf.getShort(sub + 6) & 0xFFFF) / 2;
+ int ends = sub + 14;
+ int starts = ends + segCount * 2 + 2;
+ int deltas = starts + segCount * 2;
+ int ranges = deltas + segCount * 2;
+ for (int s = 0; s < segCount; s++) {
+ int start = buf.getShort(starts + s * 2) & 0xFFFF;
+ int end = buf.getShort(ends + s * 2) & 0xFFFF;
+ if (codePoint < start || codePoint > end) continue;
+ int delta = buf.getShort(deltas + s * 2) & 0xFFFF;
+ int range = buf.getShort(ranges + s * 2) & 0xFFFF;
+ if (range == 0) return (codePoint + delta) & 0xFFFF;
+ int pos = ranges + s * 2 + range + (codePoint - start) * 2;
+ int gid = buf.getShort(pos) & 0xFFFF;
+ return gid == 0 ? 0 : (gid + delta) & 0xFFFF;
+ }
+ return -1;
+ }
+ return -1;
+ }
+
private static byte[] injectFormat12Subtable(byte[] sfnt, int cp, int gid) {
int[] cmapLoc = findSfntTable(sfnt, "cmap");
assertNotNull(cmapLoc);
diff --git a/jpdfium/src/test/java/stirling/software/jpdfium/fonts/ShapeVerifyTest.java b/jpdfium/src/test/java/stirling/software/jpdfium/fonts/ShapeVerifyTest.java
new file mode 100644
index 000000000..280010a2e
--- /dev/null
+++ b/jpdfium/src/test/java/stirling/software/jpdfium/fonts/ShapeVerifyTest.java
@@ -0,0 +1,252 @@
+package stirling.software.jpdfium.fonts;
+
+import java.io.File;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.util.List;
+
+import org.junit.jupiter.api.Test;
+
+import stirling.software.jpdfium.model.ShapeVerifyResult;
+import stirling.software.jpdfium.model.ShapedGlyph;
+import stirling.software.jpdfium.panama.FontLib;
+import stirling.software.jpdfium.panama.NativeRuntime;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assumptions.assumeTrue;
+
+/**
+ * Shaping-safety verification (HarfBuzz UNSAFE_TO_BREAK / UNSAFE_TO_CONCAT
+ * semantics, empirically re-shaped like {@code hb-shape --verify}):
+ *
+ *
+ * - Splitting a text run at a shape-safe boundary must reproduce the full
+ * run's glyphs; splitting inside a cluster or across a kerning pair must
+ * be reported unsafe.
+ * - Removing a byte span must leave the surviving glyphs identical; a
+ * removal whose boundary crosses a kerning pair must be reported unsafe.
+ * - Subsetting with retained GIDs must keep shaping identical for the
+ * retained text (glyph ids and advances).
+ *
+ */
+class ShapeVerifyTest {
+
+ private static File systemFont() {
+ String[] candidates = {
+ "/System/Library/Fonts/Supplemental/Arial.ttf",
+ "/usr/share/fonts/dejavu/DejaVuSans.ttf",
+ "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
+ "/usr/share/fonts/TTF/DejaVuSans.ttf",
+ "C:\\Windows\\Fonts\\arial.ttf"
+ };
+ for (String p : candidates) {
+ File f = new File(p);
+ if (f.isFile()) return f;
+ }
+ return null;
+ }
+
+ @Test
+ void splitAtWordBoundaryIsSafe() throws Exception {
+ assumeTrue(NativeRuntime.isFull(), "needs real native library");
+ File ttf = systemFont();
+ assumeTrue(ttf != null, "no system TTF found");
+ byte[] bytes = Files.readAllBytes(ttf.toPath());
+
+ ShapeVerifyResult result = FontLib.verifyShapeSplit(bytes, "Hello World", 6);
+ assertTrue(result.safe(), "splitting between words must be shape-safe: " + result.reason());
+ assertEquals("identical", result.reason());
+ assertTrue(result.fullGlyphs() >= 10);
+ assertEquals(result.fullGlyphs(), result.partGlyphs());
+ assertEquals(-1, result.diffIndex());
+ }
+
+ @Test
+ void splitInsideClusterIsUnsafe() throws Exception {
+ assumeTrue(NativeRuntime.isFull(), "needs real native library");
+ File ttf = systemFont();
+ assumeTrue(ttf != null, "no system TTF found");
+ byte[] bytes = Files.readAllBytes(ttf.toPath());
+
+ // "éa": é occupies UTF-8 bytes 0..1, so byte 1 is inside its cluster.
+ ShapeVerifyResult result = FontLib.verifyShapeSplit(bytes, "éa", 1);
+ assertFalse(result.safe(), "splitting inside a UTF-8 character must be rejected");
+ assertEquals("split inside cluster", result.reason());
+ assertFalse(result.hbPredicted(), "HarfBuzz cannot predict a mid-character split");
+ }
+
+ @Test
+ void splitAcrossKerningPairMatchesAdvanceDifference() throws Exception {
+ assumeTrue(NativeRuntime.isFull(), "needs real native library");
+ File ttf = systemFont();
+ assumeTrue(ttf != null, "no system TTF found");
+ byte[] bytes = Files.readAllBytes(ttf.toPath());
+
+ // Ground truth from shaping itself: if "AV" shapes narrower than "A"
+ // plus "V" separately, the pair kerns and the split is genuinely unsafe.
+ float kerned = totalAdvance(FontLib.shapeText(bytes, "AV", 12f));
+ float separate = totalAdvance(FontLib.shapeText(bytes, "A", 12f))
+ + totalAdvance(FontLib.shapeText(bytes, "V", 12f));
+
+ ShapeVerifyResult result = FontLib.verifyShapeSplit(bytes, "AV", 1);
+ if (Math.abs(kerned - separate) >= 0.01f) {
+ assertFalse(result.safe(), "kerned pair must verify as unsafe: " + result.reason());
+ assertTrue(result.diffIndex() >= 0, "unsafe split must point at the first difference");
+ } else {
+ assertTrue(result.safe(), "non-kerned pair must verify as safe: " + result.reason());
+ }
+ }
+
+ @Test
+ void removalSpanInsideClusterIsUnsafe() throws Exception {
+ assumeTrue(NativeRuntime.isFull(), "needs real native library");
+ File ttf = systemFont();
+ assumeTrue(ttf != null, "no system TTF found");
+ byte[] bytes = Files.readAllBytes(ttf.toPath());
+
+ // Removing byte 0 of "éa" cuts the é cluster in half.
+ ShapeVerifyResult result = FontLib.verifyShapeRemoval(bytes, "éa", 0, 1);
+ assertFalse(result.safe(), "removal span boundary inside a character must be rejected");
+ assertEquals("span boundary inside cluster", result.reason());
+ }
+
+ @Test
+ void removalOfInteriorWordIsSafe() throws Exception {
+ assumeTrue(NativeRuntime.isFull(), "needs real native library");
+ File ttf = systemFont();
+ assumeTrue(ttf != null, "no system TTF found");
+ byte[] bytes = Files.readAllBytes(ttf.toPath());
+
+ // Remove "ello" from "Hello": remaining "Ho" must shape as H + o.
+ ShapeVerifyResult result = FontLib.verifyShapeRemoval(bytes, "Hello", 1, 5);
+ assertTrue(result.safe(), "removing a non-interacting span must be shape-safe: " + result.reason());
+ assertEquals("identical", result.reason());
+ assertTrue(result.partGlyphs() >= 1);
+ }
+
+ @Test
+ void removalChangingKerningMatchesAdvanceDifference() throws Exception {
+ assumeTrue(NativeRuntime.isFull(), "needs real native library");
+ File ttf = systemFont();
+ assumeTrue(ttf != null, "no system TTF found");
+ byte[] bytes = Files.readAllBytes(ttf.toPath());
+
+ // Remove "V" from "AVa": if A-V kerns, the leading A advances
+ // differently once V is gone, so the removal is genuinely unsafe.
+ float withV = FontLib.shapeText(bytes, "AVa", 12f).get(0).advanceX();
+ float withoutV = FontLib.shapeText(bytes, "Aa", 12f).get(0).advanceX();
+
+ ShapeVerifyResult result = FontLib.verifyShapeRemoval(bytes, "AVa", 1, 2);
+ if (Math.abs(withV - withoutV) >= 0.01f) {
+ assertFalse(result.safe(), "kerned removal must verify as unsafe: " + result.reason());
+ assertTrue(result.diffIndex() >= 0);
+ } else {
+ assertTrue(result.safe(), "non-interacting removal must verify as safe: " + result.reason());
+ }
+ }
+
+ @Test
+ void subsetWithRetainedGidsShapesIdentically() throws Exception {
+ assumeTrue(NativeRuntime.isFull(), "needs real native library");
+ File ttf = systemFont();
+ assumeTrue(ttf != null, "no system TTF found");
+ byte[] bytes = Files.readAllBytes(ttf.toPath());
+
+ String text = "Hello World";
+ int[] codepoints = text.codePoints().toArray();
+ byte[] subset = FontLib.subset(bytes, codepoints, true);
+ assertNotNull(subset);
+ assertTrue(subset.length > 0);
+ assertTrue(subset.length < bytes.length, "subset must be smaller than the original font");
+
+ List before = FontLib.shapeText(bytes, text, 12f);
+ List after = FontLib.shapeText(subset, text, 12f);
+ assertEquals(before.size(), after.size(), "retained-GID subset must shape to the same glyph count");
+ for (int i = 0; i < before.size(); i++) {
+ ShapedGlyph b = before.get(i);
+ ShapedGlyph a = after.get(i);
+ assertEquals(b.glyphId(), a.glyphId(), "glyph id must survive retained-GID subsetting at " + i);
+ assertEquals(b.cluster(), a.cluster(), "cluster must survive subsetting at " + i);
+ }
+
+ // PDF embedding drops GPOS/GSUB/kern by design, so advances survive
+ // subsetting exactly only when the original run has no active kerning.
+ float beforeTotal = totalAdvance(before);
+ float unKernedTotal = 0;
+ for (int i = 0; i < text.length(); i++) {
+ unKernedTotal += totalAdvance(
+ FontLib.shapeText(bytes, text.substring(i, i + 1), 12f));
+ }
+ boolean kerned = Math.abs(beforeTotal - unKernedTotal) >= 0.01f;
+ if (!kerned) {
+ for (int i = 0; i < before.size(); i++) {
+ assertEquals(before.get(i).advanceX(), after.get(i).advanceX(), 0.01f,
+ "advance must survive subsetting at " + i);
+ }
+ }
+ }
+
+ @Test
+ void shapeReportsGlyphSafetyFlags() throws Exception {
+ assumeTrue(NativeRuntime.isFull(), "needs real native library");
+ File ttf = systemFont();
+ assumeTrue(ttf != null, "no system TTF found");
+ byte[] bytes = Files.readAllBytes(ttf.toPath());
+
+ List glyphs = FontLib.shapeText(bytes, "Hello World", 12f);
+ assertFalse(glyphs.isEmpty());
+ for (ShapedGlyph g : glyphs) {
+ assertTrue(g.flags() >= 0, "flags field must be present and non-negative");
+ }
+ }
+
+ @Test
+ void verifyRejectsInvalidBounds() throws Exception {
+ assumeTrue(NativeRuntime.isFull(), "needs real native library");
+ File ttf = systemFont();
+ assumeTrue(ttf != null, "no system TTF found");
+ byte[] bytes = Files.readAllBytes(ttf.toPath());
+
+ assertThrows(IllegalArgumentException.class,
+ () -> FontLib.verifyShapeSplit(bytes, "Hello", -1));
+ assertThrows(IllegalArgumentException.class,
+ () -> FontLib.verifyShapeSplit(bytes, "Hello", 6));
+ assertThrows(IllegalArgumentException.class,
+ () -> FontLib.verifyShapeRemoval(bytes, "Hello", 3, 2));
+ assertThrows(IllegalArgumentException.class,
+ () -> FontLib.verifyShapeRemoval(bytes, "Hello", 0, 6));
+ // A multi-byte character makes byte offsets diverge from char offsets:
+ // "éa" is 3 bytes, so split byte 3 is the end, but 4 is out of range.
+ assertThrows(IllegalArgumentException.class,
+ () -> FontLib.verifyShapeSplit(bytes, "éa", 4));
+ // shapeText rejects empty text before reaching native code.
+ assertThrows(IllegalArgumentException.class, () -> FontLib.shapeText(bytes, "", 12f));
+ }
+
+ @Test
+ void utf8ByteOffsetsAnchorClusters() throws Exception {
+ assumeTrue(NativeRuntime.isFull(), "needs real native library");
+ File ttf = systemFont();
+ assumeTrue(ttf != null, "no system TTF found");
+ byte[] bytes = Files.readAllBytes(ttf.toPath());
+
+ // "a é" in UTF-8: a=0, space=1, é=2..3. Splitting before é is byte 2.
+ String text = "a é";
+ byte[] utf8 = text.getBytes(StandardCharsets.UTF_8);
+ assertEquals(4, utf8.length);
+
+ ShapeVerifyResult beforeAccent = FontLib.verifyShapeSplit(bytes, text, 2);
+ assertTrue(beforeAccent.safe(), "split before an accented letter must be shape-safe: "
+ + beforeAccent.reason());
+ }
+
+ private static float totalAdvance(List glyphs) {
+ float total = 0;
+ for (ShapedGlyph g : glyphs) total += g.advanceX();
+ return total;
+ }
+}
diff --git a/jpdfium/src/test/resources/pdfs/repair/encrypted-empty-user-malformed-length.pdf b/jpdfium/src/test/resources/pdfs/repair/encrypted-empty-user-malformed-length.pdf
new file mode 100644
index 000000000..46fac865e
Binary files /dev/null and b/jpdfium/src/test/resources/pdfs/repair/encrypted-empty-user-malformed-length.pdf differ
diff --git a/native/bridge/include/jpdfium.h b/native/bridge/include/jpdfium.h
index 2c192f3c1..306c7c856 100644
--- a/native/bridge/include/jpdfium.h
+++ b/native/bridge/include/jpdfium.h
@@ -444,11 +444,29 @@ JPDFIUM_EXPORT int32_t jpdfium_font_covers_text(const uint8_t* data, int64_t len
// Shape UTF-8 text with a font program via HarfBuzz.
// Returns a flat JSON array, one object per glyph:
-// [{"g":gid,"ax":x_advance,"ay":y_advance,"dx":x_offset,"dy":y_offset,"cluster":byte_index}]
-// Advances and offsets are 26.6 fractional points at font_size. Caller frees *json.
+// [{"g":gid,"ax":x_advance,"ay":y_advance,"dx":x_offset,"dy":y_offset,"cluster":byte_index,
+// "f":flags}]
+// Advances and offsets are 26.6 fractional points at font_size. `flags` carries
+// HarfBuzz glyph flags (bit 0: UNSAFE_TO_BREAK, bit 1: UNSAFE_TO_CONCAT when the
+// HarfBuzz build supports it). Caller frees *json.
JPDFIUM_EXPORT int32_t jpdfium_text_shape(const uint8_t* font_data, int64_t font_len,
const char* utf8_text, float font_size, char** json);
+// Verify that editing UTF-8 text at a shaping boundary is shape-safe, using
+// the same re-shape-and-compare approach as `hb-shape --verify` (empirical, so
+// it is authoritative where HarfBuzz's conservative UNSAFE_TO_BREAK /
+// UNSAFE_TO_CONCAT glyph flags only predict):
+// mode 0: would splitting the text at byte offset `a` change the shaping?
+// mode 1: would removing the span [a, b) change the surviving glyphs?
+// Offsets are byte indexes into utf8_text and must land on cluster starts.
+// Returns JSON:
+// {"safe":bool,"mode":n,"reason":"...","diff":n,"full_glyphs":n,"part_glyphs":n,
+// "hb_predicted":bool,"unsafe_break_glyphs":n,"unsafe_concat_glyphs":n}
+// `diff` is the first differing glyph index (-1 when none). Caller frees *json.
+JPDFIUM_EXPORT int32_t jpdfium_shape_verify(const uint8_t* font_data, int64_t font_len,
+ const char* utf8_text, int32_t mode, int32_t a,
+ int32_t b, char** json);
+
// Fix /ToUnicode CMap for all fonts on a page using the font's internal cmap table.
// This is the most critical step for reliable auto-redact: wrong ToUnicode -> wrong text
// extraction -> patterns miss -> redact silently fails.
diff --git a/native/bridge/src/jpdfium_advanced.cpp b/native/bridge/src/jpdfium_advanced.cpp
index d3925daa1..16aeb020d 100644
--- a/native/bridge/src/jpdfium_advanced.cpp
+++ b/native/bridge/src/jpdfium_advanced.cpp
@@ -25,6 +25,57 @@
#include
#include
+// HB_GLYPH_FLAG_UNSAFE_TO_CONCAT and HB_BUFFER_FLAG_PRODUCE_UNSAFE_TO_CONCAT
+// arrived in HarfBuzz 3.0.0; older builds (e.g. Ubuntu 22.04 harfbuzz 2.7.4)
+// still produce UNSAFE_TO_BREAK but never UNSAFE_TO_CONCAT.
+#if (HB_VERSION_MAJOR * 100 + HB_VERSION_MINOR) >= 300
+#define JPDFIUM_HB_HAS_UNSAFE_TO_CONCAT 1
+#else
+#define JPDFIUM_HB_HAS_UNSAFE_TO_CONCAT 0
+#endif
+
+namespace {
+
+struct ShapeEntry {
+ hb_codepoint_t glyph;
+ unsigned int cluster;
+ int32_t ax;
+ int32_t ay;
+ int32_t dx;
+ int32_t dy;
+};
+
+bool sameShaping(const ShapeEntry& x, const ShapeEntry& y) {
+ return x.glyph == y.glyph && x.cluster == y.cluster && x.ax == y.ax && x.ay == y.ay &&
+ x.dx == y.dx && x.dy == y.dy;
+}
+
+// Shapes text[itemOffset, itemOffset + itemLength) under the given segment
+// properties. Clusters stay absolute byte offsets into text (HarfBuzz
+// itemization semantics), so pieces concatenate comparably with a full run.
+bool shapeSlice(hb_font_t* font, const char* text, int textLen, unsigned itemOffset, int itemLength,
+ const hb_segment_properties_t* props, std::vector* out) {
+ hb_buffer_t* buf = hb_buffer_create();
+ if (!buf) return false;
+ hb_buffer_set_segment_properties(buf, props);
+#if JPDFIUM_HB_HAS_UNSAFE_TO_CONCAT
+ hb_buffer_set_flags(buf, HB_BUFFER_FLAG_PRODUCE_UNSAFE_TO_CONCAT);
+#endif
+ hb_buffer_add_utf8(buf, text, textLen, itemOffset, itemLength);
+ hb_shape(font, buf, nullptr, 0);
+ unsigned int count = 0;
+ hb_glyph_info_t* infos = hb_buffer_get_glyph_infos(buf, &count);
+ hb_glyph_position_t* pos = hb_buffer_get_glyph_positions(buf, &count);
+ for (unsigned int i = 0; i < count; i++) {
+ out->push_back(ShapeEntry{infos[i].codepoint, infos[i].cluster, pos[i].x_advance,
+ pos[i].y_advance, pos[i].x_offset, pos[i].y_offset});
+ }
+ hb_buffer_destroy(buf);
+ return true;
+}
+
+} // namespace
+
int32_t jpdfium_font_get_data(int64_t page, int32_t font_index, uint8_t** data, int64_t* len) {
try {
PageWrapper* pw = decodePage(page);
@@ -173,6 +224,9 @@ int32_t jpdfium_text_shape(const uint8_t* font_data, int64_t font_len, const cha
hb_buffer_t* buf = hb_buffer_create();
hb_buffer_add_utf8(buf, utf8_text, -1, 0, -1);
hb_buffer_guess_segment_properties(buf);
+#if JPDFIUM_HB_HAS_UNSAFE_TO_CONCAT
+ hb_buffer_set_flags(buf, HB_BUFFER_FLAG_PRODUCE_UNSAFE_TO_CONCAT);
+#endif
hb_shape(font, buf, nullptr, 0);
unsigned int count = 0;
@@ -180,13 +234,15 @@ int32_t jpdfium_text_shape(const uint8_t* font_data, int64_t font_len, const cha
hb_glyph_position_t* pos = hb_buffer_get_glyph_positions(buf, &count);
std::string out = "[";
- char entry[128];
+ char entry[160];
for (unsigned int i = 0; i < count; ++i) {
if (i) out += ',';
std::snprintf(entry, sizeof(entry),
- "{\"g\":%u,\"ax\":%d,\"ay\":%d,\"dx\":%d,\"dy\":%d,\"cluster\":%u}",
+ "{\"g\":%u,\"ax\":%d,\"ay\":%d,\"dx\":%d,\"dy\":%d,\"cluster\":%u,"
+ "\"f\":%u}",
infos[i].codepoint, pos[i].x_advance, pos[i].y_advance, pos[i].x_offset,
- pos[i].y_offset, infos[i].cluster);
+ pos[i].y_offset, infos[i].cluster,
+ hb_glyph_info_get_glyph_flags(&infos[i]));
out += entry;
}
out += ']';
@@ -199,6 +255,215 @@ int32_t jpdfium_text_shape(const uint8_t* font_data, int64_t font_len, const cha
}
}
+// Empirically verifies shaping safety at a text edit boundary using the same
+// re-shape-and-compare approach as `hb-shape --verify`: the full run is shaped
+// once, the edited pieces are re-shaped under the full run's segment
+// properties, and the glyph sequences must agree in glyph id, cluster,
+// advance and offset. This is the authoritative check behind HarfBuzz's
+// conservative UNSAFE_TO_BREAK / UNSAFE_TO_CONCAT glyph flags, which are also
+// reported for diagnostics.
+//
+// mode 0: is splitting the text at byte offset `a` shape-safe?
+// mode 1: is removing the span [a, b) shape-safe for the surviving glyphs?
+int32_t jpdfium_shape_verify(const uint8_t* font_data, int64_t font_len, const char* utf8_text,
+ int32_t mode, int32_t a, int32_t b, char** json) {
+ try {
+ if (!font_data || font_len <= 0 || !utf8_text || !json) return JPDFIUM_ERR_INVALID;
+ if (mode != 0 && mode != 1) return JPDFIUM_ERR_INVALID;
+ int n = static_cast(std::strlen(utf8_text));
+ if (n <= 0) return JPDFIUM_ERR_INVALID;
+ if (mode == 0) {
+ if (a < 0 || a > n) return JPDFIUM_ERR_INVALID;
+ } else if (a < 0 || b <= a || b > n) {
+ return JPDFIUM_ERR_INVALID;
+ }
+
+ hb_blob_t* blob = hb_blob_create(reinterpret_cast(font_data),
+ static_cast(font_len),
+ HB_MEMORY_MODE_READONLY, nullptr, nullptr);
+ hb_face_t* face = hb_face_create(blob, 0);
+ hb_blob_destroy(blob);
+ if (hb_face_get_glyph_count(face) <= 1) {
+ hb_face_destroy(face);
+ return JPDFIUM_ERR_INVALID;
+ }
+ hb_font_t* font = hb_font_create(face);
+ hb_face_destroy(face);
+ hb_font_set_scale(font, 64, 64);
+
+ // Full reference run: glyph sequence plus HarfBuzz safety flags.
+ hb_buffer_t* fullBuf = hb_buffer_create();
+ hb_buffer_add_utf8(fullBuf, utf8_text, n, 0, n);
+ hb_buffer_guess_segment_properties(fullBuf);
+#if JPDFIUM_HB_HAS_UNSAFE_TO_CONCAT
+ hb_buffer_set_flags(fullBuf, HB_BUFFER_FLAG_PRODUCE_UNSAFE_TO_CONCAT);
+#endif
+ hb_shape(font, fullBuf, nullptr, 0);
+ hb_segment_properties_t props;
+ hb_buffer_get_segment_properties(fullBuf, &props);
+ unsigned int fullCount = 0;
+ hb_glyph_info_t* fullInfos = hb_buffer_get_glyph_infos(fullBuf, &fullCount);
+ hb_glyph_position_t* fullPos = hb_buffer_get_glyph_positions(fullBuf, &fullCount);
+ std::vector full;
+ std::vector fullFlags;
+ std::set clusterStarts;
+ unsigned int unsafeBreak = 0;
+ unsigned int unsafeConcat = 0;
+ for (unsigned int i = 0; i < fullCount; i++) {
+ unsigned int flags = hb_glyph_info_get_glyph_flags(&fullInfos[i]);
+ fullFlags.push_back(flags);
+ if (flags & HB_GLYPH_FLAG_UNSAFE_TO_BREAK) unsafeBreak++;
+#if JPDFIUM_HB_HAS_UNSAFE_TO_CONCAT
+ if (flags & HB_GLYPH_FLAG_UNSAFE_TO_CONCAT) unsafeConcat++;
+#endif
+ clusterStarts.insert(fullInfos[i].cluster);
+ full.push_back(ShapeEntry{fullInfos[i].codepoint, fullInfos[i].cluster,
+ fullPos[i].x_advance, fullPos[i].y_advance,
+ fullPos[i].x_offset, fullPos[i].y_offset});
+ }
+ hb_buffer_destroy(fullBuf);
+
+ bool safe = false;
+ std::string reason = "unknown";
+ int diff = -1;
+ std::vector part;
+ if (mode == 0) {
+ if (a == 0 || a == n) {
+ safe = true;
+ reason = "empty side";
+ } else if (clusterStarts.find(static_cast(a)) == clusterStarts.end()) {
+ reason = "split inside cluster";
+ } else {
+ if (!shapeSlice(font, utf8_text, n, 0, a, &props, &part)) {
+ hb_font_destroy(font);
+ return JPDFIUM_ERR_NATIVE;
+ }
+ std::vector tail;
+ if (!shapeSlice(font, utf8_text, n, static_cast(a), n - a, &props,
+ &tail)) {
+ hb_font_destroy(font);
+ return JPDFIUM_ERR_NATIVE;
+ }
+ part.insert(part.end(), tail.begin(), tail.end());
+ if (part.size() != full.size()) {
+ reason = "glyph count differs after split";
+ for (size_t i = 0; i < part.size() && i < full.size(); i++) {
+ if (!sameShaping(part[i], full[i])) {
+ diff = static_cast(i);
+ break;
+ }
+ }
+ } else {
+ for (size_t i = 0; i < full.size(); i++) {
+ if (!sameShaping(part[i], full[i])) {
+ diff = static_cast(i);
+ break;
+ }
+ }
+ if (diff < 0) {
+ safe = true;
+ reason = "identical";
+ } else {
+ reason = "glyph mismatch after split";
+ }
+ }
+ }
+ } else {
+ if (a == 0 && b == n) {
+ safe = true;
+ reason = "nothing remains";
+ } else {
+ // Span boundaries may sit at any cluster start or at the text edges.
+ bool aIsBoundary = a == 0 || clusterStarts.find(static_cast(a)) !=
+ clusterStarts.end();
+ bool bIsBoundary = b == n || clusterStarts.find(static_cast(b)) !=
+ clusterStarts.end();
+ if (!aIsBoundary || !bIsBoundary) {
+ reason = "span boundary inside cluster";
+ } else {
+ // Expected survivors: before-part unchanged, after-part
+ // shifted by the removed byte count.
+ std::vector expected;
+ for (const ShapeEntry& e : full) {
+ if (e.cluster < static_cast(a)) {
+ expected.push_back(e);
+ } else if (e.cluster >= static_cast(b)) {
+ ShapeEntry shifted = e;
+ shifted.cluster -= static_cast(b - a);
+ expected.push_back(shifted);
+ }
+ }
+ std::string remaining(utf8_text, utf8_text + a);
+ remaining.append(utf8_text + b, utf8_text + n);
+ if (!shapeSlice(font, remaining.c_str(), static_cast(remaining.size()), 0,
+ static_cast(remaining.size()), &props, &part)) {
+ hb_font_destroy(font);
+ return JPDFIUM_ERR_NATIVE;
+ }
+ if (part.size() != expected.size()) {
+ reason = "glyph count differs after removal";
+ for (size_t i = 0; i < part.size() && i < expected.size(); i++) {
+ if (!sameShaping(part[i], expected[i])) {
+ diff = static_cast(i);
+ break;
+ }
+ }
+ } else {
+ for (size_t i = 0; i < expected.size(); i++) {
+ if (!sameShaping(part[i], expected[i])) {
+ diff = static_cast(i);
+ break;
+ }
+ }
+ if (diff < 0) {
+ safe = true;
+ reason = "identical";
+ } else {
+ reason = "glyph mismatch after removal";
+ }
+ }
+ }
+ }
+ }
+
+ // HarfBuzz's own prediction at the edit boundary (diagnostic only:
+ // the flags are conservative, the re-shape comparison above decides).
+ bool hbPredicted = false;
+ if (mode == 0) {
+ for (size_t i = 0; i < full.size(); i++) {
+ if (full[i].cluster == static_cast(a) &&
+ (fullFlags[i] & HB_GLYPH_FLAG_UNSAFE_TO_BREAK)) {
+ hbPredicted = true;
+ break;
+ }
+ }
+ } else {
+ for (size_t i = 0; i < full.size(); i++) {
+ unsigned int cluster = full[i].cluster;
+ if ((cluster == static_cast(a) ||
+ cluster == static_cast(b)) &&
+ (fullFlags[i] & HB_GLYPH_FLAG_UNSAFE_TO_CONCAT)) {
+ hbPredicted = true;
+ break;
+ }
+ }
+ }
+
+ char verdict[512];
+ std::snprintf(verdict, sizeof(verdict),
+ "{\"safe\":%s,\"mode\":%d,\"reason\":\"%s\",\"diff\":%d,"
+ "\"full_glyphs\":%zu,\"part_glyphs\":%zu,\"hb_predicted\":%s,"
+ "\"unsafe_break_glyphs\":%u,\"unsafe_concat_glyphs\":%u}",
+ safe ? "true" : "false", mode, reason.c_str(), diff, full.size(), part.size(),
+ hbPredicted ? "true" : "false", unsafeBreak, unsafeConcat);
+ hb_font_destroy(font);
+ *json = strdup(verdict);
+ return JPDFIUM_OK;
+ } catch (...) {
+ return JPDFIUM_ERR_NATIVE;
+ }
+}
+
int32_t jpdfium_font_subset(const uint8_t* font_data, int64_t font_len, const uint32_t* codepoints,
int32_t num_codepoints, int32_t retain_gids, uint8_t** out_data,
int64_t* out_len) {
diff --git a/native/bridge/src/jpdfium_repair.cpp b/native/bridge/src/jpdfium_repair.cpp
index a92f396ea..15f0419a1 100644
--- a/native/bridge/src/jpdfium_repair.cpp
+++ b/native/bridge/src/jpdfium_repair.cpp
@@ -24,6 +24,7 @@
#include
#include
+#include
#include
#include
#include
@@ -514,6 +515,185 @@ std::shared_ptr loadAndRepairMemory(const uint8_t* input, size_t inputLen,
} // namespace
+// Scan one decompressed content stream for a degenerate clipping path: a
+// "re" whose width or height operand is zero, later applied as the active
+// clip with W/W*. Blank-looking PDFs whose text is present but hidden behind
+// a zero-area clip are a common producer bug; qpdf --qdf exposes them only
+// to patient humans, so inspect reports them mechanically.
+//
+// State machine over PDF content tokens: comments, literal and hex strings
+// are skipped; the four numeric operands preceding each "re" are kept; the
+// current path is "degenerate" while it consists solely of zero-area
+// rectangles. W/W* only flags the page when every subpath so far was
+// zero-area, so a clip built from a real rectangle never trips the warning.
+static bool content_has_zero_area_clip(const char* data, size_t len) {
+ // Path construction state for the subpaths seen since the last paint.
+ bool saw_zero_rect = false; // a "re" with zero width or height
+ bool saw_area_path = false; // any "re" with real area, or m/l/c/v/y/h
+ bool clip_requested = false; // W or W* seen, clip applies at next paint
+ double nums[4] = {0, 0, 0, 0}; // most recent numbers, x y w h for a "re"
+
+ size_t i = 0;
+ while (i < len) {
+ char c = data[i];
+ if (c == '\0') break;
+ if (std::isspace(static_cast(c)) || c == '\014') {
+ i++;
+ continue;
+ }
+ // Comment: skip to end of line.
+ if (c == '%') {
+ while (i < len && data[i] != '\n' && data[i] != '\r') i++;
+ continue;
+ }
+ // Literal string: skip with escape and nesting handling.
+ if (c == '(') {
+ int depth = 1;
+ i++;
+ while (i < len && depth > 0) {
+ if (data[i] == '\\') {
+ i += 2;
+ continue;
+ }
+ if (data[i] == '(')
+ depth++;
+ else if (data[i] == ')')
+ depth--;
+ i++;
+ }
+ continue;
+ }
+ // Hex string: skip to the closing '>'.
+ if (c == '<' && i + 1 < len && data[i + 1] == '<') {
+ // Dictionary start: a delimiter like any other.
+ i += 2;
+ continue;
+ }
+ if (c == '<') {
+ i++;
+ while (i < len && data[i] != '>') i++;
+ if (i < len) i++;
+ continue;
+ }
+ // Structural delimiters that end a token.
+ if (c == '>' || c == '[' || c == ']' || c == '{' || c == '}' || c == ')') {
+ i++;
+ continue;
+ }
+ if (c == '/') {
+ i++;
+ while (i < len && !std::isspace(static_cast(data[i])) &&
+ !strchr("()<>[]{}/%", data[i]))
+ i++;
+ continue;
+ }
+ // Read a token: operators, numbers, inline-image bytes.
+ size_t start = i;
+ while (i < len && !std::isspace(static_cast(data[i])) &&
+ !strchr("()<>[]{}/%", data[i])) {
+ i++;
+ }
+ if (i == start) {
+ i++;
+ continue;
+ }
+ std::string token(data + start, i - start);
+ // Number: slide the last-four-operands window.
+ char* end = nullptr;
+ double value = std::strtod(token.c_str(), &end);
+ if (end && *end == '\0' && end != token.c_str() &&
+ token.find_first_not_of("+-.0123456789") == std::string::npos) {
+ nums[0] = nums[1];
+ nums[1] = nums[2];
+ nums[2] = nums[3];
+ nums[3] = value;
+ continue;
+ }
+ if (token == "re") {
+ // Operands survive in nums[] from the trailing numbers; only the
+ // count is cleared, so re still reads its x y w h.
+ double w = nums[2];
+ double h = nums[3];
+ if (w == 0.0 || h == 0.0)
+ saw_zero_rect = true;
+ else
+ saw_area_path = true;
+ } else if (token == "m" || token == "l" || token == "c" || token == "v" || token == "y" ||
+ token == "h") {
+ saw_area_path = true; // unknown area: be conservative
+ } else if (token == "W" || token == "W*") {
+ clip_requested = true;
+ } else if (token == "n" || token == "S" || token == "s" || token == "f" || token == "F" ||
+ token == "B" || token == "B*" || token == "b" || token == "b*") {
+ // Path painted: the pending clip (if any) becomes active.
+ if (clip_requested && saw_zero_rect && !saw_area_path) return true;
+ saw_zero_rect = false;
+ saw_area_path = false;
+ clip_requested = false;
+ }
+ }
+ return false;
+}
+
+// Collects 1-based indices of pages whose content paints under a zero-area
+// clipping path. Bounded per page: streams larger than the cap are skipped
+// (diagnostics must never be a decompression-bomb vector), and undecodable
+// content simply contributes nothing.
+static void collect_zero_area_clip_pages(QPDF& pdf, std::vector& out) {
+ const size_t kMaxScanBytes = 8u << 20; // 8 MB decompressed per page
+ std::vector pages;
+ try {
+ pages = pdf.getAllPages();
+ } catch (...) {
+ return;
+ }
+ for (size_t p = 0; p < pages.size(); p++) {
+ QPDFObjectHandle contents;
+ try {
+ contents = pages[p].getKey("/Contents");
+ } catch (...) {
+ continue;
+ }
+ std::string combined;
+ bool overflow = false;
+ if (contents.isStream()) {
+ try {
+ std::shared_ptr buf = contents.getStreamData(qpdf_dl_generalized);
+ if (buf && buf->getSize() <= kMaxScanBytes) {
+ combined.assign(reinterpret_cast(buf->getBuffer()),
+ buf->getSize());
+ } else
+ overflow = true;
+ } catch (...) {
+ continue;
+ }
+ } else if (contents.isArray()) {
+ try {
+ // getStreamData is non-const: iterate a copied vector.
+ for (auto& part : contents.getArrayAsVector()) {
+ if (!part.isStream()) continue;
+ std::shared_ptr buf = part.getStreamData(qpdf_dl_generalized);
+ if (!buf) continue;
+ if (combined.size() + buf->getSize() > kMaxScanBytes) {
+ overflow = true;
+ break;
+ }
+ combined.append(reinterpret_cast(buf->getBuffer()),
+ buf->getSize());
+ }
+ } catch (...) {
+ continue;
+ }
+ } else {
+ continue;
+ }
+ if (overflow) continue;
+ if (content_has_zero_area_clip(combined.data(), combined.size())) {
+ out.push_back(static_cast(p + 1));
+ }
+ }
+}
+
// Helper: serialize qpdf warnings to JSON array
static std::string warnings_to_json(const std::vector& warnings) {
std::ostringstream os;
@@ -842,9 +1022,34 @@ JPDFIUM_EXPORT int32_t jpdfium_repair_inspect(const uint8_t* input, int64_t inpu
os << "\"status\":\"loaded\",";
os << "\"warning_count\":" << warnings.size() << ",";
os << "\"page_count\":" << pageCount << ",";
+
+ // Degenerate-clip detection: pages that paint under a zero-area
+ // clipping path render blank while their text and objects are intact.
+ std::vector zeroClipPages;
+ collect_zero_area_clip_pages(pdf, zeroClipPages);
+ os << "\"zero_area_clip_pages\":[";
+ for (size_t i = 0; i < zeroClipPages.size(); i++) {
+ if (i) os << ",";
+ os << zeroClipPages[i];
+ }
+ os << "],";
+
+ std::string issues = warnings_to_json(warnings);
+ if (issues.size() >= 2 && issues[issues.size() - 1] == ']') {
+ issues.resize(issues.size() - 1); // drop the closing bracket
+ }
+ for (int page : zeroClipPages) {
+ if (issues.size() > 1) issues += ",";
+ issues += "{\"message\":\"page " + std::to_string(page) +
+ ": zero-area clipping path applied (re with zero width or height"
+ " followed by W) - page content may be invisible\",\"page\":" +
+ std::to_string(page) + "}";
+ }
+ issues += "]";
+
os << "\"xref_valid\":true,";
os << "\"trailer_valid\":true,";
- os << "\"issues\":" << warnings_to_json(warnings);
+ os << "\"issues\":" << issues;
} catch (std::exception& e) {
std::string msg = e.what();
diff --git a/native/bridge/src/jpdfium_stub.cpp b/native/bridge/src/jpdfium_stub.cpp
index 481cf4eb1..dde7aa154 100644
--- a/native/bridge/src/jpdfium_stub.cpp
+++ b/native/bridge/src/jpdfium_stub.cpp
@@ -1160,6 +1160,16 @@ int32_t jpdfium_text_shape(const uint8_t*, int64_t, const char*, float, char** j
return JPDFIUM_OK;
}
+int32_t jpdfium_shape_verify(const uint8_t*, int64_t, const char*, int32_t, int32_t, int32_t,
+ char** json) {
+ if (!json) return JPDFIUM_ERR_INVALID;
+ *json = dup_cstring(
+ "{\"safe\":false,\"mode\":0,\"reason\":\"not in this native build\",\"diff\":-1,"
+ "\"full_glyphs\":0,\"part_glyphs\":0,\"hb_predicted\":false,"
+ "\"unsafe_break_glyphs\":0,\"unsafe_concat_glyphs\":0}");
+ return JPDFIUM_ERR_NOT_FOUND;
+}
+
// Glyph-Level Redaction stub
int32_t jpdfium_redact_glyph_aware(int64_t, const char**, int32_t, uint32_t, float, uint32_t,
diff --git a/reachability-metadata/metadata/com.stirling/jpdfium/1.2.0/reachability-metadata.json b/reachability-metadata/metadata/com.stirling/jpdfium/1.2.0/reachability-metadata.json
index a09631924..33183ed85 100644
--- a/reachability-metadata/metadata/com.stirling/jpdfium/1.2.0/reachability-metadata.json
+++ b/reachability-metadata/metadata/com.stirling/jpdfium/1.2.0/reachability-metadata.json
@@ -252,6 +252,18 @@
"returnType": "jint",
"parameterTypes": ["void*", "void*", "jint", "void*", "jint", "jint", "jint", "void*", "jint"]
},
+ {
+ "returnType": "jint",
+ "parameterTypes": [
+ "void*",
+ "jlong",
+ "void*",
+ "jint",
+ "jint",
+ "jint",
+ "void*"
+ ]
+ },
{
"returnType": "jint",
"parameterTypes": [