开发者

Android: Find out which font file is appropriate for the characters I want to display

开发者 https://www.devze.com 2023-04-11 03:38 出处:网络
I am maintaining an Android app that people use to display strings in various exotic languages like Tibetan or old Greek. Because Android devices come with very few fonts, users can put font files on

I am maintaining an Android app that people use to display strings in various exotic languages like Tibetan or old Greek. Because Android devices come with very few fonts, users can put font files on the SD card, and the app will use them.

QUESTION: Given a string, how can I automatically decide which font file is the most appropriate, so that this string appears without characters being replaced with squares/boxes?

Notes:

  • Each string is in one language.
  • Strings are displayed in a WebView.
  • Custom fonts work, the only problem is deciding which font file to use.
  • Instead of a single font, it could provide a list of fonts that are acceptable for that string.

Unnecessary context, for the curious: I am t开发者_开发技巧rying to develop this feature: http://code.google.com/p/ankidroid/issues/detail?id=779


UPDATE: I ended up creating the Antisquare Open Source library based on Mostafa's idea.

It has a getSuitableFonts method which is blazingly fast.


Android by itself does not provide enough for such a task. Loading and rendering fonts in Android happens in Skia, which is written in C. Skia detects if a character can't be found in a font and falls back to another font for such characters (not the whole string). That's how Japanese, Hebrew, or Arabic text is shown in Android and that's exactly why these scripts don't have bold face! (Their font is selected through fallback and fallback only selects one font file.)

Unfortunately, this mechanism is not provided in APIs and you have to build similar thing on your own. It seems complicated, but is easier than it looks. All you have to do is:

  1. Prepare lists of characters available in each font file.
  2. For every string find the font that has more characters of the string.

Getting list of characters in each font

You don't have to do this on-the-fly in your Android app. You can prepare the list of characters in each font and put these lists in your app. I say that because this is way easier with tools that may not be available in Android. I would do that through Python scripting in a font app (most serious font tools have awesome Python scripting environments), but these apps are expensive and are for serious type designers. Since you're an Android developer, I recommend using sfntly, a library in Java and C++. Doing what you need (getting a list of Unicode characters available in a font file) is easy with sfntly. This sample works with CMap tables (tables that hold character to glyph mapping) and should be a good starting point for you.

Now the interesting part is that snftly is in Java and you may be able to include that in your Android app and do everything automatically. That's awesome by I recommend you start by getting familiar with snftly.

Selecting the font

After the previous part you'll have a list of Unicode character for every font, and based on these lists selecting the font file that provides most characters of every string is trivial.

0

精彩评论

暂无评论...
验证码 换一张
取 消