需求:替換系統(tǒng)字體
分析:
過程:frameworks/base/data/fonts/fonts.xml
? ??????????external/roboto-fonts/
????? ??????noto-fonts/other/
關(guān)于字體上層主要在?
frameworks/base/graphics/java/android/graphics/Typeface.java
static {
final ArrayMap systemFontMap =new ArrayMap<>();
final ArrayMap 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),
};
}
static代碼塊 是系統(tǒng)typeface的初始化
DEFAULT 、DEFAULT_BOLD 苔可、SANS_SERIF嚣潜、SERIF旁钧、MONOSPACE
因?yàn)镈EFAULT_FAMILY ="sans-serif"? 所以DEFAULT =?SANS_SERIF? ??DEFAULT_BOLD =?SANS_SERIF + BOLD
所以先介紹下三種familyName的含義
sans-serif:區(qū)別于“serif”何陆,沒有"筆鋒",筆畫粗細(xì)也基本差不多
serif:帶“筆鋒”雾袱,而且根據(jù)橫豎斜的寬度也不一樣
monospace:打印用字體翩剪,屬于“sans-serif”,每個字母等間距
其次介紹下"font-weight"祟偷,
weight指的是粗細(xì)”
那么默認(rèn)的weight是多少察滑?
===>>>Typeface.java?
create(null,0);
nativeCreateFromTypeface(ni, style)
===>>>Typeface.cpp
frameworks/base/core/jni/android/graphics/Typeface.cpp
Typeface* face = Typeface::createRelative(family, (SkTypeface::Style)style);
===>>>Typeface.cpp
frameworks/base/libs/hwui/hwui/Typeface.cpp
Typeface* Typeface::createRelative(Typeface* src, SkTypeface::Style style) {
? ? Typeface* resolvedFace = Typeface::resolveDefault(src);
Typeface* hwTypeface = new Typeface();
? ? hwTypeface->fFontCollection = collection;
? ? hwTypeface->fSkiaStyle = SkTypeface::kNormal;
? ? hwTypeface->fBaseWeight = SkFontStyle::kNormal_Weight;
? ? hwTypeface->fStyle = minikin::FontStyle(4 /* weight */, false /* italic */);
? ? Typeface::setDefault(hwTypeface);
===>>>SkFontStyle.h
external/skia/include/core/SkFontStyle.h
enum Weight {
? ? ? ? kInvisible_Weight? =? ? 0,
? ? ? ? kThin_Weight? ? ? ? =? 100,
? ? ? ? kExtraLight_Weight? =? 200,
? ? ? ? kLight_Weight? ? ? =? 300,
? ? ? ? kNormal_Weight? ? ? =? 400,
? ? ? ? kMedium_Weight? ? ? =? 500,
? ? ? ? kSemiBold_Weight? ? =? 600,
? ? ? ? kBold_Weight? ? ? ? =? 700,
? ? ? ? kExtraBold_Weight? =? 800,
? ? ? ? kBlack_Weight? ? ? =? 900,
? ? ? ? kExtraBlack_Weight? = 1000,
? ? };
所以默認(rèn)的weight是400
再根據(jù)frameworks/base/data/fonts/fonts.mk? ? 找到Roboto-Regular.ttf? 才是默認(rèn)的自體文件
<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>
其中要注意ttc文件
```
1234
```
<family lang="zh-Hans">
? ? ? ? <font weight="400" style="normal" index="2">NotoSansCJK-Regular.ttc</font>
? ? </family>
? ? <!-- TODO: Add Bopo -->
? ? <family lang="zh-Hant">
? ? ? ? <font weight="400" style="normal" index="3">NotoSansCJK-Regular.ttc</font>
? ? </family>
? ? <family lang="ja">
? ? ? ? <font weight="400" style="normal" index="0">NotoSansCJK-Regular.ttc</font>
? ? </family>
? ? <family lang="ko">
? ? ? ? <font weight="400" style="normal" index="1">NotoSansCJK-Regular.ttc</font>
? ? </family>
```