From d6a1aadd0d30263130cf746dd6b5f8b5b70d5348 Mon Sep 17 00:00:00 2001 From: Simon Steiner Date: Tue, 2 Feb 2021 14:48:54 +0000 Subject: [PATCH] FOP-2997: Double byte glyphs not working in SVG font git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1886124 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/fop/fonts/MultiByteFont.java | 2 +- .../fop/fonts/truetype/SVGGlyphData.java | 2 +- .../intermediate/AbstractIFPainter.java | 4 ++- .../fop/render/pdf/PDFPainterTestCase.java | 25 +++++++++++++++---- 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/fop-core/src/main/java/org/apache/fop/fonts/MultiByteFont.java b/fop-core/src/main/java/org/apache/fop/fonts/MultiByteFont.java index 4f1263ebe..929b4bf6f 100644 --- a/fop-core/src/main/java/org/apache/fop/fonts/MultiByteFont.java +++ b/fop-core/src/main/java/org/apache/fop/fonts/MultiByteFont.java @@ -859,7 +859,7 @@ public class MultiByteFont extends CIDFont implements Substitutable, Positionabl return null; } - public SVGGlyphData getSVG(char c) { + public SVGGlyphData getSVG(int c) { int gid = findGlyphIndex(c); return svgs.get(gid); } diff --git a/fop-core/src/main/java/org/apache/fop/fonts/truetype/SVGGlyphData.java b/fop-core/src/main/java/org/apache/fop/fonts/truetype/SVGGlyphData.java index e46e50074..d018f5bf2 100644 --- a/fop-core/src/main/java/org/apache/fop/fonts/truetype/SVGGlyphData.java +++ b/fop-core/src/main/java/org/apache/fop/fonts/truetype/SVGGlyphData.java @@ -70,7 +70,7 @@ public class SVGGlyphData { Element gElement = (Element) nodes.item(0); if (gElement != null) { String transform = gElement.getAttribute("transform"); - if (transform.contains("scale(")) { + if (transform.split("scale\\(").length == 3) { String scaleStr = transform.split("scale\\(")[1].split("\\)")[0].trim(); scale = Float.parseFloat(scaleStr); gElement.removeAttribute("transform"); 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 44620ff61..e9499f881 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 @@ -478,7 +478,8 @@ public abstract class AbstractIFPainter implements int sizeMillipoints = state.getFontSize(); Font font = getFontInfo().getFontInstance(triplet, sizeMillipoints); int newx = x; - for (char c : text.toCharArray()) { + for (int i = 0; i < text.length();) { + int c = text.codePointAt(i); SVGGlyphData svg = multiByteFont.getSVG(c); if (svg != null) { int codePoint = font.mapCodePoint(c); @@ -491,6 +492,7 @@ public abstract class AbstractIFPainter implements drawImage(dataURL, boundingBox); } newx += font.getCharWidth(c); + i += Character.charCount(c); } } } 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 aa0274d97..1fe4a76df 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 @@ -311,6 +311,24 @@ public class PDFPainterTestCase { @Test public void testSVGFont() throws IFException, IOException { + String out = drawSVGFont("\n" + + "\n" + + ""); + Assert.assertTrue(out.contains("0.00012 0 0 0.00012 0 0 cm")); + Assert.assertTrue(out.contains("1 0 0 rg")); + } + + @Test + public void testSVGFontScale() throws IFException, IOException { + String out = drawSVGFont("\n" + + "" + + "\n" + + ""); + Assert.assertTrue(out.contains("0.00012 0 0 0.00012 0 0 cm")); + Assert.assertTrue(out.contains("1 0 0 rg")); + } + + private String drawSVGFont(String svg) throws IFException, IOException { FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI()); foUserAgent = fopFactory.newFOUserAgent(); PDFDocumentHandler pdfDocumentHandler = new PDFDocumentHandler(new IFContext(foUserAgent)); @@ -323,9 +341,7 @@ public class PDFPainterTestCase { font.setWidthArray(new int[1]); Map svgs = new HashMap<>(); SVGGlyphData svgGlyph = new SVGGlyphData(); - svgGlyph.setSVG("\n" - + "\n" - + ""); + svgGlyph.setSVG(svg); svgs.put(0, svgGlyph); font.setSVG(svgs); font.setBBoxArray(new Rectangle[] {new Rectangle()}); @@ -338,7 +354,6 @@ public class PDFPainterTestCase { filters.setDisableAllFilters(true); ByteArrayOutputStream bos = new ByteArrayOutputStream(); pdfPainter.generator.getStream().output(bos); - Assert.assertTrue(bos.toString().contains("0.00012 0 0 0.00012 0 0 cm")); - Assert.assertTrue(bos.toString().contains("1 0 0 rg")); + return bos.toString(); } } -- 2.39.5