Thursday, March 31, 2011

Estimate pixels of proportional font in SVG or Java

Is there an way to estimate the number of pixels used by a proportional font? I am writing software that creates an image in SVG and transform that to PNG in Java. In this image I am using a text with a proportional font (size 16). I can sometimes fit 26 characters in the picture and sometimes only 19. This is because 'WWW' takes much more space as 'li1'. How can I estimate the number of pixels a String needs?

From stackoverflow
  • Have a look at the FontMetrics class - the stringWidth() method in particular.

    If you have a graphics object g you are using for painting

    g.getFontMetrics(f).stringWidth(message)
    
    Edwin : Thanks. This worked well.

0 comments:

Post a Comment