Skip to content

Commit

Permalink
FOP-3225: Avoid setting PCL cursor to keep spacing consistant
Browse files Browse the repository at this point in the history
  • Loading branch information
simonsteiner1984 committed Dec 13, 2024
1 parent fd08436 commit fb919cd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
16 changes: 15 additions & 1 deletion fop-core/src/main/java/org/apache/fop/render/pcl/PCLPainter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -448,6 +450,7 @@ private void drawTextNative(int x, int y, int letterSpacing, int wordSpacing, in
}
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;
Expand Down Expand Up @@ -510,7 +513,7 @@ private void drawTextUsingSoftFont(int x, int y, int letterSpacing, int wordSpac
}
}

if (x != -1 && y != -1) {
if (x != -1 && y != -1 && (currentX != x || currentY != y)) {
setCursorPos(x, y);
}

Expand All @@ -527,6 +530,7 @@ private void drawTextUsingSoftFont(int x, int y, int letterSpacing, int wordSpac
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)) {
Expand Down Expand Up @@ -662,6 +666,14 @@ public Dimension getImageSize() {
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);
Expand Down Expand Up @@ -702,6 +714,8 @@ private void changePrintDirection() throws IOException {
* @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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;

import javax.xml.transform.stream.StreamResult;

Expand Down Expand Up @@ -127,6 +128,32 @@ public void testTruetype() throws IFException, IOException, FontFormatException,
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);
Expand All @@ -142,6 +169,7 @@ private ByteArrayOutputStream getPCL(boolean optimizeResources)
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);
Expand Down

0 comments on commit fb919cd

Please sign in to comment.