From 26d0a38357dda3442e075bd6fff3fac6065e1e77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=B6=9B?= Date: Mon, 25 May 2020 18:08:45 +0800 Subject: [PATCH] feat:android TextNode font support load from res --- .../app/src/main/res/font/font_iconfont.ttf | Bin 0 -> 2108 bytes .../main/java/pub/doric/shader/TextNode.java | 43 ++++++++++++++++-- doric-demo/bundle/fonts/assets_iconfont.ttf | Bin 0 -> 2108 bytes doric-demo/src/TextDemo.ts | 25 +++++++++- 4 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 doric-android/app/src/main/res/font/font_iconfont.ttf create mode 100644 doric-demo/bundle/fonts/assets_iconfont.ttf 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 0000000000000000000000000000000000000000..6ea00a93968c042413b0fded35f6c6d7e1b29ecb GIT binary patch literal 2108 zcmd^APiP!f9RA+S?##|c+N9~m9@;XSq&aBVY&L(?nrcinQ7F2!Yd{cmx|!Wg+1*(t z)6zhM1@Yji5j+IVAvZw;BZ8-bIeJjYp&*E@Jw-(dUc5-V@q2IHVod~lcXr?JeZTL0 zzu)`4w{PD-0O&y*Ch`lHUZ30Ue0dKLPm`T{?b29b=98C)=&#XVEH}K?&u@Hnj{c8q zt!jO>^5;*tJ_XDYV9eBfZ|TGf-*60f*`{kG>^tVW#5dW7YmMl7ih!}VXj1i{?1|IT zH!sjw4exqO4B%IepP=tHy@vl>sq+qyTxb49D_Ds(Ha_zhDubQ@Yd72bZM>O~K4|4? z9)1kPkiBUPzw*}6nLp4IYk&A)?iVSH`~7wT#tphD7&=^LGH@Gd|In<20Vcg2p}QM<{QCgu|F8@Ses3z4%;X14r<+#wPjiG)@rT);Ng)+|$@X zuP`-EV^Q>L+=CIZoIW4=UgR&imsj28a?q><%_!Xk7yathx)*lcZolYZWfGOLSm { 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 0000000000000000000000000000000000000000..6ea00a93968c042413b0fded35f6c6d7e1b29ecb GIT binary patch literal 2108 zcmd^APiP!f9RA+S?##|c+N9~m9@;XSq&aBVY&L(?nrcinQ7F2!Yd{cmx|!Wg+1*(t z)6zhM1@Yji5j+IVAvZw;BZ8-bIeJjYp&*E@Jw-(dUc5-V@q2IHVod~lcXr?JeZTL0 zzu)`4w{PD-0O&y*Ch`lHUZ30Ue0dKLPm`T{?b29b=98C)=&#XVEH}K?&u@Hnj{c8q zt!jO>^5;*tJ_XDYV9eBfZ|TGf-*60f*`{kG>^tVW#5dW7YmMl7ih!}VXj1i{?1|IT zH!sjw4exqO4B%IepP=tHy@vl>sq+qyTxb49D_Ds(Ha_zhDubQ@Yd72bZM>O~K4|4? z9)1kPkiBUPzw*}6nLp4IYk&A)?iVSH`~7wT#tphD7&=^LGH@Gd|In<20Vcg2p}QM<{QCgu|F8@Ses3z4%;X14r<+#wPjiG)@rT);Ng)+|$@X zuP`-EV^Q>L+=CIZoIW4=UgR&imsj28a?q><%_!Xk7yathx)*lcZolYZWfGOLSm