Skip to content

Commit

Permalink
Remove redundantly external files and refactor drawPath
Browse files Browse the repository at this point in the history
  • Loading branch information
phthtgvi committed Dec 23, 2023
1 parent a7e749e commit 75ed38f
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 117 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
*.bat
build
svg-reader
external/samples/gradient
!docs/latex/code_documentation.pdf
*.json
*.jar
24 changes: 0 additions & 24 deletions external/samples/gradient/linear.svg

This file was deleted.

24 changes: 0 additions & 24 deletions external/samples/gradient/radial.svg

This file was deleted.

File renamed without changes
4 changes: 0 additions & 4 deletions external/samples/pic.svg

This file was deleted.

57 changes: 31 additions & 26 deletions src/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ Parser::Parser(const std::string &file_name) {

Group *Parser::getRoot() { return dynamic_cast< Group * >(root); }

Attributes xmlToString(xml_attribute<> *attribute) {
Attributes xmlToString(rapidxml::xml_attribute<> *attribute) {
Attributes attributes;
while (attribute) {
attributes.push_back(
Expand All @@ -179,14 +179,14 @@ Attributes xmlToString(xml_attribute<> *attribute) {
}

SVGElement *Parser::parseElements(std::string file_name) {
xml_document<> doc;
rapidxml::xml_document<> doc;
std::ifstream file(file_name);
std::vector< char > buffer((std::istreambuf_iterator< char >(file)),
std::istreambuf_iterator< char >());
buffer.push_back('\0');
doc.parse< 0 >(&buffer[0]);

xml_node<> *svg = doc.first_node();
rapidxml::xml_node<> *svg = doc.first_node();
viewBox.second.x = getFloatAttribute(svg, "width");
viewBox.second.y = getFloatAttribute(svg, "height");
std::string viewBox = getAttribute(svg, "viewBox");
Expand All @@ -195,8 +195,8 @@ SVGElement *Parser::parseElements(std::string file_name) {
ss >> this->viewBox.first.x >> this->viewBox.first.y >>
this->viewBox.second.x >> this->viewBox.second.y;
}
xml_node<> *node = svg->first_node();
xml_node<> *prev = NULL;
rapidxml::xml_node<> *node = svg->first_node();
rapidxml::xml_node<> *prev = NULL;

SVGElement *root = new Group();
SVGElement *current = root;
Expand Down Expand Up @@ -230,7 +230,7 @@ SVGElement *Parser::parseElements(std::string file_name) {
doc.allocate_string(group_attribute.first.c_str());
char *value =
doc.allocate_string(group_attribute.second.c_str());
xml_attribute<> *new_attribute =
rapidxml::xml_attribute<> *new_attribute =
doc.allocate_attribute(name, value);
node->append_attribute(new_attribute);
}
Expand Down Expand Up @@ -265,7 +265,7 @@ SVGElement *Parser::parseElements(std::string file_name) {
doc.allocate_string(group_attribute.first.c_str());
char *value =
doc.allocate_string(group_attribute.second.c_str());
xml_attribute<> *new_attribute =
rapidxml::xml_attribute<> *new_attribute =
doc.allocate_attribute(name, value);
node->append_attribute(new_attribute);
}
Expand Down Expand Up @@ -293,7 +293,7 @@ SVGElement *Parser::parseElements(std::string file_name) {
return root;
}

std::string Parser::getAttribute(xml_node<> *node, std::string name) {
std::string Parser::getAttribute(rapidxml::xml_node<> *node, std::string name) {
if (name == "text") return removeExtraSpaces(node->value());
std::string result;
if (node->first_attribute(name.c_str()) == NULL) {
Expand All @@ -314,7 +314,7 @@ std::string Parser::getAttribute(xml_node<> *node, std::string name) {
return result;
}

float Parser::getFloatAttribute(xml_node<> *node, std::string name) {
float Parser::getFloatAttribute(rapidxml::xml_node<> *node, std::string name) {
float result;
if (node->first_attribute(name.c_str()) == NULL) {
if (std::string(node->name()).find("Gradient") != std::string::npos) {
Expand Down Expand Up @@ -348,7 +348,8 @@ float Parser::getFloatAttribute(xml_node<> *node, std::string name) {
return result;
}

mColor Parser::parseColor(xml_node<> *node, std::string name, std::string &id) {
mColor Parser::parseColor(rapidxml::xml_node<> *node, std::string name,
std::string &id) {
std::string color = getAttribute(node, name);
color.erase(std::remove(color.begin(), color.end(), ' '), color.end());
if (color.find("url") == std::string::npos) {
Expand Down Expand Up @@ -397,9 +398,9 @@ Gradient *Parser::parseGradient(std::string id) {
return gradients.at(id);
}

std::vector< Stop > Parser::getGradientStops(xml_node<> *node) {
std::vector< Stop > Parser::getGradientStops(rapidxml::xml_node<> *node) {
std::vector< Stop > stops;
xml_node<> *stop_node = node->first_node();
rapidxml::xml_node<> *stop_node = node->first_node();
while (stop_node) {
if (std::string(stop_node->name()) == "stop") {
std::string id = "";
Expand All @@ -413,8 +414,8 @@ std::vector< Stop > Parser::getGradientStops(xml_node<> *node) {
return stops;
}

void Parser::GetGradients(xml_node<> *node) {
xml_node<> *gradient_node = node->first_node();
void Parser::GetGradients(rapidxml::xml_node<> *node) {
rapidxml::xml_node<> *gradient_node = node->first_node();
while (gradient_node) {
if (std::string(gradient_node->name()).find("Gradient") !=
std::string::npos) {
Expand Down Expand Up @@ -463,7 +464,7 @@ void Parser::GetGradients(xml_node<> *node) {
}
}

std::vector< Vector2Df > Parser::parsePoints(xml_node<> *node) {
std::vector< Vector2Df > Parser::parsePoints(rapidxml::xml_node<> *node) {
std::vector< Vector2Df > points;
std::string points_string = getAttribute(node, "points");

Expand All @@ -479,7 +480,7 @@ std::vector< Vector2Df > Parser::parsePoints(xml_node<> *node) {
return points;
}

std::vector< PathPoint > Parser::parsePathPoints(xml_node<> *node) {
std::vector< PathPoint > Parser::parsePathPoints(rapidxml::xml_node<> *node) {
std::vector< PathPoint > points;
std::string path_string = getAttribute(node, "d");

Expand Down Expand Up @@ -622,7 +623,8 @@ std::vector< PathPoint > Parser::parsePathPoints(xml_node<> *node) {
return handle_points;
}

std::vector< std::string > Parser::getTransformOrder(xml_node<> *node) {
std::vector< std::string > Parser::getTransformOrder(
rapidxml::xml_node<> *node) {
std::string transform_tag;
if (std::string(node->name()).find("Gradient") != std::string::npos)
transform_tag = getAttribute(node, "gradientTransform");
Expand Down Expand Up @@ -651,7 +653,7 @@ std::vector< std::string > Parser::getTransformOrder(xml_node<> *node) {
return order;
}

SVGElement *Parser::parseShape(xml_node<> *node) {
SVGElement *Parser::parseShape(rapidxml::xml_node<> *node) {
SVGElement *shape = NULL;
std::string type = node->name();
std::string id = "";
Expand Down Expand Up @@ -694,7 +696,7 @@ SVGElement *Parser::parseShape(xml_node<> *node) {
return shape;
}

Line *Parser::parseLine(xml_node<> *node, const mColor &stroke_color,
Line *Parser::parseLine(rapidxml::xml_node<> *node, const mColor &stroke_color,
float stroke_width) {
Line *shape = new Line(
Vector2Df(getFloatAttribute(node, "x1"), getFloatAttribute(node, "y1")),
Expand All @@ -703,7 +705,7 @@ Line *Parser::parseLine(xml_node<> *node, const mColor &stroke_color,
return shape;
}

Rect *Parser::parseRect(xml_node<> *node, const mColor &fill_color,
Rect *Parser::parseRect(rapidxml::xml_node<> *node, const mColor &fill_color,
const mColor &stroke_color, float stroke_width) {
float x = getFloatAttribute(node, "x");
float y = getFloatAttribute(node, "y");
Expand All @@ -716,7 +718,8 @@ Rect *Parser::parseRect(xml_node<> *node, const mColor &fill_color,
return shape;
}

Circle *Parser::parseCircle(xml_node<> *node, const mColor &fill_color,
Circle *Parser::parseCircle(rapidxml::xml_node<> *node,
const mColor &fill_color,
const mColor &stroke_color, float stroke_width) {
float cx = getFloatAttribute(node, "cx");
float cy = getFloatAttribute(node, "cy");
Expand All @@ -726,7 +729,7 @@ Circle *Parser::parseCircle(xml_node<> *node, const mColor &fill_color,
return shape;
}

Ell *Parser::parseEllipse(xml_node<> *node, const mColor &fill_color,
Ell *Parser::parseEllipse(rapidxml::xml_node<> *node, const mColor &fill_color,
const mColor &stroke_color, float stroke_width) {
float radius_x = getFloatAttribute(node, "rx");
float radius_y = getFloatAttribute(node, "ry");
Expand All @@ -737,7 +740,8 @@ Ell *Parser::parseEllipse(xml_node<> *node, const mColor &fill_color,
return shape;
}

Plygon *Parser::parsePolygon(xml_node<> *node, const mColor &fill_color,
Plygon *Parser::parsePolygon(rapidxml::xml_node<> *node,
const mColor &fill_color,
const mColor &stroke_color, float stroke_width) {
Plygon *shape = new Plygon(fill_color, stroke_color, stroke_width);
std::vector< Vector2Df > points = parsePoints(node);
Expand All @@ -751,7 +755,8 @@ Plygon *Parser::parsePolygon(xml_node<> *node, const mColor &fill_color,
return shape;
}

Plyline *Parser::parsePolyline(xml_node<> *node, const mColor &fill_color,
Plyline *Parser::parsePolyline(rapidxml::xml_node<> *node,
const mColor &fill_color,
const mColor &stroke_color, float stroke_width) {
Plyline *shape = new Plyline(fill_color, stroke_color, stroke_width);
std::vector< Vector2Df > points = parsePoints(node);
Expand All @@ -765,7 +770,7 @@ Plyline *Parser::parsePolyline(xml_node<> *node, const mColor &fill_color,
return shape;
}

Text *Parser::parseText(xml_node<> *node, const mColor &fill_color,
Text *Parser::parseText(rapidxml::xml_node<> *node, const mColor &fill_color,
const mColor &stroke_color, float stroke_width) {
float x = getFloatAttribute(node, "x");
float y = getFloatAttribute(node, "y");
Expand All @@ -788,7 +793,7 @@ Text *Parser::parseText(xml_node<> *node, const mColor &fill_color,
return shape;
}

Path *Parser::parsePath(xml_node<> *node, const mColor &fill_color,
Path *Parser::parsePath(rapidxml::xml_node<> *node, const mColor &fill_color,
const mColor &stroke_color, float stroke_width) {
Path *shape = new Path(fill_color, stroke_color, stroke_width);
std::vector< PathPoint > points = parsePathPoints(node);
Expand Down
Loading

0 comments on commit 75ed38f

Please sign in to comment.