Skip to content

Commit

Permalink
feat(android): support get custom type face
Browse files Browse the repository at this point in the history
  • Loading branch information
siguangli2018 authored and siguangli committed Oct 12, 2023
1 parent ce62a52 commit c609991
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.tencent.renderer.component.text;

import android.graphics.Typeface;

/**
* Provide and implement by host
*/
Expand Down Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down

0 comments on commit c609991

Please sign in to comment.