Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
163 lines (137 sloc) 3.81 KB
#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 BaseContext
{
public:
BaseContext()
{}
BaseContext(BaseContext const & parent)
{}
void on_exit_element()
{}
void transform_matrix(const boost::array<double, 6> & matrix)
{}
// Viewport Events Policy
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()
{}
};
class ShapeContext: public BaseContext
{
public:
ShapeContext(BaseContext const & parent)
{}
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()
{}
};
struct ChildContextFactories
{
template<class ParentContext, class ElementTag, class Enable = void>
struct apply
{
// Default definition handles "svg" and "g" elements
typedef factory::context::on_stack<BaseContext> type;
};
};
// This specialization handles all shape elements (elements from traits::shape_elements sequence)
template<class ElementTag>
struct ChildContextFactories::apply<BaseContext, ElementTag,
typename boost::enable_if<boost::mpl::has_key<traits::shape_elements, ElementTag> >::type>
{
typedef factory::context::on_stack<ShapeContext> type;
};
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)
{
BaseContext context;
document_traversal<
processed_elements<processed_elements_t>,
processed_attributes<processed_attributes_t>,
viewport_policy<policy::viewport::as_transform>,
context_factories<ChildContextFactories>
>::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;
}