Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public final class PdfMeasurer
- Object
- PdfMeasurer
ImplementsMeasurer
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());Constructors
public PdfMeasurer() |
Methods
public void register(String family, boolean bold, boolean italic, PdfFont font) | Registers a font for a family and face. |
public PdfFont font(TextStyle style) | The font a style is drawn in. |
public static float drawnSize(TextStyle style) | The size text of a style is drawn at: smaller for superscript and subscript. |
public float width(String text, TextStyle style) | The advance width of text drawn in a style. |
public float ascent(TextStyle style) | The distance from the top of a line of this style to its baseline. |
public float descent(TextStyle style) | The distance from the baseline to the bottom of a line of this style. |
Inherited methods
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
familyString- the family name a
TextStyleuses, such asSeriforCalibri boldboolean- whether this is the bold face
italicboolean- whether this is the italic face
fontPdfFont- the font
font
public PdfFont font(TextStyle style)The font a style is drawn in.
Parameters
styleTextStyle- 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
styleTextStyle- the style
Returns
the size in points
width
public float width(String text, TextStyle style)The advance width of text drawn in a style.
Parameters
textString- the text, never containing a line break
styleTextStyle- the style
Returns
the width in points
ascent
public float ascent(TextStyle style)The distance from the top of a line of this style to its baseline.
Parameters
styleTextStyle- the style
Returns
the ascent in points
descent
public float descent(TextStyle style)The distance from the baseline to the bottom of a line of this style.
Parameters
styleTextStyle- the style
Returns
the descent in points, positive