Skip to content

Commit

Permalink
Fix parser and add transform method
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangfitus committed Nov 26, 2023
1 parent de10c11 commit 109bcd5
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 13 deletions.
51 changes: 41 additions & 10 deletions src/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Attributes Parser::parseAttributes(std::string attributes) {
}
getline(ss, value, c);
name.erase(std::remove(name.begin(), name.end(), ' '), name.end());
name.erase(std::remove(name.begin(), name.end(), '\t'), name.end());
attributes_vector.push_back(std::make_pair(name, value));
}
return attributes_vector;
Expand All @@ -68,7 +69,16 @@ Tags Parser::parseTags(std::string svg) {
std::stringstream ss(svg);
while (std::getline(ss, line)) {
int start_pos = line.find("<");
if (start_pos == std::string::npos) {
continue;
}
int end_pos = line.find(">");
while (end_pos == std::string::npos) {
std::string next_line;
std::getline(ss, next_line);
line += " " + next_line;
end_pos = line.find(">");
}
std::string tag = line.substr(start_pos + 1, end_pos - start_pos - 1);
if (tag.size() > 0) {
int space_pos = tag.find(" ");
Expand Down Expand Up @@ -364,18 +374,39 @@ float Parser::getRotate(std::string transform_value) {
return degree;
}

float getScale(std::string transform_value) {
float scale = 0;
sscanf(transform_value.c_str(), "scale(%f)", &scale);
return scale;
}

std::pair< float, float > getScaleXY(std::string transform_value) {
float scale_x = 0, scale_y = 0;
sscanf(transform_value.c_str(), "scale(%f, %f)", &scale_x, &scale_y);
return std::pair< float, float >(scale_x, scale_y);
}

void Parser::applyTransform(Shape *shape,
const std::vector< std::string > &transform_order) {
// for (auto type : transform_order) {
// if (type.find("translate") != std::string::npos) {
// float trans_x = getTranslate(type).first,
// trans_y = getTranslate(type).second;
// shape->translate(trans_x, trans_y);
// } else if (type.find("rotate") != std::string::npos) {
// float degree = getRotate(type);
// shape->rotate(degree);
// }
// }
for (auto type : transform_order) {
if (type.find("translate") != std::string::npos) {
float trans_x = getTranslate(type).first,
trans_y = getTranslate(type).second;
shape->translate(trans_x, trans_y);
} else if (type.find("rotate") != std::string::npos) {
float degree = getRotate(type);
shape->rotate(degree);
} else if (type.find("scale") != std::string::npos) {
if (type.find(",") != std::string::npos) {
float scale_x = getScaleXY(type).first,
scale_y = getScaleXY(type).second;
shape->scale(scale_x, scale_y);
} else {
float scale = getScale(type);
shape->scale(scale);
}
}
}
}

void Parser::parseLine(Attributes attributes) {
Expand Down
8 changes: 7 additions & 1 deletion src/graphics/Ellipse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ std::string Ellipse::getClass() const { return "Ellipse"; }

void Ellipse::setRadius(const Vector2Df &radius) { this->radius = radius; }

Vector2Df Ellipse::getRadius() const { return radius; }
Vector2Df Ellipse::getRadius() const { return radius; }

void Ellipse::printData() const {
Shape::printData();
std::cout << "Radius: " << getRadius().x << " " << getRadius().y
<< std::endl;
}
2 changes: 2 additions & 0 deletions src/graphics/Ellipse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class Ellipse : public Shape {
* @return The radius of the ellipse.
*/
Vector2Df getRadius() const;

void printData() const override;
};

#endif // ELLIPSE_HPP_
13 changes: 12 additions & 1 deletion src/graphics/Shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,15 @@ void Shape::printData() const {
std::cout << "Stroke width: " << getOutlineThickness() << std::endl;
std::cout << "Position: " << getPosition().x << " " << getPosition().y
<< std::endl;
}
}

void Shape::translate(float x, float y) {
position.x += x;
position.y += y;
}

void Shape::rotate(float angle) {}

void Shape::scale(float x, float y) {}

void Shape::scale(float factor) {}
10 changes: 9 additions & 1 deletion src/graphics/Shape.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ class Shape {

virtual void printData() const;

void translate(float x, float y);

void rotate(float angle);

void scale(float x, float y);

void scale(float factor);

protected:
/**
* @brief Constructs a Shape object
Expand All @@ -120,7 +128,7 @@ class Shape {
Vector2Df position; ///< Position of the shape
Vector2Df origin; ///< Origin of translation/rotation/scaling of the object
float rotation; ///< Orientation of the object, in degrees
Vector2Df scale; ///< Scale of the object
Vector2Df scale_; ///< Scale of the object
};

#endif // SHAPE_HPP_

0 comments on commit 109bcd5

Please sign in to comment.