| Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 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 | |
| Raph Levien | d573794 | 2014-06-01 22:52:12 -0700 | [diff] [blame] | 19 | import android.content.res.AssetManager; |
| 20 | |
| Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 21 | /** |
| 22 | * A family of typefaces with different styles. |
| 23 | * |
| 24 | * @hide |
| 25 | */ |
| 26 | public class FontFamily { |
| Raph Levien | 9a5b61c | 2014-04-29 18:26:48 -0700 | [diff] [blame] | 27 | /** |
| 28 | * @hide |
| 29 | */ |
| Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 30 | public long mNativePtr; |
| Raph Levien | 9a5b61c | 2014-04-29 18:26:48 -0700 | [diff] [blame] | 31 | |
| Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 32 | public FontFamily() { |
| Raph Levien | f9e3d31 | 2014-05-27 16:33:11 -0700 | [diff] [blame] | 33 | mNativePtr = nCreateFamily(null, 0); |
| Raph Levien | 9a5b61c | 2014-04-29 18:26:48 -0700 | [diff] [blame] | 34 | if (mNativePtr == 0) { |
| Raph Levien | f9e3d31 | 2014-05-27 16:33:11 -0700 | [diff] [blame] | 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"); |
| Raph Levien | 9a5b61c | 2014-04-29 18:26:48 -0700 | [diff] [blame] | 49 | } |
| Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 50 | } |
| Raph Levien | 15cf475 | 2014-05-05 16:08:07 -0700 | [diff] [blame] | 51 | |
| 52 | @Override |
| 53 | protected void finalize() throws Throwable { |
| 54 | try { |
| 55 | nUnrefFamily(mNativePtr); |
| 56 | } finally { |
| 57 | super.finalize(); |
| 58 | } |
| 59 | } |
| Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 60 | |
| Raph Levien | d573794 | 2014-06-01 22:52:12 -0700 | [diff] [blame] | 61 | public boolean addFont(String path) { |
| 62 | return nAddFont(mNativePtr, path); |
| Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| Raph Levien | 117cbeb | 2014-08-25 13:47:16 -0700 | [diff] [blame] | 65 | public boolean addFontWeightStyle(String path, int weight, boolean style) { |
| 66 | return nAddFontWeightStyle(mNativePtr, path, weight, style); |
| 67 | } |
| 68 | |
| Raph Levien | d573794 | 2014-06-01 22:52:12 -0700 | [diff] [blame] | 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); |
| Raph Levien | 117cbeb | 2014-08-25 13:47:16 -0700 | [diff] [blame] | 76 | private static native boolean nAddFontWeightStyle(long nativeFamily, String path, |
| 77 | int weight, boolean isItalic); |
| Raph Levien | d573794 | 2014-06-01 22:52:12 -0700 | [diff] [blame] | 78 | private static native boolean nAddFontFromAsset(long nativeFamily, AssetManager mgr, |
| 79 | String path); |
| Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 80 | } |