diff options
author | Rodrigo Arias Mallo <rodarima@gmail.com> | 2024-07-27 12:52:51 +0200 |
---|---|---|
committer | Rodrigo Arias Mallo <rodarima@gmail.com> | 2024-07-27 12:54:47 +0200 |
commit | c2081d28740e03d43b9dbbe9dbd5ac6484e8953a (patch) | |
tree | eb1c8ac0965fee0139ee58db131a142b2150b4c8 /src/nanosvg.h | |
parent | 59b746f013f60050b91cb5f6f8dd4a96c47f380e (diff) |
Add SVG support for currentColor
The currentColor special value for the fill and stroke attributes allows
an image to follow the same foreground color of the surounding text.
Diffstat (limited to 'src/nanosvg.h')
-rw-r--r-- | src/nanosvg.h | 21 |
1 files changed, 12 insertions, 9 deletions
diff --git a/src/nanosvg.h b/src/nanosvg.h index e4bfa760..98b06bea 100644 --- a/src/nanosvg.h +++ b/src/nanosvg.h @@ -165,15 +165,16 @@ typedef struct NSVGimage { float width; // Width of the image. float height; // Height of the image. + unsigned current_color; // For "currentColor" NSVGshape* shapes; // Linked list of shapes in the image. } NSVGimage; // Parses SVG file from a file, returns SVG image as paths. -NSVGimage* nsvgParseFromFile(const char* filename, const char* units, float dpi); +NSVGimage* nsvgParseFromFile(const char* filename, const char* units, float dpi, unsigned current_color); // Parses SVG file from a null terminated string, returns SVG image as paths. // Important note: changes the string. -NSVGimage* nsvgParse(char* input, const char* units, float dpi); +NSVGimage* nsvgParse(char* input, const char* units, float dpi, unsigned current_color); // Duplicates a path. NSVGpath* nsvgDuplicatePath(NSVGpath* p); @@ -616,7 +617,7 @@ static void nsvg__curveBounds(float* bounds, float* curve) } } -static NSVGparser* nsvg__createParser(void) +static NSVGparser* nsvg__createParser(unsigned current_color) { NSVGparser* p; p = (NSVGparser*)malloc(sizeof(NSVGparser)); @@ -646,6 +647,8 @@ static NSVGparser* nsvg__createParser(void) /* TODO: Let the user change the initial value */ p->attr[0].fontSize = 40.0f; + p->image->current_color = current_color; + return p; error: @@ -1810,7 +1813,7 @@ static int nsvg__parseAttr(NSVGparser* p, const char* name, const char* value) nsvg__parseUrl(attr->fillGradient, value); } else if (strncmp(value, "currentColor", 12) == 0) { attr->hasFill = 1; - attr->fillColor = 0; /* TODO: Black by default */ + attr->fillColor = p->image->current_color; } else { attr->hasFill = 1; attr->fillColor = nsvg__parseColor(value); @@ -1827,7 +1830,7 @@ static int nsvg__parseAttr(NSVGparser* p, const char* name, const char* value) nsvg__parseUrl(attr->strokeGradient, value); } else if (strncmp(value, "currentColor", 12) == 0) { attr->hasStroke = 1; - attr->strokeColor = 0; /* TODO: Black by default */ + attr->strokeColor = p->image->current_color; } else { attr->hasStroke = 1; attr->strokeColor = nsvg__parseColor(value); @@ -3097,12 +3100,12 @@ static void nsvg__createGradients(NSVGparser* p) } } -NSVGimage* nsvgParse(char* input, const char* units, float dpi) +NSVGimage* nsvgParse(char* input, const char* units, float dpi, unsigned current_color) { NSVGparser* p; NSVGimage* ret = 0; - p = nsvg__createParser(); + p = nsvg__createParser(current_color); if (p == NULL) { return NULL; } @@ -3124,7 +3127,7 @@ NSVGimage* nsvgParse(char* input, const char* units, float dpi) return ret; } -NSVGimage* nsvgParseFromFile(const char* filename, const char* units, float dpi) +NSVGimage* nsvgParseFromFile(const char* filename, const char* units, float dpi, unsigned current_color) { FILE* fp = NULL; size_t size; @@ -3141,7 +3144,7 @@ NSVGimage* nsvgParseFromFile(const char* filename, const char* units, float dpi) if (fread(data, 1, size, fp) != size) goto error; data[size] = '\0'; // Must be null terminated. fclose(fp); - image = nsvgParse(data, units, dpi); + image = nsvgParse(data, units, dpi, current_color); free(data); return image; |