| 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 | |
| Roozbeh Pournader | 99975a3 | 2017-08-09 09:42:20 -0700 | [diff] [blame] | 19 | import android.annotation.Nullable; |
| Raph Levien | d573794 | 2014-06-01 22:52:12 -0700 | [diff] [blame] | 20 | import android.content.res.AssetManager; |
| Seigo Nonaka | ff55115 | 2017-03-28 16:16:41 -0700 | [diff] [blame] | 21 | import android.graphics.fonts.FontVariationAxis; |
| Roozbeh Pournader | 99975a3 | 2017-08-09 09:42:20 -0700 | [diff] [blame] | 22 | import android.text.TextUtils; |
| Raph Levien | 296bf8c | 2016-04-06 15:23:57 -0700 | [diff] [blame] | 23 | import android.util.Log; |
| Siyamed Sinir | 46dd24a | 2018-02-09 12:10:34 -0800 | [diff] [blame] | 24 | |
| Seigo Nonaka | 8b48e62 | 2017-01-07 15:33:38 +0900 | [diff] [blame] | 25 | import dalvik.annotation.optimization.CriticalNative; |
| Raph Levien | d573794 | 2014-06-01 22:52:12 -0700 | [diff] [blame] | 26 | |
| Seigo Nonaka | abe5031 | 2018-02-20 14:49:59 -0800 | [diff] [blame^] | 27 | import libcore.util.NativeAllocationRegistry; |
| 28 | |
| Raph Levien | 296bf8c | 2016-04-06 15:23:57 -0700 | [diff] [blame] | 29 | import java.io.FileInputStream; |
| 30 | import java.io.IOException; |
| Ben Wagner | fb95699 | 2016-01-28 16:05:33 -0500 | [diff] [blame] | 31 | import java.nio.ByteBuffer; |
| Raph Levien | 296bf8c | 2016-04-06 15:23:57 -0700 | [diff] [blame] | 32 | import java.nio.channels.FileChannel; |
| Ben Wagner | a87b07d | 2015-11-06 11:57:09 -0500 | [diff] [blame] | 33 | |
| Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 34 | /** |
| 35 | * A family of typefaces with different styles. |
| 36 | * |
| 37 | * @hide |
| 38 | */ |
| 39 | public class FontFamily { |
| Raph Levien | 296bf8c | 2016-04-06 15:23:57 -0700 | [diff] [blame] | 40 | |
| 41 | private static String TAG = "FontFamily"; |
| 42 | |
| Seigo Nonaka | abe5031 | 2018-02-20 14:49:59 -0800 | [diff] [blame^] | 43 | private static final NativeAllocationRegistry sBuilderRegistry = new NativeAllocationRegistry( |
| 44 | FontFamily.class.getClassLoader(), nGetBuilderReleaseFunc(), 64); |
| 45 | |
| 46 | private @Nullable Runnable mNativeBuilderCleaner; |
| 47 | |
| 48 | private static final NativeAllocationRegistry sFamilyRegistry = new NativeAllocationRegistry( |
| 49 | FontFamily.class.getClassLoader(), nGetFamilyReleaseFunc(), 64); |
| 50 | |
| Raph Levien | 9a5b61c | 2014-04-29 18:26:48 -0700 | [diff] [blame] | 51 | /** |
| 52 | * @hide |
| 53 | */ |
| Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 54 | public long mNativePtr; |
| Raph Levien | 9a5b61c | 2014-04-29 18:26:48 -0700 | [diff] [blame] | 55 | |
| Seigo Nonaka | 8b48e62 | 2017-01-07 15:33:38 +0900 | [diff] [blame] | 56 | // Points native font family builder. Must be zero after freezing this family. |
| 57 | private long mBuilderPtr; |
| 58 | |
| Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 59 | public FontFamily() { |
| Seigo Nonaka | 8b48e62 | 2017-01-07 15:33:38 +0900 | [diff] [blame] | 60 | mBuilderPtr = nInitBuilder(null, 0); |
| Seigo Nonaka | abe5031 | 2018-02-20 14:49:59 -0800 | [diff] [blame^] | 61 | mNativeBuilderCleaner = sBuilderRegistry.registerNativeAllocation(this, mBuilderPtr); |
| Raph Levien | f9e3d31 | 2014-05-27 16:33:11 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| Roozbeh Pournader | 99975a3 | 2017-08-09 09:42:20 -0700 | [diff] [blame] | 64 | public FontFamily(@Nullable String[] langs, int variant) { |
| 65 | final String langsString; |
| 66 | if (langs == null || langs.length == 0) { |
| 67 | langsString = null; |
| 68 | } else if (langs.length == 1) { |
| 69 | langsString = langs[0]; |
| 70 | } else { |
| 71 | langsString = TextUtils.join(",", langs); |
| 72 | } |
| 73 | mBuilderPtr = nInitBuilder(langsString, variant); |
| Seigo Nonaka | abe5031 | 2018-02-20 14:49:59 -0800 | [diff] [blame^] | 74 | mNativeBuilderCleaner = sBuilderRegistry.registerNativeAllocation(this, mBuilderPtr); |
| Seigo Nonaka | 8b48e62 | 2017-01-07 15:33:38 +0900 | [diff] [blame] | 75 | } |
| 76 | |
| Seigo Nonaka | 5b6347a | 2017-03-31 14:30:05 -0700 | [diff] [blame] | 77 | /** |
| 78 | * Finalize the FontFamily creation. |
| 79 | * |
| 80 | * @return boolean returns false if some error happens in native code, e.g. broken font file is |
| 81 | * passed, etc. |
| 82 | */ |
| 83 | public boolean freeze() { |
| Seigo Nonaka | 8b48e62 | 2017-01-07 15:33:38 +0900 | [diff] [blame] | 84 | if (mBuilderPtr == 0) { |
| 85 | throw new IllegalStateException("This FontFamily is already frozen"); |
| Raph Levien | 9a5b61c | 2014-04-29 18:26:48 -0700 | [diff] [blame] | 86 | } |
| Seigo Nonaka | 8b48e62 | 2017-01-07 15:33:38 +0900 | [diff] [blame] | 87 | mNativePtr = nCreateFamily(mBuilderPtr); |
| Seigo Nonaka | abe5031 | 2018-02-20 14:49:59 -0800 | [diff] [blame^] | 88 | mNativeBuilderCleaner.run(); |
| Seigo Nonaka | 8b48e62 | 2017-01-07 15:33:38 +0900 | [diff] [blame] | 89 | mBuilderPtr = 0; |
| Seigo Nonaka | abe5031 | 2018-02-20 14:49:59 -0800 | [diff] [blame^] | 90 | if (mNativePtr != 0) { |
| 91 | sFamilyRegistry.registerNativeAllocation(this, mNativePtr); |
| 92 | } |
| Seigo Nonaka | 5b6347a | 2017-03-31 14:30:05 -0700 | [diff] [blame] | 93 | return mNativePtr != 0; |
| Seigo Nonaka | 8b48e62 | 2017-01-07 15:33:38 +0900 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | public void abortCreation() { |
| 97 | if (mBuilderPtr == 0) { |
| 98 | throw new IllegalStateException("This FontFamily is already frozen or abandoned"); |
| 99 | } |
| Seigo Nonaka | abe5031 | 2018-02-20 14:49:59 -0800 | [diff] [blame^] | 100 | mNativeBuilderCleaner.run(); |
| Seigo Nonaka | 8b48e62 | 2017-01-07 15:33:38 +0900 | [diff] [blame] | 101 | mBuilderPtr = 0; |
| Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 102 | } |
| Raph Levien | 15cf475 | 2014-05-05 16:08:07 -0700 | [diff] [blame] | 103 | |
| Seigo Nonaka | ff55115 | 2017-03-28 16:16:41 -0700 | [diff] [blame] | 104 | public boolean addFont(String path, int ttcIndex, FontVariationAxis[] axes, int weight, |
| Seigo Nonaka | 20e5d91 | 2017-01-18 20:31:27 +0900 | [diff] [blame] | 105 | int italic) { |
| Seigo Nonaka | 8b48e62 | 2017-01-07 15:33:38 +0900 | [diff] [blame] | 106 | if (mBuilderPtr == 0) { |
| 107 | throw new IllegalStateException("Unable to call addFont after freezing."); |
| 108 | } |
| Raph Levien | 296bf8c | 2016-04-06 15:23:57 -0700 | [diff] [blame] | 109 | try (FileInputStream file = new FileInputStream(path)) { |
| 110 | FileChannel fileChannel = file.getChannel(); |
| 111 | long fontSize = fileChannel.size(); |
| 112 | ByteBuffer fontBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fontSize); |
| Seigo Nonaka | 20e5d91 | 2017-01-18 20:31:27 +0900 | [diff] [blame] | 113 | if (axes != null) { |
| Seigo Nonaka | ff55115 | 2017-03-28 16:16:41 -0700 | [diff] [blame] | 114 | for (FontVariationAxis axis : axes) { |
| 115 | nAddAxisValue(mBuilderPtr, axis.getOpenTypeTagValue(), axis.getStyleValue()); |
| Seigo Nonaka | 20e5d91 | 2017-01-18 20:31:27 +0900 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | return nAddFont(mBuilderPtr, fontBuffer, ttcIndex, weight, italic); |
| Raph Levien | 296bf8c | 2016-04-06 15:23:57 -0700 | [diff] [blame] | 119 | } catch (IOException e) { |
| 120 | Log.e(TAG, "Error mapping font file " + path); |
| 121 | return false; |
| 122 | } |
| Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| Seigo Nonaka | ff55115 | 2017-03-28 16:16:41 -0700 | [diff] [blame] | 125 | public boolean addFontFromBuffer(ByteBuffer font, int ttcIndex, FontVariationAxis[] axes, |
| Seigo Nonaka | 20e5d91 | 2017-01-18 20:31:27 +0900 | [diff] [blame] | 126 | int weight, int italic) { |
| Seigo Nonaka | 8b48e62 | 2017-01-07 15:33:38 +0900 | [diff] [blame] | 127 | if (mBuilderPtr == 0) { |
| 128 | throw new IllegalStateException("Unable to call addFontWeightStyle after freezing."); |
| 129 | } |
| Seigo Nonaka | 20e5d91 | 2017-01-18 20:31:27 +0900 | [diff] [blame] | 130 | if (axes != null) { |
| Seigo Nonaka | ff55115 | 2017-03-28 16:16:41 -0700 | [diff] [blame] | 131 | for (FontVariationAxis axis : axes) { |
| 132 | nAddAxisValue(mBuilderPtr, axis.getOpenTypeTagValue(), axis.getStyleValue()); |
| Seigo Nonaka | 20e5d91 | 2017-01-18 20:31:27 +0900 | [diff] [blame] | 133 | } |
| Seigo Nonaka | ac873c9 | 2017-03-07 15:34:53 -0800 | [diff] [blame] | 134 | } |
| Seigo Nonaka | 20e5d91 | 2017-01-18 20:31:27 +0900 | [diff] [blame] | 135 | return nAddFontWeightStyle(mBuilderPtr, font, ttcIndex, weight, italic); |
| Raph Levien | 117cbeb | 2014-08-25 13:47:16 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| Clara Bayarri | b44abf2 | 2017-02-16 14:19:19 +0000 | [diff] [blame] | 138 | /** |
| 139 | * @param mgr The AssetManager to use for this context. |
| 140 | * @param path The path to the font file to load. |
| 141 | * @param cookie If available, the resource cookie given by Resources. |
| 142 | * @param isAsset {@code true} if this is from the assets/ folder, {@code false} if from |
| 143 | * resources |
| 144 | * @param weight The weight of the font. If 0 is given, the weight and italic will be resolved |
| 145 | * using the OS/2 table in the font. |
| 146 | * @param isItalic Whether this font is italic. If the weight is set to 0, this will be resolved |
| 147 | * using the OS/2 table in the font. |
| 148 | * @return |
| 149 | */ |
| Clara Bayarri | 18e9f9f | 2016-12-19 16:20:29 +0000 | [diff] [blame] | 150 | public boolean addFontFromAssetManager(AssetManager mgr, String path, int cookie, |
| Seigo Nonaka | 20e5d91 | 2017-01-18 20:31:27 +0900 | [diff] [blame] | 151 | boolean isAsset, int ttcIndex, int weight, int isItalic, |
| Seigo Nonaka | ff55115 | 2017-03-28 16:16:41 -0700 | [diff] [blame] | 152 | FontVariationAxis[] axes) { |
| Seigo Nonaka | 8b48e62 | 2017-01-07 15:33:38 +0900 | [diff] [blame] | 153 | if (mBuilderPtr == 0) { |
| 154 | throw new IllegalStateException("Unable to call addFontFromAsset after freezing."); |
| 155 | } |
| Seigo Nonaka | 20e5d91 | 2017-01-18 20:31:27 +0900 | [diff] [blame] | 156 | if (axes != null) { |
| Seigo Nonaka | ff55115 | 2017-03-28 16:16:41 -0700 | [diff] [blame] | 157 | for (FontVariationAxis axis : axes) { |
| 158 | nAddAxisValue(mBuilderPtr, axis.getOpenTypeTagValue(), axis.getStyleValue()); |
| Seigo Nonaka | 20e5d91 | 2017-01-18 20:31:27 +0900 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | return nAddFontFromAssetManager(mBuilderPtr, mgr, path, cookie, isAsset, ttcIndex, weight, |
| 162 | isItalic); |
| Raph Levien | d573794 | 2014-06-01 22:52:12 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| Seigo Nonaka | 2660aca | 2017-03-22 09:14:33 -0700 | [diff] [blame] | 165 | // TODO: Remove once internal user stop using private API. |
| 166 | private static boolean nAddFont(long builderPtr, ByteBuffer font, int ttcIndex) { |
| 167 | return nAddFont(builderPtr, font, ttcIndex, -1, -1); |
| 168 | } |
| 169 | |
| Roozbeh Pournader | 99975a3 | 2017-08-09 09:42:20 -0700 | [diff] [blame] | 170 | private static native long nInitBuilder(String langs, int variant); |
| Seigo Nonaka | 8b48e62 | 2017-01-07 15:33:38 +0900 | [diff] [blame] | 171 | |
| 172 | @CriticalNative |
| 173 | private static native long nCreateFamily(long mBuilderPtr); |
| 174 | |
| 175 | @CriticalNative |
| Seigo Nonaka | abe5031 | 2018-02-20 14:49:59 -0800 | [diff] [blame^] | 176 | private static native long nGetBuilderReleaseFunc(); |
| Seigo Nonaka | 8b48e62 | 2017-01-07 15:33:38 +0900 | [diff] [blame] | 177 | |
| 178 | @CriticalNative |
| Seigo Nonaka | abe5031 | 2018-02-20 14:49:59 -0800 | [diff] [blame^] | 179 | private static native long nGetFamilyReleaseFunc(); |
| Seigo Nonaka | 20e5d91 | 2017-01-18 20:31:27 +0900 | [diff] [blame] | 180 | // By passing -1 to weigth argument, the weight value is resolved by OS/2 table in the font. |
| 181 | // By passing -1 to italic argument, the italic value is resolved by OS/2 table in the font. |
| 182 | private static native boolean nAddFont(long builderPtr, ByteBuffer font, int ttcIndex, |
| 183 | int weight, int isItalic); |
| Seigo Nonaka | 8b48e62 | 2017-01-07 15:33:38 +0900 | [diff] [blame] | 184 | private static native boolean nAddFontWeightStyle(long builderPtr, ByteBuffer font, |
| Seigo Nonaka | 20e5d91 | 2017-01-18 20:31:27 +0900 | [diff] [blame] | 185 | int ttcIndex, int weight, int isItalic); |
| Seigo Nonaka | 8b48e62 | 2017-01-07 15:33:38 +0900 | [diff] [blame] | 186 | private static native boolean nAddFontFromAssetManager(long builderPtr, AssetManager mgr, |
| Seigo Nonaka | 20e5d91 | 2017-01-18 20:31:27 +0900 | [diff] [blame] | 187 | String path, int cookie, boolean isAsset, int ttcIndex, int weight, int isItalic); |
| Seigo Nonaka | ac873c9 | 2017-03-07 15:34:53 -0800 | [diff] [blame] | 188 | |
| 189 | // The added axis values are only valid for the next nAddFont* method call. |
| 190 | @CriticalNative |
| 191 | private static native void nAddAxisValue(long builderPtr, int tag, float value); |
| Raph Levien | 1a73f732 | 2014-01-30 16:06:28 -0800 | [diff] [blame] | 192 | } |