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