diff options
author | Simon Steiner <ssteiner@apache.org> | 2023-07-20 12:52:09 +0100 |
---|---|---|
committer | Simon Steiner <ssteiner@apache.org> | 2023-07-20 12:52:09 +0100 |
commit | 05f8a2c9d2084b29b7ddf49a25d6a8e028243b7c (patch) | |
tree | 66a428bd3ea890a051e98a197f000e8cbe284a84 /fop-core/src/test | |
parent | b16022ece329197f72f47943085d45b56e26806e (diff) | |
download | xmlgraphics-fop-05f8a2c9d2084b29b7ddf49a25d6a8e028243b7c.tar.gz xmlgraphics-fop-05f8a2c9d2084b29b7ddf49a25d6a8e028243b7c.zip |
FOP-2985: Reset character spacing by Dave Roxburgh
Diffstat (limited to 'fop-core/src/test')
-rw-r--r-- | fop-core/src/test/java/org/apache/fop/render/afp/AFPPainterTestCase.java | 46 | ||||
-rw-r--r-- | fop-core/src/test/java/org/apache/fop/render/pdf/PDFPainterTestCase.java | 26 |
2 files changed, 63 insertions, 9 deletions
diff --git a/fop-core/src/test/java/org/apache/fop/render/afp/AFPPainterTestCase.java b/fop-core/src/test/java/org/apache/fop/render/afp/AFPPainterTestCase.java index 3add2a495..b396d6d67 100644 --- a/fop-core/src/test/java/org/apache/fop/render/afp/AFPPainterTestCase.java +++ b/fop-core/src/test/java/org/apache/fop/render/afp/AFPPainterTestCase.java @@ -28,7 +28,6 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URI; -import java.net.URISyntaxException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -45,6 +44,8 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import org.apache.commons.io.IOUtils; + import org.apache.xmlgraphics.image.loader.Image; import org.apache.xmlgraphics.image.loader.ImageException; import org.apache.xmlgraphics.image.loader.ImageFlavor; @@ -155,7 +156,7 @@ public class AFPPainterTestCase { } @Test - public void testPresentationText() throws URISyntaxException, IFException, IOException { + public void testPresentationText() throws Exception { List<String> strings = new ArrayList<String>(); strings.add("test"); Assert.assertEquals(writeText(strings), "BEGIN DOCUMENT DOC00001\n" @@ -190,7 +191,7 @@ public class AFPPainterTestCase { } @Test - public void testPresentationText2() throws URISyntaxException, IFException, IOException { + public void testPresentationText2() throws Exception { List<String> strings = new ArrayList<String>(); for (int i = 0; i < 5000; i++) { strings.add("tes"); @@ -211,7 +212,37 @@ public class AFPPainterTestCase { + "END DOCUMENT DOC00001\n"); } - private String writeText(List<String> text) throws URISyntaxException, IOException, IFException { + /** + * Checks that letter spacing is reset to 0 after the relevant text block. + */ + @Test + public void testLetterSpacingReset() throws Exception { + List<String> strings = new ArrayList<>(); + strings.add("xxxx"); + InputStream inputStream = getDocResultInputStream(strings, 10000); + byte[] bytes = IOUtils.toByteArray(inputStream); + // The 134th byte is incremented by 5 to account for the 5 extra bytes inserted for the reset. + Assert.assertEquals((byte)39, bytes[134]); + // And these are the 5 reset bytes. + Assert.assertEquals((byte)5, bytes[163]); + Assert.assertEquals((byte)195, bytes[164]); + Assert.assertEquals((byte)0, bytes[165]); + Assert.assertEquals((byte)0, bytes[166]); + Assert.assertEquals((byte)0, bytes[167]); + } + + private String writeText(List<String> text) throws Exception { + InputStream bis = getDocResultInputStream(text); + StringBuilder sb = new StringBuilder(); + new AFPParser(false).read(bis, sb); + return sb.toString(); + } + + private static InputStream getDocResultInputStream(List<String> text) throws Exception { + return getDocResultInputStream(text, 0); + } + + private static InputStream getDocResultInputStream(List<String> text, int letterSpacing) throws Exception { FOUserAgent agent = FopFactory.newInstance(new URI(".")).newFOUserAgent(); IFContext context = new IFContext(agent); AFPDocumentHandler doc = new AFPDocumentHandler(context); @@ -232,14 +263,11 @@ public class AFPPainterTestCase { doc.startDocument(); doc.startPage(0, "", "", new Dimension()); for (String s : text) { - afpPainter.drawText(0, 0, 0, 0, null, s); + afpPainter.drawText(0, 0, letterSpacing, 0, null, s); } doc.endDocument(); - InputStream bis = new ByteArrayInputStream(outputStream.toByteArray()); - StringBuilder sb = new StringBuilder(); - new AFPParser(false).read(bis, sb); - return sb.toString(); + return new ByteArrayInputStream(outputStream.toByteArray()); } @Test 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 7b5d34bdc..c2d9a325b 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 @@ -184,6 +184,32 @@ public class PDFPainterTestCase { verify(pdfContentGenerator).add("0 Tr\n"); } + /** + * Tests that letter spacing is set and reset appropriately. + * @throws IFException + */ + @Test + public void testLetterSpacing() throws IFException { + final StringBuilder sb = new StringBuilder(); + pdfDocumentHandler = makePDFDocumentHandler(sb); + + FontInfo fi = new FontInfo(); + fi.addFontProperties("f1", new FontTriplet("a", "normal", 400)); + MultiByteFont font = new MultiByteFont(null, null); + fi.addMetrics("f1", font); + pdfDocumentHandler.setFontInfo(fi); + MyPDFPainter pdfPainter = new MyPDFPainter(pdfDocumentHandler, null); + pdfPainter.setFont("a", "normal", 400, null, 12, null); + pdfPainter.drawText(0, 0, 4321, 0, null, "test"); + + assertEquals("BT\n" + + "/f1 0.012 Tf\n" + + "1 0 0 -1 0 0 Tm [<0000000000000000>] TJ\n", + sb.toString()); + verify(pdfContentGenerator).updateCharacterSpacing(4.321f); + verify(pdfContentGenerator).resetCharacterSpacing(); + } + @Test public void testSimulateStyleColor() throws Exception { FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI()); |