summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/FontUtils.java
blob: 09064ee16801e9c31d57e8f514d05563909ee28e (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
package com.mapbox.mapboxsdk.utils;

import android.graphics.Typeface;

import com.mapbox.mapboxsdk.MapStrictMode;
import com.mapbox.mapboxsdk.log.Logger;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.mapbox.mapboxsdk.constants.MapboxConstants.DEFAULT_FONT;

/**
 * Utility class to select a font from a range of font names based on the availability of fonts on the device.
 */
public class FontUtils {

  private static final String TAG = "Mbgl-FontUtils";

  private FontUtils() {
    // no instance
  }

  /**
   * Select a font from a range of font names to match the availability of fonts on the device.
   *
   * @param fontNames the range of font names to select from
   * @return the selected fon
   */
  public static String extractValidFont(String... fontNames) {
    if (fontNames == null) {
      return null;
    }

    List<String> validFonts = getAvailableFonts();
    for (String fontName : fontNames) {
      if (validFonts.contains(fontName)) {
        return fontName;
      }
    }

    Logger.i(TAG, String.format(
      "Couldn't map font family for local ideograph, using %s instead", DEFAULT_FONT)
    );
    return DEFAULT_FONT;
  }

  private static List<String> getAvailableFonts() {
    List<String> fonts = new ArrayList<>();
    try {
      Typeface typeface = Typeface.create(Typeface.DEFAULT, Typeface.NORMAL);
      Field f = Typeface.class.getDeclaredField("sSystemFontMap");
      f.setAccessible(true);
      Map<String, Typeface> fontMap = (Map<String, Typeface>) f.get(typeface);
      fonts.addAll(fontMap.keySet());
    } catch (Exception exception) {
      Logger.e(TAG,"Couldn't load fonts from Typeface", exception);
      MapStrictMode.strictModeViolation("Couldn't load fonts from Typeface", exception);
    }
    return fonts;
  }
}