Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2014 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.graphics;
     18 
     19 import android.content.res.AssetManager;
     20 
     21 /**
     22  * A family of typefaces with different styles.
     23  *
     24  * @hide
     25  */
     26 public class FontFamily {
     27     /**
     28      * @hide
     29      */
     30     public long mNativePtr;
     31 
     32     public FontFamily() {
     33         mNativePtr = nCreateFamily(null, 0);
     34         if (mNativePtr == 0) {
     35             throw new IllegalStateException("error creating native FontFamily");
     36         }
     37     }
     38 
     39     public FontFamily(String lang, String variant) {
     40         int varEnum = 0;
     41         if ("compact".equals(variant)) {
     42             varEnum = 1;
     43         } else if ("elegant".equals(variant)) {
     44             varEnum = 2;
     45         }
     46         mNativePtr = nCreateFamily(lang, varEnum);
     47         if (mNativePtr == 0) {
     48             throw new IllegalStateException("error creating native FontFamily");
     49         }
     50     }
     51 
     52     @Override
     53     protected void finalize() throws Throwable {
     54         try {
     55             nUnrefFamily(mNativePtr);
     56         } finally {
     57             super.finalize();
     58         }
     59     }
     60 
     61     public boolean addFont(String path) {
     62         return nAddFont(mNativePtr, path);
     63     }
     64 
     65     public boolean addFontWeightStyle(String path, int weight, boolean style) {
     66         return nAddFontWeightStyle(mNativePtr, path, weight, style);
     67     }
     68 
     69     public boolean addFontFromAsset(AssetManager mgr, String path) {
     70         return nAddFontFromAsset(mNativePtr, mgr, path);
     71     }
     72 
     73     private static native long nCreateFamily(String lang, int variant);
     74     private static native void nUnrefFamily(long nativePtr);
     75     private static native boolean nAddFont(long nativeFamily, String path);
     76     private static native boolean nAddFontWeightStyle(long nativeFamily, String path,
     77             int weight, boolean isItalic);
     78     private static native boolean nAddFontFromAsset(long nativeFamily, AssetManager mgr,
     79             String path);
     80 }
     81