// -------------------------------------------------------------- // System Headers // -------------------------------------------------------------- #include #include #include // -------------------------------------------------------------- // Local Headers // -------------------------------------------------------------- #include "svg.h" // -------------------------------------------------------------- // KV - A key/value pair, used by Tag // -------------------------------------------------------------- struct KV { KV(const string &key, const string &value) : mKey(key), mValue(value) { } template static string toString(const T &value) { std::ostringstream out; out << value; return(out.str()); } const string mKey; const string mValue; }; // -------------------------------------------------------------- // Tag - An svg entry such as or // -------------------------------------------------------------- class Tag { public: Tag(const string &name) : mName(name) { } template void addAttribute(const string &key, const T &value) { mKV.push_back(KV(key, KV::toString(value))); } string toString() const { string s; for(KV kv : mKV) s += " " + kv.mKey + "=\"" + kv.mValue + "\""; return("<" + mName + s + "/>"); } private: string mName; vector mKV; }; // -------------------------------------------------------------- // doAttribute(x) expands out to: addAttribute("x", x) // -------------------------------------------------------------- #define doAttribute(x) addAttribute(#x, x) // -------------------------------------------------------------- // rect // -------------------------------------------------------------- void SVG::rect( int x, int y, int width, int height, const string &stroke, const string &fill ) { Tag t("rect"); t.doAttribute(x); t.doAttribute(y); t.doAttribute(width); t.doAttribute(height); t.doAttribute(stroke); t.doAttribute(fill); mCommands.push_back(t.toString()); } // -------------------------------------------------------------- // circle // -------------------------------------------------------------- void SVG::circle( int x, int y, int radius, const string &stroke, const string &fill ) { Tag t("circle"); t.addAttribute("cx", x); t.addAttribute("cy", y); t.addAttribute("r", radius); t.doAttribute(stroke); t.doAttribute(fill); mCommands.push_back(t.toString()); } // -------------------------------------------------------------- // line // -------------------------------------------------------------- void SVG::line( int x1, int y1, int x2, int y2, const string &color ) { Tag t("line"); t.doAttribute(x1); t.doAttribute(x2); t.doAttribute(y1); t.doAttribute(y2); t.addAttribute("stroke", color); mCommands.push_back(t.toString()); } // -------------------------------------------------------------- // border // -------------------------------------------------------------- void SVG::border(const string &color) { rect(0, 0, mWidth, mHeight, color, Colors::none); } // -------------------------------------------------------------- // getWidth // -------------------------------------------------------------- int SVG::getWidth() const { return(mWidth); } // -------------------------------------------------------------- // getHeight // -------------------------------------------------------------- int SVG::getHeight() const { return(mHeight); } // -------------------------------------------------------------- // write // -------------------------------------------------------------- bool SVG::write(const string &fileName, string &errMsg) { std::ostringstream out; out << "\n" " " << fileName << "\n" " \n" " \n" "\n"; for(string s : mCommands) out<< " " << s << "\n"; out << " \n" << " \n" << "\n"; std::ofstream f(fileName); if(!f) { errMsg = "Could not open: [" + fileName + "]"; return(false); } f << out.str(); return(true); }