Class PdfFont

java.lang.Object
com.codename1.pdf.PdfFont

public abstract class PdfFont extends Object

A font text is drawn and measured with.

The 14 standard PDF fonts (HELVETICA, TIMES_ROMAN, COURIER and their styles, plus SYMBOL and ZAPF_DINGBATS) are built into every reader and add nothing to the file, but they cover only the Western European characters of WinAnsiEncoding; any other character is drawn as ?. For any other script, load a TrueType font with [#loadTrueType(byte[])]: only the glyphs a document uses are embedded.

PdfDocument doc = new PdfDocument();
PdfCanvas canvas = doc.addPage(PdfPageSize.A4).getCanvas();
PdfFont body = PdfFont.loadTrueType(Util.readInputStream(
        CN.getResourceAsStream("/NotoSans-Regular.ttf")));
canvas.setFont(body, 12);
canvas.drawText("Grusse, Jalapeno", 72, 72);

A font object holds no per-document state and may be shared by any number of documents, including from different threads on a server.

  • Field Details

    • HELVETICA

      public static final PdfFont HELVETICA
      Helvetica, the standard sans serif.
    • HELVETICA_BOLD

      public static final PdfFont HELVETICA_BOLD
      Helvetica Bold.
    • HELVETICA_OBLIQUE

      public static final PdfFont HELVETICA_OBLIQUE
      Helvetica Oblique.
    • HELVETICA_BOLD_OBLIQUE

      public static final PdfFont HELVETICA_BOLD_OBLIQUE
      Helvetica Bold Oblique.
    • TIMES_ROMAN

      public static final PdfFont TIMES_ROMAN
      Times Roman, the standard serif.
    • TIMES_BOLD

      public static final PdfFont TIMES_BOLD
      Times Bold.
    • TIMES_ITALIC

      public static final PdfFont TIMES_ITALIC
      Times Italic.
    • TIMES_BOLD_ITALIC

      public static final PdfFont TIMES_BOLD_ITALIC
      Times Bold Italic.
    • COURIER

      public static final PdfFont COURIER
      Courier, the standard monospaced font.
    • COURIER_BOLD

      public static final PdfFont COURIER_BOLD
      Courier Bold.
    • COURIER_OBLIQUE

      public static final PdfFont COURIER_OBLIQUE
      Courier Oblique.
    • COURIER_BOLD_OBLIQUE

      public static final PdfFont COURIER_BOLD_OBLIQUE
      Courier Bold Oblique.
    • SYMBOL

      public static final PdfFont SYMBOL
      Symbol: Greek letters and mathematical signs, by their code in the font's own encoding (a char 'a' draws alpha).
    • ZAPF_DINGBATS

      public static final PdfFont ZAPF_DINGBATS
      ZapfDingbats: ornaments and arrows, by their code in the font's own encoding.
  • Constructor Details

    • PdfFont

      protected PdfFont()
      Subclassed only inside Codename One.
  • Method Details

    • loadTrueType

      public static PdfFont loadTrueType(byte[] data) throws IOException
      Loads a TrueType (.ttf) font, or the TrueType outlines of an OpenType font. Fonts whose outlines are CFF (.otf with a CFF table) and collections (.ttc) are rejected with an IOException, as is a font whose license bits forbid embedding.
      Throws:
      IOException
    • getName

      public abstract String getName()
      The PostScript name of the font.
    • getStringWidth

      public abstract float getStringWidth(String text, float size)
      The advance width of text at size points, in points.
    • getAscent

      public abstract float getAscent(float size)
      The height above the baseline of the font's tallest letters at size points, in points.
    • getDescent

      public abstract float getDescent(float size)
      The depth below the baseline at size points, as a positive number of points.
    • getLineHeight

      public float getLineHeight(float size)
      The recommended distance between two baselines at size points.
    • canDisplay

      public abstract boolean canDisplay(char c)
      True when the font can draw c with a glyph of its own.
    • createState

      protected abstract com.codename1.impl.pdf.FontState createState()
      Creates the per-document state that tracks which glyphs a document uses and writes the font objects. Internal.