Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
sammycage committed May 26, 2023
1 parent 906d326 commit a03316b
Show file tree
Hide file tree
Showing 29 changed files with 678 additions and 1,014 deletions.
13 changes: 5 additions & 8 deletions include/lunasvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ namespace lunasvg {
class Rect;
class Matrix;

class LUNASVG_API Box
{
class LUNASVG_API Box {
public:
Box() = default;
Box(double x, double y, double w, double h);
Expand All @@ -61,8 +60,7 @@ class LUNASVG_API Box

class Transform;

class LUNASVG_API Matrix
{
class LUNASVG_API Matrix {
public:
Matrix() = default;
Matrix(double a, double b, double c, double d, double e, double f);
Expand Down Expand Up @@ -99,8 +97,7 @@ class LUNASVG_API Matrix
double f{0};
};

class LUNASVG_API Bitmap
{
class LUNASVG_API Bitmap {
public:
/**
* @note Bitmap format is ARGB Premultiplied.
Expand Down Expand Up @@ -130,8 +127,7 @@ class LUNASVG_API Bitmap

class LayoutSymbol;

class LUNASVG_API Document
{
class LUNASVG_API Document {
public:
/**
* @brief Creates a document from a file
Expand Down Expand Up @@ -209,6 +205,7 @@ class LUNASVG_API Document
Bitmap renderToBitmap(std::uint32_t width = 0, std::uint32_t height = 0, std::uint32_t backgroundColor = 0x00000000) const;

~Document();

private:
Document();

Expand Down
12 changes: 4 additions & 8 deletions source/canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,9 @@ void Canvas::luminance()
auto height = plutovg_surface_get_height(surface);
auto stride = plutovg_surface_get_stride(surface);
auto data = plutovg_surface_get_data(surface);
for(int y = 0;y < height;y++)
{
for(int y = 0; y < height; y++) {
auto pixels = reinterpret_cast<uint32_t*>(data + stride * y);
for(int x = 0;x < width;x++)
{
for(int x = 0; x < width; x++) {
auto pixel = pixels[x];
auto r = (pixel >> 16) & 0xFF;
auto g = (pixel >> 8) & 0xFF;
Expand Down Expand Up @@ -229,8 +227,7 @@ static plutovg_texture_type_t to_plutovg_texture_type(TextureType type)

static void to_plutovg_stops(plutovg_gradient_t* gradient, const GradientStops& stops)
{
for(const auto& stop : stops)
{
for(const auto& stop : stops) {
auto offset = std::get<0>(stop);
auto& color = std::get<1>(stop);
plutovg_gradient_add_stop_rgba(gradient, offset, color.red() / 255.0, color.green() / 255.0, color.blue() / 255.0, color.alpha() / 255.0);
Expand All @@ -241,8 +238,7 @@ void to_plutovg_path(plutovg_t* pluto, const Path& path)
{
PathIterator it(path);
std::array<Point, 3> p;
while(!it.isDone())
{
while(!it.isDone()) {
switch(it.currentSegment(p)) {
case PathCommand::MoveTo:
plutovg_move_to(pluto, p[0].x, p[0].y);
Expand Down
13 changes: 5 additions & 8 deletions source/canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,17 @@ using GradientStops = std::vector<GradientStop>;

using DashArray = std::vector<double>;

struct DashData
{
struct DashData {
DashArray array;
double offset{0.0};
};

enum class TextureType
{
enum class TextureType {
Plain,
Tiled
};

enum class BlendMode
{
enum class BlendMode {
Src,
Src_Over,
Dst_In,
Expand All @@ -35,8 +32,7 @@ enum class BlendMode

class CanvasImpl;

class Canvas
{
class Canvas {
public:
static std::shared_ptr<Canvas> create(unsigned char* data, unsigned int width, unsigned int height, unsigned int stride);
static std::shared_ptr<Canvas> create(double x, double y, double width, double height);
Expand All @@ -61,6 +57,7 @@ class Canvas
Rect box() const;

~Canvas();

private:
Canvas(unsigned char* data, int width, int height, int stride);
Canvas(int x, int y, int width, int height);
Expand Down
3 changes: 1 addition & 2 deletions source/clippathelement.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ namespace lunasvg {

class LayoutClipPath;

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

Expand Down
3 changes: 1 addition & 2 deletions source/defselement.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

namespace lunasvg {

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

Expand Down
92 changes: 31 additions & 61 deletions source/element.cpp
Original file line number Diff line number Diff line change
@@ -1,53 +1,8 @@
#include "element.h"
#include "parser.h"
#include "svgelement.h"

namespace lunasvg {

void PropertyList::set(PropertyID id, const std::string& value, int specificity)
{
auto property = get(id);
if(property == nullptr)
{
Property property{id, value, specificity};
m_properties.push_back(std::move(property));
return;
}

if(property->specificity > specificity)
return;

property->specificity = specificity;
property->value = value;
}

Property* PropertyList::get(PropertyID id) const
{
auto data = m_properties.data();
auto end = data + m_properties.size();
while(data < end)
{
if(data->id == id)
return const_cast<Property*>(data);
++data;
}

return nullptr;
}

void PropertyList::add(const Property& property)
{
set(property.id, property.value, property.specificity);
}

void PropertyList::add(const PropertyList& properties)
{
auto it = properties.m_properties.begin();
auto end = properties.m_properties.end();
for(;it != end;++it)
add(*it);
}

void Node::layout(LayoutContext*, LayoutContainer*) const
{
}
Expand All @@ -66,18 +21,31 @@ Element::Element(ElementID id)

void Element::set(PropertyID id, const std::string& value, int specificity)
{
properties.set(id, value, specificity);
for(auto& property : properties) {
if(property.id == id) {
if(specificity >= property.specificity) {
property.specificity = specificity;
property.value = value;
return;
}
}
}

Property property{specificity, id, value};
properties.push_back(std::move(property));
}

static const std::string EmptyString;

const std::string& Element::get(PropertyID id) const
{
auto property = properties.get(id);
if(property == nullptr)
return EmptyString;
for(auto& property : properties) {
if(property.id == id) {
return property.value;
}
}

return property->value;
return EmptyString;
}

static const std::string InheritString{"inherit"};
Expand All @@ -97,7 +65,13 @@ const std::string& Element::find(PropertyID id) const

bool Element::has(PropertyID id) const
{
return properties.get(id);
for(auto& property : properties) {
if(property.id == id) {
return true;
}
}

return false;
}

Element* Element::previousElement() const
Expand All @@ -108,12 +82,10 @@ Element* Element::previousElement() const
Element* element = nullptr;
auto it = parent->children.begin();
auto end = parent->children.end();
for(;it != end;++it)
{
for(; it != end; ++it) {
auto node = it->get();
if(node->isText())
continue;

if(node == this)
return element;
element = static_cast<Element*>(node);
Expand All @@ -130,8 +102,7 @@ Element* Element::nextElement() const
Element* element = nullptr;
auto it = parent->children.rbegin();
auto end = parent->children.rend();
for(;it != end;++it)
{
for(; it != end; ++it) {
auto node = it->get();
if(node->isText())
continue;
Expand All @@ -153,22 +124,21 @@ Node* Element::addChild(std::unique_ptr<Node> child)

void Element::layoutChildren(LayoutContext* context, LayoutContainer* current) const
{
for(auto& child : children)
for(auto& child : children) {
child->layout(context, current);
}
}

Rect Element::currentViewport() const
{
if(parent == nullptr)
{
if(parent == nullptr) {
auto element = static_cast<const SVGElement*>(this);
if(element->has(PropertyID::ViewBox))
return element->viewBox();
return Rect{0, 0, 300, 150};
}

if(parent->id == ElementID::Svg)
{
if(parent->id == ElementID::Svg) {
auto element = static_cast<SVGElement*>(parent);
if(element->has(PropertyID::ViewBox))
return element->viewBox();
Expand Down
Loading

0 comments on commit a03316b

Please sign in to comment.