package com.dg.helpers;

import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;

import java.util.HashMap;

/**
 * Created by Daniel Cohen Gindi (danielgindi@gmail.com)
 *
 * This loads the specified font from the assets folder.
 * If the path specified does not contain path and extension
 *   - it will search for assets/fonts/[fontname].[ttf|otf]
 */
public class FontHelper
{
    private static FontHelper _instance;
    private Context _context;
    private HashMap<String, Typeface> _fontCache = new HashMap<String, Typeface>();

    public static FontHelper sharedInstance(Context context)
    {
        if (_instance == null)
        {
            _instance = new FontHelper(context.getApplicationContext());
        }
        return _instance;
    }

    private FontHelper(Context context)
    {
        _context = context;
    }

    public Typeface getFont(String fontFamily)
    {
        Typeface typeface = _fontCache.get(fontFamily);
        if (typeface == null)
        {
            typeface = loadFont(_context, fontFamily);
            if (typeface != null)
            {
                _fontCache.put(fontFamily, typeface);
            }
        }
        return typeface;
    }

    public static Typeface loadFont(Context context, String fontFamily)
    {
        if (!fontFamily.contains(".") && !fontFamily.contains("/"))
        {
            String ttfFontFamily = "fonts/" + fontFamily + ".ttf";

            try
            {
                return Typeface.createFromAsset(context.getResources().getAssets(), ttfFontFamily);
            }
            catch (Exception ignored)
            {
            }

            String otfFontFamily = "fonts/" + fontFamily + ".otf";

            try
            {
                return Typeface.createFromAsset(context.getResources().getAssets(), otfFontFamily);
            }
            catch (Exception ignored)
            {
            }
        }

        try
        {
            return Typeface.createFromAsset(context.getResources().getAssets(), fontFamily);
        }
        catch (Exception e)
        {
            Log.e("FontHelper", e.getMessage());
            return null;
        }
    }
}

@@ -878,49 +878,700 @@
     
-public Typeface getFont(String fontFamily
+/**%0A     * Loads the font using the default %3Ccode%3EsharedInstance()%3C/code%3E.%0A     * This caches the fonts for reuse.%0A     * @param fontFamily%0A     * @return The loaded Typeface or null if failed.%0A     */%0A    public Typeface getFont(String fontFamily)%0A    %7B%0A        return getFont(fontFamily, true, true);%0A    %7D%0A%0A    /**%0A     * Loads the font using the default %3Ccode%3EsharedInstance()%3C/code%3E.%0A     * @param fontFamily%0A     * @param loadFromCache Should the font be loaded from cache?%0A     * @param saveToCache Should the font be saved to cache?%0A     * @return The loaded Typeface or null if failed.%0A     */%0A    public Typeface getFont(String fontFamily, boolean loadFromCache, boolean saveToCache
 )%0A  
@@ -1601,16 +1601,32 @@
 peface =
+ loadFromCache ?
  _fontCa
@@ -1636,32 +1636,39 @@
 .get(fontFamily)
+ : null
 ;%0A        if (ty
@@ -1724,16 +1724,26 @@
 loadFont
+FromAssets
 (_contex
@@ -1766,32 +1766,47 @@
             if (
+saveToCache && 
 typeface != null
@@ -1939,39 +1939,1245 @@
     
-public static Typeface loadFont
+/**%0A     * Loads the font using the default %3Ccode%3EsharedInstance()%3C/code%3E.%0A     * This caches the fonts for reuse.%0A     * @param context%0A     * @param fontFamily%0A     * @return The loaded Typeface or null if failed.%0A     */%0A    public static Typeface getFont(Context context, String fontFamily)%0A    %7B%0A        return sharedInstance(context).getFont(fontFamily);%0A    %7D%0A%0A    /**%0A     * Loads the font using the default %3Ccode%3EsharedInstance()%3C/code%3E.%0A     * @param context%0A     * @param fontFamily%0A     * @param loadFromCache Should the font be loaded from cache?%0A     * @param saveToCache Should the font be saved to cache?%0A     * @return The loaded Typeface or null if failed.%0A     */%0A    public static Typeface getFont(Context context,%0A                                   String fontFamily,%0A                                   boolean loadFromCache,%0A                                   boolean saveToCache)%0A    %7B%0A        return sharedInstance(context).getFont(fontFamily, loadFromCache, saveToCache);%0A    %7D%0A%0A    /**%0A     * Loads the font directly from the Assets. No caching done here.%0A     * @param context%0A     * @param fontFamily%0A     * @return The loaded Typeface or null if failed.%0A     */%0A    private static Typeface loadFontFromAssets
 (Con

Make sure that people always load the fonts from cache