From 75ed38fdbd1d2d957dcc468337ab2a1314c9a44b Mon Sep 17 00:00:00 2001 From: quan27 Date: Sat, 23 Dec 2023 15:09:41 +0700 Subject: [PATCH] Remove redundantly external files and refactor drawPath --- .gitignore | 1 + ...x_logo,_2019.svg => Firefox_logo_2019.svg} | 0 external/samples/gradient/linear.svg | 24 -------- external/samples/gradient/radial.svg | 24 -------- .../mixed/{sample12.svg => sample11.svg} | 0 external/samples/pic.svg | 4 -- src/Parser.cpp | 57 ++++++++++--------- src/Parser.hpp | 40 ++++++------- src/Renderer.cpp | 38 ++++++------- src/graphics/Line.cpp | 2 +- 10 files changed, 73 insertions(+), 117 deletions(-) rename external/samples/TestCases/{Firefox_logo,_2019.svg => Firefox_logo_2019.svg} (100%) delete mode 100644 external/samples/gradient/linear.svg delete mode 100644 external/samples/gradient/radial.svg rename external/samples/mixed/{sample12.svg => sample11.svg} (100%) delete mode 100644 external/samples/pic.svg diff --git a/.gitignore b/.gitignore index dfc25cc3..6faf2c73 100644 --- a/.gitignore +++ b/.gitignore @@ -32,6 +32,7 @@ *.bat build svg-reader +external/samples/gradient !docs/latex/code_documentation.pdf *.json *.jar diff --git a/external/samples/TestCases/Firefox_logo,_2019.svg b/external/samples/TestCases/Firefox_logo_2019.svg similarity index 100% rename from external/samples/TestCases/Firefox_logo,_2019.svg rename to external/samples/TestCases/Firefox_logo_2019.svg diff --git a/external/samples/gradient/linear.svg b/external/samples/gradient/linear.svg deleted file mode 100644 index 5b112a35..00000000 --- a/external/samples/gradient/linear.svg +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - - - - - - - - - - Nguyen Van A - - \ No newline at end of file diff --git a/external/samples/gradient/radial.svg b/external/samples/gradient/radial.svg deleted file mode 100644 index de22070e..00000000 --- a/external/samples/gradient/radial.svg +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - - - - - - - - - - Nguyen Van A - - \ No newline at end of file diff --git a/external/samples/mixed/sample12.svg b/external/samples/mixed/sample11.svg similarity index 100% rename from external/samples/mixed/sample12.svg rename to external/samples/mixed/sample11.svg diff --git a/external/samples/pic.svg b/external/samples/pic.svg deleted file mode 100644 index 7cfefb62..00000000 --- a/external/samples/pic.svg +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/src/Parser.cpp b/src/Parser.cpp index b1bce775..aefa7532 100644 --- a/src/Parser.cpp +++ b/src/Parser.cpp @@ -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( @@ -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"); @@ -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; @@ -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); } @@ -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); } @@ -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) { @@ -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) { @@ -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) { @@ -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 = ""; @@ -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) { @@ -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"); @@ -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"); @@ -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"); @@ -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 = ""; @@ -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")), @@ -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"); @@ -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"); @@ -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"); @@ -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); @@ -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); @@ -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"); @@ -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); diff --git a/src/Parser.hpp b/src/Parser.hpp index 11f28ddb..c1b20dd9 100644 --- a/src/Parser.hpp +++ b/src/Parser.hpp @@ -13,8 +13,6 @@ #include "../external/rapidxml/rapidxml.hpp" #include "Graphics.hpp" -using namespace rapidxml; - typedef std::vector< std::pair< std::string, std::string > > Attributes; /** @@ -68,7 +66,7 @@ class Parser { * * @return The attributes of the node. */ - std::string getAttribute(xml_node<>* node, std::string name); + std::string getAttribute(rapidxml::xml_node<>* node, std::string name); /** * @brief Gets the floating point attributes of a node. @@ -78,7 +76,7 @@ class Parser { * * @return The floating point attributes of the node. */ - float getFloatAttribute(xml_node<>* node, std::string name); + float getFloatAttribute(rapidxml::xml_node<>* node, std::string name); /** * @brief Gets the gradient stops of a node. @@ -86,14 +84,14 @@ class Parser { * @param node The node to be parsed. * @return The gradient stops of the node. */ - std::vector< Stop > getGradientStops(xml_node<>* node); + std::vector< Stop > getGradientStops(rapidxml::xml_node<>* node); /** * @brief Gets the gradients of a node. * * @param node The node to be parsed. */ - void GetGradients(xml_node<>* node); + void GetGradients(rapidxml::xml_node<>* node); /** * @brief Gets the gradient of a node. @@ -111,7 +109,8 @@ class Parser { * @param id The id to check if the color is a reference. * @return The color attributes of the node. */ - mColor parseColor(xml_node<>* node, std::string color, std::string& id); + mColor parseColor(rapidxml::xml_node<>* node, std::string color, + std::string& id); /** * @brief Gets the points of the element @@ -119,7 +118,7 @@ class Parser { * @param node The node to be parsed. * @return The points of the element */ - std::vector< Vector2Df > parsePoints(xml_node<>* node); + std::vector< Vector2Df > parsePoints(rapidxml::xml_node<>* node); /** * @brief Gets the points of the path element @@ -127,7 +126,7 @@ class Parser { * @param node The node to be parsed. * @return The points of the path element */ - std::vector< PathPoint > parsePathPoints(xml_node<>* node); + std::vector< PathPoint > parsePathPoints(rapidxml::xml_node<>* node); /** * @brief Gets the transform order of the element @@ -135,7 +134,7 @@ class Parser { * @param node The node to be parsed. * @return The transform order of the element */ - std::vector< std::string > getTransformOrder(xml_node<>* node); + std::vector< std::string > getTransformOrder(rapidxml::xml_node<>* node); /** * @brief Parses the line element @@ -145,7 +144,7 @@ class Parser { * @param stroke_width The width of the stroke * @return The line element */ - Line* parseLine(xml_node<>* node, const mColor& stroke_color, + Line* parseLine(rapidxml::xml_node<>* node, const mColor& stroke_color, float stroke_width); /** @@ -157,7 +156,7 @@ class Parser { * @param stroke_width The width of the stroke * @return The rect element */ - Rect* parseRect(xml_node<>* node, const mColor& fill_color, + Rect* parseRect(rapidxml::xml_node<>* node, const mColor& fill_color, const mColor& stroke_color, float stroke_width); /** @@ -169,7 +168,8 @@ class Parser { * @param stroke_width The width of the stroke * @return The polyline element */ - class Plyline* parsePolyline(xml_node<>* node, const mColor& fill_color, + class Plyline* parsePolyline(rapidxml::xml_node<>* node, + const mColor& fill_color, const mColor& stroke_color, float stroke_width); @@ -182,7 +182,8 @@ class Parser { * @param stroke_width The width of the stroke * @return The polygon element */ - class Plygon* parsePolygon(xml_node<>* node, const mColor& fill_color, + class Plygon* parsePolygon(rapidxml::xml_node<>* node, + const mColor& fill_color, const mColor& stroke_color, float stroke_width); /** @@ -194,7 +195,7 @@ class Parser { * @param stroke_width The width of the stroke * @return The circle element */ - Circle* parseCircle(xml_node<>* node, const mColor& fill_color, + Circle* parseCircle(rapidxml::xml_node<>* node, const mColor& fill_color, const mColor& stroke_color, float stroke_width); /** @@ -206,7 +207,8 @@ class Parser { * @param stroke_width The width of the stroke * @return The ellipse element */ - class Ell* parseEllipse(xml_node<>* node, const mColor& fill_color, + class Ell* parseEllipse(rapidxml::xml_node<>* node, + const mColor& fill_color, const mColor& stroke_color, float stroke_width); /** @@ -218,7 +220,7 @@ class Parser { * @param stroke_width The width of the stroke * @return The path element */ - Path* parsePath(xml_node<>* node, const mColor& fill_color, + Path* parsePath(rapidxml::xml_node<>* node, const mColor& fill_color, const mColor& stroke_color, float stroke_width); /** @@ -229,7 +231,7 @@ class Parser { * @param stroke_width The width of the stroke * @return The text element */ - Text* parseText(xml_node<>* node, const mColor& fill_color, + Text* parseText(rapidxml::xml_node<>* node, const mColor& fill_color, const mColor& stroke_color, float stroke_width); /** @@ -238,7 +240,7 @@ class Parser { * @param node The node to be parsed. * @return The group of elements */ - SVGElement* parseShape(xml_node<>* node); + SVGElement* parseShape(rapidxml::xml_node<>* node); private: static Parser* instance; ///< The instance of the Parser. diff --git a/src/Renderer.cpp b/src/Renderer.cpp index e26cd25a..09e8a500 100644 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -1,6 +1,8 @@ #include "Renderer.hpp" #include +#include +#include Renderer* Renderer::instance = nullptr; @@ -262,8 +264,6 @@ void Renderer::drawPolygon(Gdiplus::Graphics& graphics, Plygon* polygon) const { delete polygon_fill; } -#include -#include void Renderer::drawText(Gdiplus::Graphics& graphics, Text* text) const { mColor outline_color = text->getOutlineColor(); graphics.SetTextRenderingHint(Gdiplus::TextRenderingHintAntiAliasGridFit); @@ -343,13 +343,13 @@ void Renderer::drawPolyline(Gdiplus::Graphics& graphics, Gdiplus::RectF bound(min_bound.x, min_bound.y, max_bound.x - min_bound.x, max_bound.y - min_bound.y); Gdiplus::Brush* polyline_fill = getBrush(polyline, bound); - // if (Gdiplus::PathGradientBrush* brush = - // dynamic_cast< Gdiplus::PathGradientBrush* >(polyline_fill)) { - // mColor color = polyline->getGradient()->getStops().back().getColor(); - // Gdiplus::SolidBrush corner_fill( - // Gdiplus::Color(color.a, color.r, color.g, color.b)); - // graphics.FillPath(&corner_fill, &path); - // } + if (Gdiplus::PathGradientBrush* brush = + dynamic_cast< Gdiplus::PathGradientBrush* >(polyline_fill)) { + mColor color = polyline->getGradient()->getStops().back().getColor(); + Gdiplus::SolidBrush corner_fill( + Gdiplus::Color(color.a, color.r, color.g, color.b)); + graphics.FillPath(&corner_fill, &path); + } graphics.FillPath(polyline_fill, &path); graphics.DrawPath(&polyline_outline, &path); delete polyline_fill; @@ -509,23 +509,23 @@ void Renderer::drawPath(Gdiplus::Graphics& graphics, Path* path) const { center.y = (cosAngle * cosAngle + sinAngle * sinAngle) * point2.y + Y; - float startAngle = + float start_angle = atan2((point1.y - point2.y) / ry, (point1.x - point2.x) / rx); - float endAngle = + float end_angle = atan2((-point1.y - point2.y) / ry, (-point1.x - point2.x) / rx); - float deltaAngle = endAngle - startAngle; + float delta_angle = end_angle - start_angle; - if (sweep_flag && deltaAngle < 0) { - deltaAngle += 2.0 * acos(-1); - } else if (!sweep_flag && deltaAngle > 0) { - deltaAngle -= 2.0 * acos(-1); + if (sweep_flag && delta_angle < 0) { + delta_angle += 2.0 * acos(-1); + } else if (!sweep_flag && delta_angle > 0) { + delta_angle -= 2.0 * acos(-1); } float start_angle_degree = - std::fmod((startAngle * 180.0) / acos(-1), 360); + std::fmod((start_angle * 180.0) / acos(-1), 360); float delta_angle_degree = - std::fmod((deltaAngle * 180.0) / acos(-1), 360); + std::fmod((delta_angle * 180.0) / acos(-1), 360); gdi_path.AddArc(center.x - rx, center.y - ry, 2.0 * rx, 2.0 * ry, start_angle_degree, delta_angle_degree); @@ -635,7 +635,7 @@ Gdiplus::Brush* Renderer::getBrush(SVGElement* shape, Gdiplus::Color(color.a, color.r, color.g, color.b)); return fill; } - return NULL; + return nullptr; } void Renderer::applyTransformsOnBrush( diff --git a/src/graphics/Line.cpp b/src/graphics/Line.cpp index 71a56a26..dd053c36 100644 --- a/src/graphics/Line.cpp +++ b/src/graphics/Line.cpp @@ -16,5 +16,5 @@ void Line::setDirection(const Vector2Df& direction) { Vector2Df Line::getDirection() const { return direction; } float Line::getLength() const { - return std::sqrt(std::pow(direction.x, 2) + std::pow(direction.y, 2)); + return std::sqrt(direction.x * direction.x + direction.y * direction.y); } \ No newline at end of file