new function list_deep_free
[swftools.git] / lib / modules / swfdraw.c
1 // swfdraw.c
2
3 #include "../rfxswf.h"
4
5 typedef struct _SWFSHAPEDRAWER
6 {
7     SHAPE*shape;
8     TAG*tag;
9     int tagfree;
10     SCOORD firstx;
11     SCOORD firsty;
12     SCOORD lastx;
13     SCOORD lasty;
14     SRECT bbox;
15     char isfinished;
16 } SWFSHAPEDRAWER;
17
18 static void swf_ShapeDrawerSetLineStyle(drawer_t*draw, void*style);
19 static void swf_ShapeDrawerSetFillStyle(drawer_t*draw, void*style);
20 static void swf_ShapeDrawerMoveTo(drawer_t*draw, FPOINT * to);
21 static void swf_ShapeDrawerLineTo(drawer_t*draw, FPOINT * to);
22 static void swf_ShapeDrawerSplineTo(drawer_t*draw, FPOINT * c1, FPOINT*  to);
23 static void swf_ShapeDrawerFinish(drawer_t*draw);
24 static void swf_ShapeDrawerClear(drawer_t*draw);
25
26 static void swf_ShapeDrawerInit(drawer_t*draw, TAG*tag, int fillstylebits, int linestylebits)
27 {
28     SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)rfx_calloc(sizeof(SWFSHAPEDRAWER));
29     draw->internal = sdraw;
30
31     draw->setLineStyle = swf_ShapeDrawerSetLineStyle;
32     draw->setFillStyle = swf_ShapeDrawerSetFillStyle;
33     draw->moveTo = swf_ShapeDrawerMoveTo;
34     draw->lineTo = swf_ShapeDrawerLineTo;
35     draw->splineTo = swf_ShapeDrawerSplineTo;
36     draw->finish = swf_ShapeDrawerFinish;
37     draw->dealloc = swf_ShapeDrawerClear;
38     
39     sdraw->tagfree = 0;
40     if(tag == 0) {
41         tag = swf_InsertTag(0, ST_DEFINESHAPE);
42         sdraw->tagfree = 1;
43     }
44     sdraw->tag = tag;
45     swf_ShapeNew(&sdraw->shape);
46     draw->pos.x = 0;
47     draw->pos.y = 0;
48
49     swf_SetU8(sdraw->tag,0);
50     sdraw->shape->bits.fill = fillstylebits;
51     sdraw->shape->bits.line = linestylebits;
52     
53     sdraw->bbox.xmin = sdraw->bbox.ymin = SCOORD_MAX;
54     sdraw->bbox.xmax = sdraw->bbox.ymax = SCOORD_MIN;
55
56     sdraw->isfinished = 0;
57
58     swf_ShapeSetStyle(sdraw->tag,sdraw->shape,linestylebits?1:0,fillstylebits?1:0,0/*?*/);
59 }
60
61 void swf_Shape10DrawerInit(drawer_t*draw, TAG*tag)
62 {
63     swf_ShapeDrawerInit(draw, tag, 0, 1);
64 }
65
66 void swf_Shape01DrawerInit(drawer_t*draw, TAG*tag)
67 {
68     swf_ShapeDrawerInit(draw, tag, 1, 0);
69 }
70
71 void swf_Shape11DrawerInit(drawer_t*draw, TAG*tag)
72 {
73     swf_ShapeDrawerInit(draw, tag, 1, 1);
74 }
75
76 static void swf_ShapeDrawerSetLineStyle(drawer_t*draw, void*style)
77 {
78     SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
79 }
80 static void swf_ShapeDrawerSetFillStyle(drawer_t*draw, void*style)
81 {
82     SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
83 }
84 static void fixEndPoint(drawer_t*draw)
85 {
86     SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
87     if(   sdraw->firstx != sdraw->lastx 
88        || sdraw->firsty != sdraw->lasty) {
89         /* fix non-closing shapes */
90         FPOINT to;
91         to.x = sdraw->firstx/20.0;
92         to.y = sdraw->firsty/20.0;
93         if(sdraw->shape->bits.fill) // do this only if the shape is filled
94             draw->lineTo(draw, &to);
95     }
96 }
97 static void swf_ShapeDrawerMoveTo(drawer_t*draw, FPOINT * to)
98 {
99     SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
100     int x = to->x*20+0.001;
101     int y = to->y*20+0.001;
102
103     /* we need to write moveto always- it
104        might be that it signals the end of a polygon, otherwise
105        we would end up connecting two polygons which should
106        be seperate 
107         TODO: check if the last operation was a moveTo- if
108               yes we *can* skip it.
109      */
110
111     //if(sdraw->lastx != x || sdraw->lasty != y) {
112         fixEndPoint(draw);
113         swf_ShapeSetMove(sdraw->tag,sdraw->shape,x,y);
114         sdraw->firstx = sdraw->lastx = x;
115         sdraw->firsty = sdraw->lasty = y;
116         draw->pos = *to;
117     //}
118 }
119 static void swf_ShapeDrawerLineTo(drawer_t*draw, FPOINT * to)
120 {
121     SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
122     int x = to->x*20+0.001;
123     int y = to->y*20+0.001;
124     if(sdraw->lastx < sdraw->bbox.xmin) sdraw->bbox.xmin = sdraw->lastx;
125     if(sdraw->lasty < sdraw->bbox.ymin) sdraw->bbox.ymin = sdraw->lasty;
126     if(sdraw->lastx > sdraw->bbox.xmax) sdraw->bbox.xmax = sdraw->lastx;
127     if(sdraw->lasty > sdraw->bbox.ymax) sdraw->bbox.ymax = sdraw->lasty;
128     if(x < sdraw->bbox.xmin) sdraw->bbox.xmin = x;
129     if(y < sdraw->bbox.ymin) sdraw->bbox.ymin = y;
130     if(x > sdraw->bbox.xmax) sdraw->bbox.xmax = x;
131     if(y > sdraw->bbox.ymax) sdraw->bbox.ymax = y;
132     swf_ShapeSetLine(sdraw->tag,sdraw->shape,x-sdraw->lastx,y-sdraw->lasty);
133     sdraw->lastx = x;
134     sdraw->lasty = y;
135     draw->pos = *to;
136 }
137 static void swf_ShapeDrawerSplineTo(drawer_t*draw, FPOINT * c1, FPOINT*  to)
138 {
139     SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
140     int tx = c1->x*20+0.001;
141     int ty = c1->y*20+0.001;
142     int x = to->x*20+0.001;
143     int y = to->y*20+0.001;
144     if(sdraw->lastx < sdraw->bbox.xmin) sdraw->bbox.xmin = sdraw->lastx;
145     if(sdraw->lasty < sdraw->bbox.ymin) sdraw->bbox.ymin = sdraw->lasty;
146     if(sdraw->lastx > sdraw->bbox.xmax) sdraw->bbox.xmax = sdraw->lastx;
147     if(sdraw->lasty > sdraw->bbox.ymax) sdraw->bbox.ymax = sdraw->lasty;
148     if(x < sdraw->bbox.xmin) sdraw->bbox.xmin = x;
149     if(y < sdraw->bbox.ymin) sdraw->bbox.ymin = y;
150     if(x > sdraw->bbox.xmax) sdraw->bbox.xmax = x;
151     if(y > sdraw->bbox.ymax) sdraw->bbox.ymax = y;
152     if(tx < sdraw->bbox.xmin) sdraw->bbox.xmin = tx;
153     if(ty < sdraw->bbox.ymin) sdraw->bbox.ymin = ty;
154     if(tx > sdraw->bbox.xmax) sdraw->bbox.xmax = tx;
155     if(ty > sdraw->bbox.ymax) sdraw->bbox.ymax = ty;
156     swf_ShapeSetCurve(sdraw->tag,sdraw->shape, tx-sdraw->lastx,ty-sdraw->lasty, x-tx,y-ty);
157     sdraw->lastx = x;
158     sdraw->lasty = y;
159     draw->pos = *to;
160 }
161 static void swf_ShapeDrawerFinish(drawer_t*draw)
162 {
163     SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
164     if(sdraw->isfinished)
165         return;
166         
167     fixEndPoint(draw);
168
169     if(sdraw->bbox.xmin == SCOORD_MAX) {
170         /* no points at all -> empty bounding box */
171         sdraw->bbox.xmin = sdraw->bbox.ymin = 
172         sdraw->bbox.xmax = sdraw->bbox.ymax = 0;
173     }
174     sdraw->isfinished = 1;
175     swf_ShapeSetEnd(sdraw->tag);
176 }
177
178 static void swf_ShapeDrawerClear(drawer_t*draw)
179 {
180     SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
181     if(sdraw->tagfree) {
182         swf_DeleteTag(0, sdraw->tag);
183         sdraw->tag = 0;
184     }
185     swf_ShapeFree(sdraw->shape);
186     sdraw->shape = 0;
187
188     rfx_free(draw->internal);
189     draw->internal = 0;
190 }
191
192 SRECT swf_ShapeDrawerGetBBox(drawer_t*draw)
193 {
194     SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
195     return sdraw->bbox;
196 }
197
198 SHAPE* swf_ShapeDrawerToShape(drawer_t*draw)
199 {
200     SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
201     SHAPE* shape = (SHAPE*)rfx_alloc(sizeof(SHAPE));
202     if(!sdraw->isfinished) {
203         fprintf(stderr, "Warning: you should Finish() your drawer before calling DrawerToShape");
204         swf_ShapeDrawerFinish(draw);
205     }
206     memcpy(shape, sdraw->shape, sizeof(SHAPE));
207     shape->bitlen = (sdraw->tag->len-1)*8;
208     shape->data = (U8*)rfx_alloc(sdraw->tag->len-1);
209     memcpy(shape->data, &sdraw->tag->data[1], sdraw->tag->len-1);
210     return shape;
211 }