-
Notifications
You must be signed in to change notification settings - Fork 368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nano SVG is still locale dependent #139
Comments
Reason is most likely that Here is a patch to replace them with locale independent code (using what's already available): diff --git a/src/nanosvg.h b/src/nanosvg.h
index 33fe706..5ad039f 100644
--- a/src/nanosvg.h
+++ b/src/nanosvg.h
@@ -1422,8 +1422,7 @@ static unsigned int nsvg__parseColor(const char* str)
static float nsvg__parseOpacity(const char* str)
{
- float val = 0;
- sscanf(str, "%f", &val);
+ float val = nsvg__atof(str);
if (val < 0.0f) val = 0.0f;
if (val > 1.0f) val = 1.0f;
return val;
@@ -1431,8 +1430,7 @@ static float nsvg__parseOpacity(const char* str)
static float nsvg__parseMiterLimit(const char* str)
{
- float val = 0;
- sscanf(str, "%f", &val);
+ float val = nsvg__atof(str);
if (val < 0.0f) val = 0.0f;
return val;
}
@@ -1463,9 +1461,9 @@ static int nsvg__parseUnits(const char* units)
static NSVGcoordinate nsvg__parseCoordinateRaw(const char* str)
{
NSVGcoordinate coord = {0, NSVG_UNITS_USER};
- char units[32]="";
- sscanf(str, "%f%31s", &coord.value, units);
- coord.units = nsvg__parseUnits(units);
+ char buf[64];
+ coord.units = nsvg__parseUnits(nsvg__parseNumber(str, buf, 64));
+ coord.value = nsvg__atof(buf);
return coord;
}
@@ -2505,7 +2503,25 @@ static void nsvg__parseSVG(NSVGparser* p, const char** attr)
} else if (strcmp(attr[i], "height") == 0) {
p->image->height = nsvg__parseCoordinate(p, attr[i + 1], 0.0f, 0.0f);
} else if (strcmp(attr[i], "viewBox") == 0) {
- sscanf(attr[i + 1], "%f%*[%%, \t]%f%*[%%, \t]%f%*[%%, \t]%f", &p->viewMinx, &p->viewMiny, &p->viewWidth, &p->viewHeight);
+ const char *s = attr[i + 1];
+ char buf[64];
+ s = nsvg__parseNumber(s, buf, 64);
+ p->viewMinx = nsvg__atof(buf);
+ while (*s && (nsvg__isspace(*s) || *s == '%' || *s == ',')) s++;
+ if (*s) {
+ s = nsvg__parseNumber(s, buf, 64);
+ p->viewMiny = nsvg__atof(buf);
+ while (*s && (nsvg__isspace(*s) || *s == '%' || *s == ',')) s++;
+ if (*s) {
+ s = nsvg__parseNumber(s, buf, 64);
+ p->viewWidth = nsvg__atof(buf);
+ while (*s && (nsvg__isspace(*s) || *s == '%' || *s == ',')) s++;
+ if (*s) {
+ s = nsvg__parseNumber(s, buf, 64);
+ p->viewHeight = nsvg__atof(buf);
+ }
+ }
+ }
} else if (strcmp(attr[i], "preserveAspectRatio") == 0) {
if (strstr(attr[i + 1], "none") != 0) {
// No uniform scaling
|
@wcout do you have a time to create PR from this patch? |
Fix issue #139: Nano SVG is still locale dependent
This issue could get closed now I guess. |
@memononen For your information (and for all interested users): Unfortunately commit c3ad36e, merged in PR #205 (commit 214cf85) on Apr 7, 2022 introduced a new locale dependency by using - if (sscanf(str, "rgb(%u%%, %u%%, %u%%)", &r, &g, &b) == 3) // decimal integer percentage
- return NSVG_RGB(r*255/100, g*255/100, b*255/100);
+ float rf=0, gf=0, bf=0;
+ if (sscanf(str, "rgb(%f%%, %f%%, %f%%)", &rf, &gf, &bf) == 3) // decimal integer percentage
+ return NSVG_RGB(round(rf*2.55f), round(gf*2.55f), round(bf*2.55f)); // (255 / 100.0f) Before this commit only integer percent values would be parsed correctly, now fractional percent values are allowed but the locale dependency would break parsing fractional values if used on a locale that doesn't use (This is untested: noticed by code review.) |
Yes... This old issues. xfig was already sensible to this, that files could not exchanged, as German locale uses the "," as separator, but English locale uses ".". |
@oehhar I'm working on a fix and making good progress. I'll open a PR when all my final tests succeeded. |
Please see PR #220 for my proposed fix. |
Make nsvg__parseColorRGB() independent of the current locale (#139)
Thanks, Albrecht, great! I suppose this may be closed. As a side note: I use an archaic MS-VC6++ compiler for tests and it throws the following 4 warnings: Warning 1
The related code line is: Warning 2
The related code line is: Warning 3
The related code line is: Warning 4
The related code line is: It might be ok (or not) to address them, as the rest of nanosvg does not throw any warnings. Thank you all, |
@oehhar Thanks for your comment. Yes, MSVC compilers tend to issue all sorts of these warnings. Which warning level (usually /W3 or /W4) are you using? That said, although I'm not the maintainer of this project, I intend to create another PR that should remove some kind of code duplication by implementing Unfortunately I'm currently too busy with another project but I'll come back to this when time permits. |
Dear Albrecht, Your fix is in tksvg and tk 8.7 now, thanks for that. About the warning level? It is "/W 3". For your information, the whole nmake output is below. The compiler is MSVC6 with Paltform SDK 2003SP1. Thank you again,
|
It looks like Nano SVG parser is still depends on locale.
Here is the related issue: VCVRack/Rack#461
The text was updated successfully, but these errors were encountered: