androidfontstruetypeandroid-fonts

How to read kerning pairs table from TTF file in Android


I am currently drawing text on Canvas while using external (non-standard) font, loaded from TTF file. I want to enable kerning for the text I am displaying.

What I want to know is if there is a possibility to read kerning pairs from typeface using Android API.


Solution

  • What I want to know is if there is a possibility to read kerning pairs from typeface using Android API.

    There is no public API to read kerning pairs from a TTF file. However, I pulled the relevant code from Apache FOP and you can read the kerning pairs using this library.

    Example usage:

    TTFFile file = TTFFile.open(getAssets().open("fonts/font.ttf"));
    Map<Integer, Map<Integer, Integer>> kerning = file.getKerning();
    

    You can also retrieve other metadata. Example:

    TTFFile ttfFile = TTFFile.open(new File("/system/fonts/Roboto-Regular.ttf"));
    
    String name = ttfFile.getFullName();             // "Roboto Regular"
    String family = ttfFile.getSubFamilyName();      // "Regular"
    int fontWeight = ttfFile.getWeightClass();       // 400
    String copyright = ttfFile.getCopyrightNotice(); // "Font data copyright Google 2014"
    

    I want to enable kerning for the text I am displaying.

    See:

    How to adjust text kerning in Android TextView?

    setLetterSpacing(float)