/** * Factory for AnkiFont creation. Creates a typeface wrapper from a font file representing. * * @param ctx Activity context, needed to access assets * @param path Path to typeface file, needed when this is a custom font. * @param fromAssets True if the font is to be found in assets of application * @return A new AnkiFont object or null if the file can't be interpreted as typeface. */ public static AnkiFont createAnkiFont(Context ctx, String path, boolean fromAssets) { File fontfile = new File(path); String name = Utils.splitFilename(fontfile.getName())[0]; String family = name; List<String> attributes = new ArrayList<>(); if (fromAssets) { path = fAssetPathPrefix.concat(fontfile.getName()); } Typeface tf = getTypeface(ctx, path); if (tf == null) { // unable to create typeface return null; } if (tf.isBold() || name.toLowerCase(Locale.US).contains("bold")) { attributes.add("font-weight: bolder;"); family = family.replaceFirst("(?i)-?Bold", ""); } else if (name.toLowerCase(Locale.US).contains("light")) { attributes.add("font-weight: lighter;"); family = family.replaceFirst("(?i)-?Light", ""); } else { attributes.add("font-weight: normal;"); } if (tf.isItalic() || name.toLowerCase(Locale.US).contains("italic")) { attributes.add("font-style: italic;"); family = family.replaceFirst("(?i)-?Italic", ""); } else if (name.toLowerCase(Locale.US).contains("oblique")) { attributes.add("font-style: oblique;"); family = family.replaceFirst("(?i)-?Oblique", ""); } else { attributes.add("font-style: normal;"); } if (name.toLowerCase(Locale.US).contains("condensed") || name.toLowerCase(Locale.US).contains("narrow")) { attributes.add("font-stretch: condensed;"); family = family.replaceFirst("(?i)-?Condensed", ""); family = family.replaceFirst("(?i)-?Narrow(er)?", ""); } else if (name.toLowerCase(Locale.US).contains("expanded") || name.toLowerCase(Locale.US).contains("wide")) { attributes.add("font-stretch: expanded;"); family = family.replaceFirst("(?i)-?Expanded", ""); family = family.replaceFirst("(?i)-?Wide(r)?", ""); } AnkiFont createdFont = new AnkiFont(name, family, attributes, path); // determine if override font or default font SharedPreferences preferences = AnkiDroidApp.getSharedPrefs(ctx); String defaultFont = preferences.getString("defaultFont", ""); boolean overrideFont = preferences.getString("overrideFontBehavior", "0").equals("1"); if (defaultFont.equalsIgnoreCase(name)) { if (overrideFont) { createdFont.setAsOverride(); } else { createdFont.setAsDefault(); } } return createdFont; }
public AnkiFont(Context ctx, String path, boolean fromAssets) { mPath = path; File fontfile = new File(mPath); mName = Utils.removeExtension(fontfile.getName()); mFamily = mName; if (fromAssets) { mPath = fAssetPathPrefix.concat(fontfile.getName()); } Typeface tf = getTypeface(ctx, mPath); if (tf.isBold() || mName.toLowerCase().contains("bold")) { mWeight = "font-weight: bolder;"; mFamily = mFamily.replaceFirst("(?i)-?Bold", ""); } else if (mName.toLowerCase().contains("light")) { mWeight = "font-weight: lighter;"; mFamily = mFamily.replaceFirst("(?i)-?Light", ""); } else { mWeight = "font-weight: normal;"; } if (tf.isItalic() || mName.toLowerCase().contains("italic")) { mStyle = "font-style: italic;"; mFamily = mFamily.replaceFirst("(?i)-?Italic", ""); } else if (mName.toLowerCase().contains("oblique")) { mStyle = "font-style: oblique;"; mFamily = mFamily.replaceFirst("(?i)-?Oblique", ""); } else { mStyle = "font-style: normal;"; } if (mName.toLowerCase().contains("condensed") || mName.toLowerCase().contains("narrow")) { mStretch = "font-stretch: condensed;"; mFamily = mFamily.replaceFirst("(?i)-?Condensed", ""); mFamily = mFamily.replaceFirst("(?i)-?Narrow(er)?", ""); } else if (mName.toLowerCase().contains("expanded") || mName.toLowerCase().contains("wide")) { mStretch = "font-stretch: expanded;"; mFamily = mFamily.replaceFirst("(?i)-?Expanded", ""); mFamily = mFamily.replaceFirst("(?i)-?Wide(r)?", ""); } else { mStretch = "font-stretch: normal;"; } mFamily = mFamily.replaceFirst("(?i)-?Regular", ""); }
public static <T extends View> T applyDefaultStyles(T view) { if (view == null || view.getContext() == null || view.getContext().isRestricted()) { return view; } if (view instanceof TextView) { TextView text = (TextView) view; Typeface typeface = text.getTypeface(); if (typeface == null) { text.setTypeface(FontLoader.loadTypeface(view.getContext(), HoloFont.ROBOTO_REGULAR.font)); return view; } HoloFont font; boolean isBold = typeface.isBold(), isItalic = typeface.isItalic(); if (isBold && isItalic) { font = HoloFont.ROBOTO_BOLD_ITALIC; } else if (isBold && !isItalic) { font = HoloFont.ROBOTO_BOLD; } else if (!isBold && isItalic) { font = HoloFont.ROBOTO_ITALIC; } else { font = HoloFont.ROBOTO_REGULAR; } if (!font.ignore) { typeface = FontLoader.loadTypeface(view.getContext(), font.font); if (typeface != null) { text.setTypeface(typeface); } } } if (view instanceof ViewGroup) { ViewGroup group = (ViewGroup) view; for (int i = 0; i < group.getChildCount(); i++) { FontLoader.applyDefaultStyles(group.getChildAt(i)); } } return view; }