From 80c44d74adad093575fc820956a9b9f73b5e158e Mon Sep 17 00:00:00 2001 From: Bruno Van de Velde Date: Sat, 9 Mar 2019 22:35:08 +0100 Subject: [PATCH] Removed warnings from nanosvg when compiling TGUI --- .codecov.yml | 1 + include/TGUI/SvgImage.hpp | 6 +- include/TGUI/nanosvg/nanosvg.h | 812 ++++++++++++++--------------- include/TGUI/nanosvg/nanosvgrast.h | 544 +++++++++---------- src/TGUI/SvgImage.cpp | 6 +- 5 files changed, 685 insertions(+), 684 deletions(-) diff --git a/.codecov.yml b/.codecov.yml index 1c64a693..3dde8c45 100644 --- a/.codecov.yml +++ b/.codecov.yml @@ -11,3 +11,4 @@ ignore: - tests/.* - examples/.* - include/TGUI/Aurora/.* +- include/TGUI/nanosvg/.* diff --git a/include/TGUI/SvgImage.hpp b/include/TGUI/SvgImage.hpp index 424582da..c53801f9 100644 --- a/include/TGUI/SvgImage.hpp +++ b/include/TGUI/SvgImage.hpp @@ -32,11 +32,11 @@ ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -class NSVGimage; -class NSVGrasterizer; - namespace tgui { + class NSVGimage; + class NSVGrasterizer; + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @internal ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/include/TGUI/nanosvg/nanosvg.h b/include/TGUI/nanosvg/nanosvg.h index eaf29d15..187dc916 100644 --- a/include/TGUI/nanosvg/nanosvg.h +++ b/include/TGUI/nanosvg/nanosvg.h @@ -1,4 +1,7 @@ -// Some changes have been made to the file to remove compiler warnings. +// Some modifications have been made to the original file: +// - Code was placed in a "tgui" namespace to avoid conflicts if a TGUI user also uses NanoSVG in their project +// - All floats were replaced by doubles (to get rid of warnings about implicit conversions and promotions) +// - Some other c++ compiler warnings were fixed, mainly about usage of old-style casts /* * Copyright (c) 2013-14 Mikko Mononen memon@inside.org @@ -31,11 +34,8 @@ #ifndef NANOSVG_H #define NANOSVG_H -#ifndef NANOSVG_CPLUSPLUS -#ifdef __cplusplus -extern "C" { -#endif -#endif +namespace tgui +{ // NanoSVG is a simple stupid single-header-file SVG parse. The output of the parser is a list of cubic bezier shapes. // @@ -61,10 +61,10 @@ extern "C" { image = nsvgParseFromFile("test.svg", "px", 96); printf("size: %f x %f\n", image->width, image->height); // Use... - for (NSVGshape *shape = image->shapes; shape != NULL; shape = shape->next) { - for (NSVGpath *path = shape->paths; path != NULL; path = path->next) { + for (NSVGshape *shape = image->shapes; shape != nullptr; shape = shape->next) { + for (NSVGpath *path = shape->paths; path != nullptr; path = path->next) { for (int i = 0; i < path->npts-1; i += 3) { - float* p = &path->pts[i*2]; + double* p = &path->pts[i*2]; drawCubicBez(p[0],p[1], p[2],p[3], p[4],p[5], p[6],p[7]); } } @@ -109,13 +109,13 @@ enum NSVGflags { typedef struct NSVGgradientStop { unsigned int color; - float offset; + double offset; } NSVGgradientStop; typedef struct NSVGgradient { - float xform[6]; + double xform[6]; char spread; - float fx, fy; + double fx, fy; int nstops; NSVGgradientStop stops[1]; } NSVGgradient; @@ -130,11 +130,11 @@ typedef struct NSVGpaint { typedef struct NSVGpath { - float* pts; // Cubic bezier points: x0,y0, [cpx1,cpx1,cpx2,cpy2,x1,y1], ... + double* pts; // Cubic bezier points: x0,y0, [cpx1,cpx1,cpx2,cpy2,x1,y1], ... int npts; // Total number of bezier points. char closed; // Flag indicating if shapes should be treated as closed. - float bounds[4]; // Tight bounding box of the shape [minx,miny,maxx,maxy]. - struct NSVGpath* next; // Pointer to next path, or NULL if last element. + double bounds[4]; // Tight bounding box of the shape [minx,miny,maxx,maxy]. + struct NSVGpath* next; // Pointer to next path, or nullptr if last element. } NSVGpath; typedef struct NSVGshape @@ -142,34 +142,34 @@ 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. - float strokeWidth; // Stroke width (scaled). - float strokeDashOffset; // Stroke dash offset (scaled). - float strokeDashArray[8]; // Stroke dash array (scaled). - char strokeDashCount; // Number of dash values in dash array. + double opacity; // Opacity of the shape. + double strokeWidth; // Stroke width (scaled). + double strokeDashOffset; // Stroke dash offset (scaled). + double strokeDashArray[8]; // Stroke dash array (scaled). + char strokeDashCount; // Number of dash values in dash array. char strokeLineJoin; // Stroke join type. char strokeLineCap; // Stroke cap type. - float miterLimit; // Miter limit + double 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]. + double bounds[4]; // Tight bounding box of the shape [minx,miny,maxx,maxy]. NSVGpath* paths; // Linked list of paths in the image. - struct NSVGshape* next; // Pointer to next shape, or NULL if last element. + struct NSVGshape* next; // Pointer to next shape, or nullptr if last element. } NSVGshape; typedef struct NSVGimage { - float width; // Width of the image. - float height; // Height of the image. + double width; // Width of the image. + double height; // Height of the image. 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, double dpi); // 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, double dpi); // Duplicates a path. NSVGpath* nsvgDuplicatePath(NSVGpath* p); @@ -177,11 +177,7 @@ NSVGpath* nsvgDuplicatePath(NSVGpath* p); // Deletes an image. void nsvgDelete(NSVGimage* image); -#ifndef NANOSVG_CPLUSPLUS -#ifdef __cplusplus } -#endif -#endif #endif // NANOSVG_H @@ -191,8 +187,11 @@ void nsvgDelete(NSVGimage* image); #include #include -#define NSVG_PI (3.14159265358979323846264338327f) -#define NSVG_KAPPA90 (0.5522847493f) // Length proportional to radius of a cubic bezier handle for 90deg arcs. +namespace tgui +{ + +#define NSVG_PI (3.14159265358979323846264338327) +#define NSVG_KAPPA90 (0.5522847493) // Length proportional to radius of a cubic bezier handle for 90deg arcs. #define NSVG_ALIGN_MIN 0 #define NSVG_ALIGN_MID 1 @@ -202,7 +201,7 @@ void nsvgDelete(NSVGimage* image); #define NSVG_ALIGN_SLICE 2 #define NSVG_NOTUSED(v) do { (void)(1 ? (void)0 : ( (void)(v) ) ); } while(0) -#define NSVG_RGB(r, g, b) (((unsigned int)r) | ((unsigned int)g << 8) | ((unsigned int)b << 16)) +#define NSVG_RGB(r, g, b) ((static_cast(r)) | (static_cast(g) << 8) | (static_cast(b) << 16)) #ifdef _MSC_VER #pragma warning (disable: 4996) // Switch off security warnings @@ -232,8 +231,8 @@ static int nsvg__isnum(char c) return strchr("0123456789+-.eE", c) != nullptr; } -static NSVG_INLINE float nsvg__minf(float a, float b) { return a < b ? a : b; } -static NSVG_INLINE float nsvg__maxf(float a, float b) { return a > b ? a : b; } +static NSVG_INLINE double nsvg__minf(double a, double b) { return a < b ? a : b; } +static NSVG_INLINE double nsvg__maxf(double a, double b) { return a > b ? a : b; } // Simple XML parser @@ -386,7 +385,7 @@ enum NSVGunits { }; typedef struct NSVGcoordinate { - float value; + double value; int units; } NSVGcoordinate; @@ -409,7 +408,7 @@ typedef struct NSVGgradientData }; char spread; char units; - float xform[6]; + double xform[6]; int nstops; NSVGgradientStop* stops; struct NSVGgradientData* next; @@ -418,26 +417,26 @@ typedef struct NSVGgradientData typedef struct NSVGattrib { char id[64]; - float xform[6]; + double xform[6]; unsigned int fillColor; unsigned int strokeColor; - float opacity; - float fillOpacity; - float strokeOpacity; + double opacity; + double fillOpacity; + double strokeOpacity; char fillGradient[64]; char strokeGradient[64]; - float strokeWidth; - float strokeDashOffset; - float strokeDashArray[NSVG_MAX_DASHES]; + double strokeWidth; + double strokeDashOffset; + double strokeDashArray[NSVG_MAX_DASHES]; int strokeDashCount; char strokeLineJoin; char strokeLineCap; - float miterLimit; + double miterLimit; char fillRule; - float fontSize; + double fontSize; unsigned int stopColor; - float stopOpacity; - float stopOffset; + double stopOpacity; + double stopOffset; char hasFill; char hasStroke; char visible; @@ -447,68 +446,68 @@ typedef struct NSVGparser { NSVGattrib attr[NSVG_MAX_ATTR]; int attrHead; - float* pts; + double* pts; int npts; int cpts; NSVGpath* plist; NSVGimage* image; NSVGgradientData* gradients; NSVGshape* shapesTail; - float viewMinx, viewMiny, viewWidth, viewHeight; + double viewMinx, viewMiny, viewWidth, viewHeight; int alignX, alignY, alignType; - float dpi; + double dpi; char pathFlag; char defsFlag; } NSVGparser; -static void nsvg__xformIdentity(float* t) +static void nsvg__xformIdentity(double* t) { - t[0] = 1.0f; t[1] = 0.0f; - t[2] = 0.0f; t[3] = 1.0f; - t[4] = 0.0f; t[5] = 0.0f; + t[0] = 1.0; t[1] = 0.0; + t[2] = 0.0; t[3] = 1.0; + t[4] = 0.0; t[5] = 0.0; } -static void nsvg__xformSetTranslation(float* t, float tx, float ty) +static void nsvg__xformSetTranslation(double* t, double tx, double ty) { - t[0] = 1.0f; t[1] = 0.0f; - t[2] = 0.0f; t[3] = 1.0f; + t[0] = 1.0; t[1] = 0.0; + t[2] = 0.0; t[3] = 1.0; t[4] = tx; t[5] = ty; } -static void nsvg__xformSetScale(float* t, float sx, float sy) +static void nsvg__xformSetScale(double* t, double sx, double sy) { - t[0] = sx; t[1] = 0.0f; - t[2] = 0.0f; t[3] = sy; - t[4] = 0.0f; t[5] = 0.0f; + t[0] = sx; t[1] = 0.0; + t[2] = 0.0; t[3] = sy; + t[4] = 0.0; t[5] = 0.0; } -static void nsvg__xformSetSkewX(float* t, float a) +static void nsvg__xformSetSkewX(double* t, double a) { - t[0] = 1.0f; t[1] = 0.0f; - t[2] = tanf(a); t[3] = 1.0f; - t[4] = 0.0f; t[5] = 0.0f; + t[0] = 1.0; t[1] = 0.0; + t[2] = tan(a); t[3] = 1.0; + t[4] = 0.0; t[5] = 0.0; } -static void nsvg__xformSetSkewY(float* t, float a) +static void nsvg__xformSetSkewY(double* t, double a) { - t[0] = 1.0f; t[1] = tanf(a); - t[2] = 0.0f; t[3] = 1.0f; - t[4] = 0.0f; t[5] = 0.0f; + t[0] = 1.0; t[1] = tan(a); + t[2] = 0.0; t[3] = 1.0; + t[4] = 0.0; t[5] = 0.0; } -static void nsvg__xformSetRotation(float* t, float a) +static void nsvg__xformSetRotation(double* t, double a) { - float cs = cosf(a), sn = sinf(a); + double cs = cos(a), sn = sin(a); t[0] = cs; t[1] = sn; t[2] = -sn; t[3] = cs; - t[4] = 0.0f; t[5] = 0.0f; + t[4] = 0.0; t[5] = 0.0; } -static void nsvg__xformMultiply(float* t, float* s) +static void nsvg__xformMultiply(double* t, double* s) { - float t0 = t[0] * s[0] + t[1] * s[2]; - float t2 = t[2] * s[0] + t[3] * s[2]; - float t4 = t[4] * s[0] + t[5] * s[2] + s[4]; + double t0 = t[0] * s[0] + t[1] * s[2]; + double t2 = t[2] * s[0] + t[3] * s[2]; + double t4 = t[4] * s[0] + t[5] * s[2] + s[4]; t[1] = t[0] * s[1] + t[1] * s[3]; t[3] = t[2] * s[1] + t[3] * s[3]; t[5] = t[4] * s[1] + t[5] * s[3] + s[5]; @@ -517,37 +516,37 @@ static void nsvg__xformMultiply(float* t, float* s) t[4] = t4; } -static void nsvg__xformInverse(float* inv, float* t) +static void nsvg__xformInverse(double* inv, double* t) { - double invdet, det = (double)t[0] * t[3] - (double)t[2] * t[1]; + double invdet, det = t[0] * t[3] - t[2] * t[1]; if (det > -1e-6 && det < 1e-6) { nsvg__xformIdentity(t); return; } invdet = 1.0 / det; - inv[0] = (float)(t[3] * invdet); - inv[2] = (float)(-t[2] * invdet); - inv[4] = (float)(((double)t[2] * t[5] - (double)t[3] * t[4]) * invdet); - inv[1] = (float)(-t[1] * invdet); - inv[3] = (float)(t[0] * invdet); - inv[5] = (float)(((double)t[1] * t[4] - (double)t[0] * t[5]) * invdet); + inv[0] = t[3] * invdet; + inv[2] = -t[2] * invdet; + inv[4] = (t[2] * t[5] - t[3] * t[4]) * invdet; + inv[1] = -t[1] * invdet; + inv[3] = t[0] * invdet; + inv[5] = (t[1] * t[4] - t[0] * t[5]) * invdet; } -static void nsvg__xformPremultiply(float* t, float* s) +static void nsvg__xformPremultiply(double* t, double* s) { - float s2[6]; - memcpy(s2, s, sizeof(float)*6); + double s2[6]; + memcpy(s2, s, sizeof(double)*6); nsvg__xformMultiply(s2, t); - memcpy(t, s2, sizeof(float)*6); + memcpy(t, s2, sizeof(double)*6); } -static void nsvg__xformPoint(float* dx, float* dy, float x, float y, float* t) +static void nsvg__xformPoint(double* dx, double* dy, double x, double y, double* t) { *dx = x*t[0] + y*t[2] + t[4]; *dy = x*t[1] + y*t[3] + t[5]; } -static void nsvg__xformVec(float* dx, float* dy, float x, float y, float* t) +static void nsvg__xformVec(double* dx, double* dy, double x, double y, double* t) { *dx = x*t[0] + y*t[2]; *dy = x*t[1] + y*t[3]; @@ -555,7 +554,7 @@ static void nsvg__xformVec(float* dx, float* dy, float x, float y, float* t) #define NSVG_EPSILON (1e-12) -static int nsvg__ptInBounds(float* pt, float* bounds) +static int nsvg__ptInBounds(double* pt, double* bounds) { return pt[0] >= bounds[0] && pt[0] <= bounds[2] && pt[1] >= bounds[1] && pt[1] <= bounds[3]; } @@ -567,14 +566,14 @@ static double nsvg__evalBezier(double t, double p0, double p1, double p2, double return it*it*it*p0 + 3.0*it*it*t*p1 + 3.0*it*t*t*p2 + t*t*t*p3; } -static void nsvg__curveBounds(float* bounds, float* curve) +static void nsvg__curveBounds(double* bounds, double* curve) { int i, j, count; double roots[2], a, b, c, b2ac, t, v; - float* v0 = &curve[0]; - float* v1 = &curve[2]; - float* v2 = &curve[4]; - float* v3 = &curve[6]; + double* v0 = &curve[0]; + double* v1 = &curve[2]; + double* v2 = &curve[4]; + double* v3 = &curve[6]; // Start the bounding box by end points bounds[0] = nsvg__minf(v0[0], v3[0]); @@ -612,8 +611,8 @@ static void nsvg__curveBounds(float* bounds, float* curve) } for (j = 0; j < count; j++) { v = nsvg__evalBezier(roots[j], v0[i], v1[i], v2[i], v3[i]); - bounds[0+i] = nsvg__minf(bounds[0+i], (float)v); - bounds[2+i] = nsvg__maxf(bounds[2+i], (float)v); + bounds[0+i] = nsvg__minf(bounds[0+i], v); + bounds[2+i] = nsvg__maxf(bounds[2+i], v); } } } @@ -621,12 +620,12 @@ static void nsvg__curveBounds(float* bounds, float* curve) static NSVGparser* nsvg__createParser() { NSVGparser* p; - p = (NSVGparser*)malloc(sizeof(NSVGparser)); - if (p == NULL) goto error; + p = static_cast(malloc(sizeof(NSVGparser))); + if (p == nullptr) goto error; memset(p, 0, sizeof(NSVGparser)); - p->image = (NSVGimage*)malloc(sizeof(NSVGimage)); - if (p->image == NULL) goto error; + p->image = static_cast(malloc(sizeof(NSVGimage))); + if (p->image == nullptr) goto error; memset(p->image, 0, sizeof(NSVGimage)); // Init style @@ -653,14 +652,14 @@ error: if (p->image) free(p->image); free(p); } - return NULL; + return nullptr; } static void nsvg__deletePaths(NSVGpath* path) { while (path) { NSVGpath *next = path->next; - if (path->pts != NULL) + if (path->pts != nullptr) free(path->pts); free(path); path = next; @@ -676,7 +675,7 @@ static void nsvg__deletePaint(NSVGpaint* paint) static void nsvg__deleteGradientData(NSVGgradientData* grad) { NSVGgradientData* next; - while (grad != NULL) { + while (grad != nullptr) { next = grad->next; free(grad->stops); free(grad); @@ -686,7 +685,7 @@ static void nsvg__deleteGradientData(NSVGgradientData* grad) static void nsvg__deleteParser(NSVGparser* p) { - if (p != NULL) { + if (p != nullptr) { nsvg__deletePaths(p->plist); nsvg__deleteGradientData(p->gradients); nsvgDelete(p->image); @@ -700,11 +699,11 @@ static void nsvg__resetPath(NSVGparser* p) p->npts = 0; } -static void nsvg__addPoint(NSVGparser* p, float x, float y) +static void nsvg__addPoint(NSVGparser* p, double x, double y) { if (p->npts+1 > p->cpts) { p->cpts = p->cpts ? p->cpts*2 : 8; - p->pts = (float*)realloc(p->pts, p->cpts*2*sizeof(float)); + p->pts = static_cast(realloc(p->pts, p->cpts*2*sizeof(double))); if (!p->pts) return; } p->pts[p->npts*2+0] = x; @@ -712,7 +711,7 @@ static void nsvg__addPoint(NSVGparser* p, float x, float y) p->npts++; } -static void nsvg__moveTo(NSVGparser* p, float x, float y) +static void nsvg__moveTo(NSVGparser* p, double x, double y) { if (p->npts > 0) { p->pts[(p->npts-1)*2+0] = x; @@ -722,21 +721,21 @@ static void nsvg__moveTo(NSVGparser* p, float x, float y) } } -static void nsvg__lineTo(NSVGparser* p, float x, float y) +static void nsvg__lineTo(NSVGparser* p, double x, double y) { - float px,py, dx,dy; + double px,py, dx,dy; if (p->npts > 0) { px = p->pts[(p->npts-1)*2+0]; py = p->pts[(p->npts-1)*2+1]; dx = x - px; dy = y - py; - nsvg__addPoint(p, px + dx/3.0f, py + dy/3.0f); - nsvg__addPoint(p, x - dx/3.0f, y - dy/3.0f); + nsvg__addPoint(p, px + dx/3.0, py + dy/3.0); + nsvg__addPoint(p, x - dx/3.0, y - dy/3.0); nsvg__addPoint(p, x, y); } } -static void nsvg__cubicBezTo(NSVGparser* p, float cpx1, float cpy1, float cpx2, float cpy2, float x, float y) +static void nsvg__cubicBezTo(NSVGparser* p, double cpx1, double cpy1, double cpx2, double cpy2, double x, double y) { nsvg__addPoint(p, cpx1, cpy1); nsvg__addPoint(p, cpx2, cpy2); @@ -762,46 +761,46 @@ static void nsvg__popAttr(NSVGparser* p) p->attrHead--; } -static float nsvg__actualOrigX(NSVGparser* p) +static double nsvg__actualOrigX(NSVGparser* p) { return p->viewMinx; } -static float nsvg__actualOrigY(NSVGparser* p) +static double nsvg__actualOrigY(NSVGparser* p) { return p->viewMiny; } -static float nsvg__actualWidth(NSVGparser* p) +static double nsvg__actualWidth(NSVGparser* p) { return p->viewWidth; } -static float nsvg__actualHeight(NSVGparser* p) +static double nsvg__actualHeight(NSVGparser* p) { return p->viewHeight; } -static float nsvg__actualLength(NSVGparser* p) +static double nsvg__actualLength(NSVGparser* p) { - float w = nsvg__actualWidth(p), h = nsvg__actualHeight(p); - return sqrtf(w*w + h*h) / sqrtf(2.0f); + double w = nsvg__actualWidth(p), h = nsvg__actualHeight(p); + return sqrt(w*w + h*h) / sqrt(2.0); } -static float nsvg__convertToPixels(NSVGparser* p, NSVGcoordinate c, float orig, float length) +static double nsvg__convertToPixels(NSVGparser* p, NSVGcoordinate c, double orig, double length) { NSVGattrib* attr = nsvg__getAttr(p); switch (c.units) { case NSVG_UNITS_USER: return c.value; case NSVG_UNITS_PX: return c.value; - case NSVG_UNITS_PT: return c.value / 72.0f * p->dpi; - case NSVG_UNITS_PC: return c.value / 6.0f * p->dpi; - case NSVG_UNITS_MM: return c.value / 25.4f * p->dpi; - case NSVG_UNITS_CM: return c.value / 2.54f * p->dpi; + case NSVG_UNITS_PT: return c.value / 72.0 * p->dpi; + case NSVG_UNITS_PC: return c.value / 6.0 * p->dpi; + case NSVG_UNITS_MM: return c.value / 25.4 * p->dpi; + case NSVG_UNITS_CM: return c.value / 2.54 * p->dpi; case NSVG_UNITS_IN: return c.value * p->dpi; case NSVG_UNITS_EM: return c.value * attr->fontSize; - case NSVG_UNITS_EX: return c.value * attr->fontSize * 0.52f; // x-height of Helvetica. - case NSVG_UNITS_PERCENT: return orig + c.value / 100.0f * length; + case NSVG_UNITS_EX: return c.value * attr->fontSize * 0.52; // x-height of Helvetica. + case NSVG_UNITS_PERCENT: return orig + c.value / 100.0 * length; default: return c.value; } return c.value; @@ -815,36 +814,36 @@ static NSVGgradientData* nsvg__findGradientData(NSVGparser* p, const char* id) return grad; grad = grad->next; } - return NULL; + return nullptr; } -static NSVGgradient* nsvg__createGradient(NSVGparser* p, const char* id, const float* localBounds, char* paintType) +static NSVGgradient* nsvg__createGradient(NSVGparser* p, const char* id, const double* localBounds, char* paintType) { NSVGattrib* attr = nsvg__getAttr(p); - NSVGgradientData* data = NULL; - NSVGgradientData* ref = NULL; - NSVGgradientStop* stops = NULL; + NSVGgradientData* data = nullptr; + NSVGgradientData* ref = nullptr; + NSVGgradientStop* stops = nullptr; NSVGgradient* grad; - float ox, oy, sw, sh, sl; + double ox, oy, sw, sh, sl; int nstops = 0; data = nsvg__findGradientData(p, id); - if (data == NULL) return NULL; + if (data == nullptr) return nullptr; // TODO: use ref to fill in all unset values too. ref = data; - while (ref != NULL) { - if (stops == NULL && ref->stops != NULL) { + while (ref != nullptr) { + if (stops == nullptr && ref->stops != nullptr) { stops = ref->stops; nstops = ref->nstops; break; } ref = nsvg__findGradientData(p, ref->ref); } - if (stops == NULL) return NULL; + if (stops == nullptr) return nullptr; - grad = (NSVGgradient*)malloc(sizeof(NSVGgradient) + sizeof(NSVGgradientStop)*(nstops-1)); - if (grad == NULL) return NULL; + grad = static_cast(malloc(sizeof(NSVGgradient) + sizeof(NSVGgradientStop)*(nstops-1))); + if (grad == nullptr) return nullptr; // The shape width and height. if (data->units == NSVG_OBJECT_SPACE) { @@ -858,10 +857,10 @@ static NSVGgradient* nsvg__createGradient(NSVGparser* p, const char* id, const f sw = nsvg__actualWidth(p); sh = nsvg__actualHeight(p); } - sl = sqrtf(sw*sw + sh*sh) / sqrtf(2.0f); + sl = sqrt(sw*sw + sh*sh) / sqrt(2.0); if (data->type == NSVG_PAINT_LINEAR_GRADIENT) { - float x1, y1, x2, y2, dx, dy; + double x1, y1, x2, y2, dx, dy; x1 = nsvg__convertToPixels(p, data->linear.x1, ox, sw); y1 = nsvg__convertToPixels(p, data->linear.y1, oy, sh); x2 = nsvg__convertToPixels(p, data->linear.x2, ox, sw); @@ -873,7 +872,7 @@ static NSVGgradient* nsvg__createGradient(NSVGparser* p, const char* id, const f grad->xform[2] = dx; grad->xform[3] = dy; grad->xform[4] = x1; grad->xform[5] = y1; } else { - float cx, cy, fx, fy, r; + double cx, cy, fx, fy, r; cx = nsvg__convertToPixels(p, data->radial.cx, ox, sw); cy = nsvg__convertToPixels(p, data->radial.cy, oy, sh); fx = nsvg__convertToPixels(p, data->radial.fx, ox, sw); @@ -899,19 +898,19 @@ static NSVGgradient* nsvg__createGradient(NSVGparser* p, const char* id, const f return grad; } -static float nsvg__getAverageScale(float* t) +static double nsvg__getAverageScale(double* t) { - float sx = sqrtf(t[0]*t[0] + t[2]*t[2]); - float sy = sqrtf(t[1]*t[1] + t[3]*t[3]); - return (sx + sy) * 0.5f; + double sx = sqrt(t[0]*t[0] + t[2]*t[2]); + double sy = sqrt(t[1]*t[1] + t[3]*t[3]); + return (sx + sy) * 0.5; } -static void nsvg__getLocalBounds(float* bounds, NSVGshape *shape, float* xform) +static void nsvg__getLocalBounds(double* bounds, NSVGshape *shape, double* xform) { NSVGpath* path; - float curve[4*2], curveBounds[4]; + double curve[4*2], curveBounds[4]; int i, first = 1; - for (path = shape->paths; path != NULL; path = path->next) { + for (path = shape->paths; path != nullptr; path = path->next) { nsvg__xformPoint(&curve[0], &curve[1], path->pts[0], path->pts[1], xform); for (i = 0; i < path->npts-1; i += 3) { nsvg__xformPoint(&curve[2], &curve[3], path->pts[(i+1)*2], path->pts[(i+1)*2+1], xform); @@ -939,23 +938,23 @@ static void nsvg__getLocalBounds(float* bounds, NSVGshape *shape, float* xform) static void nsvg__addShape(NSVGparser* p) { NSVGattrib* attr = nsvg__getAttr(p); - float scale = 1.0f; + double scale = 1.0; NSVGshape* shape; NSVGpath* path; int i; - if (p->plist == NULL) + if (p->plist == nullptr) return; - shape = (NSVGshape*)malloc(sizeof(NSVGshape)); - if (shape == NULL) goto error; + shape = static_cast(malloc(sizeof(NSVGshape))); + if (shape == nullptr) 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->strokeDashOffset = attr->strokeDashOffset * scale; - shape->strokeDashCount = (char)attr->strokeDashCount; + shape->strokeDashCount = static_cast(attr->strokeDashCount); for (i = 0; i < attr->strokeDashCount; i++) shape->strokeDashArray[i] = attr->strokeDashArray[i] * scale; shape->strokeLineJoin = attr->strokeLineJoin; @@ -965,14 +964,14 @@ static void nsvg__addShape(NSVGparser* p) shape->opacity = attr->opacity; shape->paths = p->plist; - p->plist = NULL; + p->plist = nullptr; // Calculate shape bounds shape->bounds[0] = shape->paths->bounds[0]; shape->bounds[1] = shape->paths->bounds[1]; shape->bounds[2] = shape->paths->bounds[2]; shape->bounds[3] = shape->paths->bounds[3]; - for (path = shape->paths->next; path != NULL; path = path->next) { + for (path = shape->paths->next; path != nullptr; path = path->next) { shape->bounds[0] = nsvg__minf(shape->bounds[0], path->bounds[0]); shape->bounds[1] = nsvg__minf(shape->bounds[1], path->bounds[1]); shape->bounds[2] = nsvg__maxf(shape->bounds[2], path->bounds[2]); @@ -985,13 +984,13 @@ static void nsvg__addShape(NSVGparser* p) } else if (attr->hasFill == 1) { shape->fill.type = NSVG_PAINT_COLOR; shape->fill.color = attr->fillColor; - shape->fill.color |= (unsigned int)(attr->fillOpacity*255) << 24; + shape->fill.color |= static_cast(attr->fillOpacity*255) << 24; } else if (attr->hasFill == 2) { - float inv[6], localBounds[4]; + double 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) { + if (shape->fill.gradient == nullptr) { shape->fill.type = NSVG_PAINT_NONE; } } @@ -1002,13 +1001,13 @@ static void nsvg__addShape(NSVGparser* p) } else if (attr->hasStroke == 1) { shape->stroke.type = NSVG_PAINT_COLOR; shape->stroke.color = attr->strokeColor; - shape->stroke.color |= (unsigned int)(attr->strokeOpacity*255) << 24; + shape->stroke.color |= static_cast(attr->strokeOpacity*255) << 24; } else if (attr->hasStroke == 2) { - float inv[6], localBounds[4]; + double 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) + if (shape->stroke.gradient == nullptr) shape->stroke.type = NSVG_PAINT_NONE; } @@ -1016,7 +1015,7 @@ static void nsvg__addShape(NSVGparser* p) shape->flags = (attr->visible ? NSVG_FLAGS_VISIBLE : 0x00); // Add to tail - if (p->image->shapes == NULL) + if (p->image->shapes == nullptr) p->image->shapes = shape; else p->shapesTail->next = shape; @@ -1031,9 +1030,9 @@ error: static void nsvg__addPath(NSVGparser* p, char closed) { NSVGattrib* attr = nsvg__getAttr(p); - NSVGpath* path = NULL; - float bounds[4]; - float* curve; + NSVGpath* path = nullptr; + double bounds[4]; + double* curve; int i; if (p->npts < 4) @@ -1042,12 +1041,12 @@ static void nsvg__addPath(NSVGparser* p, char closed) if (closed) nsvg__lineTo(p, p->pts[0], p->pts[1]); - path = (NSVGpath*)malloc(sizeof(NSVGpath)); - if (path == NULL) goto error; + path = static_cast(malloc(sizeof(NSVGpath))); + if (path == nullptr) goto error; memset(path, 0, sizeof(NSVGpath)); - path->pts = (float*)malloc(p->npts*2*sizeof(float)); - if (path->pts == NULL) goto error; + path->pts = static_cast(malloc(p->npts*2*sizeof(double))); + if (path->pts == nullptr) goto error; path->closed = closed; path->npts = p->npts; @@ -1078,8 +1077,8 @@ static void nsvg__addPath(NSVGparser* p, char closed) return; error: - if (path != NULL) { - if (path->pts != NULL) free(path->pts); + if (path != nullptr) { + if (path->pts != nullptr) free(path->pts); free(path); } } @@ -1087,8 +1086,8 @@ error: // We roll our own string to float because the std library one uses locale and messes things up. static double nsvg__atof(const char* s) { - char* cur = (char*)s; - char* end = NULL; + const char* cur = s; + char* end = nullptr; double res = 0.0, sign = 1.0; long long intPart = 0, fracPart = 0; char hasIntPart = 0, hasFracPart = 0; @@ -1106,7 +1105,7 @@ static double nsvg__atof(const char* s) // Parse digit sequence intPart = strtoll(cur, &end, 10); if (cur != end) { - res = (double)intPart; + res = static_cast(intPart); hasIntPart = 1; cur = end; } @@ -1119,7 +1118,7 @@ static double nsvg__atof(const char* s) // Parse digit sequence fracPart = strtoll(cur, &end, 10); if (cur != end) { - res += (double)fracPart / pow(10.0, (double)(end - cur)); + res += static_cast(fracPart) / pow(10.0, static_cast(end - cur)); hasFracPart = 1; cur = end; } @@ -1136,7 +1135,7 @@ static double nsvg__atof(const char* s) cur++; // skip 'E' expPart = strtol(cur, &end, 10); // Parse digit sequence with sign if (cur != end) { - res *= pow(10.0, (double)expPart); + res *= pow(10.0, static_cast(expPart)); } } @@ -1422,18 +1421,18 @@ static unsigned int nsvg__parseColor(const char* str) return nsvg__parseColorName(str); } -static float nsvg__parseOpacity(const char* str) +static double nsvg__parseOpacity(const char* str) { - float val = (float)nsvg__atof(str); - if (val < 0.0f) val = 0.0f; - if (val > 1.0f) val = 1.0f; + double val = nsvg__atof(str); + if (val < 0.0) val = 0.0; + if (val > 1.0) val = 1.0; return val; } -static float nsvg__parseMiterLimit(const char* str) +static double nsvg__parseMiterLimit(const char* str) { - float val = (float)nsvg__atof(str); - if (val < 0.0f) val = 0.0f; + double val = nsvg__atof(str); + if (val < 0.0) val = 0.0; return val; } @@ -1465,23 +1464,23 @@ static NSVGcoordinate nsvg__parseCoordinateRaw(const char* str) NSVGcoordinate coord = {0, NSVG_UNITS_USER}; char buf[64]; coord.units = nsvg__parseUnits(nsvg__parseNumber(str, buf, 64)); - coord.value = (float)nsvg__atof(buf); + coord.value = nsvg__atof(buf); return coord; } -static NSVGcoordinate nsvg__coord(float v, int units) +static NSVGcoordinate nsvg__coord(double v, int units) { NSVGcoordinate coord = {v, units}; return coord; } -static float nsvg__parseCoordinate(NSVGparser* p, const char* str, float orig, float length) +static double nsvg__parseCoordinate(NSVGparser* p, const char* str, double orig, double length) { NSVGcoordinate coord = nsvg__parseCoordinateRaw(str); return nsvg__convertToPixels(p, coord, orig, length); } -static int nsvg__parseTransformArgs(const char* str, float* args, int maxNa, int* na) +static int nsvg__parseTransformArgs(const char* str, double* args, int maxNa, int* na) { const char* end; const char* ptr; @@ -1501,81 +1500,81 @@ static int nsvg__parseTransformArgs(const char* str, float* args, int maxNa, int if (*ptr == '-' || *ptr == '+' || *ptr == '.' || nsvg__isdigit(*ptr)) { if (*na >= maxNa) return 0; ptr = nsvg__parseNumber(ptr, it, 64); - args[(*na)++] = (float)nsvg__atof(it); + args[(*na)++] = nsvg__atof(it); } else { ++ptr; } } - return (int)(end - str); + return static_cast(end - str); } -static int nsvg__parseMatrix(float* xform, const char* str) +static int nsvg__parseMatrix(double* xform, const char* str) { - float t[6]; + double t[6]; int na = 0; int len = nsvg__parseTransformArgs(str, t, 6, &na); if (na != 6) return len; - memcpy(xform, t, sizeof(float)*6); + memcpy(xform, t, sizeof(double)*6); return len; } -static int nsvg__parseTranslate(float* xform, const char* str) +static int nsvg__parseTranslate(double* xform, const char* str) { - float args[2]; - float t[6]; + double args[2]; + double t[6]; int na = 0; int len = nsvg__parseTransformArgs(str, args, 2, &na); if (na == 1) args[1] = 0.0; nsvg__xformSetTranslation(t, args[0], args[1]); - memcpy(xform, t, sizeof(float)*6); + memcpy(xform, t, sizeof(double)*6); return len; } -static int nsvg__parseScale(float* xform, const char* str) +static int nsvg__parseScale(double* xform, const char* str) { - float args[2]; + double args[2]; int na = 0; - float t[6]; + double t[6]; int len = nsvg__parseTransformArgs(str, args, 2, &na); if (na == 1) args[1] = args[0]; nsvg__xformSetScale(t, args[0], args[1]); - memcpy(xform, t, sizeof(float)*6); + memcpy(xform, t, sizeof(double)*6); return len; } -static int nsvg__parseSkewX(float* xform, const char* str) +static int nsvg__parseSkewX(double* xform, const char* str) { - float args[1]; + double args[1]; int na = 0; - float t[6]; + double t[6]; int len = nsvg__parseTransformArgs(str, args, 1, &na); - nsvg__xformSetSkewX(t, args[0]/180.0f*NSVG_PI); - memcpy(xform, t, sizeof(float)*6); + nsvg__xformSetSkewX(t, args[0]/180.0*NSVG_PI); + memcpy(xform, t, sizeof(double)*6); return len; } -static int nsvg__parseSkewY(float* xform, const char* str) +static int nsvg__parseSkewY(double* xform, const char* str) { - float args[1]; + double args[1]; int na = 0; - float t[6]; + double t[6]; int len = nsvg__parseTransformArgs(str, args, 1, &na); - nsvg__xformSetSkewY(t, args[0]/180.0f*NSVG_PI); - memcpy(xform, t, sizeof(float)*6); + nsvg__xformSetSkewY(t, args[0]/180.0*NSVG_PI); + memcpy(xform, t, sizeof(double)*6); return len; } -static int nsvg__parseRotate(float* xform, const char* str) +static int nsvg__parseRotate(double* xform, const char* str) { - float args[3]; + double args[3]; int na = 0; - float m[6]; - float t[6]; + double m[6]; + double t[6]; int len = nsvg__parseTransformArgs(str, args, 3, &na); if (na == 1) - args[1] = args[2] = 0.0f; + args[1] = args[2] = 0.0; nsvg__xformIdentity(m); if (na > 1) { @@ -1583,7 +1582,7 @@ static int nsvg__parseRotate(float* xform, const char* str) nsvg__xformMultiply(m, t); } - nsvg__xformSetRotation(t, args[0]/180.0f*NSVG_PI); + nsvg__xformSetRotation(t, args[0]/180.0*NSVG_PI); nsvg__xformMultiply(m, t); if (na > 1) { @@ -1591,14 +1590,14 @@ static int nsvg__parseRotate(float* xform, const char* str) nsvg__xformMultiply(m, t); } - memcpy(xform, m, sizeof(float)*6); + memcpy(xform, m, sizeof(double)*6); return len; } -static void nsvg__parseTransform(float* xform, const char* str) +static void nsvg__parseTransform(double* xform, const char* str) { - float t[6]; + double t[6]; nsvg__xformIdentity(xform); while (*str) { @@ -1686,11 +1685,11 @@ static const char* nsvg__getNextDashItem(const char* s, char* it) return s; } -static int nsvg__parseStrokeDashArray(NSVGparser* p, const char* str, float* strokeDashArray) +static int nsvg__parseStrokeDashArray(NSVGparser* p, const char* str, double* strokeDashArray) { char item[64]; int count = 0, i; - float sum = 0.0f; + double sum = 0.0; // Handle "none" if (str[0] == 'n') @@ -1701,12 +1700,12 @@ static int nsvg__parseStrokeDashArray(NSVGparser* p, const char* str, float* str str = nsvg__getNextDashItem(str, item); if (!*item) break; if (count < NSVG_MAX_DASHES) - strokeDashArray[count++] = fabsf(nsvg__parseCoordinate(p, item, 0.0f, nsvg__actualLength(p))); + strokeDashArray[count++] = fabs(nsvg__parseCoordinate(p, item, 0.0, nsvg__actualLength(p))); } for (i = 0; i < count; i++) sum += strokeDashArray[i]; - if (sum <= 1e-6f) + if (sum <= 1e-6) count = 0; return count; @@ -1716,7 +1715,7 @@ static void nsvg__parseStyle(NSVGparser* p, const char* str); static int nsvg__parseAttr(NSVGparser* p, const char* name, const char* value) { - float xform[6]; + double xform[6]; NSVGattrib* attr = nsvg__getAttr(p); if (!attr) return 0; @@ -1752,11 +1751,11 @@ static int nsvg__parseAttr(NSVGparser* p, const char* name, const char* value) attr->strokeColor = nsvg__parseColor(value); } } else if (strcmp(name, "stroke-width") == 0) { - attr->strokeWidth = nsvg__parseCoordinate(p, value, 0.0f, nsvg__actualLength(p)); + attr->strokeWidth = nsvg__parseCoordinate(p, value, 0.0, nsvg__actualLength(p)); } else if (strcmp(name, "stroke-dasharray") == 0) { attr->strokeDashCount = nsvg__parseStrokeDashArray(p, value, attr->strokeDashArray); } else if (strcmp(name, "stroke-dashoffset") == 0) { - attr->strokeDashOffset = nsvg__parseCoordinate(p, value, 0.0f, nsvg__actualLength(p)); + attr->strokeDashOffset = nsvg__parseCoordinate(p, value, 0.0, nsvg__actualLength(p)); } else if (strcmp(name, "stroke-opacity") == 0) { attr->strokeOpacity = nsvg__parseOpacity(value); } else if (strcmp(name, "stroke-linecap") == 0) { @@ -1768,7 +1767,7 @@ static int nsvg__parseAttr(NSVGparser* p, const char* name, const char* value) } else if (strcmp(name, "fill-rule") == 0) { attr->fillRule = nsvg__parseFillRule(value); } else if (strcmp(name, "font-size") == 0) { - attr->fontSize = nsvg__parseCoordinate(p, value, 0.0f, nsvg__actualLength(p)); + attr->fontSize = nsvg__parseCoordinate(p, value, 0.0, nsvg__actualLength(p)); } else if (strcmp(name, "transform") == 0) { nsvg__parseTransform(xform, value); nsvg__xformPremultiply(attr->xform, xform); @@ -1777,7 +1776,7 @@ static int nsvg__parseAttr(NSVGparser* p, const char* name, const char* value) } else if (strcmp(name, "stop-opacity") == 0) { attr->stopOpacity = nsvg__parseOpacity(value); } else if (strcmp(name, "offset") == 0) { - attr->stopOffset = nsvg__parseCoordinate(p, value, 0.0f, 1.0f); + attr->stopOffset = nsvg__parseCoordinate(p, value, 0.0, 1.0); } else if (strcmp(name, "id") == 0) { strncpy(attr->id, value, 63); attr->id[63] = '\0'; @@ -1804,14 +1803,14 @@ static int nsvg__parseNameValue(NSVGparser* p, const char* start, const char* en while (str > start && (*str == ':' || nsvg__isspace(*str))) --str; ++str; - n = (int)(str - start); + n = static_cast(str - start); if (n > 511) n = 511; if (n) memcpy(name, start, n); name[n] = 0; while (val < end && (*val == ':' || nsvg__isspace(*val))) ++val; - n = (int)(end - val); + n = static_cast(end - val); if (n > 511) n = 511; if (n) memcpy(value, val, n); value[n] = 0; @@ -1882,7 +1881,7 @@ static int nsvg__getArgsPerElement(char cmd) return 0; } -static void nsvg__pathMoveTo(NSVGparser* p, float* cpx, float* cpy, float* args, int rel) +static void nsvg__pathMoveTo(NSVGparser* p, double* cpx, double* cpy, double* args, int rel) { if (rel) { *cpx += args[0]; @@ -1894,7 +1893,7 @@ static void nsvg__pathMoveTo(NSVGparser* p, float* cpx, float* cpy, float* args, nsvg__moveTo(p, *cpx, *cpy); } -static void nsvg__pathLineTo(NSVGparser* p, float* cpx, float* cpy, float* args, int rel) +static void nsvg__pathLineTo(NSVGparser* p, double* cpx, double* cpy, double* args, int rel) { if (rel) { *cpx += args[0]; @@ -1906,7 +1905,7 @@ static void nsvg__pathLineTo(NSVGparser* p, float* cpx, float* cpy, float* args, nsvg__lineTo(p, *cpx, *cpy); } -static void nsvg__pathHLineTo(NSVGparser* p, float* cpx, float* cpy, float* args, int rel) +static void nsvg__pathHLineTo(NSVGparser* p, double* cpx, double* cpy, double* args, int rel) { if (rel) *cpx += args[0]; @@ -1915,7 +1914,7 @@ static void nsvg__pathHLineTo(NSVGparser* p, float* cpx, float* cpy, float* args nsvg__lineTo(p, *cpx, *cpy); } -static void nsvg__pathVLineTo(NSVGparser* p, float* cpx, float* cpy, float* args, int rel) +static void nsvg__pathVLineTo(NSVGparser* p, double* cpx, double* cpy, double* args, int rel) { if (rel) *cpy += args[0]; @@ -1924,10 +1923,10 @@ static void nsvg__pathVLineTo(NSVGparser* p, float* cpx, float* cpy, float* args nsvg__lineTo(p, *cpx, *cpy); } -static void nsvg__pathCubicBezTo(NSVGparser* p, float* cpx, float* cpy, - float* cpx2, float* cpy2, float* args, int rel) +static void nsvg__pathCubicBezTo(NSVGparser* p, double* cpx, double* cpy, + double* cpx2, double* cpy2, double* args, int rel) { - float x2, y2, cx1, cy1, cx2, cy2; + double x2, y2, cx1, cy1, cx2, cy2; if (rel) { cx1 = *cpx + args[0]; @@ -1953,10 +1952,10 @@ static void nsvg__pathCubicBezTo(NSVGparser* p, float* cpx, float* cpy, *cpy = y2; } -static void nsvg__pathCubicBezShortTo(NSVGparser* p, float* cpx, float* cpy, - float* cpx2, float* cpy2, float* args, int rel) +static void nsvg__pathCubicBezShortTo(NSVGparser* p, double* cpx, double* cpy, + double* cpx2, double* cpy2, double* args, int rel) { - float x1, y1, x2, y2, cx1, cy1, cx2, cy2; + double x1, y1, x2, y2, cx1, cy1, cx2, cy2; x1 = *cpx; y1 = *cpy; @@ -1983,11 +1982,11 @@ static void nsvg__pathCubicBezShortTo(NSVGparser* p, float* cpx, float* cpy, *cpy = y2; } -static void nsvg__pathQuadBezTo(NSVGparser* p, float* cpx, float* cpy, - float* cpx2, float* cpy2, float* args, int rel) +static void nsvg__pathQuadBezTo(NSVGparser* p, double* cpx, double* cpy, + double* cpx2, double* cpy2, double* args, int rel) { - float x1, y1, x2, y2, cx, cy; - float cx1, cy1, cx2, cy2; + double x1, y1, x2, y2, cx, cy; + double cx1, cy1, cx2, cy2; x1 = *cpx; y1 = *cpy; @@ -2004,10 +2003,10 @@ static void nsvg__pathQuadBezTo(NSVGparser* p, float* cpx, float* cpy, } // Convert to cubic bezier - cx1 = x1 + 2.0f/3.0f*(cx - x1); - cy1 = y1 + 2.0f/3.0f*(cy - y1); - cx2 = x2 + 2.0f/3.0f*(cx - x2); - cy2 = y2 + 2.0f/3.0f*(cy - y2); + cx1 = x1 + 2.0/3.0*(cx - x1); + cy1 = y1 + 2.0/3.0*(cy - y1); + cx2 = x2 + 2.0/3.0*(cx - x2); + cy2 = y2 + 2.0/3.0*(cy - y2); nsvg__cubicBezTo(p, cx1,cy1, cx2,cy2, x2,y2); @@ -2017,11 +2016,11 @@ static void nsvg__pathQuadBezTo(NSVGparser* p, float* cpx, float* cpy, *cpy = y2; } -static void nsvg__pathQuadBezShortTo(NSVGparser* p, float* cpx, float* cpy, - float* cpx2, float* cpy2, float* args, int rel) +static void nsvg__pathQuadBezShortTo(NSVGparser* p, double* cpx, double* cpy, + double* cpx2, double* cpy2, double* args, int rel) { - float x1, y1, x2, y2, cx, cy; - float cx1, cy1, cx2, cy2; + double x1, y1, x2, y2, cx, cy; + double cx1, cy1, cx2, cy2; x1 = *cpx; y1 = *cpy; @@ -2037,10 +2036,10 @@ static void nsvg__pathQuadBezShortTo(NSVGparser* p, float* cpx, float* cpy, cy = 2*y1 - *cpy2; // Convert to cubix bezier - cx1 = x1 + 2.0f/3.0f*(cx - x1); - cy1 = y1 + 2.0f/3.0f*(cy - y1); - cx2 = x2 + 2.0f/3.0f*(cx - x2); - cy2 = y2 + 2.0f/3.0f*(cy - y2); + cx1 = x1 + 2.0/3.0*(cx - x1); + cy1 = y1 + 2.0/3.0*(cy - y1); + cx2 = x2 + 2.0/3.0*(cx - x2); + cy2 = y2 + 2.0/3.0*(cy - y2); nsvg__cubicBezTo(p, cx1,cy1, cx2,cy2, x2,y2); @@ -2050,40 +2049,40 @@ static void nsvg__pathQuadBezShortTo(NSVGparser* p, float* cpx, float* cpy, *cpy = y2; } -static float nsvg__sqr(float x) { return x*x; } -static float nsvg__vmag(float x, float y) { return sqrtf(x*x + y*y); } +static double nsvg__sqr(double x) { return x*x; } +static double nsvg__vmag(double x, double y) { return sqrt(x*x + y*y); } -static float nsvg__vecrat(float ux, float uy, float vx, float vy) +static double nsvg__vecrat(double ux, double uy, double vx, double vy) { return (ux*vx + uy*vy) / (nsvg__vmag(ux,uy) * nsvg__vmag(vx,vy)); } -static float nsvg__vecang(float ux, float uy, float vx, float vy) +static double nsvg__vecang(double ux, double uy, double vx, double vy) { - float r = nsvg__vecrat(ux,uy, vx,vy); - if (r < -1.0f) r = -1.0f; - if (r > 1.0f) r = 1.0f; - return ((ux*vy < uy*vx) ? -1.0f : 1.0f) * acosf(r); + double r = nsvg__vecrat(ux,uy, vx,vy); + if (r < -1.0) r = -1.0; + if (r > 1.0) r = 1.0; + return ((ux*vy < uy*vx) ? -1.0 : 1.0) * acos(r); } -static void nsvg__pathArcTo(NSVGparser* p, float* cpx, float* cpy, float* args, int rel) +static void nsvg__pathArcTo(NSVGparser* p, double* cpx, double* cpy, double* args, int rel) { // Ported from canvg (https://code.google.com/p/canvg/) - float rx, ry, rotx; - float x1, y1, x2, y2, cx, cy, dx, dy, d; - float x1p, y1p, cxp, cyp, s, sa, sb; - float ux, uy, vx, vy, a1, da; - float x, y, tanx, tany, a, px = 0, py = 0, ptanx = 0, ptany = 0, t[6]; - float sinrx, cosrx; + double rx, ry, rotx; + double x1, y1, x2, y2, cx, cy, dx, dy, d; + double x1p, y1p, cxp, cyp, s, sa, sb; + double ux, uy, vx, vy, a1, da; + double x, y, tanx, tany, a, px = 0, py = 0, ptanx = 0, ptany = 0, t[6]; + double sinrx, cosrx; int fa, fs; int i, ndivs; - float hda, kappa; + double hda, kappa; - rx = fabsf(args[0]); // y radius - ry = fabsf(args[1]); // x radius - 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 + rx = fabs(args[0]); // y radius + ry = fabs(args[1]); // x radius + rotx = args[2] / 180.0 * NSVG_PI; // x rotation angle + fa = fabs(args[3]) > 1e-6 ? 1 : 0; // Large arc + fs = fabs(args[4]) > 1e-6 ? 1 : 0; // Sweep direction x1 = *cpx; // start point y1 = *cpy; if (rel) { // end point @@ -2096,8 +2095,8 @@ static void nsvg__pathArcTo(NSVGparser* p, float* cpx, float* cpy, float* args, dx = x1 - x2; dy = y1 - y2; - d = sqrtf(dx*dx + dy*dy); - if (d < 1e-6f || rx < 1e-6f || ry < 1e-6f) { + d = sqrt(dx*dx + dy*dy); + if (d < 1e-6 || rx < 1e-6 || ry < 1e-6) { // The arc degenerates to a line nsvg__lineTo(p, x2, y2); *cpx = x2; @@ -2105,46 +2104,46 @@ static void nsvg__pathArcTo(NSVGparser* p, float* cpx, float* cpy, float* args, return; } - sinrx = sinf(rotx); - cosrx = cosf(rotx); + sinrx = sin(rotx); + cosrx = cos(rotx); // Convert to center point parameterization. // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes // 1) Compute x1', y1' - x1p = cosrx * dx / 2.0f + sinrx * dy / 2.0f; - y1p = -sinrx * dx / 2.0f + cosrx * dy / 2.0f; + x1p = cosrx * dx / 2.0 + sinrx * dy / 2.0; + y1p = -sinrx * dx / 2.0 + cosrx * dy / 2.0; d = nsvg__sqr(x1p)/nsvg__sqr(rx) + nsvg__sqr(y1p)/nsvg__sqr(ry); if (d > 1) { - d = sqrtf(d); + d = sqrt(d); rx *= d; ry *= d; } // 2) Compute cx', cy' - s = 0.0f; + s = 0.0; sa = nsvg__sqr(rx)*nsvg__sqr(ry) - nsvg__sqr(rx)*nsvg__sqr(y1p) - nsvg__sqr(ry)*nsvg__sqr(x1p); sb = nsvg__sqr(rx)*nsvg__sqr(y1p) + nsvg__sqr(ry)*nsvg__sqr(x1p); - if (sa < 0.0f) sa = 0.0f; - if (sb > 0.0f) - s = sqrtf(sa / sb); + if (sa < 0.0) sa = 0.0; + if (sb > 0.0) + s = sqrt(sa / sb); if (fa == fs) s = -s; cxp = s * rx * y1p / ry; cyp = s * -ry * x1p / rx; // 3) Compute cx,cy from cx',cy' - cx = (x1 + x2)/2.0f + cosrx*cxp - sinrx*cyp; - cy = (y1 + y2)/2.0f + sinrx*cxp + cosrx*cyp; + cx = (x1 + x2)/2.0 + cosrx*cxp - sinrx*cyp; + cy = (y1 + y2)/2.0 + sinrx*cxp + cosrx*cyp; // 4) Calculate theta1, and delta theta. ux = (x1p - cxp) / rx; uy = (y1p - cyp) / ry; vx = (-x1p - cxp) / rx; vy = (-y1p - cyp) / ry; - a1 = nsvg__vecang(1.0f,0.0f, ux,uy); // Initial angle + a1 = nsvg__vecang(1.0,0.0, ux,uy); // Initial angle da = nsvg__vecang(ux,uy, vx,vy); // Delta angle -// if (vecrat(ux,uy,vx,vy) <= -1.0f) da = NSVG_PI; -// if (vecrat(ux,uy,vx,vy) >= 1.0f) da = 0; +// if (vecrat(ux,uy,vx,vy) <= -1.0) da = NSVG_PI; +// if (vecrat(ux,uy,vx,vy) >= 1.0) da = 0; if (fs == 0 && da > 0) da -= 2 * NSVG_PI; @@ -2158,16 +2157,16 @@ static void nsvg__pathArcTo(NSVGparser* p, float* cpx, float* cpy, float* args, // Split arc into max 90 degree segments. // The loop assumes an iteration per end point (including start and end), this +1. - ndivs = (int)(fabsf(da) / (NSVG_PI*0.5f) + 1.0f); - hda = (da / (float)ndivs) / 2.0f; - kappa = fabsf(4.0f / 3.0f * (1.0f - cosf(hda)) / sinf(hda)); - if (da < 0.0f) + ndivs = static_cast(fabs(da) / (NSVG_PI*0.5) + 1.0); + hda = (da / static_cast(ndivs)) / 2.0; + kappa = fabs(4.0 / 3.0 * (1.0 - cos(hda)) / sin(hda)); + if (da < 0.0) kappa = -kappa; for (i = 0; i <= ndivs; i++) { - a = a1 + da * ((float)i/(float)ndivs); - dx = cosf(a); - dy = sinf(a); + a = a1 + da * (static_cast(i) / static_cast(ndivs)); + dx = cos(a); + dy = sin(a); nsvg__xformPoint(&x, &y, dx*rx, dy*ry, t); // position nsvg__xformVec(&tanx, &tany, -dy*rx * kappa, dx*ry * kappa, t); // tangent if (i > 0) @@ -2184,12 +2183,12 @@ static void nsvg__pathArcTo(NSVGparser* p, float* cpx, float* cpy, float* args, static void nsvg__parsePath(NSVGparser* p, const char** attr) { - const char* s = NULL; + const char* s = nullptr; char cmd = '\0'; - float args[10]; + double args[10]; int nargs; int rargs = 0; - float cpx, cpy, cpx2, cpy2; + double cpx, cpy, cpx2, cpy2; const char* tmp[4]; char closedFlag; int i; @@ -2219,7 +2218,7 @@ static void nsvg__parsePath(NSVGparser* p, const char** attr) if (!*item) break; if (nsvg__isnum(item[0])) { if (nargs < 10) - args[nargs++] = (float)nsvg__atof(item); + args[nargs++] = nsvg__atof(item); if (nargs >= rargs) { switch (cmd) { case 'm': @@ -2316,36 +2315,36 @@ static void nsvg__parsePath(NSVGparser* p, const char** attr) static void nsvg__parseRect(NSVGparser* p, const char** attr) { - float x = 0.0f; - float y = 0.0f; - float w = 0.0f; - float h = 0.0f; - float rx = -1.0f; // marks not set - float ry = -1.0f; + double x = 0.0; + double y = 0.0; + double w = 0.0; + double h = 0.0; + double rx = -1.0; // marks not set + double ry = -1.0; int i; for (i = 0; attr[i]; i += 2) { if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) { if (strcmp(attr[i], "x") == 0) x = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigX(p), nsvg__actualWidth(p)); if (strcmp(attr[i], "y") == 0) y = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigY(p), nsvg__actualHeight(p)); - if (strcmp(attr[i], "width") == 0) w = nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualWidth(p)); - if (strcmp(attr[i], "height") == 0) h = nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualHeight(p)); - if (strcmp(attr[i], "rx") == 0) rx = fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualWidth(p))); - if (strcmp(attr[i], "ry") == 0) ry = fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualHeight(p))); + if (strcmp(attr[i], "width") == 0) w = nsvg__parseCoordinate(p, attr[i+1], 0.0, nsvg__actualWidth(p)); + if (strcmp(attr[i], "height") == 0) h = nsvg__parseCoordinate(p, attr[i+1], 0.0, nsvg__actualHeight(p)); + if (strcmp(attr[i], "rx") == 0) rx = fabs(nsvg__parseCoordinate(p, attr[i+1], 0.0, nsvg__actualWidth(p))); + if (strcmp(attr[i], "ry") == 0) ry = fabs(nsvg__parseCoordinate(p, attr[i+1], 0.0, nsvg__actualHeight(p))); } } - if (rx < 0.0f && ry > 0.0f) rx = ry; - if (ry < 0.0f && rx > 0.0f) ry = rx; - if (rx < 0.0f) rx = 0.0f; - if (ry < 0.0f) ry = 0.0f; - if (rx > w/2.0f) rx = w/2.0f; - if (ry > h/2.0f) ry = h/2.0f; + if (rx < 0.0 && ry > 0.0) rx = ry; + if (ry < 0.0 && rx > 0.0) ry = rx; + if (rx < 0.0) rx = 0.0; + if (ry < 0.0) ry = 0.0; + if (rx > w/2.0) rx = w/2.0; + if (ry > h/2.0) ry = h/2.0; - if (w != 0.0f && h != 0.0f) { + if (w != 0.0 && h != 0.0) { nsvg__resetPath(p); - if (rx < 0.00001f || ry < 0.0001f) { + if (rx < 0.00001 || ry < 0.0001) { nsvg__moveTo(p, x, y); nsvg__lineTo(p, x+w, y); nsvg__lineTo(p, x+w, y+h); @@ -2371,20 +2370,20 @@ static void nsvg__parseRect(NSVGparser* p, const char** attr) static void nsvg__parseCircle(NSVGparser* p, const char** attr) { - float cx = 0.0f; - float cy = 0.0f; - float r = 0.0f; + double cx = 0.0; + double cy = 0.0; + double r = 0.0; int i; for (i = 0; attr[i]; i += 2) { if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) { if (strcmp(attr[i], "cx") == 0) cx = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigX(p), nsvg__actualWidth(p)); if (strcmp(attr[i], "cy") == 0) cy = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigY(p), nsvg__actualHeight(p)); - if (strcmp(attr[i], "r") == 0) r = fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualLength(p))); + if (strcmp(attr[i], "r") == 0) r = fabs(nsvg__parseCoordinate(p, attr[i+1], 0.0, nsvg__actualLength(p))); } } - if (r > 0.0f) { + if (r > 0.0) { nsvg__resetPath(p); nsvg__moveTo(p, cx+r, cy); @@ -2401,22 +2400,22 @@ static void nsvg__parseCircle(NSVGparser* p, const char** attr) static void nsvg__parseEllipse(NSVGparser* p, const char** attr) { - float cx = 0.0f; - float cy = 0.0f; - float rx = 0.0f; - float ry = 0.0f; + double cx = 0.0; + double cy = 0.0; + double rx = 0.0; + double ry = 0.0; int i; for (i = 0; attr[i]; i += 2) { if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) { if (strcmp(attr[i], "cx") == 0) cx = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigX(p), nsvg__actualWidth(p)); if (strcmp(attr[i], "cy") == 0) cy = nsvg__parseCoordinate(p, attr[i+1], nsvg__actualOrigY(p), nsvg__actualHeight(p)); - if (strcmp(attr[i], "rx") == 0) rx = fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualWidth(p))); - if (strcmp(attr[i], "ry") == 0) ry = fabsf(nsvg__parseCoordinate(p, attr[i+1], 0.0f, nsvg__actualHeight(p))); + if (strcmp(attr[i], "rx") == 0) rx = fabs(nsvg__parseCoordinate(p, attr[i+1], 0.0, nsvg__actualWidth(p))); + if (strcmp(attr[i], "ry") == 0) ry = fabs(nsvg__parseCoordinate(p, attr[i+1], 0.0, nsvg__actualHeight(p))); } } - if (rx > 0.0f && ry > 0.0f) { + if (rx > 0.0 && ry > 0.0) { nsvg__resetPath(p); @@ -2434,10 +2433,10 @@ static void nsvg__parseEllipse(NSVGparser* p, const char** attr) static void nsvg__parseLine(NSVGparser* p, const char** attr) { - float x1 = 0.0; - float y1 = 0.0; - float x2 = 0.0; - float y2 = 0.0; + double x1 = 0.0; + double y1 = 0.0; + double x2 = 0.0; + double y2 = 0.0; int i; for (i = 0; attr[i]; i += 2) { @@ -2463,7 +2462,7 @@ static void nsvg__parsePoly(NSVGparser* p, const char** attr, int closeFlag) { int i; const char* s; - float args[2]; + double args[2]; int nargs, npts = 0; char item[64]; @@ -2476,7 +2475,7 @@ static void nsvg__parsePoly(NSVGparser* p, const char** attr, int closeFlag) nargs = 0; while (*s) { s = nsvg__getNextPathItem(s, item); - args[nargs++] = (float)nsvg__atof(item); + args[nargs++] = nsvg__atof(item); if (nargs >= 2) { if (npts == 0) nsvg__moveTo(p, args[0], args[1]); @@ -2490,7 +2489,7 @@ static void nsvg__parsePoly(NSVGparser* p, const char** attr, int closeFlag) } } - nsvg__addPath(p, (char)closeFlag); + nsvg__addPath(p, static_cast(closeFlag)); nsvg__addShape(p); } @@ -2501,26 +2500,26 @@ static void nsvg__parseSVG(NSVGparser* p, const char** attr) for (i = 0; attr[i]; i += 2) { if (!nsvg__parseAttr(p, attr[i], attr[i + 1])) { if (strcmp(attr[i], "width") == 0) { - p->image->width = nsvg__parseCoordinate(p, attr[i + 1], 0.0f, 0.0f); + p->image->width = nsvg__parseCoordinate(p, attr[i + 1], 0.0, 0.0); } else if (strcmp(attr[i], "height") == 0) { - p->image->height = nsvg__parseCoordinate(p, attr[i + 1], 0.0f, 0.0f); + p->image->height = nsvg__parseCoordinate(p, attr[i + 1], 0.0, 0.0); } else if (strcmp(attr[i], "viewBox") == 0) { const char *s = attr[i + 1]; char buf[64]; s = nsvg__parseNumber(s, buf, 64); - p->viewMinx = (float)nsvg__atof(buf); + p->viewMinx = nsvg__atof(buf); while (*s && (nsvg__isspace(*s) || *s == '%' || *s == ',')) s++; if (!*s) return; s = nsvg__parseNumber(s, buf, 64); - p->viewMiny = (float)nsvg__atof(buf); + p->viewMiny = nsvg__atof(buf); while (*s && (nsvg__isspace(*s) || *s == '%' || *s == ',')) s++; if (!*s) return; s = nsvg__parseNumber(s, buf, 64); - p->viewWidth = (float)nsvg__atof(buf); + p->viewWidth = nsvg__atof(buf); while (*s && (nsvg__isspace(*s) || *s == '%' || *s == ',')) s++; if (!*s) return; s = nsvg__parseNumber(s, buf, 64); - p->viewHeight = (float)nsvg__atof(buf); + p->viewHeight = nsvg__atof(buf); } else if (strcmp(attr[i], "preserveAspectRatio") == 0) { if (strstr(attr[i + 1], "none") != nullptr) { // No uniform scaling @@ -2553,20 +2552,20 @@ static void nsvg__parseSVG(NSVGparser* p, const char** attr) static void nsvg__parseGradient(NSVGparser* p, const char** attr, char type) { int i; - NSVGgradientData* grad = (NSVGgradientData*)malloc(sizeof(NSVGgradientData)); - if (grad == NULL) return; + NSVGgradientData* grad = static_cast(malloc(sizeof(NSVGgradientData))); + if (grad == nullptr) return; memset(grad, 0, sizeof(NSVGgradientData)); grad->units = NSVG_OBJECT_SPACE; grad->type = type; if (grad->type == NSVG_PAINT_LINEAR_GRADIENT) { - grad->linear.x1 = nsvg__coord(0.0f, NSVG_UNITS_PERCENT); - grad->linear.y1 = nsvg__coord(0.0f, NSVG_UNITS_PERCENT); - grad->linear.x2 = nsvg__coord(100.0f, NSVG_UNITS_PERCENT); - grad->linear.y2 = nsvg__coord(0.0f, NSVG_UNITS_PERCENT); + grad->linear.x1 = nsvg__coord(0.0, NSVG_UNITS_PERCENT); + grad->linear.y1 = nsvg__coord(0.0, NSVG_UNITS_PERCENT); + grad->linear.x2 = nsvg__coord(100.0, NSVG_UNITS_PERCENT); + grad->linear.y2 = nsvg__coord(0.0, NSVG_UNITS_PERCENT); } else if (grad->type == NSVG_PAINT_RADIAL_GRADIENT) { - grad->radial.cx = nsvg__coord(50.0f, NSVG_UNITS_PERCENT); - grad->radial.cy = nsvg__coord(50.0f, NSVG_UNITS_PERCENT); - grad->radial.r = nsvg__coord(50.0f, NSVG_UNITS_PERCENT); + grad->radial.cx = nsvg__coord(50.0, NSVG_UNITS_PERCENT); + grad->radial.cy = nsvg__coord(50.0, NSVG_UNITS_PERCENT); + grad->radial.r = nsvg__coord(50.0, NSVG_UNITS_PERCENT); } nsvg__xformIdentity(grad->xform); @@ -2629,7 +2628,7 @@ static void nsvg__parseGradientStop(NSVGparser* p, const char** attr) curAttr->stopOffset = 0; curAttr->stopColor = 0; - curAttr->stopOpacity = 1.0f; + curAttr->stopOpacity = 1.0; for (i = 0; attr[i]; i += 2) { nsvg__parseAttr(p, attr[i], attr[i + 1]); @@ -2637,11 +2636,11 @@ static void nsvg__parseGradientStop(NSVGparser* p, const char** attr) // Add stop to the last gradient. grad = p->gradients; - if (grad == NULL) return; + if (grad == nullptr) return; grad->nstops++; - grad->stops = (NSVGgradientStop*)realloc(grad->stops, sizeof(NSVGgradientStop)*grad->nstops); - if (grad->stops == NULL) return; + grad->stops = static_cast(realloc(grad->stops, sizeof(NSVGgradientStop)*grad->nstops)); + if (grad->stops == nullptr) return; // Insert idx = grad->nstops-1; @@ -2658,13 +2657,13 @@ static void nsvg__parseGradientStop(NSVGparser* p, const char** attr) stop = &grad->stops[idx]; stop->color = curAttr->stopColor; - stop->color |= (unsigned int)(curAttr->stopOpacity*255) << 24; + stop->color |= static_cast(curAttr->stopOpacity*255) << 24; stop->offset = curAttr->stopOffset; } static void nsvg__startElement(void* ud, const char* el, const char** attr) { - NSVGparser* p = (NSVGparser*)ud; + NSVGparser* p = static_cast(ud); if (p->defsFlag) { // Skip everything but gradients in defs @@ -2726,7 +2725,7 @@ static void nsvg__startElement(void* ud, const char* el, const char** attr) static void nsvg__endElement(void* ud, const char* el) { - NSVGparser* p = (NSVGparser*)ud; + NSVGparser* p = static_cast(ud); if (strcmp(el, "g") == 0) { nsvg__popAttr(p); @@ -2744,11 +2743,11 @@ static void nsvg__content(void* ud, const char* s) // empty } -static void nsvg__imageBounds(NSVGparser* p, float* bounds) +static void nsvg__imageBounds(NSVGparser* p, double* bounds) { NSVGshape* shape; shape = p->image->shapes; - if (shape == NULL) { + if (shape == nullptr) { bounds[0] = bounds[1] = bounds[2] = bounds[3] = 0.0; return; } @@ -2756,7 +2755,7 @@ static void nsvg__imageBounds(NSVGparser* p, float* bounds) bounds[1] = shape->bounds[1]; bounds[2] = shape->bounds[2]; bounds[3] = shape->bounds[3]; - for (shape = shape->next; shape != NULL; shape = shape->next) { + for (shape = shape->next; shape != nullptr; shape = shape->next) { bounds[0] = nsvg__minf(bounds[0], shape->bounds[0]); bounds[1] = nsvg__minf(bounds[1], shape->bounds[1]); bounds[2] = nsvg__maxf(bounds[2], shape->bounds[2]); @@ -2764,19 +2763,19 @@ static void nsvg__imageBounds(NSVGparser* p, float* bounds) } } -static float nsvg__viewAlign(float content, float container, int type) +static double nsvg__viewAlign(double content, double container, int type) { if (type == NSVG_ALIGN_MIN) return 0; else if (type == NSVG_ALIGN_MAX) return container - content; // mid - return (container - content) * 0.5f; + return (container - content) * 0.5; } -static void nsvg__scaleGradient(NSVGgradient* grad, float tx, float ty, float sx, float sy) +static void nsvg__scaleGradient(NSVGgradient* grad, double tx, double ty, double sx, double sy) { - float t[6]; + double t[6]; nsvg__xformSetTranslation(t, tx, ty); nsvg__xformMultiply (grad->xform, t); @@ -2788,9 +2787,9 @@ static void nsvg__scaleToViewbox(NSVGparser* p, const char* units) { NSVGshape* shape; NSVGpath* path; - float tx, ty, sx, sy, us, bounds[4], t[6], avgs; + double tx, ty, sx, sy, us, bounds[4], t[6], avgs; int i; - float* pt; + double* pt; // Guess image size if not set completely. nsvg__imageBounds(p, bounds); @@ -2821,7 +2820,7 @@ static void nsvg__scaleToViewbox(NSVGparser* p, const char* units) sx = p->viewWidth > 0 ? p->image->width / p->viewWidth : 0; sy = p->viewHeight > 0 ? p->image->height / p->viewHeight : 0; // Unit scaling - us = 1.0f / nsvg__convertToPixels(p, nsvg__coord(1.0f, nsvg__parseUnits(units)), 0.0f, 1.0f); + us = 1.0 / nsvg__convertToPixels(p, nsvg__coord(1.0, nsvg__parseUnits(units)), 0.0, 1.0); // Fix aspect ratio if (p->alignType == NSVG_ALIGN_MEET) { @@ -2839,13 +2838,13 @@ static void nsvg__scaleToViewbox(NSVGparser* p, const char* units) // Transform sx *= us; sy *= us; - avgs = (sx+sy) / 2.0f; - for (shape = p->image->shapes; shape != NULL; shape = shape->next) { + avgs = (sx+sy) / 2.0; + for (shape = p->image->shapes; shape != nullptr; shape = shape->next) { shape->bounds[0] = (shape->bounds[0] + tx) * sx; shape->bounds[1] = (shape->bounds[1] + ty) * sy; shape->bounds[2] = (shape->bounds[2] + tx) * sx; shape->bounds[3] = (shape->bounds[3] + ty) * sy; - for (path = shape->paths; path != NULL; path = path->next) { + for (path = shape->paths; path != nullptr; path = path->next) { path->bounds[0] = (path->bounds[0] + tx) * sx; path->bounds[1] = (path->bounds[1] + ty) * sy; path->bounds[2] = (path->bounds[2] + tx) * sx; @@ -2859,12 +2858,12 @@ static void nsvg__scaleToViewbox(NSVGparser* p, const char* units) if (shape->fill.type == NSVG_PAINT_LINEAR_GRADIENT || shape->fill.type == NSVG_PAINT_RADIAL_GRADIENT) { nsvg__scaleGradient(shape->fill.gradient, tx,ty, sx,sy); - memcpy(t, shape->fill.gradient->xform, sizeof(float)*6); + memcpy(t, shape->fill.gradient->xform, sizeof(double)*6); nsvg__xformInverse(shape->fill.gradient->xform, t); } if (shape->stroke.type == NSVG_PAINT_LINEAR_GRADIENT || shape->stroke.type == NSVG_PAINT_RADIAL_GRADIENT) { nsvg__scaleGradient(shape->stroke.gradient, tx,ty, sx,sy); - memcpy(t, shape->stroke.gradient->xform, sizeof(float)*6); + memcpy(t, shape->stroke.gradient->xform, sizeof(double)*6); nsvg__xformInverse(shape->stroke.gradient->xform, t); } @@ -2875,14 +2874,14 @@ static void nsvg__scaleToViewbox(NSVGparser* p, const char* units) } } -NSVGimage* nsvgParse(char* input, const char* units, float dpi) +NSVGimage* nsvgParse(char* input, const char* units, double dpi) { NSVGparser* p; NSVGimage* ret = nullptr; p = nsvg__createParser(); - if (p == NULL) { - return NULL; + if (p == nullptr) { + return nullptr; } p->dpi = dpi; @@ -2892,27 +2891,27 @@ NSVGimage* nsvgParse(char* input, const char* units, float dpi) nsvg__scaleToViewbox(p, units); ret = p->image; - p->image = NULL; + p->image = nullptr; nsvg__deleteParser(p); return ret; } -NSVGimage* nsvgParseFromFile(const char* filename, const char* units, float dpi) +NSVGimage* nsvgParseFromFile(const char* filename, const char* units, double dpi) { - FILE* fp = NULL; + FILE* fp = nullptr; size_t size; - char* data = NULL; - NSVGimage* image = NULL; + char* data = nullptr; + NSVGimage* image = nullptr; fp = fopen(filename, "rb"); if (!fp) goto error; fseek(fp, 0, SEEK_END); size = ftell(fp); fseek(fp, 0, SEEK_SET); - data = (char*)malloc(size+1); - if (data == NULL) goto error; + data = static_cast(malloc(size+1)); + if (data == nullptr) goto error; if (fread(data, 1, size, fp) != size) goto error; data[size] = '\0'; // Must be null terminated. fclose(fp); @@ -2925,23 +2924,23 @@ error: if (fp) fclose(fp); if (data) free(data); if (image) nsvgDelete(image); - return NULL; + return nullptr; } NSVGpath* nsvgDuplicatePath(NSVGpath* p) { - NSVGpath* res = NULL; + NSVGpath* res = nullptr; - if (p == NULL) - return NULL; + if (p == nullptr) + return nullptr; - res = (NSVGpath*)malloc(sizeof(NSVGpath)); - if (res == NULL) goto error; + res = static_cast(malloc(sizeof(NSVGpath))); + if (res == nullptr) goto error; memset(res, 0, sizeof(NSVGpath)); - res->pts = (float*)malloc(p->npts*2*sizeof(float)); - if (res->pts == NULL) goto error; - memcpy(res->pts, p->pts, p->npts * sizeof(float) * 2); + res->pts = static_cast(malloc(p->npts*2*sizeof(double))); + if (res->pts == nullptr) goto error; + memcpy(res->pts, p->pts, p->npts * sizeof(double) * 2); res->npts = p->npts; memcpy(res->bounds, p->bounds, sizeof(p->bounds)); @@ -2951,19 +2950,19 @@ NSVGpath* nsvgDuplicatePath(NSVGpath* p) return res; error: - if (res != NULL) { + if (res != nullptr) { free(res->pts); free(res); } - return NULL; + return nullptr; } void nsvgDelete(NSVGimage* image) { NSVGshape *snext, *shape; - if (image == NULL) return; + if (image == nullptr) return; shape = image->shapes; - while (shape != NULL) { + while (shape != nullptr) { snext = shape->next; nsvg__deletePaths(shape->paths); nsvg__deletePaint(&shape->fill); @@ -2974,5 +2973,6 @@ void nsvgDelete(NSVGimage* image) free(image); } -#endif +} +#endif diff --git a/include/TGUI/nanosvg/nanosvgrast.h b/include/TGUI/nanosvg/nanosvgrast.h index a4f30e42..e89c9eb5 100644 --- a/include/TGUI/nanosvg/nanosvgrast.h +++ b/include/TGUI/nanosvg/nanosvgrast.h @@ -1,3 +1,8 @@ +// Some modifications have been made to the original file: +// - Code was placed in a "tgui" namespace to avoid conflicts if a TGUI user also uses NanoSVG in their project +// - All floats were replaced by doubles (to get rid of warnings about implicit conversions and promotions) +// - Some other c++ compiler warnings were fixed, mainly about usage of old-style casts + /* * Copyright (c) 2013-14 Mikko Mononen memon@inside.org * @@ -25,13 +30,9 @@ #ifndef NANOSVGRAST_H #define NANOSVGRAST_H -#ifndef NANOSVGRAST_CPLUSPLUS -#ifdef __cplusplus -extern "C" { -#endif -#endif - -typedef struct NSVGrasterizer NSVGrasterizer; +namespace tgui +{ + struct NSVGrasterizer; /* Example Usage: // Load SVG @@ -59,18 +60,13 @@ NSVGrasterizer* nsvgCreateRasterizer(); // h - height of the image to render // stride - number of bytes per scaleline in the destination buffer void nsvgRasterize(NSVGrasterizer* r, - NSVGimage* image, float tx, float ty, float scale, + NSVGimage* image, double tx, double ty, double scale, unsigned char* dst, int w, int h, int stride); // Deletes rasterizer context. void nsvgDeleteRasterizer(NSVGrasterizer*); - -#ifndef NANOSVGRAST_CPLUSPLUS -#ifdef __cplusplus } -#endif -#endif #endif // NANOSVGRAST_H @@ -78,6 +74,9 @@ void nsvgDeleteRasterizer(NSVGrasterizer*); #include +namespace tgui +{ + #define NSVG__SUBSAMPLES 5 #define NSVG__FIXSHIFT 10 #define NSVG__FIX (1 << NSVG__FIXSHIFT) @@ -85,22 +84,22 @@ void nsvgDeleteRasterizer(NSVGrasterizer*); #define NSVG__MEMPAGE_SIZE 1024 typedef struct NSVGedge { - float x0,y0, x1,y1; + double x0,y0, x1,y1; int dir; struct NSVGedge* next; } NSVGedge; typedef struct NSVGpoint { - float x, y; - float dx, dy; - float len; - float dmx, dmy; + double x, y; + double dx, dy; + double len; + double dmx, dmy; unsigned char flags; } NSVGpoint; typedef struct NSVGactiveEdge { int x,dx; - float ey; + double ey; int dir; struct NSVGactiveEdge *next; } NSVGactiveEdge; @@ -114,16 +113,16 @@ typedef struct NSVGmemPage { typedef struct NSVGcachedPaint { char type; char spread; - float xform[6]; + double xform[6]; unsigned int colors[256]; } NSVGcachedPaint; struct NSVGrasterizer { - float px, py; + double px, py; - float tessTol; - float distTol; + double tessTol; + double distTol; NSVGedge* edges; int nedges; @@ -150,28 +149,28 @@ struct NSVGrasterizer NSVGrasterizer* nsvgCreateRasterizer() { - NSVGrasterizer* r = (NSVGrasterizer*)malloc(sizeof(NSVGrasterizer)); - if (r == NULL) goto error; + NSVGrasterizer* r = static_cast(malloc(sizeof(NSVGrasterizer))); + if (r == nullptr) goto error; memset(r, 0, sizeof(NSVGrasterizer)); - r->tessTol = 0.25f; - r->distTol = 0.01f; + r->tessTol = 0.25; + r->distTol = 0.01; return r; error: nsvgDeleteRasterizer(r); - return NULL; + return nullptr; } void nsvgDeleteRasterizer(NSVGrasterizer* r) { NSVGmemPage* p; - if (r == NULL) return; + if (r == nullptr) return; p = r->pages; - while (p != NULL) { + while (p != nullptr) { NSVGmemPage* next = p->next; free(p); p = next; @@ -190,17 +189,17 @@ static NSVGmemPage* nsvg__nextPage(NSVGrasterizer* r, NSVGmemPage* cur) NSVGmemPage *newp; // If using existing chain, return the next page in chain - if (cur != NULL && cur->next != NULL) { + if (cur != nullptr && cur->next != nullptr) { return cur->next; } // Alloc new page - newp = (NSVGmemPage*)malloc(sizeof(NSVGmemPage)); - if (newp == NULL) return NULL; + newp = static_cast(malloc(sizeof(NSVGmemPage))); + if (newp == nullptr) return nullptr; memset(newp, 0, sizeof(NSVGmemPage)); // Add to linked list - if (cur != NULL) + if (cur != nullptr) cur->next = newp; else r->pages = newp; @@ -211,7 +210,7 @@ static NSVGmemPage* nsvg__nextPage(NSVGrasterizer* r, NSVGmemPage* cur) static void nsvg__resetPool(NSVGrasterizer* r) { NSVGmemPage* p = r->pages; - while (p != NULL) { + while (p != nullptr) { p->size = 0; p = p->next; } @@ -221,8 +220,8 @@ static void nsvg__resetPool(NSVGrasterizer* r) static unsigned char* nsvg__alloc(NSVGrasterizer* r, int size) { unsigned char* buf; - if (size > NSVG__MEMPAGE_SIZE) return NULL; - if (r->curpage == NULL || r->curpage->size+size > NSVG__MEMPAGE_SIZE) { + if (size > NSVG__MEMPAGE_SIZE) return nullptr; + if (r->curpage == nullptr || r->curpage->size+size > NSVG__MEMPAGE_SIZE) { r->curpage = nsvg__nextPage(r, r->curpage); } buf = &r->curpage->mem[r->curpage->size]; @@ -230,35 +229,35 @@ static unsigned char* nsvg__alloc(NSVGrasterizer* r, int size) return buf; } -static int nsvg__ptEquals(float x1, float y1, float x2, float y2, float tol) +static int nsvg__ptEquals(double x1, double y1, double x2, double y2, double tol) { - float dx = x2 - x1; - float dy = y2 - y1; + double dx = x2 - x1; + double dy = y2 - y1; return dx*dx + dy*dy < tol*tol; } -static void nsvg__addPathPoint(NSVGrasterizer* r, float x, float y, int flags) +static void nsvg__addPathPoint(NSVGrasterizer* r, double x, double y, int flags) { NSVGpoint* pt; if (r->npoints > 0) { pt = &r->points[r->npoints-1]; if (nsvg__ptEquals(pt->x,pt->y, x,y, r->distTol)) { - pt->flags = (unsigned char)(pt->flags | flags); + pt->flags = static_cast(pt->flags | flags); return; } } if (r->npoints+1 > r->cpoints) { r->cpoints = r->cpoints > 0 ? r->cpoints * 2 : 64; - r->points = (NSVGpoint*)realloc(r->points, sizeof(NSVGpoint) * r->cpoints); - if (r->points == NULL) return; + r->points = static_cast(realloc(r->points, sizeof(NSVGpoint) * r->cpoints)); + if (r->points == nullptr) return; } pt = &r->points[r->npoints]; pt->x = x; pt->y = y; - pt->flags = (unsigned char)flags; + pt->flags = static_cast(flags); r->npoints++; } @@ -266,8 +265,8 @@ static void nsvg__appendPathPoint(NSVGrasterizer* r, NSVGpoint pt) { if (r->npoints+1 > r->cpoints) { r->cpoints = r->cpoints > 0 ? r->cpoints * 2 : 64; - r->points = (NSVGpoint*)realloc(r->points, sizeof(NSVGpoint) * r->cpoints); - if (r->points == NULL) return; + r->points = static_cast(realloc(r->points, sizeof(NSVGpoint) * r->cpoints)); + if (r->points == nullptr) return; } r->points[r->npoints] = pt; r->npoints++; @@ -277,15 +276,15 @@ static void nsvg__duplicatePoints(NSVGrasterizer* r) { if (r->npoints > r->cpoints2) { r->cpoints2 = r->npoints; - r->points2 = (NSVGpoint*)realloc(r->points2, sizeof(NSVGpoint) * r->cpoints2); - if (r->points2 == NULL) return; + r->points2 = static_cast(realloc(r->points2, sizeof(NSVGpoint) * r->cpoints2)); + if (r->points2 == nullptr) return; } memcpy(r->points2, r->points, sizeof(NSVGpoint) * r->npoints); r->npoints2 = r->npoints; } -static void nsvg__addEdge(NSVGrasterizer* r, float x0, float y0, float x1, float y1) +static void nsvg__addEdge(NSVGrasterizer* r, double x0, double y0, double x1, double y1) { NSVGedge* e; @@ -295,8 +294,8 @@ static void nsvg__addEdge(NSVGrasterizer* r, float x0, float y0, float x1, float if (r->nedges+1 > r->cedges) { r->cedges = r->cedges > 0 ? r->cedges * 2 : 64; - r->edges = (NSVGedge*)realloc(r->edges, sizeof(NSVGedge) * r->cedges); - if (r->edges == NULL) return; + r->edges = static_cast(realloc(r->edges, sizeof(NSVGedge) * r->cedges)); + if (r->edges == nullptr) return; } e = &r->edges[r->nedges]; @@ -317,37 +316,37 @@ static void nsvg__addEdge(NSVGrasterizer* r, float x0, float y0, float x1, float } } -static float nsvg__normalize(float *x, float* y) +static double nsvg__normalize(double *x, double* y) { - float d = sqrtf((*x)*(*x) + (*y)*(*y)); - if (d > 1e-6f) { - float id = 1.0f / d; + double d = sqrt((*x)*(*x) + (*y)*(*y)); + if (d > 1e-6) { + double id = 1.0 / d; *x *= id; *y *= id; } return d; } -static float nsvg__absf(float x) { return x < 0 ? -x : x; } +static double nsvg__absf(double x) { return x < 0 ? -x : x; } static void nsvg__flattenCubicBez(NSVGrasterizer* r, - float x1, float y1, float x2, float y2, - float x3, float y3, float x4, float y4, + double x1, double y1, double x2, double y2, + double x3, double y3, double x4, double y4, int level, int type) { - float x12,y12,x23,y23,x34,y34,x123,y123,x234,y234,x1234,y1234; - float dx,dy,d2,d3; + double x12,y12,x23,y23,x34,y34,x123,y123,x234,y234,x1234,y1234; + double dx,dy,d2,d3; if (level > 10) return; - x12 = (x1+x2)*0.5f; - y12 = (y1+y2)*0.5f; - x23 = (x2+x3)*0.5f; - y23 = (y2+y3)*0.5f; - x34 = (x3+x4)*0.5f; - y34 = (y3+y4)*0.5f; - x123 = (x12+x23)*0.5f; - y123 = (y12+y23)*0.5f; + x12 = (x1+x2)*0.5; + y12 = (y1+y2)*0.5; + x23 = (x2+x3)*0.5; + y23 = (y2+y3)*0.5; + x34 = (x3+x4)*0.5; + y34 = (y3+y4)*0.5; + x123 = (x12+x23)*0.5; + y123 = (y12+y23)*0.5; dx = x4 - x1; dy = y4 - y1; @@ -359,26 +358,26 @@ static void nsvg__flattenCubicBez(NSVGrasterizer* r, return; } - x234 = (x23+x34)*0.5f; - y234 = (y23+y34)*0.5f; - x1234 = (x123+x234)*0.5f; - y1234 = (y123+y234)*0.5f; + x234 = (x23+x34)*0.5; + y234 = (y23+y34)*0.5; + x1234 = (x123+x234)*0.5; + y1234 = (y123+y234)*0.5; nsvg__flattenCubicBez(r, x1,y1, x12,y12, x123,y123, x1234,y1234, level+1, 0); nsvg__flattenCubicBez(r, x1234,y1234, x234,y234, x34,y34, x4,y4, level+1, type); } -static void nsvg__flattenShape(NSVGrasterizer* r, NSVGshape* shape, float scale) +static void nsvg__flattenShape(NSVGrasterizer* r, NSVGshape* shape, double scale) { int i, j; NSVGpath* path; - for (path = shape->paths; path != NULL; path = path->next) { + for (path = shape->paths; path != nullptr; path = path->next) { r->npoints = 0; // Flatten path nsvg__addPathPoint(r, path->pts[0]*scale, path->pts[1]*scale, 0); for (i = 0; i < path->npts-1; i += 3) { - float* p = &path->pts[i*2]; + double* p = &path->pts[i*2]; nsvg__flattenCubicBez(r, p[0]*scale,p[1]*scale, p[2]*scale,p[3]*scale, p[4]*scale,p[5]*scale, p[6]*scale,p[7]*scale, 0, 0); } // Close path @@ -396,27 +395,27 @@ enum NSVGpointFlags NSVG_PT_LEFT = 0x04 }; -static void nsvg__initClosed(NSVGpoint* left, NSVGpoint* right, NSVGpoint* p0, NSVGpoint* p1, float lineWidth) +static void nsvg__initClosed(NSVGpoint* left, NSVGpoint* right, NSVGpoint* p0, NSVGpoint* p1, double lineWidth) { - float w = lineWidth * 0.5f; - float dx = p1->x - p0->x; - float dy = p1->y - p0->y; - float len = nsvg__normalize(&dx, &dy); - float px = p0->x + dx*len*0.5f, py = p0->y + dy*len*0.5f; - float dlx = dy, dly = -dx; - float lx = px - dlx*w, ly = py - dly*w; - float rx = px + dlx*w, ry = py + dly*w; + double w = lineWidth * 0.5; + double dx = p1->x - p0->x; + double dy = p1->y - p0->y; + double len = nsvg__normalize(&dx, &dy); + double px = p0->x + dx*len*0.5, py = p0->y + dy*len*0.5; + double dlx = dy, dly = -dx; + double lx = px - dlx*w, ly = py - dly*w; + double rx = px + dlx*w, ry = py + dly*w; left->x = lx; left->y = ly; right->x = rx; right->y = ry; } -static void nsvg__buttCap(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p, float dx, float dy, float lineWidth, int connect) +static void nsvg__buttCap(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p, double dx, double dy, double lineWidth, int connect) { - float w = lineWidth * 0.5f; - float px = p->x, py = p->y; - float dlx = dy, dly = -dx; - float lx = px - dlx*w, ly = py - dly*w; - float rx = px + dlx*w, ry = py + dly*w; + double w = lineWidth * 0.5; + double px = p->x, py = p->y; + double dlx = dy, dly = -dx; + double lx = px - dlx*w, ly = py - dly*w; + double rx = px + dlx*w, ry = py + dly*w; nsvg__addEdge(r, lx, ly, rx, ry); @@ -428,13 +427,13 @@ static void nsvg__buttCap(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, right->x = rx; right->y = ry; } -static void nsvg__squareCap(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p, float dx, float dy, float lineWidth, int connect) +static void nsvg__squareCap(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p, double dx, double dy, double lineWidth, int connect) { - float w = lineWidth * 0.5f; - float px = p->x - dx*w, py = p->y - dy*w; - float dlx = dy, dly = -dx; - float lx = px - dlx*w, ly = py - dly*w; - float rx = px + dlx*w, ry = py + dly*w; + double w = lineWidth * 0.5; + double px = p->x - dx*w, py = p->y - dy*w; + double dlx = dy, dly = -dx; + double lx = px - dlx*w, ly = py - dly*w; + double rx = px + dlx*w, ry = py + dly*w; nsvg__addEdge(r, lx, ly, rx, ry); @@ -447,22 +446,22 @@ static void nsvg__squareCap(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right } #ifndef NSVG_PI -#define NSVG_PI (3.14159265358979323846264338327f) +#define NSVG_PI (3.14159265358979323846264338327) #endif -static void nsvg__roundCap(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p, float dx, float dy, float lineWidth, int ncap, int connect) +static void nsvg__roundCap(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p, double dx, double dy, double lineWidth, int ncap, int connect) { int i; - float w = lineWidth * 0.5f; - float px = p->x, py = p->y; - float dlx = dy, dly = -dx; - float lx = 0, ly = 0, rx = 0, ry = 0, prevx = 0, prevy = 0; + double w = lineWidth * 0.5; + double px = p->x, py = p->y; + double dlx = dy, dly = -dx; + double lx = 0, ly = 0, rx = 0, ry = 0, prevx = 0, prevy = 0; for (i = 0; i < ncap; i++) { - 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; + double a = static_cast(i)/static_cast(ncap-1)*NSVG_PI; + double ax = cos(a) * w, ay = sin(a) * w; + double x = px - dlx*ax - dx*ay; + double y = py - dly*ax - dy*ay; if (i > 0) nsvg__addEdge(r, prevx, prevy, x, y); @@ -486,15 +485,15 @@ static void nsvg__roundCap(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, right->x = rx; right->y = ry; } -static void nsvg__bevelJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p0, NSVGpoint* p1, float lineWidth) +static void nsvg__bevelJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p0, NSVGpoint* p1, double lineWidth) { - float w = lineWidth * 0.5f; - float dlx0 = p0->dy, dly0 = -p0->dx; - float dlx1 = p1->dy, dly1 = -p1->dx; - float lx0 = p1->x - (dlx0 * w), ly0 = p1->y - (dly0 * w); - float rx0 = p1->x + (dlx0 * w), ry0 = p1->y + (dly0 * w); - float lx1 = p1->x - (dlx1 * w), ly1 = p1->y - (dly1 * w); - float rx1 = p1->x + (dlx1 * w), ry1 = p1->y + (dly1 * w); + double w = lineWidth * 0.5; + double dlx0 = p0->dy, dly0 = -p0->dx; + double dlx1 = p1->dy, dly1 = -p1->dx; + double lx0 = p1->x - (dlx0 * w), ly0 = p1->y - (dly0 * w); + double rx0 = p1->x + (dlx0 * w), ry0 = p1->y + (dly0 * w); + double lx1 = p1->x - (dlx1 * w), ly1 = p1->y - (dly1 * w); + double rx1 = p1->x + (dlx1 * w), ry1 = p1->y + (dly1 * w); nsvg__addEdge(r, lx0, ly0, left->x, left->y); nsvg__addEdge(r, lx1, ly1, lx0, ly0); @@ -506,13 +505,13 @@ static void nsvg__bevelJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right right->x = rx1; right->y = ry1; } -static void nsvg__miterJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p0, NSVGpoint* p1, float lineWidth) +static void nsvg__miterJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p0, NSVGpoint* p1, double lineWidth) { - float w = lineWidth * 0.5f; - float dlx0 = p0->dy, dly0 = -p0->dx; - float dlx1 = p1->dy, dly1 = -p1->dx; - float lx0, rx0, lx1, rx1; - float ly0, ry0, ly1, ry1; + double w = lineWidth * 0.5; + double dlx0 = p0->dy, dly0 = -p0->dx; + double dlx1 = p1->dy, dly1 = -p1->dx; + double lx0, rx0, lx1, rx1; + double ly0, ry0, ly1, ry1; if (p1->flags & NSVG_PT_LEFT) { lx0 = lx1 = p1->x - p1->dmx * w; @@ -542,21 +541,21 @@ static void nsvg__miterJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right right->x = rx1; right->y = ry1; } -static void nsvg__roundJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p0, NSVGpoint* p1, float lineWidth, int ncap) +static void nsvg__roundJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p0, NSVGpoint* p1, double lineWidth, int ncap) { int i, n; - float w = lineWidth * 0.5f; - float dlx0 = p0->dy, dly0 = -p0->dx; - float dlx1 = p1->dy, dly1 = -p1->dx; - float a0 = atan2f(dly0, dlx0); - float a1 = atan2f(dly1, dlx1); - float da = a1 - a0; - float lx, ly, rx, ry; + double w = lineWidth * 0.5; + double dlx0 = p0->dy, dly0 = -p0->dx; + double dlx1 = p1->dy, dly1 = -p1->dx; + double a0 = atan2(dly0, dlx0); + double a1 = atan2(dly1, dlx1); + double da = a1 - a0; + double lx, ly, rx, ry; if (da < NSVG_PI) da += NSVG_PI*2; if (da > NSVG_PI) da -= NSVG_PI*2; - n = (int)ceilf((nsvg__absf(da) / NSVG_PI) * (float)ncap); + n = static_cast(ceil((nsvg__absf(da) / NSVG_PI) * static_cast(ncap))); if (n < 2) n = 2; if (n > ncap) n = ncap; @@ -566,11 +565,11 @@ static void nsvg__roundJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right ry = right->y; for (i = 0; i < n; i++) { - 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; - float rx1 = p1->x + ax, ry1 = p1->y + ay; + double u = static_cast(i)/static_cast(n-1); + double a = a0 + u*da; + double ax = cos(a) * w, ay = sin(a) * w; + double lx1 = p1->x - ax, ly1 = p1->y - ay; + double rx1 = p1->x + ax, ry1 = p1->y + ay; nsvg__addEdge(r, lx1, ly1, lx, ly); nsvg__addEdge(r, rx, ry, rx1, ry1); @@ -583,11 +582,11 @@ static void nsvg__roundJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right right->x = rx; right->y = ry; } -static void nsvg__straightJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p1, float lineWidth) +static void nsvg__straightJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* right, NSVGpoint* p1, double lineWidth) { - float w = lineWidth * 0.5f; - float lx = p1->x - (p1->dmx * w), ly = p1->y - (p1->dmy * w); - float rx = p1->x + (p1->dmx * w), ry = p1->y + (p1->dmy * w); + double w = lineWidth * 0.5; + double lx = p1->x - (p1->dmx * w), ly = p1->y - (p1->dmy * w); + double rx = p1->x + (p1->dmx * w), ry = p1->y + (p1->dmy * w); nsvg__addEdge(r, lx, ly, left->x, left->y); nsvg__addEdge(r, right->x, right->y, rx, ry); @@ -596,17 +595,17 @@ static void nsvg__straightJoin(NSVGrasterizer* r, NSVGpoint* left, NSVGpoint* ri right->x = rx; right->y = ry; } -static int nsvg__curveDivs(float r, float arc, float tol) +static int nsvg__curveDivs(double r, double arc, double tol) { - float da = acosf(r / (r + tol)) * 2.0f; - int divs = (int)ceilf(arc / da); + double da = acos(r / (r + tol)) * 2.0; + int divs = static_cast(ceil(arc / da)); if (divs < 2) divs = 2; return divs; } -static void nsvg__expandStroke(NSVGrasterizer* r, NSVGpoint* points, int npoints, int closed, int lineJoin, int lineCap, float lineWidth) +static void nsvg__expandStroke(NSVGrasterizer* r, NSVGpoint* points, int npoints, int closed, int lineJoin, int lineCap, double lineWidth) { - int ncap = nsvg__curveDivs(lineWidth*0.5f, NSVG_PI, r->tessTol); // Calculate divisions per half circle. + int ncap = nsvg__curveDivs(lineWidth*0.5, NSVG_PI, r->tessTol); // Calculate divisions per half circle. NSVGpoint left = {0,0,0,0,0,0,0,0}, right = {0,0,0,0,0,0,0,0}, firstLeft = {0,0,0,0,0,0,0,0}, firstRight = {0,0,0,0,0,0,0,0}; NSVGpoint* p0, *p1; int j, s, e; @@ -632,8 +631,8 @@ static void nsvg__expandStroke(NSVGrasterizer* r, NSVGpoint* points, int npoints firstRight = right; } else { // Add cap - float dx = p1->x - p0->x; - float dy = p1->y - p0->y; + double dx = p1->x - p0->x; + double dy = p1->y - p0->y; nsvg__normalize(&dx, &dy); if (lineCap == NSVG_CAP_BUTT) nsvg__buttCap(r, &left, &right, p0, dx, dy, lineWidth, 0); @@ -663,8 +662,8 @@ static void nsvg__expandStroke(NSVGrasterizer* r, NSVGpoint* points, int npoints nsvg__addEdge(r, right.x, right.y, firstRight.x, firstRight.y); } else { // Add cap - float dx = p1->x - p0->x; - float dy = p1->y - p0->y; + double dx = p1->x - p0->x; + double dy = p1->y - p0->y; nsvg__normalize(&dx, &dy); if (lineCap == NSVG_CAP_BUTT) nsvg__buttCap(r, &right, &left, p1, -dx, -dy, lineWidth, 1); @@ -675,7 +674,7 @@ static void nsvg__expandStroke(NSVGrasterizer* r, NSVGpoint* points, int npoints } } -static void nsvg__prepareStroke(NSVGrasterizer* r, float miterLimit, int lineJoin) +static void nsvg__prepareStroke(NSVGrasterizer* r, double miterLimit, int lineJoin) { int i, j; NSVGpoint* p0, *p1; @@ -695,19 +694,19 @@ static void nsvg__prepareStroke(NSVGrasterizer* r, float miterLimit, int lineJoi p0 = &r->points[r->npoints-1]; p1 = &r->points[0]; for (j = 0; j < r->npoints; j++) { - float dlx0, dly0, dlx1, dly1, dmr2, cross; + double dlx0, dly0, dlx1, dly1, dmr2, cross; dlx0 = p0->dy; dly0 = -p0->dx; dlx1 = p1->dy; dly1 = -p1->dx; // Calculate extrusions - p1->dmx = (dlx0 + dlx1) * 0.5f; - p1->dmy = (dly0 + dly1) * 0.5f; + p1->dmx = (dlx0 + dlx1) * 0.5; + p1->dmy = (dly0 + dly1) * 0.5; dmr2 = p1->dmx*p1->dmx + p1->dmy*p1->dmy; - if (dmr2 > 0.000001f) { - float s2 = 1.0f / dmr2; - if (s2 > 600.0f) { - s2 = 600.0f; + if (dmr2 > 0.000001) { + double s2 = 1.0 / dmr2; + if (s2 > 600.0) { + s2 = 600.0; } p1->dmx *= s2; p1->dmy *= s2; @@ -718,12 +717,12 @@ static void nsvg__prepareStroke(NSVGrasterizer* r, float miterLimit, int lineJoi // Keep track of left turns. cross = p1->dx * p0->dy - p0->dx * p1->dy; - if (cross > 0.0f) + if (cross > 0.0) p1->flags |= NSVG_PT_LEFT; // Check to see if the corner needs to be beveled. if (p1->flags & NSVG_PT_CORNER) { - if ((dmr2 * miterLimit*miterLimit) < 1.0f || lineJoin == NSVG_JOIN_BEVEL || lineJoin == NSVG_JOIN_ROUND) { + if ((dmr2 * miterLimit*miterLimit) < 1.0 || lineJoin == NSVG_JOIN_BEVEL || lineJoin == NSVG_JOIN_ROUND) { p1->flags |= NSVG_PT_BEVEL; } } @@ -732,22 +731,22 @@ static void nsvg__prepareStroke(NSVGrasterizer* r, float miterLimit, int lineJoi } } -static void nsvg__flattenShapeStroke(NSVGrasterizer* r, NSVGshape* shape, float scale) +static void nsvg__flattenShapeStroke(NSVGrasterizer* r, NSVGshape* shape, double scale) { int i, j, closed; NSVGpath* path; NSVGpoint* p0, *p1; - float miterLimit = shape->miterLimit; + double miterLimit = shape->miterLimit; int lineJoin = shape->strokeLineJoin; int lineCap = shape->strokeLineCap; - float lineWidth = shape->strokeWidth * scale; + double lineWidth = shape->strokeWidth * scale; - for (path = shape->paths; path != NULL; path = path->next) { + for (path = shape->paths; path != nullptr; path = path->next) { // Flatten path r->npoints = 0; nsvg__addPathPoint(r, path->pts[0]*scale, path->pts[1]*scale, NSVG_PT_CORNER); for (i = 0; i < path->npts-1; i += 3) { - float* p = &path->pts[i*2]; + double* p = &path->pts[i*2]; nsvg__flattenCubicBez(r, p[0]*scale,p[1]*scale, p[2]*scale,p[3]*scale, p[4]*scale,p[5]*scale, p[6]*scale,p[7]*scale, 0, NSVG_PT_CORNER); } if (r->npoints < 2) @@ -766,7 +765,7 @@ static void nsvg__flattenShapeStroke(NSVGrasterizer* r, NSVGshape* shape, float if (shape->strokeDashCount > 0) { int idash = 0, dashState = 1; - float totalDist = 0, dashLen, allDashLen, dashOffset; + double totalDist = 0, dashLen, allDashLen, dashOffset; NSVGpoint cur; if (closed) @@ -784,10 +783,10 @@ static void nsvg__flattenShapeStroke(NSVGrasterizer* r, NSVGshape* shape, float for (j = 0; j < shape->strokeDashCount; j++) allDashLen += shape->strokeDashArray[j]; if (shape->strokeDashCount & 1) - allDashLen *= 2.0f; + allDashLen *= 2.0; // Find location inside pattern - dashOffset = fmodf(shape->strokeDashOffset, allDashLen); - if (dashOffset < 0.0f) + dashOffset = fmod(shape->strokeDashOffset, allDashLen); + if (dashOffset < 0.0) dashOffset += allDashLen; while (dashOffset > shape->strokeDashArray[idash]) { @@ -797,15 +796,15 @@ static void nsvg__flattenShapeStroke(NSVGrasterizer* r, NSVGshape* shape, float dashLen = (shape->strokeDashArray[idash] - dashOffset) * scale; for (j = 1; j < r->npoints2; ) { - float dx = r->points2[j].x - cur.x; - float dy = r->points2[j].y - cur.y; - float dist = sqrtf(dx*dx + dy*dy); + double dx = r->points2[j].x - cur.x; + double dy = r->points2[j].y - cur.y; + double dist = sqrt(dx*dx + dy*dy); if ((totalDist + dist) > dashLen) { // Calculate intermediate point - float d = (dashLen - totalDist) / dist; - float x = cur.x + dx * d; - float y = cur.y + dy * d; + double d = (dashLen - totalDist) / dist; + double x = cur.x + dx * d; + double y = cur.y + dy * d; nsvg__addPathPoint(r, x, y, NSVG_PT_CORNER); // Stroke @@ -821,7 +820,7 @@ static void nsvg__flattenShapeStroke(NSVGrasterizer* r, NSVGshape* shape, float cur.x = x; cur.y = y; cur.flags = NSVG_PT_CORNER; - totalDist = 0.0f; + totalDist = 0.0; r->npoints = 0; nsvg__appendPathPoint(r, cur); } else { @@ -843,8 +842,8 @@ static void nsvg__flattenShapeStroke(NSVGrasterizer* r, NSVGshape* shape, float static int nsvg__cmpEdge(const void *p, const void *q) { - const NSVGedge* a = (const NSVGedge*)p; - const NSVGedge* b = (const NSVGedge*)q; + const NSVGedge* a = static_cast(p); + const NSVGedge* b = static_cast(q); if (a->y0 < b->y0) return -1; if (a->y0 > b->y0) return 1; @@ -852,28 +851,28 @@ static int nsvg__cmpEdge(const void *p, const void *q) } -static NSVGactiveEdge* nsvg__addActive(NSVGrasterizer* r, NSVGedge* e, float startPoint) +static NSVGactiveEdge* nsvg__addActive(NSVGrasterizer* r, NSVGedge* e, double startPoint) { NSVGactiveEdge* z; - if (r->freelist != NULL) { + if (r->freelist != nullptr) { // Restore from freelist. z = r->freelist; r->freelist = z->next; } else { // Alloc new edge. - z = (NSVGactiveEdge*)nsvg__alloc(r, sizeof(NSVGactiveEdge)); - if (z == NULL) return NULL; + z = reinterpret_cast(nsvg__alloc(r, sizeof(NSVGactiveEdge))); + if (z == nullptr) return nullptr; } - float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0); + double dxdy = (e->x1 - e->x0) / (e->y1 - e->y0); // STBTT_assert(e->y0 <= start_point); // round dx down to avoid going too far if (dxdy < 0) - z->dx = (int)(-floorf(NSVG__FIX * -dxdy)); + z->dx = static_cast(-floor(NSVG__FIX * -dxdy)); else - z->dx = (int)floorf(NSVG__FIX * dxdy); - z->x = (int)floorf(NSVG__FIX * (e->x0 + dxdy * (startPoint - e->y0))); + z->dx = static_cast(floor(NSVG__FIX * dxdy)); + z->x = static_cast(floor(NSVG__FIX * (e->x0 + dxdy * (startPoint - e->y0)))); // z->x -= off_x * FIX; z->ey = e->y1; z->next = nullptr; @@ -897,20 +896,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)(scanline[i] + ((x1 - x0) * maxWeight >> NSVG__FIXSHIFT)); + scanline[i] = static_cast(scanline[i] + ((x1 - x0) * maxWeight >> NSVG__FIXSHIFT)); } else { if (i >= 0) // add antialiasing for x0 - scanline[i] = (unsigned char)(scanline[i] + (((NSVG__FIX - (x0 & NSVG__FIXMASK)) * maxWeight) >> NSVG__FIXSHIFT)); + scanline[i] = static_cast(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)(scanline[j] + (((x1 & NSVG__FIXMASK) * maxWeight) >> NSVG__FIXSHIFT)); + scanline[j] = static_cast(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)(scanline[i] + maxWeight); + scanline[i] = static_cast(scanline[i] + maxWeight); } } } @@ -925,7 +924,7 @@ static void nsvg__fillActiveEdges(unsigned char* scanline, int len, NSVGactiveEd if (fillRule == NSVG_FILLRULE_NONZERO) { // Non-zero - while (e != NULL) { + while (e != nullptr) { if (w == 0) { // if we're currently at zero, we need to record the edge start point x0 = e->x; w += e->dir; @@ -939,7 +938,7 @@ static void nsvg__fillActiveEdges(unsigned char* scanline, int len, NSVGactiveEd } } else if (fillRule == NSVG_FILLRULE_EVENODD) { // Even-odd - while (e != NULL) { + while (e != nullptr) { if (w == 0) { // if we're currently at zero, we need to record the edge start point x0 = e->x; w = 1; @@ -952,31 +951,31 @@ static void nsvg__fillActiveEdges(unsigned char* scanline, int len, NSVGactiveEd } } -static float nsvg__clampf(float a, float mn, float mx) { return a < mn ? mn : (a > mx ? mx : a); } +static double nsvg__clampf(double a, double mn, double mx) { return a < mn ? mn : (a > mx ? mx : a); } static unsigned int nsvg__RGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a) { return (r) | (g << 8) | (b << 16) | (a << 24); } -static unsigned int nsvg__lerpRGBA(unsigned int c0, unsigned int c1, float u) +static unsigned int nsvg__lerpRGBA(unsigned int c0, unsigned int c1, double u) { - int iu = (int)(nsvg__clampf(u, 0.0f, 1.0f) * 256.0f); + int iu = static_cast(nsvg__clampf(u, 0.0, 1.0) * 256.0); int r = (((c0) & 0xff)*(256-iu) + (((c1) & 0xff)*iu)) >> 8; int g = (((c0>>8) & 0xff)*(256-iu) + (((c1>>8) & 0xff)*iu)) >> 8; int b = (((c0>>16) & 0xff)*(256-iu) + (((c1>>16) & 0xff)*iu)) >> 8; int a = (((c0>>24) & 0xff)*(256-iu) + (((c1>>24) & 0xff)*iu)) >> 8; - return nsvg__RGBA((unsigned char)r, (unsigned char)g, (unsigned char)b, (unsigned char)a); + return nsvg__RGBA(static_cast(r), static_cast(g), static_cast(b), static_cast(a)); } -static unsigned int nsvg__applyOpacity(unsigned int c, float u) +static unsigned int nsvg__applyOpacity(unsigned int c, double u) { - int iu = (int)(nsvg__clampf(u, 0.0f, 1.0f) * 256.0f); + int iu = static_cast(nsvg__clampf(u, 0.0, 1.0) * 256.0); int r = (c) & 0xff; int g = (c>>8) & 0xff; int b = (c>>16) & 0xff; int a = (((c>>24) & 0xff)*iu) >> 8; - return nsvg__RGBA((unsigned char)r, (unsigned char)g, (unsigned char)b, (unsigned char)a); + return nsvg__RGBA(static_cast(r), static_cast(g), static_cast(b), static_cast(a)); } static inline int nsvg__div255(int x) @@ -985,7 +984,7 @@ static inline int nsvg__div255(int x) } static void nsvg__scanlineSolid(unsigned char* dst, int count, unsigned char* cover, int x, int y, - float tx, float ty, float scale, NSVGcachedPaint* cache) + double tx, double ty, double scale, NSVGcachedPaint* cache) { if (cache->type == NSVG_PAINT_COLOR) { @@ -997,7 +996,7 @@ static void nsvg__scanlineSolid(unsigned char* dst, int count, unsigned char* co for (i = 0; i < count; i++) { int r,g,b; - int a = nsvg__div255((int)cover[0] * ca); + int a = nsvg__div255(static_cast(cover[0]) * ca); int ia = 255 - a; // Premultiply r = nsvg__div255(cr * a); @@ -1005,15 +1004,15 @@ static void nsvg__scanlineSolid(unsigned char* dst, int count, unsigned char* co b = nsvg__div255(cb * a); // Blend over - r += nsvg__div255(ia * (int)dst[0]); - g += nsvg__div255(ia * (int)dst[1]); - b += nsvg__div255(ia * (int)dst[2]); - a += nsvg__div255(ia * (int)dst[3]); + r += nsvg__div255(ia * static_cast(dst[0])); + g += nsvg__div255(ia * static_cast(dst[1])); + b += nsvg__div255(ia * static_cast(dst[2])); + a += nsvg__div255(ia * static_cast(dst[3])); - dst[0] = (unsigned char)r; - dst[1] = (unsigned char)g; - dst[2] = (unsigned char)b; - dst[3] = (unsigned char)a; + dst[0] = static_cast(r); + dst[1] = static_cast(g); + dst[2] = static_cast(b); + dst[3] = static_cast(a); cover++; dst += 4; @@ -1021,25 +1020,25 @@ static void nsvg__scanlineSolid(unsigned char* dst, int count, unsigned char* co } else if (cache->type == NSVG_PAINT_LINEAR_GRADIENT) { // TODO: spread modes. // TODO: plenty of opportunities to optimize. - float fx, fy, dx, gy; - float* t = cache->xform; + double fx, fy, dx, gy; + double* t = cache->xform; int i, cr, cg, cb, ca; unsigned int c; - fx = ((float)x - tx) / scale; - fy = ((float)y - ty) / scale; - dx = 1.0f / scale; + fx = (static_cast(x) - tx) / scale; + fy = (static_cast(y) - ty) / scale; + dx = 1.0 / scale; for (i = 0; i < count; i++) { int r,g,b,a,ia; gy = fx*t[1] + fy*t[3] + t[5]; - c = cache->colors[(int)nsvg__clampf(gy*255.0f, 0, 255.0f)]; + c = cache->colors[static_cast(nsvg__clampf(gy*255.0, 0, 255.0))]; cr = (c) & 0xff; cg = (c >> 8) & 0xff; cb = (c >> 16) & 0xff; ca = (c >> 24) & 0xff; - a = nsvg__div255((int)cover[0] * ca); + a = nsvg__div255(static_cast(cover[0]) * ca); ia = 255 - a; // Premultiply @@ -1048,15 +1047,15 @@ static void nsvg__scanlineSolid(unsigned char* dst, int count, unsigned char* co b = nsvg__div255(cb * a); // Blend over - r += nsvg__div255(ia * (int)dst[0]); - g += nsvg__div255(ia * (int)dst[1]); - b += nsvg__div255(ia * (int)dst[2]); - a += nsvg__div255(ia * (int)dst[3]); + r += nsvg__div255(ia * static_cast(dst[0])); + g += nsvg__div255(ia * static_cast(dst[1])); + b += nsvg__div255(ia * static_cast(dst[2])); + a += nsvg__div255(ia * static_cast(dst[3])); - dst[0] = (unsigned char)r; - dst[1] = (unsigned char)g; - dst[2] = (unsigned char)b; - dst[3] = (unsigned char)a; + dst[0] = static_cast(r); + dst[1] = static_cast(g); + dst[2] = static_cast(b); + dst[3] = static_cast(a); cover++; dst += 4; @@ -1066,27 +1065,27 @@ static void nsvg__scanlineSolid(unsigned char* dst, int count, unsigned char* co // TODO: spread modes. // TODO: plenty of opportunities to optimize. // TODO: focus (fx,fy) - float fx, fy, dx, gx, gy, gd; - float* t = cache->xform; + double fx, fy, dx, gx, gy, gd; + double* t = cache->xform; int i, cr, cg, cb, ca; unsigned int c; - fx = ((float)x - tx) / scale; - fy = ((float)y - ty) / scale; - dx = 1.0f / scale; + fx = (static_cast(x) - tx) / scale; + fy = (static_cast(y) - ty) / scale; + dx = 1.0 / scale; for (i = 0; i < count; i++) { int r,g,b,a,ia; gx = fx*t[0] + fy*t[2] + t[4]; gy = fx*t[1] + fy*t[3] + t[5]; - gd = sqrtf(gx*gx + gy*gy); - c = cache->colors[(int)nsvg__clampf(gd*255.0f, 0, 255.0f)]; + gd = sqrt(gx*gx + gy*gy); + c = cache->colors[static_cast(nsvg__clampf(gd*255.0, 0, 255.0))]; cr = (c) & 0xff; cg = (c >> 8) & 0xff; cb = (c >> 16) & 0xff; ca = (c >> 24) & 0xff; - a = nsvg__div255((int)cover[0] * ca); + a = nsvg__div255(static_cast(cover[0]) * ca); ia = 255 - a; // Premultiply @@ -1095,15 +1094,15 @@ static void nsvg__scanlineSolid(unsigned char* dst, int count, unsigned char* co b = nsvg__div255(cb * a); // Blend over - r += nsvg__div255(ia * (int)dst[0]); - g += nsvg__div255(ia * (int)dst[1]); - b += nsvg__div255(ia * (int)dst[2]); - a += nsvg__div255(ia * (int)dst[3]); + r += nsvg__div255(ia * static_cast(dst[0])); + g += nsvg__div255(ia * static_cast(dst[1])); + b += nsvg__div255(ia * static_cast(dst[2])); + a += nsvg__div255(ia * static_cast(dst[3])); - dst[0] = (unsigned char)r; - dst[1] = (unsigned char)g; - dst[2] = (unsigned char)b; - dst[3] = (unsigned char)a; + dst[0] = static_cast(r); + dst[1] = static_cast(g); + dst[2] = static_cast(b); + dst[3] = static_cast(a); cover++; dst += 4; @@ -1112,9 +1111,9 @@ static void nsvg__scanlineSolid(unsigned char* dst, int count, unsigned char* co } } -static void nsvg__rasterizeSortedEdges(NSVGrasterizer *r, float tx, float ty, float scale, NSVGcachedPaint* cache, char fillRule) +static void nsvg__rasterizeSortedEdges(NSVGrasterizer *r, double tx, double ty, double scale, NSVGcachedPaint* cache, char fillRule) { - NSVGactiveEdge *active = NULL; + NSVGactiveEdge *active = nullptr; int y, s; int e = 0; int maxWeight = (255 / NSVG__SUBSAMPLES); // weight per vertical scanline @@ -1126,7 +1125,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 = (float)(y*NSVG__SUBSAMPLES + s) + 0.5f; + double scany = static_cast(y*NSVG__SUBSAMPLES + s) + 0.5; NSVGactiveEdge **step = &active; // update all active edges; @@ -1165,9 +1164,9 @@ static void nsvg__rasterizeSortedEdges(NSVGrasterizer *r, float tx, float ty, fl while (e < r->nedges && r->edges[e].y0 <= scany) { if (r->edges[e].y1 > scany) { NSVGactiveEdge* z = nsvg__addActive(r, &r->edges[e], scany); - if (z == NULL) break; + if (z == nullptr) break; // find insertion point - if (active == NULL) { + if (active == nullptr) { active = z; } else if (z->x < active->x) { // insert at front @@ -1187,7 +1186,7 @@ static void nsvg__rasterizeSortedEdges(NSVGrasterizer *r, float tx, float ty, fl } // now process all active edges in non-zero fashion - if (active != NULL) + if (active != nullptr) nsvg__fillActiveEdges(r->scanline, r->width, active, maxWeight, &xmin, &xmax, fillRule); } // Blit @@ -1210,9 +1209,9 @@ static void nsvg__unpremultiplyAlpha(unsigned char* image, int w, int h, int str for (x = 0; x < w; x++) { int r = row[0], g = row[1], b = row[2], a = row[3]; if (a != 0) { - row[0] = (unsigned char)(r*255/a); - row[1] = (unsigned char)(g*255/a); - row[2] = (unsigned char)(b*255/a); + row[0] = static_cast(r*255/a); + row[1] = static_cast(g*255/a); + row[2] = static_cast(b*255/a); } row += 4; } @@ -1249,9 +1248,9 @@ static void nsvg__unpremultiplyAlpha(unsigned char* image, int w, int h, int str n++; } if (n > 0) { - row[0] = (unsigned char)(r/n); - row[1] = (unsigned char)(g/n); - row[2] = (unsigned char)(b/n); + row[0] = static_cast(r/n); + row[1] = static_cast(g/n); + row[2] = static_cast(b/n); } } row += 4; @@ -1260,7 +1259,7 @@ static void nsvg__unpremultiplyAlpha(unsigned char* image, int w, int h, int str } -static void nsvg__initPaint(NSVGcachedPaint* cache, NSVGpaint* paint, float opacity) +static void nsvg__initPaint(NSVGcachedPaint* cache, NSVGpaint* paint, double opacity) { int i, j; NSVGgradient* grad; @@ -1275,7 +1274,7 @@ static void nsvg__initPaint(NSVGcachedPaint* cache, NSVGpaint* paint, float opac grad = paint->gradient; cache->spread = grad->spread; - memcpy(cache->xform, grad->xform, sizeof(float)*6); + memcpy(cache->xform, grad->xform, sizeof(double)*6); if (grad->nstops == 0) { for (i = 0; i < 256; i++) @@ -1285,14 +1284,14 @@ static void nsvg__initPaint(NSVGcachedPaint* cache, NSVGpaint* paint, float opac cache->colors[i] = nsvg__applyOpacity(grad->stops[i].color, opacity); } else { unsigned int ca, cb = 0; - float ua, ub, du, u; + double ua, ub, du, u; int ia, ib, count; ca = nsvg__applyOpacity(grad->stops[0].color, opacity); ua = nsvg__clampf(grad->stops[0].offset, 0, 1); ub = nsvg__clampf(grad->stops[grad->nstops-1].offset, ua, 1); - ia = (int)(ua * 255.0f); - ib = (int)(ub * 255.0f); + ia = static_cast(ua * 255.0); + ib = static_cast(ub * 255.0); for (i = 0; i < ia; i++) { cache->colors[i] = ca; } @@ -1302,12 +1301,12 @@ static void nsvg__initPaint(NSVGcachedPaint* cache, NSVGpaint* paint, float opac cb = nsvg__applyOpacity(grad->stops[i+1].color, opacity); ua = nsvg__clampf(grad->stops[i].offset, 0, 1); ub = nsvg__clampf(grad->stops[i+1].offset, 0, 1); - ia = (int)(ua * 255.0f); - ib = (int)(ub * 255.0f); + ia = static_cast(ua * 255.0); + ib = static_cast(ub * 255.0); count = ib - ia; if (count <= 0) continue; u = 0; - du = 1.0f / (float)count; + du = 1.0 / static_cast(count); for (j = 0; j < count; j++) { cache->colors[ia+j] = nsvg__lerpRGBA(ca,cb,u); u += du; @@ -1323,12 +1322,12 @@ static void nsvg__initPaint(NSVGcachedPaint* cache, NSVGpaint* paint, float opac /* static void dumpEdges(NSVGrasterizer* r, const char* name) { - float xmin = 0, xmax = 0, ymin = 0, ymax = 0; - NSVGedge *e = NULL; + double xmin = 0, xmax = 0, ymin = 0, ymax = 0; + NSVGedge *e = nullptr; int i; if (r->nedges == 0) return; FILE* fp = fopen(name, "w"); - if (fp == NULL) return; + if (fp == nullptr) return; xmin = xmax = r->edges[0].x0; ymin = ymax = r->edges[0].y0; @@ -1363,11 +1362,11 @@ static void dumpEdges(NSVGrasterizer* r, const char* name) */ void nsvgRasterize(NSVGrasterizer* r, - NSVGimage* image, float tx, float ty, float scale, + NSVGimage* image, double tx, double ty, double scale, unsigned char* dst, int w, int h, int stride) { - NSVGshape *shape = NULL; - NSVGedge *e = NULL; + NSVGshape *shape = nullptr; + NSVGedge *e = nullptr; NSVGcachedPaint cache; int i; @@ -1378,20 +1377,20 @@ void nsvgRasterize(NSVGrasterizer* r, if (w > r->cscanline) { r->cscanline = w; - r->scanline = (unsigned char*)realloc(r->scanline, w); - if (r->scanline == NULL) return; + r->scanline = static_cast(realloc(r->scanline, w)); + if (r->scanline == nullptr) return; } for (i = 0; i < h; i++) memset(&dst[i*stride], 0, w*4); - for (shape = image->shapes; shape != NULL; shape = shape->next) { + for (shape = image->shapes; shape != nullptr; shape = shape->next) { if (!(shape->flags & NSVG_FLAGS_VISIBLE)) continue; if (shape->fill.type != NSVG_PAINT_NONE) { nsvg__resetPool(r); - r->freelist = NULL; + r->freelist = nullptr; r->nedges = 0; nsvg__flattenShape(r, shape, scale); @@ -1413,9 +1412,9 @@ void nsvgRasterize(NSVGrasterizer* r, nsvg__rasterizeSortedEdges(r, tx,ty,scale, &cache, shape->fillRule); } - if (shape->stroke.type != NSVG_PAINT_NONE && (shape->strokeWidth * scale) > 0.01f) { + if (shape->stroke.type != NSVG_PAINT_NONE && (shape->strokeWidth * scale) > 0.01) { nsvg__resetPool(r); - r->freelist = NULL; + r->freelist = nullptr; r->nedges = 0; nsvg__flattenShapeStroke(r, shape, scale); @@ -1443,11 +1442,12 @@ void nsvgRasterize(NSVGrasterizer* r, nsvg__unpremultiplyAlpha(dst, w, h, stride); - r->bitmap = NULL; + r->bitmap = nullptr; r->width = 0; r->height = 0; r->stride = 0; } -#endif +} +#endif diff --git a/src/TGUI/SvgImage.cpp b/src/TGUI/SvgImage.cpp index 677fcd88..a7893707 100644 --- a/src/TGUI/SvgImage.cpp +++ b/src/TGUI/SvgImage.cpp @@ -73,7 +73,7 @@ namespace tgui Vector2f SvgImage::getSize() const { if (m_svg) - return {m_svg->width, m_svg->height}; + return {static_cast(m_svg->width), static_cast(m_svg->height)}; else return {0, 0}; } @@ -94,8 +94,8 @@ namespace tgui return; } - float scaleX = size.x / m_svg->width; - float scaleY = size.y / m_svg->height; + float scaleX = size.x / static_cast(m_svg->width); + float scaleY = size.y / static_cast(m_svg->height); float scale = std::min(scaleX, scaleY); auto pixels = std::make_unique(size.x * size.y * 4);