+
+/**
+ * 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);
+}
+
+
+
+// Fear the wrath of the tokenizing svg data
+// Seriously this isn't really very DOM-like at all is it?
+pair<unsigned, unsigned> Document::ParseSVGPathData(const string & d, const SVGMatrix & transform, bool & closed)
+{
+ closed = false;
+ Real x[4] = {0,0,0,0};
+ Real y[4] = {0,0,0,0};
+
+ string token("");
+ string command("m");
+
+ Real x0(0);
+ Real y0(0);
+
+ unsigned i = 0;
+ unsigned prev_i = 0;
+
+ bool start = false;
+
+
+ static string delims("()[],{}<>;:=LlHhVvmMqQzZcC");
+
+ pair<unsigned, unsigned> range(m_count, m_count);
+
+ while (i < d.size() && GetToken(d, token, i, delims).size() > 0)
+ {
+ if (isalpha(token[0]))
+ command = token;
+ else
+ {
+ i = prev_i; // hax
+ if(command == "")
+ command = "L";
+ }
+
+ bool relative = islower(command[0]);
+
+ if (command == "m" || command == "M")
+ {
+ //Debug("Construct moveto command");
+ Real dx = RealFromStr(GetToken(d,token,i,delims));
+ assert(GetToken(d,token,i,delims) == ",");
+ Real dy = RealFromStr(GetToken(d,token,i,delims));
+
+ x[0] = (relative) ? x[0] + dx : dx;
+ y[0] = (relative) ? y[0] + dy : dy;
+
+ x0 = x[0];
+ y0 = y[0];
+ //Debug("mmoveto %f,%f", Float(x[0]),Float(y[0]));
+ command = (command == "m") ? "l" : "L";
+ }
+ else if (command == "c" || command == "C" || command == "q" || command == "Q")
+ {
+ //Debug("Construct curveto command");
+ Real dx = RealFromStr(GetToken(d,token,i,delims));
+ assert(GetToken(d,token,i,delims) == ",");
+ Real dy = RealFromStr(GetToken(d,token,i,delims));
+
+ x[1] = (relative) ? x[0] + dx : dx;
+ y[1] = (relative) ? y[0] + dy : dy;
+
+ dx = RealFromStr(GetToken(d,token,i,delims));
+ assert(GetToken(d,token,i,delims) == ",");
+ dy = RealFromStr(GetToken(d,token,i,delims));
+
+ x[2] = (relative) ? x[0] + dx : dx;
+ y[2] = (relative) ? y[0] + dy : dy;
+
+ if (command != "q" && command != "Q")
+ {
+ dx = RealFromStr(GetToken(d,token,i,delims));
+ assert(GetToken(d,token,i,delims) == ",");
+ dy = RealFromStr(GetToken(d,token,i,delims));
+ x[3] = (relative) ? x[0] + dx : dx;
+ y[3] = (relative) ? y[0] + dy : dy;
+ }
+ else
+ {
+ x[3] = x[2];
+ y[3] = y[2];
+ Real old_x1(x[1]), old_y1(y[1]);
+ x[1] = x[0] + Real(2) * (old_x1 - x[0])/ Real(3);
+ y[1] = y[0] + Real(2) * (old_y1 - y[0])/ Real(3);
+ x[2] = x[3] + Real(2) * (old_x1 - x[3])/ Real(3);
+ y[2] = y[3] + Real(2) * (old_y1 - y[3])/ Real(3);
+ }
+
+ Real x3(x[3]);
+ Real y3(y[3]);
+ for (int j = 0; j < 4; ++j)
+ TransformXYPair(x[j],y[j], transform);
+
+ range.second = AddBezier(Bezier(x[0],y[0],x[1],y[1],x[2],y[2],x[3],y[3]));
+
+ //Debug("[%u] curveto %f,%f %f,%f %f,%f", index, Float(x[1]),Float(y[1]),Float(x[2]),Float(y[2]),Float(x[3]),Float(y[3]));
+
+ x[0] = x3;
+ y[0] = y3;
+
+
+ }
+ else if (command == "l" || command == "L" || command == "h" || command == "H" || command == "v" || command == "V")
+ {
+ //Debug("Construct lineto command, relative %d", relative);
+
+ Real dx = RealFromStr(GetToken(d,token,i,delims));
+ Real dy = 0;
+ if (command == "l" || command == "L")
+ {
+ assert(GetToken(d,token,i,delims) == ",");
+ dy = RealFromStr(GetToken(d,token,i,delims));
+ }
+ else if (command == "v" || command == "V")
+ {
+ swap(dx,dy);
+ }
+
+ x[1] = (relative) ? x[0] + dx : dx;
+ y[1] = (relative) ? y[0] + dy : dy;
+ if (command == "v" || command == "V")
+ {
+ x[1] = x[0];
+ }
+ else if (command == "h" || command == "H")
+ {
+ y[1] = y[0];
+ }
+
+ Real x1(x[1]);
+ Real y1(y[1]);
+
+ TransformXYPair(x[0],y[0],transform);
+ TransformXYPair(x[1],y[1],transform);
+
+
+ range.second = AddBezier(Bezier(x[0],y[0],x[1],y[1],x[1],y[1],x[1],y[1]));
+
+ //Debug("[%u] lineto %f,%f %f,%f", index, Float(x[0]),Float(y[0]),Float(x[1]),Float(y[1]));
+
+ x[0] = x1;
+ y[0] = y1;
+
+ }
+ else if (command == "z" || command == "Z")
+ {
+ //Debug("Construct returnto command");
+ x[1] = x0;
+ y[1] = y0;
+ x[2] = x0;
+ y[2] = y0;
+ x[3] = x0;
+ y[3] = y0;
+
+ Real x3(x[3]);
+ Real y3(y[3]);
+ for (int j = 0; j < 4; ++j)
+ TransformXYPair(x[j],y[j], transform);
+
+ range.second = AddBezier(Bezier(x[0],y[0],x[1],y[1],x[2],y[2],x[3],y[3]));
+ //Debug("[%u] returnto %f,%f %f,%f", index, Float(x[0]),Float(y[0]),Float(x[1]),Float(y[1]));
+
+ x[0] = x3;
+ y[0] = y3;
+ command = "m";
+ closed = true;
+ }
+ else
+ {
+ Warn("Unrecognised command \"%s\", set to \"m\"", command.c_str());
+ command = "m";
+ }
+
+ if (!start)
+ {
+ x0 = x[0];
+ y0 = y[0];
+ start = true;
+ }
+ prev_i = i;
+ }
+ return range;
+}
+
+void Document::SetFont(const string & font_filename)
+{
+ if (m_font_data != NULL)
+ {
+ free(m_font_data);
+ }
+
+ FILE *font_file = fopen(font_filename.c_str(), "rb");
+ fseek(font_file, 0, SEEK_END);
+ size_t font_file_size = ftell(font_file);
+ fseek(font_file, 0, SEEK_SET);
+ m_font_data = (unsigned char*)malloc(font_file_size);
+ size_t read = fread(m_font_data, 1, font_file_size, font_file);
+ if (read != font_file_size)
+ {
+ Fatal("Failed to read font data from \"%s\" - Read %u bytes expected %u - %s", font_filename.c_str(), read, font_file_size, strerror(errno));
+ }
+ fclose(font_file);
+ stbtt_InitFont(&m_font, m_font_data, 0);
+}
+
+void Document::AddText(const string & text, Real scale, Real x, Real y)
+{
+ if (m_font_data == NULL)
+ {
+ Warn("No font loaded");
+ return;
+ }
+
+ Real x0(x);
+ //Real y0(y);
+ int ascent = 0, descent = 0, line_gap = 0;
+ stbtt_GetFontVMetrics(&m_font, &ascent, &descent, &line_gap);
+ Real font_scale = scale;
+ font_scale /= Real(ascent - descent);
+ Real y_advance = Real(font_scale) * Real(ascent - descent + line_gap);
+ for (unsigned i = 0; i < text.size(); ++i)
+ {
+ if (text[i] == '\n')
+ {
+ y += y_advance;
+ x = x0;
+ }
+ if (!isprint(text[i]))
+ continue;
+
+ int advance_width = 0, left_side_bearing = 0, kerning = 0;
+ stbtt_GetCodepointHMetrics(&m_font, text[i], &advance_width, &left_side_bearing);
+ if (i >= 1)
+ {
+ kerning = stbtt_GetCodepointKernAdvance(&m_font, text[i-1], text[i]);
+ }
+ x += font_scale * Real(kerning);
+ AddFontGlyphAtPoint(&m_font, text[i], font_scale, x, y);
+ x += font_scale * Real(advance_width);
+ }
+}
+
+void Document::AddFontGlyphAtPoint(stbtt_fontinfo *font, int character, Real scale, Real x, Real y)
+{
+ int glyph_index = stbtt_FindGlyphIndex(font, character);
+
+ // Check if there is actully a glyph to render.
+ if (stbtt_IsGlyphEmpty(font, glyph_index))
+ {
+ return;
+ }
+
+ stbtt_vertex *instructions;
+ int num_instructions = stbtt_GetGlyphShape(font, glyph_index, &instructions);
+
+ Real current_x(0), current_y(0);
+ unsigned start_index = m_count;
+ unsigned end_index = m_count;
+ for (int i = 0; i < num_instructions; ++i)
+ {
+ // TTF uses 16-bit signed ints for coordinates:
+ // with the y-axis inverted compared to us.
+ // Convert and scale any data.
+ Real inst_x = Real(instructions[i].x)*scale;
+ Real inst_y = Real(instructions[i].y)*-scale;
+ Real inst_cx = Real(instructions[i].cx)*scale;
+ Real inst_cy = Real(instructions[i].cy)*-scale;
+ Real old_x(current_x), old_y(current_y);
+ current_x = inst_x;
+ current_y = inst_y;
+
+ switch(instructions[i].type)
+ {
+ // Move To
+ case STBTT_vmove:
+ break;
+ // Line To
+ case STBTT_vline:
+ end_index = AddBezier(Bezier(old_x + x, old_y + y, old_x + x, old_y + y, current_x + x, current_y + y, current_x + x, current_y + y));
+ break;
+ // Quadratic Bezier To:
+ case STBTT_vcurve:
+ // Quadratic -> Cubic:
+ // - Endpoints are the same.
+ // - cubic1 = quad0+(2/3)*(quad1-quad0)
+ // - cubic2 = quad2+(2/3)*(quad1-quad2)
+ end_index = AddBezier(Bezier(old_x + x, old_y + y, old_x + Real(2)*(inst_cx-old_x)/Real(3) + x, old_y + Real(2)*(inst_cy-old_y)/Real(3) + y,
+ current_x + Real(2)*(inst_cx-current_x)/Real(3) + x, current_y + Real(2)*(inst_cy-current_y)/Real(3) + y, current_x + x, current_y + y));
+ break;
+ }
+ }
+
+ if (start_index < m_count && end_index < m_count)
+ {
+ AddPath(start_index, end_index);
+ }
+ //Debug("Added Glyph \"%c\" at %f %f, scale %f", (char)character, Float(x), Float(y), Float(scale));
+
+ stbtt_FreeShape(font, instructions);
+}
+
+void Document::TransformObjectBounds(const SVGMatrix & transform, ObjectType type)
+{
+ #ifdef TRANSFORM_BEZIERS_TO_PATH
+ for (unsigned i = 0; i < m_objects.paths.size(); ++i)
+ {
+ Path & p = m_objects.paths[i];
+ p.m_bounds.x = transform.a * p.m_bounds.x + transform.e;
+ p.m_bounds.y = transform.d * p.m_bounds.y + transform.f;
+ p.m_bounds.w *= transform.a;
+ p.m_bounds.h *= transform.d;
+ }
+ return;
+ #endif
+
+ for (unsigned i = 0; i < m_count; ++i)
+ {
+ if (type == NUMBER_OF_OBJECT_TYPES || m_objects.types[i] == type)
+ {
+ TransformXYPair(m_objects.bounds[i].x, m_objects.bounds[i].y, transform);
+ m_objects.bounds[i].w *= transform.a;
+ m_objects.bounds[i].h *= transform.d;
+ }
+ }
+}
+
+void Document::TranslateObjects(const Real & dx, const Real & dy, ObjectType type)
+{
+ #ifdef TRANSFORM_BEZIERS_TO_PATH
+ for (unsigned i = 0; i < m_objects.paths.size(); ++i)
+ {
+ Path & p = m_objects.paths[i];
+ p.m_bounds.x += dx;
+ p.m_bounds.y += dy;
+ }
+ return;
+ #endif
+
+ for (unsigned i = 0; i < m_count; ++i)
+ {
+ if (type == NUMBER_OF_OBJECT_TYPES || m_objects.types[i] == type)
+ {
+ m_objects.bounds[i].x += dx;
+ m_objects.bounds[i].y += dy;
+ }
+ }
+}
+
+void Document::ScaleObjectsAboutPoint(const Real & x, const Real & y, const Real & scale_amount, ObjectType type)
+{
+ #ifdef TRANSFORM_BEZIERS_TO_PATH
+ for (unsigned i = 0; i < m_objects.paths.size(); ++i)
+ {
+ Path & p = m_objects.paths[i];
+ p.m_bounds.w /= scale_amount;
+ p.m_bounds.h /= scale_amount;
+ p.m_bounds.x -= x;
+ p.m_bounds.x /= scale_amount;
+ p.m_bounds.x += x;
+ p.m_bounds.y -= y;
+ p.m_bounds.y /= scale_amount;
+ p.m_bounds.y += y;
+ }
+ return;
+ #endif
+
+ for (unsigned i = 0; i < m_count; ++i)
+ {
+ if (type != NUMBER_OF_OBJECT_TYPES && m_objects.types[i] != type)
+ continue;
+
+ m_objects.bounds[i].w /= scale_amount;
+ m_objects.bounds[i].h /= scale_amount;
+ //m_objects.bounds[i].x = x + (m_objects.bounds[i].x-x)/scale_amount;
+ //m_objects.bounds[i].y = y + (m_objects.bounds[i].y-x)/scale_amount;
+ m_objects.bounds[i].x -= x;
+ m_objects.bounds[i].x /= scale_amount;
+ m_objects.bounds[i].x += x;
+
+ m_objects.bounds[i].y -= y;
+ m_objects.bounds[i].y /= scale_amount;
+ m_objects.bounds[i].y += y;
+ }
+
+}
+
+