+ return (ObjectCount() == equ.ObjectCount()
+ && memcmp(m_objects.bounds.data(), equ.m_objects.bounds.data(), ObjectCount() * sizeof(Rect)) == 0
+ && memcmp(m_objects.data_indices.data(), equ.m_objects.data_indices.data(), ObjectCount() * sizeof(unsigned)) == 0
+ && memcmp(m_objects.beziers.data(), equ.m_objects.beziers.data(), m_objects.beziers.size() * sizeof(Bezier)) == 0);
+}
+
+
+
+// Behold my amazing tokenizing abilities
+static string & GetToken(const string & d, string & token, unsigned & i, const string & delims = "()[],{}<>;:=")
+{
+ token.clear();
+ while (i < d.size() && iswspace(d[i]))
+ {
+ ++i;
+ }
+
+ while (i < d.size())
+ {
+ if (iswspace(d[i]) || strchr(delims.c_str(),d[i]) != NULL)
+ {
+ if (token.size() == 0 && !iswspace(d[i]))
+ {
+ token += d[i++];
+ }
+ break;
+ }
+ token += d[i++];
+ }
+ //Debug("Got token \"%s\"", token.c_str());
+ return token;
+}
+
+static void GetXYPair(const string & d, Real & x, Real & y, unsigned & i,const string & delims = "()[],{}<>;:=")
+{
+ string token("");
+ while (GetToken(d, token, i, delims) == ",");
+ x = RealFromStr(token);
+ if (GetToken(d, token, i, delims) != ",")
+ {
+ Fatal("Expected \",\" seperating x,y pair");
+ }
+ y = RealFromStr(GetToken(d,token,i,delims));
+}
+
+static bool GetKeyValuePair(const string & d, string & key, string & value, unsigned & i, const string & delims = "()[],{}<>;:=")
+{
+ key = "";
+ string token;
+ while (GetToken(d, token, i, delims) == ":" || token == ";");
+ key = token;
+ if (GetToken(d, token, i, delims) != ":")
+ {
+ Error("Expected \":\" seperating key:value pair");
+ return false;
+ }
+ value = "";
+ GetToken(d, value, i, delims);
+ return true;
+}
+
+static void TransformXYPair(Real & x, Real & y, const SVGMatrix & transform)
+{
+ Real x0(x);
+ x = transform.a * x + transform.c * y + transform.e;
+ y = transform.b * x0 + transform.d * y + transform.f;
+}
+
+void Document::ParseSVGTransform(const string & s, SVGMatrix & transform)
+{
+ //Debug("Parsing transform %s", s.c_str());
+ string token;
+ string command;
+ unsigned i = 0;
+
+ while (i < s.size())
+ {
+ GetToken(s, command, i);
+ if (command == "," || command == "" || command == ":")
+ {
+ if (i < s.size())
+ GetToken(s, command, i);
+ else
+ return;
+ }
+ //Debug("Token is \"%s\"", command.c_str());
+
+ SVGMatrix delta = {1,0,0,0,1,0};
+
+
+ assert(GetToken(s,token, i) == "(");
+ if (command == "translate")
+ {
+ GetXYPair(s, delta.e, delta.f, i);
+ assert(GetToken(s,token, i) == ")");
+ }
+ else if (command == "matrix")
+ {
+ GetXYPair(s, delta.a, delta.b,i);
+ GetXYPair(s, delta.c, delta.d,i);
+ GetXYPair(s, delta.e, delta.f,i);
+ assert(GetToken(s,token, i) == ")");
+ }
+ else if (command == "scale")
+ {
+ delta.a = RealFromStr(GetToken(s,token,i));
+ GetToken(s, token, i);
+ if (token == ",")
+ {
+ delta.d = RealFromStr(GetToken(s,token,i));
+ assert(GetToken(s, token, i) == ")");
+ }
+ else
+ {
+ delta.d = delta.a;
+ assert(token == ")");
+ }
+
+ }
+ else
+ {
+ Warn("Unrecognised transform \"%s\", using identity", command.c_str());
+ }
+
+ //Debug("Old transform is {%f,%f,%f,%f,%f,%f}", transform.a, transform.b, transform.c, transform.d,transform.e,transform.f);
+ //Debug("Delta transform is {%f,%f,%f,%f,%f,%f}", delta.a, delta.b, delta.c, delta.d,delta.e,delta.f);
+
+ SVGMatrix old(transform);
+ transform.a = old.a * delta.a + old.c * delta.b;
+ transform.c = old.a * delta.c + old.c * delta.d;
+ transform.e = old.a * delta.e + old.c * delta.f + old.e;
+
+ transform.b = old.b * delta.a + old.d * delta.b;
+ transform.d = old.b * delta.c + old.d * delta.d;
+ transform.f = old.b * delta.e + old.d * delta.f + old.f;
+
+ //Debug("New transform is {%f,%f,%f,%f,%f,%f}", transform.a, transform.b, transform.c, transform.d,transform.e,transform.f);
+ }
+}
+
+inline Colour ParseColourString(const string & colour_str)
+{
+ Colour c(0,0,0,0);
+ if (colour_str == "red")
+ c = {255,0,0,255};
+ else if (colour_str == "blue")
+ c = {0,0,255,255};
+ else if (colour_str == "green")
+ c = {0,255,0,255};
+ else if (colour_str == "black")
+ c = {0,0,0,255};
+ else if (colour_str == "white")
+ c = {255,255,255,255};
+ else if (colour_str.size() == 7 && colour_str[0] == '#')
+ {
+ //Debug("Parse colour string: \"%s\"", colour_str.c_str());
+ char comp[3] = {colour_str[1], colour_str[2], '\0'};
+ c.r = strtoul(comp, NULL, 16);
+ comp[0] = colour_str[3]; comp[1] = colour_str[4];
+ c.g = strtoul(comp, NULL, 16);
+ comp[0] = colour_str[5]; comp[1] = colour_str[6];
+ c.b = strtoul(comp, NULL, 16);
+ c.a = 255;
+ //Debug("Colour is: %u, %u, %u, %u", c.r, c.g, c.b, c.a);
+ }
+ return c;
+}
+
+void Document::ParseSVGNode(pugi::xml_node & root, SVGMatrix & parent_transform)
+{
+ //Debug("Parse node <%s>", root.name());
+
+
+ // Centre the SVGs
+ if (strcmp(root.name(),"svg") == 0)
+ {
+ Real ww = RealFromStr(root.attribute("width").as_string());
+ Real hh = RealFromStr(root.attribute("height").as_string());
+ parent_transform.e -= parent_transform.a * ww/Real(2);
+ parent_transform.f -= parent_transform.d * hh/Real(2);
+ }
+
+ for (pugi::xml_node child = root.first_child(); child; child = child.next_sibling())
+ {
+ SVGMatrix transform(parent_transform);
+ pugi::xml_attribute attrib_trans = child.attribute("transform");
+ if (!attrib_trans.empty())
+ {
+ ParseSVGTransform(attrib_trans.as_string(), transform);
+ }
+
+
+
+ if (strcmp(child.name(), "svg") == 0 || strcmp(child.name(),"g") == 0
+ || strcmp(child.name(), "group") == 0)
+ {
+
+ ParseSVGNode(child, transform);
+ continue;
+ }
+ else if (strcmp(child.name(), "path") == 0)
+ {
+ string d = child.attribute("d").as_string();
+ //Debug("Path data attribute is \"%s\"", d.c_str());
+ bool closed = false;
+ pair<unsigned, unsigned> range = ParseSVGPathData(d, transform, closed);
+ if (true && range.first < m_count && range.second < m_count)//(closed)
+ {
+
+ string colour_str("");
+ map<string, string> style;
+ if (child.attribute("style"))
+ {
+ ParseSVGStyleData(child.attribute("style").as_string(), style);
+ }
+
+ // Determine shading colour
+ if (child.attribute("fill"))
+ {
+ colour_str = child.attribute("fill").as_string();
+ }
+ else if (style.find("fill") != style.end())
+ {
+ colour_str = style["fill"];
+ }
+ Colour fill = ParseColourString(colour_str);
+ Colour stroke = fill;
+
+ if (child.attribute("stroke"))
+ {
+ colour_str = child.attribute("stroke").as_string();
+ stroke = ParseColourString(colour_str);
+ }
+ else if (style.find("stroke") != style.end())
+ {
+ colour_str = style["stroke"];
+ stroke = ParseColourString(colour_str);
+ }
+
+
+ // Determin shading alpha
+ if (child.attribute("fill-opacity"))
+ {
+ fill.a = 255*child.attribute("fill-opacity").as_float();
+ }
+ else if (style.find("fill-opacity") != style.end())
+ {
+ fill.a = 255*strtod(style["fill-opacity"].c_str(), NULL);
+ }
+ if (child.attribute("stroke-opacity"))
+ {
+ stroke.a = 255*child.attribute("stroke-opacity").as_float();
+ }
+ else if (style.find("stroke-opacity") != style.end())
+ {
+ stroke.a = 255*strtod(style["stroke-opacity"].c_str(), NULL);
+ }
+ AddPath(range.first, range.second, fill, stroke);
+ }
+
+ }
+ else if (strcmp(child.name(), "line") == 0)
+ {
+ Real x0(child.attribute("x1").as_float());
+ Real y0(child.attribute("y1").as_float());
+ Real x1(child.attribute("x2").as_float());
+ Real y1(child.attribute("y2").as_float());
+ TransformXYPair(x0,y0,transform);
+ TransformXYPair(x1,y1,transform);
+ AddBezier(Bezier(x0,y0,x1,y1,x1,y1,x1,y1));
+ }
+ else if (strcmp(child.name(), "rect") == 0)
+ {
+ Real coords[4];
+ const char * attrib_names[] = {"x", "y", "width", "height"};
+ for (size_t i = 0; i < 4; ++i)
+ coords[i] = child.attribute(attrib_names[i]).as_float();
+
+ Real x2(coords[0]+coords[2]);
+ Real y2(coords[1]+coords[3]);
+ TransformXYPair(coords[0],coords[1],transform); // x, y, transform
+ TransformXYPair(x2,y2,transform);
+ coords[2] = x2 - coords[0];
+ coords[3] = y2 - coords[1];
+
+ bool outline = !(child.attribute("fill") && strcmp(child.attribute("fill").as_string(),"none") != 0);
+ Add(outline?RECT_OUTLINE:RECT_FILLED, Rect(coords[0], coords[1], coords[2], coords[3]),0);
+ }
+ else if (strcmp(child.name(), "circle") == 0)
+ {
+ Real cx = child.attribute("cx").as_float();
+ Real cy = child.attribute("cy").as_float();
+ Real r = child.attribute("r").as_float();
+
+ Real x = (cx - r);
+ Real y = (cy - r);
+ TransformXYPair(x,y,transform);
+ Real w = Real(2)*r*transform.a; // width scales
+ Real h = Real(2)*r*transform.d; // height scales
+
+
+ Rect rect(x,y,w,h);
+ Add(CIRCLE_FILLED, rect,0);
+ Debug("Added Circle %s", rect.Str().c_str());
+ }
+ else if (strcmp(child.name(), "text") == 0)
+ {
+ Real x = child.attribute("x").as_float();
+ Real y = child.attribute("y").as_float();
+ TransformXYPair(x,y,transform);
+ Debug("Add text \"%s\"", child.child_value());
+ AddText(child.child_value(), 0.05, x, y);
+ }
+ }
+}
+
+void Document::ParseSVGStyleData(const string & style, map<string, string> & results)
+{
+ unsigned i = 0;
+ string key;
+ string value;
+ while (i < style.size() && GetKeyValuePair(style, key, value, i))
+ {
+ results[key] = value;
+ }
+}
+
+/**
+ * Parse an SVG string into a rectangle
+ */
+void Document::ParseSVG(const string & input, const Rect & bounds)
+{
+ using namespace pugi;
+
+ xml_document doc_xml;
+ xml_parse_result result = doc_xml.load(input.c_str());
+
+ if (!result)
+ Error("Couldn't parse SVG input - %s", result.description());
+
+ Debug("Loaded XML - %s", result.description());
+ SVGMatrix transform = {bounds.w, 0,bounds.x, 0,bounds.h,bounds.y};
+ ParseSVGNode(doc_xml, transform);
+}
+
+/**
+ * Load an SVG into a rectangle
+ */
+void Document::LoadSVG(const string & filename, const Rect & bounds)
+{
+ using namespace pugi;
+
+ xml_document doc_xml;
+ ifstream input(filename.c_str(), ios_base::in);
+ xml_parse_result result = doc_xml.load(input);
+
+ if (!result)
+ Error("Couldn't load \"%s\" - %s", filename.c_str(), result.description());
+
+ Debug("Loaded XML from \"%s\" - %s", filename.c_str(), result.description());
+
+ input.close();
+ // a c e, b d f
+ SVGMatrix transform = {bounds.w,0 ,bounds.x, 0,bounds.h,bounds.y};
+ ParseSVGNode(doc_xml, transform);