Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
svgpp/src/samples/sample01c.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
132 lines (111 sloc)
3.04 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <rapidxml_ns/rapidxml_ns.hpp> | |
#include <svgpp/policy/xml/rapidxml_ns.hpp> | |
#include <svgpp/svgpp.hpp> | |
using namespace svgpp; | |
typedef rapidxml_ns::xml_node<> const * xml_element_t; | |
class Context | |
{ | |
public: | |
void on_enter_element(tag::element::any) | |
{} | |
void on_exit_element() | |
{} | |
void transform_matrix(const boost::array<double, 6> & matrix) | |
{} | |
void set_viewport(double viewport_x, double viewport_y, double viewport_width, double viewport_height) | |
{} | |
void set_viewbox_size(double viewbox_width, double viewbox_height) | |
{} | |
void disable_rendering() | |
{} | |
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< | |
// SVG Structural Elements | |
tag::element::svg, | |
tag::element::g, | |
// SVG Shape Elements | |
tag::element::circle, | |
tag::element::ellipse, | |
tag::element::line, | |
tag::element::path, | |
tag::element::polygon, | |
tag::element::polyline, | |
tag::element::rect | |
>::type processed_elements_t; | |
// This cryptic code just merges predefined sequences traits::shapes_attributes_by_element | |
// and traits::viewport_attributes with tag::attribute::transform attribute into single MPL sequence | |
typedef | |
boost::mpl::fold< | |
boost::mpl::protect< | |
boost::mpl::joint_view< | |
traits::shapes_attributes_by_element, | |
traits::viewport_attributes | |
> | |
>, | |
boost::mpl::set< | |
tag::attribute::transform | |
>::type, | |
boost::mpl::insert<boost::mpl::_1, boost::mpl::_2> | |
>::type processed_attributes_t; | |
void loadSvg(xml_element_t xml_root_element) | |
{ | |
Context context; | |
document_traversal< | |
processed_elements<processed_elements_t>, | |
processed_attributes<processed_attributes_t>, | |
viewport_policy<policy::viewport::as_transform> | |
>::load_document(xml_root_element, context); | |
} | |
int main() | |
{ | |
#define TEXT(x) #x | |
char text[] = | |
TEXT(<svg xmlns="http://www.w3.org/2000/svg">) | |
TEXT( <g transform="translate(20,40)">) | |
TEXT( <svg preserveAspectRatio="xMinYMin meet" viewBox="0 0 30 40" width="50" height="30">) | |
TEXT( <rect x='.5' y='.5' width='29' height='39'/>) | |
TEXT( </svg>) | |
TEXT( </g>) | |
TEXT(</svg>); | |
rapidxml_ns::xml_document<> doc; // character type defaults to char | |
try | |
{ | |
doc.parse<0>(text); | |
if (rapidxml_ns::xml_node<> * svg_element = doc.first_node("svg")) | |
{ | |
loadSvg(svg_element); | |
} | |
} | |
catch (std::exception const & e) | |
{ | |
std::cerr << "Error loading SVG: " << e.what() << std::endl; | |
return 1; | |
} | |
return 0; | |
} |