|
@ -1,5 +1,11 @@ |
|
|
#include "svg.h" |
|
|
#include "svg.h" |
|
|
|
|
|
|
|
|
|
|
|
inline std::string float_to_string(float data) { |
|
|
|
|
|
auto str = std::to_string(data); |
|
|
|
|
|
return str.substr(0, str.find('.') + 3); // format -> xxx.__
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// SvgGraph interface
|
|
|
void SvgGraph::insert(SvgObject *obj) { |
|
|
void SvgGraph::insert(SvgObject *obj) { |
|
|
objects.emplace_back(obj); |
|
|
objects.emplace_back(obj); |
|
|
} |
|
|
} |
|
@ -10,27 +16,40 @@ SvgGraph::~SvgGraph() { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
std::string SvgLine::dump() const { |
|
|
std::string SvgGraph::dump() const { |
|
|
|
|
|
std::string xml = R"(<svg xmlns="http://www.w3.org/2000/svg" version="1.1")";
|
|
|
|
|
|
xml += " width=\"" + std::to_string(width) + "\""; |
|
|
|
|
|
xml += " height=\"" + std::to_string(height) + "\">\n"; |
|
|
|
|
|
for (const auto *object : objects) { |
|
|
|
|
|
xml += " " + object->dump() + "\n"; |
|
|
|
|
|
} |
|
|
|
|
|
return xml + "</svg>\n"; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// SvgLine render as XML content
|
|
|
|
|
|
std::string SvgLine::dump() const { |
|
|
/// basic attributes of svg-line
|
|
|
/// basic attributes of svg-line
|
|
|
|
|
|
|
|
|
std::string xml = "<line "; |
|
|
std::string xml = "<line "; |
|
|
xml += "x1=\"" + std::to_string(start.x) + "\" "; |
|
|
xml += "x1=\"" + std::to_string(start.x) + "\" "; |
|
|
xml += "y1=\"" + std::to_string(start.y) + "\" "; |
|
|
xml += "y1=\"" + std::to_string(start.y) + "\" "; |
|
|
xml += "x2=\"" + std::to_string(end.x) + "\" "; |
|
|
xml += "x2=\"" + std::to_string(end.x) + "\" "; |
|
|
xml += "y2=\"" + std::to_string(end.y) + "\" "; |
|
|
xml += "y2=\"" + std::to_string(end.y) + "\" "; |
|
|
|
|
|
if (!dasharray.empty()) { |
|
|
|
|
|
xml += "stroke-dasharray=\"" + dasharray + "\" "; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
std::string style = "stroke:#636379;stroke-width:1;"; |
|
|
/// style attribute of svg-line
|
|
|
|
|
|
std::string style = "stroke-width:" + float_to_string(width) + ";"; |
|
|
// <line x1="0" y1="0" x2="200" y2="200" style="stroke:rgb(255,0,0);stroke-width:2"/>
|
|
|
if (!color.empty()) { |
|
|
|
|
|
style += "stroke:" + color + ";"; |
|
|
|
|
|
} |
|
|
// TODO: svg-line css style
|
|
|
if (opacity != 0) { |
|
|
|
|
|
style += "stroke-opacity:" + float_to_string(opacity) + ";"; |
|
|
|
|
|
} |
|
|
return xml + "style=\"" + style + "\" />"; |
|
|
return xml + "style=\"" + style + "\" />"; |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// SvgRect render as XML content
|
|
|
std::string SvgRect::dump() const { |
|
|
std::string SvgRect::dump() const { |
|
|
/// basic attributes of svg-rect
|
|
|
/// basic attributes of svg-rect
|
|
|
std::string xml = "<rect "; |
|
|
std::string xml = "<rect "; |
|
@ -42,7 +61,7 @@ std::string SvgRect::dump() const { |
|
|
xml += "rx=\"" + std::to_string(radius) + "\" "; |
|
|
xml += "rx=\"" + std::to_string(radius) + "\" "; |
|
|
} |
|
|
} |
|
|
/// style attribute of svg-rect
|
|
|
/// style attribute of svg-rect
|
|
|
std::string style = "stroke-width:" + std::to_string(stroke).substr(0, 3) + ";"; |
|
|
std::string style = "stroke-width:" + float_to_string(stroke) + ";"; |
|
|
if (!color.empty()) { |
|
|
if (!color.empty()) { |
|
|
style += "fill:" + color + ";"; |
|
|
style += "fill:" + color + ";"; |
|
|
} |
|
|
} |
|
@ -50,20 +69,10 @@ std::string SvgRect::dump() const { |
|
|
style += "stroke:" + line_color + ";"; |
|
|
style += "stroke:" + line_color + ";"; |
|
|
} |
|
|
} |
|
|
if (opacity != 0) { |
|
|
if (opacity != 0) { |
|
|
style += "fill-opacity:" + std::to_string(opacity).substr(0, 4) + ";"; |
|
|
style += "fill-opacity:" + float_to_string(opacity) + ";"; |
|
|
} |
|
|
} |
|
|
if (line_opacity != 0) { |
|
|
if (line_opacity != 0) { |
|
|
style += "stroke-opacity:" + std::to_string(line_opacity).substr(0, 4) + ";"; |
|
|
style += "stroke-opacity:" + float_to_string(line_opacity) + ";"; |
|
|
} |
|
|
} |
|
|
return xml + "style=\"" + style + "\" />"; |
|
|
return xml + "style=\"" + style + "\" />"; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
std::string SvgGraph::dump() const { |
|
|
|
|
|
std::string xml = R"(<svg xmlns="http://www.w3.org/2000/svg" version="1.1")";
|
|
|
|
|
|
xml += " width=\"" + std::to_string(width) + "\""; |
|
|
|
|
|
xml += " height=\"" + std::to_string(height) + "\">\n"; |
|
|
|
|
|
for (const auto *object : objects) { |
|
|
|
|
|
xml += " " + object->dump() + "\n"; |
|
|
|
|
|
} |
|
|
|
|
|
return xml + "</svg>\n"; |
|
|
|
|
|
} |
|
|
|
|
|