aboutsummaryrefslogtreecommitdiff
path: root/shadows/framework/src/main/java/org/robolectric/shadows/ShadowNativeTypeface.java
blob: 0c7fcf630524459d10196ff85c2f5b5e3ec853bc (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package org.robolectric.shadows;

import static android.os.Build.VERSION_CODES.LOLLIPOP;
import static android.os.Build.VERSION_CODES.O;
import static android.os.Build.VERSION_CODES.O_MR1;
import static android.os.Build.VERSION_CODES.P;
import static android.os.Build.VERSION_CODES.Q;
import static android.os.Build.VERSION_CODES.R;
import static android.os.Build.VERSION_CODES.S;
import static android.os.Build.VERSION_CODES.TIRAMISU;
import static org.robolectric.util.reflector.Reflector.reflector;

import android.graphics.FontFamily;
import android.graphics.Typeface;
import android.graphics.fonts.FontVariationAxis;
import android.text.FontConfig;
import android.util.ArrayMap;
import android.util.Log;
import com.google.common.base.Preconditions;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.List;
import java.util.Map;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.nativeruntime.DefaultNativeRuntimeLoader;
import org.robolectric.nativeruntime.TypefaceNatives;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.util.reflector.Direct;
import org.robolectric.util.reflector.ForType;
import org.robolectric.util.reflector.Static;

/** Shadow for {@link Typeface} that is backed by native code */
@Implements(value = Typeface.class, looseSignatures = true, minSdk = O, isInAndroidSdk = false)
public class ShadowNativeTypeface extends ShadowTypeface {

  private static final String TAG = "ShadowNativeTypeface";

  // Style value for building typeface.
  private static final int STYLE_NORMAL = 0;
  private static final int STYLE_ITALIC = 1;

  @Implementation(minSdk = S)
  protected static void __staticInitializer__() {
    Shadow.directInitialize(Typeface.class);
    // Initialize the system font map. In real Android this is done as part of Application startup
    // and uses a more complex SharedMemory system not supported in Robolectric.
    Typeface.loadPreinstalledSystemFontMap();
  }

  @Implementation(minSdk = P, maxSdk = P)
  protected static void buildSystemFallback(
      String xmlPath,
      String systemFontDir,
      ArrayMap<String, Typeface> fontMap,
      ArrayMap<String, FontFamily[]> fallbackMap) {
    String fontDir = System.getProperty("robolectric.nativeruntime.fontdir");
    Preconditions.checkNotNull(fontDir);
    Preconditions.checkState(new File(fontDir).isDirectory(), "Missing fonts directory");
    Preconditions.checkState(fontDir.endsWith("/"), "Fonts directory must end with a slash");
    reflector(TypefaceReflector.class)
        .buildSystemFallback(fontDir + "fonts.xml", fontDir, fontMap, fallbackMap);
  }

  @Implementation(minSdk = O, maxSdk = O_MR1)
  protected static File getSystemFontConfigLocation() {
    // Ensure that the Robolectric native runtime is loaded in ordere to ensure that the
    // `robolectric.nativeruntime.fontdir` system property is valid.
    DefaultNativeRuntimeLoader.injectAndLoad();
    String fontDir = System.getProperty("robolectric.nativeruntime.fontdir");
    Preconditions.checkNotNull(fontDir);
    Preconditions.checkState(new File(fontDir).isDirectory(), "Missing fonts directory");
    Preconditions.checkState(fontDir.endsWith("/"), "Fonts directory must end with a slash");
    return new File(fontDir);
  }

  @SuppressWarnings("unchecked")
  @Implementation(minSdk = O, maxSdk = O_MR1)
  protected static Object makeFamilyFromParsed(Object family, Object bufferForPathMap) {
    FontConfigFamilyReflector reflector = reflector(FontConfigFamilyReflector.class, family);
    Map<String, ByteBuffer> bufferForPath = (Map<String, ByteBuffer>) bufferForPathMap;

    FontFamily fontFamily =
        Shadow.newInstance(
            FontFamily.class,
            new Class<?>[] {String.class, int.class},
            new Object[] {reflector.getLanguage(), reflector.getVariant()});
    for (FontConfig.Font font : reflector.getFonts()) {
      String fullPathName =
          System.getProperty("robolectric.nativeruntime.fontdir")
              + reflector(FontConfigFontReflector.class, font).getFontName();
      ByteBuffer fontBuffer = bufferForPath.get(fullPathName);
      if (fontBuffer == null) {
        try (FileInputStream file = new FileInputStream(fullPathName)) {
          FileChannel fileChannel = file.getChannel();
          long fontSize = fileChannel.size();
          fontBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fontSize);
          bufferForPath.put(fullPathName, fontBuffer);
        } catch (IOException e) {
          Log.w(TAG, "Error mapping font file " + fullPathName);
          continue;
        }
      }
      if (!fontFamily.addFontFromBuffer(
          fontBuffer,
          font.getTtcIndex(),
          font.getAxes(),
          font.getWeight(),
          font.isItalic() ? STYLE_ITALIC : STYLE_NORMAL)) {
        Log.e(TAG, "Error creating font " + fullPathName + "#" + font.getTtcIndex());
      }
    }
    if (!fontFamily.freeze()) {
      // Treat as system error since reaching here means that a system pre-installed font
      // can't be used by our font stack.
      Log.w(TAG, "Unable to load Family: " + reflector.getName() + ":" + reflector.getLanguage());
      return null;
    }
    return fontFamily;
  }

  @Implementation(minSdk = LOLLIPOP)
  protected static long nativeCreateFromTypeface(long nativeInstance, int style) {
    return TypefaceNatives.nativeCreateFromTypeface(nativeInstance, style);
  }

  @Implementation(minSdk = O)
  protected static long nativeCreateFromTypefaceWithExactStyle(
      long nativeInstance, int weight, boolean italic) {
    return TypefaceNatives.nativeCreateFromTypefaceWithExactStyle(nativeInstance, weight, italic);
  }

  @Implementation(minSdk = O)
  protected static long nativeCreateFromTypefaceWithVariation(
      long nativeInstance, List<FontVariationAxis> axes) {
    return TypefaceNatives.nativeCreateFromTypefaceWithVariation(nativeInstance, axes);
  }

  @Implementation(minSdk = LOLLIPOP)
  protected static long nativeCreateWeightAlias(long nativeInstance, int weight) {
    return TypefaceNatives.nativeCreateWeightAlias(nativeInstance, weight);
  }

  @Implementation(minSdk = O, maxSdk = R)
  protected static long nativeCreateFromArray(long[] familyArray, int weight, int italic) {
    return TypefaceNatives.nativeCreateFromArray(familyArray, 0, weight, italic);
  }

  @Implementation(minSdk = S)
  protected static long nativeCreateFromArray(
      long[] familyArray, long fallbackTypeface, int weight, int italic) {
    return TypefaceNatives.nativeCreateFromArray(familyArray, fallbackTypeface, weight, italic);
  }

  @Implementation(minSdk = O)
  protected static int[] nativeGetSupportedAxes(long nativeInstance) {
    return TypefaceNatives.nativeGetSupportedAxes(nativeInstance);
  }

  @Implementation(minSdk = LOLLIPOP)
  protected static void nativeSetDefault(long nativePtr) {
    TypefaceNatives.nativeSetDefault(nativePtr);
  }

  @Implementation(minSdk = LOLLIPOP)
  protected static int nativeGetStyle(long nativePtr) {
    return TypefaceNatives.nativeGetStyle(nativePtr);
  }

  @Implementation(minSdk = O)
  protected static int nativeGetWeight(long nativePtr) {
    return TypefaceNatives.nativeGetWeight(nativePtr);
  }

  @Implementation(minSdk = P)
  protected static long nativeGetReleaseFunc() {
    DefaultNativeRuntimeLoader.injectAndLoad();
    return TypefaceNatives.nativeGetReleaseFunc();
  }

  @Implementation(minSdk = S, maxSdk = TIRAMISU)
  protected static int nativeGetFamilySize(long nativePtr) {
    return TypefaceNatives.nativeGetFamilySize(nativePtr);
  }

  @Implementation(minSdk = S, maxSdk = TIRAMISU)
  protected static long nativeGetFamily(long nativePtr, int index) {
    return TypefaceNatives.nativeGetFamily(nativePtr, index);
  }

  @Implementation(minSdk = Q)
  protected static void nativeRegisterGenericFamily(String str, long nativePtr) {
    TypefaceNatives.nativeRegisterGenericFamily(str, nativePtr);
  }

  @Implementation(minSdk = S, maxSdk = TIRAMISU)
  protected static int nativeWriteTypefaces(ByteBuffer buffer, long[] nativePtrs) {
    return TypefaceNatives.nativeWriteTypefaces(buffer, nativePtrs);
  }

  @Implementation(minSdk = 10000)
  protected static int nativeWriteTypefaces(ByteBuffer buffer, int position, long[] nativePtrs) {
    return nativeWriteTypefaces(buffer, nativePtrs);
  }

  @Implementation(minSdk = S, maxSdk = TIRAMISU)
  protected static long[] nativeReadTypefaces(ByteBuffer buffer) {
    return TypefaceNatives.nativeReadTypefaces(buffer);
  }

  @Implementation(minSdk = 10000)
  protected static long[] nativeReadTypefaces(ByteBuffer buffer, int position) {
    return nativeReadTypefaces(buffer);
  }

  @Implementation(minSdk = S)
  protected static void nativeForceSetStaticFinalField(String fieldName, Typeface typeface) {
    TypefaceNatives.nativeForceSetStaticFinalField(fieldName, typeface);
  }

  @Implementation(minSdk = S)
  protected static void nativeAddFontCollections(long nativePtr) {
    TypefaceNatives.nativeAddFontCollections(nativePtr);
  }

  static void ensureInitialized() {
    try {
      // Forces static initialization. This should be called before any native code that calls
      // Typeface::resolveDefault.
      Class.forName("android.graphics.Typeface");
    } catch (ClassNotFoundException e) {
      throw new LinkageError("Unable to load Typeface", e);
    }
  }

  @Override
  public FontDesc getFontDescription() {
    throw new UnsupportedOperationException(
        "Legacy ShadowTypeface description APIs are not supported");
  }

  /**
   * Shadow for {@link Typeface.Builder}. It is empty to avoid using the legacy {@link
   * Typeface.Builder} shadow.
   */
  @Implements(
      value = Typeface.Builder.class,
      minSdk = P,
      shadowPicker = ShadowNativeTypefaceBuilder.Picker.class,
      isInAndroidSdk = false)
  public static class ShadowNativeTypefaceBuilder {
    /** Shadow picker for {@link Typeface.Builder}. */
    public static final class Picker extends GraphicsShadowPicker<Object> {
      public Picker() {
        super(ShadowLegacyTypeface.ShadowBuilder.class, ShadowNativeTypefaceBuilder.class);
      }
    }
  }

  @ForType(Typeface.class)
  interface TypefaceReflector {
    @CanIgnoreReturnValue
    @Static
    @Direct
    FontConfig.Alias[] buildSystemFallback(
        String xmlPath,
        String fontDir,
        ArrayMap<String, Typeface> fontMap,
        ArrayMap<String, FontFamily[]> fallbackMap);
  }

  @ForType(className = "android.text.FontConfig$Family")
  interface FontConfigFamilyReflector {
    String getLanguage();

    int getVariant();

    FontConfig.Font[] getFonts();

    String getName();
  }

  @ForType(className = "android.text.FontConfig$Font")
  interface FontConfigFontReflector {
    String getFontName();
  }
}