diff --git a/CHANGELOG.md b/CHANGELOG.md index c04bcb450..217fc877c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -164,6 +164,20 @@ publishing workflows, not library releases, so they are not listed here. exactly like `font.ttf`, and the scan no longer allocates a regex per file name. +- qpdf font embedding, subsetting and the PDF/X writer compress their output + at zlib level 9. The level is process-global state in qpdf, so those passes + hold the exclusive writer lock for the whole write: other threads' merge, + split or optimize work in the same process waits for a font pass instead of + interleaving with it. + +- Measured end to end on one arm64 driver (7 repetitions of identical + commands) across the unreleased rework: embedding the 7-font TAMReview + sample costs p50 60→129 ms and repairing a damaged 3.1 MB document p50 + 84→121 ms, while compression presets are unchanged (185→184 ms and 82→81 ms + p50). The embed pass now builds a document-wide font inventory, normalizes + and verifies; the numbers are recorded so later changes can tell + optimization from regression. + ### Fixed - The bundled `JPDFiumCompactCmyk.icc` fallback is now generated only from the 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 d464c121b..27e88c56b 100644 --- a/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontEmbedder.java +++ b/jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfFontEmbedder.java @@ -7,7 +7,9 @@ import java.io.StringReader; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; +import java.nio.charset.CharacterCodingException; import java.nio.charset.Charset; +import java.nio.charset.CodingErrorAction; import java.nio.charset.StandardCharsets; import java.nio.file.FileVisitResult; import java.nio.file.Files; @@ -277,19 +279,22 @@ public static Result embedMissing(byte[] pdf, Path... fontDirs) throws IOExcepti if (face != null && !face.embeddable()) face = null; if (face == null) { if (index == null) index = indexFor(fontDirs); - face = index.get(key); - if (face != null && !face.embeddable()) face = null; + for (String candidate : nameKeys(name)) { + face = index.get(candidate); + if (face != null && face.embeddable()) break; + face = null; + } } if (face == null && bundled != null) { - face = resolve(bundled, key); + face = resolveName(bundled, name); } if (face == null) { if (index == null) index = indexFor(fontDirs); - face = resolve(index, key); + face = resolveName(index, name); } if (face == null) { if (index == null) index = indexFor(fontDirs); - Face restricted = restricted(index, key); + Face restricted = restrictedName(index, name); statuses.add(restricted == null ? new FontStatus(name, "", -1, false, "no matching font") : new FontStatus(name, source(restricted), restricted.fsType(), false, @@ -336,18 +341,24 @@ public static Result embedMissing(byte[] pdf, Path... fontDirs) throws IOExcepti * predefined CMap whose code-to-glyph mapping cannot be preserved */ public static Result embedMissingStrict(byte[] pdf, Path... fontDirs) throws IOException { + if (!QpdfLib.isFontInventorySupported()) { + throw new JPDFiumException( + "strict font embedding requires the native font inventory"); + } return embedMissing(pdf, fontDirs).requireAllEmbedded(); } /** * The native bridge can rewrite {@code /CIDToGIDMap} and {@code /ToUnicode} for * Identity-encoded CID fonts and for predefined CJK CMaps (Adobe-Japan1 {@code 90ms-RKSJ-H}, - * Adobe-Korea1 {@code KSC-EUC-H}, Adobe-GB1 {@code GB-EUC-H}), whose code-to-CID and + * {@code 90ms-RKSJ-V}, {@code 90msp-RKSJ-H}, {@code 90msp-RKSJ-V}, Adobe-Korea1 {@code KSC-EUC-H}, + * {@code KSC-EUC-V}, Adobe-GB1 {@code GB-EUC-H}, {@code GB-EUC-V}), whose code-to-CID and * CID-to-Unicode mappings are bundled. Other predefined CMaps or an existing CID-to-GID stream carry an ordering that a substitute program does not * share; embedding them would keep the document renderable but change its text. */ private static String substitutionBlocker(PdfFontAuditor.FontResource resource) { - if (!resource.hasToUnicode() && !isAscii(resource.baseName())) { + boolean supportedCjk = isSupportedCjkPredefinedCMap(resource.encoding()); + if (!resource.hasToUnicode() && !isAscii(resource.baseName()) && !supportedCjk) { // Legacy CJK producers put GBK/Shift-JIS bytes in /BaseFont and rely on the // original font's glyph order. A substitute would change both text extraction // and the drawn glyphs because there is no /ToUnicode to preserve. @@ -378,9 +389,7 @@ private static String substitutionBlocker(PdfFontAuditor.FontResource resource) } boolean identityEncoding = "Identity-H".equals(resource.encoding()) || "Identity-V".equals(resource.encoding()) - || "90ms-RKSJ-H".equals(resource.encoding()) - || "KSC-EUC-H".equals(resource.encoding()) - || "GB-EUC-H".equals(resource.encoding()); + || supportedCjk; if (!identityEncoding) { return "CID encoding: predefined CMap " + resource.encoding() + " cannot be rewritten without a verified code-to-glyph mapping"; @@ -394,6 +403,17 @@ private static String substitutionBlocker(PdfFontAuditor.FontResource resource) return null; } + private static boolean isSupportedCjkPredefinedCMap(String encoding) { + return "90ms-RKSJ-H".equals(encoding) + || "90ms-RKSJ-V".equals(encoding) + || "90msp-RKSJ-H".equals(encoding) + || "90msp-RKSJ-V".equals(encoding) + || "KSC-EUC-H".equals(encoding) + || "KSC-EUC-V".equals(encoding) + || "GB-EUC-H".equals(encoding) + || "GB-EUC-V".equals(encoding); + } + private static boolean isAscii(String value) { for (int i = 0; i < value.length(); i++) { if (value.charAt(i) > 0x7F) return false; @@ -426,11 +446,11 @@ public static FontStatus diagnose(String baseName, Path... fontDirs) { "available"); } } - face = resolve(index, key); + face = resolveName(index, baseName); if (face != null) { return new FontStatus(baseName, source(face), face.fsType(), true, "available"); } - Face restricted = restricted(index, key); + Face restricted = restrictedName(index, baseName); return restricted == null ? new FontStatus(baseName, "", -1, false, "no matching font") : new FontStatus(baseName, source(restricted), restricted.fsType(), false, @@ -487,7 +507,7 @@ private static Map programsFor( Map operationPrograms = new HashMap<>(); for (String name : missing) { if (blockers.containsKey(name)) continue; - Face face = resolve(index, key(name)); + Face face = resolveName(index, name); if (face == null) continue; byte[] data = program(face, operationPrograms); if (!embeddableProgram(programFsType(face, data))) continue; @@ -506,6 +526,43 @@ public static Result embedMissing(Path input, Path... fontDirs) throws IOExcepti return embedMissing(Files.readAllBytes(input), fontDirs); } + private static Face resolveName(Map index, String name) { + for (String candidate : nameKeys(name)) { + Face face = resolve(index, candidate); + if (face != null) return face; + } + return null; + } + + private static Face restrictedName(Map index, String name) { + for (String candidate : nameKeys(name)) { + Face face = restricted(index, candidate); + if (face != null) return face; + } + return null; + } + + static List nameKeys(String name) { + List keys = new ArrayList<>(2); + String direct = key(name); + if (!direct.isEmpty()) keys.add(direct); + boolean latin1 = !name.isEmpty() && name.chars().allMatch(c -> c <= 0xFF); + if (latin1) { + try { + String decoded = StandardCharsets.UTF_8.newDecoder() + .onMalformedInput(CodingErrorAction.REPORT) + .onUnmappableCharacter(CodingErrorAction.REPORT) + .decode(ByteBuffer.wrap(name.getBytes(StandardCharsets.ISO_8859_1))) + .toString(); + String decodedKey = key(decoded); + if (!decodedKey.isEmpty() && !keys.contains(decodedKey)) keys.add(decodedKey); + } catch (CharacterCodingException ignored) { + // Not a UTF-8 byte sequence after the Latin-1 round trip. + } + } + return keys; + } + static String key(String name) { int start = 0; if (name.length() > 7 && name.charAt(6) == '+') { @@ -983,6 +1040,8 @@ private static byte[] sanitizeSymbolicTrueType(byte[] fontData) { if (subRec + 8 > cmapOffset + cmapLength) break; int platformId = buf.getShort(subRec) & 0xFFFF; int encodingId = buf.getShort(subRec + 2) & 0xFFFF; + int subtableOffset = buf.getInt(subRec + 4); + if (subtableOffset < 0 || subtableOffset > cmapLength - 4) continue; if (platformId == 3 && encodingId == 0) { return fontData; } @@ -996,6 +1055,8 @@ private static byte[] sanitizeSymbolicTrueType(byte[] fontData) { selectedSubtableOffset = cmapOffset + 4; } if (selectedSubtableOffset + 8 > fontData.length) return fontData; + int selectedOffset = buf.getInt(selectedSubtableOffset + 4); + if (selectedOffset < 0 || selectedOffset > cmapLength - 4) return fontData; byte[] modified = fontData.clone(); ByteBuffer out = ByteBuffer.wrap(modified); diff --git a/jpdfium/src/main/java/stirling/software/jpdfium/text/PdfTextSearcher.java b/jpdfium/src/main/java/stirling/software/jpdfium/text/PdfTextSearcher.java index 9f54731ae..50d1c3598 100644 --- a/jpdfium/src/main/java/stirling/software/jpdfium/text/PdfTextSearcher.java +++ b/jpdfium/src/main/java/stirling/software/jpdfium/text/PdfTextSearcher.java @@ -119,13 +119,15 @@ private static int parseIntInPlace(String s, int start, int end) { i++; } if (i >= end) throw new NumberFormatException("empty number"); - int result = 0; + long limit = negative ? 2_147_483_648L : 2_147_483_647L; + long result = 0; for (; i < end; i++) { char c = s.charAt(i); if (c < '0' || c > '9') throw new NumberFormatException("not a digit"); result = result * 10 + (c - '0'); + if (result > limit) throw new NumberFormatException("integer overflow"); } - return negative ? -result : result; + return (int) (negative ? -result : result); } /** diff --git a/jpdfium/src/main/java/stirling/software/jpdfium/util/NativeJsonParser.java b/jpdfium/src/main/java/stirling/software/jpdfium/util/NativeJsonParser.java index d8ed6ceaa..be7cd0b57 100644 --- a/jpdfium/src/main/java/stirling/software/jpdfium/util/NativeJsonParser.java +++ b/jpdfium/src/main/java/stirling/software/jpdfium/util/NativeJsonParser.java @@ -83,17 +83,19 @@ public static int intField(String json, String key) { int length = json.length(); boolean negative = json.charAt(index) == '-'; if (negative) index++; - int value = 0; + long limit = negative ? 2_147_483_648L : 2_147_483_647L; + long value = 0; boolean digits = false; while (index < length) { char c = json.charAt(index); if (c < '0' || c > '9') break; value = value * 10 + (c - '0'); + if (value > limit) throw new NumberFormatException("integer overflow"); digits = true; index++; } if (!digits) return 0; - return negative ? -value : value; + return (int) (negative ? -value : value); } /** Extract a long field from a single JSON object string. Returns 0 if missing. */ @@ -108,12 +110,21 @@ public static long longField(String json, String key) { while (index < length) { char c = json.charAt(index); if (c < '0' || c > '9') break; - value = value * 10L + (c - '0'); + int digit = c - '0'; + if (negative) { + if (value < (Long.MIN_VALUE + digit) / 10) + throw new NumberFormatException("long overflow"); + value = value * 10L - digit; + } else { + if (value > (Long.MAX_VALUE - digit) / 10) + throw new NumberFormatException("long overflow"); + value = value * 10L + digit; + } digits = true; index++; } if (!digits) return 0L; - return negative ? -value : value; + return value; } /** Extract a boolean field from a single JSON object string. Returns false if missing. */ 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 a6bbb46ad..ad30733b3 100644 --- a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderRoundTripTest.java +++ b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderRoundTripTest.java @@ -401,9 +401,10 @@ private static void assertSameOrResolvedText( i++; } } - while (matched < a.length() + while (matched < a.length() && i < b.length() && (a.charAt(matched) == '?' || a.charAt(matched) == '\uFFFD')) { matched++; + i++; } assertEquals(a.length(), matched, where + " lost resolved text: " + a + " vs " + b); } 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 6b27660f1..c8dcb478a 100644 --- a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderTest.java +++ b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontEmbedderTest.java @@ -127,6 +127,153 @@ void embedsJapan1RksjCidFontWithAUnicodeFallback() throws Exception { } } + @Test + void embedsJapan1ProportionalRksjCidFont() throws Exception { + assumeTrue(PdfFontEmbedder.isAvailable(), "native qpdf font embedding unavailable"); + var url = getClass().getResource("/pdfs/general/90ms_rksj_h_sample.pdf"); + assertNotNull(url, "90ms-RKSJ-H fixture missing"); + byte[] input = Files.readAllBytes(Path.of(url.toURI())); + byte[] proportional = withCidEncoding(input, "90ms-RKSJ-H", "90msp-RKSJ-H"); + + PdfFontEmbedder.Result result = PdfFontEmbedder.embedMissing(proportional); + + assertTrue(result.embedded().contains("HeiseiMin-W3"), + "the proportional predefined CMap must embed, got " + result); + try (PDDocument doc = Loader.loadPDF(result.pdf())) { + assertTrue(new PDFTextStripper().getText(doc).contains("日本語テスト"), + "90msp-RKSJ-H must preserve Japanese text"); + } + + byte[] ascii = withPageContent(proportional, + "BT /F1 14 Tf 72 700 Td (A) Tj ET"); + PdfFontEmbedder.Result asciiResult = PdfFontEmbedder.embedMissing(ascii); + try (PDDocument doc = Loader.loadPDF(asciiResult.pdf())) { + assertEquals("A", new PDFTextStripper().getText(doc).strip(), + "90msp-RKSJ-H ASCII must map through CID 34"); + COSDictionary font = doc.getPage(0).getResources() + .getFont(COSName.getPDFName("F1")).getCOSObject(); + COSArray descendants = (COSArray) font.getDictionaryObject(COSName.DESCENDANT_FONTS); + COSDictionary descendant = (COSDictionary) descendants.getObject(0); + COSBase mapping = descendant.getDictionaryObject(COSName.CID_TO_GID_MAP); + assertTrue(mapping instanceof COSStream, "ASCII mapping needs a CIDToGIDMap stream"); + byte[] bytes; + try (var in = ((COSStream) mapping).createInputStream()) { + bytes = in.readAllBytes(); + } + int gid = ((bytes[34 * 2] & 0xFF) << 8) | (bytes[34 * 2 + 1] & 0xFF); + assertTrue(gid > 0, "90msp-RKSJ-H code A must map to the glyph at CID 34"); + } + } + + @Test + void embedsJapan1VerticalRksjCidFont() throws Exception { + assumeTrue(PdfFontEmbedder.isAvailable(), "native qpdf font embedding unavailable"); + var url = getClass().getResource("/pdfs/general/90ms_rksj_h_sample.pdf"); + assertNotNull(url, "90ms-RKSJ-H fixture missing"); + byte[] input = Files.readAllBytes(Path.of(url.toURI())); + for (String verticalCMap : List.of("90ms-RKSJ-V", "90msp-RKSJ-V")) { + byte[] vertical = withCidEncoding(input, "90ms-RKSJ-H", verticalCMap); + PdfFontEmbedder.Result result = PdfFontEmbedder.embedMissing(vertical); + assertTrue(result.embedded().contains("HeiseiMin-W3"), + verticalCMap + " must embed, got " + result); + try (PDDocument doc = Loader.loadPDF(result.pdf())) { + String text = new PDFTextStripper().getText(doc).replaceAll("\\s+", ""); + assertTrue(text.contains("日本語テスト"), + verticalCMap + " must preserve Japanese text"); + } + } + } + + @Test + void embedsVerticalKoreaAndGbPredefinedCMaps() throws Exception { + assumeTrue(PdfFontEmbedder.isAvailable(), "native qpdf font embedding unavailable"); + var gbUrl = getClass().getResource("/pdfs/general/gb_euc_h_sample.pdf"); + assertNotNull(gbUrl, "GB-EUC-H fixture missing"); + byte[] gbInput = Files.readAllBytes(Path.of(gbUrl.toURI())); + byte[] gbVertical = withCidEncoding(gbInput, "GB-EUC-H", "GB-EUC-V"); + PdfFontEmbedder.Result gbResult = PdfFontEmbedder.embedMissing(gbVertical); + assertTrue(gbResult.embedded().contains("SimSun"), + "GB-EUC-V must embed, got " + gbResult); + try (PDDocument doc = Loader.loadPDF(gbResult.pdf())) { + String text = new PDFTextStripper().getText(doc).replaceAll("\\s+", ""); + assertTrue(text.contains("中文测试"), + "GB-EUC-V must preserve Chinese text"); + } + + assumeTrue(PdfFontEmbedder.diagnose("Batang").resolved(), + "no Korean-capable substitute installed"); + var kscUrl = getClass().getResource("/pdfs/general/ksc_euc_h_sample.pdf"); + assertNotNull(kscUrl, "KSC-EUC-H fixture missing"); + byte[] kscInput = Files.readAllBytes(Path.of(kscUrl.toURI())); + byte[] kscVertical = withCidEncoding(kscInput, "KSC-EUC-H", "KSC-EUC-V"); + PdfFontEmbedder.Result kscResult = PdfFontEmbedder.embedMissing(kscVertical); + assertTrue(kscResult.embedded().contains("Batang"), + "KSC-EUC-V must embed, got " + kscResult); + try (PDDocument doc = Loader.loadPDF(kscResult.pdf())) { + String text = new PDFTextStripper().getText(doc).replaceAll("\\s+", ""); + assertTrue(text.contains("한국어테스트"), + "KSC-EUC-V must preserve Korean text"); + } + } + + @Test + void embedsJapan1RksjWithNonAsciiBaseFontWithoutToUnicode() throws Exception { + assumeTrue(PdfFontEmbedder.isAvailable(), "native qpdf font embedding unavailable"); + var url = getClass().getResource("/pdfs/general/90ms_rksj_h_sample.pdf"); + assertNotNull(url, "90ms-RKSJ-H fixture missing"); + byte[] input = Files.readAllBytes(Path.of(url.toURI())); + byte[] modified; + try (PDDocument doc = Loader.loadPDF(input)) { + for (PDPage page : doc.getPages()) { + for (COSName name : page.getResources().getFontNames()) { + COSDictionary font = page.getResources().getFont(name).getCOSObject(); + COSArray descendants = (COSArray) font.getDictionaryObject(COSName.DESCENDANT_FONTS); + if (descendants != null && descendants.size() > 0) { + font.removeItem(COSName.TO_UNICODE); + font.setItem(COSName.BASE_FONT, COSName.getPDFName("\uFF2D\uFF33 \u30B4\u30B7\u3083\u30AF")); + COSDictionary desc = (COSDictionary) descendants.getObject(0); + desc.removeItem(COSName.TO_UNICODE); + desc.setItem(COSName.BASE_FONT, COSName.getPDFName("\uFF2D\uFF33 \u30B4\u30B7\u3083\u30AF")); + } + } + } + ByteArrayOutputStream out = new ByteArrayOutputStream(); + doc.save(out); + modified = out.toByteArray(); + } + + PdfFontEmbedder.Result result = PdfFontEmbedder.embedMissing(modified); + assertFalse(result.statuses().stream().anyMatch(s -> s.reason().contains("legacy non-ASCII")), + "bundled CJK CMap must not be blocked by non-ASCII BaseFont check: " + result); + } + + private static byte[] withPageContent(byte[] pdf, String content) throws IOException { + try (PDDocument doc = Loader.loadPDF(pdf)) { + doc.getPage(0).setContents(new PDStream(doc, + new ByteArrayInputStream(content.getBytes(StandardCharsets.US_ASCII)))); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + doc.save(out); + return out.toByteArray(); + } + } + + private static byte[] withCidEncoding(byte[] pdf, String from, String to) throws IOException { + try (PDDocument doc = Loader.loadPDF(pdf)) { + for (PDPage page : doc.getPages()) { + for (COSName name : page.getResources().getFontNames()) { + COSDictionary font = page.getResources().getFont(name).getCOSObject(); + COSBase encoding = font.getDictionaryObject(COSName.ENCODING); + if (encoding instanceof COSName value && value.getName().equals(from)) { + font.setItem(COSName.ENCODING, COSName.getPDFName(to)); + } + } + } + ByteArrayOutputStream out = new ByteArrayOutputStream(); + doc.save(out); + return out.toByteArray(); + } + } + @Test void embedsGb1GbeucCidFontWithAUnicodeFallback() throws Exception { assumeTrue(PdfFontEmbedder.isAvailable(), "native qpdf font embedding unavailable"); @@ -1553,6 +1700,12 @@ void missingIdentityCidFontRewritesWidthsFromSubstitute() throws Exception { } } + @Test + void utf8NameBytesProduceDecodedLookupKey() { + assertTrue(PdfFontEmbedder.nameKeys("Caf\u00c3\u00a9").contains("café"), + "UTF-8 name bytes must also resolve as decoded text"); + } + @Test void missingFontReportsNoCandidate() { PdfFontEmbedder.FontStatus status = PdfFontEmbedder.diagnose("DefinitelyNotAFontFamily"); diff --git a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontInventoryTest.java b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontInventoryTest.java index 6bfcc9aef..4f434da43 100644 --- a/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontInventoryTest.java +++ b/jpdfium/src/test/java/stirling/software/jpdfium/doc/PdfFontInventoryTest.java @@ -5,7 +5,10 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assumptions.assumeTrue; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.lang.foreign.MemorySegment; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.List; @@ -14,6 +17,13 @@ import java.util.function.Function; import java.util.stream.Collectors; +import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.pdmodel.PDPage; +import org.apache.pdfbox.pdmodel.PDResources; +import org.apache.pdfbox.pdmodel.common.PDRectangle; +import org.apache.pdfbox.pdmodel.common.PDStream; +import org.apache.pdfbox.cos.COSDictionary; +import org.apache.pdfbox.cos.COSName; import org.junit.jupiter.api.Test; import stirling.software.jpdfium.PdfDocument; @@ -87,6 +97,32 @@ void nonAsciiBaseFontIsValidUtf8() throws Exception { assertFalse(fonts.keySet().stream().anyMatch(name -> name.indexOf('\uFFFD') >= 0)); } + @Test + void fontDictionaryWithoutTypeIsInventoried() throws Exception { + assumeTrue(PdfFontAuditor.inventoryAvailable(), "font inventory not in this native build"); + byte[] pdf; + try (PDDocument doc = new PDDocument()) { + PDPage page = new PDPage(PDRectangle.LETTER); + doc.addPage(page); + COSDictionary font = new COSDictionary(); + font.setName(COSName.SUBTYPE, "TrueType"); + font.setName(COSName.BASE_FONT, "ArialMT"); + 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(doc, new ByteArrayInputStream( + "BT /F1 12 Tf 72 700 Td (H) Tj ET".getBytes(StandardCharsets.US_ASCII)))); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + doc.save(out); + pdf = out.toByteArray(); + } + + assertTrue(byName(PdfFontAuditor.inventoryBytes(pdf)).containsKey("ArialMT"), + "a reachable font dictionary without /Type must stay in the inventory"); + } + @Test void validUtf8FontNameBytesStayReversible() throws Exception { Map fonts = byName(inventory("utf8-name-font.pdf")); diff --git a/jpdfium/src/test/java/stirling/software/jpdfium/text/PdfTextSearcherTest.java b/jpdfium/src/test/java/stirling/software/jpdfium/text/PdfTextSearcherTest.java index d196025eb..089aa1b02 100644 --- a/jpdfium/src/test/java/stirling/software/jpdfium/text/PdfTextSearcherTest.java +++ b/jpdfium/src/test/java/stirling/software/jpdfium/text/PdfTextSearcherTest.java @@ -36,6 +36,13 @@ void parseSingleMatch() { assertEquals(3, m.length()); } + @Test + void overflowedMatchesAreSkipped() { + List matches = PdfTextSearcher.parseMatchesJson( + "[{\"start\":2147483648,\"len\":1}]", 0); + assertTrue(matches.isEmpty()); + } + @Test void parseMultipleMatches() { String json = "[{\"start\":0,\"len\":4},{\"start\":10,\"len\":2}]"; diff --git a/jpdfium/src/test/java/stirling/software/jpdfium/util/NativeJsonParserTest.java b/jpdfium/src/test/java/stirling/software/jpdfium/util/NativeJsonParserTest.java index 5915275bb..ae1831068 100644 --- a/jpdfium/src/test/java/stirling/software/jpdfium/util/NativeJsonParserTest.java +++ b/jpdfium/src/test/java/stirling/software/jpdfium/util/NativeJsonParserTest.java @@ -4,6 +4,7 @@ import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -139,6 +140,18 @@ void ignoresIntKeyInsideStringValue() { 4, NativeJsonParser.intField("{\"note\":\"say \\\"count\\\":1\",\"count\":4}", "count")); } + @Test + void rejectsOverflowingIntegers() { + assertThrows(NumberFormatException.class, + () -> NativeJsonParser.intField("{\"c\":2147483648}", "c")); + assertThrows(NumberFormatException.class, + () -> NativeJsonParser.intField("{\"c\":-2147483649}", "c")); + assertThrows(NumberFormatException.class, + () -> NativeJsonParser.longField("{\"size\":9223372036854775808}", "size")); + assertThrows(NumberFormatException.class, + () -> NativeJsonParser.longField("{\"size\":-9223372036854775809}", "size")); + } + @Test void parsesLongLargerThanInt() { assertEquals(9_223_372_036_854_775_807L, NativeJsonParser.longField("{\"size\":9223372036854775807}", "size")); diff --git a/native/bridge/src/jpdfium_advanced.cpp b/native/bridge/src/jpdfium_advanced.cpp index 77db1419a..3d9fd8e6d 100644 --- a/native/bridge/src/jpdfium_advanced.cpp +++ b/native/bridge/src/jpdfium_advanced.cpp @@ -1862,14 +1862,131 @@ static FnCidScan fn_used_cids_for_font(QPDF& pdf, QPDFObjectHandle const& font, return result; } +static void fn_isolate_unicode_cmap(std::string& sfnt) { + if (sfnt.size() < 12) return; + auto read16 = [&](size_t off) -> uint16_t { + return (static_cast(sfnt[off]) << 8) | static_cast(sfnt[off + 1]); + }; + auto read32 = [&](size_t off) -> uint32_t { + return (static_cast(static_cast(sfnt[off])) << 24) | + (static_cast(static_cast(sfnt[off + 1])) << 16) | + (static_cast(static_cast(sfnt[off + 2])) << 8) | + static_cast(static_cast(sfnt[off + 3])); + }; + auto write16 = [&](size_t off, uint16_t v) { + sfnt[off] = static_cast((v >> 8) & 0xFF); + sfnt[off + 1] = static_cast(v & 0xFF); + }; + auto write32 = [&](size_t off, uint32_t v) { + sfnt[off] = static_cast((v >> 24) & 0xFF); + sfnt[off + 1] = static_cast((v >> 16) & 0xFF); + sfnt[off + 2] = static_cast((v >> 8) & 0xFF); + sfnt[off + 3] = static_cast(v & 0xFF); + }; + + uint16_t numTables = read16(4); + if (sfnt.size() < 12 + static_cast(numTables) * 16) return; + + size_t cmapEntryOff = 0; + uint32_t cmapOffset = 0; + uint32_t cmapLength = 0; + uint32_t headOffset = 0; + + for (size_t i = 0; i < numTables; ++i) { + size_t entry = 12 + i * 16; + if (std::memcmp(&sfnt[entry], "cmap", 4) == 0) { + cmapEntryOff = entry; + cmapOffset = read32(entry + 8); + cmapLength = read32(entry + 12); + } else if (std::memcmp(&sfnt[entry], "head", 4) == 0) { + headOffset = read32(entry + 8); + } + } + + if (!cmapEntryOff || cmapOffset > sfnt.size() || sfnt.size() - cmapOffset < 4 || + cmapLength > sfnt.size() - cmapOffset) + return; + + uint16_t numSubtables = read16(static_cast(cmapOffset) + 2); + if (numSubtables <= 1 || cmapLength < 4 || + static_cast(numSubtables) * 8 > cmapLength - 4) + return; + + int unicodeIndex = -1; + for (size_t i = 0; i < numSubtables; ++i) { + size_t subOff = static_cast(cmapOffset) + 4 + i * 8; + uint16_t plat = read16(subOff); + uint16_t enc = read16(subOff + 2); + if (plat == 3 && (enc == 1 || enc == 10)) { + unicodeIndex = static_cast(i); + break; + } + } + if (unicodeIndex < 0) { + for (size_t i = 0; i < numSubtables; ++i) { + size_t subOff = static_cast(cmapOffset) + 4 + i * 8; + uint16_t plat = read16(subOff); + uint16_t enc = read16(subOff + 2); + if (plat == 0 && (enc == 3 || enc == 4 || enc == 0)) { + unicodeIndex = static_cast(i); + break; + } + } + } + if (unicodeIndex < 0) return; + + if (unicodeIndex > 0) { + size_t srcOff = static_cast(cmapOffset) + 4 + unicodeIndex * 8; + size_t dstOff = static_cast(cmapOffset) + 4; + std::memmove(&sfnt[dstOff], &sfnt[srcOff], 8); + } + uint16_t plat = read16(static_cast(cmapOffset) + 4); + if (plat == 0) { + write16(static_cast(cmapOffset) + 4, 3); + write16(static_cast(cmapOffset) + 6, 1); + } + write16(static_cast(cmapOffset) + 2, 1); + + uint32_t cmapSum = 0; + for (size_t i = 0; i < cmapLength; i += 4) { + uint32_t word = 0; + for (size_t b = 0; b < 4 && i + b < cmapLength; ++b) { + word = + (word << 8) | static_cast(sfnt[static_cast(cmapOffset) + i + b]); + } + if (cmapLength - i < 4) { + word <<= (4 - (cmapLength - i)) * 8; + } + cmapSum += word; + } + write32(cmapEntryOff + 4, cmapSum); + + if (headOffset && headOffset <= sfnt.size() && sfnt.size() - headOffset >= 12) { + write32(static_cast(headOffset) + 8, 0); + uint32_t fontSum = 0; + for (size_t i = 0; i < sfnt.size(); i += 4) { + uint32_t word = 0; + for (size_t b = 0; b < 4 && i + b < sfnt.size(); ++b) { + word = (word << 8) | static_cast(sfnt[i + b]); + } + if (sfnt.size() - i < 4) { + word <<= (4 - (sfnt.size() - i)) * 8; + } + fontSum += word; + } + write32(static_cast(headOffset) + 8, 0xB1B0AFBA - fontSum); + } +} + static std::string fn_subset_sfnt(FT_Library lib, const uint8_t* data, size_t size, FT_Long face_index, std::set const& glyphs, FT_Face face) { (void)lib; if (!data || size == 0 || size > std::numeric_limits::max()) return {}; - hb_blob_t* blob = - hb_blob_create(reinterpret_cast(data), static_cast(size), - HB_MEMORY_MODE_READONLY, nullptr, nullptr); + std::string sfnt_copy(reinterpret_cast(data), size); + fn_isolate_unicode_cmap(sfnt_copy); + hb_blob_t* blob = hb_blob_create(sfnt_copy.data(), static_cast(sfnt_copy.size()), + HB_MEMORY_MODE_READONLY, nullptr, nullptr); hb_face_t* hb_face = hb_face_create(blob, static_cast(face_index)); hb_blob_destroy(blob); std::string out; @@ -1881,6 +1998,17 @@ static std::string fn_subset_sfnt(FT_Library lib, const uint8_t* data, size_t si u = FT_Get_Next_Char(face, u, &gid)) if (glyphs.contains(gid)) hb_set_add(hb_subset_input_unicode_set(input), u); } + hb_set_t* drop_tables = hb_subset_input_set(input, HB_SUBSET_SETS_DROP_TABLE_TAG); + for (hb_tag_t tag : + {HB_TAG('G', 'S', 'U', 'B'), HB_TAG('G', 'P', 'O', 'S'), HB_TAG('G', 'D', 'E', 'F'), + HB_TAG('B', 'A', 'S', 'E'), HB_TAG('J', 'S', 'T', 'F'), HB_TAG('M', 'A', 'T', 'H'), + HB_TAG('E', 'B', 'D', 'T'), HB_TAG('E', 'B', 'L', 'C'), HB_TAG('E', 'B', 'S', 'C'), + HB_TAG('C', 'B', 'D', 'T'), HB_TAG('C', 'B', 'L', 'C'), HB_TAG('s', 'b', 'i', 'x'), + HB_TAG('S', 'V', 'G', ' '), HB_TAG('h', 'd', 'm', 'x'), HB_TAG('V', 'D', 'M', 'X'), + HB_TAG('L', 'T', 'S', 'H'), HB_TAG('P', 'C', 'L', 'T'), HB_TAG('D', 'S', 'I', 'G'), + HB_TAG('k', 'e', 'r', 'n'), HB_TAG('m', 'e', 't', 'a'), HB_TAG('S', 'T', 'A', 'T')}) { + hb_set_add(drop_tables, tag); + } hb_subset_input_set_flags(input, HB_SUBSET_FLAGS_RETAIN_GIDS | HB_SUBSET_FLAGS_GLYPH_NAMES | HB_SUBSET_FLAGS_NOTDEF_OUTLINE); if (hb_face_t* subset = hb_subset_or_fail(hb_face, input)) { @@ -2134,31 +2262,47 @@ static void repair_simple_font_widths(QPDFObjectHandle& target, FT_Face face, } // Synthesize a missing /W array on a horizontal CIDFont from FreeType glyph advances. -static std::map parse_cid_widths(QPDFObjectHandle const& w) { +static std::map parse_cid_widths(QPDFObjectHandle const& w, bool& complete) { + complete = true; std::map widths; if (!w.isArray()) return widths; const auto items = w.getArrayAsVector(); size_t i = 0; while (i < items.size()) { - if (!items[i].isInteger()) break; + if (!items[i].isInteger()) { + complete = false; + break; + } const uint32_t first = static_cast(items[i].getIntValueAsInt()); ++i; - if (i >= items.size()) break; + if (i >= items.size()) { + complete = false; + break; + } if (items[i].isArray()) { const auto run = items[i].getArrayAsVector(); ++i; for (size_t j = 0; j < run.size() && first + j <= kFontMaxCid; ++j) { - if (run[j].isNumber()) + if (run[j].isNumber()) { widths[first + static_cast(j)] = static_cast(std::lround(run[j].getNumericValue())); + } else { + complete = false; + } } continue; } - if (i + 1 >= items.size() || !items[i].isInteger() || !items[i + 1].isNumber()) break; + if (i + 1 >= items.size() || !items[i].isInteger() || !items[i + 1].isNumber()) { + complete = false; + break; + } const uint32_t last = static_cast(items[i].getIntValueAsInt()); const int width = static_cast(std::lround(items[i + 1].getNumericValue())); i += 2; - if (last < first || last > kFontMaxCid) continue; + if (last < first || last > kFontMaxCid) { + complete = false; + continue; + } for (uint32_t cid = first; cid <= last; ++cid) widths[cid] = width; } return widths; @@ -2227,7 +2371,9 @@ static void repair_cid_font_widths(QPDF& pdf, QPDFObjectHandle& font, QPDFObject if (cid_gid.empty()) return; QPDFObjectHandle existing_w = target.getKey("/W"); - std::map widths = parse_cid_widths(existing_w); + bool widths_complete = true; + std::map widths = parse_cid_widths(existing_w, widths_complete); + if (existing_w.isArray() && !widths_complete) return; // 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 @@ -2749,7 +2895,7 @@ static void process_font(QPDF& pdf, QPDFObjectHandle font, bool do_fix_tounicode simple_glyphs, usage, state, &retained_glyphs); } if (cid && full) { - if (did_resubset) { + if (did_resubset && descriptor.hasKey("/CIDSet")) { // 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. @@ -2772,7 +2918,7 @@ static void process_font(QPDF& pdf, QPDFObjectHandle font, bool do_fix_tounicode descriptor.replaceKey("/CIDSet", QPDFObjectHandle::newStream(&pdf, bits)); state.changed = true; } - } else { + } else if (descriptor.hasKey("/CIDSet")) { repair_cidset_stream(pdf, descriptor, face.get(), cid_to_gid, cid_identity, gid_to_cid, state); } @@ -3183,7 +3329,7 @@ static std::vector qpdf_write(QPDF& pdf) { // PDF/A validators expect the endstream keyword on its own line. Write it // explicitly instead of relying on the qpdf default. writer.setNewlineBeforeEndstream(true); - jpdfium_qpdf_internal::WriterLock lock{-1}; + jpdfium_qpdf_internal::WriterLock lock{9}; writer.write(); std::shared_ptr buf = writer.getBufferSharedPointer(); std::vector result(buf->getSize()); diff --git a/native/bridge/src/jpdfium_qpdf.cpp b/native/bridge/src/jpdfium_qpdf.cpp index c489346d4..1d1b969c7 100644 --- a/native/bridge/src/jpdfium_qpdf.cpp +++ b/native/bridge/src/jpdfium_qpdf.cpp @@ -1829,10 +1829,14 @@ std::string extractCffTable(FT_Library lib, std::string const& sfntBytes) { return cff; } +static void ensureMicrosoftUnicodeCmap(std::string& sfnt); + 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()), + std::string sfntCopy = p.bytes; + ensureMicrosoftUnicodeCmap(sfntCopy); + hb_blob_t* blob = hb_blob_create(sfntCopy.data(), static_cast(sfntCopy.size()), HB_MEMORY_MODE_READONLY, nullptr, nullptr); hb_face_t* face = hb_face_create(blob, 0); hb_blob_destroy(blob); @@ -1845,6 +1849,17 @@ 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_set_t* drop_tables = hb_subset_input_set(input, HB_SUBSET_SETS_DROP_TABLE_TAG); + for (hb_tag_t tag : + {HB_TAG('G', 'S', 'U', 'B'), HB_TAG('G', 'P', 'O', 'S'), HB_TAG('G', 'D', 'E', 'F'), + HB_TAG('B', 'A', 'S', 'E'), HB_TAG('J', 'S', 'T', 'F'), HB_TAG('M', 'A', 'T', 'H'), + HB_TAG('E', 'B', 'D', 'T'), HB_TAG('E', 'B', 'L', 'C'), HB_TAG('E', 'B', 'S', 'C'), + HB_TAG('C', 'B', 'D', 'T'), HB_TAG('C', 'B', 'L', 'C'), HB_TAG('s', 'b', 'i', 'x'), + HB_TAG('S', 'V', 'G', ' '), HB_TAG('h', 'd', 'm', 'x'), HB_TAG('V', 'D', 'M', 'X'), + HB_TAG('L', 'T', 'S', 'H'), HB_TAG('P', 'C', 'L', 'T'), HB_TAG('D', 'S', 'I', 'G'), + HB_TAG('k', 'e', 'r', 'n'), HB_TAG('m', 'e', 't', 'a'), HB_TAG('S', 'T', 'A', 'T')}) { + hb_set_add(drop_tables, tag); + } hb_subset_input_set_flags(input, HB_SUBSET_FLAGS_GLYPH_NAMES | HB_SUBSET_FLAGS_NOTDEF_OUTLINE | (retainGids ? HB_SUBSET_FLAGS_RETAIN_GIDS : 0)); @@ -1862,7 +1877,7 @@ std::string subsetProgram(FontProgram const& p, std::set const& glyphs, return out; } -void ensureMicrosoftUnicodeCmap(std::string& sfnt) { +static void ensureMicrosoftUnicodeCmap(std::string& sfnt) { if (sfnt.size() < 12) return; auto read16 = [&](size_t off) -> uint16_t { return (static_cast(sfnt[off]) << 8) | static_cast(sfnt[off + 1]); @@ -1912,30 +1927,46 @@ void ensureMicrosoftUnicodeCmap(std::string& sfnt) { uint16_t numSubtables = read16(static_cast(cmapOffset) + 2); if (cmapLength < 4 || static_cast(numSubtables) * 8 > cmapLength - 4) return; - bool hasMsUnicode = false; - size_t unicodeSubtableOff = 0; - + int unicodeIndex = -1; for (size_t i = 0; i < numSubtables; ++i) { size_t subOff = cmapOffset + 4 + i * 8; uint16_t plat = read16(subOff); uint16_t enc = read16(subOff + 2); if (plat == 3 && (enc == 1 || enc == 10)) { - hasMsUnicode = true; + unicodeIndex = static_cast(i); break; } - if (plat == 0 && (enc == 3 || enc == 4 || enc == 0)) { - uint32_t subtableDataOffset = read32(subOff + 4); - if (static_cast(cmapOffset) + subtableDataOffset + 2 <= sfnt.size() && - read16(static_cast(cmapOffset) + subtableDataOffset) == 4) - unicodeSubtableOff = subOff; + } + if (unicodeIndex < 0) { + for (size_t i = 0; i < numSubtables; ++i) { + size_t subOff = cmapOffset + 4 + i * 8; + uint16_t plat = read16(subOff); + uint16_t enc = read16(subOff + 2); + if (plat == 0 && (enc == 3 || enc == 4 || enc == 0)) { + uint32_t subtableDataOffset = read32(subOff + 4); + if (static_cast(cmapOffset) + subtableDataOffset + 2 <= sfnt.size() && + read16(static_cast(cmapOffset) + subtableDataOffset) == 4) { + unicodeIndex = static_cast(i); + break; + } + } } } - if (hasMsUnicode || !unicodeSubtableOff) return; + if (unicodeIndex < 0) return; - // Convert Unicode subtable (0, 3) to Microsoft Unicode (3, 1) - write16(unicodeSubtableOff, 3); - write16(unicodeSubtableOff + 2, 1); + if (numSubtables > 1 || read16(static_cast(cmapOffset) + 4) == 0) { + if (unicodeIndex > 0) { + size_t srcOff = static_cast(cmapOffset) + 4 + unicodeIndex * 8; + size_t dstOff = static_cast(cmapOffset) + 4; + std::memmove(&sfnt[dstOff], &sfnt[srcOff], 8); + } + if (read16(static_cast(cmapOffset) + 4) == 0) { + write16(static_cast(cmapOffset) + 4, 3); + write16(static_cast(cmapOffset) + 6, 1); + } + write16(static_cast(cmapOffset) + 2, 1); + } // Recalculate cmap checksum uint32_t cmapSum = 0; @@ -2065,7 +2096,7 @@ FT_UInt glyphForCode(FT_Face face, int code, bool symbolic, SimpleEncoding const return 0; } -bool addWidths(QPDFObjectHandle font, FT_Face face, bool symbolic) { +QPDFObjectHandle buildWidths(QPDFObjectHandle const& font, FT_Face face, bool symbolic) { auto enc = parseEncoding(font); auto widths = QPDFObjectHandle::newArray(); bool any_positive = false; @@ -2080,11 +2111,13 @@ bool addWidths(QPDFObjectHandle font, FT_Face face, bool symbolic) { if (w > 0) any_positive = true; widths.appendItem(QPDFObjectHandle::newInteger(w)); } - if (!any_positive) return false; + return any_positive ? widths : QPDFObjectHandle(); +} + +void applyWidths(QPDFObjectHandle& font, QPDFObjectHandle const& widths) { font.replaceKey("/FirstChar", QPDFObjectHandle::newInteger(0)); font.replaceKey("/LastChar", QPDFObjectHandle::newInteger(255)); font.replaceKey("/Widths", widths); - return true; } bool reachesSameGlyphs(FT_Library lib, std::string const& subset, FT_Face original, @@ -2810,10 +2843,14 @@ bool sameFont(QPDFObjectHandle const& a, QPDFObjectHandle const& b) { } } -enum class CidEncoding { TwoByte, SingleByte, Rksj, KscEuc, GbEuc }; +enum class CidEncoding { TwoByte, SingleByte, Rksj, RksjProp, KscEuc, GbEuc }; -bool japan1CidForCode(uint16_t code, uint32_t& cid) { +bool japan1CidForCode(uint16_t code, uint32_t& cid, bool proportional = false) { // Codes in the printable ASCII block map linearly; skip the table search. + if (proportional && code >= 0x0020 && code <= 0x007E) { + cid = 1 + (code - 0x0020); + return true; + } if (code >= 0x0020 && code <= 0x007D) { cid = 231 + (code - 0x0020); return true; @@ -2940,7 +2977,8 @@ void collectStringCids(std::string_view bytes, std::set& cids, std::se } return; } - if (encoding == CidEncoding::Rksj) { + if (encoding == CidEncoding::Rksj || encoding == CidEncoding::RksjProp) { + bool proportional = encoding == CidEncoding::RksjProp; for (size_t i = 0; i < bytes.size();) { unsigned char lead = static_cast(bytes[i]); uint16_t code = lead; @@ -2956,7 +2994,7 @@ void collectStringCids(std::string_view bytes, std::set& cids, std::se ++i; } uint32_t cid = 0; - if (!japan1CidForCode(code, cid)) { + if (!japan1CidForCode(code, cid, proportional)) { complete = false; return; } @@ -3596,7 +3634,9 @@ CidScanResult usedCidsForFont(QPDF& qpdf, QPDFObjectHandle const& target, FontUsageIndex const* index = nullptr) { if (index && index->complete) { if (auto const* found = index->find(target)) return *found; - return {}; + // Direct dictionaries can be rewritten after indexing; only indirect + // fonts have a stable key that proves the font was not reached. + if (target.isIndirect()) return {}; } CidScanResult result; int budget = 2048; @@ -3808,9 +3848,9 @@ void pinTrueTypeMapping(QPDF& qpdf, QPDFObjectHandle font, FT_Face face, bool sy font.removeKey("/Encoding"); auto unicodeOf = unicodeByGlyph(face, symbolic); std::map codes; + SimpleEncoding none; for (int code = 0; code < 256; ++code) { - FT_UInt gid = FT_Get_Char_Index(face, code); - if (!gid) gid = FT_Get_Char_Index(face, 0xF000 | code); + FT_UInt gid = glyphForCode(face, code, true, none); if (gid) { if (auto it = unicodeOf.find(gid); it != unicodeOf.end()) codes[code] = it->second; } @@ -3833,7 +3873,8 @@ void pinTrueTypeMapping(QPDF& qpdf, QPDFObjectHandle font, FT_Face face, bool sy char name[64] = ""; if (const char* agl = adobeGlyphNameForUnicode(static_cast(u))) { std::snprintf(name, sizeof name, "%s", agl); - } else if (differs && unicodeForGlyphName(enc.differences.at(code))) { + } else if (differs && (unicodeForGlyphName(enc.differences.at(code)) || + (gid && FT_HAS_GLYPH_NAMES(face)))) { std::snprintf(name, sizeof name, "%s", enc.differences.at(code).c_str()); } else if (u > 0 && u <= 0xFFFF) { std::snprintf(name, sizeof name, "uni%04lX", u); @@ -4303,6 +4344,7 @@ int subsetEmbeddedFonts(QPDF& qpdf, bool subsetTagged) { // programs can be marked as shared (see below) and excluded from uniqueFonts. std::vector formFonts; std::set visitedFormResources; + std::set visitedFormStreams; std::vector pendingFormResources{ acroForm.isDictionary() ? acroForm.getKey("/DR") : QPDFObjectHandle()}; while (!pendingFormResources.empty()) { @@ -4325,6 +4367,8 @@ int subsetEmbeddedFonts(QPDF& qpdf, bool subsetTagged) { if (xobjects.isDictionary()) { for (auto const& [name, xobject] : xobjects.getDictAsMap()) { if (!xobject.isStream()) continue; + if (xobject.isIndirect() && !visitedFormStreams.insert(xobject.getObjGen()).second) + continue; pendingFormResources.push_back(xobject.getDict().getKey("/Resources")); } } @@ -4555,12 +4599,19 @@ EmbedFontsResult embedFonts(std::function const& load, bool identityCidMap = cidMap.isNull() || cidMap.isNameAndEquals("/Identity"); bool identityEncoding = font.getKey("/Encoding").isNameAndEquals("/Identity-H") || font.getKey("/Encoding").isNameAndEquals("/Identity-V"); + QPDFObjectHandle cjkEncoding = font.getKey("/Encoding"); + bool japan1Proportional = cid && !hasProgram && onlyMissing && + (cjkEncoding.isNameAndEquals("/90msp-RKSJ-H") || + cjkEncoding.isNameAndEquals("/90msp-RKSJ-V")); bool japan1Rksj = cid && !hasProgram && onlyMissing && - font.getKey("/Encoding").isNameAndEquals("/90ms-RKSJ-H"); + (cjkEncoding.isNameAndEquals("/90ms-RKSJ-H") || + cjkEncoding.isNameAndEquals("/90ms-RKSJ-V") || japan1Proportional); bool korea1Ksc = cid && !hasProgram && onlyMissing && - font.getKey("/Encoding").isNameAndEquals("/KSC-EUC-H"); + (cjkEncoding.isNameAndEquals("/KSC-EUC-H") || + cjkEncoding.isNameAndEquals("/KSC-EUC-V")); bool gb1Gb = cid && !hasProgram && onlyMissing && - font.getKey("/Encoding").isNameAndEquals("/GB-EUC-H"); + (cjkEncoding.isNameAndEquals("/GB-EUC-H") || + cjkEncoding.isNameAndEquals("/GB-EUC-V")); bool predefinedCjk = japan1Rksj || korea1Ksc || gb1Gb; std::map cidUnicode; std::map codeUnicode; @@ -4570,9 +4621,10 @@ EmbedFontsResult embedFonts(std::function const& load, if (cid && !hasProgram && onlyMissing) { if (!identityCidMap) continue; if (predefinedCjk) { - CidEncoding enc = japan1Rksj ? CidEncoding::Rksj - : korea1Ksc ? CidEncoding::KscEuc - : CidEncoding::GbEuc; + CidEncoding enc = japan1Proportional ? CidEncoding::RksjProp + : japan1Rksj ? CidEncoding::Rksj + : korea1Ksc ? CidEncoding::KscEuc + : CidEncoding::GbEuc; auto scan = usedCidsForFont(*qpdf, font, enc); usedCids = scan.cids; if (!scan.complete || usedCids.empty()) continue; @@ -4591,7 +4643,8 @@ EmbedFontsResult embedFonts(std::function const& load, if (code > 0xFFFF) continue; uint32_t cidFromCode = 0; bool found = - japan1Rksj ? japan1CidForCode(static_cast(code), cidFromCode) + japan1Rksj ? japan1CidForCode(static_cast(code), cidFromCode, + japan1Proportional) : korea1Ksc ? korea1CidForCode(static_cast(code), cidFromCode) : gb1CidForCode(static_cast(code), cidFromCode); if (!found) continue; @@ -4633,6 +4686,21 @@ EmbedFontsResult embedFonts(std::function const& load, writeCidWidths(target, program.face, cidGlyph); } } + auto flags = + descriptor.isDictionary() ? descriptor.getKey("/Flags") : QPDFObjectHandle(); + bool hasEncoding = font.hasKey("/Encoding") || target.hasKey("/Encoding"); + bool builtInMapping = !cid && !hasEncoding; + bool isSymbolName = (targetName == "Symbol" || targetName == "ZapfDingbats" || + targetName.find("Symbol") != std::string::npos || + targetName.find("Dingbats") != std::string::npos); + bool symbolic = builtInMapping && + (isSymbolName || (flags.isInteger() && (flags.getIntValue() & 4) != 0)); + bool mapBuiltIn = builtInMapping && symbolic; + QPDFObjectHandle pendingWidths; + if (!cid && (onlyMissing || !target.getKey("/Widths").isArray())) { + pendingWidths = buildWidths(target, program.face, mapBuiltIn); + if (!pendingWidths.isInitialized()) continue; + } if (descriptor.isIndirect()) { // A descriptor can be shared by a font that is not being // replaced. Always isolate it before installing a program so @@ -4644,16 +4712,6 @@ EmbedFontsResult embedFonts(std::function const& load, descriptor.replaceKey("/FontName", target.getKey("/BaseFont")); target.replaceKey("/FontDescriptor", descriptor); } - auto flags = - descriptor.isDictionary() ? descriptor.getKey("/Flags") : QPDFObjectHandle(); - bool hasEncoding = font.hasKey("/Encoding") || target.hasKey("/Encoding"); - bool builtInMapping = !cid && !hasEncoding; - bool isSymbolName = (targetName == "Symbol" || targetName == "ZapfDingbats" || - targetName.find("Symbol") != std::string::npos || - targetName.find("Dingbats") != std::string::npos); - bool symbolic = builtInMapping && - (isSymbolName || (flags.isInteger() && (flags.getIntValue() & 4) != 0)); - bool mapBuiltIn = builtInMapping && symbolic; if (!cid && !hasProgram && onlyMissing) { // A substitute must cover every character the content actually // draws. Installing a program with missing glyphs silently @@ -4672,6 +4730,7 @@ EmbedFontsResult embedFonts(std::function const& load, } if (!covered) continue; } + if (pendingWidths.isInitialized()) applyWidths(target, pendingWidths); target.replaceKey("/Type", QPDFObjectHandle::newName("/Font")); if (cid) font.replaceKey("/Type", QPDFObjectHandle::newName("/Font")); if (!descriptor.isDictionary()) { @@ -4719,10 +4778,6 @@ EmbedFontsResult embedFonts(std::function const& load, pinTrueTypeMapping(*qpdf, target, program.face, symbolic); } } - if (!cid && (onlyMissing || !target.getKey("/Widths").isArray()) && - !addWidths(target, program.face, mapBuiltIn)) { - continue; - } // OTTO/CFF can be carried by a pre-1.6 PDF as /FontFile3 // /Subtype /Type1C or /CIDFontType0C instead of requiring /Subtype /OpenType // and a PDF 1.6 bump (which would violate PDF/A-1). @@ -4826,7 +4881,24 @@ EmbedFontsResult embedFonts(std::function const& load, : "/FontFile3", stream); if (cid) { - std::set activeCids = cidsPresentInProgram(target, program.face); + std::set activeCids; + if (!glyphs.empty()) { + std::string map_view; + auto map = target.getKey("/CIDToGIDMap"); + if (map.isStream()) { + try { + auto data = map.getStreamData(qpdf_dl_generalized); + if (data) { + map_view.assign(reinterpret_cast(data->getBuffer()), + data->getSize()); + } + } catch (...) { + } + } + activeCids = cidsForRetainedGlyphs(target, map_view, glyphs); + } else { + activeCids = cidsPresentInProgram(target, program.face); + } if (activeCids.empty()) { // The installed map could not be read back. Derive the bitmap // from the mapping this rewrite actually wrote, or leave the @@ -4875,7 +4947,7 @@ EmbedFontsResult embedFonts(std::function const& load, } else { writer.setOutputMemory(); } - WriterLock lock{-1}; + WriterLock lock{9}; writer.write(); return {out ? nullptr : writer.getBufferSharedPointer(), "", 0}; } catch (QPDFExc const& e) { @@ -5079,7 +5151,8 @@ std::string inspectFonts(QPDF& qpdf) { } } } - for (auto& directFont : directFonts) { + for (size_t directIndex = 0; directIndex < directFonts.size(); ++directIndex) { + QPDFObjectHandle directFont = directFonts[directIndex]; std::string key = directFont.unparse(); if (!reachableDirect.contains(key)) continue; auto subtype = directFont.getKey("/Subtype"); @@ -5109,7 +5182,7 @@ std::string inspectFonts(QPDF& qpdf) { if (!dict.isDictionary()) return false; auto type = dict.getKey("/Type"); auto subtype = dict.getKey("/Subtype"); - if (!type.isName() || type.getName() != "/Font") return false; + if (type.isName() && type.getName() != "/Font") return false; // CID descendants are part of their Type 0 parent; reporting both would // duplicate the same program (or its absence). return !subtype.isName() || @@ -5440,7 +5513,7 @@ JPDFIUM_EXPORT int32_t jpdfium_qpdf_subset_fonts(const uint8_t* input, int64_t i writer.setDecodeLevel(qpdf_dl_none); writer.setNewlineBeforeEndstream(true); writer.setOutputMemory(); - WriterLock lock{-1}; + WriterLock lock{9}; writer.write(); auto buf = writer.getBufferSharedPointer(); if (!buf || buf->getSize() == 0) return -1; diff --git a/native/bridge/src/jpdfium_qpdf_writer_lock.h b/native/bridge/src/jpdfium_qpdf_writer_lock.h index 1e26f1ad7..ca15a6d86 100644 --- a/native/bridge/src/jpdfium_qpdf_writer_lock.h +++ b/native/bridge/src/jpdfium_qpdf_writer_lock.h @@ -5,6 +5,10 @@ // requests an explicit level takes it exclusively and restores qpdf's default // afterwards. This keeps concurrent jobs from recompressing each other's // streams at the wrong level without serializing ordinary qpdf work. +// +// The font passes (embed, subset, PDF/X write) request level 9, so while one +// of them runs, other threads' writers wait rather than interleave: a font +// pass is a whole-document rewrite that holds the mutex for its full duration. #ifndef JPDFIUM_QPDF_WRITER_LOCK_H #define JPDFIUM_QPDF_WRITER_LOCK_H