18 #define NEW(t) (t*)calloc(1, sizeof(t))
19 #define N_NEW(n,t) (t*)calloc((n), sizeof(t))
28 #define agxbputc(X,C) ((((X)->ptr >= (X)->eptr) ? agxbmore(X,1) : 0), \
29 (void)(*(X)->ptr++ = ((unsigned char)C)))
30 #define agxbuse(X) (agxbputc(X,'\0'),(char*)((X)->ptr = (X)->buf))
32 static void agxbinit(
agxbuf * xb,
unsigned int hint,
unsigned char *init)
41 xb->
buf =
N_NEW(hint,
unsigned char);
56 if (size + ssz > nsize)
60 nbuf = realloc(xb->
buf, nsize);
62 nbuf =
N_NEW(nsize,
unsigned char);
63 memcpy(nbuf, xb->
buf, cnt);
74 unsigned int ssz = strlen(s);
77 memcpy(xb->
ptr, s, ssz);
92 static char *parseReal(
char *s,
double *fp)
105 static char *parseInt(
char *s,
int *ip)
110 r = sscanf(s,
"%d%n", ip, &sz);
111 if (r != 1)
return 0;
112 else return (s + sz);
115 *ip = (
int)strtol (s, &endp, 10);
122 static char *parseUInt(
char *s,
unsigned int *ip)
126 *ip = (
unsigned int)strtoul (s, &endp, 10);
134 static char *parsePoint(
char *s,
xdot_point * pp)
137 r = sscanf(s,
"%lf %lf%n", &(pp->
x), &(pp->
y), &sz);
138 if (r != 2)
return 0;
144 static char *parseRect(
char *s,
xdot_rect * rp)
149 r = sscanf(s,
"%lf %lf %lf %lf%n", &(rp->
x), &(rp->
y), &(rp->
w),
151 if (r != 4)
return 0;
152 else return (s + sz);
155 rp->
x = strtod (s, &endp);
161 rp->
y = strtod (s, &endp);
167 rp->
w = strtod (s, &endp);
173 rp->
h = strtod (s, &endp);
193 for (i = 0; i < pp->
cnt; i++) {
194 ps->
x = strtod (s, &endp);
201 ps->
y = strtod (s, &endp);
215 static char *parseString(
char *s,
char **sp)
221 if (!s || (i <= 0))
return 0;
222 while (*s && (*s !=
'-')) s++;
227 c =
N_NEW(i + 1,
char);
229 while ((i > 0) && *s) {
243 static char *parseAlign(
char *s,
xdot_align * ap)
257 #define CHK(s) if(!s){*error=1;return 0;}
286 s = parsePolyline(s, &op->
u.
polygon);
294 s = parsePolyline(s, &op->
u.
polygon);
302 s = parsePolyline(s, &op->
u.
bezier);
310 s = parsePolyline(s, &op->
u.
bezier);
317 s = parseString(s, &cs);
336 s = parseString(s, &cs);
364 s = parseReal(s, &op->
u.
text.
x);
366 s = parseReal(s, &op->
u.
text.
y);
390 s = parseString(s, &op->
u.
style);
457 ops = (
char *) calloc(
XDBSIZE, sz);
460 ops = (
char*)(x->
ops);
462 ops = (
char *) realloc(ops, bufsz * sz);
463 memset(ops + (initcnt*sz),
'\0', (bufsz - initcnt)*sz);
466 while ((s = parseOp(&op, s, fns, &error))) {
467 if (x->
cnt == bufsz) {
470 ops = (
char *) realloc(ops, bufsz * sz);
471 memset(ops + (oldsz*sz),
'\0', (bufsz - oldsz)*sz);
501 typedef void (*
pf) (
char *,
void *);
506 static void trim (
char* buf)
511 if ((dotp = strchr (buf,
'.'))) {
515 while (*p ==
'0') *p-- =
'\0';
527 sprintf(buf,
" %.02f", r->
x);
530 sprintf(buf,
" %.02f", r->
y);
533 sprintf(buf,
" %.02f", r->
w);
536 sprintf(buf,
" %.02f", r->
h);
546 sprintf(buf,
" %d", p->
cnt);
548 for (i = 0; i < p->
cnt; i++) {
549 sprintf(buf,
" %.02f", p->
pts[i].
x);
552 sprintf(buf,
" %.02f", p->
pts[i].
y);
558 static void printString(
char *p,
pf print,
void *info)
562 sprintf(buf,
" %d -", (
int) strlen(p));
567 static void printInt(
int i,
pf print,
void *info)
571 sprintf(buf,
" %d", i);
575 static void printFloat(
float f,
pf print,
void *info,
int space)
580 sprintf(buf,
" %.02f", f);
582 sprintf(buf,
"%.02f", f);
587 static void printAlign(
xdot_align a,
pf print,
void *info)
603 gradprint (
char* s,
void* v)
616 printFloat (cp->
u.
ling.
x0, gradprint, xb, 0);
617 printFloat (cp->
u.
ling.
y0, gradprint, xb, 1);
618 printFloat (cp->
u.
ling.
x1, gradprint, xb, 1);
619 printFloat (cp->
u.
ling.
y1, gradprint, xb, 1);
625 printFloat (cp->
u.
ring.
x0, gradprint, xb, 0);
626 printFloat (cp->
u.
ring.
y0, gradprint, xb, 1);
627 printFloat (cp->
u.
ring.
r0, gradprint, xb, 1);
628 printFloat (cp->
u.
ring.
x1, gradprint, xb, 1);
629 printFloat (cp->
u.
ring.
y1, gradprint, xb, 1);
630 printFloat (cp->
u.
ring.
r1, gradprint, xb, 1);
634 printInt (n_stops, gradprint, xb);
635 for (i = 0; i < n_stops; i++) {
636 printFloat (stops[i].frac, gradprint, xb, 1);
637 printString (stops[i].color, gradprint, xb);
648 static void printXDot_Op(
xdot_op * op,
pf print,
void *info,
int more)
651 unsigned char buf[BUFSIZ];
657 printRect(&op->
u.
ellipse, print, info);
661 printRect(&op->
u.
ellipse, print, info);
665 printPolyline(&op->
u.
polygon, print, info);
669 printPolyline(&op->
u.
polygon, print, info);
673 printPolyline(&op->
u.
bezier, print, info);
677 printPolyline(&op->
u.
bezier, print, info);
681 printString(op->
u.
color, print, info);
686 printString(
agxbuse(&xb), print, info);
690 printString(op->
u.
color, print, info);
695 printString(
agxbuse(&xb), print, info);
699 printPolyline(&op->
u.
polyline, print, info);
703 printInt(op->
u.
text.
x, print, info);
704 printInt(op->
u.
text.
y, print, info);
707 printString(op->
u.
text.
text, print, info);
711 printFloat(op->
u.
font.
size, print, info, 1);
712 printString(op->
u.
font.
name, print, info);
720 printString(op->
u.
style, print, info);
724 printRect(&op->
u.
image.
pos, print, info);
733 static void jsonRect(
xdot_rect * r,
pf print,
void *info)
737 sprintf(buf,
"[%.06f,%.06f,%.06f,%.06f]", r->
x, r->
y, r->
w, r->
h);
747 for (i = 0; i < p->
cnt; i++) {
748 sprintf(buf,
"%.06f,%.06f", p->
pts[i].
x, p->
pts[i].
y);
750 if (i < p->cnt-1) print(
",", info);
755 static void jsonString(
char *p,
pf print,
void *info)
757 unsigned char c, buf[BUFSIZ];
763 if (c ==
'"')
agxbput(
"\\\"", &xb);
764 else if (c ==
'\\')
agxbput(
"\\\\", &xb);
773 static void jsonXDot_Op(
xdot_op * op,
pf print,
void *info,
int more)
776 unsigned char buf[BUFSIZ];
781 print(
"{E : ", info);
782 jsonRect(&op->
u.
ellipse, print, info);
785 print(
"{e : ", info);
786 jsonRect(&op->
u.
ellipse, print, info);
789 print(
"{P : ", info);
790 jsonPolyline(&op->
u.
polygon, print, info);
793 print(
"{p : ", info);
794 jsonPolyline(&op->
u.
polygon, print, info);
797 print(
"{b : ", info);
798 jsonPolyline(&op->
u.
bezier, print, info);
801 print(
"{B : ", info);
802 jsonPolyline(&op->
u.
bezier, print, info);
805 print(
"{c : ", info);
806 jsonString(op->
u.
color, print, info);
809 print(
"{c : ", info);
811 jsonString(
agxbuse(&xb), print, info);
814 print(
"{C : ", info);
815 jsonString(op->
u.
color, print, info);
818 print(
"{C : ", info);
820 jsonString(
agxbuse(&xb), print, info);
824 jsonPolyline(&op->
u.
polyline, print, info);
827 print(
"{T : [", info);
828 printInt(op->
u.
text.
x, print, info);
830 printInt(op->
u.
text.
y, print, info);
836 jsonString(op->
u.
text.
text, print, info);
840 print(
"{F : [", info);
842 printFloat(op->
u.
font.
size, print, info, 1);
844 jsonString(op->
u.
font.
name, print, info);
848 print(
"{t : ", info);
852 print(
"{S : ", info);
853 jsonString(op->
u.
style, print, info);
856 print(
"{I : [", info);
870 static void _printXDot(
xdot * x,
pf print,
void *info,
print_op ofn)
874 char *base = (
char *) (x->
ops);
875 for (i = 0; i < x->
cnt; i++) {
877 ofn(op, print, info, (i < x->cnt - 1));
884 unsigned char buf[BUFSIZ];
887 _printXDot(x, (
pf)
agxbput, &xb, printXDot_Op);
896 _printXDot(x, (
pf) fputs, fp, printXDot_Op);
902 _printXDot(x, (
pf) fputs, fp, jsonXDot_Op);
906 static void freeXOpData(
xdot_op * x)
953 base = (
char *) (x->
ops);
954 for (i = 0; i < x->
cnt; i++) {
969 if (!x || !sp)
return 1;
972 base = (
char *) (x->
ops);
973 for (i = 0; i < x->
cnt; i++) {
1044 #define CHK1(s) if(!s){free(stops);return NULL;}
1059 s = parseReal(s, &clr->
u.
ring.
x0);
1061 s = parseReal(s, &clr->
u.
ring.
y0);
1063 s = parseReal(s, &clr->
u.
ring.
r0);
1065 s = parseReal(s, &clr->
u.
ring.
x1);
1067 s = parseReal(s, &clr->
u.
ring.
y1);
1069 s = parseReal(s, &clr->
u.
ring.
r1);
1076 s = parseReal(s, &d);
1079 s = parseString(s, &stops[i].color);
1100 s = parseReal(s, &clr->
u.
ling.
x0);
1102 s = parseReal(s, &clr->
u.
ling.
y0);
1104 s = parseReal(s, &clr->
u.
ling.
x1);
1106 s = parseReal(s, &clr->
u.
ling.
y1);
1113 s = parseReal(s, &d);
1116 s = parseString(s, &stops[i].color);
1136 return linGradient (cp+1, clr);
1139 return radGradient (cp+1, clr);
1177 static void execOp(
xdot_op * op,
int param)
int statXDot(xdot *x, xdot_stats *sp)
size_t agxbput(agxbuf *xb, const char *s)
void(* freefunc_t)(xdot_op *)
char * sprintXDot(xdot *x)
void(* print_op)(xdot_op *op, pf print, void *info, int more)
xdot * parseXDot(char *s)
char * parseXDotColor(char *cp, xdot_color *clr)
void fprintXDot(FILE *fp, xdot *x)
void agxbinit(agxbuf *xb, unsigned int hint, unsigned char *init)
xdot_grad_type colorType(char *cp)
xdot * parseXDotF(char *s, drawfunc_t fns[], int sz)
void jsonXDot(FILE *fp, xdot *x)
void(* drawfunc_t)(xdot_op *, int)
void(* pf)(char *, void *)
void agxbfree(agxbuf *xb)
void freeXDotColor(xdot_color *cp)
xdot * parseXDotFOn(char *s, drawfunc_t fns[], int sz, xdot *x)
int agxbmore(agxbuf *xb, size_t ssz)