1. 程式人生 > >Android中加入思源字體/NotoSansCJK/SourceHanSans

Android中加入思源字體/NotoSansCJK/SourceHanSans

default languages nes 字體 google optional one puts ++

系統版本號:Android 4.2.2_r1

本文主要是在Android中加入思源字體的過程記錄。

思源字體是Google和Adobe在2014.07.18公布的中文字體。

1.獲取思源字體(Google:Noto Sans CJK; Adobe:Source Han Sans)

2.解壓後例如以下幾個才是思源字體。

  • NotoSansHans-Regular.otf 常規
  • NotoSansHans-Black.otf 黑體
  • NotoSansHans-DemiLight.otf
  • NotoSansHans-Medium.otf
  • NotoSansHans-Thin.otf
  • NotoSansHans-Bold.otf
  • NotoSansHans-Light.otf

註:這裏說下otf,意為opentypettf(truetype)是兼容的。

3.安裝體驗

因為Android中僅僅使用到常規和加粗體,這裏也僅僅以這兩個做實驗(同一時候也有容量的考慮)。在高PPI(分辨率)的屏幕上也許僅僅是感覺到字形變了,在低分辨率的屏幕上變的更加清晰了。前後對照:

之前:

技術分享

之後:

技術分享

(上傳到這裏就不清晰了,真是怪,點擊鏈接看原圖更為清晰)

  1) 將NotoSansHans-Regular.otf NotoSansHans-Bold.otf放到/system/fonts/

文件夾下。

  2) 將例如以下fallback_fonts.xml放到/vendor/etc/

<?xml version="1.0" encoding="utf-8"?>

<!--

Vendor-provided fallback fonts

This file can be edited to add references to fonts that are not installed or referenced in the

default system. The file should then be placed in /vendor/etc/fallback_fonts.xml. Note

that in your makefile, this directory should be referenced as $(TARGET_COPY_OUT_VENDOR)/etc/:

PRODUCT_COPY_FILES += \

frameworks/base/data/fonts/vendor_fonts.xml:$(TARGET_COPY_OUT_VENDOR)/etc/fallback_fonts.xml

For example, vendors might want to build configurations for locales that are

better served by fonts which either handle glyphs not supported in the default fonts or which

handle these glyphs differently than the default fallback fonts.

Each entry in this list is a "family", which consists of a list of "files"

(the filenames for that family). The files objects are

provided in the order of the styles supported for that family: regular, bold, italic, and

bold-italic. Only providing one font means that all styles will be rendered with that font.

Providing two means that these two fonts will render regular and bold fonts (italics will

be mapped to these two fonts).

There is also an optional "order" attribute on the Family tag. This specifies the index at

which that family of fonts should be inserted in the fallback font list, where the

default fallback fonts on the system (in /system/etc/fallback_fonts.xml) start at index 0.

If no ‘order‘ attribute is supplied, that family will be inserted either at the end of the

current fallback list (if no order was supplied for any previous family in this file) or

after the previous family (if there was an order specified previously). Typically, vendors

may want to supply an order for the first family that puts this set of fonts at the appropriate

place in the overall fallback fonts. The order of this list determines which fallback font

will be used to support any glyphs that are not handled by the default system fonts.

Han languages (Chinese, Japanese, and Korean) share a common range of unicode characters;

their ordering in the fallback or vendor files gives priority to the first in the list.

Language-specific ordering can be configured by adding a BCP 47-style "lang" attribute to

a "file" element; fonts matching the language of text being drawn will be prioritised over

all others.

The sample configuration below is an example of how one might provide two families of fonts

that get inserted at the first and second (0 and 1) position in the overall fallback fonts.

See /system/etc/system_fonts.xml and /system/etc/fallback_fonts.xml for more information

and to understand the order in which the default system fonts are loaded and structured for

lookup.

-->

<!-- Sample fallback font additions to the default fallback list. These fonts will be added

to the top two positions of the fallback list, since the first has an order of 0. -->

<familyset>

<family order="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時就能夠自己主動加入默認中文字體,思路和上邊的類似,下面是git log信息。

commit xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Author: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Date: Mon Aug 18 15:44:24 2014 +0800

Add Noto Sans CJK fonts for Chinese

diff --git a/device/rockchip/rk30sdk/device.mk b/device/rockchip/rk30sdk/device.mk

index 8befd40..b4e2822 100755

--- 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 --git a/frameworks/base/data/fonts/Android.mk b/frameworks/base/data/fonts/Android.mk

index e02e95a..0b4bc6c 100755

--- 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 --git a/frameworks/base/data/fonts/NotoSansHans-Bold.otf b/frameworks/base/data/fonts/NotoSansHans-Bold.otf

new file mode 100644

index 0000000..006372e

Binary files /dev/null and b/frameworks/base/data/fonts/NotoSansHans-Bold.otf differ

diff --git a/frameworks/base/data/fonts/NotoSansHans-Regular.otf b/frameworks/base/data/fonts/NotoSansHans-Regular.otf

new file mode 100644

index 0000000..923f89c

Binary files /dev/null and b/frameworks/base/data/fonts/NotoSansHans-Regular.otf differ

diff --git a/frameworks/base/data/fonts/fonts.mk b/frameworks/base/data/fonts/fonts.mk

index 875795a..490c6e6 100755

--- 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 --git a/frameworks/base/data/fonts/vendor_fonts.xml b/frameworks/base/data/fonts/vendor_fonts.xml

index 8690ee1..fba0c88 100755

--- a/frameworks/base/data/fonts/vendor_fonts.xml

+++ b/frameworks/base/data/fonts/vendor_fonts.xml

@@ -45,17 +45,26 @@

<!-- Sample fallback font additions to the default fallback list. These fonts will be added

to the top two positions of the fallback list, since the first has an order of 0. -->

-<!--

<familyset>

<family order="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.mkfonts.mk才幹夠完畢拷貝。Google是以Apache License, version 2.0協議公布的,所以大可不必操心會有律師函的問題,在高版本號的Android中這套字體已經是內置到Android中了。DroidSansFallback.ttf並非全然指中文字體,因為之前並沒有開發出專心的中文字體,眼下處理方法是把所以臨時不規範的字體都放入了DroidSansFallback.ttf中,Fallback是備胎的意思。全部的胎都爆了才會使用這個。

參考:《關於Android的字體》。低版本號的Android參考《Android 怎樣添加新的字庫<>

Android中加入思源字體/NotoSansCJK/SourceHanSans