(String assetPath)
| 79 | private static final HashMap<String, Typeface> typefaceCache = new HashMap<>(); |
| 80 | |
| 81 | public static Typeface getTypeface(String assetPath) { |
| 82 | synchronized (typefaceCache) { |
| 83 | if (!typefaceCache.containsKey(assetPath)) { |
| 84 | try { |
| 85 | Typeface t = null; |
| 86 | switch (assetPath) { |
| 87 | case "fonts/rmedium.ttf": |
| 88 | t = Typeface.create("sans-serif-medium", Typeface.NORMAL); |
| 89 | break; |
| 90 | case "fonts/ritalic.ttf": |
| 91 | t = Typeface.create("sans-serif", Typeface.ITALIC); |
| 92 | break; |
| 93 | case "fonts/rmediumitalic.ttf": |
| 94 | t = Typeface.create("sans-serif-medium", Typeface.ITALIC); |
| 95 | break; |
| 96 | case "fonts/rmono.ttf": |
| 97 | t = Typeface.MONOSPACE; |
| 98 | break; |
| 99 | case "fonts/mw_bold.ttf": |
| 100 | t = Typeface.create("serif", Typeface.BOLD); |
| 101 | break; |
| 102 | default: |
| 103 | // should not reach here |
| 104 | break; |
| 105 | } |
| 106 | if (t == null) { |
| 107 | if (Build.VERSION.SDK_INT >= 26) { |
| 108 | Typeface.Builder builder = new Typeface.Builder(HostInfo.getApplication().getAssets(), assetPath); |
| 109 | if (assetPath.contains("medium")) { |
| 110 | builder.setWeight(700); |
| 111 | } |
| 112 | if (assetPath.contains("italic")) { |
| 113 | builder.setItalic(true); |
| 114 | } |
| 115 | t = builder.build(); |
| 116 | } else { |
| 117 | t = Typeface.createFromAsset(HostInfo.getApplication().getAssets(), assetPath); |
| 118 | } |
| 119 | } |
| 120 | typefaceCache.put(assetPath, t); |
| 121 | } catch (Exception e) { |
| 122 | if (BuildConfig.DEBUG) { |
| 123 | Utils.loge("Could not get typeface '" + assetPath + "' because " + e.getMessage()); |
| 124 | } |
| 125 | return null; |
| 126 | } |
| 127 | } |
| 128 | return typefaceCache.get(assetPath); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | public static void invalidate() { |
| 133 | // nothing to invalidate yet |
no test coverage detected