Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FontLoader.java « holoeverywhere « org « src « library « HoloEverywhere « 3rd_party « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bf3db13508d007126fe0c80aada32b7bf6efb615 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277

package org.holoeverywhere;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.os.Build.VERSION;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public final class FontLoader {
    public static interface FontSelector {
        public HoloFont getFontForView(View view);
    }

    public static class HoloFont implements FontSelector {
        public static final HoloFont ROBOTO;
        public static final HoloFont ROBOTO_BOLD;
        public static final HoloFont ROBOTO_BOLD_ITALIC;
        public static final HoloFont ROBOTO_ITALIC;
        public static final HoloFont ROBOTO_REGULAR;

        static {
            ROBOTO = makeFont(
                    ROBOTO_REGULAR = makeFont(R.raw.roboto_regular),
                    ROBOTO_BOLD = makeFont(R.raw.roboto_bold),
                    ROBOTO_ITALIC = makeFont(R.raw.roboto_italic),
                    ROBOTO_BOLD_ITALIC = makeFont(R.raw.roboto_bolditalic)
                    );
        }

        public static HoloFont makeFont(HoloFont regular, HoloFont bold, HoloFont italic,
                HoloFont boldItalic) {
            return new HoloFontMerger(regular, bold, italic, boldItalic);
        }

        public static HoloFont makeFont(int rawResourceId) {
            return new HoloFont(rawResourceId);
        }

        public static HoloFont makeFont(int rawResourceId, boolean ignore) {
            return new HoloFont(rawResourceId, ignore);
        }

        public static HoloFont makeFont(Typeface typeface) {
            return new HoloFont(typeface);
        }

        /**
         * Font raw resource id
         */
        protected int mFontId;
        /**
         * If this flag setted this font doesn't modify any view
         */
        protected boolean mIgnore;
        /**
         * Loaded typeface
         */
        protected Typeface mTypeface;

        private HoloFont(int font) {
            this(font, VERSION.SDK_INT >= 11);
        }

        private HoloFont(int font, boolean ignore) {
            mFontId = font;
            mIgnore = ignore;
            mTypeface = null;
        }

        private HoloFont(Typeface typeface) {
            mFontId = -1;
            mIgnore = false;
            mTypeface = typeface;
        }

        public <T extends View> T apply(T view) {
            if (mIgnore || mTypeface == null && mFontId <= 0 || view == null) {
                return view;
            }
            return FontLoader.apply(view, obtainTypeface(view.getContext()));
        }

        @Override
        public HoloFont getFontForView(View view) {
            return this;
        }

        public Typeface obtainTypeface(Context context) {
            if (mTypeface != null) {
                return mTypeface;
            }
            if (mTypeface == null && mFontId > 0) {
                mTypeface = loadTypeface(context, mFontId);
            }
            return mTypeface;
        }
    }

    private static final class HoloFontMerger extends HoloFont {
        private HoloFont mBold;
        private HoloFont mBoldItalic;
        private HoloFont mItalic;
        private HoloFont mRegular;

        public HoloFontMerger(HoloFont regular, HoloFont bold, HoloFont italic, HoloFont boldItalic) {
            super(regular.mFontId, false);
            mRegular = regular;
            mBold = bold;
            mItalic = italic;
            mBoldItalic = boldItalic;
        }

        @Override
        public HoloFont getFontForView(View view) {
            if (!(view instanceof TextView)) {
                return super.getFontForView(view);
            }
            TextView textView = (TextView) view;
            Typeface typeface = textView.getTypeface();
            if (typeface == null) {
                return mRegular;
            }
            final boolean bold = typeface.isBold(), italic = typeface.isItalic();
            if (bold && italic) {
                return mBoldItalic;
            } else if (bold) {
                return mBold;
            } else if (italic) {
                return mItalic;
            } else {
                return mRegular;
            }
        }

    }

    private static final SparseArray<Typeface> sFontCache = new SparseArray<Typeface>();

    private static final String TAG = FontLoader.class.getSimpleName();

    public static <T extends View> T apply(T view) {
        return apply(view, HoloFont.ROBOTO);
    }

    public static <T extends View> T apply(T view, FontSelector fontSelector) {
        if (view == null || view.getContext() == null
                || view.getContext().isRestricted()) {
            Log.e(FontLoader.TAG, "View or context is invalid");
            return view;
        }
        return internalApply(view, fontSelector);
    }

    public static <T extends View> T apply(T view, HoloFont font) {
        return apply(view, (FontSelector) font);
    }

    @SuppressLint("NewApi")
    public static <T extends View> T apply(T view, int font) {
        if (view == null || view.getContext() == null
                || view.getContext().isRestricted()) {
            Log.e(FontLoader.TAG, "View or context is invalid");
            return view;
        }
        Typeface typeface = FontLoader.loadTypeface(view.getContext(), font);
        if (typeface == null) {
            Log.v(FontLoader.TAG, "Font " + font + " not found in resources");
            return view;
        } else {
            return FontLoader.apply(view, typeface);
        }
    }

    public static <T extends View> T apply(T view, Typeface typeface) {
        if (view == null || view.getContext() == null
                || view.getContext().isRestricted()) {
            return view;
        }
        if (typeface == null) {
            Log.v(FontLoader.TAG, "Font is null");
            return view;
        }
        if (view instanceof TextView) {
            ((TextView) view).setTypeface(typeface);
        }
        if (view instanceof ViewGroup) {
            ViewGroup group = (ViewGroup) view;
            for (int i = 0; i < group.getChildCount(); i++) {
                FontLoader.apply(group.getChildAt(i), typeface);
            }
        }
        return view;
    }

    private static <T extends View> T internalApply(T view, FontSelector fontSelector) {
        if (view instanceof TextView) {
            TextView textView = (TextView) view;
            HoloFont font = fontSelector.getFontForView(textView);
            if (font != null && !font.mIgnore) {
                Typeface typeface = font.obtainTypeface(view.getContext());
                if (typeface != null) {
                    textView.setTypeface(typeface);
                }
            }
        } else if (view instanceof ViewGroup) {
            ViewGroup group = (ViewGroup) view;
            final int childCount = group.getChildCount();
            for (int i = 0; i < childCount; i++) {
                internalApply(group.getChildAt(i), fontSelector);
            }
        }
        return view;
    }

    public static Typeface loadTypeface(Context context, int font) {
        Typeface typeface = FontLoader.sFontCache.get(font);
        if (typeface == null) {
            try {
                File file = new File(context.getCacheDir(), "fonts");
                if (!file.exists()) {
                    file.mkdirs();
                }
                file = new File(file, "font_0x" + Integer.toHexString(font));
                FontLoader.sFontCache.put(font,
                        typeface = readTypeface(file, context.getResources(), font, true));
            } catch (Exception e) {
                Log.e(FontLoader.TAG, "Error of loading font", e);
            }
        }
        return typeface;
    }

    private static Typeface readTypeface(File file, Resources res, int font,
            boolean allowReadExistsFile) throws Exception {
        try {
            if (!allowReadExistsFile || !file.exists()) {
                InputStream is = new BufferedInputStream(res.openRawResource(font));
                OutputStream os = new ByteArrayOutputStream(Math.max(is.available(), 1024));
                byte[] buffer = new byte[1024];
                int read;
                while ((read = is.read(buffer)) > 0) {
                    os.write(buffer, 0, read);
                }
                is.close();
                os.flush();
                buffer = ((ByteArrayOutputStream) os).toByteArray();
                os.close();
                os = new FileOutputStream(file);
                os.write(buffer);
                os.flush();
                os.close();
            }
            return Typeface.createFromFile(file);
        } catch (Exception e) {
            if (allowReadExistsFile) {
                return readTypeface(file, res, font, false);
            }
            throw e;
        }
    }

    private FontLoader() {
    }
}