From 4b17261f97150456ba0a6c56477d5c4e148d4fcc Mon Sep 17 00:00:00 2001 From: Micah Elizabeth Scott Date: Sat, 24 Jan 2015 20:23:05 -0800 Subject: [PATCH] Keep the 'id' attribute for shapes This remembers the 'id' string for shapes (or their parent groups). I find this useful for storing pertinent names or metadata in the shape's layer name in Illustrator. When saving the document as SVG, that string ends up in the 'id' attribute. The library was already saving 'id' attributes for gradients. I didn't see an elegant way to combine that mechanism with this patch, but the small change in attr parsing order should allow the two to coexist. --- src/nanosvg.h | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/nanosvg.h b/src/nanosvg.h index 14a890f..41c5571 100644 --- a/src/nanosvg.h +++ b/src/nanosvg.h @@ -126,6 +126,7 @@ typedef struct NSVGpath typedef struct NSVGshape { + char id[64]; // Optional 'id' attr of the shape or its group NSVGpaint fill; // Fill paint NSVGpaint stroke; // Stroke paint float opacity; // Opacity of the shape. @@ -360,6 +361,7 @@ typedef struct NSVGgradientData typedef struct NSVGattrib { + char id[64]; float xform[6]; unsigned int fillColor; unsigned int strokeColor; @@ -567,6 +569,7 @@ static NSVGparser* nsvg__createParser() // Init style nsvg__xformIdentity(p->attr[0].xform); + memset(p->attr[0].id, 0, sizeof p->attr[0].id); p->attr[0].fillColor = NSVG_RGB(0,0,0); p->attr[0].strokeColor = NSVG_RGB(0,0,0); p->attr[0].opacity = 1; @@ -788,6 +791,7 @@ static void nsvg__addShape(NSVGparser* p) if (shape == NULL) goto error; memset(shape, 0, sizeof(NSVGshape)); + memcpy(shape->id, attr->id, sizeof shape->id); scale = nsvg__getAverageScale(attr->xform); shape->strokeWidth = attr->strokeWidth * scale; shape->strokeLineJoin = attr->strokeLineJoin; @@ -1506,6 +1510,9 @@ static int nsvg__parseAttr(NSVGparser* p, const char* name, const char* value) attr->stopOpacity = nsvg__parseFloat(NULL, value, 2); } else if (strcmp(name, "offset") == 0) { attr->stopOffset = nsvg__parseFloat(NULL, value, 2); + } else if (strcmp(name, "id") == 0) { + strncpy(attr->id, value, 63); + attr->id[63] = '\0'; } else { return 0; } @@ -2276,7 +2283,10 @@ static void nsvg__parseGradient(NSVGparser* p, const char** attr, char type) // TODO: does not handle percent and objectBoundingBox correctly yet. for (i = 0; attr[i]; i += 2) { - if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) { + if (strcmp(attr[i], "id") == 0) { + strncpy(grad->id, attr[i+1], 63); + grad->id[63] = '\0'; + } else if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) { if (strcmp(attr[i], "gradientUnits") == 0) { if (strcmp(attr[i+1], "objectBoundingBox") == 0) grad->units = NSVG_OBJECT_SPACE; @@ -2312,9 +2322,6 @@ static void nsvg__parseGradient(NSVGparser* p, const char** attr, char type) } else if (strcmp(attr[i], "xlink:href") == 0) { strncpy(grad->ref, attr[i+1], 63); grad->ref[63] = '\0'; - } else if (strcmp(attr[i], "id") == 0) { - strncpy(grad->id, attr[i+1], 63); - grad->id[63] = '\0'; } } }