xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Gabriel E. Sánchez Martínez" <gabrielesanc...@gmail.com>
Subject misaligned text
Date Sat, 23 Aug 2014 02:14:20 GMT

I am new to batik, and am hoping someone can help me with an issue I am 
having.  I was able to generate an SVG file using the SVGGraphics2D 
class from my Java program.  The file is successfully created, and looks 
more or less good, but the text is slightly misaligned.  I want a text 
label centered from left to right on the image.  By java rendering code 
has something like this:


// g2 is a Graphics2D
// xFrom and xTo are integers having the left and right limits of the 
drawing area
Font mediumFont = new Font("SansSerif", Font.PLAIN, 12);
Font largeFont = new Font("SansSerif", Font.PLAIN, 16);
FontMetrics fm;

fm = g2.getFontMetrics();
labelText = "My Title";
labelWidth = fm.stringWidth(labelText);
labelHeight = fm.getHeight();
g2.drawString(labelText, (xFrom + xTo - labelWidth) / 2, 5 + labelHeight);

fm = g2.getFontMetrics();
labelText = "My Subtitle";
labelWidth = fm.stringWidth(labelText);
labelHeight = fm.getHeight();
g2.drawString(labelText, (xFrom + xTo - labelWidth) / 2, 10 + 2 * 


The image is correctly rendered when I use non-batik plain-java code to 
show it on a JFrame or when I export it to an image, but when I use 
batik to generate an SVG, the title and subtitle are not centered with 
respect to each other.  They are approximately centered on the image, 
but not exactly, and for my specific application I need precise text 
alignment.  I notice the font on the SVG is slightly different from the 
plain-java font, and wonder if that has something to do with it.  I am 
using the setEmbeddedFontsOn property of the SVGGeneratorContext.

What am I missing?

I am running Kubuntu 14.04 and using OpenJDK 7.

Thanks in advance,

To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org

View raw message