Platform: Hi3751V350
OS: Android 9.0
Kernel: v4.9.127_s5

需求:

客户要求系统添加某个指定的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 APIDEFAULT         = 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),};}

关键代码:

setDefault(sSystemFontMap.get(DEFAULT_FAMILY));private static final String DEFAULT_FAMILY = "sans-serif";

继续看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表示字体风格,一般来说是常规的和斜体的两种;

在不考虑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))endeffont_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;

cp /mnt/media_rw/sda1/1.ttf 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

相关的代码如下:

    private static @Nullable ByteBuffer mmap(String fullPath) {try (FileInputStream file = new FileInputStream(fullPath)) {final FileChannel fileChannel = file.getChannel();final long fontSize = fileChannel.size();return fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fontSize);} catch (IOException e) {Log.e(TAG, "Error mapping font file " + fullPath);return null;}}

看下是否是权限有差异:

-rw-r--r-- 1 root root   108128 2008-12-31 10:00 DroidSansMono.ttf
-rwx------ 1 root root  2678868 1969-12-31 18:08 Fonts.ttf

-rw-r--r-- 代表0644,手动给予权限再重启即可;

chmod 0644 Fonts.ttf

其他:

Linux的文件基本上分为三个属性:可读(r=4),可写(w=2),可执行(x=1);

[Hi3751V350][Android9.0] 调试笔记 --- 添加并设置默认系统字库相关推荐

  1. [RK3288][Android6.0] 调试笔记 --- 电池电量一直显示100%

    Platform: Rockchip OS: Android 6.0 Kernel: 3.10.92 之前文章[RK3288][Android6.0] 调试笔记 - 伪电池驱动添加 阐述了如何添加一个 ...

  2. [RK3288][Android6.0] 调试笔记 --- touch无法获取坐标点

    Platform: Rockchip OS: Android 6.0 Kernel: 3.10.92 有网友遇到调试touch的时候能触发中断,但无法获取坐标点 具体可参考文章 [RK3288][An ...

  3. [RK3288][Android6.0] 调试笔记 --- AndroidTool低格无效问题

    Platform: Rockchip OS: Android 6.0 Kernel: 3.10.92 现象: 之前文章 [RK3288][Android6.0] 调试笔记 - AndroidTool两 ...

  4. [RK3288][Android6.0] 调试笔记 --- 设置中文为默认输入法

    Platform: RK3288 OS: Android 6.0 Kernel: 3.10.92 添加方法: 1. 添加第三方apk 路径: rk3288/device/rockchip/rk3288 ...

  5. gpio驱动广播Android,[RK3288][Android6.0] 调试笔记 --- 通用GPIO驱动控制LED【转】

    Platform: ROCKCHIP OS: Android 6.0 Kernel: 3.10.92 由于板子没有lcd无法得知sd卡升级是否完成,因此使用LED显示. Recovery中升级完成后控 ...

  6. rk3368 Android9.0调试记录之系统分区调整

    系统分区调整记录 Platform: RK3368 OS: Android 9.0 Kernel: 4.4.194 文章目录 系统分区调整记录 1. 升级固件后开机进recovery出错 1.1. 调 ...

  7. [RK3288][Android6.0] 调试笔记 --- adb无法安装apk提示签名错误

    Platform: RK3288 OS: Android 6.0 Kernel: 3.10.92 现象: 通过adb去安装apk,发现一直安装不上,提示签名相关错误(log被我弄丢了?). 分析: 此 ...

  8. Ubuntu添加和设置默认中文字体

    参考:https://blog.csdn.net/gengyuchao/article/details/101215243 首先,通过命令 $ fc-list :lang=zh 可以查看已安装的中文字 ...

  9. Jodd 5.0 使用自定义WebApp及设置默认拦截器

    首先使用IDEA创建Java工程,目录如下图所示: 使用 Jetty作为Web服务器,配置 jetty.xml: <?xml version="1.0" encoding=& ...

  10. [RK3288][Android6.0] 调试笔记 --- 相机曝光模式以及等级的获取和设置

    Platform: RK3288 OS: Android 6.0 Kernel: 3.10.92 曝光模式种类: V4L2_EXPOSURE_AUTO Automatic exposure time, ...

最新文章

  1. 一款不错的网页对话插件
  2. redis源码剖析(七)—— Redis 数据结构dict.c
  3. 拉普拉斯变换_拉普拉斯变化(s变换)定义与性质
  4. 【Hadoop】MR 切片机制 MR全流程
  5. 处理MySql连接超时引起的错误
  6. C语言贪吃蛇 新手入门(超详细)
  7. JAVA毕业设计后勤管理系统计算机源码+lw文档+系统+调试部署+数据库
  8. NetLog 大规模应用实战:Database-sharding 技术
  9. 如何清空各种浏览器缓存
  10. 专访李亚锋:“大数据+”趋势下的电信实践之路
  11. Android_openCV图片处理
  12. 动态规划石子排序java_动态规划之石子归并
  13. 计算机睡眠重启后无法识别网络,教您一招解决电脑休眠唤醒后无法使用USB键盘的操作方法...
  14. GDR(Gradual Decoder Refresh)帧
  15. 一进二出宿舍限电模块的基本功能
  16. 征文投稿丨只需6步!轻量应用服务器快速建站指南
  17. 2022年第十四届华中杯数学建模A题解题思路附代码
  18. sql语句查询出重复的数据
  19. Win32汇编:过程与宏调用
  20. ios系统微信H5页面背景音乐自动播放

热门文章

  1. POJ 2856 Y2K Accounting Bug【简单暴力】
  2. 某乎x-zse-96、x-zst-81最新通杀方案
  3. Increasing Triplet Subsequence
  4. Lake Shore低温温度传感器之Cernox
  5. 【JZOJ A组】Melancholy
  6. AndroidStudio 设置全局查找快捷键
  7. SMILES, a Chemical Language and Information System.【SMILES, 一种化学语言和信息系统。】
  8. Greedy method and ε-greedy method
  9. 【团队管理】如何做好技术团队年终复盘
  10. 周末阅读:北漂程序员边城的幸福生活