Skip to content

Commit

Permalink
#72 Possible attribute formatter implementation (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
fcooper8472 authored and martinjrobins committed Jul 17, 2018
1 parent db2c8e5 commit 8d7cc29
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/backend/BackendSVG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,15 @@ void BackendSVG::rect(const bfloat2_t &x, const float r) noexcept {
const auto &delta = x.delta();
vfloat2_t min = x.min();

m_out << "<rect ";

// Position, width and height
m_out << "<rect x=\"" << min[0] << "\" y=\"" << min[1] << "\" width=\""
<< delta[0] << "\" height=\"" << delta[1] << "\" ";
m_out << m_att("x", min[0]) << m_att("y", min[1]) << m_att("width", delta[0])
<< m_att("height", delta[1]);

// Rounding corners
if (r > 0.f) {
m_out << "rx=\"" << r << "\" ry=\"" << r << "\" ";
m_out << m_att("rx", r) << m_att("ry", r);
}

// Styling
Expand All @@ -115,8 +117,8 @@ void BackendSVG::rect(const bfloat2_t &x, const float r) noexcept {

void BackendSVG::circle_begin(const vfloat2_t &centre, const float r) noexcept {

m_out << "<circle cx=\"" << centre[0] << "\" cy=\"" << centre[1] << "\" r=\""
<< r << "\" ";
m_out << "<circle ";
m_out << m_att("cx", centre[0]) << m_att("cy", centre[1]) << m_att("r", r);

// Styling
m_out << m_fill_color << ' ' << m_line_color << ' ' << m_linewidth;
Expand All @@ -133,7 +135,6 @@ void BackendSVG::circle_begin(const vfloat2_t &centre, const float r) noexcept {

void BackendSVG::circle_end() noexcept { m_out << "</circle>\n"; }


void BackendSVG::circle(const vfloat2_t &centre, float r) noexcept {
circle_begin(centre, r);
circle_end();
Expand Down
25 changes: 25 additions & 0 deletions src/backend/BackendSVG.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,34 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "util/Exception.hpp"
#include "util/Vector.hpp"

#include <iomanip>
#include <ostream>
#include <sstream>

namespace trase {

/// Helper class for BackendSVG to format attributes as 'name="val" '
class AttributeFormatter {
std::stringstream m_ss;
int m_precision = 4;

public:
AttributeFormatter() { m_ss << std::setprecision(m_precision); }

/// Format as 'name="val" ' (inc quotes and space), and return a string
///
/// @tparam T type of val
/// @param name name of attribute
/// @param val value of attribute
/// @return string of 'name="val" '
template <typename T>
std::string operator()(const std::string &name, const T val) {
m_ss.str("");
m_ss << name << "=\"" << val << "\" ";
return m_ss.str();
}
};

class BackendSVG {
std::ostream &m_out;
std::string m_linewidth;
Expand All @@ -67,6 +91,7 @@ class BackendSVG {
std::string m_font_size_base;
std::string m_font_face_base;
TransformMatrix m_transform;
AttributeFormatter m_att;

/// Add the opening circle tag to m_out
/// @param centre coordinates of the centre of the circle
Expand Down

0 comments on commit 8d7cc29

Please sign in to comment.