Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android): support get custom type face #3546

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading