Class FontMetrics

java.lang.Object
net.codecrete.qrbill.canvas.FontMetrics

public class FontMetrics extends Object
Simple font metrics class, independent of graphics subsystems and installed fonts.

It supports Helvetica, Arial, Frutiger, Liberation Sans. Kerning and ligatures are not supported.

  • Constructor Details

    • FontMetrics

      public FontMetrics(String fontFamilyList)
      Creates a new instance for the first supported font in the specified list.
      Parameters:
      fontFamilyList - list of font families
  • Method Details

    • getFontFamilyList

      public String getFontFamilyList()
      Gets the font family list.
      Returns:
      font family list (comma separated)
    • getFirstFontFamily

      public String getFirstFontFamily()
      Gets the first font family (from the font family list).
      Returns:
      first font family
    • getAscender

      public double getAscender(int fontSize)
      Distance between baseline and top of the highest letter.
      Parameters:
      fontSize - the font size (in pt)
      Returns:
      the distance (in mm)
    • getDescender

      public double getDescender(int fontSize)
      Distance between baseline and bottom of letter extending the farthest below the baseline.
      Parameters:
      fontSize - the font size (in pt)
      Returns:
      the distance (in mm)
    • getLineHeight

      public double getLineHeight(int fontSize)
      Distance between the baselines of two consecutive text lines.
      Parameters:
      fontSize - the font size (in pt)
      Returns:
      the distance (in mm)
    • splitLines

      public String[] splitLines(String text, double maxLength, int fontSize)
      Splits the text into lines.

      If a line would exceed the specified maximum length, line breaks are inserted. Newlines are treated as fixed line breaks.

      Parameters:
      text - the text
      maxLength - the maximum line length (in pt)
      fontSize - the font size (in pt)
      Returns:
      an array of text lines
    • getTextWidth

      public double getTextWidth(CharSequence text, int fontSize, boolean isBold)
      Returns the width of the specified text for the specified font size
      Parameters:
      text - text
      fontSize - font size (in pt)
      isBold - indicates if the text is in bold or regular weight
      Returns:
      width (in mm)