Class BasicFontMetrics

  • Direct Known Subclasses:
    SystemFontMetrics

    public abstract class BasicFontMetrics
    extends Object
    Font metrics. The only way to generate accurate images in any format that contain text is to know the font metrics. Specifically, we need to know the width of every character and the maximum height (since we want all characters to fit within same line height). I used ttf2tfm to dump the font metrics from Mac TrueType fonts and then converted that to a Java class for use in a PostScript generator for trees. Commands:
             $ ttf2tfm /Library/Fonts/Arial\ Black.ttf > metrics
     
    Then run metrics into python code after stripping header/footer:
             #
             # Process lines from ttf2tfm that look like this:
             # Glyph  Code   Glyph Name                Width  llx    lly      urx    ury
             # ------------------------------------------------------------------------
             #     3  00020  space                       333  0,     0 --     0,     0
             #
             lines = open("metrics").read().split('\n')
             print "public class FontName {"
             print "    {"
             maxh = 0;
             for line in lines[4:]: # skip header 0..3
                             all = line.split(' ')
                             words = [x for x in all if len(x)>0]
                             ascii = int(words[1], 16)
                             height = int(words[8])
                             if height>maxh: maxh = height
                             if ascii>=128: break
                             print "        widths[%d] = %s; // %s" % (ascii, words[3], words[2])
    
             print "        maxCharHeight = "+str(maxh)+";"
             print "    }"
             print "}"
     
    Units are 1000th of an 'em'.
    • Field Detail

      • maxCharHeight

        protected int maxCharHeight
      • widths

        protected int[] widths
    • Constructor Detail

      • BasicFontMetrics

        public BasicFontMetrics()
    • Method Detail

      • getWidth

        public double getWidth​(String s,
                               int fontSize)
      • getWidth

        public double getWidth​(char c,
                               int fontSize)
      • getLineHeight

        public double getLineHeight​(int fontSize)