Another fix for issue #20: more robust matrix parsing

- separated number parsing into a function
- more robust parse logic for matrix
This commit is contained in:
Mikko Mononen 2014-11-04 15:23:41 +02:00
parent ac375ba28d
commit ae51200111
2 changed files with 52 additions and 42 deletions

View File

@ -39,14 +39,13 @@ solution "nanosvg"
targetdir("build")
configuration { "linux" }
links { "X11","Xrandr", "rt", "GL", "GLU", "pthread" }
links { "X11","Xrandr", "rt", "pthread" }
configuration { "windows" }
links { "glu32","opengl32", "gdi32", "winmm", "user32" }
links { "winmm", "user32" }
configuration { "macosx" }
links { "glfw3" }
linkoptions { "-framework OpenGL", "-framework Cocoa", "-framework IOKit", "-framework CoreVideo" }
linkoptions { "-framework Cocoa", "-framework IOKit" }
configuration "Debug"
defines { "DEBUG" }

View File

@ -903,48 +903,57 @@ error:
}
}
static const char* nsvg__parseNumber(const char* s, char* it, const int size)
{
const int last = size-1;
int i = 0;
// sign
if (*s == '-' || *s == '+') {
if (i < last) it[i++] = *s;
s++;
}
// integer part
while (*s && nsvg__isdigit(*s)) {
if (i < last) it[i++] = *s;
s++;
}
if (*s == '.') {
// decimal point
if (i < last) it[i++] = *s;
s++;
// fraction part
while (*s && nsvg__isdigit(*s)) {
if (i < last) it[i++] = *s;
s++;
}
}
// exponent
if (*s == 'e' || *s == 'E') {
if (i < last) it[i++] = *s;
s++;
if (*s == '-' || *s == '+') {
if (i < last) it[i++] = *s;
s++;
}
while (*s && nsvg__isdigit(*s)) {
if (i < last) it[i++] = *s;
s++;
}
}
it[i] = '\0';
return s;
}
static const char* nsvg__getNextPathItem(const char* s, char* it)
{
int i = 0;
it[0] = '\0';
// Skip white spaces and commas
while (*s && (nsvg__isspace(*s) || *s == ',')) s++;
if (!*s) return s;
if (*s == '-' || *s == '+' || *s == '.' || nsvg__isdigit(*s)) {
// sign
if (*s == '-' || *s == '+') {
if (i < 63) it[i++] = *s;
s++;
}
// integer part
while (*s && nsvg__isdigit(*s)) {
if (i < 63) it[i++] = *s;
s++;
}
if (*s == '.') {
// decimal point
if (i < 63) it[i++] = *s;
s++;
// fraction part
while (*s && nsvg__isdigit(*s)) {
if (i < 63) it[i++] = *s;
s++;
}
}
// exponent
if (*s == 'e' || *s == 'E') {
if (i < 63) it[i++] = *s;
s++;
if (*s == '-' || *s == '+') {
if (i < 63) it[i++] = *s;
s++;
}
while (*s && nsvg__isdigit(*s)) {
if (i < 63) it[i++] = *s;
s++;
}
}
it[i] = '\0';
s = nsvg__parseNumber(s, it, 64);
} else {
// Parse command
it[0] = *s++;
@ -1257,6 +1266,7 @@ static int nsvg__parseTransformArgs(const char* str, float* args, int maxNa, int
{
const char* end;
const char* ptr;
char it[64];
*na = 0;
ptr = str;
@ -1269,10 +1279,10 @@ static int nsvg__parseTransformArgs(const char* str, float* args, int maxNa, int
return 1;
while (ptr < end) {
if (nsvg__isnum(*ptr)) {
if (*ptr == '-' || *ptr == '+' || *ptr == '.' || nsvg__isdigit(*ptr)) {
if (*na >= maxNa) return 0;
args[(*na)++] = (float)atof(ptr);
while (ptr < end && nsvg__isnum(*ptr)) ++ptr;
ptr = nsvg__parseNumber(ptr, it, 64);
args[(*na)++] = (float)atof(it);
} else {
++ptr;
}
@ -1280,6 +1290,7 @@ static int nsvg__parseTransformArgs(const char* str, float* args, int maxNa, int
return (int)(end - str);
}
static int nsvg__parseMatrix(float* xform, const char* str)
{
float t[6];