C++解析SVG图,并保存为png等格式
什么是SVG图
SVG,即可缩放矢量图形(Scalable Vector Graphics),是一种 XML 应用,可以以一种简洁、可移植的形式表示图形信息。SVG 主要可以概括为以下几点:
SVG 指可伸缩矢量图形
SVG 用来定义网络的基于矢量的图形
SVG 使用 XML 格式定义图形
SVG 图像在放大或改变尺寸的情况下其图形质量不会有所损失
SVG 是万维网联盟的标准
SVG 与诸如 DOM 和 XSL 之类的 W3C 标准是一个整体。
C++解析SVG的库
这里使用的解析SVG的库为NanoSVG,其相关介绍如下:
NanoSVG is a simple stupid single-header-file SVG parse. The output of the parser is a list of cubic bezier shapes.
The library suits well for anything from rendering scalable icons in your editor application to prototyping a game.
NanoSVG supports a wide range of SVG features, but something may be missing, feel free to create a pull request!
The shapes in the SVG images are transformed by the viewBox and converted to specified units.
NanoSVG can return the paths in few different units. For example if you want to render an image, you may choose to get the paths in pixels, or if you are feeding the data into a CNC-cutter, you may want to use millimeters.
The units passed to NanoSVG should be one of: ‘px’, ‘pt’, ‘pc’ ‘mm’, ‘cm’, or ‘in’.
DPI (dots-per-inch) controls how the unit conversion is done.
If you don’t know or care about the units stuff, “px” and 96 should get you going.
NanoSVG 如何使用
In order to use NanoSVG in your own project, just copy nanosvg.h to your project.
In one C/C++ define NANOSVG_IMPLEMENTATION
before including the library to expand the NanoSVG implementation in that file.
NanoSVG depends on stdio.h
,string.h
and math.h
, they should be included where the implementation is expanded before including NanoSVG.
`#include <stdio.h>`
`#include <string.h>`
`#incl