Example usage for org.apache.pdfbox.pdmodel.font PDFontDescriptor getAscent

List of usage examples for org.apache.pdfbox.pdmodel.font PDFontDescriptor getAscent

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel.font PDFontDescriptor getAscent.

Prototype

public float getAscent() 

Source Link

Document

This will get the ascent for the font.

Usage

From source file:com.planbase.pdf.layoutmanager.TextStyle.java

License:Apache License

private TextStyle(PDFont f, float sz, Color tc) {
    if (f == null) {
        throw new IllegalArgumentException("Font must not be null");
    }//from  w w w.j  a  v a2s  . co  m
    if (tc == null) {
        tc = Color.BLACK;
    }

    font = f;
    textColor = tc;
    fontSize = sz;
    // Somewhere it says that font units are 1000 times page units, but my tests with
    // PDType1Font.HELVETICA and PDType1Font.HELVETICA_BOLD from size 5-200 show that 960x is
    // pretty darn good.
    // TODO: Fix font-size for other fonts.
    factor = fontSize / 960f;
    PDFontDescriptor fontDescriptor = font.getFontDescriptor();
    float rawAscent = fontDescriptor.getAscent();
    float rawDescent = fontDescriptor.getDescent();
    // Characters look best with the descent size both above and below.  Also acts as a good
    // default leading.
    ascent = rawAscent * factor;
    descent = rawDescent * -factor;
    leading = descent / 2;
    // height = ascent + descent + leading;

    float avgFontWidth = 500;
    try {
        avgFontWidth = font.getAverageFontWidth();
    } catch (IOException ioe) {
        //throw new IllegalStateException("IOException probably means an issue reading font metrics from the underlying font file used in this PDF", ioe);
        ; // just use default if there's an exception.
    }
    avgCharWidth = avgFontWidth * fontSize;
}