added argument to initUsage.
[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     if(sdraw->lastx != x || sdraw->lasty != y) {
102         fixEndPoint(draw);
103         swf_ShapeSetMove(sdraw->tag,sdraw->shape,x,y);
104         sdraw->firstx = sdraw->lastx = x;
105         sdraw->firsty = sdraw->lasty = y;
106         draw->pos = *to;
107     }
108 }
109 static void swf_ShapeDrawerLineTo(drawer_t*draw, FPOINT * to)
110 {
111     SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
112     int x = to->x*20;
113     int y = to->y*20;
114     if(sdraw->lastx < sdraw->bbox.xmin) sdraw->bbox.xmin = sdraw->lastx;
115     if(sdraw->lasty < sdraw->bbox.ymin) sdraw->bbox.ymin = sdraw->lasty;
116     if(sdraw->lastx > sdraw->bbox.xmax) sdraw->bbox.xmax = sdraw->lastx;
117     if(sdraw->lasty > sdraw->bbox.ymax) sdraw->bbox.ymax = sdraw->lasty;
118     if(x < sdraw->bbox.xmin) sdraw->bbox.xmin = x;
119     if(y < sdraw->bbox.ymin) sdraw->bbox.ymin = y;
120     if(x > sdraw->bbox.xmax) sdraw->bbox.xmax = x;
121     if(y > sdraw->bbox.ymax) sdraw->bbox.ymax = y;
122     swf_ShapeSetLine(sdraw->tag,sdraw->shape,x-sdraw->lastx,y-sdraw->lasty);
123     sdraw->lastx = x;
124     sdraw->lasty = y;
125     draw->pos = *to;
126 }
127 static void swf_ShapeDrawerSplineTo(drawer_t*draw, FPOINT * c1, FPOINT*  to)
128 {
129     SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
130     int tx = c1->x*20;
131     int ty = c1->y*20;
132     int x = to->x*20;
133     int y = to->y*20;
134     if(sdraw->lastx < sdraw->bbox.xmin) sdraw->bbox.xmin = sdraw->lastx;
135     if(sdraw->lasty < sdraw->bbox.ymin) sdraw->bbox.ymin = sdraw->lasty;
136     if(sdraw->lastx > sdraw->bbox.xmax) sdraw->bbox.xmax = sdraw->lastx;
137     if(sdraw->lasty > sdraw->bbox.ymax) sdraw->bbox.ymax = sdraw->lasty;
138     if(x < sdraw->bbox.xmin) sdraw->bbox.xmin = x;
139     if(y < sdraw->bbox.ymin) sdraw->bbox.ymin = y;
140     if(x > sdraw->bbox.xmax) sdraw->bbox.xmax = x;
141     if(y > sdraw->bbox.ymax) sdraw->bbox.ymax = y;
142     if(tx < sdraw->bbox.xmin) sdraw->bbox.xmin = tx;
143     if(ty < sdraw->bbox.ymin) sdraw->bbox.ymin = ty;
144     if(tx > sdraw->bbox.xmax) sdraw->bbox.xmax = tx;
145     if(ty > sdraw->bbox.ymax) sdraw->bbox.ymax = ty;
146     swf_ShapeSetCurve(sdraw->tag,sdraw->shape, tx-sdraw->lastx,ty-sdraw->lasty, x-tx,y-ty);
147     sdraw->lastx = x;
148     sdraw->lasty = y;
149     draw->pos = *to;
150 }
151 static void swf_ShapeDrawerFinish(drawer_t*draw)
152 {
153     SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
154         
155     fixEndPoint(draw);
156
157     if(sdraw->bbox.xmin == SCOORD_MAX) {
158         /* no points at all -> empty bounding box */
159         sdraw->bbox.xmin = sdraw->bbox.ymin = 
160         sdraw->bbox.xmax = sdraw->bbox.ymax = 0;
161     }
162     sdraw->isfinished = 1;
163     swf_ShapeSetEnd(sdraw->tag);
164 }
165
166 static void swf_ShapeDrawerClear(drawer_t*draw)
167 {
168     SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
169     if(sdraw->tagfree) {
170         swf_DeleteTag(sdraw->tag);
171         sdraw->tag = 0;
172     }
173     swf_ShapeFree(sdraw->shape);
174     sdraw->shape = 0;
175
176     free(draw->internal);
177     draw->internal = 0;
178 }
179
180 SRECT swf_ShapeDrawerGetBBox(drawer_t*draw)
181 {
182     SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
183     return sdraw->bbox;
184 }
185
186 SHAPE* swf_ShapeDrawerToShape(drawer_t*draw)
187 {
188     SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
189     SHAPE* shape = malloc(sizeof(SHAPE));
190     if(!sdraw->isfinished) {
191         fprintf(stderr, "Warning: you should Finish() your drawer before calling DrawerToShape");
192         swf_ShapeDrawerFinish(draw);
193     }
194     memcpy(shape, sdraw->shape, sizeof(SHAPE));
195     shape->bitlen = (sdraw->tag->len-1)*8;
196     shape->data = (U8*)malloc(sdraw->tag->len-1);
197     memcpy(shape->data, &sdraw->tag->data[1], sdraw->tag->len-1);
198     return shape;
199 }