Skip to content

Support ttc fonts in pdfbox #953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
29 changes: 25 additions & 4 deletions java/src/main/java/com/genexus/reports/PDFReportPDFBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.google.zxing.common.BitMatrix;
import com.google.zxing.oned.Code128Writer;

import org.apache.fontbox.ttf.TrueTypeCollection;
import org.apache.fontbox.ttf.TrueTypeFont;
import org.apache.pdfbox.cos.*;
import org.apache.pdfbox.io.IOUtils;
import org.apache.pdfbox.pdmodel.*;
Expand Down Expand Up @@ -572,21 +574,40 @@ public void setAsianFont(String fontName, String style) {
}
}

private PDFont getOrLoadFont(String fontName, PDDocument doc) throws IOException {
private PDFont getOrLoadFont(String fontName, PDDocument doc) {
PDFont cachedFont = fontCache.get(fontName);
if (cachedFont != null) {
return cachedFont;
}
PDFont font = createPDType1FontFromName(fontName);
String cacheKey = fontName;
if (font == null) {
String fontPath = getFontLocation(fontName);
if (!fontPath.isEmpty()) {
font = PDType0Font.load(doc, new File(fontPath));
} else {
File fontFile = new File(fontPath);
try {
if (fontPath.toLowerCase().endsWith(".ttc")) {
try (TrueTypeCollection ttc = new TrueTypeCollection(fontFile)) {
TrueTypeFont ttf = ttc.getFontByName(fontName);
if (ttf != null) {
font = PDType0Font.load(doc, ttf, true);
}
}
} else {
font = PDType0Font.load(doc, fontFile);
}
} catch (Exception e) {
log.error("Failed to load font {} defaulting to Helvetica", fontName, e);
}
}
if (font == null) {
font = new PDType1Font(Standard14Fonts.FontName.HELVETICA);
cacheKey = "Helvetica";
}
}
fontCache.put(fontName, font);
if (!fontCache.containsKey(cacheKey)) {
fontCache.put(cacheKey, font);
}
return font;
}

Expand Down
Loading