|
19 | 19 | import com.google.zxing.common.BitMatrix; |
20 | 20 | import com.google.zxing.oned.Code128Writer; |
21 | 21 |
|
| 22 | +import org.apache.fontbox.ttf.TrueTypeCollection; |
| 23 | +import org.apache.fontbox.ttf.TrueTypeFont; |
22 | 24 | import org.apache.pdfbox.cos.*; |
23 | 25 | import org.apache.pdfbox.io.IOUtils; |
24 | 26 | import org.apache.pdfbox.pdmodel.*; |
@@ -572,21 +574,40 @@ public void setAsianFont(String fontName, String style) { |
572 | 574 | } |
573 | 575 | } |
574 | 576 |
|
575 | | - private PDFont getOrLoadFont(String fontName, PDDocument doc) throws IOException { |
| 577 | + private PDFont getOrLoadFont(String fontName, PDDocument doc) { |
576 | 578 | PDFont cachedFont = fontCache.get(fontName); |
577 | 579 | if (cachedFont != null) { |
578 | 580 | return cachedFont; |
579 | 581 | } |
580 | 582 | PDFont font = createPDType1FontFromName(fontName); |
| 583 | + String cacheKey = fontName; |
581 | 584 | if (font == null) { |
582 | 585 | String fontPath = getFontLocation(fontName); |
583 | 586 | 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) { |
586 | 604 | font = new PDType1Font(Standard14Fonts.FontName.HELVETICA); |
| 605 | + cacheKey = "Helvetica"; |
587 | 606 | } |
588 | 607 | } |
589 | | - fontCache.put(fontName, font); |
| 608 | + if (!fontCache.containsKey(cacheKey)) { |
| 609 | + fontCache.put(cacheKey, font); |
| 610 | + } |
590 | 611 | return font; |
591 | 612 | } |
592 | 613 |
|
|
0 commit comments