RK:android5.1 修改系统默认字体

https://my.oschina.net/wolfcs/blog/366228?fromerr=RPZf2zmJ  android 5.0中字库文件管理配置的变化

https://blog.csdn.net/a282255307/article/details/76870441  浅析Android字体加载原理   Leslie_Yu 

https://www.jianshu.com/p/c5087d5a2268?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation   Android8.1系统字体

一.Android api  Typeface 字型

android.graphics.Typeface提供了3个API供应用程序创建特定字体:

1. Typeface createFromAsset(AssetManager mgr, String path);

2. Typeface createFromFile(File path);

3. Typeface createFromFile(String path);

以及如下3个API来创建系统字体:

1. Typeface create(String familyName, int style);

2. Typeface create(Typeface family, int style);

3. Typeface defaultFromStyle(int style);

1.ZK_RXXX_RK3288_ANDROID5.1\frameworks\base\graphics\java\android\graphics\Typeface.java

    static Typeface[] sDefaults;
    private static final LongSparseArray<SparseArray<Typeface>> sTypefaceCache =
            new LongSparseArray<SparseArray<Typeface>>(3);

    static Typeface sDefaultTypeface;
    static Map<String, Typeface> sSystemFontMap;
    static FontFamily[] sFallbackFonts;

    static final String FONTS_CONFIG = "fonts.xml";

static代码块 初始化系统字体

    static {
        init();
        // 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),
        };

    }

 init

/*
 * (non-Javadoc)
 *
 * This should only be called once, from the static class initializer block.
 */
private static void init() {
    // Load font config and initialize Minikin state
    //获取系统字体配置文件位置放置于system/etc目录下
    File systemFontConfigLocation = getSystemFontConfigLocation();
    //获取配置文件fonts.xml
    File configFilename = new File(systemFontConfigLocation, FONTS_CONFIG);
    //以下代码是对fonts.xml的解析,即是对系统字体的解析
    try {
        FileInputStream fontsIn = new FileInputStream(configFilename);
        FontListParser.Config fontConfig = FontListParser.parse(fontsIn);

        Map<String, ByteBuffer> bufferForPath = new HashMap<String, ByteBuffer>();
        //用来承载fonts.xml中的每个family节点
        List<FontFamily> familyList = new ArrayList<FontFamily>();
        // Note that the default typeface is always present in the fallback list;
        // this is an enhancement from pre-Minikin behavior.
        //从每个family节点中解析字体样式,这里解析系统默认字体
        for (int i = 0; i < fontConfig.families.size(); i++) {
            FontListParser.Family f = fontConfig.families.get(i);
            if (i == 0 || f.name == null) {
                familyList.add(makeFamilyFromParsed(f, bufferForPath));
            }
        }
        //系统默认字体集合
        sFallbackFonts = familyList.toArray(new FontFamily[familyList.size()]);
        //设置默认系统字体
        setDefault(Typeface.createFromFamilies(sFallbackFonts));
        //这里加载系统字体,包括默认字体
        Map<String, Typeface> systemFonts = new HashMap<String, Typeface>();
        for (int i = 0; i < fontConfig.families.size(); i++) {
            Typeface typeface;
            FontListParser.Family f = fontConfig.families.get(i);
            if (f.name != null) {
                if (i == 0) {
                    // The first entry is the default typeface; no sense in
                    // duplicating the corresponding FontFamily.
                    typeface = sDefaultTypeface;
                } else {
                    //从每个family节点中解析字体
                    FontFamily fontFamily = makeFamilyFromParsed(f, bufferForPath);
                    FontFamily[] families = { fontFamily };
                    typeface = Typeface.createFromFamiliesWithDefault(families);
                }
                //解析的字体添加到系统字体中
                systemFonts.put(f.name, typeface);
            }
        }
        //通过权重别号解析字体,别名必须与字体对应
        for (FontListParser.Alias alias : fontConfig.aliases) {
            Typeface base = systemFonts.get(alias.toName);
            Typeface newFace = base;
            int weight = alias.weight;
            if (weight != 400) {
                newFace = new Typeface(nativeCreateWeightAlias(base.native_instance, weight));
            }
            systemFonts.put(alias.name, newFace);
        }
        //系统字体集合
        sSystemFontMap = systemFonts;

    } catch (RuntimeException e) {
        Log.w(TAG, "Didn't create default family (most likely, non-Minikin build)", e);
        // TODO: normal in non-Minikin case, remove or make error when Minikin-only
    } catch (FileNotFoundException e) {
        Log.e(TAG, "Error opening " + configFilename, e);
    } catch (IOException e) {
        Log.e(TAG, "Error reading " + configFilename, e);
    } catch (XmlPullParserException e) {
        Log.e(TAG, "XML parse exception for " + configFilename, e);
    }
}

 二.fonts.xm<?xml version="1.0" encoding="utf-8"?><!--    NOTE: this is the newer (L) version of the system font configuration,

    supporting richer weight selection. Some apps will expect the older
    version, so please keep system_fonts.xml and fallback_fonts.xml in sync
    with any changes, even though framework will only read this file.

    All fonts withohut names are added to the default list. Fonts are chosen
    based on a match: full BCP-47 language tag including script, then just
    language, and finally order (the first font containing the glyph).

    Order of appearance is also the tiebreaker for weight matching. This is
    the reason why the 900 weights of Roboto precede the 700 weights - we
    prefer the former when an 800 weight is requested. Since bold spans
    effectively add 300 to the weight, this ensures that 900 is the bold
    paired with the 500 weight, ensuring adequate contrast.
-->
<familyset version="22">
    <!-- 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>

    <!-- Note that aliases must come after the fonts they reference. -->
    <alias name="sans-serif-thin" to="sans-serif" weight="100" />
    <alias name="sans-serif-light" to="sans-serif" weight="300" />
    <alias name="sans-serif-medium" to="sans-serif" weight="500" />
    <alias name="sans-serif-black" to="sans-serif" weight="900" />
    <alias name="arial" to="sans-serif" />
    <alias name="helvetica" to="sans-serif" />
    <alias name="tahoma" to="sans-serif" />
    <alias name="verdana" to="sans-serif" />

    <family name="sans-serif-condensed">
        <font weight="300" style="normal">RobotoCondensed-Light.ttf</font>
        <font weight="300" style="italic">RobotoCondensed-LightItalic.ttf</font>
        <font weight="400" style="normal">RobotoCondensed-Regular.ttf</font>
        <font weight="400" style="italic">RobotoCondensed-Italic.ttf</font>
        <font weight="700" style="normal">RobotoCondensed-Bold.ttf</font>
        <font weight="700" style="italic">RobotoCondensed-BoldItalic.ttf</font>
    </family>
    <alias name="sans-serif-condensed-light" to="sans-serif-condensed" weight="300" />

    <family name="serif">
        <font weight="400" style="normal">NotoSerif-Regular.ttf</font>
        <font weight="700" style="normal">NotoSerif-Bold.ttf</font>
        <font weight="400" style="italic">NotoSerif-Italic.ttf</font>
        <font weight="700" style="italic">NotoSerif-BoldItalic.ttf</font>
    </family>
    <alias name="times" to="serif" />
    <alias name="times new roman" to="serif" />
    <alias name="palatino" to="serif" />
    <alias name="georgia" to="serif" />
    <alias name="baskerville" to="serif" />
    <alias name="goudy" to="serif" />
    <alias name="fantasy" to="serif" />
    <alias name="ITC Stone Serif" to="serif" />

    <family name="monospace">
        <font weight="400" style="normal">DroidSansMono.ttf</font>
    </family>
    <alias name="sans-serif-monospace" to="monospace" />
    <alias name="monaco" to="monospace" />

    <family name="serif-monospace">
        <font weight="400" style="normal">CutiveMono.ttf</font>
    </family>
    <alias name="courier" to="serif-monospace" />
    <alias name="courier new" to="serif-monospace" />

    <family name="casual">
        <font weight="400" style="normal">ComingSoon.ttf</font>
    </family>

    <family name="cursive">
        <font weight="400" style="normal">DancingScript-Regular.ttf</font>
        <font weight="700" style="normal">DancingScript-Bold.ttf</font>
    </family>

    <family name="sans-serif-smallcaps">
        <font weight="400" style="normal">CarroisGothicSC-Regular.ttf</font>
    </family>
……………………………………………………………………………………………………………………………………………
<family> <font weight="400" style="normal">NanumGothic.ttf</font> </family> <family> <font weight="400" style="normal">NotoSansSymbols-Regular-Subsetted.ttf</font> </family> <family> <font weight="400" style="normal">NotoColorEmoji.ttf</font> </family> <family> <font weight="400" style="normal">DroidSansFallback.ttf</font> </family> <family lang="ja"> <font weight="400" style="normal">MTLmr3m.ttf</font> </family> <!-- Noto Sans Tai Le is intentionally kept last, to make sure it doesn't override the East Asian punctuation for Chinese. --> <family> <font weight="400" style="normal">NotoSansTaiLe-Regular.ttf</font> </family> </familyset>

  开头注释:暂时为保持兼容性 会保留老的字库配置文件  system_fonts.xml和fallback_fonts.xml 。并且要保持system_fonts.xml和fallback_fonts.xml同步。

  对于任何更改,即使框架将只读取此文件。所有有名称的字体都添加到默认列表中。

三.Android5.1 修改系统默认字体,系统默认字体样式

diff --git a/frameworks/base/data/fonts/fonts.xml b/frameworks/base/data/fonts/fonts.xml
old mode 100644
new mode 100755
index 37527e9..892a982
--- a/frameworks/base/data/fonts/fonts.xml
+++ b/frameworks/base/data/fonts/fonts.xml
@@ -16,13 +16,25 @@
     paired with the 500 weight, ensuring adequate contrast.
 -->
 <familyset version="22">
+<family lang="zh-Hans">
+        <font weight="400" style="normal">hifont.ttf</font>
+    </family>
+    <family lang="zh-Hant">
+        <font weight="400" style="normal">hifont.ttf</font>
+    </family>
+    <family lang="ja">
+        <font weight="400" style="normal">hifont.ttf</font>
+    </family>
+    <family lang="ko">
+        <font weight="400" style="normal">hifont.ttf</font>
+    </family>
     <!-- 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="normal">Roboto-Black.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>

  

 

posted @ 2020-10-21 16:27  CrushGirl  阅读(1009)  评论(0编辑  收藏  举报