Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 310
Alex Semeniuk Avatar asked Jan 26 '16 09:01

Alex Semeniuk


People also ask

How do I read TTF files?

You can open a TTF file in Microsoft Windows Font Viewer (Windows), Apple Font Book (Mac), or iFont (iOS, Android). Before opening a TTF file in Windows Font Viewer, you must place the file in the C:/​Windows/​Fonts directory. Otherwise, Windows will not recognize the file as a valid font file.

What is TFF font file?

(TrueType Font file) A TrueType font file in Windows that contains the mathematical outlines of each character in the font. In the Mac, the icon of a TrueType file looks like a document, dog-eared on the upper left, with three A's on it. TTF files are stored in the \WINDOWS\SYSTEM or \WINDOWS\FONTS folders.

What is kerning tables?

The 'kern' table contains the values that adjust the intercharacter spacing for glyphs in a font. It can have multiple subtables in varied formats that can contain information for vertical or horizontal text. Kerning values are used to adjust intercharacter spacing.

What are kerning pairs?

Kerning is the manual adjustment of the spacing between two specific glyphs. A kerning pair is the same adjustment, but determined by the type designer—with the instructions embedded directly within in a font file.


1 Answers

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)

like image 69
Jared Rummler Avatar answered Sep 17 '22 18:09

Jared Rummler