Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
sammycage committed Jan 6, 2024
1 parent 2e36239 commit 4d0322d
Show file tree
Hide file tree
Showing 30 changed files with 192 additions and 286 deletions.
6 changes: 0 additions & 6 deletions source/clippathelement.cpp
Expand Up @@ -19,7 +19,6 @@ std::unique_ptr<LayoutClipPath> ClipPathElement::getClipper(LayoutContext* conte
{
if(context->hasReference(this))
return nullptr;

LayoutBreaker layoutBreaker(context, this);
auto clipper = makeUnique<LayoutClipPath>();
clipper->units = clipPathUnits();
Expand All @@ -29,9 +28,4 @@ std::unique_ptr<LayoutClipPath> ClipPathElement::getClipper(LayoutContext* conte
return clipper;
}

std::unique_ptr<Node> ClipPathElement::clone() const
{
return cloneElement<ClipPathElement>();
}

} // namespace lunasvg
4 changes: 1 addition & 3 deletions source/clippathelement.h
Expand Up @@ -7,14 +7,12 @@ namespace lunasvg {

class LayoutClipPath;

class ClipPathElement : public GraphicsElement {
class ClipPathElement final : public GraphicsElement {
public:
ClipPathElement();

Units clipPathUnits() const;
std::unique_ptr<LayoutClipPath> getClipper(LayoutContext* context) const;

std::unique_ptr<Node> clone() const;
};

} // namespace lunasvg
Expand Down
5 changes: 0 additions & 5 deletions source/defselement.cpp
Expand Up @@ -7,9 +7,4 @@ DefsElement::DefsElement()
{
}

std::unique_ptr<Node> DefsElement::clone() const
{
return cloneElement<DefsElement>();
}

} // namespace lunasvg
4 changes: 1 addition & 3 deletions source/defselement.h
Expand Up @@ -5,11 +5,9 @@

namespace lunasvg {

class DefsElement : public GraphicsElement {
class DefsElement final : public GraphicsElement {
public:
DefsElement();

std::unique_ptr<Node> clone() const;
};

} // namespace lunasvg
Expand Down
21 changes: 20 additions & 1 deletion source/element.cpp
@@ -1,5 +1,6 @@
#include "element.h"
#include "svgelement.h"
#include "parser.h"

namespace lunasvg {

Expand Down Expand Up @@ -142,7 +143,6 @@ Rect Element::currentViewport() const
auto element = static_cast<SVGElement*>(parent);
if(element->has(PropertyID::ViewBox))
return element->viewBox();

LengthContext lengthContext(element);
auto _x = lengthContext.valueForLength(element->x(), LengthMode::Width);
auto _y = lengthContext.valueForLength(element->y(), LengthMode::Height);
Expand All @@ -154,4 +154,23 @@ Rect Element::currentViewport() const
return parent->currentViewport();
}

void Element::build(const TreeBuilder* builder)
{
for(auto& child : children) {
if(child->isText())
continue;
auto element = static_cast<Element*>(child.get());
element->build(builder);
}
}

std::unique_ptr<Node> Element::clone() const
{
auto element = TreeBuilder::createElement(id);
element->properties = properties;
for(auto& child : children)
element->addChild(child->clone());
return element;
}

} // namespace lunasvg
17 changes: 8 additions & 9 deletions source/element.h
Expand Up @@ -16,6 +16,7 @@ enum class ElementID {
Defs,
Ellipse,
G,
Image,
Line,
LinearGradient,
Marker,
Expand All @@ -31,6 +32,8 @@ enum class ElementID {
Style,
Svg,
Symbol,
Text,
TSpan,
Use
};

Expand Down Expand Up @@ -150,6 +153,8 @@ class TextNode : public Node {

using NodeList = std::list<std::unique_ptr<Node>>;

class TreeBuilder;

class Element : public Node {
public:
Element(ElementID id);
Expand All @@ -165,11 +170,12 @@ class Element : public Node {
void layoutChildren(LayoutContext* context, LayoutContainer* current) const;
Rect currentViewport() const;

virtual void build(const TreeBuilder* builder);

template<typename T>
void transverse(T callback) {
if(!callback(this))
return;

for(auto& child : children) {
if(child->isText()) {
if(!callback(child.get()))
Expand All @@ -182,14 +188,7 @@ class Element : public Node {
}
}

template<typename T>
std::unique_ptr<T> cloneElement() const {
auto element = makeUnique<T>();
element->properties = properties;
for(auto& child : children)
element->addChild(child->clone());
return element;
}
std::unique_ptr<Node> clone() const final;

public:
ElementID id;
Expand Down
6 changes: 0 additions & 6 deletions source/gelement.cpp
Expand Up @@ -12,7 +12,6 @@ void GElement::layout(LayoutContext* context, LayoutContainer* current) const
{
if(isDisplayNone())
return;

auto group = makeUnique<LayoutGroup>();
group->transform = transform();
group->opacity = opacity();
Expand All @@ -22,9 +21,4 @@ void GElement::layout(LayoutContext* context, LayoutContainer* current) const
current->addChildIfNotEmpty(std::move(group));
}

std::unique_ptr<Node> GElement::clone() const
{
return cloneElement<GElement>();
}

} // namespace lunasvg
3 changes: 1 addition & 2 deletions source/gelement.h
Expand Up @@ -5,12 +5,11 @@

namespace lunasvg {

class GElement : public GraphicsElement {
class GElement final : public GraphicsElement {
public:
GElement();

void layout(LayoutContext* context, LayoutContainer* current) const;
std::unique_ptr<Node> clone() const;
};

} // namespace lunasvg
Expand Down
37 changes: 0 additions & 37 deletions source/geometryelement.cpp
Expand Up @@ -2,8 +2,6 @@
#include "parser.h"
#include "layoutcontext.h"

#include <cmath>

namespace lunasvg {

GeometryElement::GeometryElement(ElementID id)
Expand Down Expand Up @@ -50,11 +48,6 @@ Path PathElement::path() const
return d();
}

std::unique_ptr<Node> PathElement::clone() const
{
return cloneElement<PathElement>();
}

PolyElement::PolyElement(ElementID id)
: GeometryElement(id)
{
Expand Down Expand Up @@ -86,11 +79,6 @@ Path PolygonElement::path() const
return path;
}

std::unique_ptr<Node> PolygonElement::clone() const
{
return cloneElement<PolygonElement>();
}

PolylineElement::PolylineElement()
: PolyElement(ElementID::Polyline)
{
Expand All @@ -110,11 +98,6 @@ Path PolylineElement::path() const
return path;
}

std::unique_ptr<Node> PolylineElement::clone() const
{
return cloneElement<PolylineElement>();
}

CircleElement::CircleElement()
: GeometryElement(ElementID::Circle)
{
Expand Down Expand Up @@ -154,11 +137,6 @@ Path CircleElement::path() const
return path;
}

std::unique_ptr<Node> CircleElement::clone() const
{
return cloneElement<CircleElement>();
}

EllipseElement::EllipseElement()
: GeometryElement(ElementID::Ellipse)
{
Expand Down Expand Up @@ -206,11 +184,6 @@ Path EllipseElement::path() const
return path;
}

std::unique_ptr<Node> EllipseElement::clone() const
{
return cloneElement<EllipseElement>();
}

LineElement::LineElement()
: GeometryElement(ElementID::Line)
{
Expand Down Expand Up @@ -254,11 +227,6 @@ Path LineElement::path() const
return path;
}

std::unique_ptr<Node> LineElement::clone() const
{
return cloneElement<LineElement>();
}

RectElement::RectElement()
: GeometryElement(ElementID::Rect)
{
Expand Down Expand Up @@ -327,9 +295,4 @@ Path RectElement::path() const
return path;
}

std::unique_ptr<Node> RectElement::clone() const
{
return cloneElement<RectElement>();
}

} // namespace lunasvg
42 changes: 14 additions & 28 deletions source/geometryelement.h
Expand Up @@ -16,14 +16,12 @@ class GeometryElement : public GraphicsElement {
virtual Path path() const = 0;
};

class PathElement : public GeometryElement {
class PathElement final : public GeometryElement {
public:
PathElement();

Path d() const;
Path path() const;

std::unique_ptr<Node> clone() const;
Path path() const final;
};

class PolyElement : public GeometryElement {
Expand All @@ -33,38 +31,32 @@ class PolyElement : public GeometryElement {
PointList points() const;
};

class PolygonElement : public PolyElement {
class PolygonElement final : public PolyElement {
public:
PolygonElement();

Path path() const;

std::unique_ptr<Node> clone() const;
Path path() const final;
};

class PolylineElement : public PolyElement {
class PolylineElement final : public PolyElement {
public:
PolylineElement();

Path path() const;

std::unique_ptr<Node> clone() const;
Path path() const final;
};

class CircleElement : public GeometryElement {
class CircleElement final : public GeometryElement {
public:
CircleElement();

Length cx() const;
Length cy() const;
Length r() const;

Path path() const;

std::unique_ptr<Node> clone() const;
Path path() const final;
};

class EllipseElement : public GeometryElement {
class EllipseElement final : public GeometryElement {
public:
EllipseElement();

Expand All @@ -73,12 +65,10 @@ class EllipseElement : public GeometryElement {
Length rx() const;
Length ry() const;

Path path() const;

std::unique_ptr<Node> clone() const;
Path path() const final;
};

class LineElement : public GeometryElement {
class LineElement final : public GeometryElement {
public:
LineElement();

Expand All @@ -87,12 +77,10 @@ class LineElement : public GeometryElement {
Length x2() const;
Length y2() const;

Path path() const;

std::unique_ptr<Node> clone() const;
Path path() const final;
};

class RectElement : public GeometryElement {
class RectElement final : public GeometryElement {
public:
RectElement();

Expand All @@ -103,9 +91,7 @@ class RectElement : public GeometryElement {
Length width() const;
Length height() const;

Path path() const;

std::unique_ptr<Node> clone() const;
Path path() const final;
};

} // namespace lunasvg
Expand Down
3 changes: 1 addition & 2 deletions source/graphicselement.h
Expand Up @@ -5,8 +5,7 @@

namespace lunasvg {

class GraphicsElement : public StyledElement
{
class GraphicsElement : public StyledElement {
public:
GraphicsElement(ElementID id);

Expand Down

0 comments on commit 4d0322d

Please sign in to comment.