Chapter 10,
EPUB
File Processing
--
Style Processing
The content of this chapter is relatively simple, because Chapter 9 focuses on the process of display, so the process of processing styles is described in a separate chapter.
Processing a style includes two parts: creating a style and applying a style.
Create a style
The process of creating a style file is actually parsing the style file. In Version 1.0, the built-in resource file style. XML is read directly, and the file is located in assets \ default.
In chapter 2, we have introduced how to parse the resource file to obtain the text to be displayed on the progress bar. Style. xml files are also resource files, so they are similar in the parsing process.
We will first go back and forth to the second chapter to describe the sequence of calls between the three core classes zmlzmlprocessor, zlxmlparser, and zlxmlreader for parsing XML files:
1. The read method in the resourcetreereader class of the zlxmlreaderadapter abstract class calls the read method of the zlxmlprocessor class.
2. The read method of the zlxmlprocessor class obtains a byte stream class (assetinputstream class) for resource files through the getinputstream method of the androidassetsfile class (subclass of the zlresourcefile class ), this byte stream class is used as the parameter to initialize a volume stream class for resource files. Then, the doit method of the zlxmlparser class is called.
3. The zlxmlparser class's doit method converts a file into a char array using the extract stream class. When the for loop is used to iterate the char array, the doit method in turn calls the subclass of the zlxmlreaderadapter abstract class (resourcetreereader class) the startelementhandler and endelementhandler Methods operate on different nodes represented by elements in the byte array.
Replace the highlighted textstylereader class in the above call sequence with the resourcetreereader class, which can be applied to the parsing process of style files.
We have already repeatedly introduced the order of resolution in Chapter 2 and Chapter 6. This chapter will not be repeated. We only focus on the operations on different nodes in the char array iteration process with for loop. Node operations are completed in the startelementhandler method of the textstylereader class.
In the style. xml file, there are two types of nodes: Base and style. The base node represents the basic style, while the style node represents the label style corresponding to various labels.
. All words in the XHTML file will first apply the basic style represented by the base tag, and then the text content in different labels will apply the corresponding style based on the basic style, the label style overwrites the basic style.
The startelementhandler method of the textstylereader class is mainly used to operate on these two nodes.
Textstylereader
Class
Startelementhandler
Method
In this method, the Code determines whether the base and style labels are read.
If the base tag is read, the Code creates a new zltextbasestyle class to represent the basic style;
If the style label is read, the Code creates a zltextfullstyledecoration class that represents a specific label style.
All attributes in the label are assigned to the attributes of the zltextfullstyledecoration class. The important attributes include: left indent: leftindentoption, right indent: rightindentoption, first line indent, fontfamilyoption, fontsizedeltaoption, bold boldoption, and italic italicoption. Of course, the name of the final current tag is myname.
The newly created zltextfullstyledecoration class adds the zltextstylecollection class mydecorationmap attribute to the array with the ID attribute of the tag as the key name.
When the for loop of the char array is completed, the style creation process is completed accordingly. After this process is completed, we will get a "zltextbasestyle class representing the basic font style" and "a series of zltextfullstyledecoration classes representing different label styles ".
Application Style
The process of applying a style consists of the resettextstyle and applycontrol methods in the zltextviewbase class.
Zltextviewbase
Class
Resettextstyle
Method
The getbasestyle method is called in the buildinfos method of the zltextview class (see chapter 9 ).
The getbasestyle method gets the zltextbasestyle class generated by the parsing Style File. The settextstyle method then assigns the zltextbasestyle class representing the basic style to the mytextstyle attribute of the zltextviewbase class. At the same time, font-related styles are stored in the mycontext attribute of the zltextviewbase class.
Zltextviewbase
Class
Applycontrol
Method
The applycontrol method is triggered when the program reads the zltextcontrolelement class representing the tag information. Based on the isstart attribute of the current zltextcontrolelement class, this method first determines whether the current class corresponds to the start part or end part of the tag pair.
Start part of a tag pair
If the current zltextcontrolelement class corresponds to the starting part of the tag pair, the code will follow the kind attribute of the zltextcontrolelement class (for the value assignment of this attribute, see Chapter 7) obtain the corresponding zltextfullstyledecoration class from the array pointed to by the mydecorationmap attribute of the zltextstylecollection class (line 1, refer to the creation process just introduced ).
The Code then calls the createdecoratedstyle method of the zltextstyledecoration class (row 109). This method generates the corresponding zltextfulldecoratedstyle Class Based on the zltextfuldecoratedstyle class.
In the createdecoratedstyle method of the zltextstyledecoration class, the constructor of the lifecycle class is called. In this function, the zltextfullstyledecoration class is stored in the myfulldecoration attribute of the lifecycle class (row 33 ).
(Note,ZltextstyledecorationClassCreatedecoratedstyleThe method is also calledZltextstyleConstructor. This constructor willZltextfulldecoratedstyleClass (orZltextbasestyleClass,ZltextbasestyleClass andZltextfulldecoratedstyleAll classesZltextstyleClass) to the newZltextfulldecoratedstyleOfBaseAttribute .)
Finally, in the settextstyle method of the zltextviewbase class, the current zltextbasestyle class or zltextfulldecoratedstyle class is stored in the mytextstyle attribute of the zltextviewbase class, the font-related styles are stored in the mycontext attribute of the zltextviewbase class.
End Part of tag pair
If the current zltextcontrolelement class corresponds to the end part of the tag pair, the code will style back to the base attribute of the zltextfulldecoratedstyle class pointing to the old zltextfulldecoratedstyle class.
The applycontrol method forms a Hierarchical Storage Structure for the operations on the base attribute of the zltextfulldecoratedstyle class in the starting and ending parts of the label pair (this storage structure is described in Chapter 2 using zltreeresource the mychildren attributes of the class constitute a similar storage structure, for mutual reference ).
We can use the B tag as an example to see how this Hierarchical Storage Structure works. , We can see that the word "temporary name" is the content in the B label.
When the Code reads the content before Label B, it is always applying the basic style (zltextbasestyle class). Once it reads the starting part of Label B, A new style (zltextfulldecoratedstyle class) of the B label will be created, and the basic style will be overwritten. At the same time, the Code will set the basic style (zltextbasestyle class) store it in the base attribute of the B label style (zltextfulldecoratedstyle class. Before the Code reads the end part of Label B, the style of Label B is always applied. Once the end part of the B tag is read, the basic style (zltextbasestyle class) is retrieved from the base attribute of the B tag style (zltextfulldecoratedstyle class ). Then, we started to apply the basic style.
In this example, the H1 tag is used. Before the display process, the H1 tag is converted to the following format: "zltextcontrolelement class representing the start part of the P tag + text content in the p tag + zltextcontrolelement class representing the end part of the P tag + representing the end part of the P tag zltextcontrolelement class "(refer to the introduction to relevant procedures in chapter 7 ).
Before the Code reads the starting part with the p tag, it applies the basic style. Once you read the starting part of the P tag, the code will create a new P tag style to overwrite the basic style and store the basic style to the base attribute of the P tag style. Then, the program reads the starting part of the H1 tag, creates an H1 tag style to overwrite the P tag style, and stores the P tag style to the base attribute of the H1 tag style. When the Code reads the text content in the H1 tag, the style of the H1 tag is always applied. Once the Code reads the end part of the H1 tag, it extracts the P tag style from the base attribute of the H1 tag style. Then the Code reads the end part of the P tag, also, the basic style is extracted from the base attribute of the P label style. So far, the Code has begun to restore the basic application style.
Chapter 10 Epub File Processing-style processing