Supporting complex text layout with OpenType fonts in Android

1.7k Views Asked by At

What I would like

I would like to put an OpenType font in my assets folder and use Typeface.createFromAsset to render Unicode text in my Android application for a language that uses complex text layout (CTL).

Background

I want to do this for traditional Mongolian script, but it is the same issue (so I hear) for other languages like Thai, Arabic, Indic languages, and many minority languages. An OpenType font already includes the information necessary to properly display the text. The problem is that not all software supports OpenType rendering. Some do, but others don't. From what I can gather, Android didn't used to, but maybe now does?

My question

Android Lollipop is out now. Is there any support for OpenType fonts in Android?

If there is still nothing easy, then what is the process? I have heard of things like Pango and Harfbuzz (which claims to work with Android) but I haven't found any simple instructions or tutorials for how to implement them. The closest thing I have found is an Indic text renderer, but it is far from easy to understand.

Related questions on StackOverflow

These questions show that there is interest in this topic, but in my opinion they haven't been satisfactorily answered. Some of them are also outdated. I want to know if there is a current solution.

What I have tried

I have written my own rendering engine to display traditional Mongolian Unicode text. But writing a rendering engine for a specific language is reinventing the wheel if there are already OpenType fonts available. Also, it is not easily adaptable to other languages.

2

There are 2 best solutions below

5
On

I have no experience with Mongolian in particular, but Lollipop finally introduced Paint.setFontFeatureSettings() which takes a string that follows the CSS format, providing "low-level control over OpenType font features"!

1
On

If you want to use Harfbuzz to do this, I have written a simple guide to implementing a text shaper using Harfbuzz: http://www.simon-cozens.org/content/duffers-guide-fontconfig-and-harfbuzz

That should get you from font selection to knowing which glyphs should be used and where to put them in the output.