|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * <p>This source code is licensed under the MIT license found in the LICENSE file in the root |
| 5 | + * directory of this source tree. |
| 6 | + */ |
| 7 | +package com.facebook.react.views.text; |
| 8 | + |
| 9 | +import android.content.res.AssetManager; |
| 10 | +import android.graphics.Paint; |
| 11 | +import android.graphics.Typeface; |
| 12 | + |
| 13 | +import androidx.annotation.Nullable; |
| 14 | + |
| 15 | +public class ReactTypefaceUtils { |
| 16 | + public static final int UNSET = -1; |
| 17 | + |
| 18 | + public static int parseFontWeight(@Nullable String fontWeightString) { |
| 19 | + int fontWeightNumeric = |
| 20 | + fontWeightString != null ? parseNumericFontWeight(fontWeightString) : UNSET; |
| 21 | + int fontWeight = fontWeightNumeric != UNSET ? fontWeightNumeric : Typeface.NORMAL; |
| 22 | + |
| 23 | + if (fontWeight == 700 || "bold".equals(fontWeightString)) fontWeight = Typeface.BOLD; |
| 24 | + else if (fontWeight == 400 || "normal".equals(fontWeightString)) fontWeight = Typeface.NORMAL; |
| 25 | + |
| 26 | + return fontWeight; |
| 27 | + } |
| 28 | + |
| 29 | + public static int parseFontStyle(@Nullable String fontStyleString) { |
| 30 | + int fontStyle = UNSET; |
| 31 | + if ("italic".equals(fontStyleString)) { |
| 32 | + fontStyle = Typeface.ITALIC; |
| 33 | + } else if ("normal".equals(fontStyleString)) { |
| 34 | + fontStyle = Typeface.NORMAL; |
| 35 | + } |
| 36 | + |
| 37 | + return fontStyle; |
| 38 | + } |
| 39 | + |
| 40 | + public static Typeface applyStyles(@Nullable Typeface typeface, |
| 41 | + int style, int weight, @Nullable String family, AssetManager assetManager) { |
| 42 | + int oldStyle; |
| 43 | + if (typeface == null) { |
| 44 | + oldStyle = 0; |
| 45 | + } else { |
| 46 | + oldStyle = typeface.getStyle(); |
| 47 | + } |
| 48 | + |
| 49 | + int want = 0; |
| 50 | + if ((weight == Typeface.BOLD) |
| 51 | + || ((oldStyle & Typeface.BOLD) != 0 && weight == ReactTextShadowNode.UNSET)) { |
| 52 | + want |= Typeface.BOLD; |
| 53 | + } |
| 54 | + |
| 55 | + if ((style == Typeface.ITALIC) |
| 56 | + || ((oldStyle & Typeface.ITALIC) != 0 && style == ReactTextShadowNode.UNSET)) { |
| 57 | + want |= Typeface.ITALIC; |
| 58 | + } |
| 59 | + |
| 60 | + if (family != null) { |
| 61 | + typeface = ReactFontManager.getInstance().getTypeface(family, want, weight, assetManager); |
| 62 | + } else if (typeface != null) { |
| 63 | + // TODO(t9055065): Fix custom fonts getting applied to text children with different style |
| 64 | + typeface = Typeface.create(typeface, want); |
| 65 | + } |
| 66 | + |
| 67 | + if (typeface != null) { |
| 68 | + return typeface; |
| 69 | + } else { |
| 70 | + return Typeface.defaultFromStyle(want); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Return -1 if the input string is not a valid numeric fontWeight (100, 200, ..., 900), otherwise |
| 76 | + * return the weight. |
| 77 | + */ |
| 78 | + private static int parseNumericFontWeight(String fontWeightString) { |
| 79 | + // This should be much faster than using regex to verify input and Integer.parseInt |
| 80 | + return fontWeightString.length() == 3 |
| 81 | + && fontWeightString.endsWith("00") |
| 82 | + && fontWeightString.charAt(0) <= '9' |
| 83 | + && fontWeightString.charAt(0) >= '1' |
| 84 | + ? 100 * (fontWeightString.charAt(0) - '0') |
| 85 | + : UNSET; |
| 86 | + } |
| 87 | +} |
0 commit comments