一,需求描述
系统中添加某个指定的TTF文件作为系统默认的字库;
源码路径:
frameworks\base\graphics\java\android\graphics\Typeface.java
frameworks\base\data\fonts\fonts.mk
frameworks\base\data\fonts\Android.mk
frameworks\base\data\fonts\fonts.xml
二,实现步骤
- Typeface 这个类会在 Android 应用程序启动的过程中,通过反射的方式被加载;打开Typeface.java,可以看到里面有一个static代码块;
static {
final ArrayMap<String, Typeface> systemFontMap = new ArrayMap<>();
final ArrayMap<String, FontFamily[]> systemFallbackMap = new ArrayMap<>();
buildSystemFallback("/system/etc/fonts.xml", "/system/fonts/", systemFontMap,
systemFallbackMap);
sSystemFontMap = Collections.unmodifiableMap(systemFontMap);
sSystemFallbackMap = Collections.unmodifiableMap(systemFallbackMap);
setDefault(sSystemFontMap.get(DEFAULT_FAMILY));
// Set up defaults and typefaces exposed in public API
DEFAULT = create((String) null, 0);
DEFAULT_BOLD = create((String) null, Typeface.BOLD);
SANS_SERIF = create("sans-serif", 0);
SERIF = create("serif", 0);
MONOSPACE = create("monospace", 0);
sDefaults = new Typeface[] {
DEFAULT,
DEFAULT_BOLD,
create((String) null, Typeface.ITALIC),
create((String) null, Typeface.BOLD_ITALIC),
};
}
- fonts.xml文件
<!-- first font is default -->
<family name="sans-serif">
<font weight="100" style="normal">Roboto-Thin.ttf</font>
<font weight="100" style="italic">Roboto-ThinItalic.ttf</font>
<font weight="300" style="normal">Roboto-Light.ttf</font>
<font weight="300" style="italic">Roboto-LightItalic.ttf</font>
<font weight="400" style="normal">Roboto-Regular.ttf</font>
<font weight="400" style="italic">Roboto-Italic.ttf</font>
<font weight="500" style="normal">Roboto-Medium.ttf</font>
<font weight="500" style="italic">Roboto-MediumItalic.ttf</font>
<font weight="900" style="normal">Roboto-Black.ttf</font>
<font weight="900" style="italic">Roboto-BlackItalic.ttf</font>
<font weight="700" style="normal">Roboto-Bold.ttf</font>
<font weight="700" style="italic">Roboto-BoldItalic.ttf</font>
</family>
weight 表示字体粗细的程度;
style表示字体风格,一般来说是常规的和斜体的两种;
- 不考虑以上内容,可参考如下修改:
diff --git a/data/fonts/fonts.xml b/data/fonts/fonts.xml
--- a/data/fonts/fonts.xml
+++ b/data/fonts/fonts.xml
@@ -22,18 +22,7 @@
<familyset version="23">
<!-- first font is default -->
<family name="sans-serif">
- <font weight="100" style="normal">Roboto-Thin.ttf</font>
- <font weight="100" style="italic">Roboto-ThinItalic.ttf</font>
- <font weight="300" style="normal">Roboto-Light.ttf</font>
- <font weight="300" style="italic">Roboto-LightItalic.ttf</font>
- <font weight="400" style="normal">Roboto-Regular.ttf</font>
- <font weight="400" style="italic">Roboto-Italic.ttf</font>
- <font weight="500" style="normal">Roboto-Medium.ttf</font>
- <font weight="500" style="italic">Roboto-MediumItalic.ttf</font>
- <font weight="900" style="normal">Roboto-Black.ttf</font>
- <font weight="900" style="italic">Roboto-BlackItalic.ttf</font>
- <font weight="700" style="normal">Roboto-Bold.ttf</font>
- <font weight="700" style="italic">Roboto-BoldItalic.ttf</font>
+ <font weight="400" style="normal">Fonts.ttf</font>
</family>
<!-- Note that aliases must come after the fonts they reference. -->
- 把fonts放置到system/fonts/
diff --git a/data/fonts/Android.mk b/data/fonts/Android.mk
old mode 100644
new mode 100755
index 76eb4e67..6be1723d
--- a/data/fonts/Android.mk
+++ b/data/fonts/Android.mk
@@ -74,7 +74,8 @@ $(eval include $(BUILD_PREBUILT))
endef
font_src_files := \
- AndroidClock.ttf
+ AndroidClock.ttf \
+ Fonts.ttf
$(foreach f, $(font_src_files), $(call build-one-font-module, $(f)))
diff --git a/data/fonts/fonts.mk b/data/fonts/fonts.mk
old mode 100644
new mode 100755
index e884f2fe..c48f848b
--- a/data/fonts/fonts.mk
+++ b/data/fonts/fonts.mk
@@ -17,4 +17,5 @@
PRODUCT_PACKAGES := \
DroidSansMono.ttf \
AndroidClock.ttf \
+ Fonts.ttf \
fonts.xml
三,调试方法
- 复制fonts.ttf 到system/fonts/目录下,并修改fonts.xml;
adb root
adb remount
adb push 1.ttf system/fonts/Fonts.ttf
adb shell
chmod 0644 system/fonts/Fonts.ttf
busybox vi system/etc/fonts.xml
- 权限不够可能会导致重启之后无法启动系统;
无法启动的日志如下:
12-31 18:00:35.799 E/Typeface( 2108): Error mapping font file /system/fonts/Fonts.ttf
12-31 18:00:35.800 E/Typeface( 2108): Unable to load Family: sans-serif : null
12-31 18:00:35.925 E/Typeface( 2108): Unable to load Family: sans-serif : nul