OSDN Git Service

Expose Typeface creation APIs with ttc and font variation.
[android-x86/frameworks-base.git] / core / java / android / text / FontConfig.java
1 /*
2  * Copyright (C) 2017 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
17 package android.text;
18
19 import static java.lang.annotation.RetentionPolicy.SOURCE;
20
21 import android.annotation.IntDef;
22 import android.annotation.NonNull;
23 import android.annotation.Nullable;
24 import android.graphics.FontListParser;
25 import android.os.Parcel;
26 import android.os.ParcelFileDescriptor;
27 import android.os.Parcelable;
28
29 import java.io.IOException;
30 import java.lang.annotation.Retention;
31 import java.util.Arrays;
32
33
34 /**
35  * Font configuration descriptions for System fonts.
36  */
37 public final class FontConfig implements Parcelable {
38     private final @NonNull Family[] mFamilies;
39     private final @NonNull Alias[] mAliases;
40
41     public FontConfig(@NonNull Family[] families, @NonNull Alias[] aliases) {
42         mFamilies = families;
43         mAliases = aliases;
44     }
45
46     /**
47      * For duplicating file descriptors.
48      *
49      * Note that this copy constructor can not be usable for deep copy.
50      * @hide
51      */
52     public FontConfig(@NonNull FontConfig config) {
53         mFamilies = new Family[config.mFamilies.length];
54         for (int i = 0; i < config.mFamilies.length; ++i) {
55             mFamilies[i] = new Family(config.mFamilies[i]);
56         }
57         mAliases = Arrays.copyOf(config.mAliases, config.mAliases.length);
58     }
59
60     /**
61      * Returns the ordered list of families included in the system fonts.
62      */
63     public @NonNull Family[] getFamilies() {
64         return mFamilies;
65     }
66
67     /**
68      * Returns the list of aliases defined for the font families in the system fonts.
69      */
70     public @NonNull Alias[] getAliases() {
71         return mAliases;
72     }
73
74     /**
75      * @hide
76      */
77     public FontConfig(Parcel in) {
78         mFamilies = in.readTypedArray(Family.CREATOR);
79         mAliases = in.readTypedArray(Alias.CREATOR);
80     }
81
82     @Override
83     public void writeToParcel(Parcel out, int flag) {
84         out.writeTypedArray(mFamilies, flag);
85         out.writeTypedArray(mAliases, flag);
86     }
87
88     @Override
89     public int describeContents() {
90         return 0;
91     }
92
93     public static final Parcelable.Creator<FontConfig> CREATOR = new Parcelable.Creator() {
94         public FontConfig createFromParcel(Parcel in) {
95             return new FontConfig(in);
96         }
97         public FontConfig[] newArray(int size) {
98             return new FontConfig[size];
99         }
100     };
101
102     /**
103      * Class that holds information about a Font axis.
104      */
105     public static final class Axis implements Parcelable {
106         private final int mTag;
107         private final float mStyleValue;
108
109         public Axis(int tag, float styleValue) {
110             mTag = tag;
111             mStyleValue = styleValue;
112         }
113
114         /** @hide */
115         public Axis(@NonNull String tagString, float styleValue) {
116             if (!FontListParser.isValidTag(tagString)) {
117                 throw new IllegalArgumentException("Invalid tag pattern: " + tagString);
118             }
119             mTag = FontListParser.makeTag(tagString);
120             mStyleValue = styleValue;
121         }
122
123         /**
124          * Returns the variable font axis tag associated to this axis.
125          */
126         public int getTag() {
127             return mTag;
128         }
129
130         /**
131          * Returns the style value associated to the given axis for this font.
132          */
133         public float getStyleValue() {
134             return mStyleValue;
135         }
136
137         /**
138          * @hide
139          */
140         public Axis(Parcel in) {
141             mTag = in.readInt();
142             mStyleValue = in.readFloat();
143         }
144
145         @Override
146         public void writeToParcel(Parcel out, int flag) {
147             out.writeInt(mTag);
148             out.writeFloat(mStyleValue);
149         }
150
151         @Override
152         public int describeContents() {
153             return 0;
154         }
155
156         public static final Creator<Axis> CREATOR = new Creator<Axis>() {
157             @Override
158             public Axis createFromParcel(Parcel in) {
159                 return new Axis(in);
160             }
161
162             @Override
163             public Axis[] newArray(int size) {
164                 return new Axis[size];
165             }
166         };
167     }
168
169     /**
170      * Class that holds information about a Font.
171      */
172     public static final class Font implements Parcelable {
173         private final @NonNull String mFontName;
174         private final int mTtcIndex;
175         private final @NonNull Axis[] mAxes;
176         private final int mWeight;
177         private final boolean mIsItalic;
178         private @Nullable ParcelFileDescriptor mFd;
179
180         /**
181          * @hide
182          */
183         public Font(@NonNull String fontName, int ttcIndex, @NonNull Axis[] axes, int weight,
184                 boolean isItalic) {
185             mFontName = fontName;
186             mTtcIndex = ttcIndex;
187             mAxes = axes;
188             mWeight = weight;
189             mIsItalic = isItalic;
190             mFd = null;
191         }
192
193         /**
194          * This is for duplicating FileDescriptors.
195          *
196          * Note that this copy ctor doesn't deep copy the members.
197          *
198          * @hide
199          */
200         public Font(Font origin) {
201             mFontName = origin.mFontName;
202             mTtcIndex = origin.mTtcIndex;
203             mAxes = origin.mAxes;
204             mWeight = origin.mWeight;
205             mIsItalic = origin.mIsItalic;
206             if (origin.mFd != null) {
207                 try {
208                     mFd = origin.mFd.dup();
209                 } catch (IOException e) {
210                     e.printStackTrace();
211                 }
212             }
213         }
214
215         /**
216          * Returns the name associated by the system to this font.
217          */
218         public @NonNull String getFontName() {
219             return mFontName;
220         }
221
222         /**
223          * Returns the index to be used to access this font when accessing a TTC file.
224          */
225         public int getTtcIndex() {
226             return mTtcIndex;
227         }
228
229         /**
230          * Returns the list of axes associated to this font.
231          */
232         public @NonNull Axis[] getAxes() {
233             return mAxes;
234         }
235
236         /**
237          * Returns the weight value for this font.
238          */
239         public int getWeight() {
240             return mWeight;
241         }
242
243         /**
244          * Returns whether this font is italic.
245          */
246         public boolean isItalic() {
247             return mIsItalic;
248         }
249
250         /**
251          * Returns a file descriptor to access the specified font. This should be closed after use.
252          */
253         public @Nullable ParcelFileDescriptor getFd() {
254             return mFd;
255         }
256
257         /**
258          * @hide
259          */
260         public void setFd(@NonNull ParcelFileDescriptor fd) {
261             mFd = fd;
262         }
263
264         /**
265          * @hide
266          */
267         public Font(Parcel in) {
268             mFontName = in.readString();
269             mTtcIndex = in.readInt();
270             mAxes = in.createTypedArray(Axis.CREATOR);
271             mWeight = in.readInt();
272             mIsItalic = in.readInt() == 1;
273             if (in.readInt() == 1) { /* has FD */
274                 mFd = ParcelFileDescriptor.CREATOR.createFromParcel(in);
275             } else {
276                 mFd = null;
277             }
278         }
279
280         @Override
281         public void writeToParcel(Parcel out, int flag) {
282             out.writeString(mFontName);
283             out.writeInt(mTtcIndex);
284             out.writeTypedArray(mAxes, flag);
285             out.writeInt(mWeight);
286             out.writeInt(mIsItalic ? 1 : 0);
287             out.writeInt(mFd == null ? 0 : 1);
288             if (mFd != null) {
289                 mFd.writeToParcel(out, flag);
290             }
291         }
292
293         @Override
294         public int describeContents() {
295             return 0;
296         }
297
298         public static final Creator<Font> CREATOR = new Creator<Font>() {
299             @Override
300             public Font createFromParcel(Parcel in) {
301                 return new Font(in);
302             }
303
304             @Override
305             public Font[] newArray(int size) {
306                 return new Font[size];
307             }
308         };
309     }
310
311     /**
312      * Class that holds information about a Font alias.
313      */
314     public static final class Alias implements Parcelable {
315         private final @NonNull String mName;
316         private final @NonNull String mToName;
317         private final int mWeight;
318
319         public Alias(@NonNull String name, @NonNull String toName, int weight) {
320             mName = name;
321             mToName = toName;
322             mWeight = weight;
323         }
324
325         /**
326          * Returns the new name for the alias.
327          */
328         public @NonNull String getName() {
329             return mName;
330         }
331
332         /**
333          * Returns the existing name to which this alias points to.
334          */
335         public @NonNull String getToName() {
336             return mToName;
337         }
338
339         /**
340          * Returns the weight associated with this alias.
341          */
342         public int getWeight() {
343             return mWeight;
344         }
345
346         /**
347          * @hide
348          */
349         public Alias(Parcel in) {
350             mName = in.readString();
351             mToName = in.readString();
352             mWeight = in.readInt();
353         }
354
355         @Override
356         public void writeToParcel(Parcel out, int flag) {
357             out.writeString(mName);
358             out.writeString(mToName);
359             out.writeInt(mWeight);
360         }
361
362         @Override
363         public int describeContents() {
364             return 0;
365         }
366
367         public static final Creator<Alias> CREATOR = new Creator<Alias>() {
368             @Override
369             public Alias createFromParcel(Parcel in) {
370                 return new Alias(in);
371             }
372
373             @Override
374             public Alias[] newArray(int size) {
375                 return new Alias[size];
376             }
377         };
378     }
379
380     /**
381      * Class that holds information about a Font family.
382      */
383     public static final class Family implements Parcelable {
384         private final @NonNull String mName;
385         private final @NonNull Font[] mFonts;
386         private final @NonNull String mLanguage;
387
388         /** @hide */
389         @Retention(SOURCE)
390         @IntDef({VARIANT_DEFAULT, VARIANT_COMPACT, VARIANT_ELEGANT})
391         public @interface Variant {}
392
393         /**
394          * Value for font variant.
395          *
396          * Indicates the font has no variant attribute.
397          */
398         public static final int VARIANT_DEFAULT = 0;
399
400         /**
401          * Value for font variant.
402          *
403          * Indicates the font is for compact variant.
404          * @see android.graphics.Paint#setElegantTextHeight
405          */
406         public static final int VARIANT_COMPACT = 1;
407
408         /**
409          * Value for font variant.
410          *
411          * Indiates the font is for elegant variant.
412          * @see android.graphics.Paint#setElegantTextHeight
413          */
414         public static final int VARIANT_ELEGANT = 2;
415
416         // Must be same with Minikin's variant values.
417         // See frameworks/minikin/include/minikin/FontFamily.h
418         private final @Variant int mVariant;
419
420         public Family(@NonNull String name, @NonNull Font[] fonts, @NonNull String language,
421                 @Variant int variant) {
422             mName = name;
423             mFonts = fonts;
424             mLanguage = language;
425             mVariant = variant;
426         }
427
428         /**
429          * For duplicating file descriptor underlying Font object.
430          *
431          * This copy constructor is not for deep copying.
432          * @hide
433          */
434         public Family(Family origin) {
435             mName = origin.mName;
436             mLanguage = origin.mLanguage;
437             mVariant = origin.mVariant;
438             mFonts = new Font[origin.mFonts.length];
439             for (int i = 0; i < origin.mFonts.length; ++i) {
440                 mFonts[i] = new Font(origin.mFonts[i]);
441             }
442         }
443
444         /**
445          * Returns the name given by the system to this font family.
446          */
447         public @Nullable String getName() {
448             return mName;
449         }
450
451         /**
452          * Returns the list of fonts included in this family.
453          */
454         public @Nullable Font[] getFonts() {
455             return mFonts;
456         }
457
458         /**
459          * Returns the language for this family. May be null.
460          */
461         public @Nullable String getLanguage() {
462             return mLanguage;
463         }
464
465         /**
466          * Returns the font variant for this family, e.g. "elegant" or "compact". May be null.
467          */
468         public @Variant int getVariant() {
469             return mVariant;
470         }
471
472         /**
473          * @hide
474          */
475         public Family(Parcel in) {
476             mName = in.readString();
477             mFonts = in.readTypedArray(Font.CREATOR);
478             mLanguage = in.readString();
479             mVariant = in.readInt();
480         }
481
482         @Override
483         public void writeToParcel(Parcel out, int flag) {
484             out.writeString(mName);
485             out.writeTypedArray(mFonts, flag);
486             out.writeString(mLanguage);
487             out.writeInt(mVariant);
488         }
489
490         @Override
491         public int describeContents() {
492             return 0;
493         }
494
495         public static final Creator<Family> CREATOR = new Creator<Family>() {
496             @Override
497             public Family createFromParcel(Parcel in) {
498                 return new Family(in);
499             }
500
501             @Override
502             public Family[] newArray(int size) {
503                 return new Family[size];
504             }
505         };
506     }
507 }