Reading SVG in C ++
Now there are several C / C ++ libraries that can load SVG and draw it into a raster, but this is only a small part of the possible applications of SVG in applications.
I developed a C ++ library that should take on the implementation of most of the nuances of the specification, providing SVG data in a convenient way. In this case, the task was not to impose on the programmer any simplifications of the initial information from SVG. For example, a developer is often not interested in what units of measurement the length was set for, it is more convenient for him to work with values reduced to the same dimension. But in some cases (for example, creating an SVG DOM), information about units must be transferred to the program. Or rectangles, circles, etc. - in some cases they are conveniently represented by paths for uniformity, but sometimes we want to know what shape it was.
So, welcome, SVG ++ 1.0 !
- Free for commercial and non-commercial use, license - Boost .
- Header-only, cross-platform, enough C ++ 03.
- Of the third-party libraries, only Boost is required. Oh yes, another XML parser to your taste. Out of the box there are adapters for RapidXML NS , libxml2 and MSXML, but support for others is easy to add.
As accepted, a motivating example:
#include
#include
#include
using namespace svgpp;
class Context
{
public:
void on_enter_element(tag::element::any);
void on_exit_element();
void transform_matrix(const boost::array & matrix);
void path_move_to(double x, double y, tag::coordinate::absolute);
void path_line_to(double x, double y, tag::coordinate::absolute);
void path_cubic_bezier_to(
double x1, double y1,
double x2, double y2,
double x, double y,
tag::coordinate::absolute);
void path_quadratic_bezier_to(
double x1, double y1,
double x, double y,
tag::coordinate::absolute);
void path_elliptical_arc_to(
double rx, double ry, double x_axis_rotation,
bool large_arc_flag, bool sweep_flag,
double x, double y,
tag::coordinate::absolute);
void path_close_subpath();
void path_exit();
};
typedef
boost::mpl::set<
tag::element::circle,
tag::element::ellipse,
tag::element::line,
tag::element::path,
tag::element::polygon,
tag::element::polyline,
tag::element::rect,
tag::element::svg,
tag::element::g
>::type processed_elements_t;
typedef
boost::mpl::insert<
traits::shapes_attributes_by_element,
tag::attribute::transform
>::type processed_attributes_t;
void loadSvg(rapidxml_ns::xml_node<> const * xml_root_element)
{
Context context;
document_traversal<
processed_elements,
processed_attributes
>::load_document(xml_root_element, context);
}
This small code allows Context objects to obtain information about the geometry of SVG objects. To do this, “under the hood” of SVG ++, support for features of SVG path , basic shapes , units and transformations is implemented . And this is just a small part of the capabilities of SVG ++.
In fact, to fully support the geometry (even without the styles of drawing, clipping, masks, etc.), you need to take a few more steps (take into account the size of viewports, the resolution of the device, links, etc.), but more on that in the examples, tutorial and help.
The library does not draw and will not draw anything on its own, however, it includes the svgpp / src / demo / render demo application that implements SVG rasterization (a subset of the specification) using AGG or GDI + for rendering .
Familiarity with the SVG specification is highly desirable to make full use of the library .
- The main page of the library with documentation is svgpp.org (the documentation is being prepared for translation into English, therefore, a temporary mishmash of Russian and English languages)
- Github repository github.com/svgpp/svgpp
- Forum on Google Groups