• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.theme.app;
18 
19 import android.graphics.Paint;
20 import android.graphics.Typeface;
21 import android.graphics.fonts.Font;
22 import android.graphics.fonts.FontFamily;
23 import android.graphics.fonts.FontVariationAxis;
24 import android.graphics.fonts.SystemFonts;
25 import android.graphics.text.PositionedGlyphs;
26 import android.graphics.text.TextRunShaper;
27 
28 import java.io.File;
29 
30 public class TypefaceTestUtil {
getFirstFont(String text, Paint p)31     public static File getFirstFont(String text, Paint p) {
32         PositionedGlyphs glyphs = TextRunShaper.shapeTextRun(
33                 text, 0, text.length(), 0, text.length(), 0f, 0f, false, p);
34 
35         requireNotEqual(0, glyphs.glyphCount());
36         Font font = glyphs.getFont(0);
37         requireNonNull(font.getFile());
38         return font.getFile();
39     }
40 
getRobotoTypeface(int weight, boolean italic)41     public static Typeface getRobotoTypeface(int weight, boolean italic) {
42         FontFamily.Builder builder = null;
43         for (Font font : SystemFonts.getAvailableFonts()) {
44             boolean useThisFont = false;
45             if (font.getFile().getName().startsWith("Roboto-")) {
46                 for (FontVariationAxis axis : font.getAxes()) {
47                     if (axis.getTag().equals("wdth") && axis.getStyleValue() == 100f) {
48                         useThisFont = true;
49                     }
50                 }
51             } else if (font.getFile().getName().equals("RobotoStatic-Regular.ttf")) {
52                 useThisFont = true;
53             }
54             if (useThisFont) {
55                 if (builder == null) {
56                     builder = new FontFamily.Builder(font);
57                 } else {
58                     builder.addFont(font);
59                 }
60             }
61         }
62         requireNonNull(builder);
63         Typeface tf = new Typeface.CustomFallbackBuilder(builder.build()).build();
64         requireNonNull(tf);
65         return Typeface.create(tf, weight, italic);
66     }
67 
requireNonNull(Object obj)68     private static void requireNonNull(Object obj) {
69         if (obj == null) {
70             throw new RuntimeException("Must not null");
71         }
72     }
73 
requireNotEqual(int l, int r)74     private static void requireNotEqual(int l, int r) {
75         if (l == r) {
76             throw new RuntimeException("Must not equal");
77         }
78     }
79 }
80