Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 157 additions & 0 deletions jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfRepair.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package stirling.software.jpdfium.model;

/**
* Verdict of a shaping-safety check at a text edit boundary.
*
* <p>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) {}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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() {}

/**
Expand Down Expand Up @@ -121,7 +127,8 @@ public static List<ShapedGlyph> 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;
}
Expand All @@ -133,6 +140,90 @@ public static List<ShapedGlyph> 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.
*
* <p>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<String, String> 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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
Loading
Loading