OSDN Git Service

Implement family fallback.
[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.fonts.FontVariationAxis;
25 import android.net.Uri;
26
27 import java.lang.annotation.Retention;
28
29
30 /**
31  * Font configuration descriptions for System fonts.
32  * @hide
33  */
34 public final class FontConfig {
35     private final @NonNull Family[] mFamilies;
36     private final @NonNull Alias[] mAliases;
37
38     public FontConfig(@NonNull Family[] families, @NonNull Alias[] aliases) {
39         mFamilies = families;
40         mAliases = aliases;
41     }
42
43     /**
44      * Returns the ordered list of families included in the system fonts.
45      */
46     public @NonNull Family[] getFamilies() {
47         return mFamilies;
48     }
49
50     /**
51      * Returns the list of aliases defined for the font families in the system fonts.
52      */
53     public @NonNull Alias[] getAliases() {
54         return mAliases;
55     }
56
57     /**
58      * Class that holds information about a Font.
59      */
60     public static final class Font {
61         private final @NonNull String mFontName;
62         private final int mTtcIndex;
63         private final @NonNull FontVariationAxis[] mAxes;
64         private final int mWeight;
65         private final boolean mIsItalic;
66         private Uri mUri;
67         private final String mFallbackFor;
68
69         /**
70          * @hide
71          */
72         public Font(@NonNull String fontName, int ttcIndex, @NonNull FontVariationAxis[] axes,
73                 int weight, boolean isItalic, String fallbackFor) {
74             mFontName = fontName;
75             mTtcIndex = ttcIndex;
76             mAxes = axes;
77             mWeight = weight;
78             mIsItalic = isItalic;
79             mFallbackFor = fallbackFor;
80         }
81
82         /**
83          * Returns the name associated by the system to this font.
84          */
85         public @NonNull String getFontName() {
86             return mFontName;
87         }
88
89         /**
90          * Returns the index to be used to access this font when accessing a TTC file.
91          */
92         public int getTtcIndex() {
93             return mTtcIndex;
94         }
95
96         /**
97          * Returns the list of axes associated to this font.
98          */
99         public @NonNull FontVariationAxis[] getAxes() {
100             return mAxes;
101         }
102
103         /**
104          * Returns the weight value for this font.
105          */
106         public int getWeight() {
107             return mWeight;
108         }
109
110         /**
111          * Returns whether this font is italic.
112          */
113         public boolean isItalic() {
114             return mIsItalic;
115         }
116
117         /**
118          * Returns the content uri associated to this font.
119          *
120          * You can reach to the font contents by calling {@link
121          * android.content.ContentResolver#openInputStream}.
122          */
123         public @Nullable Uri getUri() {
124             return mUri;
125         }
126
127         public void setUri(@NonNull Uri uri) {
128             mUri = uri;
129         }
130
131         public String getFallbackFor() {
132             return mFallbackFor;
133         }
134     }
135
136     /**
137      * Class that holds information about a Font alias.
138      */
139     public static final class Alias {
140         private final @NonNull String mName;
141         private final @NonNull String mToName;
142         private final int mWeight;
143
144         public Alias(@NonNull String name, @NonNull String toName, int weight) {
145             mName = name;
146             mToName = toName;
147             mWeight = weight;
148         }
149
150         /**
151          * Returns the new name for the alias.
152          */
153         public @NonNull String getName() {
154             return mName;
155         }
156
157         /**
158          * Returns the existing name to which this alias points to.
159          */
160         public @NonNull String getToName() {
161             return mToName;
162         }
163
164         /**
165          * Returns the weight associated with this alias.
166          */
167         public int getWeight() {
168             return mWeight;
169         }
170     }
171
172     /**
173      * Class that holds information about a Font family.
174      */
175     public static final class Family {
176         private final @NonNull String mName;
177         private final @NonNull Font[] mFonts;
178         private final @NonNull String mLanguage;
179
180         /** @hide */
181         @Retention(SOURCE)
182         @IntDef({VARIANT_DEFAULT, VARIANT_COMPACT, VARIANT_ELEGANT})
183         public @interface Variant {}
184
185         /**
186          * Value for font variant.
187          *
188          * Indicates the font has no variant attribute.
189          */
190         public static final int VARIANT_DEFAULT = 0;
191
192         /**
193          * Value for font variant.
194          *
195          * Indicates the font is for compact variant.
196          * @see android.graphics.Paint#setElegantTextHeight
197          */
198         public static final int VARIANT_COMPACT = 1;
199
200         /**
201          * Value for font variant.
202          *
203          * Indiates the font is for elegant variant.
204          * @see android.graphics.Paint#setElegantTextHeight
205          */
206         public static final int VARIANT_ELEGANT = 2;
207
208         // Must be same with Minikin's variant values.
209         // See frameworks/minikin/include/minikin/FontFamily.h
210         private final @Variant int mVariant;
211
212         public Family(@NonNull String name, @NonNull Font[] fonts, @NonNull String language,
213                 @Variant int variant) {
214             mName = name;
215             mFonts = fonts;
216             mLanguage = language;
217             mVariant = variant;
218         }
219
220         /**
221          * Returns the name given by the system to this font family.
222          */
223         public @Nullable String getName() {
224             return mName;
225         }
226
227         /**
228          * Returns the list of fonts included in this family.
229          */
230         public @Nullable Font[] getFonts() {
231             return mFonts;
232         }
233
234         /**
235          * Returns the language for this family. May be null.
236          */
237         public @Nullable String getLanguage() {
238             return mLanguage;
239         }
240
241         /**
242          * Returns the font variant for this family, e.g. "elegant" or "compact". May be null.
243          */
244         public @Variant int getVariant() {
245             return mVariant;
246         }
247     }
248 }