aboutsummaryrefslogtreecommitdiff
path: root/shadows/framework/src/main/java/org/robolectric/shadows/ShadowNativeFontFamily.java
blob: 495e22cb50dd8ffc133465dee0f931cce35a7ba6 (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
package org.robolectric.shadows;

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 android.content.res.AssetManager;
import android.graphics.FontFamily;
import java.io.IOException;
import java.nio.ByteBuffer;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.nativeruntime.DefaultNativeRuntimeLoader;
import org.robolectric.nativeruntime.FontFamilyNatives;
import org.robolectric.shadows.ShadowNativeFontFamily.Picker;
import org.robolectric.versioning.AndroidVersions.U;
import org.robolectric.versioning.AndroidVersions.V;

/** Shadow for {@link FontFamily} that is backed by native code */
@Implements(
    value = FontFamily.class,
    minSdk = O,
    isInAndroidSdk = false,
    shadowPicker = Picker.class,
    callNativeMethodsByDefault = true)
public class ShadowNativeFontFamily {

  /**
   * {@link android.graphics.FontFamily} invokes its own native methods in its static initializer.
   * This must be deferred starting in Android V.
   */
  @Implementation(minSdk = V.SDK_INT)
  protected static void __staticInitializer__() {
    // deferred
  }

  @Implementation(minSdk = O, maxSdk = U.SDK_INT)
  public static long nInitBuilder(String langs, int variant) {
    DefaultNativeRuntimeLoader.injectAndLoad();
    return FontFamilyNatives.nInitBuilder(langs, variant);
  }

  @Implementation(minSdk = O, maxSdk = O_MR1)
  protected static void nAllowUnsupportedFont(long builderPtr) {
    FontFamilyNatives.nAllowUnsupportedFont(builderPtr);
  }

  @Implementation(minSdk = O, maxSdk = U.SDK_INT)
  protected static long nCreateFamily(long mBuilderPtr) {
    return FontFamilyNatives.nCreateFamily(mBuilderPtr);
  }

  @Implementation(minSdk = P, maxSdk = U.SDK_INT)
  protected static long nGetBuilderReleaseFunc() {
    DefaultNativeRuntimeLoader.injectAndLoad();
    return FontFamilyNatives.nGetBuilderReleaseFunc();
  }

  @Implementation(minSdk = P, maxSdk = U.SDK_INT)
  protected static long nGetFamilyReleaseFunc() {
    return FontFamilyNatives.nGetFamilyReleaseFunc();
  }

  // By passing -1 to weight argument, the weight value is resolved by OS/2 table in the font.
  // By passing -1 to italic argument, the italic value is resolved by OS/2 table in the font.
  @Implementation(minSdk = O, maxSdk = U.SDK_INT)
  protected static boolean nAddFont(
      long builderPtr, ByteBuffer font, int ttcIndex, int weight, int isItalic) {
    return FontFamilyNatives.nAddFont(builderPtr, font, ttcIndex, weight, isItalic);
  }

  @Implementation(minSdk = O, maxSdk = Q)
  protected static boolean nAddFontFromAssetManager(
      long builderPtr,
      AssetManager mgr,
      String path,
      int cookie,
      boolean isAsset,
      int ttcIndex,
      int weight,
      int isItalic) {
    try {
      ByteBuffer byteBuffer = ShadowNativeFont.assetToBuffer(mgr, path, isAsset, cookie);
      return nAddFont(builderPtr, byteBuffer, ttcIndex, weight, isItalic);
    } catch (IOException e) {
      throw new UnsupportedOperationException(e);
    }
  }

  @Implementation(minSdk = O, maxSdk = U.SDK_INT)
  protected static boolean nAddFontWeightStyle(
      long builderPtr, ByteBuffer font, int ttcIndex, int weight, int isItalic) {
    return FontFamilyNatives.nAddFontWeightStyle(builderPtr, font, ttcIndex, weight, isItalic);
  }

  // The added axis values are only valid for the next nAddFont* method call.
  @Implementation(minSdk = O, maxSdk = U.SDK_INT)
  protected static void nAddAxisValue(long builderPtr, int tag, float value) {
    FontFamilyNatives.nAddAxisValue(builderPtr, tag, value);
  }

  @Implementation(minSdk = O, maxSdk = O_MR1)
  protected static void nAbort(long mBuilderPtr) {
    // no-op
  }

  /** Shadow picker for {@link FontFamily}. */
  public static final class Picker extends GraphicsShadowPicker<Object> {
    public Picker() {
      super(ShadowFontFamily.class, ShadowNativeFontFamily.class);
    }
  }
}