Is there a way to fall back to Typeface.SERIF in a StaticLayout?
Asked Answered
A

1

15

I'm creating a custom View class that displays text using a StaticLayout with a custom typeface loaded from a .ttf asset file. The basic look of the typeface is a serif font and the TrueType properties in the font file indicate that it's serif. Nevertheless, for characters not in the font, Android falls back to a sans-serif typeface (it looks like Roboto or Noto, depending on the Android version).

Is there a way to control this so that the Android layout engine falls back to a serif typeface? (Like maybe some magic string to pass to Paint#setFontFeatureSettings(), or perhaps a way to specify a stack of typefaces for use in a Paint?)

Any solution would have to work when the app is distributed, so playing games with Android's system configuration files is not an option. Also, I'm aware of this question (that is still unanswered after four years), but my problem is a bit different, although the solution to both issues might very well be the same.

Anderer answered 12/8, 2016 at 6:54 Comment(0)
G
0

As of Lollipop, there's a hidden class called FontFamily and a hidden method called Typeface.createFromFamilies. If you're very careful, you can probably use this stuff via reflection or other trickery, but be prepared to fallback to the public API.

Gangplank answered 3/11, 2016 at 7:10 Comment(2)
Unfortunately, as of Android Pie, the FontFamily class appears to be in the dark gray list, so isn't accessible even through reflection. (See developer.android.com/about/versions/pie/…).Anderer
Yes, time has proven the wisdom of my advice of being able to fall back on the public API lol. It's unfortunate that so much of the Android framework is hidden, often without any good reason. For example, there's a public method that creates a ParcelFileDescriptor that duplicates a FileDescriptor, but no public method that adopts one. That's functionality that has existed in ParcelFileDescriptor from the very beginning, but remains hidden to this day. Android is full of this nonsense.Gangplank

© 2022 - 2024 — McMap. All rights reserved.