Extract font width using FOP
Hi all, I'm trying to auto size a font to not exceed the block dimensions, giving a default max size and downgrading to predefined steps until it fits or a minimum size is reached. I can't use the SVG "trick" because I want to fit to a max defined width, and and as the size is reduced more text will fit in a single line. My idea is to build an external processor step that calculates the optimum font size to be used for the block. But to do this I need the exact font width (in millipoints) of each character (or word) of the selected font triplet on each font size I intend to use. Browsing the fop project source I've found the org.apache.fop.fonts.Font.getCharWidth(), but It's for me a bit tricky to instantiate correctly this Class. I assume the the best way to get this is to start from the org.apache.fop.tools.fontlist.FontListMain that does already the fop configuration and loading of the font. But as a xsl-fo end-user, I've no knowledge on the fop internals (structure/architecture) to go from A to B. Can you please give me some hints or practical examples? Wold it make sense to extend the org.apache.fop.tools to provide this functionality? Or am I on the wrong way? Thanks in advance for any hint or feedback you can provide. Regards. p.s. I'm generating documents using a custom font, that I've imported using org.apache.fop.fonts.apps.TTFReader and imported using the specific configuration file. -- View this message in context: http://apache-fop.1065347.n5.nabble.com/Extract-font-width-using-FOP-tp37360.html Sent from the FOP - Users mailing list archive at Nabble.com. - To unsubscribe, e-mail: fop-users-unsubscr...@xmlgraphics.apache.org For additional commands, e-mail: fop-users-h...@xmlgraphics.apache.org
Re: Extract font width using FOP
You can right an extension in Java and use Font Metrics... I wrote this a while back, so it might need to be updated. This example will return the width of a String. import java.awt.Font; import java.awt.FontMetrics; import java.awt.Toolkit; public class StringLength { public static void main(String[] args) throws NumberFormatException { if(args.length < 2){ System.out.println("StringLength "); System.exit(0); }else{ getStringLength(args[0], Integer.parseInt(args[1])); } } @SuppressWarnings("deprecation") public static int getStringLength(String str, int size){ Toolkit toolkit = Toolkit.getDefaultToolkit(); int stringlen = 0; Font font = new Font("Arial", Font.PLAIN, 9); FontMetrics fontMetrics = toolkit.getFontMetrics(font); System.out.println ("[Stylesheet Info] Calculating width for: " + str + ": " + fontMetrics.stringWidth(str)); stringlen = fontMetrics.stringWidth(str); return stringlen; } } -- View this message in context: http://apache-fop.1065347.n5.nabble.com/Extract-font-width-using-FOP-tp37360p37361.html Sent from the FOP - Users mailing list archive at Nabble.com. - To unsubscribe, e-mail: fop-users-unsubscr...@xmlgraphics.apache.org For additional commands, e-mail: fop-users-h...@xmlgraphics.apache.org
Re: Extract font width using FOP
except that (in general) FOP does not use AWT to obtain font metrics On Fri, Nov 16, 2012 at 4:29 PM, Bonekrusher wrote: > You can right an extension in Java and use Font Metrics... I wrote this a > while back, so it might need to be updated. This example will return the > width of a String. > > > import java.awt.Font; > import java.awt.FontMetrics; > import java.awt.Toolkit; > > public class StringLength { > > > public static void main(String[] args) throws > NumberFormatException { > > if(args.length < 2){ > System.out.println("StringLength font size>"); > System.exit(0); > }else{ > getStringLength(args[0], > Integer.parseInt(args[1])); > } > > } > > @SuppressWarnings("deprecation") > public static int getStringLength(String str, int size){ > > Toolkit toolkit = Toolkit.getDefaultToolkit(); > int stringlen = 0; > Font font = new Font("Arial", Font.PLAIN, 9); > FontMetrics fontMetrics = toolkit.getFontMetrics(font); > System.out.println ("[Stylesheet Info] Calculating width > for: " + str + ": > " + fontMetrics.stringWidth(str)); > stringlen = fontMetrics.stringWidth(str); > return stringlen; > } > > > } > > > > > -- > View this message in context: > http://apache-fop.1065347.n5.nabble.com/Extract-font-width-using-FOP-tp37360p37361.html > Sent from the FOP - Users mailing list archive at Nabble.com. > > - > To unsubscribe, e-mail: fop-users-unsubscr...@xmlgraphics.apache.org > For additional commands, e-mail: fop-users-h...@xmlgraphics.apache.org > >