aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/widgets/TypefaceCache.java
blob: ea6dcc142fa08b393914d59b2c0f4da13b82ddd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package org.wordpress.android.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

import org.wordpress.android.R;

import java.util.Hashtable;

public class TypefaceCache {

    /**
     * Cache used for all views that support custom fonts - defaults to the system font, but
     * Merriweather is also available via the "wpFontFamily" attribute
     */
    public static final int FAMILY_DEFAULT = 0;
    public static final int FAMILY_DEFAULT_LIGHT = 1;
    public static final int FAMILY_MERRIWEATHER = 2;

    private static final Hashtable<String, Typeface> mTypefaceCache = new Hashtable<>();

    public static Typeface getTypeface(Context context) {
        return getTypeface(context, FAMILY_DEFAULT, Typeface.NORMAL);
    }
    public static Typeface getTypeface(Context context, int family, int fontStyle) {
        if (context == null) {
            return null;
        }

        if (family == FAMILY_MERRIWEATHER) {
            final String typefaceName;
            switch (fontStyle) {
                case Typeface.BOLD:
                    typefaceName = "Merriweather-Bold.ttf";
                    break;
                case Typeface.ITALIC:
                    typefaceName = "Merriweather-Italic.ttf";
                    break;
                case Typeface.BOLD_ITALIC:
                    typefaceName = "Merriweather-BoldItalic.ttf";
                    break;
                default:
                    typefaceName = "Merriweather-Regular.ttf";
                    break;
            }
            return getTypefaceForTypefaceName(context, typefaceName);
        }

        // default system font
        if (family == FAMILY_DEFAULT_LIGHT) {
            return Typeface.create("sans-serif-light", fontStyle);
        } else {
            return Typeface.defaultFromStyle(fontStyle);
        }
    }

    /*
     * returns the desired typeface from the cache, loading it from app's assets if necessary
     */
    protected static Typeface getTypefaceForTypefaceName(Context context, String typefaceName) {
        if (!mTypefaceCache.containsKey(typefaceName)) {
            Typeface typeface = Typeface.createFromAsset(context.getApplicationContext().getAssets(), "fonts/"
                    + typefaceName);
            if (typeface != null) {
                mTypefaceCache.put(typefaceName, typeface);
            }
        }

        return mTypefaceCache.get(typefaceName);
    }

    /*
     * sets the typeface for a TextView (or TextView descendant such as EditText or Button) based on
     * the passed attributes, defaults to normal
     */
    protected static void setCustomTypeface(Context context, TextView view, AttributeSet attrs) {
        if (context == null || view == null) return;

        // skip at design-time
        if (view.isInEditMode()) return;

        // default if not set in attributes
        int family = FAMILY_DEFAULT;
        if (attrs != null) {
            TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.WPTextView, 0, 0);
            if (a != null) {
                try {
                    family = a.getInteger(R.styleable.WPTextView_wpFontFamily, FAMILY_DEFAULT);
                } finally {
                    a.recycle();
                }
            }
        }

        // nothing more to do if this is the default system font
        if (family == FAMILY_DEFAULT) {
            return;
        }

        // determine the font style from the existing typeface
        final int fontStyle;
        if (view.getTypeface() != null) {
            boolean isBold = view.getTypeface().isBold();
            boolean isItalic = view.getTypeface().isItalic();
            if (isBold && isItalic) {
                fontStyle = Typeface.BOLD_ITALIC;
            } else if (isBold) {
                fontStyle = Typeface.BOLD;
            } else if (isItalic) {
                fontStyle = Typeface.ITALIC;
            } else {
                fontStyle = Typeface.NORMAL;
            }
        } else {
            fontStyle = Typeface.NORMAL;
        }

        Typeface typeface = getTypeface(context, family, fontStyle);
        if (typeface != null) {
            view.setTypeface(typeface);
        }
    }
}