Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.

public abstract class PdfFont

  1. Object
  2. PdfFont

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.

Fields

public static final PdfFont HELVETICAHelvetica, the standard sans serif.
public static final PdfFont HELVETICA_BOLDHelvetica Bold.
public static final PdfFont HELVETICA_OBLIQUEHelvetica Oblique.
public static final PdfFont HELVETICA_BOLD_OBLIQUEHelvetica Bold Oblique.
public static final PdfFont TIMES_ROMANTimes Roman, the standard serif.
public static final PdfFont TIMES_BOLDTimes Bold.
public static final PdfFont TIMES_ITALICTimes Italic.
public static final PdfFont TIMES_BOLD_ITALICTimes Bold Italic.
public static final PdfFont COURIERCourier, the standard monospaced font.
public static final PdfFont COURIER_BOLDCourier Bold.
public static final PdfFont COURIER_OBLIQUECourier Oblique.
public static final PdfFont COURIER_BOLD_OBLIQUECourier Bold Oblique.
public static final PdfFont SYMBOLSymbol: Greek letters and mathematical signs, by their code in the font’s own encoding (a char 'a' draws alpha).
public static final PdfFont ZAPF_DINGBATSZapfDingbats: ornaments and arrows, by their code in the font’s own encoding.

Constructors

protected PdfFont()Subclassed only inside Codename One.

Methods

public static PdfFont loadTrueType(byte[] data) throws IOExceptionLoads a TrueType (.ttf) font, or the TrueType outlines of an OpenType font.
public abstract String getName()The PostScript name of the font.
public abstract float getStringWidth(String text, float size)The advance width of text at size points, in points.
public abstract float getAscent(float size)The height above the baseline of the font’s tallest letters at size points, in points.
public abstract float getDescent(float size)The depth below the baseline at size points, as a positive number of points.
public float getLineHeight(float size)The recommended distance between two baselines at size points.
public abstract boolean canDisplay(char c)True when the font can draw c with a glyph of its own.
protected abstract FontState createState()Creates the per-document state that tracks which glyphs a document uses and writes the font objects.

Inherited methods

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.

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 FontState createState()
Creates the per-document state that tracks which glyphs a document uses and writes the font objects. Internal.