I have a problem while testing fonts I supply in an asset/fonts folder. I simply placed some TTF font files in this folder. They were chosen from fonts in my font library without caring about their provenance. Some of them were my own fonts(GAMZ). The files are scanned by the class fontProvider which maintains brief records of the fonts in an ArrayList.
public class Font {
int style;
String file;
public Font(String file, int style) {
this.style = style;
this.file = file;
}
public String name() {
int ixdot = file.indexOf('.');
String g;
if (ixdot < 0) g = file;
else g = file.substring(0, ixdot);
return g;
}
}
public class FontProvider {
private ArrayList<Font> fontlist;
private MainActivity mainactivity;
public FontProvider(MainActivity mainactivity) {
this.mainactivity = mainactivity ;
getFonts();
sysoutfonts(); // not supplied but lists the fonts with System.out calls
}
public void getFonts() {
String fontfiles[] = null;
AssetManager assetManager = mainactivity.getAssets();
Typeface tf = null;
try {
fontfiles = assetManager.list("fonts");
} catch (IOException e) {}
for (String file : fontfiles) {
tf = Typeface.createFromAsset(assetManager, "fonts/" + file);
if (tf != null) {
Font f = new Font(file, tf.getStyle());
fontlist.add(f);
}
}
fontlist.trimToSize();
}
}
There are other methods in fontProvider but they are not relevant and not shown here. When run on a USB connected API 19 tablet all is OK and sysoutprint() reports, on my adblog:
Chalk (Normal), GAMZ One (Bold), GAMZ Teacher's Pet Printed Cards (Bold), GAMZ Teacher's Pet Rounded (Bold), GAMZ Two (Bold), Kids (Normal), Southern Normal(Normal)
However, when I try it on my API 23 tablet, it crashes. Always being neurotic about any routine that involves external resources, I applied a try/catch to the Typeface.createFromAsset(...) call. ie:
try {
tf = Typeface.createFromAsset(assetManager, "fonts/" + file);
} catch (Exception e) {
System.out.println("Exception e: " + e.getMessage());
}
The API 23 tablet no longer crashes bout sysoutprint() reports:
GAMZ One (Bold), GAMZ Teacher's Pet Printed Cards (Bold), GAMZ Teacher's Pet Rounded (Bold), GAMZ Two (Bold), Kids (Normal)
Chalk and Southern are missing! Instead the e.getMessage() reports, for these TTF files:
Exception e: Font asset not found fonts/Southern Normal.ttf
So: No error in API 19 but these fonts won't load into a Typeface in API23!
Has there been some subtle change to the API implementation, since Level 19, of the Typeface.createFromAsset() method(), even a bug?
PS Typeface.createFromFile() behaves in the same way.
PPS. I'm sure I can find a workaround for this by choosing different, but similar, fonts for my final app, even if that involves designing the fonts myself.