A kind of SVG file fast resolving method and system
Technical field
The present invention relates to a kind of vector graphics analytic technique, relate in particular to a kind of SVG document analysis technology.
Background technology
The figure that shows in the computing machine generally can be divided into two big types---polar plot and bitmap.
Polar plot uses straight line and curve to describe figure, and the element of these figures is some points, line, rectangle, polygon, circle and camber line or the like, and they all calculate through mathematical formulae and obtain.For example the vector graphics of width of cloth flower is actually by line segment and forms the housing profile, spends the color that demonstrates by the color of housing and the color decision that housing sealed.Because vector graphics can calculate through formula and obtain, so the vector graphics file volume is generally less.The vector graphics biggest advantage is no matter to amplify, dwindle or rotation etc. can distortion.
Bitmap is also referred to as dot matrix image or drawing image, is made up of a single point that is called pixel (picture element).
Scalable vector graphics (Scalable Vector Graphics is called for short " SVG ") is based on extend markup language (XML), is used to describe a kind of graphical format of two-dimension vector graphics.SVG is formulated by W3C, is an open standard.The XML grammer is deferred in the SVG strictness, and describes picture material with the descriptive language of text formatting, therefore is a kind of vector graphics format that has nothing to do with image resolution ratio.
SVG comprises 3 types object: vector graphics (graph edge that comprises straight line, curve), dot matrix image and text.Various image objects can make up, conversion, and revise its pattern, also can be defined as the pre-service object.
Different with the traditional image form is, SVG adopts text to describe the figure of vector quantization, and this makes the SVG image file good readability to be arranged as html web page.After the user is with tool image output SVG, can uses any word processing TO SVG image, and can see the text codes that is used for describing image.The content of having grasped the people of SVG grammer even can only just can having read in the image with a notepad is come.
The complete supporting documentation object of SVG model (Document Object Model is called for short " DOM ").DOM is a kind of document platform, content, structure or the pattern of dynamic storage of its permission program or script and upload file.Because SVG supports DOM fully, thereby the SVG document can be come into contacts with through accordant interface standard and extraneous program.Object element among SVG and the SVG can be accepted the driving of external event fully through script, and mouse action is for example realized self or to control of other objects, image etc.This also is one of electronic document excellent characteristic that should possess.
Existing method of resolving the SVG file generally is divided into 2 kinds:
A kind of be by the analyzing XML document parser with the SVG file to resolve to XML DOM earlier, convert SVG DOM to from XML DOM again.This mode must just can achieve the goal through 2 parsings: for the first time the SVG document analysis is become XML DOM; For the second time convert XML DOM into SVG DOM.Clearly this process will spend at least greater than the time of 1 times of single resolution file, because except resolution file, convert XML DOM into SVG DOM and also need spend the regular hour.Obvious this mode efficiency ratio is lower, and it is less or to the less demanding occasion of efficient that this technology can only be used in the SVG file.
Another kind of mode is that the SVG file is directly resolved, and directly generates SVG DOM, and this mode is compared quicker with the mode of first kind of 2 parsing.General existing analysis mode all be node label with SVG as key word, in the process of resolving, create the SVG node object through the mode of comparison keyword.
Specifically, the involved Drawing Object of SVG has: rectangle, straight line, circle, ellipse, broken line, polygon, path, text, dot matrix image or the like.Wherein every kind of Drawing Object has the attribute of oneself again, like rectangle x (left summit x coordinate), y (left summit y coordinate), width (width), height (highly) etc. is arranged.Each Drawing Object can be used as independent node, also can make up between the Drawing Object, forms new node.The node label of SVG is a key word of describing these Drawing Objects and figure relevant information; Label like the rectangle node is " rect "; The label of straight line node is " line "; The label of oval node is " ellipse ", and the label of horizontal gradual change look node is " linearGradient " or the like.
Be the example of a SVG file layout as follows, description to be a start position be (138,91), white rectangle and the black ellipse that the center be (387,147.5), major axis 88, minor axis 47.5 of wide by 98, high 88:
? Xml version=" 1.0 " encoding=" utf-8 " standalone=" no "? A >/* xml normative document */
< svg width=" 640 " height=" 480 " version=" 1.1 " >/* svg root node position */
< rect x=" 138 " y=" 91 " width=" 98 " height=" 88 " fill=" #FFFFFF " stroke=" #000000 " />/* rectangle, start position (138,91), wide by 98, high by 88, fill white, line color be black */
< ellipse cx=" 387 " cy=" 147.5 " rx=" 88 " ry=" 47.5 " fill=" #000000 " stroke=" #000000 " />/* is oval, center (387,147.5), major axis 88, minor axis 47.5 is filled black, line color be black */
</svg>
Existingly become the process of SVG DOM structure following the SVG document analysis: read the SVG file, as an object, the attribute in the label is as the attribute of object with each node label in the SVG file, sets up object that should attribute.As be resolved to " rect " character string, and then just needing to create a rectangular object, the attribute of this rectangular object i.e. the attribute of this node label.Equally, the attribute of node also has corresponding attribute tags in the SVG file, representes the transverse axis position like x, and y representes longitudinal axis position, and width representes width, and fill representes Fill Color, and srtoke representes line color or the like.
Traditional analysis mode is after being resolved to a node label, just needs to compare with the node label that has defined, when node label relatively follows the node label of definition to meet, just creates corresponding objects.As: definition " rect ", " ellipse ", labels such as " line ".During node label in reading file, with this node label relatively, when finding that relatively this node label is " rect ", create corresponding rectangular object with " rect ", " ellipse " that have defined, " line ".Must spend the more time so relatively goes up in character string.
Suppose that the character string that is resolved to is tagname, this node label and defined node label compared through computerese:
If(tagname==“rect”)
{ // create rectangular object
}
……
else?if(tagname==“line”)
{ // create straight line object
}
Above-mentioned implementation will realize that under the bigger situation of SVG file, only the compare string string just needs the more time of cost through the compare string string.Because the application generic-document of SVG industrial circle is more complicated all; Archive sit is various; File content is abundant, so file itself is all bigger, and SVG node number is probably above 10,000; In resolving, need the cost plenty of time carry out the comparison of SVG node label, make the greatest problem that file size and number of nodes become influences SVG resolver speed.In the SVG application scenario, the general user hopes that the operation of oneself can access quick response, and therefore urgent need is wanted the method for the SVG file of the big file multinode of a kind of fast resolving.
Summary of the invention
The technical matters that the present invention mainly solves provides a kind of SVG file fast resolving method and system thereof, makes that the resolution speed of SVG file is speeded, and the SVG document analysis speed of especially big file multinode obviously speeds.
In order to solve the problems of the technologies described above, the invention provides a kind of SVG file fast resolving method, comprise following steps:
Various types of other node label in the SVG file is corresponding with a simple identification respectively, set up the mapping relations table of node label and simple identification;
Resolve in the process of said SVG file, when reading node label, from this mapping relations table, find the corresponding simple identification of this node label through mapping algorithm;
Judge the pairing object type of this node label according to this simple identification, and create the object of corresponding classification.
As the improvement of technique scheme, this method can also comprise following steps:
Preserve the pairing object type of each simple identification in advance;
Saidly judge the pairing object type of this node label, and it is following to create the method for object of corresponding classification according to this simple identification:
The said simple identification that finds and each existing simple identification are compared,, then set up the object of this simple identification corresponding class if consistent.
As the improvement of technique scheme, said node label is generally the not character string of regular length.
As the improvement of technique scheme, said simple identification can be the character string of numeral, character or regular length.
As the improvement of technique scheme, said simple identification can be an integer.
As the improvement of technique scheme, said mapping relations table is the Hash mapping table; Said mapping algorithm is " hash-map " algorithm.
As the improvement of technique scheme, this method can also comprise following steps:
The attribute tags of the various node labels in the SVG file is corresponding with a simple identification respectively, set up the mapping relations table of attribute tags and simple identification, and preserve the corresponding attribute classification of each simple identification;
Resolve in the process of said SVG file, when reading attribute tags, from the mapping relations table of this attribute tags and simple identification, find the corresponding simple identification of this attribute tags through mapping algorithm;
This simple identification and each simple identification of being preserved are compared, confirm the pairing attribute classification of this simple identification, and the attribute of corresponding classification is set for object.
The present invention also provides a kind of SVG file fast resolving system, comprises:
Memory module is used for storing the mapping relations table of various types of other node label of SVG file and corresponding simple identification;
Mapping block is used in the process of resolving said SVG file, when reading node label, from this mapping relations table, finds the corresponding simple identification of this node label through mapping algorithm;
The Object Creation module, the simple identification that is used for finding according to this mapping block is judged the pairing object type of this node label, and creates the object of corresponding classification.
As the improvement of technique scheme, said memory module can also be used to store the pairing object type of each simple identification;
Said Object Creation module compares the said simple identification that finds and each existing simple identification, if consistent, then sets up the object of this simple identification corresponding class.
As the improvement of technique scheme, said node label is generally the not character string of regular length; Said simple identification can be numeral, character or regular length character string.
As the improvement of technique scheme, said simple identification can be an integer.
As the improvement of technique scheme, said mapping relations table is the Hash mapping table; Said mapping algorithm is " hash-map " algorithm.
As the improvement of technique scheme, the mapping relations table of the attribute tags that said memory module can also be used for storing the various node labels of SVG file and corresponding simple identification;
Said mapping block also is used for resolving the process of said SVG file, when reading attribute tags, from the mapping relations table of this attribute tags and simple identification, finds the corresponding simple identification of this attribute tags through mapping algorithm;
The simple identification that said Object Creation module also is used for finding according to this mapping block is judged the pairing attribute classification of this attribute tags, and for object the attribute of corresponding classification is set.
Embodiment of the present invention compared with prior art, the key distinction and effect thereof are: various types of other node label in the SVG file is corresponding with a simple identification respectively, set up the mapping relations table of node label and simple identification; Resolve in the process of SVG file, when reading node label, from this mapping relations table, find the corresponding simple identification of this node label fast through mapping algorithm; According to the contrast of this simple identification and judge the pairing object type of this node label, create the object of corresponding classification.
Because the node label in the SVG file generally is made up of the unfixed character string of length; In the resolving of file, need carry out the contrast and the identification of great deal of nodes label; The versus speed of character string is relatively slow, makes that the speed of this node label of identification is slack-off, and embodiment of the present invention at first is mapped as simple identification (like numeral, single character or 2 character strings) through mapping techniques with character string; When resolving the SVG node label; Directly simple identification is compared, thereby need not to carry out one by one the more not comparison of the character string of regular length, the comparison speed of the digital or single character relatively comparison speed of the character string of regular length wants fast a lot; Therefore in the SVG file, contain under the situation of great deal of nodes label, adopt embodiment of the present invention can obviously improve the resolution speed of SVG file.
Description of drawings
Below in conjunction with accompanying drawing and embodiment the present invention is done further explain.
Fig. 1 is the corresponding vector graphics synoptic diagram of SVG file example in the prior art;
Fig. 2 is the SVG file fast resolving method flow diagram of first embodiment of the invention.
Embodiment
For making the object of the invention, technical scheme and advantage clearer, will combine accompanying drawing that embodiment of the present invention is done to describe in detail further below.
First embodiment of the invention relates to a kind of SVG file fast resolving method.
In this embodiment, need be in advance that node labels all in the SVG file is corresponding with an integer respectively, with mapping relations table of corresponding relation formation of node label and integer, like Hash table, mapping relations are that node label is mapped to integer.And, preserve the pairing object type of different integers in advance, the object of the required establishment of different integers perhaps is set in advance.
Concrete process of analysis is as shown in Figure 2.
Step 201: read and resolve the SVG file, when being resolved to node label, get into step 202.
Step 202:,, from the mapping relations table of node label and integer, find the corresponding integer of this node label fast like the hash-map algorithm through mapping algorithm.Shared time of this mapping algorithm is few, can not influence SVG document analysis bulk velocity.
Step 203: the integer that mapping obtains is compared with existing each integer one by one,, then set up the DOM object of the corresponding classification of this integer if identical.
For example, the corresponding integer 1 of rectangular label " rect " is set, the corresponding integer 2 of straight line label " line ", the corresponding integer 3 of oval label " ellipse ", horizontal gradual change colour code is signed " linearGradient " corresponding integer 4.......Simultaneously, preserve simple identification 1 corresponding rectangular object; 2 line correspondence objects; 3 corresponding oval objects; 4 corresponding gradual change look objects.
Suppose that the character string that is resolved to is tagname, at first through mapping algorithm find the corresponding numeral of character string: int>:: iterator iter=tagmap.find (tagname);
The result who then mapping is obtained compares with existing digit, if mapping result be 1 then create rectangular object, if mapping result be 2 then create the straight line object, if mapping result is 3 then creates oval object or the like.
if(iter->second==1)
{ // be rectangle is created rectangular object here.
}
else?if(iter->second==2)
{ // be straight line is created the straight line object here.
}
Because the node label in the SVG file generally is made up of character string, and the length of character string is irregular, makes that the versus speed of node label is relatively slow, in the SVG file, contains under the situation of great deal of nodes label, has obviously reduced the resolution speed of SVG file; After adopting this embodiment; Need not to carry out one by one again the comparison of character string; It is fast a lot of that the comparison speed of the irregular character string of comparison speed relative length of numeral is wanted; Therefore in the SVG file, contain under the situation of great deal of nodes label, adopt this embodiment can obviously improve the resolution speed of SVG file.
Need to prove; Simple label is except can being the integer in this embodiment; It also can be the character string (like 2 character string) of numeral, character or the regular length of other types; The comparison speed of other numerals or character is fast more a lot of than character string equally, the same purpose that can reach quickening SVG document analysis speed.
This embodiment is applicable to other too and follows the resolving of the significant file of node of XML form except the parsing that is applicable to the SVG file.
Second embodiment of the invention relates to a kind of fast resolving method of SVG file equally, and is roughly the same with the first embodiment thought, and its difference is, only that node label is corresponding with numeral in the first embodiment of the invention, sets up the mapping relations table; And in this embodiment, further that the attribute tags in the various node labels is corresponding with numeral, also set up the best property of attribute mapping relation table, and preserve the corresponding attribute classification of each simple identification.As transverse axis location tags x correspondence 1 is set, longitudinal axis location tags y correspondence 2, width label width correspondence 3, Fill Color label fill correspondence 4, line color label srtoke corresponding 5 or the like.
In the process of resolving the SVG file, when reading attribute tags, from this best property of attribute mapping relation table, find the corresponding simple identification of this attribute tags through mapping algorithm equally; With finding simple identification and each simple identification of being preserved to compare, confirm the pairing attribute classification of this simple identification that finds, the attribute of corresponding classification is set for object.
Because each node comprises a plurality of attributes usually; Attribute tags generally also is to be made up of the unfixed character string of length; Under the more situation of number of nodes, contrast and these attribute tags of identification need spend the plenty of time equally, through this embodiment; Make the contrast and the identification speed of attribute tags obviously speed, reach the effect that further speeds SVG document analysis speed.
Third embodiment of the invention relates to a kind of SVG file fast resolving system, comprises: memory module is used for storing the mapping relations table of various types of other node label of SVG file and corresponding simple identification and the pairing object type of each simple identification; Mapping block is used in the process of resolving the SVG file, when reading node label, from this mapping relations table, finds the corresponding simple identification of this node label through mapping algorithm; The Object Creation module, the simple identification that is used for finding according to this mapping block is judged the pairing object type of this node label, and creates the object of corresponding classification; Specifically can the simple identification that find be compared with each existing simple identification one by one,, then set up the object of this simple identification corresponding class if consistent.
Wherein, the node label in the SVG file is generally the not character string of regular length, and corresponding simple identification can be numeral, character or regular length character string etc., is good with integer especially.
Above-mentioned mapping relations table can be the Hash mapping table, and mapping algorithm can be " hash-map " algorithm, also can be existing other mapping methods.
As further improvement, the mapping relations table of the attribute tags that this memory module can also be used for storing the various node labels of SVG file and corresponding simple identification, and preserve the corresponding attribute classification of each simple identification; This mapping block can also be used in the process of resolving the SVG file, when reading attribute tags, from the mapping relations table of this attribute tags and simple identification, finds the corresponding simple identification of this attribute tags through mapping algorithm; The simple identification that this Object Creation module can also be used for finding according to this mapping block is judged the pairing attribute classification of this attribute tags, and for object the attribute of corresponding classification is set.Each simple identification of being preserved in simple identification that specifically can mapping block be found and the memory module compares, and confirms the pairing attribute classification of this simple identification, and for object the attribute of corresponding classification is set.
Though through reference some preferred implementation of the present invention; The present invention is illustrated and describes; But those of ordinary skill in the art should be understood that and can do various changes to it in form with on the details, and without departing from the spirit and scope of the present invention.