• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.fonts;
18 
19 import android.util.Pair;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.HashSet;
24 import java.util.Set;
25 
26 public class NativeSystemFontHelper {
27     static {
28         System.loadLibrary("ctsgraphics_jni");
29     }
30 
tagToStr(int tag)31     private static String tagToStr(int tag) {
32         char[] buf = new char[4];
33         buf[0] = (char) ((tag >> 24) & 0xFF);
34         buf[1] = (char) ((tag >> 16) & 0xFF);
35         buf[2] = (char) ((tag >> 8) & 0xFF);
36         buf[3] = (char) (tag & 0xFF);
37         return String.valueOf(buf);
38     }
39 
getAvailableFonts()40     public static Set<Font> getAvailableFonts() {
41         long iterPtr = nOpenIterator();
42         HashSet<Font> nativeFonts = new HashSet<>();
43         try {
44             for (long fontPtr = nNext(iterPtr); fontPtr != 0; fontPtr = nNext(iterPtr)) {
45                 try {
46                     FontVariationAxis[] axes = new FontVariationAxis[nGetAxisCount(fontPtr)];
47                     for (int i = 0; i < axes.length; ++i) {
48                         axes[i] = new FontVariationAxis(
49                                 tagToStr(nGetAxisTag(fontPtr, i)), nGetAxisValue(fontPtr, i));
50                     }
51                     nativeFonts.add(new Font.Builder(new File(nGetFilePath(fontPtr)))
52                             .setWeight(nGetWeight(fontPtr))
53                             .setSlant(nIsItalic(fontPtr)
54                                     ?  FontStyle.FONT_SLANT_ITALIC : FontStyle.FONT_SLANT_UPRIGHT)
55                             .setTtcIndex(nGetCollectionIndex(fontPtr))
56                             .setFontVariationSettings(axes)
57                             .build());
58                 } catch (IOException e) {
59                     throw new RuntimeException(e);
60                 } finally {
61                     nCloseFont(fontPtr);
62                 }
63             }
64         } finally {
65             nCloseIterator(iterPtr);
66         }
67         return nativeFonts;
68     }
69 
matchFamilyStyleCharacter(String familyName, int weight, boolean italic, String languageTags, int familyVariant, String text)70     public static Pair<File, Integer> matchFamilyStyleCharacter(String familyName, int weight,
71             boolean italic, String languageTags, int familyVariant, String text) {
72         final long fontPtr = nMatchFamilyStyleCharacter(familyName, weight, italic, languageTags,
73                 familyVariant, text);
74         final int runLength = nMatchFamilyStyleCharacter_runLength(familyName, weight, italic,
75                 languageTags, familyVariant, text);
76         try {
77             return new Pair<>(new File(nGetFilePath(fontPtr)), runLength);
78         } finally {
79             nCloseFont(fontPtr);
80         }
81     }
82 
nOpenIterator()83     private static native long nOpenIterator();
nCloseIterator(long ptr)84     private static native void nCloseIterator(long ptr);
nNext(long ptr)85     private static native long nNext(long ptr);
nCloseFont(long ptr)86     private static native void nCloseFont(long ptr);
nGetFilePath(long ptr)87     private static native String nGetFilePath(long ptr);
nGetWeight(long ptr)88     private static native int nGetWeight(long ptr);
nIsItalic(long ptr)89     private static native boolean nIsItalic(long ptr);
nGetLocale(long ptr)90     private static native String nGetLocale(long ptr);
nGetCollectionIndex(long ptr)91     private static native int nGetCollectionIndex(long ptr);
nGetAxisCount(long ptr)92     private static native int nGetAxisCount(long ptr);
nGetAxisTag(long ptr, int index)93     private static native int nGetAxisTag(long ptr, int index);
nGetAxisValue(long ptr, int index)94     private static native float nGetAxisValue(long ptr, int index);
nMatchFamilyStyleCharacter(String familyName, int weight, boolean italic, String languageTags, int familyVariant, String text)95     private static native long nMatchFamilyStyleCharacter(String familyName, int weight,
96             boolean italic, String languageTags, int familyVariant, String text);
nMatchFamilyStyleCharacter_runLength(String familyName, int weight, boolean italic, String languageTags, int familyVariant, String text)97     private static native int nMatchFamilyStyleCharacter_runLength(String familyName, int weight,
98             boolean italic, String languageTags, int familyVariant, String text);
99 }
100