diff --git a/openhtmltopdf-pdfbox/src/main/java/com/openhtmltopdf/pdfboxout/PdfBoxFontResolver.java b/openhtmltopdf-pdfbox/src/main/java/com/openhtmltopdf/pdfboxout/PdfBoxFontResolver.java index 402621b6b..344facf52 100644 --- a/openhtmltopdf-pdfbox/src/main/java/com/openhtmltopdf/pdfboxout/PdfBoxFontResolver.java +++ b/openhtmltopdf-pdfbox/src/main/java/com/openhtmltopdf/pdfboxout/PdfBoxFontResolver.java @@ -76,7 +76,7 @@ public PdfBoxFontResolver(SharedContext sharedContext, PDDocument doc, FSCacheEx _pdfUaConform = pdfUaConform; // All fonts are required to be embedded in PDF/A documents, so we don't add the built-in fonts, if conformance is required. - _fontFamilies = (_pdfAConformance == PdfAConformance.NONE) ? createInitialFontMap() : new HashMap>(); + _fontFamilies = (_pdfAConformance == PdfAConformance.NONE && !pdfUaConform) ? createInitialFontMap() : new HashMap>(); } @Override @@ -721,6 +721,19 @@ private PdfBoxRawPDFontMetrics getFontMetricsFromCache(String family, int weight private void putFontMetricsInCache(String family, int weight, IdentValue style, PdfBoxRawPDFontMetrics metrics) { _metricsCache.put(createFontMetricsCacheKey(family, weight, style), metrics); } + + private boolean loadMetrics() { + try { + PDFontDescriptor descriptor = _font.getFontDescriptor(); + _metrics = PdfBoxRawPDFontMetrics.fromPdfBox(_font, descriptor); + putFontMetricsInCache(_family, _weight, _style, _metrics); + return true; + } catch (IOException e) { + XRLog.exception( + "Couldn't load font. Please check that it is a valid truetype font."); + return false; + } + } private boolean realizeFont() { if (_font == null && _fontSupplier != null) { @@ -728,6 +741,11 @@ private boolean realizeFont() { _font = _fontSupplier.supply(); _fontSupplier = null; + + if (!isMetricsAvailable()) { + // If we already have metrics, they must have come from the cache. + return loadMetrics(); + } } if (_font == null && _supplier != null) { @@ -744,10 +762,7 @@ private boolean realizeFont() { _font = PDType0Font.load(_doc, is, _isSubset); if (!isMetricsAvailable()) { - // If we already have metrics, they must have come from the cache. - PDFontDescriptor descriptor = _font.getFontDescriptor(); - _metrics = PdfBoxRawPDFontMetrics.fromPdfBox(_font, descriptor); - putFontMetricsInCache(_family, _weight, _style, _metrics); + return loadMetrics(); } } catch (IOException e) { XRLog.exception("Couldn't load font. Please check that it is a valid truetype font."); diff --git a/openhtmltopdf-pdfbox/src/main/java/com/openhtmltopdf/pdfboxout/PdfBoxTextRenderer.java b/openhtmltopdf-pdfbox/src/main/java/com/openhtmltopdf/pdfboxout/PdfBoxTextRenderer.java index efa481d01..97df52658 100644 --- a/openhtmltopdf-pdfbox/src/main/java/com/openhtmltopdf/pdfboxout/PdfBoxTextRenderer.java +++ b/openhtmltopdf-pdfbox/src/main/java/com/openhtmltopdf/pdfboxout/PdfBoxTextRenderer.java @@ -76,6 +76,7 @@ public FSFontMetrics getFSFontMetrics(FontContext context, FSFont font, String s PdfBoxRawPDFontMetrics metrics = des.getFontMetrics(); if (metrics == null) { + XRLog.exception("Font metrics not available. Probably a bug."); continue; }