Supporting complex text layout with OpenType fonts in Android
Asked Answered
A

2

1

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.

Affinal answered 18/12, 2014 at 4:26 Comment(3)
I have successfully rendered Sanskrit and Tibetan in Android, I think it supports most languages out of the box. On older androids I think I had to install TibetanMachine.ttf to display Tibetan correctly. But not now. I think you just use Java function to render a string of text, that's it.Sandeesandeep
Thanks for your comment. I found this that talked about the old way to do it: openideals.com/2009/11/20/… What changed? How are Tibetan and Sanskrit now supported?Affinal
Lollipop now has support for open type font features.Valdis
B
2

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.

Bonsai answered 7/1, 2015 at 6:40 Comment(1)
Nice! That's a lot more useful information than I have seen anywhere else. It looks like most of your programming is in Perl and Ruby? Any hints for using this in Android?Affinal
R
0

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"!

Rooky answered 27/12, 2014 at 20:26 Comment(5)
Although I am still not quite sure how to apply this and there isn't enough detail to mark this question as solved, I appreciate you taking the time to help with it. I'll gladly give you the bounty rather than let it run out. Thank you.Affinal
@Affinal thanks! Maybe I can help more: in places where CTL is natively supported, do you do anything specific to use Mongolian script, or it "just works"?Rooky
In environments that support OpenType, using a well made Mongolian font makes the script display correctly without doing anything else. I don't understand all of the details of how these smart fonts work, but they have substitution tables and rules built into them that choose the correct glyph according to the context. For example, in Mongolian a letter is written differently depending on whether it appears at the beginning, middle, or end of a word. An OpenType font can recognize this and choose the glyph accordingly.Affinal
I just found this: blog.sqisland.com/2014/11/…Affinal
I read that article before: it just shows what I said in my answer with an example!Rooky

© 2022 - 2024 — McMap. All rights reserved.