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 FontConfig(@onNull Family[] families, @NonNull Alias[] aliases)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 */ getFamilies()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 */ getAliases()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 */ Font(@onNull String fontName, int ttcIndex, @NonNull FontVariationAxis[] axes, int weight, boolean isItalic, String fallbackFor)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 */ getFontName()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 */ getTtcIndex()92 public int getTtcIndex() { 93 return mTtcIndex; 94 } 95 96 /** 97 * Returns the list of axes associated to this font. 98 */ getAxes()99 public @NonNull FontVariationAxis[] getAxes() { 100 return mAxes; 101 } 102 103 /** 104 * Returns the weight value for this font. 105 */ getWeight()106 public int getWeight() { 107 return mWeight; 108 } 109 110 /** 111 * Returns whether this font is italic. 112 */ isItalic()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 */ getUri()123 public @Nullable Uri getUri() { 124 return mUri; 125 } 126 setUri(@onNull Uri uri)127 public void setUri(@NonNull Uri uri) { 128 mUri = uri; 129 } 130 getFallbackFor()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 Alias(@onNull String name, @NonNull String toName, int weight)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 */ getName()153 public @NonNull String getName() { 154 return mName; 155 } 156 157 /** 158 * Returns the existing name to which this alias points to. 159 */ getToName()160 public @NonNull String getToName() { 161 return mToName; 162 } 163 164 /** 165 * Returns the weight associated with this alias. 166 */ getWeight()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[] mLanguages; 179 180 /** @hide */ 181 @Retention(SOURCE) 182 @IntDef(prefix = { "VARIANT_" }, value = { 183 VARIANT_DEFAULT, 184 VARIANT_COMPACT, 185 VARIANT_ELEGANT 186 }) 187 public @interface Variant {} 188 189 /** 190 * Value for font variant. 191 * 192 * Indicates the font has no variant attribute. 193 */ 194 public static final int VARIANT_DEFAULT = 0; 195 196 /** 197 * Value for font variant. 198 * 199 * Indicates the font is for compact variant. 200 * @see android.graphics.Paint#setElegantTextHeight 201 */ 202 public static final int VARIANT_COMPACT = 1; 203 204 /** 205 * Value for font variant. 206 * 207 * Indiates the font is for elegant variant. 208 * @see android.graphics.Paint#setElegantTextHeight 209 */ 210 public static final int VARIANT_ELEGANT = 2; 211 212 // Must be same with Minikin's variant values. 213 // See frameworks/minikin/include/minikin/FontFamily.h 214 private final @Variant int mVariant; 215 Family(@onNull String name, @NonNull Font[] fonts, @NonNull String[] languages, @Variant int variant)216 public Family(@NonNull String name, @NonNull Font[] fonts, @NonNull String[] languages, 217 @Variant int variant) { 218 mName = name; 219 mFonts = fonts; 220 mLanguages = languages; 221 mVariant = variant; 222 } 223 224 /** 225 * Returns the name given by the system to this font family. 226 */ getName()227 public @Nullable String getName() { 228 return mName; 229 } 230 231 /** 232 * Returns the list of fonts included in this family. 233 */ getFonts()234 public @Nullable Font[] getFonts() { 235 return mFonts; 236 } 237 238 /** 239 * Returns the languages for this family. May be null. 240 */ getLanguages()241 public @Nullable String[] getLanguages() { 242 return mLanguages; 243 } 244 245 /** 246 * Returns the font variant for this family, e.g. "elegant" or "compact". May be null. 247 */ getVariant()248 public @Variant int getVariant() { 249 return mVariant; 250 } 251 } 252 } 253