Class PdfMeasurer

java.lang.Object
com.codename1.wordprocessing.PdfMeasurer
All Implemented Interfaces:
Measurer

public final class PdfMeasurer extends Object implements Measurer

Measures with the fonts a PDF is drawn in, so a document paginates identically on screen and on paper.

Out of the box every style maps to one of the standard PDF fonts: serif families to Times, monospaced ones to Courier and everything else to Helvetica. Those cover only Western European characters, so a document in any other script should register a TrueType font for each family it uses:

PdfMeasurer m = new PdfMeasurer();
try {
    // fonts bundled with the application, in its resources
    InputStream regular = CN.getResourceAsStream("/NotoSerif-Regular.ttf");
    InputStream bold = CN.getResourceAsStream("/NotoSerif-Bold.ttf");
    if (regular != null && bold != null) {
        m.register("Serif", false, false, PdfFont.loadTrueType(Util.readInputStream(regular)));
        m.register("Serif", true, false, PdfFont.loadTrueType(Util.readInputStream(bold)));
    }
} catch (IOException e) {
    Log.e(e);
}
DocumentLayout layout = new DocumentLayout(new Document(), m);
Log.p("pages: " + layout.getPageCount());
  • Constructor Details

    • PdfMeasurer

      public PdfMeasurer()
  • Method Details

    • register

      public void register(String family, boolean bold, boolean italic, PdfFont font)

      Registers a font for a family and face. A face without its own registration falls back to the family's regular font, and a family without any to a standard font.

      Parameters
      • family: the family name a TextStyle uses, such as Serif or Calibri

      • bold: whether this is the bold face

      • italic: whether this is the italic face

      • font: the font

    • font

      public PdfFont font(TextStyle style)

      The font a style is drawn in.

      Parameters
      • style: the style
      Returns

      the font, never null

    • drawnSize

      public static float drawnSize(TextStyle style)

      The size text of a style is drawn at: smaller for superscript and subscript.

      Parameters
      • style: the style
      Returns

      the size in points

    • width

      public float width(String text, TextStyle style)
      Description copied from interface: Measurer

      The advance width of text drawn in a style.

      Parameters
      • text: the text, never containing a line break

      • style: the style

      Returns

      the width in points

      Specified by:
      width in interface Measurer
    • ascent

      public float ascent(TextStyle style)
      Description copied from interface: Measurer

      The distance from the top of a line of this style to its baseline.

      Parameters
      • style: the style
      Returns

      the ascent in points

      Specified by:
      ascent in interface Measurer
    • descent

      public float descent(TextStyle style)
      Description copied from interface: Measurer

      The distance from the baseline to the bottom of a line of this style.

      Parameters
      • style: the style
      Returns

      the descent in points, positive

      Specified by:
      descent in interface Measurer