diff --git a/framework/android/src/main/java/com/tencent/mtt/hippy/adapter/font/DefaultFontScaleAdapter.java b/framework/android/src/main/java/com/tencent/mtt/hippy/adapter/font/DefaultFontScaleAdapter.java index fcbcff1d81f..91f3c90f4d9 100644 --- a/framework/android/src/main/java/com/tencent/mtt/hippy/adapter/font/DefaultFontScaleAdapter.java +++ b/framework/android/src/main/java/com/tencent/mtt/hippy/adapter/font/DefaultFontScaleAdapter.java @@ -13,27 +13,31 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package com.tencent.mtt.hippy.adapter.font; -import com.tencent.mtt.hippy.utils.LogUtils; +import android.graphics.Typeface; @SuppressWarnings({"unused"}) public class DefaultFontScaleAdapter implements HippyFontScaleAdapter { - @Override - public float getFontScale() { - return 1; - } + @Override + public float getFontScale() { + return 1; + } + + @Override + public CharSequence getEmoticonText(CharSequence text, int fontSize) { + return text; + } - @Override - public CharSequence getEmoticonText(CharSequence text, int fontSize) { - return text; - } + @Override + public String getCustomFontFilePath(String fontFamilyName, int style) { + return null; + } - @Override - public String getCustomFontFilePath(String fontFamilyName, int style) { - LogUtils.d("DefaultFontScaleAdapter", - "getCustomFontFilePath fontFamilyName=" + fontFamilyName + ", style=" + style); - return null; - } + @Override + public Typeface getCustomTypeface(String fontFamily, int style) { + return null; + } } diff --git a/renderer/native/android/src/main/java/com/tencent/renderer/component/text/FontAdapter.java b/renderer/native/android/src/main/java/com/tencent/renderer/component/text/FontAdapter.java index 45c0c2bf1d4..bdf09d96d98 100644 --- a/renderer/native/android/src/main/java/com/tencent/renderer/component/text/FontAdapter.java +++ b/renderer/native/android/src/main/java/com/tencent/renderer/component/text/FontAdapter.java @@ -16,6 +16,8 @@ package com.tencent.renderer.component.text; +import android.graphics.Typeface; + /** * Provide and implement by host */ @@ -45,4 +47,13 @@ public interface FontAdapter { * @return a {@link String} that represents custom font file path */ String getCustomFontFilePath(String fontFamily, int style); + + /** + * Get the type face customized by the host. + * + * @param fontFamily the property of text node + * @param style the typeface's intrinsic style attributes + * @return a {@link Typeface} + */ + Typeface getCustomTypeface(String fontFamily, int style); } diff --git a/renderer/native/android/src/main/java/com/tencent/renderer/component/text/TypeFaceUtil.java b/renderer/native/android/src/main/java/com/tencent/renderer/component/text/TypeFaceUtil.java index 91ffa6d562b..1971746d376 100644 --- a/renderer/native/android/src/main/java/com/tencent/renderer/component/text/TypeFaceUtil.java +++ b/renderer/native/android/src/main/java/com/tencent/renderer/component/text/TypeFaceUtil.java @@ -38,13 +38,17 @@ public class TypeFaceUtil { public static Typeface getTypeface(String fontFamilyName, int style, @Nullable FontAdapter fontAdapter) { - String cache = fontFamilyName + style; - Typeface typeface = sFontCache.get(cache); + Typeface typeface = + (fontAdapter != null) ? fontAdapter.getCustomTypeface(fontFamilyName, style) : null; if (typeface == null) { - typeface = createTypeface(fontFamilyName, style, fontAdapter); - } - if (typeface != null) { - sFontCache.put(cache, typeface); + String cache = fontFamilyName + style; + typeface = sFontCache.get(cache); + if (typeface == null) { + typeface = createTypeface(fontFamilyName, style, fontAdapter); + } + if (typeface != null) { + sFontCache.put(cache, typeface); + } } return typeface; } @@ -56,7 +60,8 @@ private static Typeface createTypeface(String fontFamilyName, int style, for (String fileExtension : FONT_EXTENSIONS) { String fileName = FONTS_PATH + fontFamilyName + extension + fileExtension; try { - typeface = Typeface.createFromAsset(ContextHolder.getAppContext().getAssets(), fileName); + typeface = Typeface.createFromAsset(ContextHolder.getAppContext().getAssets(), + fileName); if (typeface != null) { return typeface; } @@ -71,7 +76,8 @@ private static Typeface createTypeface(String fontFamilyName, int style, fileName = FONTS_PATH + fontFamilyName + fileExtension; try { typeface = Typeface.create( - Typeface.createFromAsset(ContextHolder.getAppContext().getAssets(), fileName), style); + Typeface.createFromAsset(ContextHolder.getAppContext().getAssets(), + fileName), style); if (typeface != null) { return typeface; }