1818
1919import android .graphics .Paint ;
2020import android .graphics .Typeface ;
21- import android .os .Build ;
21+ import android .os .Build .VERSION ;
22+ import android .os .Build .VERSION_CODES ;
2223import android .text .TextUtils ;
2324
2425import android .util .SparseArray ;
25- import androidx .annotation .ChecksSdkIntAtLeast ;
2626import androidx .annotation .Nullable ;
2727
2828import com .tencent .mtt .hippy .utils .ContextHolder ;
@@ -43,26 +43,25 @@ public class TypeFaceUtil {
4343 private static final String [] FONT_EXTENSIONS = {".ttf" , ".otf" };
4444 private static final String FONTS_PATH = "fonts/" ;
4545 private static final Map <String , SparseArray <Typeface >> sFontCache = new HashMap <>();
46- @ ChecksSdkIntAtLeast (api = Build .VERSION_CODES .P )
47- private static final boolean SUPPORT_FONT_WEIGHT = Build .VERSION .SDK_INT >= Build .VERSION_CODES .P ;
4846
4947 /**
50- * @deprecated use {@link #getTypeface(String, int , boolean, FontAdapter)} instead
48+ * @deprecated use {@link #getTypeface(String, String , boolean, FontAdapter)} instead
5149 */
5250 @ Deprecated
5351 public static Typeface getTypeface (String fontFamilyName , int style ,
5452 @ Nullable FontAdapter fontAdapter ) {
5553 boolean italic = (style & Typeface .ITALIC ) != 0 ;
56- int weightNumber = (style & Typeface .BOLD ) != 0 ? WEIGHT_BOLE : WEIGHT_NORMAL ;
57- return getTypeface (fontFamilyName , weightNumber , italic , fontAdapter );
54+ String weight = (style & Typeface .BOLD ) != 0 ? TEXT_FONT_STYLE_BOLD : TEXT_FONT_STYLE_NORMAL ;
55+ return getTypeface (fontFamilyName , weight , italic , fontAdapter );
5856 }
5957
60- public static Typeface getTypeface (String fontFamilyName , int weight , boolean italic ,
58+ public static Typeface getTypeface (String fontFamilyName , String weight , boolean italic ,
6159 @ Nullable FontAdapter fontAdapter ) {
62- final int style = toStyle (weight , italic );
60+ int weightNumber = getWeightNumber (weight );
61+ final int style = toStyle (weight , weightNumber , italic );
6362 Typeface typeface = (fontAdapter != null ) ? fontAdapter .getCustomTypeface (fontFamilyName , style ) : null ;
6463 if (typeface == null ) {
65- final int key = SUPPORT_FONT_WEIGHT ? ((weight << 1 ) | (italic ? 1 : 0 )) : style ;
64+ final int key = ( weightNumber > 0 ) ? ((weightNumber << 1 ) | (italic ? 1 : 0 )) : style ;
6665 SparseArray <Typeface > cache = sFontCache .get (fontFamilyName );
6766 if (cache == null ) {
6867 cache = new SparseArray <>(4 );
@@ -71,7 +70,7 @@ public static Typeface getTypeface(String fontFamilyName, int weight, boolean it
7170 typeface = cache .get (key );
7271 }
7372 if (typeface == null ) {
74- typeface = createTypeface (fontFamilyName , weight , italic , fontAdapter );
73+ typeface = createTypeface (fontFamilyName , weightNumber , style , italic , fontAdapter );
7574 if (typeface != null ) {
7675 cache .put (key , typeface );
7776 }
@@ -80,9 +79,8 @@ public static Typeface getTypeface(String fontFamilyName, int weight, boolean it
8079 return typeface ;
8180 }
8281
83- private static Typeface createTypeface (String fontFamilyName , int weight , boolean italic ,
82+ private static Typeface createTypeface (String fontFamilyName , int weightNumber , int style , boolean italic ,
8483 @ Nullable FontAdapter fontAdapter ) {
85- final int style = toStyle (weight , italic );
8684 final String extension = EXTENSIONS [style ];
8785 final String [] familyNameList ;
8886 if (fontFamilyName .indexOf (',' ) == -1 ) {
@@ -113,8 +111,8 @@ private static Typeface createTypeface(String fontFamilyName, int weight, boolea
113111 try {
114112 Typeface typeface = Typeface .createFromAsset (ContextHolder .getAppContext ().getAssets (), fileName );
115113 if (typeface != null && !typeface .equals (Typeface .DEFAULT )) {
116- if (SUPPORT_FONT_WEIGHT ) {
117- return Typeface .create (typeface , weight , italic );
114+ if (VERSION . SDK_INT >= VERSION_CODES . P && weightNumber > 0 ) {
115+ return Typeface .create (typeface , weightNumber , italic );
118116 }
119117 // "bold" has no effect on api level < P, prefer to use `Paint.setFakeBoldText(boolean)`
120118 return italic ? Typeface .create (typeface , Typeface .ITALIC ) : typeface ;
@@ -138,46 +136,57 @@ private static Typeface createTypeface(String fontFamilyName, int weight, boolea
138136 }
139137 }
140138 }
141-
142139 final Typeface systemDefault = Typeface .create (Typeface .DEFAULT , style );
143140 for (String splitName : familyNameList ) {
144141 Typeface typeface = Typeface .create (splitName , style );
145142 if (typeface != null && !typeface .equals (systemDefault )) {
146- return SUPPORT_FONT_WEIGHT ? Typeface .create (typeface , weight , italic ) : typeface ;
143+ if (VERSION .SDK_INT >= VERSION_CODES .P && weightNumber > 0 ) {
144+ return Typeface .create (typeface , weightNumber , italic );
145+ }
146+ return typeface ;
147147 }
148148 }
149- return SUPPORT_FONT_WEIGHT ? Typeface .create (systemDefault , weight , italic ) : systemDefault ;
149+ return (VERSION .SDK_INT >= VERSION_CODES .P && weightNumber > 0 ) ?
150+ Typeface .create (systemDefault , weightNumber , italic ) : systemDefault ;
150151 }
151152
152- private static int toStyle (int weight , boolean italic ) {
153- return weight < WEIGHT_BOLE ?
154- (italic ? Typeface .ITALIC : Typeface .NORMAL ) :
155- (italic ? Typeface .BOLD_ITALIC : Typeface .BOLD );
153+ private static int getWeightNumber (String weight ) {
154+ int weightNumber = 0 ;
155+ try {
156+ weightNumber = Math .min (Math .max (1 , Integer .parseInt (weight )), 1000 );
157+ } catch (NumberFormatException ignored ) {
158+ // Weight supports setting non numeric strings
159+ }
160+ return weightNumber ;
156161 }
157162
158- /**
159- * @deprecated use {@link #apply(Paint, boolean, int, String, FontAdapter)} instead
160- */
161- @ Deprecated
162- public static void apply (Paint paint , int style , int weight , String family ,
163- @ Nullable FontAdapter fontAdapter ) {
164- boolean italic = style == Typeface .ITALIC ;
165- int weightNumber = weight == Typeface .BOLD ? WEIGHT_BOLE : WEIGHT_NORMAL ;
166- apply (paint , italic , weightNumber , family , fontAdapter );
163+ private static int toStyle (String weight , int weightNumber , boolean italic ) {
164+ if (weight .equals (TEXT_FONT_STYLE_NORMAL )) {
165+ return italic ? Typeface .ITALIC : Typeface .NORMAL ;
166+ } else if (weight .equals (TEXT_FONT_STYLE_BOLD )) {
167+ return italic ? Typeface .BOLD_ITALIC : Typeface .BOLD ;
168+ } else {
169+ return weightNumber < WEIGHT_BOLE ?
170+ (italic ? Typeface .ITALIC : Typeface .NORMAL ) :
171+ (italic ? Typeface .BOLD_ITALIC : Typeface .BOLD );
172+ }
167173 }
168174
169- public static void apply (Paint paint , boolean italic , int weight , String familyName ,
175+ public static void apply (Paint paint , boolean italic , String weight , String familyName ,
170176 @ Nullable FontAdapter fontAdapter ) {
171177 Typeface typeface ;
178+ int weightNumber = getWeightNumber (weight );
172179 if (TextUtils .isEmpty (familyName )) {
173180 final Typeface base = paint .getTypeface ();
174- typeface = SUPPORT_FONT_WEIGHT
175- ? Typeface .create (base , weight , italic )
176- : Typeface .create (base , toStyle (weight , italic ));
181+ if (VERSION .SDK_INT >= VERSION_CODES .P && weightNumber > 0 ) {
182+ typeface = Typeface .create (base , weightNumber , italic );
183+ } else {
184+ typeface = Typeface .create (base , toStyle (weight , weightNumber , italic ));
185+ }
177186 } else {
178187 typeface = getTypeface (familyName , weight , italic , fontAdapter );
179188 }
180- if (weight >= WEIGHT_BOLE && typeface != null && !typeface .isBold ()) {
189+ if (weightNumber >= WEIGHT_BOLE && typeface != null && !typeface .isBold ()) {
181190 paint .setFakeBoldText (true );
182191 }
183192 paint .setTypeface (typeface );
0 commit comments