3 typedef struct _SWFSHAPEDRAWER
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);
24 static void swf_ShapeDrawerInit(drawer_t*draw, TAG*tag, int fillstylebits, int linestylebits)
26 SWFSHAPEDRAWER*sdraw = malloc(sizeof(SWFSHAPEDRAWER));
27 memset(sdraw, 0, sizeof(SWFSHAPEDRAWER));
28 draw->internal = sdraw;
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;
40 tag = swf_InsertTag(0, ST_DEFINESHAPE);
44 swf_ShapeNew(&sdraw->shape);
48 swf_SetU8(sdraw->tag,0);
49 sdraw->shape->bits.fill = fillstylebits;
50 sdraw->shape->bits.line = linestylebits;
52 sdraw->bbox.xmin = sdraw->bbox.ymin = SCOORD_MAX;
53 sdraw->bbox.xmax = sdraw->bbox.ymax = SCOORD_MIN;
55 sdraw->isfinished = 0;
57 swf_ShapeSetStyle(sdraw->tag,sdraw->shape,linestylebits?1:0,fillstylebits?1:0,0/*?*/);
60 void swf_Shape10DrawerInit(drawer_t*draw, TAG*tag)
62 swf_ShapeDrawerInit(draw, tag, 0, 1);
65 void swf_Shape01DrawerInit(drawer_t*draw, TAG*tag)
67 swf_ShapeDrawerInit(draw, tag, 1, 0);
70 void swf_Shape11DrawerInit(drawer_t*draw, TAG*tag)
72 swf_ShapeDrawerInit(draw, tag, 1, 1);
75 static void swf_ShapeDrawerSetLineStyle(drawer_t*draw, void*style)
77 SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
79 static void swf_ShapeDrawerSetFillStyle(drawer_t*draw, void*style)
81 SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
83 static void fixEndPoint(drawer_t*draw)
85 SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
86 if( sdraw->firstx != sdraw->lastx
87 || sdraw->firsty != sdraw->lasty) {
88 /* fix non-closing shapes */
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);
96 static void swf_ShapeDrawerMoveTo(drawer_t*draw, FPOINT * to)
98 SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
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
106 TODO: check if the last operation was a moveTo- if
107 yes we *can* skip it.
110 //if(sdraw->lastx != x || sdraw->lasty != y) {
112 swf_ShapeSetMove(sdraw->tag,sdraw->shape,x,y);
113 sdraw->firstx = sdraw->lastx = x;
114 sdraw->firsty = sdraw->lasty = y;
118 static void swf_ShapeDrawerLineTo(drawer_t*draw, FPOINT * to)
120 SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
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);
136 static void swf_ShapeDrawerSplineTo(drawer_t*draw, FPOINT * c1, FPOINT* to)
138 SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
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);
160 static void swf_ShapeDrawerFinish(drawer_t*draw)
162 SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
166 if(sdraw->bbox.xmin == SCOORD_MAX) {
167 /* no points at all -> empty bounding box */
168 sdraw->bbox.xmin = sdraw->bbox.ymin =
169 sdraw->bbox.xmax = sdraw->bbox.ymax = 0;
171 sdraw->isfinished = 1;
172 swf_ShapeSetEnd(sdraw->tag);
175 static void swf_ShapeDrawerClear(drawer_t*draw)
177 SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
179 swf_DeleteTag(sdraw->tag);
182 swf_ShapeFree(sdraw->shape);
185 free(draw->internal);
189 SRECT swf_ShapeDrawerGetBBox(drawer_t*draw)
191 SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
195 SHAPE* swf_ShapeDrawerToShape(drawer_t*draw)
197 SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
198 SHAPE* shape = malloc(sizeof(SHAPE));
199 if(!sdraw->isfinished) {
200 fprintf(stderr, "Warning: you should Finish() your drawer before calling DrawerToShape");
201 swf_ShapeDrawerFinish(draw);
203 memcpy(shape, sdraw->shape, sizeof(SHAPE));
204 shape->bitlen = (sdraw->tag->len-1)*8;
205 shape->data = (U8*)malloc(sdraw->tag->len-1);
206 memcpy(shape->data, &sdraw->tag->data[1], sdraw->tag->len-1);