Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Mygod/f953530fc3cf43a1aeb44771fa3868cb to your computer and use it in GitHub Desktop.
Save Mygod/f953530fc3cf43a1aeb44771fa3868cb to your computer and use it in GitHub Desktop.
SVGParser comma and whitespace enhancement for CS 184/284A spring 2018
From a818cb9ca7d4746b5508943ba6dee931ce3aa3ef Mon Sep 17 00:00:00 2001
From: Mygod <contact-git@mygod.be>
Date: Sun, 4 Feb 2018 16:59:49 -0800
Subject: [PATCH] SVGParser comma and whitespace enhancement
Please see explanation here: https://piazza.com/class/jcawl9n5m3s4s3?cid=63
---
src/svgparser.cpp | 59 ++++++++++++++++++++++++++++++++++++++-----------------
1 file changed, 41 insertions(+), 18 deletions(-)
diff --git a/src/svgparser.cpp b/src/svgparser.cpp
index 54f5645..50b2dfd 100644
--- a/src/svgparser.cpp
+++ b/src/svgparser.cpp
@@ -14,7 +14,32 @@
using namespace std;
-namespace CGL {
+namespace CGL {
+
+class svgstream : public virtual stringstream {
+public:
+ svgstream(const string &s) : stringstream(s) {
+ imbue(locale(getloc(), new comma_wsp()));
+ }
+private:
+ // list-of-Ts ::= T
+ // | T comma-wsp list-of-Ts
+ // comma-wsp ::= (wsp+ ","? wsp*) | ("," wsp*)
+ // wsp ::= (#x20 | #x9 | #xD | #xA)
+ struct comma_wsp : ctype<char> {
+ static mask const* get_table() {
+ static mask table[ctype::table_size];
+ table[' '] = std::ctype_base::space;
+ table['\t'] = std::ctype_base::space;
+ table['\r'] = std::ctype_base::space;
+ table['\n'] = std::ctype_base::space;
+ table[','] = std::ctype_base::space;
+ return table;
+ }
+ comma_wsp() : ctype<char>(get_table()) {}
+ };
+};
+
SVG *SVGParser::curr_svg;
string SVGParser::dir;
@@ -199,7 +224,7 @@ void SVGParser::parseElement( XMLElement* xml, SVGElement* element ) {
string matrix_str = data;
replace( matrix_str.begin(), matrix_str.end(), ',', ' ');
- stringstream ss (matrix_str);
+ svgstream ss (matrix_str);
float a, b, c, d, e, f;
ss >> a >> b >> c >> d >> e >> f;
@@ -208,8 +233,8 @@ void SVGParser::parseElement( XMLElement* xml, SVGElement* element ) {
0,0,1);
} else if ( type == "translate" ) {
-
- stringstream ss (data);
+
+ svgstream ss (data);
float x; if (!(ss >> x)) x = 0;
float y; if (!(ss >> y)) y = 0;
@@ -217,7 +242,7 @@ void SVGParser::parseElement( XMLElement* xml, SVGElement* element ) {
} else if (type == "scale" ) {
- stringstream ss (data);
+ svgstream ss (data);
float x; if (!(ss >> x)) x = 1;
float y; if (!(ss >> y)) y = 1;
@@ -225,7 +250,7 @@ void SVGParser::parseElement( XMLElement* xml, SVGElement* element ) {
} else if (type == "rotate") {
- stringstream ss (data);
+ svgstream ss (data);
float a; if (!(ss >> a)) a = 0;
float x; if (!(ss >> x)) x = 0;
float y; if (!(ss >> y)) y = 0;
@@ -234,14 +259,14 @@ void SVGParser::parseElement( XMLElement* xml, SVGElement* element ) {
} else if (type == "skewX" ) {
- stringstream ss (data);
+ svgstream ss (data);
float a; ss >> a;
m(0,1) = tan(a*PI/180.0f);
} else if (type == "skewY" ) {
- stringstream ss (data);
+ svgstream ss (data);
float a; ss >> a;
m(1,0) = tan(a*PI/180.0f);
@@ -292,12 +317,11 @@ void SVGParser::parseLine( XMLElement* xml, Line* line ) {
void SVGParser::parsePolyline( XMLElement* xml, Polyline* polyline ) {
- stringstream points (xml->Attribute( "points" ));
+ svgstream points (xml->Attribute( "points" ));
float x, y;
- char c;
- while( points >> x >> c >> y ) {
+ while( points >> x >> y ) {
polyline->points.push_back( Vector2D( x, y ) );
}
}
@@ -311,12 +335,11 @@ void SVGParser::parseRect( XMLElement* xml, Rect* rect ) {
void SVGParser::parsePolygon( XMLElement* xml, Polygon* polygon ) {
- stringstream points (xml->Attribute( "points" ));
+ svgstream points (xml->Attribute( "points" ));
float x, y;
- char c;
- while( points >> x >> c >> y ) {
+ while( points >> x >> y ) {
polygon->points.push_back( Vector2D( x, y ) );
}
}
@@ -460,14 +483,14 @@ void SVGParser::parseGroup( XMLElement* xml, Group* group ) {
void SVGParser::parseColorTri( XMLElement* xml, ColorTri* ctri ) {
- stringstream points (xml->Attribute( "points" ));
+ svgstream points (xml->Attribute( "points" ));
float x, y;
points >> x >> y; ctri->p0_svg = Vector2D(x,y);
points >> x >> y; ctri->p1_svg = Vector2D(x,y);
points >> x >> y; ctri->p2_svg = Vector2D(x,y);
- stringstream colors (xml->Attribute( "colors" ));
+ svgstream colors (xml->Attribute( "colors" ));
float r,g,b,a;
colors >> r >> g >> b >> a; ctri->p0_col = Color(r,g,b,a);
@@ -478,14 +501,14 @@ void SVGParser::parseColorTri( XMLElement* xml, ColorTri* ctri ) {
void SVGParser::parseTexTri( XMLElement* xml, TexTri* ttri ) {
- stringstream points (xml->Attribute( "points" ));
+ svgstream points (xml->Attribute( "points" ));
float x, y;
points >> x >> y; ttri->p0_svg = Vector2D(x,y);
points >> x >> y; ttri->p1_svg = Vector2D(x,y);
points >> x >> y; ttri->p2_svg = Vector2D(x,y);
- stringstream uvs (xml->Attribute( "uvs" ));
+ svgstream uvs (xml->Attribute( "uvs" ));
uvs >> x >> y; ttri->p0_uv = Vector2D(x,y);
uvs >> x >> y; ttri->p1_uv = Vector2D(x,y);
--
2.7.4
# IMPORTANT: save uncommited changes before doing these!
git checkout -b svg-fix
git reset --hard 14f6354fa98240d1f814aecc3b2bc22d8bbb9254
git am < 0001-SVGParser-comma-and-whitespace-enhancement.patch
git checkout master # or whatever branch ur working on
git cherry-pick svg-fix # you could also use merge
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment