diff --git a/jpdfium/src/main/java/stirling/software/jpdfium/doc/CompressOptions.java b/jpdfium/src/main/java/stirling/software/jpdfium/doc/CompressOptions.java index f67c76fec..2decda640 100644 --- a/jpdfium/src/main/java/stirling/software/jpdfium/doc/CompressOptions.java +++ b/jpdfium/src/main/java/stirling/software/jpdfium/doc/CompressOptions.java @@ -139,7 +139,8 @@ private CompressOptions(Builder b) { public int zopfliMaxInputBytes() { return zopfliMaxInputBytes; } /** * Whether to subset already-embedded TrueType/OpenType fonts in-place. - * Default: {@code true}. + * Default: {@code false}. This pass rewrites font programs, so callers opt + * in after deciding their text/render contract. */ public boolean subsetFonts() { return subsetFonts; } diff --git a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontAuditor.java b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontAuditor.java index 9ca537dc0..af9917de4 100644 --- a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontAuditor.java +++ b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontAuditor.java @@ -325,6 +325,18 @@ public static boolean inventoryAvailable() { * @return fonts in file order, or an empty list when the native build cannot inventory */ public static List inventory(PdfDocument doc) { + // Prefer the original file so a large file-backed document is never + // serialized just to list its font resources. + Path sourcePath = doc.sourcePath(); + if (!doc.isContentModified() + && sourcePath != null + && QpdfLib.isFileFontInventorySupported()) { + String json = QpdfLib.inspectFonts(sourcePath); + if (json != null) { + List parsed = parseInventory(json); + if (!parsed.isEmpty()) return parsed; + } + } // sourceBytes() is the open-time snapshot. After an in-memory edit it no longer // describes the document, so inventory the serialized current state instead. byte[] source = doc.sourceBytes(); diff --git a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontEmbedder.java b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontEmbedder.java index e5a34e87a..f97e7a99e 100644 --- a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontEmbedder.java +++ b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontEmbedder.java @@ -245,21 +245,23 @@ public static Result embedMissing(byte[] pdf, Path... fontDirs) throws IOExcepti } List resources = PdfFontAuditor.unembeddedResources(pdf); Set names = new LinkedHashSet<>(); - Map substitutable = new HashMap<>(); + Map blockers = new HashMap<>(); for (PdfFontAuditor.FontResource resource : resources) { names.add(resource.baseName()); - substitutable.merge(resource.baseName(), safeToSubstitute(resource), Boolean::logicalAnd); + String blocker = substitutionBlocker(resource); + if (blocker != null) blockers.putIfAbsent(resource.baseName(), blocker); } List missing = List.copyOf(names); if (missing.isEmpty()) return new Result(pdf, List.of(), List.of(), List.of()); Map bundled = fontDirs.length == 0 ? bundledFaces() : null; Map index = null; Map found = new LinkedHashMap<>(); + Map operationPrograms = new HashMap<>(); List statuses = new ArrayList<>(missing.size()); for (String name : missing) { - if (!substitutable.getOrDefault(name, true)) { - statuses.add(new FontStatus(name, "", -1, false, - "CID encoding or glyph order cannot be verified safely")); + String blocker = blockers.get(name); + if (blocker != null) { + statuses.add(new FontStatus(name, "", -1, false, blocker)); continue; } String key = key(name); @@ -286,7 +288,7 @@ public static Result embedMissing(byte[] pdf, Path... fontDirs) throws IOExcepti "embedding restricted by fsType " + restricted.fsType())); continue; } - byte[] data = program(face); + byte[] data = program(face, operationPrograms); int fsType = programFsType(face, data); if (!embeddableProgram(fsType)) { statuses.add(new FontStatus(name, source(face), fsType, false, @@ -335,19 +337,49 @@ public static Result embedMissingStrict(byte[] pdf, Path... fontDirs) throws IOE * an ordering that a substitute program does not share; embedding it would keep the * document renderable but change its extracted text. */ - private static boolean safeToSubstitute(PdfFontAuditor.FontResource resource) { + private static String substitutionBlocker(PdfFontAuditor.FontResource resource) { if (!resource.hasToUnicode() && !isAscii(resource.baseName())) { // Legacy CJK producers put GBK/Shift-JIS bytes in /BaseFont and rely on the // original font's glyph order. A substitute would change both text extraction // and the drawn glyphs because there is no /ToUnicode to preserve. - return false; + return "legacy non-ASCII /BaseFont without /ToUnicode cannot be mapped safely"; + } + if (!resource.cid()) { + String encoding = resource.encoding(); + if (!encoding.isEmpty() && !"custom".equals(encoding) + && !"WinAnsiEncoding".equals(encoding) + && !"MacRomanEncoding".equals(encoding) + && !"StandardEncoding".equals(encoding)) { + return "unknown simple-font encoding " + encoding + + " cannot be mapped without the original glyph names"; + } + String normalized = key(resource.baseName()); + boolean symbolicFamily = normalized.startsWith("symbol") + || normalized.startsWith("standardsymbol") + || normalized.startsWith("zapfdingbats") + || normalized.startsWith("dingbats"); + if (symbolicFamily && resource.encoding().isEmpty()) { + // A built-in Symbol/ZapfDingbats encoding maps codes through the + // producer's glyph names, not through Unicode. A substitute + // program without the same glyph-name table would silently + // change every character, so leave it unresolved. + return "built-in symbolic encoding cannot be preserved by a substitute program"; + } + return null; } - if (!resource.cid()) return true; boolean identityEncoding = "Identity-H".equals(resource.encoding()) || "Identity-V".equals(resource.encoding()); + if (!identityEncoding) { + return "CID encoding: predefined CMap " + resource.encoding() + + " cannot be rewritten without a verified code-to-glyph mapping"; + } boolean identityMap = resource.cidToGidMap().isEmpty() || "Identity".equals(resource.cidToGidMap()); - return identityEncoding && identityMap; + if (!identityMap) { + return "CIDToGIDMap " + resource.cidToGidMap() + + " carries a glyph order a substitute program does not share"; + } + return null; } private static boolean isAscii(String value) { @@ -414,17 +446,18 @@ public static FileResult embedMissingTo(Path input, Path output, Path... fontDir List resources = PdfFontAuditor.unembeddedResources(input); Set names = new LinkedHashSet<>(); - Map substitutable = new HashMap<>(); + Map blockers = new HashMap<>(); for (PdfFontAuditor.FontResource resource : resources) { names.add(resource.baseName()); - substitutable.merge(resource.baseName(), safeToSubstitute(resource), Boolean::logicalAnd); + String blocker = substitutionBlocker(resource); + if (blocker != null) blockers.putIfAbsent(resource.baseName(), blocker); } List missing = List.copyOf(names); if (missing.isEmpty()) { Files.copy(input, output, StandardCopyOption.REPLACE_EXISTING); return new FileResult(List.of(), List.of()); } - Map found = programsFor(missing, substitutable, fontDirs); + Map found = programsFor(missing, blockers, fontDirs); if (found.isEmpty() || !QpdfLib.embedFontsToFile(input, output, found, true)) { Files.copy(input, output, StandardCopyOption.REPLACE_EXISTING); return new FileResult(List.of(), missing); @@ -435,15 +468,16 @@ public static FileResult embedMissingTo(Path input, Path output, Path... fontDir } private static Map programsFor( - List missing, Map substitutable, Path... fontDirs) + List missing, Map blockers, Path... fontDirs) throws IOException { Map index = indexFor(fontDirs); Map found = new LinkedHashMap<>(); + Map operationPrograms = new HashMap<>(); for (String name : missing) { - if (!substitutable.getOrDefault(name, true)) continue; + if (blockers.containsKey(name)) continue; Face face = resolve(index, key(name)); if (face == null) continue; - byte[] data = program(face); + byte[] data = program(face, operationPrograms); if (!embeddableProgram(programFsType(face, data))) continue; found.put(name, singleFace(data, name)); } @@ -663,8 +697,12 @@ private static void indexFile(Path path, Map index) { return; } boolean collection = b.getInt(0) == TTCF; - for (int f = 0, n = collection ? b.getInt(8) : 1; f < n; f++) { + int faceCount = collection + ? Math.max(0, Math.min(b.getInt(8), (b.capacity() - 12) / 4)) + : 1; + for (int f = 0; f < faceCount; f++) { int offset = collection ? b.getInt(12 + 4 * f) : 0; + if (offset < 0 || offset > b.capacity() - 12) continue; int fsType = fsType(b, offset); // Restricted-license embedding and bitmap-only embedding are both unusable // for a program substitution that must keep outlines, but the face stays in @@ -730,6 +768,12 @@ int record = face + 12 + 16 * i; /** fsType of the bytes actually selected for embedding, not the cached index entry. */ private static int programFsType(Face face, byte[] data) { if (face.resource() != null) return face.fsType(); + if (isType1Program(data)) { + // Type 1 containers have no OS/2 table and therefore no fsType. + // Treat them as embeddable; the native writer validates the program + // before installing it. + return 0; + } try { return data.length < 12 ? -1 : fsType(ByteBuffer.wrap(data), 0); } catch (RuntimeException malformed) { @@ -737,11 +781,24 @@ private static int programFsType(Face face, byte[] data) { } } + private static boolean isType1Program(byte[] data) { + if (data.length >= 2 && (data[0] & 0xFF) == 0x80 && (data[1] & 0xFF) == 0x01) { + return true; // PFB segment header (little-endian 0x8001). + } + int probe = Math.min(data.length, 8192); + String header = new String(data, 0, probe, StandardCharsets.ISO_8859_1); + return header.startsWith("%!PS-AdobeFont") + || header.startsWith("%!FontType1") + || (header.startsWith("%!PS-Adobe-") + && (header.contains("/FontType 1") || header.contains("eexec"))); + } + private static boolean embeddableProgram(int fsType) { return fsType >= 0 && (fsType & 0xF) != 2 && (fsType & 0x0200) == 0; } - private static byte[] program(Face face) throws IOException { + private static byte[] program(Face face, Map operationPrograms) + throws IOException { // A replaced font file invalidates the cached face even when the path and // TTC offset are unchanged; resolve the current key before looking up. Face key = face; @@ -759,6 +816,10 @@ private static byte[] program(Face face) throws IOException { face.fsType()); } } + if (operationPrograms != null) { + byte[] cached = operationPrograms.get(key); + if (cached != null) return cached; + } synchronized (PROGRAMS) { byte[] cached = PROGRAMS.get(key); if (cached != null) return cached; @@ -779,6 +840,7 @@ private static byte[] program(Face face) throws IOException { } } } + if (operationPrograms != null) operationPrograms.put(key, data); synchronized (PROGRAMS) { byte[] raced = PROGRAMS.get(key); if (raced != null) return raced; @@ -788,6 +850,7 @@ private static byte[] program(Face face) throws IOException { } byte[] all = Files.readAllBytes(face.path()); byte[] data = face.offset() < 0 ? all : extractFace(all, face.offset()); + if (operationPrograms != null) operationPrograms.put(key, data); if (data.length > MAX_PROGRAM_BYTES) { return data; } @@ -823,7 +886,7 @@ private static byte[] singleFace(byte[] data, String name) { if (data.length >= 16 && b.getInt(0) == TTCF) { String wanted = key(name); int foundFace = -1; - int count = Math.min(b.getInt(8), (data.length - 12) / 4); + int count = Math.max(0, Math.min(b.getInt(8), (data.length - 12) / 4)); for (int f = 0; f < count; f++) { int faceOffset = b.getInt(12 + 4 * f); requireRange(b, faceOffset, 12); diff --git a/jpdfium/src/main/java/stirling/software/jpdfium/fonts/FontNormalizer.java b/jpdfium/src/main/java/stirling/software/jpdfium/fonts/FontNormalizer.java index 8957d8e4c..f5b81e32d 100644 --- a/jpdfium/src/main/java/stirling/software/jpdfium/fonts/FontNormalizer.java +++ b/jpdfium/src/main/java/stirling/software/jpdfium/fonts/FontNormalizer.java @@ -62,9 +62,11 @@ private FontNormalizer() {} * *

Finds font dictionaries through the page resources (including nested * form XObjects, annotation appearances, and patterns), repairs a missing or - * empty {@code /ToUnicode} map from the embedded program's cmap, and rebuilds + * empty {@code /ToUnicode} map from the embedded program's cmap, and repairs * simple-font {@code /Widths} or CID {@code /W} entries from FreeType's - * unscaled advance widths. Both repairs share one qpdf load/write cycle. + * unscaled advance widths. Missing or malformed simple widths are rebuilt; + * positive producer widths are preserved. Both repairs share one qpdf + * load/write cycle. * *

Fonts without an embedded program are skipped, since there is no cmap or * advance-width source to read. Use @@ -165,9 +167,10 @@ private static int fixToUnicodeInternal(PdfDocument doc, int pageIndex) { *

For simple fonts ({@code /TrueType}, {@code /Type1}, {@code /MMType1}) * the native bridge recomputes {@code /FirstChar}, {@code /LastChar}, and * {@code /Widths} from each glyph's unscaled horizontal advance, normalised to - * 1000 units per em. Existing widths are replaced only when the array is - * missing, has the wrong length, or disagrees with the program. CID fonts - * without a {@code /W} array gain a compact GID-keyed array. Fonts with no + * 1000 units per em. Missing, non-numeric, or non-positive widths are + * repaired; positive producer widths are preserved because they may encode + * deliberate tracking or justification. CID fonts without a {@code /W} array + * gain entries for drawn CIDs plus zero-advance glyphs. Fonts with no * readable embedded program are skipped; embed them with * {@link stirling.software.jpdfium.doc.PdfFontEmbedder} first. * diff --git a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfCompressorTest.java b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfCompressorTest.java index 25e9f1ed8..789a37a0d 100644 --- a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfCompressorTest.java +++ b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfCompressorTest.java @@ -5,6 +5,8 @@ import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.font.PDType0Font; +import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont; +import org.apache.pdfbox.pdmodel.font.encoding.WinAnsiEncoding; import org.apache.pdfbox.text.PDFTextStripper; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledIfSystemProperty; @@ -119,6 +121,55 @@ void compressSubsetsFullEmbeddedTrueTypeFont(@TempDir Path temp) throws Exceptio } } + private static Path simpleTrueTypePdf(Path temp) throws Exception { + Path src = temp.resolve("simple-font.pdf"); + try (InputStream font = PdfCompressorTest.class.getResourceAsStream( + "/org/apache/pdfbox/resources/ttf/LiberationSans-Regular.ttf"); + PDDocument pdf = new PDDocument()) { + assertNotNull(font, "PDFBox no longer bundles LiberationSans"); + PDPage page = new PDPage(); + pdf.addPage(page); + PDTrueTypeFont typeface = PDTrueTypeFont.load( + pdf, font, WinAnsiEncoding.INSTANCE); + try (PDPageContentStream content = new PDPageContentStream(pdf, page)) { + content.beginText(); + content.setFont(typeface, 24); + content.newLineAtOffset(72, 700); + content.showText("Hello World"); + content.endText(); + } + pdf.save(src.toFile()); + } + return src; + } + + @Test + void compressSubsetsSimpleTrueTypeFont(@TempDir Path temp) throws Exception { + assumeTrue(QpdfLib.isSubsetFontsSupported(), "native font subsetting unavailable"); + Path src = simpleTrueTypePdf(temp); + long originalSize = Files.size(src); + BufferedImage before = render(Files.readAllBytes(src), 0); + + try (PdfDocument doc = PdfDocument.open(src)) { + PdfCompressor.CompressResultWithBytes out = PdfCompressor.compress(doc, + CompressOptions.builder().preset(CompressPreset.WEB).subsetFonts(true).build()); + assertTrue(out.result().actions().contains("Subsetted 1 embedded font(s)"), + "expected one subsetted simple font, actions=" + out.result().actions()); + assertTrue(out.bytes().length < originalSize / 2, + "simple subset output should be less than half the source: " + + out.bytes().length + " vs " + originalSize); + assertTrue(out.bytes().length < originalSize / 10, + "compact simple subsets should approach service output, not retain " + + "every glyph slot: " + out.bytes().length + " vs " + originalSize); + try (PDDocument reopened = Loader.loadPDF(out.bytes())) { + assertEquals("Hello World", new PDFTextStripper().getText(reopened).strip(), + "the simple subset must keep the original text"); + } + assertEquals(0, differingPixels(before, render(out.bytes(), 0)), + "simple subsetting must render identically"); + } + } + @Test void losslessPresetDoesNotSubsetFonts(@TempDir Path temp) throws Exception { assumeTrue(QpdfLib.isSubsetFontsSupported(), "native font subsetting unavailable"); diff --git a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderRoundTripTest.java b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderRoundTripTest.java index 12c6eabe3..e6391d7e8 100644 --- a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderRoundTripTest.java +++ b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderRoundTripTest.java @@ -29,6 +29,7 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; @@ -182,11 +183,21 @@ void embeddingMissingFontsKeepsTextAndLayout(Path pdf) throws Exception { continue; } try (a; PdfPage b = after.page(i)) { - if (!gainedToUnicode) { - assertSameOrResolvedText(a.extractText(), b.extractText(), pdf.getFileName() + " page " + i); - } else { - String txt = b.extractText(); - assertFalse(txt.contains("\uFFFD"), pdf.getFileName() + " page " + i + " text contains U+FFFD after gaining ToUnicode"); + String beforeText = a.extractText(); + String afterText = b.extractText(); + assertSameOrResolvedText(beforeText, afterText, + pdf.getFileName() + " page " + i, gainedToUnicode); + if (gainedToUnicode) { + if (!beforeText.isEmpty()) { + assertFalse(afterText.isEmpty(), + pdf.getFileName() + " page " + i + + " lost text after gaining ToUnicode"); + } + if (!beforeText.contains("\uFFFD")) { + assertFalse(afterText.contains("\uFFFD"), + pdf.getFileName() + " page " + i + + " introduced U+FFFD after gaining ToUnicode"); + } } BufferedImage ia = renderable(a); if (ia == null) continue; @@ -278,11 +289,11 @@ private static Map usable(byte[] pdf, Map progra return usable; } - private static PDDocument load(Path pdf) throws java.io.IOException { + private static PDDocument load(Path pdf) throws IOException { PDDocument doc; try { doc = Loader.loadPDF(pdf.toFile()); - } catch (java.io.IOException | RuntimeException e) { + } catch (IOException | RuntimeException e) { assumeTrue(false, "PDFBox cannot load: " + e.getMessage()); return null; } @@ -293,13 +304,13 @@ private static PDDocument load(Path pdf) throws java.io.IOException { return doc; } - private static byte[] save(PDDocument doc) throws java.io.IOException { + private static byte[] save(PDDocument doc) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); doc.save(out, org.apache.pdfbox.pdfwriter.compress.CompressParameters.NO_COMPRESSION); return out.toByteArray(); } - private static void assertEmbeddedStylesMatch(byte[] pdf, java.util.Collection embeddedNames) throws java.io.IOException { + private static void assertEmbeddedStylesMatch(byte[] pdf, Collection embeddedNames) throws IOException { try (PDDocument doc = Loader.loadPDF(pdf)) { for (COSObject object : doc.getDocument().getObjectsByType(COSName.FONT)) { if (!(object.getObject() instanceof COSDictionary font)) continue; @@ -317,18 +328,15 @@ private static void assertEmbeddedStylesMatch(byte[] pdf, java.util.Collection= 0 || a.indexOf('\uFFFD') >= 0; + if (!unresolved && a.equals(b)) return; + if (!unresolved && gainedToUnicode && b.length() > a.length() + && !b.contains("\uFFFD") && isSubsequence(a, b)) { + // Repair can recover characters PDFium silently omitted from the + // source (sample_fonts_solidconvertor.pdf). Requiring the original + // text to remain an ordered subsequence still catches dropped text. + return; + } if (!unresolved) { assertEquals(a, b, where + " text"); return; } - assertEquals(a.length(), b.length(), where + " text length"); - for (int i = 0; i < a.length(); i++) { - char c = a.charAt(i); - assertTrue(c == b.charAt(i) || c == '?' || c == '\uFFFD', - where + " text differs at " + i + ": " + a + " vs " + b); + assertFalse(b.isEmpty(), where + " text became empty after resolving"); + // Resolving a replacement can expand one marker into several scalars + // (for example a ligature or a surrogate pair). Require every resolved + // character to survive in order rather than requiring an equal length. + int matched = 0; + int i = 0; + while (i < b.length() && matched < a.length()) { + char expected = a.charAt(matched); + if (expected == '?' || expected == '\uFFFD') { + // A repaired marker must produce at least one output scalar; + // otherwise a dropped character would look like success. + if (i < b.length()) { + matched++; + i++; + } + } else if (b.charAt(i) == expected) { + matched++; + i++; + } else { + i++; + } + } + while (matched < a.length() + && (a.charAt(matched) == '?' || a.charAt(matched) == '\uFFFD')) { + matched++; } + assertEquals(a.length(), matched, where + " lost resolved text: " + a + " vs " + b); } - private static String style(String name) { + private static String style(String name) { String lower = name.replaceFirst("^[A-Z]{6}\\+", "").toLowerCase(java.util.Locale.ROOT); return (lower.matches(".*(bold|black|heavy|negrita|negreta|fett|grassetto|gras).*") ? "bold" : "") + (lower.matches(".*(italic|oblique|cursiva|kursiv|corsivo|italique|cursiva).*") ? "italic" : ""); 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 298420e01..455fe0c0a 100644 --- a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderTest.java +++ b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderTest.java @@ -12,6 +12,7 @@ import org.apache.pdfbox.cos.COSName; import org.apache.pdfbox.cos.COSStream; import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.common.PDRectangle; import org.apache.pdfbox.pdmodel.PDPageContentStream; @@ -102,6 +103,44 @@ void fileBackedEmbeddingCopiesTheInputWhenNoFontMatches(@TempDir Path temp) thro assertArrayEquals(Files.readAllBytes(input), Files.readAllBytes(output)); } + @Test + void repeatedDistinctFormsExhaustTheScanBudgetAndFailClosed(@TempDir Path temp) throws Exception { + assumeTrue(PdfFontEmbedder.isAvailable(), "native qpdf font embedding unavailable"); + Path input = manyFormPage(temp.resolve("many-forms.pdf"), 2_100); + + PdfFontEmbedder.Result result = + PdfFontEmbedder.embedMissing(Files.readAllBytes(input), liberationSansDir(temp)); + + assertTrue(result.unresolved().contains("Helvetica"), + "a scan that exhausts its stream budget must not claim success"); + } + + private static Path manyFormPage(Path target, int forms) throws IOException { + try (PDDocument doc = new PDDocument()) { + PDPage page = new PDPage(); + doc.addPage(page); + PDResources resources = new PDResources(); + resources.put(COSName.getPDFName("F1"), + new PDType1Font(Standard14Fonts.FontName.HELVETICA)); + page.setResources(resources); + PDFormXObject[] xobjects = new PDFormXObject[forms]; + for (int i = 0; i < forms; i++) { + PDStream stream = new PDStream(doc); + try (OutputStream out = stream.createOutputStream()) { + out.write("q Q\n".getBytes(StandardCharsets.ISO_8859_1)); + } + PDFormXObject form = new PDFormXObject(stream); + form.setBBox(new PDRectangle(0, 0, 10, 10)); + xobjects[i] = form; + } + try (PDPageContentStream content = new PDPageContentStream(doc, page)) { + for (PDFormXObject form : xobjects) content.drawForm(form); + } + doc.save(target.toFile()); + } + return target; + } + private static Path helveticaPage(Path target) throws IOException { try (PDDocument doc = new PDDocument()) { PDPage page = new PDPage(); @@ -1310,6 +1349,90 @@ void replacedFontFileRechecksEmbeddingPermission(@TempDir Path temp) throws Exce restricted.statuses().toString()); } + @Test + void type1PfbProgramsAreEmbedded(@TempDir Path temp) throws Exception { + assumeTrue(PdfFontEmbedder.isAvailable(), "native qpdf font embedding unavailable"); + Path ghostscript = Path.of("/opt/homebrew/share/ghostscript/fonts"); + assumeTrue(Files.isDirectory(ghostscript), "no Ghostscript Type 1 fonts"); + byte[] input = namedType1Pdf("NimbusSanL-Regu"); + + PdfFontEmbedder.Result result = PdfFontEmbedder.embedMissing(input, ghostscript); + + assertTrue(result.embedded().contains("NimbusSanL-Regu"), + "a PFB program has no OS/2 fsType and must stay embeddable: " + + result.statuses()); + assertTrue(result.unresolved().isEmpty(), result.unresolved().toString()); + try (PDDocument pdf = Loader.loadPDF(result.pdf())) { + COSDictionary font = pdf.getPage(0).getResources() + .getFont(COSName.getPDFName("F1")).getCOSObject(); + COSDictionary descriptor = + (COSDictionary) font.getDictionaryObject(COSName.FONT_DESC); + assertNotNull(descriptor); + assertNotNull(descriptor.getDictionaryObject(COSName.FONT_FILE), + "the PFB program must be installed as /FontFile"); + } + } + + @Test + void truncatedTrueTypeCollectionIsRejected() throws Exception { + assumeTrue(PdfFontEmbedder.isAvailable(), "native qpdf font embedding unavailable"); + byte[] ttc = new byte[16]; + ttc[0] = 't'; + ttc[1] = 't'; + ttc[2] = 'c'; + ttc[3] = 'f'; + ttc[7] = 1; // one face + ttc[15] = (byte) 0xFF; // face offset beyond the file + byte[] input = Files.readAllBytes(Path.of(PdfFontEmbedderTest.class + .getResource("/pdfs/general/minimal.pdf").toURI())); + assertThrows(IllegalArgumentException.class, + () -> PdfFontEmbedder.embed(input, Map.of("CorruptTTC", ttc))); + } + + private static byte[] namedType1Pdf(String baseFont) throws IOException { + try (PDDocument pdf = new PDDocument()) { + PDPage page = new PDPage(PDRectangle.LETTER); + pdf.addPage(page); + COSDictionary descriptor = new COSDictionary(); + descriptor.setItem(COSName.TYPE, COSName.FONT_DESC); + descriptor.setName(COSName.FONT_NAME, baseFont); + descriptor.setInt(COSName.FLAGS, 32); + COSDictionary font = new COSDictionary(); + font.setItem(COSName.TYPE, COSName.FONT); + font.setItem(COSName.SUBTYPE, COSName.TYPE1); + font.setName(COSName.BASE_FONT, baseFont); + font.setItem(COSName.FONT_DESC, descriptor); + COSDictionary fonts = new COSDictionary(); + fonts.setItem("F1", font); + COSDictionary resources = new COSDictionary(); + resources.setItem(COSName.FONT, fonts); + page.setResources(new PDResources(resources)); + page.setContents(new PDStream(pdf, new ByteArrayInputStream( + "BT /F1 12 Tf 40 700 Td (Hello) Tj ET" + .getBytes(StandardCharsets.US_ASCII)))); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + pdf.save(out); + return out.toByteArray(); + } + } + + @Test + void builtInSymbolicFontsAreLeftUnresolved() throws Exception { + assumeTrue(PdfFontEmbedder.isAvailable(), "native qpdf font embedding unavailable"); + byte[] input = Files.readAllBytes(Path.of(PdfFontEmbedderTest.class + .getResource("/pdfs/general/pdfjs_ZapfDingbats.pdf").toURI())); + + PdfFontEmbedder.Result result = PdfFontEmbedder.embedMissing(input); + + assertFalse(result.embedded().stream() + .anyMatch(name -> PdfFontEmbedder.key(name).startsWith("zapfdingbats")), + "built-in ZapfDingbats encoding must not be substituted: " + result); + assertTrue(result.statuses().stream().anyMatch(status -> + status.baseName().equals("ZapfDingbats") + && status.reason().contains("symbolic encoding")), + "statuses: " + result.statuses()); + } + @Test void fileBackedEmbeddingEmbedsResourceOnlyFonts(@TempDir Path temp) throws Exception { assumeTrue(PdfFontEmbedder.isAvailable(), "native qpdf font embedding unavailable"); 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 683a10426..ce0d29184 100644 --- a/jpdfium/src/test/java/stirling/software/jpdfium/fonts/FontNormalizerTest.java +++ b/jpdfium/src/test/java/stirling/software/jpdfium/fonts/FontNormalizerTest.java @@ -298,6 +298,40 @@ void repairWidthsLeavesMatchingRealValuedWidthsUnchanged() throws Exception { } } + @Test + void repairWidthsPreservesPlausibleProducerWidth() throws Exception { + assumeTrue(NativeRuntime.isFull(), "needs real native library"); + byte[] pdf = withIntegerWidth(embeddedTrueTypePdf(false, false), 'H', 700); + try (PdfDocument doc = PdfDocument.open(pdf)) { + assertEquals(0, FontNormalizer.repairWidths(doc, 0), + "a positive producer width must not be overwritten merely for differing"); + byte[] unchanged = doc.saveBytes(); + try (PDDocument loaded = Loader.loadPDF(unchanged)) { + COSDictionary font = firstFontDictionary(loaded); + assertNotNull(font); + COSArray widths = (COSArray) font.getDictionaryObject(COSName.WIDTHS); + assertNotNull(widths); + int firstChar = font.getInt(COSName.FIRST_CHAR); + assertEquals(700, widths.getInt('H' - firstChar), + "the producer's tracking width must survive the repair pass"); + } + } + } + + private static byte[] withIntegerWidth(byte[] pdf, char code, int width) throws IOException { + try (PDDocument doc = Loader.loadPDF(pdf)) { + COSDictionary font = firstFontDictionary(doc); + assertNotNull(font); + COSArray widths = (COSArray) font.getDictionaryObject(COSName.WIDTHS); + assertNotNull(widths); + int firstChar = font.getInt(COSName.FIRST_CHAR); + widths.set(code - firstChar, COSInteger.get(width)); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + doc.save(out); + return out.toByteArray(); + } + } + @Test void fixToUnicodeReplacesBoilerplateOnlyCMap() throws Exception { assumeTrue(NativeRuntime.isFull(), "needs real native library"); @@ -405,6 +439,39 @@ void fixToUnicodeMapsWinAnsiHighRangeAndDifferences() throws Exception { } } + @Test + void normalizeAllProcessesDistinctFontsOnEveryPage() throws Exception { + assumeTrue(NativeRuntime.isFull(), "needs real native library"); + byte[] pdf = twoPageEmbeddedFontsPdf(); + try (PdfDocument doc = PdfDocument.open(pdf)) { + FontNormalizer.Result result = FontNormalizer.normalizeAll(doc); + assertEquals(2, result.fontsProcessed(), + "the all-page pass must visit both pages: " + result); + assertEquals(2, result.widthsRepaired(), + "each page's distinct font needs its own width repair: " + result); + + byte[] repaired = doc.saveBytes(); + try (PDDocument loaded = Loader.loadPDF(repaired)) { + for (int page = 0; page < 2; page++) { + COSDictionary font = loaded.getPage(page).getResources() + .getFont(COSName.getPDFName("F1")).getCOSObject(); + COSArray widths = (COSArray) font.getDictionaryObject(COSName.WIDTHS); + assertNotNull(widths, "page " + page + " font must carry /Widths"); + int firstChar = font.getInt(COSName.FIRST_CHAR, 0); + assertEquals(722, widths.getInt('H' - firstChar), + "page " + page + " H width must match the program"); + assertNotNull(font.getDictionaryObject(COSName.TO_UNICODE), + "page " + page + " font must carry /ToUnicode"); + PDFTextStripper stripper = new PDFTextStripper(); + stripper.setStartPage(page + 1); + stripper.setEndPage(page + 1); + assertEquals("H", stripper.getText(loaded).strip(), + "page " + page + " must extract its repaired text"); + } + } + } + } + @Test void normalizePageRepairsToUnicodeAndWidthsInOnePass() throws Exception { assumeTrue(NativeRuntime.isFull(), "needs real native library"); @@ -610,6 +677,34 @@ void normalizeAllSynthesizesCidSetFromProgramCoverage() throws Exception { } } + @Test + void normalizeAllWritesCidSetForRetainedSubsetGlyphs() throws Exception { + assumeTrue(NativeRuntime.isFull(), "needs real native library"); + byte[] pdf = cidTrueTypePdf("LiberationSans", new byte[] {(byte) 0x80}); + try (PdfDocument doc = PdfDocument.open(pdf)) { + FontNormalizer.Result result = FontNormalizer.normalizeAll(doc); + assertEquals(1, result.resubset(), "an untagged CID font must be re-subset"); + + byte[] repaired = doc.saveBytes(); + try (PDDocument loaded = Loader.loadPDF(repaired)) { + COSDictionary descendant = cidDescendant(loaded); + assertNotNull(descendant); + COSDictionary descriptor = + (COSDictionary) descendant.getDictionaryObject(COSName.FONT_DESC); + assertNotNull(descriptor); + byte[] cidSet = streamBytes(descriptor, COSName.CID_SET); + assertEquals(5, cidSet.length, + "CIDSet must stop at the highest retained CID, not describe the full program"); + assertEquals((byte) 0x80, cidSet[0], "CID 0 (.notdef) must be present"); + assertEquals((byte) 0x08, cidSet[4], "CID 36 is the only drawn glyph"); + for (int i = 1; i < 4; i++) + assertEquals(0, cidSet[i], "undrawn CID " + i + " must not be present"); + assertEquals("A", new PDFTextStripper().getText(loaded).strip(), + "re-subsetting must preserve extracted text"); + } + } + } + @Test void normalizeAllRepairsStaleCidSetAndInconsistentWidths() throws Exception { assumeTrue(NativeRuntime.isFull(), "needs real native library"); @@ -861,7 +956,7 @@ private static byte[] unresolvedWidthPdf() throws IOException { assertTrue(65 >= firstChar && 72 - firstChar < widths.size(), "fixture widths must cover codes 65 and 72"); widths.set(65 - firstChar, COSInteger.get(777)); - widths.set(72 - firstChar, COSInteger.get(1)); + widths.set(72 - firstChar, COSInteger.get(0)); ByteArrayOutputStream out = new ByteArrayOutputStream(); doc.save(out); @@ -935,6 +1030,48 @@ private static byte[] formXObjectFontPdf() throws IOException { * Builds a one-page PDF with an Identity-H CIDFontType2 backed by Liberation Sans, with * {@code /ToUnicode} and the descendant's {@code /W} removed. CID and GID are identical. */ + private static byte[] twoPageEmbeddedFontsPdf() throws IOException { + byte[] program = liberationSansBytes(); + try (PDDocument doc = new PDDocument()) { + for (int pageIndex = 0; pageIndex < 2; pageIndex++) { + String baseFont = pageIndex == 0 ? "PageOne-Regular" : "PageTwo-Regular"; + PDPage page = new PDPage(PDRectangle.LETTER); + doc.addPage(page); + COSDictionary descriptor = new COSDictionary(); + descriptor.setItem(COSName.TYPE, COSName.FONT_DESC); + descriptor.setName(COSName.FONT_NAME, baseFont); + descriptor.setInt(COSName.FLAGS, 32); + descriptor.setItem(COSName.FONT_BBOX, new COSArray()); + descriptor.setInt(COSName.ITALIC_ANGLE, 0); + descriptor.setInt(COSName.ASCENT, 900); + descriptor.setInt(COSName.DESCENT, -200); + descriptor.setInt(COSName.CAP_HEIGHT, 700); + descriptor.setInt(COSName.STEM_V, 80); + COSStream fontFile = new PDStream(doc, new ByteArrayInputStream(program)) + .getCOSObject(); + fontFile.setInt(COSName.LENGTH1, program.length); + descriptor.setItem(COSName.FONT_FILE2, fontFile); + COSDictionary font = new COSDictionary(); + font.setItem(COSName.TYPE, COSName.FONT); + font.setItem(COSName.SUBTYPE, COSName.TRUE_TYPE); + font.setName(COSName.BASE_FONT, baseFont); + font.setItem(COSName.ENCODING, COSName.WIN_ANSI_ENCODING); + font.setItem(COSName.FONT_DESC, descriptor); + COSDictionary fonts = new COSDictionary(); + fonts.setItem(COSName.getPDFName("F1"), font); + COSDictionary resources = new COSDictionary(); + resources.setItem(COSName.FONT, fonts); + page.setResources(new PDResources(resources)); + page.setContents(new PDStream(doc, new ByteArrayInputStream( + "BT /F1 12 Tf 72 700 Td (H) Tj ET" + .getBytes(StandardCharsets.US_ASCII)))); + } + ByteArrayOutputStream out = new ByteArrayOutputStream(); + doc.save(out); + return out.toByteArray(); + } + } + private static byte[] cidTrueTypePdf() throws IOException { return cidTrueTypePdf("LiberationSans", null); } diff --git a/native/bridge/src/jpdfium_advanced.cpp b/native/bridge/src/jpdfium_advanced.cpp index 9766a0748..a037e6311 100644 --- a/native/bridge/src/jpdfium_advanced.cpp +++ b/native/bridge/src/jpdfium_advanced.cpp @@ -741,7 +741,9 @@ SimpleEncoding parseSimpleEncoding(QPDFObjectHandle const& font, code = item.getIntValueAsInt(); } else if (item.isName()) { if (code >= 0 && code <= 255) enc.differences[code] = item.getName().substr(1); - ++code; + // Keep advancing only inside the single-byte range. A + // malformed /Differences can contain INT_MAX. + if (code < 256) ++code; } } } @@ -1209,6 +1211,13 @@ static bool fn_is_pdf_delimiter(unsigned char c) { c == '}' || c == '/' || c == '%'; } +static bool fn_is_pdf_whitespace(unsigned char c) { + // ISO 32000-1 7.2.2: NUL is whitespace in content streams. std::isspace + // does not classify it, which would desynchronize the tokenizer on + // producers that pad operands with NUL. + return c == 0x00 || c == 0x09 || c == 0x0A || c == 0x0C || c == 0x0D || c == 0x20; +} + struct FnContentOperand { enum class Kind { Name, String, Array, Other }; Kind kind = Kind::Other; @@ -1231,7 +1240,7 @@ struct FnContentLexer { void skipSpace() { while (pos < data.size()) { unsigned char c = static_cast(data[pos]); - if (std::isspace(c)) { + if (fn_is_pdf_whitespace(c)) { ++pos; } else if (c == '%') { while (pos < data.size() && data[pos] != '\n' && data[pos] != '\r') ++pos; @@ -1303,7 +1312,7 @@ struct FnContentLexer { int first_nibble = -1; while (pos < data.size() && data[pos] != '>') { unsigned char c = static_cast(data[pos++]); - if (std::isspace(c)) continue; + if (fn_is_pdf_whitespace(c)) continue; int val = -1; if (c >= '0' && c <= '9') { val = c - '0'; @@ -1340,8 +1349,7 @@ struct FnContentLexer { }; while (pos < data.size()) { unsigned char c = static_cast(data[pos]); - if (std::isspace(c) || c == '/' || c == '(' || c == ')' || c == '<' || c == '>' || - c == '[' || c == ']' || c == '{' || c == '}' || c == '%') { + if (fn_is_pdf_whitespace(c) || fn_is_pdf_delimiter(c)) { break; } if (c == '#' && pos + 2 < data.size() && @@ -1467,8 +1475,7 @@ struct FnContentLexer { size_t start = pos; while (pos < data.size()) { unsigned char ch = static_cast(data[pos]); - if (std::isspace(ch) || ch == '/' || ch == '(' || ch == ')' || ch == '<' || ch == '>' || - ch == '[' || ch == ']' || ch == '{' || ch == '}' || ch == '%') { + if (fn_is_pdf_whitespace(ch) || fn_is_pdf_delimiter(ch)) { break; } ++pos; @@ -1488,18 +1495,25 @@ struct FnContentLexer { } void skipInlineImage() { - // Image data may abut EI with no separating whitespace, and EI may be - // followed by a delimiter instead of whitespace, so validate only the - // trailing boundary. + // ISO 32000-1 7.2.2: EI is a token, so it must be separated from the + // image bytes by whitespace and from the next token by whitespace or a + // delimiter. Requiring both boundaries avoids treating a binary "EI" + // sequence inside the image data as the end marker. A stream that + // violates the delimiter rule is reported malformed so callers fail + // closed instead of trusting a partial scan. FnContentToken token; while (next(token)) { if (token.word && token.text == "ID") { - if (pos < data.size() && std::isspace(static_cast(data[pos]))) ++pos; + if (pos < data.size() && + fn_is_pdf_whitespace(static_cast(data[pos]))) + ++pos; while (pos + 1 < data.size()) { - if (data[pos] == 'E' && data[pos + 1] == 'I') { + if (data[pos] == 'E' && data[pos + 1] == 'I' && + (pos == 0 || + fn_is_pdf_whitespace(static_cast(data[pos - 1])))) { const size_t after = pos + 2; if (after >= data.size() || - std::isspace(static_cast(data[after])) || + fn_is_pdf_whitespace(static_cast(data[after])) || fn_is_pdf_delimiter(static_cast(data[after]))) { pos = after; return; @@ -2064,52 +2078,46 @@ static void repair_simple_font_widths(QPDFObjectHandle& target, FT_Face face, } std::vector widths; - std::vector resolved_codes; widths.reserve(static_cast(last_char - first_char + 1)); - resolved_codes.reserve(static_cast(last_char - first_char + 1)); bool any_positive = false; QPDFObjectHandle existing_widths = target.getKey("/Widths"); const bool has_widths_array = existing_widths.isArray(); + bool needs_rewrite = + !has_widths_array || existing_widths.getArrayNItems() != (last_char - first_char + 1); for (int code = first_char; code <= last_char; ++code) { - int width = 0; - if (simple_glyphs[code].gid != 0 && - pdf_width_from_glyph(face, simple_glyphs[code].gid, width)) { - if (width > 0) any_positive = true; - widths.push_back(width); - resolved_codes.push_back(true); - continue; - } - // A re-subset face no longer maps codes whose glyphs were dropped. - // Preserve the producer's width for unresolved codes instead of zeroing them. - int preserved = 0; - if (has_widths_array) { - const int index = code - first_char; - if (index >= 0 && index < existing_widths.getArrayNItems()) { - QPDFObjectHandle item = existing_widths.getArrayItem(index); - if (item.isNumber()) - preserved = static_cast(std::lround(item.getNumericValue())); - } - } - if (preserved > 0) any_positive = true; - widths.push_back(preserved); - resolved_codes.push_back(false); - } - - bool differs = - !has_widths_array || existing_widths.getArrayNItems() != static_cast(widths.size()); - if (!differs) { - for (size_t i = 0; i < widths.size(); ++i) { - if (!resolved_codes[i]) continue; - // Accept real-valued widths within one PDF glyph unit of FreeType's rounded advance. - QPDFObjectHandle item = existing_widths.getArrayItem(static_cast(i)); - if (!item.isNumber() || std::abs(item.getNumericValue() - widths[i]) > 1.0) { - differs = true; - break; + const int index = code - first_char; + QPDFObjectHandle item; + if (has_widths_array && index >= 0 && index < existing_widths.getArrayNItems()) + item = existing_widths.getArrayItem(index); + const bool existing_number = item.isNumber(); + const bool existing_nonnegative = existing_number && item.getNumericValue() >= 0.0; + const int preserved = + existing_number ? std::max(0, static_cast(std::lround(item.getNumericValue()))) + : 0; + int width = preserved; + int computed = 0; + const bool resolved = simple_glyphs[code].gid != 0 && + pdf_width_from_glyph(face, simple_glyphs[code].gid, computed); + if (resolved) { + if (existing_number && (preserved > 0 || (computed == 0 && existing_nonnegative))) { + // Keep a positive producer width even when it differs from the + // program advance; keep a legitimate zero-advance glyph too. + width = preserved; + } else { + // Only a missing, non-numeric, or non-positive width that + // disagrees with the program is demonstrably inconsistent. + width = std::max(0, computed); + needs_rewrite = true; } + } else if (!existing_number) { + needs_rewrite = true; + width = 0; } + if (width > 0) any_positive = true; + widths.push_back(width); } - if (differs && any_positive) { + if (needs_rewrite && any_positive) { QPDFObjectHandle array = QPDFObjectHandle::newArray(); for (int width : widths) array.appendItem(QPDFObjectHandle::newInteger(width)); target.replaceKey("/FirstChar", QPDFObjectHandle::newInteger(first_char)); @@ -2216,24 +2224,22 @@ static void repair_cid_font_widths(QPDF& pdf, QPDFObjectHandle& font, QPDFObject QPDFObjectHandle existing_w = target.getKey("/W"); std::map widths = parse_cid_widths(existing_w); - // With no /W at all, synthesize every mapped CID. With an existing array, - // repair only the CIDs the content draws: unused entries may be deliberate - // producer metadata and are not checked by the validator. - std::set check; + // With no /W at all, synthesize entries for the CIDs that content draws. + // An absent /W means every CID falls back to /DW; writing an entry for + // every mapped glyph in a large program adds size without changing layout. + // With an existing array, repair only the CIDs the content draws: unused + // entries may be deliberate producer metadata and are not checked by the + // validator. + FnCidScan scan = fn_used_cids_for_font(pdf, font, true, &state); + if (!scan.complete || scan.cids.empty()) return; + std::set check = scan.cids; if (widths.empty()) { + // A synthesized /W must spell out zero-advance glyphs even when the + // current page does not draw them, because /DW would otherwise give + // them a false advance in text extraction or later edits. for (auto const& [cid, gid] : cid_gid) { - (void)gid; - check.insert(cid); - } - } else { - FnCidScan scan = fn_used_cids_for_font(pdf, font, true, &state); - if (!scan.complete) return; - check = scan.cids; - if (check.empty()) { - for (auto const& [cid, gid] : cid_gid) { - (void)gid; - check.insert(cid); - } + int width = 0; + if (pdf_width_from_glyph(face, gid, width) && width == 0) check.insert(cid); } } @@ -2261,17 +2267,20 @@ static void repair_cid_font_widths(QPDF& pdf, QPDFObjectHandle& font, QPDFObject // Convert a CFF-in-SFNT (OTTO) program into a bare CFF stream (/Type1C or /CIDFontType0C) // when attached to a /Type1, /MMType1, or /CIDFontType0 dictionary. -static void convert_cff_in_sfnt(QPDF& pdf, QPDFObjectHandle& target, QPDFObjectHandle& descriptor, +static void convert_cff_in_sfnt(QPDF& pdf, QPDFObjectHandle& descriptor, std::unique_ptr& face, bool cid, std::string const& subtype_name, + std::vector const& simple_glyphs, FontNormalizeState& state) { if (!FT_IS_SFNT(face.get()) || !font_has_sfnt_table(face.get(), FT_MAKE_TAG('C', 'F', 'F', ' ')) || font_has_sfnt_table(face.get(), FT_MAKE_TAG('g', 'l', 'y', 'f'))) return; const bool simple_type1 = !cid && (subtype_name == "/Type1" || subtype_name == "/MMType1"); - const bool cid_type0 = cid && target.getKey("/Subtype").isNameAndEquals("/CIDFontType0"); - if (!simple_type1 && !cid_type0) return; + // CID-keyed CFF is deliberately left in its OpenType wrapper: converting it + // to /CIDFontType0C changes PDFium's glyph rasterization. Simple Type 1 + // fonts are converted only when every resolved glyph name survives. + if (!simple_type1) return; FT_ULong table_length = 0; if (FT_Load_Sfnt_Table(face.get(), FT_MAKE_TAG('C', 'F', 'F', ' '), 0, nullptr, @@ -2289,7 +2298,22 @@ static void convert_cff_in_sfnt(QPDF& pdf, QPDFObjectHandle& target, QPDFObjectH bool valid = FT_New_Memory_Face(ft_lib, reinterpret_cast(cff.data()), static_cast(cff.size()), 0, &cff_face) == 0 && cff_face; - if (valid && !cid) valid = FT_HAS_GLYPH_NAMES(cff_face) != 0; + if (valid && !cid) { + // A bare CFF reaches glyphs by name for a simple font. Every code the + // PDF encoding resolves through the OpenType wrapper must keep the same + // glyph name, or the converted stream would draw .notdef where the + // wrapper drew a real glyph. + valid = FT_HAS_GLYPH_NAMES(cff_face) != 0; + for (size_t code = 0; valid && code < simple_glyphs.size(); ++code) { + FT_UInt gid = simple_glyphs[code].gid; + if (!gid) continue; + char name[256] = ""; + if (FT_Get_Glyph_Name(face.get(), gid, name, sizeof(name)) != 0 || !name[0] || + std::strcmp(name, ".notdef") == 0) + continue; + if (FT_Get_Name_Index(cff_face, name) == 0) valid = false; + } + } if (cff_face) FT_Done_Face(cff_face); if (!valid) return; @@ -2358,7 +2382,8 @@ static bool resubset_embedded_sfnt(QPDF& pdf, QPDFObjectHandle& font, QPDFObject std::unique_ptr& face, bool cid, QPDFObjectHandle& cid_to_gid, bool cid_identity, std::vector const& simple_glyphs, - FontUsageCounts const& usage, FontNormalizeState& state) { + FontUsageCounts const& usage, FontNormalizeState& state, + std::set* retained_glyphs = nullptr) { if (face->num_glyphs <= 1 || !FT_IS_SFNT(face.get()) || font_has_sfnt_table(face.get(), FT_MAKE_TAG('C', 'F', 'F', '2')) || font_forbids_subsetting(face.get())) @@ -2475,6 +2500,7 @@ static bool resubset_embedded_sfnt(QPDF& pdf, QPDFObjectHandle& font, QPDFObject } ++state.resubset; state.changed = true; + if (retained_glyphs) *retained_glyphs = target_glyphs; return true; } @@ -2512,6 +2538,46 @@ static std::set cids_present_in_program(FT_Face face, QPDFObjectHandle return cids; } +// CIDs whose GID is present in a re-subset program. With RETAIN_GIDS the +// program keeps the original glyph indices, so CIDToGIDMap and the CID-keyed +// CFF charset still describe which CIDs survived. +static std::set cids_for_retained_glyphs(QPDFObjectHandle& cid_to_gid, bool cid_identity, + std::map const& gid_to_cid, + std::set const& glyphs) { + std::set cids{0}; + if (cid_to_gid.isStream()) { + try { + std::shared_ptr data = cid_to_gid.getStreamData(qpdf_dl_generalized); + if (data) { + const auto* bytes = static_cast(data->getBuffer()); + const size_t entries = data->getSize() / 2; + for (size_t c = 0; c < entries && c <= kFontMaxCid; ++c) { + const uint32_t gid = + (static_cast(bytes[2 * c]) << 8) | bytes[2 * c + 1]; + if (glyphs.contains(static_cast(gid))) + cids.insert(static_cast(c)); + } + } + } catch (...) { + return {0}; + } + return cids; + } + if (cid_identity && !gid_to_cid.empty()) { + for (FT_UInt gid : glyphs) { + auto found = gid_to_cid.find(static_cast(gid)); + if (found != gid_to_cid.end() && found->second <= kFontMaxCid) + cids.insert(found->second); + } + return cids; + } + if (cid_identity) { + for (FT_UInt gid : glyphs) + if (gid <= kFontMaxCid) cids.insert(static_cast(gid)); + } + return cids; +} + // Repair or synthesize /CIDSet so its bitset matches the embedded program. // ISO 19005-2 6.2.11.4 test 4 counts every CID present in the program, // regardless of whether content draws it. @@ -2704,16 +2770,44 @@ static void process_font(QPDF& pdf, QPDFObjectHandle font, bool do_fix_tounicode bool did_convert = false; if (full) { const int32_t converted_before = state.type1_converted; - convert_cff_in_sfnt(pdf, target, descriptor, face, cid, subtype_name, state); + convert_cff_in_sfnt(pdf, descriptor, face, cid, subtype_name, simple_glyphs, state); did_convert = state.type1_converted != converted_before; } + bool did_resubset = false; + std::set retained_glyphs; if (full && !did_convert) { - resubset_embedded_sfnt(pdf, font, target, descriptor, stream, data, face_index, face, cid, - cid_to_gid, cid_identity, simple_glyphs, usage, state); + did_resubset = resubset_embedded_sfnt(pdf, font, target, descriptor, stream, data, + face_index, face, cid, cid_to_gid, cid_identity, + simple_glyphs, usage, state, &retained_glyphs); } if (cid && full) { - repair_cidset_stream(pdf, descriptor, face.get(), cid_to_gid, cid_identity, gid_to_cid, - state); + if (did_resubset) { + // The FreeType face still describes the original program. Derive + // CIDSet from the glyphs the subset actually retained, not from + // the pre-subset program or from drawn CIDs alone. + std::set cids = + cids_for_retained_glyphs(cid_to_gid, cid_identity, gid_to_cid, retained_glyphs); + std::string bits = build_cid_set_bits(cids); + QPDFObjectHandle existing = descriptor.getKey("/CIDSet"); + bool differs = true; + if (existing.isStream()) { + try { + std::shared_ptr old = existing.getStreamData(qpdf_dl_generalized); + if (old) + differs = std::string(reinterpret_cast(old->getBuffer()), + old->getSize()) != bits; + } catch (...) { + differs = true; + } + } + if (differs) { + descriptor.replaceKey("/CIDSet", QPDFObjectHandle::newStream(&pdf, bits)); + state.changed = true; + } + } else { + repair_cidset_stream(pdf, descriptor, face.get(), cid_to_gid, cid_identity, gid_to_cid, + state); + } } } diff --git a/native/bridge/src/jpdfium_pdfx.cpp b/native/bridge/src/jpdfium_pdfx.cpp index 677ca1402..eb198b23c 100644 --- a/native/bridge/src/jpdfium_pdfx.cpp +++ b/native/bridge/src/jpdfium_pdfx.cpp @@ -250,13 +250,16 @@ void ClampPatternSteps(QPDFObjectHandle pattern) { if (!type.isInteger() || type.getIntValueAsInt() != 1) return; for (const char* key : {"/XStep", "/YStep"}) { QPDFObjectHandle value = pattern.getKey(key); + auto needsClamp = [](double candidate) { + return !std::isfinite(candidate) || candidate <= 0.0 || candidate > 32767.0; + }; double step = value.isNumber() ? value.getNumericValue() : 1.0; - if (!std::isfinite(step) || step <= 0.0) { - step = 1.0; - } else if (step > 32767.0) { - step = 32767.0; - } - if (!value.isNumber() || value.getNumericValue() != step) { + if (!value.isNumber() || needsClamp(step)) { + if (!std::isfinite(step) || step <= 0.0) { + step = 1.0; + } else if (step > 32767.0) { + step = 32767.0; + } pattern.replaceKey(key, QPDFObjectHandle::newReal(step, 4)); } } diff --git a/native/bridge/src/jpdfium_qpdf.cpp b/native/bridge/src/jpdfium_qpdf.cpp index 5e06eb5d0..43b3e508f 100644 --- a/native/bridge/src/jpdfium_qpdf.cpp +++ b/native/bridge/src/jpdfium_qpdf.cpp @@ -529,13 +529,16 @@ void clampTilingPatternStep(QPDFObjectHandle pattern) { if (!type.isInteger() || type.getIntValueAsInt() != 1) return; for (char const* key : {"/XStep", "/YStep"}) { QPDFObjectHandle value = pattern.getKey(key); + auto needsClamp = [](double candidate) { + return !std::isfinite(candidate) || candidate <= 0.0 || candidate > 32767.0; + }; double step = value.isNumber() ? value.getNumericValue() : 1.0; - if (!std::isfinite(step) || step <= 0.0) { - step = 1.0; - } else if (step > 32767.0) { - step = 32767.0; - } - if (!value.isNumber() || value.getNumericValue() != step) { + if (!value.isNumber() || needsClamp(step)) { + if (!std::isfinite(step) || step <= 0.0) { + step = 1.0; + } else if (step > 32767.0) { + step = 32767.0; + } pattern.replaceKey(key, QPDFObjectHandle::newReal(step, 4)); } } @@ -1814,7 +1817,8 @@ std::string extractCffTable(FT_Library lib, std::string const& sfntBytes) { return cff; } -std::string subsetProgram(FontProgram const& p, std::set const& glyphs) { +std::string subsetProgram(FontProgram const& p, std::set const& glyphs, + bool retainGids = true) { if (p.bytes.size() > std::numeric_limits::max()) return {}; hb_blob_t* blob = hb_blob_create(p.bytes.data(), static_cast(p.bytes.size()), HB_MEMORY_MODE_READONLY, nullptr, nullptr); @@ -1829,8 +1833,9 @@ std::string subsetProgram(FontProgram const& p, std::set const& glyphs) u = FT_Get_Next_Char(p.face, u, &gid)) if (glyphs.contains(gid)) hb_set_add(hb_subset_input_unicode_set(input), u); } - hb_subset_input_set_flags(input, HB_SUBSET_FLAGS_RETAIN_GIDS | HB_SUBSET_FLAGS_GLYPH_NAMES | - HB_SUBSET_FLAGS_NOTDEF_OUTLINE); + hb_subset_input_set_flags(input, HB_SUBSET_FLAGS_GLYPH_NAMES | + HB_SUBSET_FLAGS_NOTDEF_OUTLINE | + (retainGids ? HB_SUBSET_FLAGS_RETAIN_GIDS : 0)); if (hb_face_t* subset = hb_subset_or_fail(face, input)) { hb_blob_t* result = hb_face_reference_blob(subset); unsigned length = 0; @@ -1953,9 +1958,9 @@ void ensureMicrosoftUnicodeCmap(std::string& sfnt) { } QPDFObjectHandle makeProgramStream(QPDF& qpdf, FontProgram const& p, ProgramKind kind, bool cid, - std::string const& subset) { + std::string const& subset, bool normalizeCmap = false) { std::string bytes = subset.empty() ? p.bytes : subset; - if (kind == ProgramKind::TrueType) ensureMicrosoftUnicodeCmap(bytes); + if (normalizeCmap && kind == ProgramKind::TrueType) ensureMicrosoftUnicodeCmap(bytes); auto stream = QPDFObjectHandle::newStream(&qpdf, bytes); auto dict = stream.getDict(); if (kind == ProgramKind::TrueType) @@ -2012,6 +2017,20 @@ SimpleEncoding parseEncoding(QPDFObjectHandle const& font) { return enc; } +bool hasKnownSimpleEncoding(QPDFObjectHandle const& font) { + auto encoding = font.getKey("/Encoding"); + if (encoding.isNull()) return true; + auto known = [](QPDFObjectHandle const& name) { + return name.isNameAndEquals("/WinAnsiEncoding") || + name.isNameAndEquals("/MacRomanEncoding") || + name.isNameAndEquals("/StandardEncoding"); + }; + if (encoding.isName()) return known(encoding); + if (!encoding.isDictionary()) return false; + auto base = encoding.getKey("/BaseEncoding"); + return base.isNull() || known(base); +} + FT_UInt glyphForCode(FT_Face face, int code, bool symbolic, SimpleEncoding const& enc) { if (auto it = enc.differences.find(code); it != enc.differences.end()) { FT_UInt gid = it->second == ".notdef" ? 0 : glyphForName(face, it->second); @@ -2676,16 +2695,20 @@ struct ContentLexer { } void skipInlineImage() { - // BI ... ID EI. Image data may abut EI with no separating - // whitespace, and EI may be followed by a delimiter instead of - // whitespace, so validate only the trailing boundary. + // ISO 32000-1 7.2.2: EI is a token, so it must be separated from the + // image bytes by whitespace and from the next token by whitespace or a + // delimiter. Requiring both boundaries avoids treating a binary "EI" + // sequence inside the image data as the end marker. A stream that + // violates the delimiter rule is reported malformed so callers fail + // closed instead of trusting a partial scan. ContentToken token; while (next(token)) { if (token.word && token.text == "ID") { if (pos < data.size() && isPdfWhitespace(static_cast(data[pos]))) ++pos; while (pos + 1 < data.size()) { - if (data[pos] == 'E' && data[pos + 1] == 'I') { + if (data[pos] == 'E' && data[pos + 1] == 'I' && + (pos == 0 || isPdfWhitespace(static_cast(data[pos - 1])))) { const size_t after = pos + 2; if (after >= data.size() || isPdfWhitespace(static_cast(data[after])) || @@ -3169,6 +3192,30 @@ void updateCidSet(QPDF& qpdf, QPDFObjectHandle descriptor, std::set co descriptor.replaceKey("/CIDSet", QPDFObjectHandle::newStream(&qpdf, bits)); } +// CIDs whose glyph index survived a GID-retaining subset. CIDToGIDMap may +// alias several CIDs to one retained glyph, so mapping the retained GIDs back +// through the installed map is required; the drawn CID set alone is too small. +std::set cidsForRetainedGlyphs(QPDFObjectHandle const& target, + std::string const& mapBytes, + std::set const& glyphs) { + std::set cids{0}; + auto map = target.getKey("/CIDToGIDMap"); + if (map.isStream() && !mapBytes.empty()) { + const auto* bytes = reinterpret_cast(mapBytes.data()); + const size_t entries = mapBytes.size() / 2; + for (size_t c = 0; c < entries && c <= kMaxCid; ++c) { + const uint32_t gid = (static_cast(bytes[2 * c]) << 8) | bytes[2 * c + 1]; + if (glyphs.contains(static_cast(gid))) cids.insert(static_cast(c)); + } + return cids; + } + if (map.isNull() || map.isNameAndEquals("/Identity")) { + for (FT_UInt gid : glyphs) + if (gid <= kMaxCid) cids.insert(static_cast(gid)); + } + return cids; +} + // CIDs that exist in the embedded CID font program. PDF/A-2 6.2.11.4 test 4 // requires the CIDSet to describe every one, including CIDs mapped to GID 0; // describing only the CIDs drawn by content leaves the bitmap too short. @@ -3238,7 +3285,7 @@ bool subsetKeepsAdvances(FT_Library lib, std::string const& subsetBytes, FT_Face bool subsetKeepsSimpleGlyphs(FT_Library lib, std::string const& subsetBytes, FT_Face original, bool symbolic, SimpleEncoding const& enc, - std::set const& codes) { + std::set const& codes, bool retainGids) { FT_Face face = nullptr; if (subsetBytes.empty() || FT_New_Memory_Face(lib, reinterpret_cast(subsetBytes.data()), @@ -3247,8 +3294,35 @@ bool subsetKeepsSimpleGlyphs(FT_Library lib, std::string const& subsetBytes, FT_ bool same = true; for (uint32_t code : codes) { if (!same || code >= 256) continue; - same = glyphForCode(face, static_cast(code), symbolic, enc) == - glyphForCode(original, static_cast(code), symbolic, enc); + FT_UInt before = glyphForCode(original, static_cast(code), symbolic, enc); + FT_UInt after = glyphForCode(face, static_cast(code), symbolic, enc); + if (retainGids) { + same = before == after; + } else if (before == 0 || after == 0) { + same = before == after; + } else { + // Without RETAIN_GIDS the outline indices are renumbered. The PDF + // encoding reaches glyphs by name (or Unicode cmap), so compare the + // resolved name rather than the index. + char beforeName[256] = ""; + char afterName[256] = ""; + if (FT_HAS_GLYPH_NAMES(original) && FT_HAS_GLYPH_NAMES(face) && + FT_Get_Glyph_Name(original, before, beforeName, sizeof(beforeName)) == 0 && + FT_Get_Glyph_Name(face, after, afterName, sizeof(afterName)) == 0) { + same = std::strcmp(beforeName, afterName) == 0; + } else { + same = false; + } + } + if (!same) continue; + FT_Fixed beforeAdvance = 0; + FT_Fixed afterAdvance = 0; + if (before && after && + (FT_Get_Advance(original, before, FT_LOAD_NO_SCALE, &beforeAdvance) || + FT_Get_Advance(face, after, FT_LOAD_NO_SCALE, &afterAdvance) || + beforeAdvance != afterAdvance)) { + same = false; + } } FT_Done_Face(face); return same; @@ -3283,7 +3357,6 @@ bool subsetEmbeddedCidFont(QPDF& qpdf, FT_Library lib, QPDFObjectHandle& font, } std::set targetGlyphs{0}; - std::set activeCids{0}; for (uint32_t c : scan.cids) { uint32_t gid = c; if (!identity) { @@ -3293,7 +3366,6 @@ bool subsetEmbeddedCidFont(QPDF& qpdf, FT_Library lib, QPDFObjectHandle& font, } if (gid >= static_cast(program.face->num_glyphs)) return false; targetGlyphs.insert(gid); - activeCids.insert(c); } if (targetGlyphs.size() >= static_cast(program.face->num_glyphs)) return false; @@ -3311,7 +3383,16 @@ bool subsetEmbeddedCidFont(QPDF& qpdf, FT_Library lib, QPDFObjectHandle& font, for (char const* key : {"/FontFile", "/FontFile2", "/FontFile3"}) descriptor.removeKey(key); descriptor.replaceKey(program.kind == ProgramKind::TrueType ? "/FontFile2" : "/FontFile3", makeProgramStream(qpdf, program, program.kind, true, subset)); - if (descriptor.hasKey("/CIDSet")) updateCidSet(qpdf, descriptor, activeCids); + if (descriptor.hasKey("/CIDSet")) { + std::string map_view; + if (!identity && mapBytes) { + size_t map_size = + std::min(mapBytes->getSize(), 2 * (static_cast(kMaxCid) + 1)); + map_view.assign(reinterpret_cast(mapBytes->getBuffer()), map_size); + } + auto retainedCids = cidsForRetainedGlyphs(target, map_view, targetGlyphs); + if (!retainedCids.empty()) updateCidSet(qpdf, descriptor, retainedCids); + } return true; } @@ -3324,6 +3405,10 @@ bool subsetEmbeddedSimpleFont(QPDF& qpdf, FT_Library lib, QPDFObjectHandle& font auto scan = usedCidsForFont(qpdf, font, false); if (!scan.complete || scan.cids.empty()) return false; + // An unknown named /Encoding cannot be resolved to glyph names. Subsetting + // from the wrong code map would drop the glyphs the renderer actually uses. + if (!hasKnownSimpleEncoding(target)) return false; + auto flags = descriptor.getKey("/Flags"); bool symbolic = flags.isInteger() ? (flags.getIntValue() & 4) != 0 : baseName == "Symbol" || baseName == "ZapfDingbats"; @@ -3347,9 +3432,13 @@ bool subsetEmbeddedSimpleFont(QPDF& qpdf, FT_Library lib, QPDFObjectHandle& font if (targetGlyphs.size() >= static_cast(program.face->num_glyphs)) return false; // Build and verify the subset program before installing it into the descriptor. - std::string subset = subsetProgram(program, targetGlyphs); + // Symbolic fonts keep their glyph indices because PDFium resolves their + // built-in encodings through the program's cmap/GID table. + bool retainGids = symbolic; + std::string subset = subsetProgram(program, targetGlyphs, retainGids); if (subset.empty() || subset.size() + 64 >= program.bytes.size()) return false; - if (!subsetKeepsSimpleGlyphs(lib, subset, program.face, symbolic, enc, scan.cids)) return false; + if (!subsetKeepsSimpleGlyphs(lib, subset, program.face, symbolic, enc, scan.cids, retainGids)) + return false; std::string tag = makeSubsetTag(baseName, targetGlyphs); auto tagged = QPDFObjectHandle::newName("/" + tag + "+" + baseName); @@ -3779,6 +3868,23 @@ EmbedFontsResult embedFonts(std::function const& load, targetName.find("Dingbats") != std::string::npos); bool symbolic = !hasEncoding && (isSymbolName || (flags.isInteger() && (flags.getIntValue() & 4) != 0)); + if (!cid && !hasProgram && onlyMissing) { + // A substitute must cover every character the content actually + // draws. Installing a program with missing glyphs silently + // turns text into .notdef and can truncate extraction. + auto scan = usedCidsForFont(*qpdf, font, false); + if (!scan.complete) continue; + auto enc = parseEncoding(target); + bool covered = true; + for (uint32_t code : scan.cids) { + if (code >= 256) continue; + if (glyphForCode(program.face, static_cast(code), symbolic, enc) == 0) { + covered = false; + break; + } + } + if (!covered) continue; + } if (!cid && !target.getKey("/Widths").isArray() && !addWidths(target, program.face, symbolic)) { continue; @@ -3789,7 +3895,7 @@ EmbedFontsResult embedFonts(std::function const& load, descriptor = newDescriptor(*qpdf, target, program.face, symbolic); target.replaceKey("/FontDescriptor", descriptor); } - if (descriptor.isDictionary() && descriptor.hasKey("/Flags") && + if (onlyMissing && descriptor.isDictionary() && descriptor.hasKey("/Flags") && descriptor.getKey("/Flags").isInteger()) { long long f = descriptor.getKey("/Flags").getIntValue(); if (symbolic) { @@ -3801,25 +3907,12 @@ EmbedFontsResult embedFonts(std::function const& load, } std::set glyphs; std::string subset; - if (!hasProgram && !cid && !symbolic && !program.noSubsetting && - program.kind != ProgramKind::BareCff && program.kind != ProgramKind::Type1) { - auto enc = parseEncoding(target); - bool resolvable = std::ranges::all_of(enc.differences, [&](auto const& entry) { - return entry.second == ".notdef" || glyphForName(program.face, entry.second); - }); - for (int code = 0; resolvable && code < 256; ++code) { - glyphs.insert(glyphForCode(program.face, code, false, enc)); - glyphs.insert(glyphForCode(program.face, code, true, enc)); - if (FT_Select_Charmap(program.face, FT_ENCODING_UNICODE)) continue; - for (auto base : - {BaseEncoding::Standard, BaseEncoding::WinAnsi, BaseEncoding::MacRoman}) - glyphs.insert( - FT_Get_Char_Index(program.face, baseEncodingUnicode(base, code))); - } - if (resolvable) subset = subsetProgram(program, glyphs); - if (!reachesSameGlyphs(lib, subset, program.face, enc)) subset.clear(); - if (subset.empty()) glyphs.clear(); - } + // Missing simple fonts are embedded whole. Subsetting a + // replacement is an optimization, not part of the repair contract: + // a scanner that misses one code would drop a glyph and truncate + // text extraction (sample_fonts_solidconvertor.pdf). Compression + // has its own opt-in compact subsetter for fonts that are already + // embedded and whose encoding is known. std::string current = subtype.isName() ? subtype.getName() : ""; auto wrapper = descriptor.getKey("/FontFile3"); bool wrapped = wrapper.isStream() && @@ -3848,8 +3941,10 @@ EmbedFontsResult embedFonts(std::function const& load, } if (!cff.empty()) { if (cid) { - kind = ProgramKind::BareCff; - subset = std::move(cff); + // Keep the OpenType wrapper for CID-keyed CFF. Converting + // it to a bare /CIDFontType0C stream changes glyph + // rasterization in PDFium (pdfjs-issue5334, 738 pixels). + // The writer below bumps the PDF version to 1.6 instead. } else { auto enc = parseEncoding(target); if (reachesSameGlyphs(lib, cff, program.face, enc, symbolic)) { @@ -3914,7 +4009,7 @@ EmbedFontsResult embedFonts(std::function const& load, } auto& stream = program.streams[{kind, cid, glyphs}]; if (!stream.isInitialized()) - stream = makeProgramStream(*qpdf, program, kind, cid, subset); + stream = makeProgramStream(*qpdf, program, kind, cid, subset, onlyMissing); if (!glyphs.empty() && !(targetName.size() > 7 && targetName[6] == '+')) { size_t hash = std::hash{}(targetName); for (FT_UInt g : glyphs) hash = hash * 31 + g;