blob: f50de1665453d233e1380d2a428338232f45a4cd [file] [log] [blame]
Alan Viverette3da604b2020-06-10 18:34:39 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.graphics;
18
19import android.annotation.Nullable;
20import android.compat.annotation.UnsupportedAppUsage;
21import android.content.res.AssetManager;
22import android.graphics.fonts.Font;
23import android.graphics.fonts.FontVariationAxis;
24import android.os.Build;
25import android.text.TextUtils;
26
27import dalvik.annotation.optimization.CriticalNative;
28
29import libcore.util.NativeAllocationRegistry;
30
31import java.io.FileInputStream;
32import java.io.IOException;
33import java.nio.ByteBuffer;
34import java.nio.channels.FileChannel;
35
36/**
37 * A family of typefaces with different styles.
38 *
39 * @hide
40 *
41 * @deprecated Use {@link android.graphics.fonts.FontFamily} instead.
42 */
43@Deprecated
44public class FontFamily {
45
46 private static String TAG = "FontFamily";
47
48 private static final NativeAllocationRegistry sBuilderRegistry =
49 NativeAllocationRegistry.createMalloced(
50 FontFamily.class.getClassLoader(), nGetBuilderReleaseFunc());
51
52 private @Nullable Runnable mNativeBuilderCleaner;
53
54 private static final NativeAllocationRegistry sFamilyRegistry =
55 NativeAllocationRegistry.createMalloced(
56 FontFamily.class.getClassLoader(), nGetFamilyReleaseFunc());
57
58 /**
59 * @hide
60 *
61 * This cannot be deleted because it's in use by AndroidX.
62 */
63 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.Q,
64 publicAlternatives = "Use {@link android.graphics.fonts.FontFamily} instead.")
65 public long mNativePtr;
66
67 // Points native font family builder. Must be zero after freezing this family.
68 private long mBuilderPtr;
69
70 /**
71 * This cannot be deleted because it's in use by AndroidX.
72 */
73 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.Q,
74 publicAlternatives = "Use {@link android.graphics.fonts.FontFamily} instead.")
75 public FontFamily() {
76 mBuilderPtr = nInitBuilder(null, 0);
77 mNativeBuilderCleaner = sBuilderRegistry.registerNativeAllocation(this, mBuilderPtr);
78 }
79
80 /**
81 * This cannot be deleted because it's in use by AndroidX.
82 */
83 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.Q,
84 publicAlternatives = "Use {@link android.graphics.fonts.FontFamily} instead.")
85 public FontFamily(@Nullable String[] langs, int variant) {
86 final String langsString;
87 if (langs == null || langs.length == 0) {
88 langsString = null;
89 } else if (langs.length == 1) {
90 langsString = langs[0];
91 } else {
92 langsString = TextUtils.join(",", langs);
93 }
94 mBuilderPtr = nInitBuilder(langsString, variant);
95 mNativeBuilderCleaner = sBuilderRegistry.registerNativeAllocation(this, mBuilderPtr);
96 }
97
98 /**
99 * Finalize the FontFamily creation.
100 *
101 * @return boolean returns false if some error happens in native code, e.g. broken font file is
102 * passed, etc.
103 *
104 * This cannot be deleted because it's in use by AndroidX.
105 */
106 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.Q,
107 publicAlternatives = "Use {@link android.graphics.fonts.FontFamily} instead.")
108 public boolean freeze() {
109 if (mBuilderPtr == 0) {
110 throw new IllegalStateException("This FontFamily is already frozen");
111 }
112 mNativePtr = nCreateFamily(mBuilderPtr);
113 mNativeBuilderCleaner.run();
114 mBuilderPtr = 0;
115 if (mNativePtr != 0) {
116 sFamilyRegistry.registerNativeAllocation(this, mNativePtr);
117 }
118 return mNativePtr != 0;
119 }
120
121 /**
122 * This cannot be deleted because it's in use by AndroidX.
123 */
124 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.Q,
125 publicAlternatives = "Use {@link android.graphics.fonts.FontFamily} instead.")
126 public void abortCreation() {
127 if (mBuilderPtr == 0) {
128 throw new IllegalStateException("This FontFamily is already frozen or abandoned");
129 }
130 mNativeBuilderCleaner.run();
131 mBuilderPtr = 0;
132 }
133
134 /**
135 * This cannot be deleted because it's in use by AndroidX.
136 */
137 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.Q,
138 publicAlternatives = "Use {@link android.graphics.fonts.FontFamily} instead.")
139 public boolean addFont(String path, int ttcIndex, FontVariationAxis[] axes, int weight,
140 int italic) {
141 if (mBuilderPtr == 0) {
142 throw new IllegalStateException("Unable to call addFont after freezing.");
143 }
144 try (FileInputStream file = new FileInputStream(path)) {
145 FileChannel fileChannel = file.getChannel();
146 long fontSize = fileChannel.size();
147 ByteBuffer fontBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fontSize);
148 if (axes != null) {
149 for (FontVariationAxis axis : axes) {
150 nAddAxisValue(mBuilderPtr, axis.getOpenTypeTagValue(), axis.getStyleValue());
151 }
152 }
153 return nAddFont(mBuilderPtr, fontBuffer, ttcIndex, weight, italic);
154 } catch (IOException e) {
155 return false;
156 }
157 }
158
159 /**
160 * This cannot be deleted because it's in use by AndroidX.
161 */
162 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.Q,
163 publicAlternatives = "Use {@link android.graphics.fonts.FontFamily} instead.")
164 public boolean addFontFromBuffer(ByteBuffer font, int ttcIndex, FontVariationAxis[] axes,
165 int weight, int italic) {
166 if (mBuilderPtr == 0) {
167 throw new IllegalStateException("Unable to call addFontWeightStyle after freezing.");
168 }
169 if (axes != null) {
170 for (FontVariationAxis axis : axes) {
171 nAddAxisValue(mBuilderPtr, axis.getOpenTypeTagValue(), axis.getStyleValue());
172 }
173 }
174 return nAddFontWeightStyle(mBuilderPtr, font, ttcIndex, weight, italic);
175 }
176
177 /**
178 * @param mgr The AssetManager to use for this context.
179 * @param path The path to the font file to load.
180 * @param cookie If available, the resource cookie given by Resources.
181 * @param isAsset {@code true} if this is from the assets/ folder, {@code false} if from
182 * resources
183 * @param weight The weight of the font. If 0 is given, the weight and italic will be resolved
184 * using the OS/2 table in the font.
185 * @param isItalic Whether this font is italic. If the weight is set to 0, this will be resolved
186 * using the OS/2 table in the font.
187 * @return
188 *
189 * This cannot be deleted because it's in use by AndroidX.
190 */
191 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.Q,
192 publicAlternatives = "Use {@link android.graphics.fonts.FontFamily} instead.")
193 public boolean addFontFromAssetManager(AssetManager mgr, String path, int cookie,
194 boolean isAsset, int ttcIndex, int weight, int isItalic,
195 FontVariationAxis[] axes) {
196 if (mBuilderPtr == 0) {
197 throw new IllegalStateException("Unable to call addFontFromAsset after freezing.");
198 }
199
200 try {
201 ByteBuffer buffer = Font.Builder.createBuffer(mgr, path, isAsset, cookie);
202 return addFontFromBuffer(buffer, ttcIndex, axes, weight, isItalic);
203 } catch (IOException e) {
204 return false;
205 }
206 }
207
208 private static native long nInitBuilder(String langs, int variant);
209
210 @CriticalNative
211 private static native long nCreateFamily(long mBuilderPtr);
212
213 @CriticalNative
214 private static native long nGetBuilderReleaseFunc();
215
216 @CriticalNative
217 private static native long nGetFamilyReleaseFunc();
218 // By passing -1 to weigth argument, the weight value is resolved by OS/2 table in the font.
219 // By passing -1 to italic argument, the italic value is resolved by OS/2 table in the font.
220 private static native boolean nAddFont(long builderPtr, ByteBuffer font, int ttcIndex,
221 int weight, int isItalic);
222 private static native boolean nAddFontWeightStyle(long builderPtr, ByteBuffer font,
223 int ttcIndex, int weight, int isItalic);
224
225 // The added axis values are only valid for the next nAddFont* method call.
226 @CriticalNative
227 private static native void nAddAxisValue(long builderPtr, int tag, float value);
228}