/** * Retrieve the fonts from the asset and the system folder. * * @return A Map mapping the name of the font to the Typeface. If the name can't be retrieved the * file name will be used (e.g. arial.ttf). */ public static SortedSet<RTTypeface> getFonts(Context context) { /* * Fonts from the assets folder */ Map<String, String> assetFonts = getAssetFonts(context); AssetManager assets = context.getResources().getAssets(); for (String fontName : assetFonts.keySet()) { String filePath = assetFonts.get(fontName); if (!ALL_FONTS.contains(fontName)) { try { Typeface typeface = Typeface.createFromAsset(assets, filePath); ALL_FONTS.add(new RTTypeface(fontName, typeface)); } catch (Exception e) { // this can happen if we don't have access to the font or it's not a font or... } } } /* * Fonts from the system */ Map<String, String> systemFonts = getSystemFonts(); for (String fontName : systemFonts.keySet()) { String filePath = systemFonts.get(fontName); if (!ALL_FONTS.contains(fontName)) { try { Typeface typeface = Typeface.createFromFile(filePath); ALL_FONTS.add(new RTTypeface(fontName, typeface)); } catch (Exception e) { // this can happen if we don't have access to the font or it's not a font or... } } } return ALL_FONTS; }
/** * Returns the RTTypeface for a specific font identified by name. This assumes that the fonts have * already been loaded. * * @return The RTTypeface for a specific font name or Null of no such font exists. */ public static RTTypeface getTypeface(String fontName) { return ALL_FONTS.get(fontName); }