aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Steiner <ssteiner@apache.org>2024-12-13 08:54:46 +0000
committerSimon Steiner <ssteiner@apache.org>2024-12-13 08:54:46 +0000
commitfb919cd338f96d6117d56564311f53f5c6daee9f (patch)
treebb2e57459a8fb88eb58b29f2f32dca2f4409f22e
parentfd084368f8c81c6a6b5aa4e94d443f4f0ad6e5ad (diff)
downloadxmlgraphics-fop-fb919cd338f96d6117d56564311f53f5c6daee9f.tar.gz
xmlgraphics-fop-fb919cd338f96d6117d56564311f53f5c6daee9f.zip
FOP-3225: Avoid setting PCL cursor to keep spacing consistant
-rw-r--r--fop-core/src/main/java/org/apache/fop/render/pcl/PCLPainter.java16
-rw-r--r--fop-core/src/test/java/org/apache/fop/render/pcl/PCLPainterTestCase.java28
2 files changed, 43 insertions, 1 deletions
diff --git a/fop-core/src/main/java/org/apache/fop/render/pcl/PCLPainter.java b/fop-core/src/main/java/org/apache/fop/render/pcl/PCLPainter.java
index 60d6eb89f..7df553771 100644
--- a/fop-core/src/main/java/org/apache/fop/render/pcl/PCLPainter.java
+++ b/fop-core/src/main/java/org/apache/fop/render/pcl/PCLPainter.java
@@ -86,6 +86,8 @@ public class PCLPainter extends AbstractIFPainter<PCLDocumentHandler> implements
private Stack<GraphicContext> graphicContextStack = new Stack<GraphicContext>();
private GraphicContext graphicContext = new GraphicContext();
private PCLSoftFontManager sfManager;
+ private int currentX = -1;
+ private int currentY = -1;
/**
* Main constructor.
@@ -448,6 +450,7 @@ public class PCLPainter extends AbstractIFPainter<PCLDocumentHandler> implements
}
for (int i = 0; i < l; i++) {
char orgChar = text.charAt(i);
+ currentX += getCharWidth(i, font, orgChar, text);
char ch;
float xGlyphAdjust = 0;
float yGlyphAdjust = 0;
@@ -510,7 +513,7 @@ public class PCLPainter extends AbstractIFPainter<PCLDocumentHandler> implements
}
}
- if (x != -1 && y != -1) {
+ if (x != -1 && y != -1 && (currentX != x || currentY != y)) {
setCursorPos(x, y);
}
@@ -527,6 +530,7 @@ public class PCLPainter extends AbstractIFPainter<PCLDocumentHandler> implements
String current = "";
for (int i = 0; i < l; i++) {
char orgChar = text.charAt(i);
+ currentX += getCharWidth(i, font, orgChar, text);
float glyphAdjust = 0;
if (!font.hasChar(orgChar)) {
if (CharUtilities.isFixedWidthSpace(orgChar)) {
@@ -662,6 +666,14 @@ public class PCLPainter extends AbstractIFPainter<PCLDocumentHandler> implements
paintMarksAsBitmap(painter, boundingBox);
}
+ private int getCharWidth(int i, Font font, char orgChar, String text) {
+ int width = font.getCharWidth(orgChar);
+ if (i > 0) {
+ width += font.getKernValue(text.charAt(i - 1), orgChar);
+ }
+ return width;
+ }
+
/** Saves the current graphics state on the stack. */
private void saveGraphicsState() {
graphicContextStack.push(graphicContext);
@@ -702,6 +714,8 @@ public class PCLPainter extends AbstractIFPainter<PCLDocumentHandler> implements
* @param y the y coordinate (in millipoints)
*/
void setCursorPos(int x, int y) throws IOException {
+ currentX = x;
+ currentY = y;
Point2D transPoint = transformedPoint(x, y);
gen.setCursorPos(transPoint.getX(), transPoint.getY());
}
diff --git a/fop-core/src/test/java/org/apache/fop/render/pcl/PCLPainterTestCase.java b/fop-core/src/test/java/org/apache/fop/render/pcl/PCLPainterTestCase.java
index 03e76b329..eb8f8f470 100644
--- a/fop-core/src/test/java/org/apache/fop/render/pcl/PCLPainterTestCase.java
+++ b/fop-core/src/test/java/org/apache/fop/render/pcl/PCLPainterTestCase.java
@@ -30,6 +30,7 @@ import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
+import java.util.Arrays;
import javax.xml.transform.stream.StreamResult;
@@ -127,6 +128,32 @@ public class PCLPainterTestCase {
Assert.assertTrue(optimizeResources.length() > 900);
}
+ @Test
+ public void testCursorPos() throws Exception {
+ Rectangle size = new Rectangle(1, 1);
+ PCLPageDefinition pclPageDef = new PCLPageDefinition("", 0, new Dimension(), size, true);
+ PCLDocumentHandler documentHandler = new PCLDocumentHandler(new IFContext(ua));
+ ByteArrayOutputStream output = new ByteArrayOutputStream();
+ documentHandler.setResult(new StreamResult(output));
+ documentHandler.startDocument();
+ PCLPainter pclPainter = new PCLPainter(documentHandler, pclPageDef);
+ FontInfo fi = new FontInfo();
+ fi.addFontProperties("", "", "", 0);
+ MultiByteFont mbf = new MultiByteFont(ua.getResourceResolver(), EmbeddingMode.AUTO);
+ mbf.setEmbedURI(new URI("test/resources/fonts/ttf/DejaVuLGCSerif.ttf"));
+ mbf.setFontType(FontType.TRUETYPE);
+ int[] widths = new int[100];
+ Arrays.fill(widths, 100);
+ mbf.setWidthArray(widths);
+ fi.addMetrics("", new CustomFontMetricsMapper(mbf));
+ documentHandler.setFontInfo(fi);
+ pclPainter.setFont("", "", 0, "", 12000, Color.BLACK);
+ pclPainter.drawText(0, 0, 0, 0, null, "test");
+ Assert.assertTrue(output.toString().contains("&a0h0V"));
+ pclPainter.drawText(4800, 0, 0, 0, null, "test");
+ Assert.assertFalse(output.toString().contains("&a48h0V"));
+ }
+
private ByteArrayOutputStream getPCL(boolean optimizeResources)
throws IFException, URISyntaxException, IOException, FontFormatException {
Rectangle size = new Rectangle(1, 1);
@@ -142,6 +169,7 @@ public class PCLPainterTestCase {
MultiByteFont mbf = new MultiByteFont(ua.getResourceResolver(), EmbeddingMode.AUTO);
mbf.setEmbedURI(new URI("test/resources/fonts/ttf/DejaVuLGCSerif.ttf"));
mbf.setFontType(FontType.TRUETYPE);
+ mbf.setWidthArray(new int[100]);
fi.addMetrics("", new CustomFontMetricsMapper(mbf));
documentHandler.setFontInfo(fi);
pclPainter.setFont("", "", 0, "", 0, Color.BLACK);