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