Search in sources :

Example 1 with AnkiFont

use of com.ichi2.anki.AnkiFont in project Anki-Android by Ramblurr.

the class CustomFontsReviewerExt method getOverrideFontStyle.

/**
     * Returns the CSS used to set the override font.
     * @return the override font style, or the empty string if no override font is set 
     */
private String getOverrideFontStyle(Context context, Map<String, AnkiFont> customFontsMap) {
    if (mOverrideFontStyle == null) {
        SharedPreferences preferences = AnkiDroidApp.getSharedPrefs(context);
        AnkiFont overrideFont = customFontsMap.get(preferences.getString("overrideFont", null));
        if (overrideFont != null) {
            mOverrideFontStyle = "BODY, .card, * { " + overrideFont.getCSS() + " }\n";
        } else {
            mOverrideFontStyle = "";
        }
    }
    return mOverrideFontStyle;
}
Also used : AnkiFont(com.ichi2.anki.AnkiFont) SharedPreferences(android.content.SharedPreferences)

Example 2 with AnkiFont

use of com.ichi2.anki.AnkiFont in project Anki-Android by Ramblurr.

the class CustomFontsReviewerExt method getDefaultFontStyle.

/**
     * Returns the CSS used to set the default font.
     * @return the default font style, or the empty string if no default font is set 
     */
private String getDefaultFontStyle(Context context, Map<String, AnkiFont> customFontsMap) {
    if (mDefaultFontStyle == null) {
        SharedPreferences preferences = AnkiDroidApp.getSharedPrefs(context);
        AnkiFont defaultFont = customFontsMap.get(preferences.getString("defaultFont", null));
        if (defaultFont != null) {
            mDefaultFontStyle = "BODY { " + defaultFont.getCSS() + " }\n";
        } else {
            mDefaultFontStyle = "";
        }
    }
    return mDefaultFontStyle;
}
Also used : AnkiFont(com.ichi2.anki.AnkiFont) SharedPreferences(android.content.SharedPreferences)

Example 3 with AnkiFont

use of com.ichi2.anki.AnkiFont in project Anki-Android by Ramblurr.

the class Utils method getCustomFonts.

/** Returns a list of files for the installed custom fonts. */
public static List<AnkiFont> getCustomFonts(Context context) {
    String deckPath = AnkiDroidApp.getCurrentAnkiDroidDirectory();
    String fontsPath = deckPath + "/fonts/";
    File fontsDir = new File(fontsPath);
    int fontsCount = 0;
    File[] fontsList = null;
    if (fontsDir.exists() && fontsDir.isDirectory()) {
        fontsCount = fontsDir.listFiles().length;
        fontsList = fontsDir.listFiles();
    }
    String[] ankiDroidFonts = null;
    try {
        ankiDroidFonts = context.getAssets().list("fonts");
    } catch (IOException e) {
        Log.e(AnkiDroidApp.TAG, "Error on retrieving ankidroid fonts: " + e);
    }
    List<AnkiFont> fonts = new ArrayList<AnkiFont>();
    for (int i = 0; i < fontsCount; i++) {
        String filePath = fontsList[i].getAbsolutePath();
        String filePathExtension = getFileExtension(filePath);
        for (String fontExtension : FONT_FILE_EXTENSIONS) {
            // Go through the list of allowed extensios.
            if (filePathExtension.equalsIgnoreCase(fontExtension)) {
                // This looks like a font file.
                AnkiFont font = AnkiFont.createAnkiFont(context, filePath, false);
                if (font != null) {
                    fonts.add(font);
                }
                // No need to look for other file extensions.
                break;
            }
        }
    }
    for (int i = 0; i < ankiDroidFonts.length; i++) {
        // Assume all files in the assets directory are actually fonts.
        AnkiFont font = AnkiFont.createAnkiFont(context, ankiDroidFonts[i], true);
        if (font != null) {
            fonts.add(font);
        }
    }
    return fonts;
}
Also used : AnkiFont(com.ichi2.anki.AnkiFont) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 4 with AnkiFont

use of com.ichi2.anki.AnkiFont in project Anki-Android by Ramblurr.

the class CustomFontsReviewerExt method getCustomFontsStyle.

/**
     * Returns the CSS used to handle custom fonts.
     * <p>
     * Custom fonts live in fonts directory in the directory used to store decks.
     * <p>
     * Each font is mapped to the font family by the same name as the name of the font without the extension.
     */
private static String getCustomFontsStyle(Map<String, AnkiFont> customFontsMap) {
    StringBuilder builder = new StringBuilder();
    for (AnkiFont font : customFontsMap.values()) {
        builder.append(font.getDeclaration());
        builder.append('\n');
    }
    return builder.toString();
}
Also used : AnkiFont(com.ichi2.anki.AnkiFont)

Example 5 with AnkiFont

use of com.ichi2.anki.AnkiFont in project Anki-Android by Ramblurr.

the class CustomFontsReviewerExt method getCustomFontsMap.

/**
     * Returns a map from custom fonts names to the corresponding {@link AnkiFont} object.
     *
     * <p>The list of constructed lazily the first time is needed.
     */
private static Map<String, AnkiFont> getCustomFontsMap(Context context) {
    List<AnkiFont> fonts = Utils.getCustomFonts(context);
    Map<String, AnkiFont> customFontsMap = new HashMap<String, AnkiFont>();
    for (AnkiFont f : fonts) {
        customFontsMap.put(f.getName(), f);
    }
    return customFontsMap;
}
Also used : AnkiFont(com.ichi2.anki.AnkiFont) HashMap(java.util.HashMap)

Aggregations

AnkiFont (com.ichi2.anki.AnkiFont)5 SharedPreferences (android.content.SharedPreferences)2 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ZipFile (java.util.zip.ZipFile)1