Class TrackingFont

java.lang.Object
com.teamten.font.AbstractFont
com.teamten.font.TrackingFont
All Implemented Interfaces:
Font

public class TrackingFont extends AbstractFont
A font that wraps another font and adds tracking (inter-letter spacing). The tracking is defined in units of the font size. Tracking of 0.1 on an 11pt font, for example, would add 1.1pt of space after each letter.

Note that the space is added after each letter, not between letters, because the font abstraction does not know where the string of letter ends. This will cause right-aligned text to have a bit of space between it and the margin, and centered text to be slightly to the left of where it should be. You can fix this by using the underlying font for the last letter, at the cost of proper kerning and hyphenation.

The font does not support ligatures, and kerning is optionally respected.

  • Constructor Details

    • TrackingFont

      public TrackingFont(Font underlyingFont, double tracking, double kerning)
      Parameters:
      underlyingFont - the font to draw.
      tracking - fraction of font size to space out characters. Practical Typography recommends 5% to 12%. We tend to use around 10% (0.1).
      kerning - how much to kern, from 0.0 (no kerning) to 1.0 (full kerning). A value of 0.5 is recommended.
  • Method Details

    • hasCharacter

      public boolean hasCharacter(int ch)
      Description copied from interface: Font
      Whether this font handles this character.
    • create

      public static SizedFont create(SizedFont sizedFont, double tracking, double kerning)
      Utility method to create a SizedFont incorporating the TrackingFont and the same size.
    • getKerning

      public long getKerning(int leftChar, int rightChar, double fontSize)
      Description copied from interface: Font
      Get the kerning between the two code points. The result is in scaled points.
      Specified by:
      getKerning in interface Font
      Overrides:
      getKerning in class AbstractFont
      fontSize - the size of the font in points.
    • getSpaceWidth

      public long getSpaceWidth()
      Description copied from interface: Font
      The width of a space for a 1pt font, in scaled points.
    • getCharacterMetrics

      public Font.Metrics getCharacterMetrics(int ch, double fontSize)
      Description copied from interface: Font
      Return the size of a code point in the specified font size.
    • draw

      public void draw(String text, double fontSize, long x, long y, org.apache.pdfbox.pdmodel.PDPageContentStream contents) throws IOException
      Description copied from interface: Font
      Draw the text.
      Parameters:
      text - the text to draw.
      fontSize - the size in points.
      x - the left-hand edge of the text in scaled points.
      y - the baseline of the text in scaled points.
      contents - the stream to write to.
      Throws:
      IOException