Skip to content
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

fix handling of <defs> after <g> #123

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 67 additions & 15 deletions src/nanosvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ enum NSVGpaintType {
NSVG_PAINT_NONE = 0,
NSVG_PAINT_COLOR = 1,
NSVG_PAINT_LINEAR_GRADIENT = 2,
NSVG_PAINT_RADIAL_GRADIENT = 3
NSVG_PAINT_RADIAL_GRADIENT = 3,
NSVG_PAINT_GRADIENT_LINK = 4
};

enum NSVGspreadType {
Expand Down Expand Up @@ -105,6 +106,11 @@ enum NSVGflags {
NSVG_FLAGS_VISIBLE = 0x01
};

typedef struct NSVGgradientLink {
char id[64];
float xform[6];
} NSVGgradientLink;

typedef struct NSVGgradientStop {
unsigned int color;
float offset;
Expand All @@ -123,6 +129,7 @@ typedef struct NSVGpaint {
union {
unsigned int color;
NSVGgradient* gradient;
NSVGgradientLink* gradientLink;
};
} NSVGpaint;

Expand Down Expand Up @@ -816,17 +823,29 @@ static NSVGgradientData* nsvg__findGradientData(NSVGparser* p, const char* id)
return NULL;
}

static NSVGgradient* nsvg__createGradient(NSVGparser* p, const char* id, const float* localBounds, char* paintType)
static NSVGgradientLink* nsvg__createGradientLink(const char* id, const float *xform)
{
NSVGgradientLink* grad = (NSVGgradientLink*)malloc(sizeof(NSVGgradientLink));
if (grad == NULL) return NULL;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not return NULL because nsvg__createGradient(...NULL...) will crash

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, that logic was flawed. Modified the checks for NULL in nsvg__addShape (where nsvg__createGradientLink is called) to reset type to NSVG_PAINT_NONE in this case. That way, nsvg__createGradient will not get called (nsvg__assignGradients checks for type == NSVG_PAINT_GRADIENT_LINK).

strncpy(grad->id, id, 63);
grad->id[63] = '\0';
memcpy(grad->xform, xform, sizeof(float)*6);
return grad;
}

static void nsvg__getLocalBounds(float* bounds, NSVGshape *shape, float* xform);

static NSVGgradient* nsvg__createGradient(NSVGparser* p, NSVGshape* shape, NSVGgradientLink* link, char* paintType)
{
NSVGattrib* attr = nsvg__getAttr(p);
NSVGgradientData* data = NULL;
NSVGgradientData* ref = NULL;
NSVGgradientStop* stops = NULL;
NSVGgradient* grad;
float ox, oy, sw, sh, sl;
int nstops = 0;

data = nsvg__findGradientData(p, id);
if (link == NULL) return NULL;
data = nsvg__findGradientData(p, link->id);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here what if link==NULL?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed nsvg__assignGradientsto gracefully handle type == NSVG_PAINT_GRADIENT_LINK && gradientLink == NULL (which, with the modification above, should never happen anyway, so we should be safe now).

if (data == NULL) return NULL;

// TODO: use ref to fill in all unset values too.
Expand All @@ -846,6 +865,10 @@ static NSVGgradient* nsvg__createGradient(NSVGparser* p, const char* id, const f

// The shape width and height.
if (data->units == NSVG_OBJECT_SPACE) {
float inv[6], localBounds[4];
nsvg__xformInverse(inv, link->xform);
nsvg__getLocalBounds(localBounds, shape, inv);

ox = localBounds[0];
oy = localBounds[1];
sw = localBounds[2] - localBounds[0];
Expand Down Expand Up @@ -886,7 +909,7 @@ static NSVGgradient* nsvg__createGradient(NSVGparser* p, const char* id, const f
}

nsvg__xformMultiply(grad->xform, data->xform);
nsvg__xformMultiply(grad->xform, attr->xform);
nsvg__xformMultiply(grad->xform, link->xform);

grad->spread = data->spread;
memcpy(grad->stops, stops, nstops*sizeof(NSVGgradientStop));
Expand Down Expand Up @@ -985,12 +1008,11 @@ static void nsvg__addShape(NSVGparser* p)
shape->fill.color = attr->fillColor;
shape->fill.color |= (unsigned int)(attr->fillOpacity*255) << 24;
} else if (attr->hasFill == 2) {
float inv[6], localBounds[4];
nsvg__xformInverse(inv, attr->xform);
nsvg__getLocalBounds(localBounds, shape, inv);
shape->fill.gradient = nsvg__createGradient(p, attr->fillGradient, localBounds, &shape->fill.type);
if (shape->fill.gradient == NULL) {
shape->fill.type = NSVG_PAINT_GRADIENT_LINK;
shape->fill.gradientLink = nsvg__createGradientLink(attr->fillGradient, attr->xform);
if (shape->fill.gradientLink == NULL) {
shape->fill.type = NSVG_PAINT_NONE;
goto error;
}
}

Expand All @@ -1002,12 +1024,12 @@ static void nsvg__addShape(NSVGparser* p)
shape->stroke.color = attr->strokeColor;
shape->stroke.color |= (unsigned int)(attr->strokeOpacity*255) << 24;
} else if (attr->hasStroke == 2) {
float inv[6], localBounds[4];
nsvg__xformInverse(inv, attr->xform);
nsvg__getLocalBounds(localBounds, shape, inv);
shape->stroke.gradient = nsvg__createGradient(p, attr->strokeGradient, localBounds, &shape->stroke.type);
if (shape->stroke.gradient == NULL)
shape->stroke.type = NSVG_PAINT_GRADIENT_LINK;
shape->stroke.gradientLink = nsvg__createGradientLink(attr->strokeGradient, attr->xform);
if (shape->stroke.gradientLink == NULL) {
shape->stroke.type = NSVG_PAINT_NONE;
goto error;
}
}

// Set flags
Expand Down Expand Up @@ -2742,6 +2764,34 @@ static void nsvg__content(void* ud, const char* s)
// empty
}

static void nsvg__assignGradients(NSVGparser* p)
{
for (NSVGshape* shape = p->image->shapes; shape != NULL; shape = shape->next) {
if (shape->fill.type == NSVG_PAINT_GRADIENT_LINK) {
NSVGgradientLink* link = shape->fill.gradientLink;
shape->fill.gradient = nsvg__createGradient(
p, shape, link, &shape->fill.type);
if (link != NULL) {
free(link);
}
if (shape->fill.gradient == NULL) {
shape->fill.type = NSVG_PAINT_NONE;
}
}
if (shape->stroke.type == NSVG_PAINT_GRADIENT_LINK) {
NSVGgradientLink* link = shape->stroke.gradientLink;
shape->stroke.gradient = nsvg__createGradient(
p, shape, link, &shape->stroke.type);
if (link != NULL) {
free(link);
}
if (shape->stroke.gradient == NULL) {
shape->stroke.type = NSVG_PAINT_NONE;
}
}
}
}

static void nsvg__imageBounds(NSVGparser* p, float* bounds)
{
NSVGshape* shape;
Expand Down Expand Up @@ -2886,6 +2936,8 @@ NSVGimage* nsvgParse(char* input, const char* units, float dpi)

nsvg__parseXML(input, nsvg__startElement, nsvg__endElement, nsvg__content, p);

nsvg__assignGradients(p);

// Scale to viewBox
nsvg__scaleToViewbox(p, units);

Expand Down