Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce the variable scope, if possible #173

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions src/nanosvg.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,6 @@ static void nsvg__parseElement(char* s,
char* name;
int start = 0;
int end = 0;
char quote;

// Skip white space after the '<'
while (*s && nsvg__isspace(*s)) s++;
Expand All @@ -288,6 +287,7 @@ static void nsvg__parseElement(char* s,
while (!end && *s && nattr < NSVG_XML_MAX_ATTRIBS-3) {
char* name = NULL;
char* value = NULL;
char quote;

// Skip white space before the attrib name
while (*s && nsvg__isspace(*s)) s++;
Expand Down Expand Up @@ -567,8 +567,8 @@ static double nsvg__evalBezier(double t, double p0, double p1, double p2, double

static void nsvg__curveBounds(float* bounds, float* curve)
{
int i, j, count;
double roots[2], a, b, c, b2ac, t, v;
int i, j;
double roots[2], b2ac, t, v;
float* v0 = &curve[0];
float* v1 = &curve[2];
float* v2 = &curve[4];
Expand All @@ -587,6 +587,8 @@ static void nsvg__curveBounds(float* bounds, float* curve)

// Add bezier curve inflection points in X and Y.
for (i = 0; i < 2; i++) {
double a, b, c;
int count;
a = -3.0 * v0[i] + 9.0 * v1[i] - 9.0 * v2[i] + 3.0 * v3[i];
b = 6.0 * v0[i] - 12.0 * v1[i] + 6.0 * v2[i];
c = 3.0 * v1[i] - 3.0 * v0[i];
Expand Down Expand Up @@ -722,8 +724,8 @@ static void nsvg__moveTo(NSVGparser* p, float x, float y)

static void nsvg__lineTo(NSVGparser* p, float x, float y)
{
float px,py, dx,dy;
if (p->npts > 0) {
float px,py, dx,dy;
px = p->pts[(p->npts-1)*2+0];
py = p->pts[(p->npts-1)*2+1];
dx = x - px;
Expand Down Expand Up @@ -1088,7 +1090,6 @@ static double nsvg__atof(const char* s)
char* cur = (char*)s;
char* end = NULL;
double res = 0.0, sign = 1.0;
long long intPart = 0, fracPart = 0;
char hasIntPart = 0, hasFracPart = 0;

// Parse optional sign
Expand All @@ -1102,6 +1103,7 @@ static double nsvg__atof(const char* s)
// Parse integer part
if (nsvg__isdigit(*cur)) {
// Parse digit sequence
long long intPart;
intPart = strtoll(cur, &end, 10);
if (cur != end) {
res = (double)intPart;
Expand All @@ -1115,6 +1117,7 @@ static double nsvg__atof(const char* s)
cur++; // Skip '.'
if (nsvg__isdigit(*cur)) {
// Parse digit sequence
long long fracPart;
fracPart = strtoll(cur, &end, 10);
if (cur != end) {
res += (double)fracPart / pow(10.0, (double)(end - cur));
Expand Down Expand Up @@ -1819,10 +1822,10 @@ static int nsvg__parseNameValue(NSVGparser* p, const char* start, const char* en

static void nsvg__parseStyle(NSVGparser* p, const char* str)
{
const char* start;
const char* end;

while (*str) {
const char* start;
const char* end;
// Left Trim
while(*str && nsvg__isspace(*str)) ++str;
start = str;
Expand Down Expand Up @@ -2071,7 +2074,7 @@ static void nsvg__pathArcTo(NSVGparser* p, float* cpx, float* cpy, float* args,
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 x, y, tanx, tany, px = 0, py = 0, ptanx = 0, ptany = 0, t[6];
float sinrx, cosrx;
int fa, fs;
int i, ndivs;
Expand Down Expand Up @@ -2163,6 +2166,7 @@ static void nsvg__pathArcTo(NSVGparser* p, float* cpx, float* cpy, float* args,
kappa = -kappa;

for (i = 0; i <= ndivs; i++) {
float a;
a = a1 + da * ((float)i/(float)ndivs);
dx = cosf(a);
dy = sinf(a);
Expand All @@ -2183,13 +2187,9 @@ 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;
char cmd = '\0';
float args[10];
int nargs;
int rargs = 0;
float cpx, cpy, cpx2, cpy2;
const char* tmp[4];
char closedFlag;
int i;
char item[64];

Expand All @@ -2206,6 +2206,10 @@ static void nsvg__parsePath(NSVGparser* p, const char** attr)
}

if (s) {
char cmd = '\0';
int nargs;
int rargs = 0;
char closedFlag;
nsvg__resetPath(p);
cpx = 0; cpy = 0;
cpx2 = 0; cpy2 = 0;
Expand Down
30 changes: 17 additions & 13 deletions src/nanosvgrast.h
Original file line number Diff line number Diff line change
Expand Up @@ -1021,17 +1021,18 @@ 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 fx, fy, dx;
float* t = cache->xform;
int i, cr, cg, cb, ca;
unsigned int c;
int i;

fx = ((float)x - tx) / scale;
fy = ((float)y - ty) / scale;
dx = 1.0f / scale;

for (i = 0; i < count; i++) {
int r,g,b,a,ia;
float gy;
unsigned int c;
int r,g,b,a,ia, cr, cg, cb, ca;
gy = fx*t[1] + fy*t[3] + t[5];
c = cache->colors[(int)nsvg__clampf(gy*255.0f, 0, 255.0f)];
cr = (c) & 0xff;
Expand Down Expand Up @@ -1066,17 +1067,18 @@ 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 fx, fy, dx;
float* t = cache->xform;
int i, cr, cg, cb, ca;
unsigned int c;
int i;

fx = ((float)x - tx) / scale;
fy = ((float)y - ty) / scale;
dx = 1.0f / scale;

for (i = 0; i < count; i++) {
int r,g,b,a,ia;
float gx, gy, gd;
int r,g,b,a,ia,ca,cr,cg,cb;
unsigned int c;
gx = fx*t[0] + fy*t[2] + t[4];
gy = fx*t[1] + fy*t[3] + t[5];
gd = sqrtf(gx*gx + gy*gy);
Expand Down Expand Up @@ -1222,8 +1224,9 @@ static void nsvg__unpremultiplyAlpha(unsigned char* image, int w, int h, int str
for (y = 0; y < h; y++) {
unsigned char *row = &image[y*stride];
for (x = 0; x < w; x++) {
int r = 0, g = 0, b = 0, a = row[3], n = 0;
int a = row[3];
if (a == 0) {
int n = 0, r = 0, g = 0, b = 0;
if (x-1 > 0 && row[-1] != 0) {
r += row[-4];
g += row[-3];
Expand Down Expand Up @@ -1262,7 +1265,7 @@ static void nsvg__unpremultiplyAlpha(unsigned char* image, int w, int h, int str

static void nsvg__initPaint(NSVGcachedPaint* cache, NSVGpaint* paint, float opacity)
{
int i, j;
int i;
NSVGgradient* grad;

cache->type = paint->type;
Expand All @@ -1286,7 +1289,7 @@ static void nsvg__initPaint(NSVGcachedPaint* cache, NSVGpaint* paint, float opac
} else {
unsigned int ca, cb = 0;
float ua, ub, du, u;
int ia, ib, count;
int ia, ib;

ca = nsvg__applyOpacity(grad->stops[0].color, opacity);
ua = nsvg__clampf(grad->stops[0].offset, 0, 1);
Expand All @@ -1296,18 +1299,19 @@ static void nsvg__initPaint(NSVGcachedPaint* cache, NSVGpaint* paint, float opac
for (i = 0; i < ia; i++) {
cache->colors[i] = ca;
}

Copy link

Choose a reason for hiding this comment

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

:(

for (i = 0; i < grad->nstops-1; i++) {
ca = nsvg__applyOpacity(grad->stops[i].color, opacity);
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);
count = ib - ia;
int count = ib - ia;
if (count <= 0) continue;
u = 0;
du = 1.0f / (float)count;
int j;
for (j = 0; j < count; j++) {
cache->colors[ia+j] = nsvg__lerpRGBA(ca,cb,u);
u += du;
Expand Down