The parsing process for Android 5.0 system_fonts.xml files

Source: Internet
Author: User

The parsing process for Android 5.0 system_fonts.xml files First look at the format of Familyset version= "22" in 5.0
 <family name= "Sans-serif" > <font weight= "" "style=" normal ">Roboto-Thin.ttf</font> 2 2 <font weight= "style=" italic ">Roboto-ThinItalic.ttf</font> <font weight=" Styl " E= "Normal" >Roboto-Light.ttf</font> <font weight= "style=" italic ">roboto-lightitalic.ttf</" font> <font weight= "" "style=" normal ">Roboto-Regular.ttf</font> <font weight=" 40 0 "Italic" >Roboto-Italic.ttf</font> <font weight= "style=" style= "normal" >roboto-medium.ttf&l T;/font> <font weight= "$" style= "italic" >Roboto-MediumItalic.ttf</font> <font we ight= "style=" "normal" >Roboto-Black.ttf</font> <font weight= "" "style=" Italic ">roboto-black italic.ttf</font> <font weight= "" "style=" normal ">Roboto-Bold.ttf</font> <fon T weight= "style=" itAlic >Roboto-BoldItalic.ttf</font> </family> <!--Note that aliases must come after the fo NTS they reference.  --<alias name= "Sans-serif-thin" to= "Sans-serif" weight= "[]/>" "<alias name=" To= "Sans-serif" weight= "/>" <alias name= "Sans-serif-medium" to= "Sans-serif" weight= "$"/>     T;alias name= "Sans-serif-black" to= "Sans-serif" weight= ""/> <alias name= "Arial" to= "Sans-serif"/> 41  <alias name= "Helvetica" to= "Sans-serif"/> "<alias name=" Tahoma "to=" Sans-serif "/> Name= "Verdana" to= "Sans-serif"/>
In Typeface.java, the static class implements the parsing process of the System_fonts.xml file. System_fonts.xml file exists for device/system/etc/system_fonts.xmlIn two. The Typyface file parsing process for System_fonts.xml files requires two more important data structures:
    1. Font
public static class Font {        public String fontName;        //用于存储Fontfile的文件所在的Path        public int weight;             //新增加的字段,表示字体大小        public boolean isItalic;       //是否为斜体    }
    1. Family
    public static class Family {        public String name;          //对应于Family的名字, eg:sans-serif        public List<Font> fonts;     //属于该Family的Font集合        public String lang;          //语言属性        public String variant;          }
Then look at the initialization code for the System_fonts.xml file:
private static void init() {        // Load font config and initialize Minikin state        //以下根据配置文件的绝对路径(/system/etc/system_fonts.xml)得到File对象,就没什么好分析的了        File systemFontConfigLocation = getSystemFontConfigLocation();        File configFilename = new File(systemFontConfigLocation, FONTS_CONFIG);        //无关代码直接给省去了,直接分析解析代码(剩余的代码后头再进行分析)        try {            FileInputStream fontsIn = new FileInputStream(configFilename); //打开文件,没什么可以分析的            FontListParser.Config fontConfig = FontListParser.parse(fontsIn);//所有的核心都在这个parse中进行处理的            .....        } catch (RuntimeException e) {        ..............        }    }

To find out what parse really did, look directly at the source code

    /* Parse fallback list (no names) */    public static Config parse(InputStream in) throws XmlPullParserException, IOException {        try {            XmlPullParser parser = Xml.newPullParser(); //很明显这个是一个Xml文件解析工具类,不必要去深究了            parser.setInput(in, null);            parser.nextTag();            return readFamilies(parser); //重点分析这个函数        } finally {            in.close();        }    }

After the parse has created the XML parsing tool with the input stream, and the line work is forwarded to the Readfamilies function.

    private static Config readFamilies(XmlPullParser parser)            throws XmlPullParserException, IOException {        Config config = new Config(); //配置文件的所有解析结果都是存储于这个变量中        parser.require(XmlPullParser.START_TAG, null, "familyset");        while (parser.next() != XmlPullParser.END_TAG) {            if (parser.getEventType() != XmlPullParser.START_TAG) continue;            //以下两个if 条件可知,该解析只对 ‘family‘ 标签和 ‘alias‘ 标签里的内容进行解析            if (parser.getName().equals("family")) {                config.families.add(readFamily(parser));            } else if (parser.getName().equals("alias")) {                config.aliases.add(readAlias(parser));            } else {                skip(parser);            }        }        return config;    }

As can be known from the above code, readfamilies for the contents of the family and alias tags in the analysis, the rest of the content is ignored processing, and the results of processing are saved in the Config object

    1. The process of parsing the contents of the family tag, and the invocation of the readfamily function
    private static Family readfamily (Xmlpullparser parser) throws Xmlpullparserexception, IOException {        The family tag may have three properties: the name Lang Variant, so the following three lines complete the read of the corresponding property String name = Parser.getattributevalue (null, "name");        String lang = Parser.getattributevalue (null, "Lang");        String variant = Parser.getattributevalue (NULL, "variant"); /* 1.        With family as the parent tag, the font can be a child tag, and the font tag describes the attributes such as the path of the TTF file, the size of the text (weight), the * style, and so on. * 2.        Family can have multiple font tags, that is, the font under the same family belongs to the same font family */list<font> fonts = new arraylist<font> ();            The loop completes the read of the font property belonging to the same font family: weight, style, fullfilename (absolute path) while (Parser.next ()! = Xmlpullparser.end_tag) {            if (Parser.geteventtype ()! = Xmlpullparser.start_tag) Continue;            String tag = Parser.getname ();                if (tag.equals ("Font")) {String weightstr = Parser.getattributevalue (null, "weight");        If the weight attribute is not present, the default is 400        int weight = WEIGHTSTR = = null?                400:integer.parseint (WEIGHTSTR);                Boolean isitalic = "Italic". Equals (Parser.getattributevalue (null, "style"));                String filename = Parser.nexttext ();                String fullfilename = "/system/fonts/" + filename;            Fonts.add (New Font (fullfilename, Weight, isitalic));            } else {skip (parser);    }} return new Family (name, fonts, Lang, variant); }
    1. The contents of the alias tag are taken, Readalias. Alias is actually renaming family (alias can only rename a known family name)
eg :  名字为 sans-serif-thin的family必须在该句前出现<alias name="sans-serif-thin" to="sans-serif" weight="100" />
    private static Alias readAlias(XmlPullParser parser)            throws XmlPullParserException, IOException {        Alias alias = new Alias();        alias.name = parser.getAttributeValue(null, "name");        alias.toName = parser.getAttributeValue(null, "to");        String weightStr = parser.getAttributeValue(null, "weight");        if (weightStr == null) {            alias.weight = 400;        } else {            alias.weight = Integer.parseInt(weightStr);        }        skip(parser);  // alias tag is empty, ignore any contents and consume end tag        return alias;    }

In this way, the parsing of the XML file is completed.

The parsing process for Android 5.0 system_fonts.xml files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.