Search in sources :

Example 1 with HeaderTable

use of com.tom_roush.fontbox.ttf.HeaderTable in project PdfBox-Android by TomRoush.

the class TrueTypeEmbedder method createFontDescriptor.

/**
 * Creates a new font descriptor dictionary for the given TTF.
 */
private PDFontDescriptor createFontDescriptor(TrueTypeFont ttf) throws IOException {
    PDFontDescriptor fd = new PDFontDescriptor();
    fd.setFontName(ttf.getName());
    OS2WindowsMetricsTable os2 = ttf.getOS2Windows();
    PostScriptTable post = ttf.getPostScript();
    // Flags
    fd.setFixedPitch(post.getIsFixedPitch() > 0 || ttf.getHorizontalHeader().getNumberOfHMetrics() == 1);
    int fsSelection = os2.getFsSelection();
    fd.setItalic(((fsSelection & (ITALIC | OBLIQUE)) != 0));
    switch(os2.getFamilyClass()) {
        case OS2WindowsMetricsTable.FAMILY_CLASS_CLAREDON_SERIFS:
        case OS2WindowsMetricsTable.FAMILY_CLASS_FREEFORM_SERIFS:
        case OS2WindowsMetricsTable.FAMILY_CLASS_MODERN_SERIFS:
        case OS2WindowsMetricsTable.FAMILY_CLASS_OLDSTYLE_SERIFS:
        case OS2WindowsMetricsTable.FAMILY_CLASS_SLAB_SERIFS:
            fd.setSerif(true);
            break;
        case OS2WindowsMetricsTable.FAMILY_CLASS_SCRIPTS:
            fd.setScript(true);
            break;
        default:
            break;
    }
    fd.setFontWeight(os2.getWeightClass());
    fd.setSymbolic(true);
    fd.setNonSymbolic(false);
    // ItalicAngle
    fd.setItalicAngle(post.getItalicAngle());
    // FontBBox
    HeaderTable header = ttf.getHeader();
    PDRectangle rect = new PDRectangle();
    float scaling = 1000f / header.getUnitsPerEm();
    rect.setLowerLeftX(header.getXMin() * scaling);
    rect.setLowerLeftY(header.getYMin() * scaling);
    rect.setUpperRightX(header.getXMax() * scaling);
    rect.setUpperRightY(header.getYMax() * scaling);
    fd.setFontBoundingBox(rect);
    // Ascent, Descent
    HorizontalHeaderTable hHeader = ttf.getHorizontalHeader();
    fd.setAscent(hHeader.getAscender() * scaling);
    fd.setDescent(hHeader.getDescender() * scaling);
    // CapHeight, XHeight
    if (os2.getVersion() >= 1.2) {
        fd.setCapHeight(os2.getCapHeight() * scaling);
        fd.setXHeight(os2.getHeight() * scaling);
    } else {
        Path capHPath = ttf.getPath("H");
        if (capHPath != null) {
            RectF capHPathBounds = new RectF();
            capHPath.computeBounds(capHPathBounds, true);
            fd.setCapHeight(Math.round(capHPathBounds.bottom) * scaling);
        } else {
            // estimate by summing the typographical +ve ascender and -ve descender
            fd.setCapHeight((os2.getTypoAscender() + os2.getTypoDescender()) * scaling);
        }
        Path xPath = ttf.getPath("x");
        if (xPath != null) {
            RectF xPathBounds = new RectF();
            xPath.computeBounds(xPathBounds, true);
            fd.setXHeight(Math.round(xPathBounds.bottom) * scaling);
        } else {
            // estimate by halving the typographical ascender
            fd.setXHeight(os2.getTypoAscender() / 2.0f * scaling);
        }
    }
    // StemV - there's no true TTF equivalent of this, so we estimate it
    fd.setStemV(fd.getFontBoundingBox().getWidth() * .13f);
    return fd;
}
Also used : Path(android.graphics.Path) RectF(android.graphics.RectF) PostScriptTable(com.tom_roush.fontbox.ttf.PostScriptTable) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) HeaderTable(com.tom_roush.fontbox.ttf.HeaderTable) HorizontalHeaderTable(com.tom_roush.fontbox.ttf.HorizontalHeaderTable) OS2WindowsMetricsTable(com.tom_roush.fontbox.ttf.OS2WindowsMetricsTable) HorizontalHeaderTable(com.tom_roush.fontbox.ttf.HorizontalHeaderTable)

Aggregations

Path (android.graphics.Path)1 RectF (android.graphics.RectF)1 HeaderTable (com.tom_roush.fontbox.ttf.HeaderTable)1 HorizontalHeaderTable (com.tom_roush.fontbox.ttf.HorizontalHeaderTable)1 OS2WindowsMetricsTable (com.tom_roush.fontbox.ttf.OS2WindowsMetricsTable)1 PostScriptTable (com.tom_roush.fontbox.ttf.PostScriptTable)1 PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)1