2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 import static java.lang.annotation.RetentionPolicy.SOURCE;
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;
29 import java.io.IOException;
30 import java.lang.annotation.Retention;
31 import java.util.Arrays;
35 * Font configuration descriptions for System fonts.
37 public final class FontConfig implements Parcelable {
38 private final @NonNull Family[] mFamilies;
39 private final @NonNull Alias[] mAliases;
41 public FontConfig(@NonNull Family[] families, @NonNull Alias[] aliases) {
47 * For duplicating file descriptors.
49 * Note that this copy constructor can not be usable for deep copy.
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]);
57 mAliases = Arrays.copyOf(config.mAliases, config.mAliases.length);
61 * Returns the ordered list of families included in the system fonts.
63 public @NonNull Family[] getFamilies() {
68 * Returns the list of aliases defined for the font families in the system fonts.
70 public @NonNull Alias[] getAliases() {
77 public FontConfig(Parcel in) {
78 mFamilies = in.readTypedArray(Family.CREATOR);
79 mAliases = in.readTypedArray(Alias.CREATOR);
83 public void writeToParcel(Parcel out, int flag) {
84 out.writeTypedArray(mFamilies, flag);
85 out.writeTypedArray(mAliases, flag);
89 public int describeContents() {
93 public static final Parcelable.Creator<FontConfig> CREATOR = new Parcelable.Creator() {
94 public FontConfig createFromParcel(Parcel in) {
95 return new FontConfig(in);
97 public FontConfig[] newArray(int size) {
98 return new FontConfig[size];
103 * Class that holds information about a Font axis.
105 public static final class Axis implements Parcelable {
106 private final int mTag;
107 private final float mStyleValue;
109 public Axis(int tag, float styleValue) {
111 mStyleValue = styleValue;
115 public Axis(@NonNull String tagString, float styleValue) {
116 if (!FontListParser.isValidTag(tagString)) {
117 throw new IllegalArgumentException("Invalid tag pattern: " + tagString);
119 mTag = FontListParser.makeTag(tagString);
120 mStyleValue = styleValue;
124 * Returns the variable font axis tag associated to this axis.
126 public int getTag() {
131 * Returns the style value associated to the given axis for this font.
133 public float getStyleValue() {
140 public Axis(Parcel in) {
142 mStyleValue = in.readFloat();
146 public void writeToParcel(Parcel out, int flag) {
148 out.writeFloat(mStyleValue);
152 public int describeContents() {
156 public static final Creator<Axis> CREATOR = new Creator<Axis>() {
158 public Axis createFromParcel(Parcel in) {
163 public Axis[] newArray(int size) {
164 return new Axis[size];
170 * Class that holds information about a Font.
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;
183 public Font(@NonNull String fontName, int ttcIndex, @NonNull Axis[] axes, int weight,
185 mFontName = fontName;
186 mTtcIndex = ttcIndex;
189 mIsItalic = isItalic;
194 * This is for duplicating FileDescriptors.
196 * Note that this copy ctor doesn't deep copy the members.
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) {
208 mFd = origin.mFd.dup();
209 } catch (IOException e) {
216 * Returns the name associated by the system to this font.
218 public @NonNull String getFontName() {
223 * Returns the index to be used to access this font when accessing a TTC file.
225 public int getTtcIndex() {
230 * Returns the list of axes associated to this font.
232 public @NonNull Axis[] getAxes() {
237 * Returns the weight value for this font.
239 public int getWeight() {
244 * Returns whether this font is italic.
246 public boolean isItalic() {
251 * Returns a file descriptor to access the specified font. This should be closed after use.
253 public @Nullable ParcelFileDescriptor getFd() {
260 public void setFd(@NonNull ParcelFileDescriptor fd) {
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);
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);
289 mFd.writeToParcel(out, flag);
294 public int describeContents() {
298 public static final Creator<Font> CREATOR = new Creator<Font>() {
300 public Font createFromParcel(Parcel in) {
305 public Font[] newArray(int size) {
306 return new Font[size];
312 * Class that holds information about a Font alias.
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;
319 public Alias(@NonNull String name, @NonNull String toName, int weight) {
326 * Returns the new name for the alias.
328 public @NonNull String getName() {
333 * Returns the existing name to which this alias points to.
335 public @NonNull String getToName() {
340 * Returns the weight associated with this alias.
342 public int getWeight() {
349 public Alias(Parcel in) {
350 mName = in.readString();
351 mToName = in.readString();
352 mWeight = in.readInt();
356 public void writeToParcel(Parcel out, int flag) {
357 out.writeString(mName);
358 out.writeString(mToName);
359 out.writeInt(mWeight);
363 public int describeContents() {
367 public static final Creator<Alias> CREATOR = new Creator<Alias>() {
369 public Alias createFromParcel(Parcel in) {
370 return new Alias(in);
374 public Alias[] newArray(int size) {
375 return new Alias[size];
381 * Class that holds information about a Font family.
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;
390 @IntDef({VARIANT_DEFAULT, VARIANT_COMPACT, VARIANT_ELEGANT})
391 public @interface Variant {}
394 * Value for font variant.
396 * Indicates the font has no variant attribute.
398 public static final int VARIANT_DEFAULT = 0;
401 * Value for font variant.
403 * Indicates the font is for compact variant.
404 * @see android.graphics.Paint#setElegantTextHeight
406 public static final int VARIANT_COMPACT = 1;
409 * Value for font variant.
411 * Indiates the font is for elegant variant.
412 * @see android.graphics.Paint#setElegantTextHeight
414 public static final int VARIANT_ELEGANT = 2;
416 // Must be same with Minikin's variant values.
417 // See frameworks/minikin/include/minikin/FontFamily.h
418 private final @Variant int mVariant;
420 public Family(@NonNull String name, @NonNull Font[] fonts, @NonNull String language,
421 @Variant int variant) {
424 mLanguage = language;
429 * For duplicating file descriptor underlying Font object.
431 * This copy constructor is not for deep copying.
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]);
445 * Returns the name given by the system to this font family.
447 public @Nullable String getName() {
452 * Returns the list of fonts included in this family.
454 public @Nullable Font[] getFonts() {
459 * Returns the language for this family. May be null.
461 public @Nullable String getLanguage() {
466 * Returns the font variant for this family, e.g. "elegant" or "compact". May be null.
468 public @Variant int getVariant() {
475 public Family(Parcel in) {
476 mName = in.readString();
477 mFonts = in.readTypedArray(Font.CREATOR);
478 mLanguage = in.readString();
479 mVariant = in.readInt();
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);
491 public int describeContents() {
495 public static final Creator<Family> CREATOR = new Creator<Family>() {
497 public Family createFromParcel(Parcel in) {
498 return new Family(in);
502 public Family[] newArray(int size) {
503 return new Family[size];