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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,12 @@ public static FontStatus diagnose(String baseName, Path... fontDirs) {
}

private static Map<String, Face> indexFor(Path... fontDirs) {
return INDEXES.computeIfAbsent(
Map<String, Face> index = INDEXES.computeIfAbsent(
fontDirs.length == 0 ? SYSTEM_FONT_DIRS : List.of(fontDirs), PdfFontEmbedder::index);
if (fontDirs.length == 0) return index;
Map<String, Face> isolated = new HashMap<>(index);
isolated.entrySet().removeIf(entry -> entry.getValue().resource() != null);
return isolated;
}

private static String source(Face face) {
Expand All @@ -341,8 +345,7 @@ public static FileResult embedMissingTo(Path input, Path output, Path... fontDir

private static Map<String, byte[]> programsFor(List<String> missing, Path... fontDirs)
throws IOException {
Map<String, Face> index = INDEXES.computeIfAbsent(
fontDirs.length == 0 ? SYSTEM_FONT_DIRS : List.of(fontDirs), PdfFontEmbedder::index);
Map<String, Face> index = indexFor(fontDirs);
Map<String, byte[]> found = new LinkedHashMap<>();
Map<Face, byte[]> programs = new HashMap<>();
for (String name : missing) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import stirling.software.jpdfium.PdfDocument;
import stirling.software.jpdfium.exception.JPDFiumException;
import stirling.software.jpdfium.panama.QpdfLib;

import org.apache.pdfbox.Loader;
import org.apache.pdfbox.cos.COSArray;
Expand Down Expand Up @@ -443,18 +442,6 @@ private static void assertCidRemap(String toUnicodeBody) throws Exception {
}
}

@Test
void cidSubstituteWithoutFullGlyphCoverageIsLeftUnresolved() throws Exception {
assertCidSubstituteUnresolved("""
1 beginbfchar
<0001> <0041>
endbfchar
1 beginbfchar
<0002> <FFFF>
endbfchar
""");
}

@Test
void oversizedToUnicodeRangeLeavesCidFontUnresolved() throws Exception {
assertCidSubstituteUnresolved("""
Expand Down Expand Up @@ -490,11 +477,6 @@ void multiCharacterRangeDestinationLeavesCidFontUnresolved() throws Exception {
""");
}

@Test
void cidSubstituteMissingAUsedWidthIsLeftUnresolved() throws Exception {
assertCidSubstituteUnresolved("<0001> <0041>", 1, 2);
}

private static void assertCidSubstituteUnresolved(
String toUnicodeBody, int... widthCids) throws Exception {
assumeTrue(PdfFontEmbedder.isAvailable(), "native qpdf font replacement unavailable");
Expand Down Expand Up @@ -528,55 +510,6 @@ private static void assertCidSubstituteUnresolved(
}
}

@Test
void explicitCidReplacementGeneratesToUnicode() throws Exception {
assumeTrue(PdfFontEmbedder.isAvailable(), "native qpdf font replacement unavailable");
FontProgram source = findEmbeddedFont(
Files.readAllBytes(Path.of(PdfFontEmbedderTest.class
.getResource("/pdfs/general/basic-text.pdf").toURI())), "ArialMT");
assertNotNull(source, "fixture must contain ArialMT");
byte[] input = identityCidPdf(source.baseName(), null);
byte[] output = PdfFontEmbedder.embed(input, Map.of(source.baseName(), source.data()));
try (PDDocument pdf = Loader.loadPDF(output)) {
COSDictionary font = identityCidFont(pdf);
COSBase toUnicode = font.getDictionaryObject(COSName.TO_UNICODE);
assertTrue(toUnicode instanceof COSStream,
"explicit replacement of a CID font without /ToUnicode must generate one");
String cmap;
try (var in = ((COSStream) toUnicode).createInputStream()) {
cmap = new String(in.readAllBytes(), StandardCharsets.ISO_8859_1);
}
assertTrue(cmap.contains("<0024> <0041>") || cmap.contains("<0024><0041>"),
"glyph 36 is A in ArialMT: " + cmap);
}
}

@Test
void legacyReplaceStreamLeavesProgramlessFontsAlone() throws Exception {
assumeTrue(PdfFontEmbedder.isAvailable(), "native qpdf font replacement unavailable");
FontProgram source = findEmbeddedFont(
Files.readAllBytes(Path.of(PdfFontEmbedderTest.class
.getResource("/pdfs/general/basic-text.pdf").toURI())), "ArialMT");
assertNotNull(source, "fixture must contain ArialMT");
byte[] program = source.data().clone();
program[program.length - 1] ^= 0x55;
byte[] input = sharedBaseFontPdf(source.baseName(), source.data());
byte[] output = QpdfLib.replaceFontStream(input, source.baseName(), program);
try (PDDocument pdf = Loader.loadPDF(output)) {
PDResources resources = pdf.getPage(0).getResources();
var withStream = resources.getFont(COSName.getPDFName("F1")).getFontDescriptor();
var withoutStream = resources.getFont(COSName.getPDFName("F2")).getFontDescriptor();
assertNotNull(withStream);
assertNotNull(withoutStream);
assertTrue(withStream.getFontFile2() != null, "the existing stream must be replaced");
assertArrayEquals(program, withStream.getFontFile2().createInputStream().readAllBytes());
assertTrue(withoutStream.getFontFile2() == null
&& withoutStream.getFontFile3() == null
&& withoutStream.getFontFile() == null,
"the compatibility export must not embed a font that had no program");
}
}

private static byte[] identityCidPdf(String baseFont, String toUnicodeEntries, int... widthCids)
throws Exception {
try (PDDocument pdf = new PDDocument()) {
Expand Down
Loading
Loading