Skip to content

Commit fe4641e

Browse files
tomas-sexenianBeta Bot
authored andcommitted
Cherry pick branch 'genexuslabs:ttc-support' into beta
1 parent ac0c35b commit fe4641e

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

java/src/main/java/com/genexus/reports/PDFReportPDFBox.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import com.google.zxing.common.BitMatrix;
2020
import com.google.zxing.oned.Code128Writer;
2121

22+
import org.apache.fontbox.ttf.TrueTypeCollection;
23+
import org.apache.fontbox.ttf.TrueTypeFont;
2224
import org.apache.pdfbox.cos.*;
2325
import org.apache.pdfbox.io.IOUtils;
2426
import org.apache.pdfbox.pdmodel.*;
@@ -572,21 +574,40 @@ public void setAsianFont(String fontName, String style) {
572574
}
573575
}
574576

575-
private PDFont getOrLoadFont(String fontName, PDDocument doc) throws IOException {
577+
private PDFont getOrLoadFont(String fontName, PDDocument doc) {
576578
PDFont cachedFont = fontCache.get(fontName);
577579
if (cachedFont != null) {
578580
return cachedFont;
579581
}
580582
PDFont font = createPDType1FontFromName(fontName);
583+
String cacheKey = fontName;
581584
if (font == null) {
582585
String fontPath = getFontLocation(fontName);
583586
if (!fontPath.isEmpty()) {
584-
font = PDType0Font.load(doc, new File(fontPath));
585-
} else {
587+
File fontFile = new File(fontPath);
588+
try {
589+
if (fontPath.toLowerCase().endsWith(".ttc")) {
590+
try (TrueTypeCollection ttc = new TrueTypeCollection(fontFile)) {
591+
TrueTypeFont ttf = ttc.getFontByName(fontName);
592+
if (ttf != null) {
593+
font = PDType0Font.load(doc, ttf, true);
594+
}
595+
}
596+
} else {
597+
font = PDType0Font.load(doc, fontFile);
598+
}
599+
} catch (Exception e) {
600+
log.error("Failed to load font {} defaulting to Helvetica", fontName, e);
601+
}
602+
}
603+
if (font == null) {
586604
font = new PDType1Font(Standard14Fonts.FontName.HELVETICA);
605+
cacheKey = "Helvetica";
587606
}
588607
}
589-
fontCache.put(fontName, font);
608+
if (!fontCache.containsKey(cacheKey)) {
609+
fontCache.put(cacheKey, font);
610+
}
590611
return font;
591612
}
592613

0 commit comments

Comments
 (0)