package org.apache.fop.afp;
import java.awt.Color;
+import java.awt.geom.AffineTransform;
import java.io.IOException;
import org.apache.commons.logging.Log;
/** logging instance */
private static Log log = LogFactory.getLog(AFPTextHandler.class);
+ private static final int X = 0;
+ private static final int Y = 1;
+
private AFPGraphics2D g2d = null;
/** Overriding FontState */
/**
* Main constructor.
- * @param g2d the PSGraphics2D instance this instances is used by
+ * @param g2d the AFPGraphics2D instance
*/
public AFPTextHandler(AFPGraphics2D g2d) {
this.g2d = g2d;
/**
* Return the font information associated with this object
+ *
* @return the FontInfo object
*/
public FontInfo getFontInfo() {
/**
* Add a text string to the current data object of the AFP datastream.
* The text is painted using text operations.
+ *
* {@inheritDoc}
*/
public void drawString(String str, float x, float y) throws IOException {
log.debug("drawString() str=" + str + ", x=" + x + ", y=" + y);
GraphicsObject graphicsObj = g2d.getGraphicsObject();
- Color col = g2d.getColor();
+ Color color = g2d.getColor();
- AFPPaintingState state = g2d.getPaintingState();
- if (state.setColor(col)) {
- graphicsObj.setColor(col);
+ AFPPaintingState paintingState = g2d.getPaintingState();
+ if (paintingState.setColor(color)) {
+ graphicsObj.setColor(color);
}
if (overrideFont != null) {
FontInfo fontInfo = getFontInfo();
- AFPPageFonts pageFonts = state.getPageFonts();
+ AFPPageFonts pageFonts = paintingState.getPageFonts();
String internalFontName = overrideFont.getFontName();
int fontSize = overrideFont.getFontSize();
- if (state.setFontName(internalFontName) || state.setFontSize(fontSize)) {
+ if (paintingState.setFontName(internalFontName) || paintingState.setFontSize(fontSize)) {
AFPFont font = (AFPFont)fontInfo.getFonts().get(internalFontName);
AFPFontAttributes afpFontAttributes = pageFonts.registerFont(
internalFontName,
graphicsObj.setCharacterSet(fontReference);
}
}
- graphicsObj.addString(str, Math.round(x), Math.round(y));
+
+ // calculate x, y plotting coordinates from graphics context
+ AffineTransform at = g2d.getTransform();
+ float[] srcPts = new float[] { x, y };
+ float[] dstPts = new float[srcPts.length];
+ at.transform(srcPts, 0, dstPts, 0, 1);
+
+ graphicsObj.addString(str, Math.round(dstPts[X]), Math.round(dstPts[Y]));
}
/**
* Sets the overriding font.
+ *
* @param overrideFont Overriding Font to set
*/
public void setOverrideFont(Font overrideFont) {