static void svgParseRect(struct SVGParser* p, const char** attr) { float x = 0.0f; float y = 0.0f; float w = 0.0f; float h = 0.0f; int i; for (i = 0; attr[i]; i += 2) { if (!svgParseAttr(p, attr[i], attr[i + 1])) { if (strcmp(attr[i], "x") == 0) x = parseFloat(attr[i+1]); if (strcmp(attr[i], "y") == 0) y = parseFloat(attr[i+1]); if (strcmp(attr[i], "width") == 0) w = parseFloat(attr[i+1]); if (strcmp(attr[i], "height") == 0) h = parseFloat(attr[i+1]); } } if (w != 0.0f && h != 0.0f) { svgResetPath(p); svgPathPoint(p, x, y); svgPathPoint(p, x+w, y); svgPathPoint(p, x+w, y+h); svgPathPoint(p, x, y+h); svgCreatePath(p, 1); } }
static void svgParseLine(struct SVGParser* p, const char** attr) { float x1 = 0.0; float y1 = 0.0; float x2 = 0.0; float y2 = 0.0; int i; for (i = 0; attr[i]; i += 2) { if (!svgParseAttr(p, attr[i], attr[i + 1])) { if (strcmp(attr[i], "x1") == 0) x1 = parseFloat(attr[i + 1]); if (strcmp(attr[i], "y1") == 0) y1 = parseFloat(attr[i + 1]); if (strcmp(attr[i], "x2") == 0) x2 = parseFloat(attr[i + 1]); if (strcmp(attr[i], "y2") == 0) y2 = parseFloat(attr[i + 1]); } } svgResetPath(p); svgPathPoint(p, x1, y1); svgPathPoint(p, x2, y2); svgCreatePath(p, 0); }
static void quadBezRec(struct SVGParser* p, float x1, float y1, float x2, float y2, float x3, float y3, int level) { float x12,y12,x23,y23,x123,y123,d; if (level > 12) return; x12 = (x1+x2)*0.5f; y12 = (y1+y2)*0.5f; x23 = (x2+x3)*0.5f; y23 = (y2+y3)*0.5f; x123 = (x12+x23)*0.5f; y123 = (y12+y23)*0.5f; d = distPtSeg(x123, y123, x1,y1, x3,y3); if (level > 0 && d < p->tol*p->tol) { svgPathPoint(p, x123, y123); return; } quadBezRec(p, x1,y1, x12,y12, x123,y123, level+1); quadBezRec(p, x123,y123, x23,y23, x3,y3, level+1); }
static void cubicBez(struct SVGParser* p, float x1, float y1, float cx1, float cy1, float cx2, float cy2, float x2, float y2) { cubicBezRec(p, x1,y1, cx1,cy1, cx2,cy2, x2,y2, 0); svgPathPoint(p, x2, y2); }
static void cubicBezRec(struct SVGParser* p, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, int level) { float x12,y12,x23,y23,x34,y34,x123,y123,x234,y234,x1234,y1234; float d; if (level > 12) 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; x234 = (x23+x34)*0.5f; y234 = (y23+y34)*0.5f; x1234 = (x123+x234)*0.5f; y1234 = (y123+y234)*0.5f; d = distPtSeg(x1234, y1234, x1,y1, x4,y4); if (level > 0 && d < p->tol*p->tol) { svgPathPoint(p, x1234, y1234); return; } cubicBezRec(p, x1,y1, x12,y12, x123,y123, x1234,y1234, level+1); cubicBezRec(p, x1234,y1234, x234,y234, x34,y34, x4,y4, level+1); }
static void svgParsePoly(struct SVGParser* p, const char** attr, int closeFlag) { int i; const char* s; float args[2]; int nargs; char item[64]; svgResetPath(p); for (i = 0; attr[i]; i += 2) { if (!svgParseAttr(p, attr[i], attr[i + 1])) { if (strcmp(attr[i], "points") == 0) { s = attr[i + 1]; nargs = 0; while (*s) { s = getNextPathItem(s, item); args[nargs++] = (float)atof(item); if (nargs >= 2) { svgPathPoint(p, args[0], args[1]); nargs = 0; } } } } } svgCreatePath(p, closeFlag); }
void ofxSVGPathParser::cubicBezRec(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, int level) { float x12,y12,x23,y23,x34,y34,x123,y123,x234,y234,x1234,y1234; float d; if (level > 12) 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; x234 = (x23+x34)*0.5f; y234 = (y23+y34)*0.5f; x1234 = (x123+x234)*0.5f; y1234 = (y123+y234)*0.5f; d = distPtSeg(x1234, y1234, x1,y1, x4,y4); if (level > 0 && d < 1000)//tol*tol) { svgPathPoint(x1234, y1234); return; } cubicBezRec(x1,y1, x12,y12, x123,y123, x1234,y1234, level+1); cubicBezRec(x1234,y1234, x234,y234, x34,y34, x4,y4, level+1); }
static void pathVLineTo(struct SVGParser* p, float* cpx, float* cpy, float* args, int rel) { if (rel) *cpy += args[0]; else *cpy = args[0]; svgPathPoint(p, *cpx, *cpy); }
void ofxSVGPathParser::pathVLineTo(float* cpx, float* cpy, float* args, int rel) { if (rel) *cpy += args[0]; else *cpy = args[0]; svgPathPoint(*cpx, *cpy); }
static void pathLineTo(struct SVGParser* p, float* cpx, float* cpy, float* args, int rel) { if (rel) { *cpx += args[0]; *cpy += args[1]; } else { *cpx = args[0]; *cpy = args[1]; } svgPathPoint(p, *cpx, *cpy); }
void ofxSVGPathParser::pathLineTo(float* cpx, float* cpy, float* args, int rel) { if (rel) { *cpx += args[0]; *cpy += args[1]; } else { *cpx = args[0]; *cpy = args[1]; } svgPathPoint(*cpx, *cpy); }
static void svgParseCircle(struct SVGParser* p, const char** attr) { float cx = 0.0f; float cy = 0.0f; float r = 0.0f; float da; int i,n; float x,y,u; for (i = 0; attr[i]; i += 2) { if (!svgParseAttr(p, attr[i], attr[i + 1])) { if (strcmp(attr[i], "cx") == 0) cx = parseFloat(attr[i+1]); if (strcmp(attr[i], "cy") == 0) cy = parseFloat(attr[i+1]); if (strcmp(attr[i], "r") == 0) r = fabsf(parseFloat(attr[i+1])); } } if (r != 0.0f) { svgResetPath(p); da = acosf(r/(r+p->tol))*2; n = (int)ceilf(M_PI*2/da); da = (float)(M_PI*2)/n; for (i = 0; i < n; ++i) { u = i*da; x = cx + cosf(u)*r; y = cy + sinf(u)*r; svgPathPoint(p, x, y); } svgCreatePath(p, 1); } }
void ofxSVGPathParser::quadBezRec( float x1, float y1, float x2, float y2, float x3, float y3, int level) { float x12,y12,x23,y23,x123,y123,d; if (level > 12) return; // don't go too deep x12 = (x1+x2)*0.5f; y12 = (y1+y2)*0.5f; x23 = (x2+x3)*0.5f; y23 = (y2+y3)*0.5f; x123 = (x12+x23)*0.5f; y123 = (y12+y23)*0.5f; d = distPtSeg(x123, y123, x1,y1, x3,y3); if (level > 0 && d < 1000) // tol*tol) { svgPathPoint(x123, y123); return; } quadBezRec(x1,y1, x12,y12, x123,y123, level+1); quadBezRec(x123,y123, x23,y23, x3,y3, level+1); }
static void quadBez(struct SVGParser* p, float x1, float y1, float cx, float cy, float x2, float y2) { quadBezRec(p, x1,y1, cx,cy, x2,y2, 0); svgPathPoint(p, x2, y2); }
void ofxSVGPathParser::quadBez(float x1, float y1, float cx, float cy, float x2, float y2) { quadBezRec(x1,y1, cx,cy, x2,y2, 0); svgPathPoint(x2, y2); }
void ofxSVGPathParser::cubicBez(float x1, float y1, float cx1, float cy1, float cx2, float cy2, float x2, float y2) { cubicBezRec(x1,y1, cx1,cy1, cx2,cy2, x2,y2, 0); svgPathPoint(x2, y2); }