[go: up one dir, main page]

CN111611770A - Analysis method and system of SVG file - Google Patents

Analysis method and system of SVG file Download PDF

Info

Publication number
CN111611770A
CN111611770A CN201910138928.9A CN201910138928A CN111611770A CN 111611770 A CN111611770 A CN 111611770A CN 201910138928 A CN201910138928 A CN 201910138928A CN 111611770 A CN111611770 A CN 111611770A
Authority
CN
China
Prior art keywords
path
elements
graphic
svg
rectangle
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN201910138928.9A
Other languages
Chinese (zh)
Inventor
蔡佳妮
孙煜
张立东
苏贵明
王天瑞
王雪炯
袁文杰
洪福
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Shanghai Shentong Metro Co ltd
Original Assignee
Shanghai Shentong Metro Co ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Shanghai Shentong Metro Co ltd filed Critical Shanghai Shentong Metro Co ltd
Priority to CN201910138928.9A priority Critical patent/CN111611770A/en
Publication of CN111611770A publication Critical patent/CN111611770A/en
Pending legal-status Critical Current

Links

Images

Landscapes

  • Image Generation (AREA)

Abstract

本发明公开了一种SVG文件的解析方法及系统,解析方法包括:加载读取SVG文件并生成DOM节点树,新建对应的JSON数据文件;逐层遍历各个节点并进行识别;当识别出节点为图形元素节点时,获取图形属性并转换为SVG的路径元素;将路径元素的路径格式标准化,生成标准路径;解析标准路径后生成JSON对象,写入JSON数据文件中,返回遍历节点的步骤,直至遍历完所述DOM节点树的所有节点后结束流程。本发明通过对SVG中绘图元素解析成通用平台绘图API兼容性较好的路径,标准化输出为JSON数据,使得通用平台能以较好的性能以及较高的精准度在通用平台中还原出SVG图。

Figure 201910138928

The invention discloses an SVG file parsing method and system. The parsing method includes: loading and reading the SVG file, generating a DOM node tree, and creating a corresponding JSON data file; traversing each node layer by layer and identifying it; When a graphic element node is used, obtain graphic attributes and convert it into a path element of SVG; standardize the path format of the path element to generate a standard path; generate a JSON object after parsing the standard path, write it into a JSON data file, and return to the steps of traversing the node until The process ends after traversing all the nodes of the DOM node tree. The invention parses the drawing elements in the SVG into a path with better compatibility of the drawing API of the general platform, and standardizes the output as JSON data, so that the general platform can restore the SVG picture in the general platform with better performance and higher accuracy .

Figure 201910138928

Description

SVG文件的解析方法及系统Analysis method and system of SVG file

技术领域technical field

本发明涉及图形学技术领域,特别涉及一种SVG文件的解析方法及系统。The invention relates to the technical field of graphics, in particular to a method and system for parsing an SVG file.

背景技术Background technique

SVG(Scalable Vector Graphics,可缩放矢量图形)是一种使用XML(ExtensibleMarkup Language,可扩展标记语言)来描述二维图形和绘图程序的一种语言。SVG图形存储容量小,对网络的输入负载小,网络传输延时小,支持交互和动画,而且图像的放大、缩小操作不会使图像失真。SVG本身的诸多优点,十分适合作为Web(互联网)二维地图的载体。SVG (Scalable Vector Graphics, Scalable Vector Graphics) is a language that uses XML (Extensible Markup Language, Extensible Markup Language) to describe two-dimensional graphics and drawing programs. SVG graphics storage capacity is small, the input load to the network is small, the network transmission delay is small, it supports interaction and animation, and the zoom-in and zoom-out operations of the image will not distort the image. SVG itself has many advantages, it is very suitable as the carrier of Web (Internet) two-dimensional map.

由于SVG的开放性以及相关绘图方法支持较丰富,使得绘制SVG成本较低,效率较高。但也正由于其开源的特性,所见即所得,往往绘图人员绘制出来的SVG图知识产权无法得到有效保障,而位图本身存在有损、失真的特性又无法取代矢量SVG,使得SVG难以得到大范围推广。因此要发挥出SVG绘制高效、无损缩放的优点,又要避免SVG原图泄露的问题,在通用平台上动态绘制出SVG图是最好的解决方案。然而,若要在Android(安卓)、桌面Java(一种面向对象的计算机编程语言)端、网页端Canvas(一种HTML(超文本标记语言)5新增的组件,用于绘制各种图标、动画等)等通用平台上还原出SVG图,各自的绘图API(应用程序编程接口)方法并不完全兼容SVG绘图元素,无法通过直接解析SVG图的图形元素精准还原出原图。Due to the openness of SVG and the rich support of related drawing methods, the cost of drawing SVG is lower and the efficiency is higher. However, due to its open-source nature, what you see is what you get, often the intellectual property rights of the SVG images drawn by the drafters cannot be effectively guaranteed, and the bitmap itself has the characteristics of loss and distortion and cannot replace the vector SVG, making SVG difficult to obtain. Widespread promotion. Therefore, to give full play to the advantages of efficient SVG drawing and lossless scaling, and to avoid the problem of SVG original image leakage, it is the best solution to dynamically draw SVG images on a common platform. However, if you want to add new components on Android (Android), desktop Java (an object-oriented computer programming language), and Canvas (an HTML (Hypertext Markup Language) 5 on the web page to draw various icons, SVG images are restored on general platforms such as animation, etc., and their respective drawing API (application programming interface) methods are not fully compatible with SVG drawing elements, and the original image cannot be accurately restored by directly parsing the graphic elements of the SVG image.

发明内容SUMMARY OF THE INVENTION

本发明要解决的技术问题是为了克服现有技术中各个通用平台并不完全兼容SVG绘图元素,无法通过直接解析SVG图的图形元素精准还原出原图缺陷,提供一种SVG文件的解析方法及系统。The technical problem to be solved by the present invention is to overcome the defect that each general platform in the prior art is not fully compatible with SVG drawing elements and cannot accurately restore the original image by directly analyzing the graphic elements of the SVG image, and provides an SVG file parsing method and method. system.

本发明是通过下述技术方案来解决上述技术问题:The present invention solves the above-mentioned technical problems through the following technical solutions:

本发明提供了一种SVG文件的解析方法,包括以下步骤:The present invention provides a method for parsing an SVG file, comprising the following steps:

加载读取SVG文件并生成DOM(文档对象模型)节点树,新建一个与所述SVG文件相对应的JSON(一种轻量级的数据交换格式)数据文件;Load and read the SVG file and generate a DOM (Document Object Model) node tree, and create a new JSON (a lightweight data interchange format) data file corresponding to the SVG file;

逐层遍历所述DOM节点树的各个节点并对所述节点进行识别;Traverse each node of the DOM node tree layer by layer and identify the node;

当识别出所述节点为图形元素节点时,获取所述图形元素节点的图形属性并转换为SVG的路径元素;When identifying that the node is a graphic element node, obtain the graphic attribute of the graphic element node and convert it into a path element of SVG;

将所述路径元素的路径格式标准化,生成所述图形元素节点的标准路径;Standardizing the path format of the path element to generate a standard path of the graphic element node;

解析所述标准路径后生成所述图形元素节点的JSON对象,并写入所述JSON数据文件中,然后返回所述遍历所述DOM节点树的各个节点的步骤,直至遍历完所述DOM节点树的所有节点后结束流程。After parsing the standard path, generate the JSON object of the graphic element node, write it into the JSON data file, and then return to the step of traversing each node of the DOM node tree until the DOM node tree is traversed End the process after all nodes of .

较佳地,所述图形元素为矩形元素、圆元素、椭圆元素或多边形元素;Preferably, the graphic element is a rectangle element, a circle element, an ellipse element or a polygon element;

所述获取所述图形元素节点的图形属性并转换为SVG的路径元素的步骤具体包括:The step of acquiring the graphic attribute of the graphic element node and converting it into a path element of SVG specifically includes:

当根据所述图形属性确定所述图形元素为矩形元素时,获取矩形的各个属性,所述属性包括宽和高以及左上角坐标,根据所述属性连线所述矩形的四个角得到所述矩形的路径元素;When it is determined that the graphic element is a rectangular element according to the graphic attributes, various attributes of the rectangle are obtained, the attributes include width and height and the upper left corner coordinate, and the four corners of the rectangle are connected according to the attributes to obtain the Rectangular path element;

若所述矩形为圆角矩形,则所述属性还包括水平圆角和垂直圆角,采用贝塞尔曲线表达所述水平圆角和垂直圆角,并将贝塞尔曲线加入到所述矩形的四个角之前得到所述圆角矩形的路径元素;If the rectangle is a rectangle with rounded corners, the attribute further includes horizontal rounded corners and vertical rounded corners, and the horizontal rounded corners and vertical rounded corners are expressed by using Bezier curves, and the Bezier curve is added to the rectangle Get the path element of the rounded rectangle before the four corners;

当根据所述图形属性确定所述图形元素为圆元素时,获取圆的半径和圆心坐标,采用贝塞尔曲线模拟出圆,并根据贝塞尔曲线得到所述圆的路径元素;When it is determined that the graphic element is a circle element according to the graphic attribute, the radius and center coordinates of the circle are obtained, a Bezier curve is used to simulate a circle, and a path element of the circle is obtained according to the Bezier curve;

当根据所述图形属性确定所述图形元素为椭圆元素时,获取椭圆的中心坐标以及水平半径和垂直半径,采用贝塞尔曲线模拟出椭圆,并根据贝塞尔曲线得到所述椭圆的路径元素;When the graphic element is determined to be an ellipse element according to the graphic attribute, the center coordinates and the horizontal radius and the vertical radius of the ellipse are obtained, the ellipse is simulated by a Bezier curve, and the path element of the ellipse is obtained according to the Bezier curve ;

当根据所述图形属性确定所述图形元素为多边形元素时,获取多边形的点集合,由点集合连接成一条封闭的路径得到所述多边形的路径元素。When it is determined that the graphic element is a polygon element according to the graphic attribute, a set of points of the polygon is obtained, and the path element of the polygon is obtained by connecting the set of points to form a closed path.

较佳地,所述将所述路径元素的路径格式标准化的步骤具体包括:Preferably, the step of standardizing the path format of the path element specifically includes:

将所述路径元素的路径坐标统一采用绝对坐标,并统一坐标和绘图命令的间隔符。The path coordinates of the path elements are unified in absolute coordinates, and the coordinates and the spacers of the drawing commands are unified.

较佳地,所述解析所述标准路径后生成所述图形元素节点的JSON对象的步骤具体包括:解析所述标准路径,获取笔触属性和填充属性,生成所述JSON对象。Preferably, the step of generating the JSON object of the graphic element node after parsing the standard path specifically includes: parsing the standard path, acquiring stroke attributes and filling attributes, and generating the JSON object.

本发明还提供了一种SVG文件的解析系统,包括:The present invention also provides an SVG file parsing system, including:

加载读取模块,用于加载读取SVG文件并生成DOM节点树,新建一个与所述SVG文件相对应的JSON数据文件;The loading and reading module is used to load and read the SVG file and generate a DOM node tree, and create a new JSON data file corresponding to the SVG file;

遍历模块,用于逐层遍历所述DOM节点树的各个节点并对所述节点进行识别;A traversal module for traversing each node of the DOM node tree layer by layer and identifying the node;

转换模块,用于当识别出所述节点为图形元素节点时,获取所述图形元素节点的图形属性并转换为SVG的路径元素;a conversion module, configured to acquire the graphic attribute of the graphic element node and convert it into a path element of SVG when the node is identified as a graphic element node;

标准化模块,用于将所述路径元素的路径格式标准化,生成所述图形元素节点的标准路径;a standardization module, configured to standardize the path format of the path element, and generate a standard path of the graphic element node;

解析模块,用于解析所述标准路径后生成所述图形元素节点的JSON对象,并写入所述JSON数据文件中,然后调用所述遍历模块遍历所述DOM节点树的各个节点,直至遍历完所述DOM节点树的所有节点。A parsing module, for generating the JSON object of the graphic element node after parsing the standard path, and writing it into the JSON data file, and then calling the traversal module to traverse each node of the DOM node tree until the traversal is complete All nodes of the DOM node tree.

较佳地,所述图形元素为矩形元素、圆元素、椭圆元素或多边形元素;Preferably, the graphic element is a rectangle element, a circle element, an ellipse element or a polygon element;

所述转换模块具体用于:The conversion module is specifically used for:

当根据所述图形属性确定所述图形元素为矩形元素时,获取矩形的各个属性,所述属性包括宽和高以及左上角坐标,根据所述属性连线所述矩形的四个角得到所述矩形的路径元素;When it is determined that the graphic element is a rectangular element according to the graphic attributes, various attributes of the rectangle are obtained, the attributes include width and height and the upper left corner coordinate, and the four corners of the rectangle are connected according to the attributes to obtain the Rectangular path element;

若所述矩形为圆角矩形,则所述属性还包括水平圆角和垂直圆角,采用贝塞尔曲线表达所述水平圆角和垂直圆角,并将贝塞尔曲线加入到所述矩形的四个角之前得到所述圆角矩形的路径元素;If the rectangle is a rectangle with rounded corners, the attribute further includes horizontal rounded corners and vertical rounded corners, and the horizontal rounded corners and vertical rounded corners are expressed by using Bezier curves, and the Bezier curve is added to the rectangle Get the path element of the rounded rectangle before the four corners;

当根据所述图形属性确定所述图形元素为圆元素时,获取圆的半径和圆心坐标,采用贝塞尔曲线模拟出圆,并根据贝塞尔曲线得到所述圆的路径元素;When it is determined that the graphic element is a circle element according to the graphic attribute, the radius and center coordinates of the circle are obtained, a Bezier curve is used to simulate a circle, and a path element of the circle is obtained according to the Bezier curve;

当根据所述图形属性确定所述图形元素为椭圆元素时,获取椭圆的中心坐标以及水平半径和垂直半径,采用贝塞尔曲线模拟出椭圆,并根据贝塞尔曲线得到所述椭圆的路径元素;When the graphic element is determined to be an ellipse element according to the graphic attribute, the center coordinates and the horizontal radius and the vertical radius of the ellipse are obtained, the ellipse is simulated by a Bezier curve, and the path element of the ellipse is obtained according to the Bezier curve ;

当根据所述图形属性确定所述图形元素为多边形元素时,获取多边形的点集合,由点集合连接成一条封闭的路径得到所述多边形的路径元素。When it is determined that the graphic element is a polygon element according to the graphic attribute, a set of points of the polygon is obtained, and the path element of the polygon is obtained by connecting the set of points to form a closed path.

较佳地,所述标准化模块用于将所述路径元素的路径坐标统一采用绝对坐标,并统一坐标和绘图命令的间隔符。Preferably, the standardization module is configured to uniformly use absolute coordinates for the path coordinates of the path elements, and to unify the coordinates and spacers of drawing commands.

较佳地,所述解析模块用于解析所述标准路径,获取笔触属性和填充属性,生成所述JSON对象。Preferably, the parsing module is configured to parse the standard path, obtain the stroke attribute and fill attribute, and generate the JSON object.

本发明的积极进步效果在于:本发明通过对SVG中绘图元素解析成通用平台绘图API兼容性较好的路径(PATH),标准化输出为JSON数据,使得通用平台能以较好的性能以及较高的精准度在通用平台中还原出SVG图。通过通用平台动态绘制出SVG图,可以得到无损缩放的SVG还原图,同时又避免了由于SVG开放标准的特性而导致的绘图人员知识产权无法保障的问题。The positive improvement effect of the present invention is that: the present invention parses the drawing elements in the SVG into a path (PATH) with better compatibility of the drawing API of the general platform, and standardizes the output as JSON data, so that the general platform can have better performance and higher The accuracy of SVG restores the SVG image in the general platform. By dynamically drawing an SVG image on a common platform, a lossless zoomed SVG restoration image can be obtained, and at the same time, the problem of inability to protect the intellectual property rights of the drafters due to the characteristics of the SVG open standard is avoided.

附图说明Description of drawings

图1为本发明较佳实施例的SVG文件的解析方法的流程图。FIG. 1 is a flowchart of a method for parsing an SVG file according to a preferred embodiment of the present invention.

图2为本发明较佳实施例的SVG文件的解析方法中将矩形元素转换为SVG的路径元素的具体流程图。FIG. 2 is a specific flowchart of converting a rectangular element into a path element of an SVG in a method for parsing an SVG file according to a preferred embodiment of the present invention.

图3为本发明较佳实施例的SVG文件的解析方法中将圆元素转换为SVG的路径元素的具体流程图。FIG. 3 is a specific flowchart of converting a circle element into a path element of SVG in a method for parsing an SVG file according to a preferred embodiment of the present invention.

图4为本发明较佳实施例的SVG文件的解析方法中将椭圆元素转换为SVG的路径元素的具体流程图。FIG. 4 is a specific flowchart of converting an ellipse element into a path element of an SVG in a method for parsing an SVG file according to a preferred embodiment of the present invention.

图5为本发明较佳实施例的SVG文件的解析方法中将多边形元素转换为SVG的路径元素的具体流程图。FIG. 5 is a specific flowchart of converting polygon elements into path elements of SVG in a method for parsing an SVG file according to a preferred embodiment of the present invention.

图6为本发明较佳实施例的SVG文件的解析系统的模块示意图。FIG. 6 is a schematic block diagram of an SVG file parsing system according to a preferred embodiment of the present invention.

具体实施方式Detailed ways

下面通过实施例的方式进一步说明本发明,但并不因此将本发明限制在所述的实施例范围之中。The present invention is further described below by way of examples, but the present invention is not limited to the scope of the described examples.

如图1所示,本发明的SVG文件的解析方法包括以下步骤:As shown in Figure 1, the SVG file parsing method of the present invention includes the following steps:

步骤101、加载读取SVG文件并生成DOM节点树,新建一个与所述SVG文件相对应的JSON数据文件;Step 101, load and read the SVG file and generate a DOM node tree, and create a new JSON data file corresponding to the SVG file;

步骤102、逐层遍历所述DOM节点树的各个节点并对所述节点进行识别;Step 102, traverse each node of the DOM node tree layer by layer and identify the node;

步骤103、当识别出所述节点为图形元素节点时,获取所述图形元素节点的图形属性并转换为SVG的路径元素;Step 103, when the node is identified as a graphic element node, obtain the graphic attribute of the graphic element node and convert it into a path element of SVG;

步骤104、将所述路径元素的路径格式标准化,生成所述图形元素节点的标准路径;Step 104: Standardize the path format of the path element to generate a standard path of the graphic element node;

步骤105、解析所述标准路径后生成所述图形元素节点的JSON对象,并写入所述JSON数据文件中,然后返回步骤102,直至遍历完所述DOM节点树的所有节点后结束流程。Step 105: After parsing the standard path, generate the JSON object of the graphic element node, and write it into the JSON data file, and then return to step 102, and end the process after traversing all the nodes of the DOM node tree.

其中优选地,所述图形元素为矩形元素(rect)、圆元素(circle)、椭圆(ellipse)元素或多边形(polygon)元素;Preferably, the graphic element is a rectangle element (rect), a circle element (circle), an ellipse (ellipse) element or a polygon (polygon) element;

当根据所述图形属性确定所述图形元素为矩形元素时,如图2所示,将矩形元素转换为SVG的路径元素的步骤具体包括:When it is determined according to the graphic attribute that the graphic element is a rectangular element, as shown in FIG. 2 , the step of converting the rectangular element to a path element of SVG specifically includes:

步骤10311、获取矩形的各个属性,所述属性包括宽和高以及左上角坐标;Step 10311, obtain each attribute of the rectangle, the attribute includes width and height and upper left corner coordinates;

步骤10312、根据所述属性连线所述矩形的四个角得到所述矩形的路径元素;Step 10312, connecting the four corners of the rectangle according to the attribute to obtain the path element of the rectangle;

步骤10313、判断矩形是否为圆角矩形,若否,则结束转换,若是,则执行步骤10314;Step 10313, determine whether the rectangle is a rounded rectangle, if not, end the conversion, if so, execute step 10314;

步骤10314、获取圆角矩形的水平圆角和垂直圆角;Step 10314: Obtain the horizontal rounded corners and vertical rounded corners of the rounded rectangle;

步骤10315、采用贝塞尔曲线表达所述水平圆角和垂直圆角,并将贝塞尔曲线加入到所述矩形的四个角之前得到所述圆角矩形的路径元素,然后结束转换。Step 10315: Use Bezier curves to express the horizontal rounded corners and vertical rounded corners, and add the Bezier curves to the four corners of the rectangle to obtain the path elements of the rounded rectangle, and then end the conversion.

当根据所述图形属性确定所述图形元素为圆元素时,如图3所示,将圆元素转换为SVG的路径元素的步骤具体包括:When it is determined according to the graphic attribute that the graphic element is a circle element, as shown in FIG. 3 , the step of converting the circle element into a path element of SVG specifically includes:

步骤10321、获取圆的半径和圆心坐标;Step 10321: Obtain the radius of the circle and the coordinates of the center of the circle;

步骤10322、采用贝塞尔曲线模拟出圆,并根据贝塞尔曲线得到所述圆的路径元素,然后结束转换。Step 10322: Use the Bezier curve to simulate a circle, and obtain the path element of the circle according to the Bezier curve, and then end the conversion.

其中,在步骤10322中具体可采用四条贝塞尔曲线模拟出圆。Specifically, in step 10322, four Bezier curves may be used to simulate a circle.

当根据所述图形属性确定所述图形元素为椭圆元素时,如图4所示,将椭圆元素转换为SVG的路径元素的步骤具体包括:When it is determined according to the graphic attribute that the graphic element is an ellipse element, as shown in FIG. 4 , the step of converting the ellipse element to a path element of SVG specifically includes:

步骤10331、获取椭圆的中心坐标以及水平半径和垂直半径;Step 10331, obtain the center coordinates of the ellipse and the horizontal and vertical radius;

步骤10332、采用贝塞尔曲线模拟出椭圆,并根据贝塞尔曲线得到所述椭圆的路径元素,然后结束转换。Step 10332: Use the Bezier curve to simulate an ellipse, and obtain the path element of the ellipse according to the Bezier curve, and then end the conversion.

其中,在步骤10332中具体可以由水平半径组成的圆和垂直半径组成的圆依次取四分之一模拟成四段贝塞尔曲线组成椭圆。Wherein, in step 10332, a circle composed of a horizontal radius and a circle composed of a vertical radius may be sequentially taken a quarter to simulate to form an ellipse composed of four Bezier curves.

当根据所述图形属性确定所述图形元素为多边形元素时,如图5所示,将多边形元素转换为SVG的路径元素的步骤具体包括:When it is determined according to the graphic attribute that the graphic element is a polygonal element, as shown in FIG. 5 , the step of converting the polygonal element to a path element of SVG specifically includes:

步骤10341、获取多边形的点集合;Step 10341, obtain the point set of the polygon;

步骤10342、由点集合连接成一条封闭的路径得到所述多边形的路径元素,然后结束转换。Step 10342: Obtain the path element of the polygon by connecting the point set into a closed path, and then end the conversion.

在本发明的具体实施过程中,在步骤104中,具体会将所述路径元素的路径坐标统一采用绝对坐标,并统一坐标和绘图命令的间隔符,原因在于:In the specific implementation process of the present invention, in step 104, the path coordinates of the path elements will be uniformly adopted as absolute coordinates, and the coordinates and the spacers of the drawing commands will be unified. The reasons are:

在SVG文件中路径元素的形状是通过属性d来定义的,属性d的值是一个“命令+参数”的序列。由于SVG对路径格式具有较高的兼容性,所以路径的间隔符可以有多种格式,同时也支持数字的缩写以及科学记数法,且坐标也分为了绝对坐标和相对坐标两种。由于其格式较多,平台解析路径性能会受到影响。因此需要对路径格式进行标准化,统一使用绝对坐标,以及统一坐标和绘图命令的间隔符。The shape of the path element in the SVG file is defined by the attribute d, and the value of the attribute d is a sequence of "command + parameter". Since SVG has high compatibility with path formats, the spacers of paths can have various formats, and also support the abbreviation of numbers and scientific notation, and the coordinates are also divided into absolute coordinates and relative coordinates. Due to its large number of formats, the platform parsing path performance will be affected. Therefore, it is necessary to standardize the path format, uniformly use absolute coordinates, and unify coordinates and spacers for drawing commands.

而在步骤105中,具体会解析所述标准路径,获取笔触(stroke)属性和填充(fill)属性,生成所述JSON对象。In step 105, the standard path is specifically parsed, a stroke attribute and a fill attribute are obtained, and the JSON object is generated.

在本发明中,所述贝塞尔曲线(Bezier curve)是计算机图形学中相当重要的参数曲线,它通过一个方程来描述一条曲线,根据方程的最高阶数,又分为线性贝赛尔曲线、二次贝塞尔曲线、三次贝塞尔曲线和更高阶的贝塞尔曲线。In the present invention, the Bezier curve is a very important parameter curve in computer graphics. It describes a curve through an equation, and is divided into linear Bezier curves according to the highest order of the equation. , Quadratic Bezier, Cubic Bezier, and higher-order Bezier curves.

如图6所示,本发明还提供了一种SVG文件的解析系统,包括:As shown in FIG. 6 , the present invention also provides an SVG file parsing system, including:

加载读取模块1,用于加载读取SVG文件并生成DOM节点树,新建一个与所述SVG文件相对应的JSON数据文件;Loading and reading module 1 is used to load and read the SVG file and generate a DOM node tree, and create a new JSON data file corresponding to the SVG file;

遍历模块2,用于逐层遍历所述DOM节点树的各个节点并对所述节点进行识别;Traversing module 2, for traversing each node of the DOM node tree layer by layer and identifying the node;

转换模块3,用于当识别出所述节点为图形元素节点时,获取所述图形元素节点的图形属性并转换为SVG的路径元素;A conversion module 3, configured to acquire the graphic attribute of the graphic element node and convert it into a path element of SVG when the node is identified as a graphic element node;

标准化模块4,用于将所述路径元素的路径格式标准化,生成所述图形元素节点的标准路径;Standardization module 4, for standardizing the path format of the path element, and generating the standard path of the graphic element node;

解析模块5,用于解析所述标准路径后生成所述图形元素节点的JSON对象,并写入所述JSON数据文件中,然后调用所述遍历模块遍历所述DOM节点树的各个节点,直至遍历完所述DOM节点树的所有节点。Parsing module 5, for generating the JSON object of the graphic element node after parsing the standard path, and writing it into the JSON data file, and then calling the traversal module to traverse each node of the DOM node tree until traversing Finish all nodes of the DOM node tree.

其中,所述图形元素为矩形元素、圆元素、椭圆元素或多边形元素;Wherein, the graphic element is a rectangle element, a circle element, an ellipse element or a polygon element;

所述转换模块3具体用于:The conversion module 3 is specifically used for:

当根据所述图形属性确定所述图形元素为矩形元素时,获取矩形的各个属性,所述属性包括宽和高以及左上角坐标,根据所述属性连线所述矩形的四个角得到所述矩形的路径元素;When it is determined that the graphic element is a rectangular element according to the graphic attributes, various attributes of the rectangle are obtained, the attributes include width and height and the upper left corner coordinate, and the four corners of the rectangle are connected according to the attributes to obtain the Rectangular path element;

若所述矩形为圆角矩形,则所述属性还包括水平圆角和垂直圆角,采用贝塞尔曲线表达所述水平圆角和垂直圆角,并将贝塞尔曲线加入到所述矩形的四个角之前得到所述圆角矩形的路径元素;If the rectangle is a rectangle with rounded corners, the attribute further includes horizontal rounded corners and vertical rounded corners, and the horizontal rounded corners and vertical rounded corners are expressed by using Bezier curves, and the Bezier curve is added to the rectangle Get the path element of the rounded rectangle before the four corners;

当根据所述图形属性确定所述图形元素为圆元素时,获取圆的半径和圆心坐标,采用贝塞尔曲线模拟出圆,并根据贝塞尔曲线得到所述圆的路径元素;When it is determined that the graphic element is a circle element according to the graphic attribute, the radius and center coordinates of the circle are obtained, a Bezier curve is used to simulate a circle, and a path element of the circle is obtained according to the Bezier curve;

当根据所述图形属性确定所述图形元素为椭圆元素时,获取椭圆的中心坐标以及水平半径和垂直半径,采用贝塞尔曲线模拟出椭圆,并根据贝塞尔曲线得到所述椭圆的路径元素;When the graphic element is determined to be an ellipse element according to the graphic attribute, the center coordinates and the horizontal radius and the vertical radius of the ellipse are obtained, the ellipse is simulated by a Bezier curve, and the path element of the ellipse is obtained according to the Bezier curve ;

当根据所述图形属性确定所述图形元素为多边形元素时,获取多边形的点集合,由点集合连接成一条封闭的路径得到所述多边形的路径元素。When it is determined that the graphic element is a polygon element according to the graphic attribute, a set of points of the polygon is obtained, and the path element of the polygon is obtained by connecting the set of points to form a closed path.

所述标准化模块4用于将所述路径元素的路径坐标统一采用绝对坐标,并统一坐标和绘图命令的间隔符。The standardization module 4 is used for uniformly adopting absolute coordinates for the path coordinates of the path elements, and unifying the coordinates and spacers of drawing commands.

所述解析模块5用于解析所述标准路径,获取笔触属性和填充属性,生成所述JSON对象。The parsing module 5 is used for parsing the standard path, acquiring the stroke attribute and filling attribute, and generating the JSON object.

虽然以上描述了本发明的具体实施方式,但是本领域的技术人员应当理解,这仅是举例说明,本发明的保护范围是由所附权利要求书限定的。本领域的技术人员在不背离本发明的原理和实质的前提下,可以对这些实施方式做出多种变更或修改,但这些变更和修改均落入本发明的保护范围。Although the specific embodiments of the present invention are described above, those skilled in the art should understand that this is only an illustration, and the protection scope of the present invention is defined by the appended claims. Those skilled in the art can make various changes or modifications to these embodiments without departing from the principle and essence of the present invention, but these changes and modifications all fall within the protection scope of the present invention.

Claims (8)

1. An SVG file parsing method is characterized by comprising the following steps:
loading and reading an SVG file, generating a DOM node tree, and creating a JSON data file corresponding to the SVG file;
traversing each node of the DOM node tree layer by layer and identifying the node;
when the node is identified to be a graphic element node, acquiring the graphic attribute of the graphic element node and converting the graphic attribute into a path element of the SVG;
standardizing the path format of the path elements to generate standard paths of the graphic element nodes;
and generating a JSON object of the graphic element node after analyzing the standard path, writing the JSON object into the JSON data file, and then returning to the step of traversing all nodes of the DOM node tree until the process is finished after all nodes of the DOM node tree are traversed.
2. The parsing method for SVG files according to claim 1, wherein said graphic elements are rectangular elements, circular elements, elliptical elements or polygonal elements;
the step of obtaining the graphic attributes of the graphic element nodes and converting the graphic attributes into the path elements of the SVG specifically comprises the following steps:
when the graphic element is determined to be a rectangular element according to the graphic attributes, acquiring each attribute of the rectangle, wherein the attributes comprise width, height and coordinates of the upper left corner, and connecting the four corners of the rectangle according to the attributes to obtain path elements of the rectangle;
if the rectangle is a rounded rectangle, the attributes further comprise a horizontal rounded corner and a vertical rounded corner, the horizontal rounded corner and the vertical rounded corner are expressed by adopting a Bezier curve, and path elements of the rounded rectangle are obtained before the Bezier curve is added to the four corners of the rectangle;
when the figure element is determined to be a circle element according to the figure attribute, acquiring the radius and the center coordinate of the circle, simulating the circle by adopting a Bezier curve, and obtaining a path element of the circle according to the Bezier curve;
when the figure element is determined to be an ellipse element according to the figure attribute, acquiring the central coordinate, the horizontal radius and the vertical radius of the ellipse, simulating the ellipse by adopting a Bezier curve, and acquiring a path element of the ellipse according to the Bezier curve;
and when the figure elements are determined to be polygon elements according to the figure attributes, acquiring a point set of the polygon, and connecting the point set into a closed path to obtain path elements of the polygon.
3. The parsing method for SVG files according to claim 1, wherein said step of standardizing the path format of said path elements specifically comprises:
and uniformly adopting absolute coordinates for the path coordinates of the path elements, and uniformly combining the coordinates and the interval symbols of the drawing commands.
4. The parsing method for SVG files according to claim 1, wherein said step of generating JSON objects for said graphic element nodes after parsing said standard path specifically comprises: and analyzing the standard path, acquiring a stroke attribute and a filling attribute, and generating the JSON object.
5. A parsing system for SVG files, comprising:
the loading and reading module is used for loading and reading the SVG file, generating a DOM node tree and creating a JSON data file corresponding to the SVG file;
the traversal module is used for traversing each node of the DOM node tree layer by layer and identifying the node;
the conversion module is used for acquiring the graphic attributes of the graphic element nodes and converting the graphic attributes into the path elements of the SVG when the nodes are identified as the graphic element nodes;
the standardization module is used for standardizing the path format of the path elements and generating standard paths of the graphic element nodes;
and the analysis module is used for generating the JSON object of the graphic element node after analyzing the standard path, writing the JSON object into the JSON data file, and calling the traversal module to traverse each node of the DOM node tree until all nodes of the DOM node tree are traversed.
6. The parsing system of an SVG file according to claim 5, wherein said graphic elements are rectangular elements, circular elements, elliptical elements or polygonal elements;
the conversion module is specifically configured to:
when the graphic element is determined to be a rectangular element according to the graphic attributes, acquiring each attribute of the rectangle, wherein the attributes comprise width, height and coordinates of the upper left corner, and connecting the four corners of the rectangle according to the attributes to obtain path elements of the rectangle;
if the rectangle is a rounded rectangle, the attributes further comprise a horizontal rounded corner and a vertical rounded corner, the horizontal rounded corner and the vertical rounded corner are expressed by adopting a Bezier curve, and path elements of the rounded rectangle are obtained before the Bezier curve is added to the four corners of the rectangle;
when the figure element is determined to be a circle element according to the figure attribute, acquiring the radius and the center coordinate of the circle, simulating the circle by adopting a Bezier curve, and obtaining a path element of the circle according to the Bezier curve;
when the figure element is determined to be an ellipse element according to the figure attribute, acquiring the central coordinate, the horizontal radius and the vertical radius of the ellipse, simulating the ellipse by adopting a Bezier curve, and acquiring a path element of the ellipse according to the Bezier curve;
and when the figure elements are determined to be polygon elements according to the figure attributes, acquiring a point set of the polygon, and connecting the point set into a closed path to obtain path elements of the polygon.
7. The parsing system of an SVG file according to claim 5 wherein said normalization module is operable to unify path coordinates of said path elements in absolute coordinates and in space of drawing commands.
8. The parsing system of an SVG file according to claim 5 wherein said parsing module is adapted to parse said standard path, obtain stroke attributes and fill attributes, and generate said JSON object.
CN201910138928.9A 2019-02-25 2019-02-25 Analysis method and system of SVG file Pending CN111611770A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201910138928.9A CN111611770A (en) 2019-02-25 2019-02-25 Analysis method and system of SVG file

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201910138928.9A CN111611770A (en) 2019-02-25 2019-02-25 Analysis method and system of SVG file

Publications (1)

Publication Number Publication Date
CN111611770A true CN111611770A (en) 2020-09-01

Family

ID=72195793

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201910138928.9A Pending CN111611770A (en) 2019-02-25 2019-02-25 Analysis method and system of SVG file

Country Status (1)

Country Link
CN (1) CN111611770A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113360148A (en) * 2021-07-09 2021-09-07 珠海万纬物流发展有限公司 Graph processing method and device and electronic equipment

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101824712A (en) * 2009-12-31 2010-09-08 上海市色织科学技术研究所 Method and device for controlling carpet loom apparatus to automatically generate pattern on base fabric
US20120299932A1 (en) * 2009-12-30 2012-11-29 Telecom Italia S.P.A. Method for the calculation of the bounding box of vectorial graphic shapes
CN107291476A (en) * 2017-06-27 2017-10-24 北京微影时代科技有限公司 Canvas code generating methods, device, electronic equipment and storage medium
US9965446B1 (en) * 2013-07-19 2018-05-08 Amazon Technologies, Inc. Formatting a content item having a scalable object

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20120299932A1 (en) * 2009-12-30 2012-11-29 Telecom Italia S.P.A. Method for the calculation of the bounding box of vectorial graphic shapes
CN101824712A (en) * 2009-12-31 2010-09-08 上海市色织科学技术研究所 Method and device for controlling carpet loom apparatus to automatically generate pattern on base fabric
US9965446B1 (en) * 2013-07-19 2018-05-08 Amazon Technologies, Inc. Formatting a content item having a scalable object
CN107291476A (en) * 2017-06-27 2017-10-24 北京微影时代科技有限公司 Canvas code generating methods, device, electronic equipment and storage medium

Non-Patent Citations (2)

* Cited by examiner, † Cited by third party
Title
凹凸实验室: "聊聊 SVG 基本形状转换那些事", pages 1 - 7, Retrieved from the Internet <URL:https://jelly.jd.com/article/6006b1045b6c6a01506c87d6> *
周虎;李月;禚洪昌;吴平平;: "SVG在手工枪刺地毯簇绒自动化中的应用", 制造业自动化, vol. 37, no. 05, 10 May 2015 (2015-05-10), pages 39 - 41 *

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113360148A (en) * 2021-07-09 2021-09-07 珠海万纬物流发展有限公司 Graph processing method and device and electronic equipment

Similar Documents

Publication Publication Date Title
CN104484216B (en) Service interface document and on-line testing instrument generation method, device
CN102609255B (en) A kind of generation method of Spring IOC configuration file
CN107992589B (en) SVG map data loading method, device and system
CN110008366B (en) Construction drawing previewing method and system
KR20130143725A (en) Cross-compiling swf to html using an intermediate format
CN111459501B (en) SVG-based Web configuration picture storage and display system, method and medium
CN112966469B (en) Method, device, equipment and storage medium for processing charts in document
CN114417064A (en) Data processing method and device, computer equipment and storage medium
CN111951356A (en) Animation rendering method based on JSON data format
CN108875001A (en) A kind of method of large capacity geographical spatial data export VCT file
CN111078217A (en) Brain graph generation method, apparatus and computer-readable storage medium
US20090228863A1 (en) Populating information contained in java annotations into existing emf models
TW201317935A (en) System and method for analyzing measurement graphic documents
CN111611770A (en) Analysis method and system of SVG file
WO2022083221A1 (en) Method for adapting web-end video editor to mobile end
CN115268904A (en) User interface design file generation method, device, equipment and medium
CN111949353A (en) iOS-based mobile APP full page grayscale method and device
CN112131337A (en) Method, device, system and storage medium for processing unstructured data
CN109117207B (en) Data processing method of business process model
CN112396681A (en) Animation generation method and device and storage medium
CN116881244A (en) Real-time processing method and device for space data based on column storage database
CN116700704A (en) Image processing method, device, storage medium and electronic equipment
CN112764763B (en) Code conversion method, system, equipment and computer readable storage medium
US7730388B2 (en) Converting an enhanced metafile into a chronologically independent object property list for conversion into a PDF document
CN114090666A (en) Slide display method, apparatus, device and storage medium

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination