From 44e2288f3463d837385c4d4573fd4e40da8fe426 Mon Sep 17 00:00:00 2001 From: Hanno Dietrich Date: Wed, 13 Oct 2021 22:19:55 +0200 Subject: [PATCH 1/2] added cap and join style to Stroke --- main_1.0.0.cpp | 5 +++++ simple_svg_1.0.0.hpp | 20 ++++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/main_1.0.0.cpp b/main_1.0.0.cpp index 8d642d5..7597d90 100644 --- a/main_1.0.0.cpp +++ b/main_1.0.0.cpp @@ -76,5 +76,10 @@ int main() doc << Rectangle(Point(70, 55), 20, 15, Color::Yellow); + Path path(Fill(), Stroke(5, Color::Blue, false, Stroke::Cap::Round, Stroke::Join::Round)); + path << Point(0,0) << Point (10,0) << Point(10,5); + path.offset(Point(50,60)); + doc << path; + doc.save(); } diff --git a/simple_svg_1.0.0.hpp b/simple_svg_1.0.0.hpp index 864b75f..3b58cd7 100644 --- a/simple_svg_1.0.0.hpp +++ b/simple_svg_1.0.0.hpp @@ -247,8 +247,13 @@ namespace svg class Stroke : public Serializeable { public: - Stroke(double width = -1, Color color = Color::Transparent, bool nonScalingStroke = false) - : width(width), color(color), nonScaling(nonScalingStroke) { } + enum class Cap { Butt, Round, Square }; + enum class Join { Miter, Round, Bevel }; + Stroke(double width = -1, Color color = Color::Transparent, bool nonScalingStroke = false, + Cap capStyle = Cap::Butt, Join joinStyle = Join::Miter) + : width(width), color(color), nonScaling(nonScalingStroke), + capStyle(capStyle), joinStyle(joinStyle) + { } std::string toString(Layout const & layout) const { // If stroke width is invalid. @@ -259,12 +264,23 @@ namespace svg ss << attribute("stroke-width", translateScale(width, layout)) << attribute("stroke", color.toString(layout)); if (nonScaling) ss << attribute("vector-effect", "non-scaling-stroke"); + + if(capStyle == Cap::Butt) ss << attribute("stroke-linecap", "butt"); + else if(capStyle == Cap::Round) ss << attribute("stroke-linecap", "round"); + else if(capStyle == Cap::Square) ss << attribute("stroke-linecap", "square"); + + if(joinStyle == Join::Miter) ss << attribute("stroke-linejoin", "miter"); + else if(joinStyle == Join::Round) ss << attribute("stroke-linejoin", "round"); + else if(joinStyle == Join::Bevel) ss << attribute("stroke-linejoin", "bevel"); + return ss.str(); } private: double width; Color color; bool nonScaling; + Cap capStyle; + Join joinStyle; }; class Font : public Serializeable From 7e2b4ea49337c341d3201366bd5ad94055c37a52 Mon Sep 17 00:00:00 2001 From: Hanno Dietrich Date: Sun, 17 Oct 2021 07:16:28 +0200 Subject: [PATCH 2/2] using enum instead of enum class for Cap and Join style --- main_1.0.0.cpp | 2 +- simple_svg_1.0.0.hpp | 26 ++++++++++++++++---------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/main_1.0.0.cpp b/main_1.0.0.cpp index 7597d90..d3ba62b 100644 --- a/main_1.0.0.cpp +++ b/main_1.0.0.cpp @@ -76,7 +76,7 @@ int main() doc << Rectangle(Point(70, 55), 20, 15, Color::Yellow); - Path path(Fill(), Stroke(5, Color::Blue, false, Stroke::Cap::Round, Stroke::Join::Round)); + Path path(Fill(), Stroke(5, Color::Blue, false, Stroke::CapRound, Stroke::JoinRound)); path << Point(0,0) << Point (10,0) << Point(10,5); path.offset(Point(50,60)); doc << path; diff --git a/simple_svg_1.0.0.hpp b/simple_svg_1.0.0.hpp index 3b58cd7..aa1e5b6 100644 --- a/simple_svg_1.0.0.hpp +++ b/simple_svg_1.0.0.hpp @@ -247,10 +247,10 @@ namespace svg class Stroke : public Serializeable { public: - enum class Cap { Butt, Round, Square }; - enum class Join { Miter, Round, Bevel }; + enum Cap { CapUndefined, CapButt, CapRound, CapSquare }; + enum Join { JoinUndefined, JoinMiter, JoinRound, JoinBevel }; Stroke(double width = -1, Color color = Color::Transparent, bool nonScalingStroke = false, - Cap capStyle = Cap::Butt, Join joinStyle = Join::Miter) + Cap capStyle = CapUndefined, Join joinStyle = JoinUndefined) : width(width), color(color), nonScaling(nonScalingStroke), capStyle(capStyle), joinStyle(joinStyle) { } @@ -265,13 +265,19 @@ namespace svg if (nonScaling) ss << attribute("vector-effect", "non-scaling-stroke"); - if(capStyle == Cap::Butt) ss << attribute("stroke-linecap", "butt"); - else if(capStyle == Cap::Round) ss << attribute("stroke-linecap", "round"); - else if(capStyle == Cap::Square) ss << attribute("stroke-linecap", "square"); - - if(joinStyle == Join::Miter) ss << attribute("stroke-linejoin", "miter"); - else if(joinStyle == Join::Round) ss << attribute("stroke-linejoin", "round"); - else if(joinStyle == Join::Bevel) ss << attribute("stroke-linejoin", "bevel"); + switch(capStyle) { + case CapButt : ss << attribute("stroke-linecap", "butt"); break; + case CapRound : ss << attribute("stroke-linecap", "round"); break; + case CapSquare : ss << attribute("stroke-linecap", "square"); break; + default : break; + } + + switch(capStyle) { + case JoinMiter : ss << attribute("stroke-linejoin", "miter"); break; + case JoinRound : ss << attribute("stroke-linejoin", "round"); break; + case JoinBevel : ss << attribute("stroke-linejoin", "bevel"); break; + default : break; + } return ss.str(); }