diff --git a/doric-android/app/src/main/res/font/font_iconfont.ttf b/doric-android/app/src/main/res/font/font_iconfont.ttf new file mode 100644 index 00000000..6ea00a93 Binary files /dev/null and b/doric-android/app/src/main/res/font/font_iconfont.ttf differ diff --git a/doric-android/doric/src/main/java/pub/doric/shader/TextNode.java b/doric-android/doric/src/main/java/pub/doric/shader/TextNode.java index 65a3d90a..8ebc9fb5 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/TextNode.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/TextNode.java @@ -23,6 +23,8 @@ import android.util.TypedValue; import android.view.Gravity; import android.widget.TextView; +import androidx.core.content.res.ResourcesCompat; + import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSValue; @@ -30,6 +32,7 @@ import pub.doric.DoricContext; import pub.doric.extension.bridge.DoricPlugin; import pub.doric.shader.richtext.CustomTagHandler; import pub.doric.shader.richtext.HtmlParser; +import pub.doric.utils.DoricLog; import pub.doric.utils.DoricUtils; /** @@ -122,12 +125,42 @@ public class TextNode extends ViewNode { return; } String font = prop.asString().toString(); - if (font.endsWith(".ttf")) { - Typeface iconFont = Typeface.createFromAsset(getContext().getAssets(), font); - view.setTypeface(iconFont); + String fontPath=""; + String fontName=font; + if(font.contains("/")){ + int separatorIndex=font.lastIndexOf("/"); + fontPath=font.substring(0,separatorIndex+1); + fontName=font.substring(separatorIndex+1); + } + + if (fontName.endsWith(".ttf")) { + fontName = fontName.replace(".ttf",""); + } + + int resId = getContext().getResources().getIdentifier( + fontName.toLowerCase(), + "font", + getContext().getPackageName()); + if (resId > 0) { + try { + Typeface iconFont = ResourcesCompat.getFont(getContext(), resId); + view.setTypeface(iconFont); + }catch (Exception e){ + DoricLog.e("Error Font asset "+font+" in res/font"); + } + } else { - Typeface iconFont = Typeface.createFromAsset(getContext().getAssets(), font + ".ttf"); - view.setTypeface(iconFont); + fontName = fontPath+ + fontName + + ".ttf"; + try { + Typeface iconFont = Typeface.createFromAsset(getContext().getAssets(), fontName); + view.setTypeface(iconFont); + }catch (Exception e){ + e.printStackTrace(); + DoricLog.e(font+" not found in Assets"); + } + } break; diff --git a/doric-demo/bundle/fonts/assets_iconfont.ttf b/doric-demo/bundle/fonts/assets_iconfont.ttf new file mode 100644 index 00000000..6ea00a93 Binary files /dev/null and b/doric-demo/bundle/fonts/assets_iconfont.ttf differ diff --git a/doric-demo/src/TextDemo.ts b/doric-demo/src/TextDemo.ts index d8176a33..fa63029d 100644 --- a/doric-demo/src/TextDemo.ts +++ b/doric-demo/src/TextDemo.ts @@ -46,14 +46,35 @@ class TextDemo extends Panel { fontStyle: "bold_italic" }), text({ - text: "This is Icon Font text \ue631", + text: "Icon Font text from \ue631 ", + textSize: 10, font: 'iconfont' }), text({ - text: "This is Icon Font text \ue631", + text: "Icon Font text from \ue631 ", textSize: 30, font: 'iconfont' }), + text({ + text: "Icon Font text from res/font/ \ue631 ", + textSize: 10, + font: 'font_iconfont' + }), + text({ + text: "Icon Font text from res/font/ \ue631 ", + textSize: 30, + font: 'font_iconfont.ttf' + }), + text({ + text: "Icon Font text from assets/fonts/ \ue631 ", + textSize: 10, + font: 'fonts/assets_iconfont' + }), + text({ + text: "Icon Font text from assets/fonts/ \ue631 ", + textSize: 30, + font: 'fonts/assets_iconfont.ttf' + }), text({ text: "This is line Spaceing 0,\nSecond line", maxLines: 0,