Android中添加思源字体/NotoSansCJK/SourceHanSans
系统版本:Android4.2.2_r1
本文主要是在Android中添加思源字体的过程记录。思源字体是Google和Adobe在2014.07.18发布的中文字体。
1.获取思源字体(Google:NotoSansCJK;Adobe:SourceHanSans)。
2.解压后如下几个才是思源字体。
- NotoSansHans-Regular.otf常规
- NotoSansHans-Black.otf黑体
- NotoSansHans-DemiLight.otf
- NotoSansHans-Medium.otf
- NotoSansHans-Thin.otf
- NotoSansHans-Bold.otf加粗
- NotoSansHans-Light.otf
注:这里说下otf,意为opentype和ttf(truetype)是兼容的。
3.安装体验
由于Android中只使用到常规和加粗体,这里也只以这两个做实验(同时也有容量的考虑),在高PPI(分辨率)的屏幕上或许只是感觉到字形变了,在低分辨率的屏幕上变的更加清晰了,前后对比:
之前:
之后:
(上传到这里就不清晰了,真是怪,点击链接看原图更为清晰)
1)将NotoSansHans-Regular.otfNotoSansHans-Bold.otf放到/system/fonts/目录下。
2)将如下fallback_fonts.xml放到/vendor/etc/下。
<?xmlversion="1.0"encoding="utf-8"?>
<!--
Vendor-providedfallbackfonts
Thisfilecanbeeditedtoaddreferencestofontsthatarenotinstalledorreferencedinthe
defaultsystem.Thefileshouldthenbeplacedin/vendor/etc/fallback_fonts.xml.Note
thatinyourmakefile,thisdirectoryshouldbereferencedas$(TARGET_COPY_OUT_VENDOR)/etc/:
PRODUCT_COPY_FILES+=\
frameworks/base/data/fonts/vendor_fonts.xml:$(TARGET_COPY_OUT_VENDOR)/etc/fallback_fonts.xml
Forexample,vendorsmightwanttobuildconfigurationsforlocalesthatare
betterservedbyfontswhicheitherhandleglyphsnotsupportedinthedefaultfontsorwhich
handletheseglyphsdifferentlythanthedefaultfallbackfonts.
Eachentryinthislistisa"family",whichconsistsofalistof"files"
(thefilenamesforthatfamily).Thefilesobjectsare
providedintheorderofthestylessupportedforthatfamily:regular,bold,italic,and
bold-italic.Onlyprovidingonefontmeansthatallstyleswillberenderedwiththatfont.
Providingtwomeansthatthesetwofontswillrenderregularandboldfonts(italicswill
bemappedtothesetwofonts).
Thereisalsoanoptional"order"attributeontheFamilytag.Thisspecifiestheindexat
whichthatfamilyoffontsshouldbeinsertedinthefallbackfontlist,wherethe
defaultfallbackfontsonthesystem(in/system/etc/fallback_fonts.xml)startatindex0.
Ifno'order'attributeissupplied,thatfamilywillbeinsertedeitherattheendofthe
currentfallbacklist(ifnoorderwassuppliedforanypreviousfamilyinthisfile)or
afterthepreviousfamily(iftherewasanorderspecifiedpreviously).Typically,vendors
maywanttosupplyanorderforthefirstfamilythatputsthissetoffontsattheappropriate
placeintheoverallfallbackfonts.Theorderofthislistdetermineswhichfallbackfont
willbeusedtosupportanyglyphsthatarenothandledbythedefaultsystemfonts.
Hanlanguages(Chinese,Japanese,andKorean)shareacommonrangeofunicodecharacters;
theirorderinginthefallbackorvendorfilesgivesprioritytothefirstinthelist.
Language-specificorderingcanbeconfiguredbyaddingaBCP47-style"lang"attributeto
a"file"element;fontsmatchingthelanguageoftextbeingdrawnwillbeprioritisedover
allothers.
Thesampleconfigurationbelowisanexampleofhowonemightprovidetwofamiliesoffonts
thatgetinsertedatthefirstandsecond(0and1)positionintheoverallfallbackfonts.
See/system/etc/system_fonts.xmland/system/etc/fallback_fonts.xmlformoreinformation
andtounderstandtheorderinwhichthedefaultsystemfontsareloadedandstructuredfor
lookup.
-->
<!--Samplefallbackfontadditionstothedefaultfallbacklist.Thesefontswillbeadded
tothetoptwopositionsofthefallbacklist,sincethefirsthasanorderof0.-->
<familyset>
<familyorder="0">
<fileset>
<file>NotoSansHans-Regular.otf</file>
<file>NotoSansHans-Bold.otf</file>
</fileset>
</family>
<!--Italic等等会造成启动不了的问题,所以只添加前两个足矣。
<family>
<fileset>
<file>NotoSansHans-Regular.otf</file>
<file>NotoSansHans-Italic.otf</file>
<file>NotoSansHans-Bolditalic.otf</file>
<file>NotoSansHans-Black.otf</file>
<file>NotoSansHans-DemiLight.otf</file>
<file>NotoSansHans-Light.otf</file>
<file>NotoSansHans-Medium.otf</file>
<file>NotoSansHans-Thin.otf</file>
</fileset>
</family>
-->
</familyset>
同时也有做好的字库加xml在这里下载,还原的时将这三个文件删除即可。
4.以源码方式添加
如果需要在制作rom时就可以自动添加默认中文字体,思路和上边的类似,以下是gitlog信息。
commitxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Author:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Date:MonAug1815:44:242014+0800
AddNotoSansCJKfontsforChinese
diff--gita/device/rockchip/rk30sdk/device.mkb/device/rockchip/rk30sdk/device.mk
index8befd40..b4e2822100755
---a/device/rockchip/rk30sdk/device.mk
+++b/device/rockchip/rk30sdk/device.mk
@@-119,6+119,10@@PRODUCT_COPY_FILES+=\
+#fonts
+PRODUCT_COPY_FILES+=\
+frameworks/base/data/fonts/vendor_fonts.xml:$(TARGET_COPY_OUT_VENDOR)/etc/fallback_fonts.xml
diff--gita/frameworks/base/data/fonts/Android.mkb/frameworks/base/data/fonts/Android.mk
indexe02e95a..0b4bc6c100755
---a/frameworks/base/data/fonts/Android.mk
+++b/frameworks/base/data/fonts/Android.mk
@@-157,6+157,8@@font_src_files+=\
DroidSansHebrew-Regular.ttf\
DroidSansHebrew-Bold.ttf\
DroidSansThai.ttf\
+NotoSansHans-Regular.otf\
+NotoSansHans-Bold.otf\
DroidSansArmenian.ttf\
DroidSansGeorgian.ttf\
AndroidEmoji.ttf
diff--gita/frameworks/base/data/fonts/NotoSansHans-Bold.otfb/frameworks/base/data/fonts/NotoSansHans-Bold.otf
newfilemode100644
index0000000..006372e
Binaryfiles/dev/nullandb/frameworks/base/data/fonts/NotoSansHans-Bold.otfdiffer
diff--gita/frameworks/base/data/fonts/NotoSansHans-Regular.otfb/frameworks/base/data/fonts/NotoSansHans-Regular.otf
newfilemode100644
index0000000..923f89c
Binaryfiles/dev/nullandb/frameworks/base/data/fonts/NotoSansHans-Regular.otfdiffer
diff--gita/frameworks/base/data/fonts/fonts.mkb/frameworks/base/data/fonts/fonts.mk
index875795a..490c6e6100755
---a/frameworks/base/data/fonts/fonts.mk
+++b/frameworks/base/data/fonts/fonts.mk
@@-38,6+38,8@@PRODUCT_PACKAGES:=\
DroidSansHebrew-Regular.ttf\
DroidSansHebrew-Bold.ttf\
DroidSansThai.ttf\
+NotoSansHans-Regular.otf\
+NotoSansHans-Bold.otf\
DroidSerif-Regular.ttf\
DroidSerif-Bold.ttf\
DroidSerif-Italic.ttf\
diff--gita/frameworks/base/data/fonts/vendor_fonts.xmlb/frameworks/base/data/fonts/vendor_fonts.xml
index8690ee1..fba0c88100755
---a/frameworks/base/data/fonts/vendor_fonts.xml
+++b/frameworks/base/data/fonts/vendor_fonts.xml
@@-45,17+45,26@@
<!--Samplefallbackfontadditionstothedefaultfallbacklist.Thesefontswillbeadded
tothetoptwopositionsofthefallbacklist,sincethefirsthasanorderof0.-->
-<!--
<familyset>
<familyorder="0">
<fileset>
-<file>MyFont.ttf</file>
+<file>NotoSansHans-Regular.otf</file>
+<file>NotoSansHans-Bold.otf</file>
</fileset>
</family>
+
+<!--Italic等等会造成启动不了的问题,所以只添加前两个足矣。
<family>
<fileset>
-<file>MyOtherFont.ttf</file>
+<file>NotoSansHans-Regular.otf</file>
+<file>NotoSansHans-Italic.otf</file>
+<file>NotoSansHans-Bolditalic.otf</file>
+<file>NotoSansHans-Black.otf</file>
+<file>NotoSansHans-DemiLight.otf</file>
+<file>NotoSansHans-Light.otf</file>
+<file>NotoSansHans-Medium.otf</file>
+<file>NotoSansHans-Thin.otf</file>
</fileset>
</family>
+-->
</familyset>
---->
需要强调一点,关于字体拷贝,经过测试发现需要同时修改Android.mk和fonts.mk才可以完成拷贝。Google是以ApacheLicense,version2.0协议发布的,所以大可不必担心会有律师函的问题,在高版本的Android中这套字体已经是内置到Android中了。DroidSansFallback.ttf并不是完全指中文字体,由于之前并没有开发出专心的中文字体,目前处理方法是把所以暂时不规范的字体都放入了DroidSansFallback.ttf中,Fallback是备胎的意思,所有的胎都爆了才会使用这个。
参考:《关于Android的字体》。低版本的Android参考《Android如何增加新的字库》<完>
更多相关文章
- android 9.0 10.0 修改默认字体大小
- Android TextView中文字通过SpannableString来设置超链接、颜色
- Android字体加粗
- 海创软件组--20200712--Butterknife与自定义图标字体