From a6adcb5046713803e441beb25cd9f7c3260e00fd Mon Sep 17 00:00:00 2001 From: Simon Steiner Date: Thu, 24 Mar 2022 12:33:35 +0000 Subject: [PATCH] FOP-3057: Allow fallback to non svg glyphs git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1899170 13f79535-47bb-0310-9956-ffa450edef68 --- .../intermediate/AbstractIFPainter.java | 15 +++++++++++-- .../org/apache/fop/render/pdf/PDFPainter.java | 8 +++++-- .../org/apache/fop/render/ps/PSPainter.java | 6 +++-- .../fop/render/pdf/PDFPainterTestCase.java | 22 ++++++++++++++++--- 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/fop-core/src/main/java/org/apache/fop/render/intermediate/AbstractIFPainter.java b/fop-core/src/main/java/org/apache/fop/render/intermediate/AbstractIFPainter.java index fbcd16487..85e8435ef 100644 --- a/fop-core/src/main/java/org/apache/fop/render/intermediate/AbstractIFPainter.java +++ b/fop-core/src/main/java/org/apache/fop/render/intermediate/AbstractIFPainter.java @@ -476,8 +476,18 @@ public abstract class AbstractIFPainter implements drawText(x, y, letterSpacing, wordSpacing, dp, text); } - protected void drawSVGText(MultiByteFont multiByteFont, FontTriplet triplet, int x, int y, String text, - IFState state) throws IFException { + protected boolean drawSVGText(MultiByteFont multiByteFont, FontTriplet triplet, int x, int y, String text, + IFState state) throws IFException { + for (int i = 0; i < text.length();) { + int codepoint = text.codePointAt(i); + if (!Character.isWhitespace(codepoint)) { + SVGGlyphData svg = multiByteFont.getSVG(codepoint); + if (svg == null) { + return false; + } + } + i += Character.charCount(codepoint); + } int sizeMillipoints = state.getFontSize(); Font font = getFontInfo().getFontInstance(triplet, sizeMillipoints); int newx = x; @@ -497,5 +507,6 @@ public abstract class AbstractIFPainter implements newx += font.getCharWidth(c); i += Character.charCount(c); } + return true; } } diff --git a/fop-core/src/main/java/org/apache/fop/render/pdf/PDFPainter.java b/fop-core/src/main/java/org/apache/fop/render/pdf/PDFPainter.java index 114cb7ae7..bf5f67894 100644 --- a/fop-core/src/main/java/org/apache/fop/render/pdf/PDFPainter.java +++ b/fop-core/src/main/java/org/apache/fop/render/pdf/PDFPainter.java @@ -435,8 +435,12 @@ public class PDFPainter extends AbstractIFPainter { String fontKey = getFontInfo().getInternalFontKey(triplet); Typeface typeface = getTypeface(fontKey); if (typeface instanceof MultiByteFont && ((MultiByteFont) typeface).hasSVG()) { - drawSVGText((MultiByteFont) typeface, triplet, x, y, text, state); - } else if ((dp == null) || IFUtil.isDPOnlyDX(dp)) { + boolean success = drawSVGText((MultiByteFont) typeface, triplet, x, y, text, state); + if (success) { + return; + } + } + if ((dp == null) || IFUtil.isDPOnlyDX(dp)) { drawTextWithDX(x, y, text, triplet, letterSpacing, wordSpacing, IFUtil.convertDPToDX(dp)); } else { diff --git a/fop-core/src/main/java/org/apache/fop/render/ps/PSPainter.java b/fop-core/src/main/java/org/apache/fop/render/ps/PSPainter.java index a7f0f455f..2eadfcaec 100644 --- a/fop-core/src/main/java/org/apache/fop/render/ps/PSPainter.java +++ b/fop-core/src/main/java/org/apache/fop/render/ps/PSPainter.java @@ -363,8 +363,10 @@ public class PSPainter extends AbstractIFPainter { String fontKey = getFontKey(triplet); Typeface typeface = getTypeface(fontKey); if (typeface instanceof MultiByteFont && ((MultiByteFont) typeface).hasSVG()) { - drawSVGText((MultiByteFont) typeface, triplet, x, y, text, state); - return; + boolean success = drawSVGText((MultiByteFont) typeface, triplet, x, y, text, state); + if (success) { + return; + } } beginTextObject(); //TODO Ignored: state.getFontVariant() diff --git a/fop-core/src/test/java/org/apache/fop/render/pdf/PDFPainterTestCase.java b/fop-core/src/test/java/org/apache/fop/render/pdf/PDFPainterTestCase.java index f95e9f3e2..9fe1972a1 100644 --- a/fop-core/src/test/java/org/apache/fop/render/pdf/PDFPainterTestCase.java +++ b/fop-core/src/test/java/org/apache/fop/render/pdf/PDFPainterTestCase.java @@ -366,6 +366,20 @@ public class PDFPainterTestCase { Assert.assertTrue(out.contains("1 0 0 rg")); } + @Test + public void testSVGFontFallback() throws IFException, IOException { + String out = drawSVGFont(null); + Assert.assertEquals(out, "<< /Length 1 0 R >>\n" + + "stream\n" + + "q\n" + + "1 0 0 -1 0 0 cm\n" + + "BT\n" + + "/f1 0.012 Tf\n" + + "1 0 0 -1 0 0 Tm [<0000000000000000>] TJ\n" + + "\n" + + "endstream"); + } + @Test public void testSVGFontScale() throws IFException, IOException { String out = drawSVGFont("\n" @@ -388,9 +402,11 @@ public class PDFPainterTestCase { MultiByteFont font = new MultiByteFont(null, null); font.setWidthArray(new int[1]); Map svgs = new HashMap<>(); - SVGGlyphData svgGlyph = new SVGGlyphData(); - svgGlyph.setSVG(svg); - svgs.put(0, svgGlyph); + if (svg != null) { + SVGGlyphData svgGlyph = new SVGGlyphData(); + svgGlyph.setSVG(svg); + svgs.put(0, svgGlyph); + } font.setSVG(svgs); font.setBBoxArray(new Rectangle[] {new Rectangle()}); fi.addMetrics("f1", font); -- 2.39.5