Skip to content

Commit

Permalink
Fix parser
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangfitus committed Nov 3, 2023
1 parent 4c15835 commit 9a2b2d6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 32 deletions.
63 changes: 32 additions & 31 deletions src/Parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,28 @@
#define PARSER_H_
#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>

#include "Circle.hpp"
#include "Ellipse.hpp"
#include "pugixml.hpp"

using namespace std;

namespace parser {

pugi::xml_node loadFile(string path) {
pugi::xml_node loadFile(std::string path) {
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file(path.c_str());
if (!result) {
cout << "Error: " << result.description() << endl;
std::cout << "Error: " << result.description() << std::endl;
exit(-1);
}
pugi::xml_node svg = doc.child("svg");
return svg;
}

string getAttribute(pugi::xml_node node, string name) {
std::string getAttribute(pugi::xml_node node, std::string name) {
auto attr = node.attribute(name.c_str());
if (!attr) {
if (name == "fill")
return "rgb(0,0,0)";
return "black";
else if (name == "stroke")
return "none";
else if (name == "stroke-width" || name == "stroke-opacity" ||
Expand All @@ -41,11 +37,12 @@ namespace parser {
return attr.value();
};

sf::Color parseColor(pugi::xml_node node, string color) {
sf::Color parseColor(pugi::xml_node node, std::string name) {
std::string color = getAttribute(node, name);
for (auto& c : color) c = tolower(c);
if (color == "none")
return sf::Color::Transparent;
else if (color.find("rgb") == string::npos) {
else if (color.find("rgb") == std::string::npos) {
switch (color[0]) {
case 'b':
if (color[2] == 'a')
Expand All @@ -70,47 +67,51 @@ namespace parser {
} else {
unsigned int r = 0, g = 0, b = 0, a;
sscanf(color.c_str(), "rgb(%u,%u,%u)", &r, &g, &b);
a = stoi(getAttribute(node, "stroke-opacity")) * 255;
a = stof(getAttribute(node, name + "-opacity")) * 255;
return sf::Color(r, g, b, a);
}
}

vector< sf::Shape* > parseSVG() {
pugi::xml_node svg = loadFile("../sample/sample.svg");
vector< sf::Shape* > shapes;
std::vector< sf::Shape* > parseSVG(std::string path) {
pugi::xml_node svg = loadFile(path);
std::vector< sf::Shape* > shapes;
for (pugi::xml_node tool = svg.first_child(); tool;
tool = tool.next_sibling()) {
if (tool.name() == string("circle")) {
sf::Color stroke_color =
parseColor(tool, getAttribute(tool, "stroke"));
sf::Color fill_color =
parseColor(tool, getAttribute(tool, "fill"));
float stroke_width = stof(getAttribute(tool, "stroke-width"));
sf::Color stroke_color = parseColor(tool, "stroke");
sf::Color fill_color = parseColor(tool, "fill");
float stroke_width = stof(getAttribute(tool, "stroke-width"));

Circle* circle =
if (tool.name() == std::string("rect")) { // rect
} else if (tool.name() == std::string("line")) { // line
} else if (tool.name() == std::string("text")) { // text
} else if (tool.name() == std::string("circle")) {
Circle* shape =
new Circle(stof(getAttribute(tool, "r")),
stof(getAttribute(tool, "cx")),
stof(getAttribute(tool, "cy")), fill_color,
stroke_color, stroke_width);
shapes.push_back(circle);
} else if (tool.name() == string("ellipse")) {
sf::Color stroke_color =
parseColor(tool, getAttribute(tool, "stroke"));
sf::Color fill_color =
parseColor(tool, getAttribute(tool, "fill"));
float stroke_width = stof(getAttribute(tool, "stroke-width"));

Ellipse* circle =
shapes.push_back(shape);
} else if (tool.name() == std::string("ellipse")) {
Ellipse* shape =
new Ellipse(sf::Vector2f(stof(getAttribute(tool, "rx")),
stof(getAttribute(tool, "ry"))),
stof(getAttribute(tool, "cx")),
stof(getAttribute(tool, "cy")), fill_color,
stroke_color, stroke_width);
shapes.push_back(circle);
shapes.push_back(shape);
} else if (tool.name() == std::string("polygon")) { // polygon
} else if (tool.name() == std::string("polyline")) { // polyline
} else if (tool.name() == std::string("path")) { // path
}
}
return shapes;
}

void deleteShapes(std::vector< sf::Shape* >& shapes) {
for (auto shape : shapes) {
delete shape;
}
shapes.clear();
}
} // namespace parser
#endif
4 changes: 3 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int main() {
constexpr int screen_width = 1600;
constexpr int screen_height = 900;

std::vector< sf::Shape* > shapes = parser::parseSVG();
std::vector< sf::Shape* > shapes = parser::parseSVG("../sample/sample.svg");
sf::ContextSettings settings;
settings.antialiasingLevel = 16;
sf::RenderWindow window(sf::VideoMode(screen_width, screen_height),
Expand All @@ -29,5 +29,7 @@ int main() {
}
window.display();
}

parser::deleteShapes(shapes);
return 0;
}

0 comments on commit 9a2b2d6

Please sign in to comment.