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