From c4a603c20efaf84a8b019300e8a41293894b536d Mon Sep 17 00:00:00 2001 From: daniel-starke Date: Fri, 21 Apr 2017 18:14:03 +0200 Subject: [PATCH 1/3] Fixed GCC warnings --- src/nanosvg.h | 30 +++++++++++++++--------------- src/nanosvgrast.h | 34 +++++++++++++++++----------------- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/nanosvg.h b/src/nanosvg.h index 40ebac7..c49de48 100644 --- a/src/nanosvg.h +++ b/src/nanosvg.h @@ -73,30 +73,30 @@ 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 }; enum NSVGspreadType { NSVG_SPREAD_PAD = 0, NSVG_SPREAD_REFLECT = 1, - NSVG_SPREAD_REPEAT = 2, + NSVG_SPREAD_REPEAT = 2 }; enum NSVGlineJoin { NSVG_JOIN_MITER = 0, NSVG_JOIN_ROUND = 1, - NSVG_JOIN_BEVEL = 2, + NSVG_JOIN_BEVEL = 2 }; enum NSVGlineCap { NSVG_CAP_BUTT = 0, NSVG_CAP_ROUND = 1, - NSVG_CAP_SQUARE = 2, + NSVG_CAP_SQUARE = 2 }; enum NSVGfillRule { NSVG_FILLRULE_NONZERO = 0, - NSVG_FILLRULE_EVENODD = 1, + NSVG_FILLRULE_EVENODD = 1 }; enum NSVGflags { @@ -171,7 +171,7 @@ NSVGimage* nsvgParse(char* input, const char* units, float dpi); void nsvgDelete(NSVGimage* image); #ifdef __cplusplus -}; +} #endif #endif // NANOSVG_H @@ -183,7 +183,7 @@ void nsvgDelete(NSVGimage* image); #include #define NSVG_PI (3.14159265358979323846264338327f) -#define NSVG_KAPPA90 (0.5522847493f) // Lenght proportional to radius of a cubic bezier handle for 90deg arcs. +#define NSVG_KAPPA90 (0.5522847493f) // Length proportional to radius of a cubic bezier handle for 90deg arcs. #define NSVG_ALIGN_MIN 0 #define NSVG_ALIGN_MID 1 @@ -349,7 +349,7 @@ int nsvg__parseXML(char* input, enum NSVGgradientUnits { NSVG_USER_SPACE = 0, - NSVG_OBJECT_SPACE = 1, + NSVG_OBJECT_SPACE = 1 }; #define NSVG_MAX_DASHES 8 @@ -364,7 +364,7 @@ enum NSVGunits { NSVG_UNITS_IN, NSVG_UNITS_PERCENT, NSVG_UNITS_EM, - NSVG_UNITS_EX, + NSVG_UNITS_EX }; typedef struct NSVGcoordinate { @@ -921,7 +921,7 @@ static void nsvg__addShape(NSVGparser* p) { NSVGattrib* attr = nsvg__getAttr(p); float scale = 1.0f; - NSVGshape *shape, *cur, *prev; + NSVGshape *shape; NSVGpath* path; int i; @@ -936,7 +936,7 @@ static void nsvg__addShape(NSVGparser* p) scale = nsvg__getAverageScale(attr->xform); shape->strokeWidth = attr->strokeWidth * scale; shape->strokeDashOffset = attr->strokeDashOffset * scale; - shape->strokeDashCount = attr->strokeDashCount; + shape->strokeDashCount = (char)attr->strokeDashCount; for (i = 0; i < attr->strokeDashCount; i++) shape->strokeDashArray[i] = attr->strokeDashArray[i] * scale; shape->strokeLineJoin = attr->strokeLineJoin; @@ -2001,7 +2001,7 @@ static void nsvg__pathArcTo(NSVGparser* p, float* cpx, float* cpy, float* args, rx = fabsf(args[0]); // y radius ry = fabsf(args[1]); // x radius - rotx = args[2] / 180.0f * NSVG_PI; // x rotation engle + rotx = args[2] / 180.0f * NSVG_PI; // x rotation angle fa = fabsf(args[3]) > 1e-6 ? 1 : 0; // Large arc fs = fabsf(args[4]) > 1e-6 ? 1 : 0; // Sweep direction x1 = *cpx; // start point @@ -2088,7 +2088,7 @@ static void nsvg__pathArcTo(NSVGparser* p, float* cpx, float* cpy, float* args, kappa = -kappa; for (i = 0; i <= ndivs; i++) { - a = a1 + da * (i/(float)ndivs); + a = a1 + da * ((float)i/(float)ndivs); dx = cosf(a); dy = sinf(a); nsvg__xformPoint(&x, &y, dx*rx, dy*ry, t); // position @@ -2796,8 +2796,8 @@ NSVGimage* nsvgParse(char* input, const char* units, float dpi) p->dpi = dpi; nsvg__parseXML(input, nsvg__startElement, nsvg__endElement, nsvg__content, p); - - //reverse the list of shapes to match svg order + + // Reverse the list of shapes to match SVG order cur = p->image->shapes; while (cur && cur->next != NULL) { tmp = cur->next; diff --git a/src/nanosvgrast.h b/src/nanosvgrast.h index 08db3d9..2940c1f 100644 --- a/src/nanosvgrast.h +++ b/src/nanosvgrast.h @@ -64,7 +64,7 @@ void nsvgDeleteRasterizer(NSVGrasterizer*); #ifdef __cplusplus -}; +} #endif #endif // NANOSVGRAST_H @@ -239,7 +239,7 @@ static void nsvg__addPathPoint(NSVGrasterizer* r, float x, float y, int flags) if (r->npoints > 0) { pt = &r->points[r->npoints-1]; if (nsvg__ptEquals(pt->x,pt->y, x,y, r->distTol)) { - pt->flags |= flags; + pt->flags = (unsigned char)(pt->flags | flags); return; } } @@ -388,7 +388,7 @@ enum NSVGpointFlags { NSVG_PT_CORNER = 0x01, NSVG_PT_BEVEL = 0x02, - NSVG_PT_LEFT = 0x04, + NSVG_PT_LEFT = 0x04 }; static void nsvg__initClosed(NSVGpoint* left, NSVGpoint* right, NSVGpoint* p0, NSVGpoint* p1, float lineWidth) @@ -454,7 +454,7 @@ static void nsvg__roundCap(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, float lx = 0, ly = 0, rx = 0, ry = 0, prevx = 0, prevy = 0; for (i = 0; i < ncap; i++) { - float a = i/(float)(ncap-1)*NSVG_PI; + float a = (float)i/(float)(ncap-1)*NSVG_PI; float ax = cosf(a) * w, ay = sinf(a) * w; float x = px - dlx*ax - dx*ay; float y = py - dly*ax - dy*ay; @@ -551,7 +551,7 @@ static void nsvg__roundJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right if (da < NSVG_PI) da += NSVG_PI*2; if (da > NSVG_PI) da -= NSVG_PI*2; - n = (int)ceilf((nsvg__absf(da) / NSVG_PI) * ncap); + n = (int)ceilf((nsvg__absf(da) / NSVG_PI) * (float)ncap); if (n < 2) n = 2; if (n > ncap) n = ncap; @@ -561,7 +561,7 @@ static void nsvg__roundJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right ry = right->y; for (i = 0; i < n; i++) { - float u = i/(float)(n-1); + float u = (float)i/(float)(n-1); float a = a0 + u*da; float ax = cosf(a) * w, ay = sinf(a) * w; float lx1 = p1->x - ax, ly1 = p1->y - ay; @@ -838,8 +838,8 @@ static void nsvg__flattenShapeStroke(NSVGrasterizer* r, NSVGshape* shape, float static int nsvg__cmpEdge(const void *p, const void *q) { - NSVGedge* a = (NSVGedge*)p; - NSVGedge* b = (NSVGedge*)q; + const NSVGedge* a = (const NSVGedge*)p; + const NSVGedge* b = (const NSVGedge*)q; if (a->y0 < b->y0) return -1; if (a->y0 > b->y0) return 1; @@ -892,20 +892,20 @@ static void nsvg__fillScanline(unsigned char* scanline, int len, int x0, int x1, if (i < len && j >= 0) { if (i == j) { // x0,x1 are the same pixel, so compute combined coverage - scanline[i] += (unsigned char)((x1 - x0) * maxWeight >> NSVG__FIXSHIFT); + scanline[i] = (unsigned char)(scanline[i] + ((x1 - x0) * maxWeight >> NSVG__FIXSHIFT)); } else { if (i >= 0) // add antialiasing for x0 - scanline[i] += (unsigned char)(((NSVG__FIX - (x0 & NSVG__FIXMASK)) * maxWeight) >> NSVG__FIXSHIFT); + scanline[i] = (unsigned char)(scanline[i] + (((NSVG__FIX - (x0 & NSVG__FIXMASK)) * maxWeight) >> NSVG__FIXSHIFT)); else i = -1; // clip if (j < len) // add antialiasing for x1 - scanline[j] += (unsigned char)(((x1 & NSVG__FIXMASK) * maxWeight) >> NSVG__FIXSHIFT); + scanline[j] = (unsigned char)(scanline[j] + (((x1 & NSVG__FIXMASK) * maxWeight) >> NSVG__FIXSHIFT)); else j = len; // clip for (++i; i < j; ++i) // fill pixels between x0 and x1 - scanline[i] += (unsigned char)maxWeight; + scanline[i] = (unsigned char)(scanline[i] + maxWeight); } } } @@ -1021,8 +1021,8 @@ static void nsvg__scanlineSolid(unsigned char* dst, int count, unsigned char* co int i, cr, cg, cb, ca; unsigned int c; - fx = (x - tx) / scale; - fy = (y - ty) / scale; + fx = ((float)x - tx) / scale; + fy = ((float)y - ty) / scale; dx = 1.0f / scale; for (i = 0; i < count; i++) { @@ -1066,8 +1066,8 @@ static void nsvg__scanlineSolid(unsigned char* dst, int count, unsigned char* co int i, cr, cg, cb, ca; unsigned int c; - fx = (x - tx) / scale; - fy = (y - ty) / scale; + fx = ((float)x - tx) / scale; + fy = ((float)y - ty) / scale; dx = 1.0f / scale; for (i = 0; i < count; i++) { @@ -1121,7 +1121,7 @@ static void nsvg__rasterizeSortedEdges(NSVGrasterizer *r, float tx, float ty, fl xmax = 0; for (s = 0; s < NSVG__SUBSAMPLES; ++s) { // find center of pixel for this scanline - float scany = y*NSVG__SUBSAMPLES + s + 0.5f; + float scany = (float)(y*NSVG__SUBSAMPLES + s) + 0.5f; NSVGactiveEdge **step = &active; // update all active edges; From 3f40cbc53efd5ec6b9feac2d624577d6380e6831 Mon Sep 17 00:00:00 2001 From: daniel-starke Date: Fri, 21 Apr 2017 18:41:59 +0200 Subject: [PATCH 2/3] Fixed reverse shape list implementation --- src/nanosvg.h | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/nanosvg.h b/src/nanosvg.h index c49de48..b805534 100644 --- a/src/nanosvg.h +++ b/src/nanosvg.h @@ -2787,7 +2787,7 @@ NSVGimage* nsvgParse(char* input, const char* units, float dpi) { NSVGparser* p; NSVGimage* ret = 0; - NSVGshape* cur, *tmp; + NSVGshape* cur, *prev, *tmp; p = nsvg__createParser(); if (p == NULL) { @@ -2799,13 +2799,14 @@ NSVGimage* nsvgParse(char* input, const char* units, float dpi) // Reverse the list of shapes to match SVG order cur = p->image->shapes; - while (cur && cur->next != NULL) { - tmp = cur->next; - cur->next = cur->next->next; - tmp->next = p->image->shapes; - p->image->shapes = tmp; + prev = NULL; + while (cur != NULL) { + tmp = cur; cur = cur->next; + tmp->next = prev; + prev = tmp; } + if (prev != NULL) p->image->shapes = prev; // Scale to viewBox nsvg__scaleToViewbox(p, units); From 096f60a5c548e839a075288dcbad688f9b637d1c Mon Sep 17 00:00:00 2001 From: daniel-starke Date: Fri, 21 Apr 2017 22:05:26 +0200 Subject: [PATCH 3/3] Changed reverse shape list to tail cached variant --- src/nanosvg.h | 42 +++++++++++++++++------------------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/src/nanosvg.h b/src/nanosvg.h index b805534..954afda 100644 --- a/src/nanosvg.h +++ b/src/nanosvg.h @@ -145,7 +145,7 @@ typedef struct NSVGshape char strokeDashCount; // Number of dash values in dash array. char strokeLineJoin; // Stroke join type. char strokeLineCap; // Stroke cap type. - float miterLimit; // Miter limit + float miterLimit; // Miter limit char fillRule; // Fill rule, see NSVGfillRule. unsigned char flags; // Logical or of NSVG_FLAGS_* flags float bounds[4]; // Tight bounding box of the shape [minx,miny,maxx,maxy]. @@ -414,7 +414,7 @@ typedef struct NSVGattrib int strokeDashCount; char strokeLineJoin; char strokeLineCap; - float miterLimit; + float miterLimit; char fillRule; float fontSize; unsigned int stopColor; @@ -435,6 +435,7 @@ typedef struct NSVGparser NSVGpath* plist; NSVGimage* image; NSVGgradientData* gradients; + NSVGshape* shapesTail; float viewMinx, viewMiny, viewWidth, viewHeight; int alignX, alignY, alignType; float dpi; @@ -921,7 +922,7 @@ static void nsvg__addShape(NSVGparser* p) { NSVGattrib* attr = nsvg__getAttr(p); float scale = 1.0f; - NSVGshape *shape; + NSVGshape* shape; NSVGpath* path; int i; @@ -996,9 +997,12 @@ static void nsvg__addShape(NSVGparser* p) // Set flags shape->flags = (attr->visible ? NSVG_FLAGS_VISIBLE : 0x00); - // Add to head due to performance, reverse list later - shape->next = p->image->shapes; - p->image->shapes = shape; + // Add to tail + if (p->image->shapes == NULL) + p->image->shapes = shape; + else + p->shapesTail->next = shape; + p->shapesTail = shape; return; @@ -2151,23 +2155,23 @@ static void nsvg__parsePath(NSVGparser* p, const char** attr) // Moveto can be followed by multiple coordinate pairs, // which should be treated as linetos. cmd = (cmd == 'm') ? 'l' : 'L'; - rargs = nsvg__getArgsPerElement(cmd); - cpx2 = cpx; cpy2 = cpy; + rargs = nsvg__getArgsPerElement(cmd); + cpx2 = cpx; cpy2 = cpy; break; case 'l': case 'L': nsvg__pathLineTo(p, &cpx, &cpy, args, cmd == 'l' ? 1 : 0); - cpx2 = cpx; cpy2 = cpy; + cpx2 = cpx; cpy2 = cpy; break; case 'H': case 'h': nsvg__pathHLineTo(p, &cpx, &cpy, args, cmd == 'h' ? 1 : 0); - cpx2 = cpx; cpy2 = cpy; + cpx2 = cpx; cpy2 = cpy; break; case 'V': case 'v': nsvg__pathVLineTo(p, &cpx, &cpy, args, cmd == 'v' ? 1 : 0); - cpx2 = cpx; cpy2 = cpy; + cpx2 = cpx; cpy2 = cpy; break; case 'C': case 'c': @@ -2188,13 +2192,13 @@ static void nsvg__parsePath(NSVGparser* p, const char** attr) case 'A': case 'a': nsvg__pathArcTo(p, &cpx, &cpy, args, cmd == 'a' ? 1 : 0); - cpx2 = cpx; cpy2 = cpy; + cpx2 = cpx; cpy2 = cpy; break; default: if (nargs >= 2) { cpx = args[nargs-2]; cpy = args[nargs-1]; - cpx2 = cpx; cpy2 = cpy; + cpx2 = cpx; cpy2 = cpy; } break; } @@ -2787,7 +2791,6 @@ NSVGimage* nsvgParse(char* input, const char* units, float dpi) { NSVGparser* p; NSVGimage* ret = 0; - NSVGshape* cur, *prev, *tmp; p = nsvg__createParser(); if (p == NULL) { @@ -2797,17 +2800,6 @@ NSVGimage* nsvgParse(char* input, const char* units, float dpi) nsvg__parseXML(input, nsvg__startElement, nsvg__endElement, nsvg__content, p); - // Reverse the list of shapes to match SVG order - cur = p->image->shapes; - prev = NULL; - while (cur != NULL) { - tmp = cur; - cur = cur->next; - tmp->next = prev; - prev = tmp; - } - if (prev != NULL) p->image->shapes = prev; - // Scale to viewBox nsvg__scaleToViewbox(p, units);