Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added cap and join style to Stroke #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions main_1.0.0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
20 changes: 18 additions & 2 deletions simple_svg_1.0.0.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Copy link
Owner

@adishavit adishavit Oct 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This library is quite old, and may be used with older compilers.
I'm not sure enum class is really required here - that would require C++11 at least.
Perhaps a regular enum is sufficient.

Also, IIUC the old behaviour did not specify the stroke-linecap attribute at all (so e.g. something else like a CSS may have set some default externally). I suggest adding a new enum value of NotSpecified as the default value such that when that is set the attribute will not get written at all. See more below.

enum class Join { Miter, Round, Bevel };
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above.

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)
{ }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

capStyle and joinStyle always initialized with some enum value and thus... (cont. below)

std::string toString(Layout const & layout) const
{
// If stroke width is invalid.
Expand All @@ -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");

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... they always have a value here so without the NotSpecified value, the stroke-line* attribute will always be written (unlike old the behavior).
Also, you should use a switch statement here for clarity.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, an enum class is c++11, I didn't think of that. I had used that to mitigate the duplication in Round. Now all the enum values have the type as prefix, e.g. JoinRound and CapRound. The switch also looks much cleaner, thanks for that suggestion. I was unsure if I should add separate cases for Cap/JoinUndefined and a throw for the default case, but then decided against it. If you wish otherwise, I can add that too.

return ss.str();
}
private:
double width;
Color color;
bool nonScaling;
Cap capStyle;
Join joinStyle;
};

class Font : public Serializeable
Expand Down