|
需求:替换系统字体 分析: 过程:frameworks/base/data/fonts/fonts.xml external/roboto-fonts/ noto-fonts/other/
关于字体上层主要在 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代码块 是系统typeface的初始化 DEFAULT 、DEFAULT_BOLD 、SANS_SERIF、SERIF、MONOSPACE 因为DEFAULT_FAMILY ="sans-serif" 所以DEFAULT = SANS_SERIF DEFAULT_BOLD = SANS_SERIF + BOLD 所以先介绍下三种familyName的含义 sans-serif:区别于“serif”,没有"笔锋",笔画粗细也基本差不多 serif:带“笔锋”,而且根据横竖斜的宽度也不一样 monospace:打印用字体,属于“sans-serif”,每个字母等间距
其次介绍下"font-weight", weight指的是粗细” 那么默认的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, };
所以默认的weight是400 再根据frameworks/base/data/fonts/fonts.mk 找到Roboto-Regular.ttf 才是默认的自体文件 <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文件 !!如果要用ttf替换ttc的话 index属性就要去掉!!! <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>
其他字体说明 smallcaps 是大写的样式但是小写字母的大小 cursive 是草书 courier 一种常见的打印字体
|