Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Typeface Font Default / Fallback

Tags:

android

fonts

I draw (canvas) a text using a custom typeface (precisely: I use a custom font). However, this font doesn't support many characters and thus some (unsupported) characters are looking differently. Now I am asking myself the following questions:

  • How does an unsupported character look like - does the look depend on a system setting / is the look the same on all devices?
  • What (default) font is used for unsupported characters? (do the unsupported letters have the same dimensions as the supported ones in the end?)
like image 633
sjkm Avatar asked Sep 13 '25 15:09

sjkm


1 Answers

I digged through the Android code and would like to post my findings:

A Typeface.class instance is created using the static create-methods of the Typeface.class (createFromAsset/createFromFile/create/etc.). The instance then holds a list of fonts (unmodifiable List<Font>). The first element of this list is the main font (specified) and the other elements (index: 1 - (size-1)) are default fonts which are retrieved from the FontLoader.class (getFallBackFonts())

Among others, the Paint.class / Canvas.class (drawText()) use a Typeface.class instance to draw the texts (to measure characters / to draw / etc.). They try to use the main font (Typeface.mFonts[0]) when possible. If a character cannot be found in the main font then they try to use a fallback font (Typeface.mFonts[1]-Typeface.mFonts[size-1]) to measure and draw the character.

Conclusion

You can use a Typeface.class instance if you want to use a custom font. If your custom font doesn't support certain characters that you want to draw (/that you use in your text) then the system tries to make use of fallback-fonts. These fallback fonts are loaded from the system by the FontLoader.class (from a system-xml).

like image 65
sjkm Avatar answered Sep 16 '25 05:09

sjkm