When choosing a format for storing or exchanging vector 2D images, SVG is one of the main contenders, due to its openness and prevalence. For all its merits, the authors, in my opinion, were overly fascinated by the convenience and flexibility in creating documents, which led to great variability and redundancy, and, consequently, the complexity of reading. In addition, for the sake of compactness, various grammars were invented, embedded in XML, which also added a headache to programmers.
Now there are several C / C ++ libraries that can load SVG and draw it into raster, but this is only a small part of 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 the SVG data in a convenient way.
The task was not to impose on the programmer any simplifications of the initial information from the SVG. For example, the developer is often not interested in what units of measurement the length was given, it is more convenient for him to work with the values ​​reduced to the same dimension. But in some cases (for example, creating SVG DOM) information about the units must be passed to the program. Or rectangles, circles, etc. - in some cases it is convenient to represent them in ways (path) for uniformity, but sometimes we want to know what kind of a figure it was.
')
So, meet,
SVG ++ 1.0 !
- Free for commercial and non-commercial use, the license is Boost .
- Header-only, cross-platform, enough C ++ 03.
- From third-party libraries, only Boost is required. Oh yeah, another XML parser for your taste. Out of the box, there are adapters for RapidXML NS , libxml2, and MSXML, but the support of others is easy.
As is customary, a motivating example:
#include <rapidxml_ns/rapidxml_ns.hpp> #include <svgpp/policy/xml/rapidxml_ns.hpp> #include <svgpp/svgpp.hpp> using namespace svgpp; class Context { public: void on_enter_element(tag::element::any); void on_exit_element(); void transform_matrix(const boost::array<double, 6> & 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_elements_t>, processed_attributes<processed_attributes_t> >::load_document(xml_root_element, context); }
This small code allows Context objects to get information about the geometry of SVG objects. To do this, “under the hood” of SVG ++, support is provided for the features of the SVG
path , the
basic shapes ,
units and
transformations .
And this is only a small part of the capabilities of SVG ++.In fact, for full support of geometry (even without drawing styles, clipping, masks, etc.), several more steps are required (take into account the size of the viewports, device resolution, links, etc.), but about this in the examples, tutorial'e and help.The library does not draw and will not draw anything by itself, however, it includes the demo application svgpp / src / demo / render, which implements rasterization of SVG (some subset of the specification), using
AGG or GDI + for drawing.
For the full use of the library is very desirable familiarity with the
specification of SVG.
- Library home page with documentation - svgpp.org (documentation is being prepared for translation into English, therefore temporary mishmash Russian and english languages)
- GitHub repository github.com/svgpp/svgpp
- Forum on Google Groups