Skip to content

Commit

Permalink
Add getBBox and getLocalTransform #98 #134
Browse files Browse the repository at this point in the history
  • Loading branch information
sammycage committed Jan 9, 2024
1 parent 231f955 commit 6947ee1
Show file tree
Hide file tree
Showing 24 changed files with 164 additions and 152 deletions.
18 changes: 18 additions & 0 deletions include/lunasvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,24 @@ class LUNASVG_API DomElement {
*/
bool hasAttribute(const std::string& name) const;

/**
* @brief getBBox
* @return
*/
Box getBBox() const;

/**
* @brief getLocalTransform
* @return
*/
Matrix getLocalTransform() const;

/**
* @brief getAbsoluteTransform
* @return
*/
Matrix getAbsoluteTransform() const;

/**
* @brief isNull
* @return
Expand Down
4 changes: 2 additions & 2 deletions source/clippathelement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ Units ClipPathElement::clipPathUnits() const
return Parser::parseUnits(value, Units::UserSpaceOnUse);
}

std::unique_ptr<LayoutClipPath> ClipPathElement::getClipper(LayoutContext* context) const
std::unique_ptr<LayoutClipPath> ClipPathElement::getClipper(LayoutContext* context)
{
if(context->hasReference(this))
return nullptr;
LayoutBreaker layoutBreaker(context, this);
auto clipper = makeUnique<LayoutClipPath>();
auto clipper = makeUnique<LayoutClipPath>(this);
clipper->units = clipPathUnits();
clipper->transform = transform();
clipper->clipper = context->getClipper(clip_path());
Expand Down
2 changes: 1 addition & 1 deletion source/clippathelement.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ClipPathElement final : public GraphicsElement {
ClipPathElement();

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

} // namespace lunasvg
Expand Down
6 changes: 1 addition & 5 deletions source/element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@

namespace lunasvg {

void Node::layout(LayoutContext*, LayoutContainer*) const
{
}

std::unique_ptr<Node> TextNode::clone() const
{
auto node = makeUnique<TextNode>();
Expand Down Expand Up @@ -184,7 +180,7 @@ Node* Element::addChild(std::unique_ptr<Node> child)
return &*children.back();
}

void Element::layoutChildren(LayoutContext* context, LayoutContainer* current) const
void Element::layoutChildren(LayoutContext* context, LayoutContainer* current)
{
for(auto& child : children) {
child->layout(context, current);
Expand Down
14 changes: 8 additions & 6 deletions source/element.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ inline std::unique_ptr<T> makeUnique(Args&&... args)
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}

class LayoutContext;
class LayoutObject;
class LayoutContainer;
class LayoutContext;
class Element;

class Node {
Expand All @@ -138,19 +139,20 @@ class Node {
virtual bool isText() const { return false; }
virtual bool isPaint() const { return false; }
virtual bool isGeometry() const { return false; }
virtual void layout(LayoutContext*, LayoutContainer*) const;
virtual void layout(LayoutContext*, LayoutContainer*) {}
virtual std::unique_ptr<Node> clone() const = 0;

public:
Element* parent = nullptr;
LayoutObject* box = nullptr;
};

class TextNode : public Node {
class TextNode final : public Node {
public:
TextNode() = default;

bool isText() const { return true; }
std::unique_ptr<Node> clone() const;
bool isText() const final { return true; }
std::unique_ptr<Node> clone() const final;

public:
std::string text;
Expand All @@ -170,7 +172,7 @@ class Element : public Node {
Element* previousElement() const;
Element* nextElement() const;
Node* addChild(std::unique_ptr<Node> child);
void layoutChildren(LayoutContext* context, LayoutContainer* current) const;
void layoutChildren(LayoutContext* context, LayoutContainer* current);
Rect currentViewport() const;

virtual void build(const Document* document);
Expand Down
4 changes: 2 additions & 2 deletions source/gelement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ GElement::GElement()
{
}

void GElement::layout(LayoutContext* context, LayoutContainer* current) const
void GElement::layout(LayoutContext* context, LayoutContainer* current)
{
if(isDisplayNone())
return;
auto group = makeUnique<LayoutGroup>();
auto group = makeUnique<LayoutGroup>(this);
group->transform = transform();
group->opacity = opacity();
group->masker = context->getMasker(mask());
Expand Down
2 changes: 1 addition & 1 deletion source/gelement.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class GElement final : public GraphicsElement {
public:
GElement();

void layout(LayoutContext* context, LayoutContainer* current) const;
void layout(LayoutContext* context, LayoutContainer* current) final;
};

} // namespace lunasvg
Expand Down
5 changes: 2 additions & 3 deletions source/geometryelement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ GeometryElement::GeometryElement(ElementID id)
{
}

void GeometryElement::layout(LayoutContext* context, LayoutContainer* current) const
void GeometryElement::layout(LayoutContext* context, LayoutContainer* current)
{
if(isDisplayNone())
return;

auto path = this->path();
if(path.empty())
return;

auto shape = makeUnique<LayoutShape>();
auto shape = makeUnique<LayoutShape>(this);
shape->path = std::move(path);
shape->transform = transform();
shape->fillData = context->fillData(this);
Expand Down
4 changes: 2 additions & 2 deletions source/geometryelement.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class GeometryElement : public GraphicsElement {
public:
GeometryElement(ElementID id);

bool isGeometry() const { return true; }
virtual void layout(LayoutContext* context, LayoutContainer* current) const;
bool isGeometry() const final { return true; }
void layout(LayoutContext* context, LayoutContainer* current) final;
virtual Path path() const = 0;
};

Expand Down
85 changes: 27 additions & 58 deletions source/layoutcontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,14 @@

namespace lunasvg {

LayoutObject::LayoutObject(LayoutId id)
: id(id)
LayoutObject::LayoutObject(Node* node, LayoutId id)
: node(node), id(id)
{
node->box = this;
}

LayoutObject::~LayoutObject()
{
}

void LayoutObject::render(RenderState&) const
{
}

void LayoutObject::apply(RenderState&) const
{
}

Rect LayoutObject::map(const Rect&) const
{
return Rect::Invalid;
}

LayoutContainer::LayoutContainer(LayoutId id)
: LayoutObject(id)
LayoutContainer::LayoutContainer(Node* node, LayoutId id)
: LayoutObject(node, id)
{
}

Expand Down Expand Up @@ -86,8 +70,8 @@ void LayoutContainer::renderChildren(RenderState& state) const
}
}

LayoutClipPath::LayoutClipPath()
: LayoutContainer(LayoutId::ClipPath)
LayoutClipPath::LayoutClipPath(Node* node)
: LayoutContainer(node, LayoutId::ClipPath)
{
}

Expand All @@ -107,8 +91,8 @@ void LayoutClipPath::apply(RenderState& state) const
state.canvas->blend(newState.canvas.get(), BlendMode::Dst_In, 1.0);
}

LayoutMask::LayoutMask()
: LayoutContainer(LayoutId::Mask)
LayoutMask::LayoutMask(Node* node)
: LayoutContainer(node, LayoutId::Mask)
{
}

Expand Down Expand Up @@ -140,8 +124,8 @@ void LayoutMask::apply(RenderState& state) const
state.canvas->blend(newState.canvas.get(), BlendMode::Dst_In, opacity);
}

LayoutSymbol::LayoutSymbol()
: LayoutContainer(LayoutId::Symbol)
LayoutSymbol::LayoutSymbol(Node* node)
: LayoutContainer(node, LayoutId::Symbol)
{
}

Expand All @@ -155,13 +139,8 @@ void LayoutSymbol::render(RenderState& state) const
newState.endGroup(state, info);
}

Rect LayoutSymbol::map(const Rect& rect) const
{
return transform.map(rect);
}

LayoutGroup::LayoutGroup()
: LayoutContainer(LayoutId::Group)
LayoutGroup::LayoutGroup(Node* node)
: LayoutContainer(node, LayoutId::Group)
{
}

Expand All @@ -175,13 +154,8 @@ void LayoutGroup::render(RenderState& state) const
newState.endGroup(state, info);
}

Rect LayoutGroup::map(const Rect& rect) const
{
return transform.map(rect);
}

LayoutMarker::LayoutMarker()
: LayoutContainer(LayoutId::Marker)
LayoutMarker::LayoutMarker(Node* node)
: LayoutContainer(node, LayoutId::Marker)
{
}

Expand Down Expand Up @@ -217,8 +191,8 @@ void LayoutMarker::renderMarker(RenderState& state, const Point& origin, double
newState.endGroup(state, info);
}

LayoutPattern::LayoutPattern()
: LayoutContainer(LayoutId::Pattern)
LayoutPattern::LayoutPattern(Node* node)
: LayoutContainer(node, LayoutId::Pattern)
{
}

Expand Down Expand Up @@ -260,13 +234,13 @@ void LayoutPattern::apply(RenderState& state) const
state.canvas->setTexture(newState.canvas.get(), TextureType::Tiled, transform);
}

LayoutGradient::LayoutGradient(LayoutId id)
: LayoutObject(id)
LayoutGradient::LayoutGradient(Node* node, LayoutId id)
: LayoutObject(node, id)
{
}

LayoutLinearGradient::LayoutLinearGradient()
: LayoutGradient(LayoutId::LinearGradient)
LayoutLinearGradient::LayoutLinearGradient(Node* node)
: LayoutGradient(node, LayoutId::LinearGradient)
{
}

Expand All @@ -281,8 +255,8 @@ void LayoutLinearGradient::apply(RenderState& state) const
state.canvas->setLinearGradient(x1, y1, x2, y2, stops, spreadMethod, transform);
}

LayoutRadialGradient::LayoutRadialGradient()
: LayoutGradient(LayoutId::RadialGradient)
LayoutRadialGradient::LayoutRadialGradient(Node* node)
: LayoutGradient(node, LayoutId::RadialGradient)
{
}

Expand All @@ -297,8 +271,8 @@ void LayoutRadialGradient::apply(RenderState& state) const
state.canvas->setRadialGradient(cx, cy, r, fx, fy, stops, spreadMethod, transform);
}

LayoutSolidColor::LayoutSolidColor()
: LayoutObject(LayoutId::SolidColor)
LayoutSolidColor::LayoutSolidColor(Node* node)
: LayoutObject(node, LayoutId::SolidColor)
{
}

Expand Down Expand Up @@ -379,8 +353,8 @@ void MarkerData::inflate(Rect& box) const
}
}

LayoutShape::LayoutShape()
: LayoutObject(LayoutId::Shape)
LayoutShape::LayoutShape(Node* node)
: LayoutObject(node, LayoutId::Shape)
{
}

Expand All @@ -406,11 +380,6 @@ void LayoutShape::render(RenderState& state) const
newState.endGroup(state, info);
}

Rect LayoutShape::map(const Rect& rect) const
{
return transform.map(rect);
}

const Rect& LayoutShape::fillBoundingBox() const
{
if(m_fillBoundingBox.valid())
Expand Down

0 comments on commit 6947ee1

Please sign in to comment.