Use custom TypeFace on Android L

After updating my Nexus 5 to the new Android Lollipop my app doesn’t load anymore custom fonts, instead it goes for the default one. It looks like it just ignores setTypeface calls without logging anything in logcat.
After a lot of tries and a lot of googling I found out something useful that allow me to make it works.

The problem comes from how the font is encoded, for some reason previous version of the system doesn’t mind it, Android 5.0 does.
To find out if your font has this kind of problem you can use OpenType Sanitizer tool and check if the following warning is generated:

WARNING at ots-read-only/src/ bad id_range_offset

Actually I had some problem while compiling OTS, so I skip to the apply the solution without verifing the problem, fortunately it works.
If you got such encoding problem what you have to do is to reencode your font using TTX.

You just have to call ttx on your font to decode it, and then one more time on the decoded ttx to reencode it properly; so:

$ fonttools/build/scripts-2.7/ttx yourfont.ttf
$ fonttools/build/scripts-2.7/ttx yourfont.ttx

Then you get another ttf (or whatever font format you had before), maybe renamed as something like yourfont#1.ttf to solve the naming conflict. Now it should be properly encoded, replace it in your Assets and it should work.

For more information you can visit the bug report page. Which also says that the bug is fixed, anyway this will be in next release, the problem still for 5.0.

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *