Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
sammycage committed Jan 10, 2025
1 parent 5101088 commit 3c203c4
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 15 deletions.
12 changes: 9 additions & 3 deletions source/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,14 @@ void Path::addRect(const Rect& rect)

void Path::reset()
{
plutovg_path_destroy(m_data);
m_data = nullptr;
if(m_data == nullptr)
return;
if(isUnique()) {
plutovg_path_reset(m_data);
} else {
plutovg_path_destroy(m_data);
m_data = nullptr;
}
}

Rect Path::boundingRect() const
Expand All @@ -301,7 +307,7 @@ bool Path::isUnique() const
{
if(m_data)
return plutovg_path_get_reference_count(m_data) == 1;
return true;
return false;
}

bool Path::parse(const char* data, size_t length)
Expand Down
4 changes: 2 additions & 2 deletions source/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ class Transform {
float yScale() const;

const plutovg_matrix_t& matrix() const { return m_matrix; }
plutovg_matrix_t& matrix() { return m_matrix; }

bool parse(const char* data, size_t length);

Expand Down Expand Up @@ -357,11 +358,10 @@ class Path {
bool isEmpty() const;
bool isUnique() const;
bool isNull() const { return m_data == nullptr; }
plutovg_path_t* data() const { return m_data; }

bool parse(const char* data, size_t length);

plutovg_path_t* data() const { return m_data; }

private:
plutovg_path_t* release();
plutovg_path_t* ensure();
Expand Down
7 changes: 4 additions & 3 deletions source/svgelement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,6 @@ std::unique_ptr<SVGElement> SVGUseElement::cloneTargetElement(SVGElement* target

SVGImageElement::SVGImageElement(Document* document)
: SVGGraphicsElement(document, ElementID::Image)
, SVGURIReference(this)
, m_x(PropertyID::X, LengthDirection::Horizontal, LengthNegativeMode::Allow, 0.f, LengthUnits::None)
, m_y(PropertyID::Y, LengthDirection::Vertical, LengthNegativeMode::Allow, 0.f, LengthUnits::None)
, m_width(PropertyID::Width, LengthDirection::Horizontal, LengthNegativeMode::Forbid, 100.f, LengthUnits::Percent)
Expand Down Expand Up @@ -858,9 +857,11 @@ static Bitmap loadImageResource(const std::string& href)

void SVGImageElement::parseAttribute(PropertyID id, const std::string& value)
{
if(id == PropertyID::Href)
if(id == PropertyID::Href) {
m_image = loadImageResource(value);
SVGGraphicsElement::parseAttribute(id, value);
} else {
SVGGraphicsElement::parseAttribute(id, value);
}
}

SVGSymbolElement::SVGSymbolElement(Document* document)
Expand Down
4 changes: 3 additions & 1 deletion source/svgelement.h
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ class SVGUseElement final : public SVGGraphicsElement, public SVGURIReference {
SVGLength m_height;
};

class SVGImageElement final : public SVGGraphicsElement, public SVGURIReference {
class SVGImageElement final : public SVGGraphicsElement {
public:
SVGImageElement(Document* document);

Expand Down Expand Up @@ -453,6 +453,7 @@ class SVGClipPathElement final : public SVGGraphicsElement {

void applyClipMask(SVGRenderState& state) const;
void applyClipPath(SVGRenderState& state) const;

bool requiresMasking() const;

private:
Expand All @@ -473,6 +474,7 @@ class SVGMaskElement final : public SVGElement {
Rect maskRect(const SVGElement* element) const;
Rect maskBoundingBox(const SVGElement* element) const;
void applyMask(SVGRenderState& state) const;

void layoutElement(const SVGLayoutState& state) final;

private:
Expand Down
2 changes: 1 addition & 1 deletion source/svggeometryelement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ Rect SVGCircleElement::updateShape(Path& path)
{
LengthContext lengthContext(this);
auto r = lengthContext.valueForLength(m_r);
if(r <= 0.f || r <= 0.f) {
if(r <= 0.f) {
return Rect::Empty;
}

Expand Down
1 change: 1 addition & 0 deletions source/svggeometryelement.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class SVGGeometryElement : public SVGGraphicsElement {
FillRule clip_rule() const { return m_clip_rule; }

virtual Rect updateShape(Path& path) = 0;

void updateMarkerPositions(SVGMarkerPositionList& positions, const SVGLayoutState& state);
void render(SVGRenderState& state) const override;

Expand Down
9 changes: 4 additions & 5 deletions source/svgtextelement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void SVGTextFragmentsBuilder::build(const SVGTextElement* textElement)
auto recordTextFragment = [&](auto startOffset, auto endOffset) {
auto text = wholeText.substr(startOffset, endOffset - startOffset);
fragment.offset = startOffset;
fragment.length = endOffset - startOffset;
fragment.length = text.length();
fragment.width = element->font().measureText(text);
m_fragments.push_back(fragment);
m_x += fragment.width;
Expand Down Expand Up @@ -265,7 +265,7 @@ void SVGTextFragmentsBuilder::handleText(const SVGTextNode* node)

void SVGTextFragmentsBuilder::handleElement(const SVGTextPositioningElement* element)
{
auto itemIndex = m_textPositions.size();
const auto itemIndex = m_textPositions.size();
m_textPositions.emplace_back(element, m_text.length(), m_text.length());
for(const auto& child : element->children()) {
if(child->isTextNode()) {
Expand Down Expand Up @@ -394,13 +394,12 @@ SVGTextElement::SVGTextElement(Document* document)
void SVGTextElement::layout(SVGLayoutState& state)
{
SVGTextPositioningElement::layout(state);
SVGTextFragmentsBuilder fragmentsBuilder(m_text, m_fragments);
fragmentsBuilder.build(this);
SVGTextFragmentsBuilder(m_text, m_fragments).build(this);
}

void SVGTextElement::render(SVGRenderState& state) const
{
if(m_text.empty() || isVisibilityHidden() || isDisplayNone())
if(m_fragments.empty() || isVisibilityHidden() || isDisplayNone())
return;
SVGBlendInfo blendInfo(this);
SVGRenderState newState(this, state, localTransform());
Expand Down

0 comments on commit 3c203c4

Please sign in to comment.