1 #define SCOORD_MAX 0x7fffffff
2 #define SCOORD_MIN -0x80000000
5 void swf_DrawerInit(SWFSHAPEDRAWER*draw, TAG*tag)
7 memset(draw, 0, sizeof(SWFSHAPEDRAWER));
10 tag = swf_InsertTag(0, ST_DEFINESHAPE);
14 swf_ShapeNew(&draw->shape);
18 swf_SetU8(draw->tag,0);
19 draw->shape->bits.fill = 1;
20 draw->shape->bits.line = 1;
22 draw->bbox.xmin = draw->bbox.ymin = SCOORD_MAX;
23 draw->bbox.xmax = draw->bbox.ymax = SCOORD_MIN;
27 swf_ShapeSetStyle(draw->tag,draw->shape,1,1,0);
29 void swf_DrawerMoveTo(SWFSHAPEDRAWER*draw, FPOINT * to)
33 swf_ShapeSetMove(draw->tag,draw->shape,x,y);
38 void swf_DrawerLineTo(SWFSHAPEDRAWER*draw, FPOINT * to)
42 if(draw->lastx < draw->bbox.xmin) draw->bbox.xmin = draw->lastx;
43 if(draw->lasty < draw->bbox.ymin) draw->bbox.ymin = draw->lasty;
44 if(draw->lastx > draw->bbox.xmax) draw->bbox.xmax = draw->lastx;
45 if(draw->lasty > draw->bbox.ymax) draw->bbox.ymax = draw->lasty;
46 if(x < draw->bbox.xmin) draw->bbox.xmin = x;
47 if(y < draw->bbox.ymin) draw->bbox.ymin = y;
48 if(x > draw->bbox.xmax) draw->bbox.xmax = x;
49 if(y > draw->bbox.ymax) draw->bbox.ymax = y;
50 swf_ShapeSetLine(draw->tag,draw->shape,x-draw->lastx,y-draw->lasty);
55 void swf_DrawerSplineTo(SWFSHAPEDRAWER*draw, FPOINT * c1, FPOINT* to)
61 if(draw->lastx < draw->bbox.xmin) draw->bbox.xmin = draw->lastx;
62 if(draw->lasty < draw->bbox.ymin) draw->bbox.ymin = draw->lasty;
63 if(draw->lastx > draw->bbox.xmax) draw->bbox.xmax = draw->lastx;
64 if(draw->lasty > draw->bbox.ymax) draw->bbox.ymax = draw->lasty;
65 if(x < draw->bbox.xmin) draw->bbox.xmin = x;
66 if(y < draw->bbox.ymin) draw->bbox.ymin = y;
67 if(x > draw->bbox.xmax) draw->bbox.xmax = x;
68 if(y > draw->bbox.ymax) draw->bbox.ymax = y;
69 if(tx < draw->bbox.xmin) draw->bbox.xmin = tx;
70 if(ty < draw->bbox.ymin) draw->bbox.ymin = ty;
71 if(tx > draw->bbox.xmax) draw->bbox.xmax = tx;
72 if(ty > draw->bbox.ymax) draw->bbox.ymax = ty;
73 swf_ShapeSetCurve(draw->tag,draw->shape, tx-draw->lastx,ty-draw->lasty, x-tx,y-ty);
78 void swf_DrawerCubicTo(SWFSHAPEDRAWER*draw, FPOINT* c1, FPOINT* c2, FPOINT* to)
83 void swf_DrawerConicTo(SWFSHAPEDRAWER*draw, FPOINT* c, FPOINT* to)
85 FPOINT* pos = &draw->pos;
87 c1.x = (pos->x + 2 * c->x) / 3;
88 c1.y = (pos->y + 2 * c->y) / 3;
89 c2.x = (2 * c->x + to->x) / 3;
90 c2.y = (2 * c->y + to->y) / 3;
91 swf_DrawerCubicTo(draw, &c1,&c2,to);
94 void swf_DrawerFinish(SWFSHAPEDRAWER*draw)
96 if(draw->bbox.xmin == SCOORD_MAX) {
97 /* no points at all -> empty bounding box */
98 draw->bbox.xmin = draw->bbox.ymin =
99 draw->bbox.xmax = draw->bbox.ymax = 0;
101 draw->isfinished = 1;
102 swf_ShapeSetEnd(draw->tag);
104 SHAPE* swf_DrawerToShape(SWFSHAPEDRAWER*draw)
106 SHAPE* shape = malloc(sizeof(SHAPE));
107 if(!draw->isfinished) {
108 fprintf(stderr, "Warning: you should Finish() your drawer before calling DrawerToShape");
109 swf_DrawerFinish(draw);
111 memcpy(shape, draw->shape, sizeof(SHAPE));
112 shape->bitlen = (draw->tag->len-1)*8;
113 shape->data = (U8*)malloc(draw->tag->len-1);
114 memcpy(shape->data, &draw->tag->data[1], draw->tag->len-1);
117 void swf_DrawerFree(SWFSHAPEDRAWER*draw)
120 swf_DeleteTag(draw->tag);
123 // swf_ShapeFree(draw->s);
127 static char* getToken(const char**p)
131 while(**p && strchr(" ,\t\n\r", **p)) {
135 while(**p && !strchr(" ,\t\n\r", **p)) {
138 result = malloc((*p)-start+1);
139 memcpy(result,start,(*p)-start+1);
140 result[(*p)-start] = 0;
144 void swf_DrawString(SWFSHAPEDRAWER*draw, const char*string)
146 const char*p = string;
148 char*token = getToken(&p);
149 if(!strncmp(token, "moveTo", 6)) {
151 to.x = atoi(getToken(&p));
152 to.y = atoi(getToken(&p));
153 swf_DrawerMoveTo(draw, &to);
155 else if(!strncmp(token, "lineTo", 6)) {
157 to.x = atoi(getToken(&p));
158 to.y = atoi(getToken(&p));
159 swf_DrawerLineTo(draw, &to);
161 else if(!strncmp(token, "curveTo", 7) || !strncmp(token, "splineTo", 8)) {
163 mid.x = atoi(getToken(&p));
164 mid.y = atoi(getToken(&p));
165 to.x = atoi(getToken(&p));
166 to.y = atoi(getToken(&p));
167 swf_DrawerSplineTo(draw, &mid, &to);
171 //swf_DrawerFinish(draw); // must be done by application.