moved drawer stuff to ../drawer.c.
authorkramm <kramm>
Sat, 31 Jan 2004 20:03:53 +0000 (20:03 +0000)
committerkramm <kramm>
Sat, 31 Jan 2004 20:03:53 +0000 (20:03 +0000)
lib/modules/swfdraw.c

index 29a8b1f..b54a0e4 100644 (file)
-#define SCOORD_MAX 0x7fffffff
-#define SCOORD_MIN -0x80000000
-
 // swfdraw.c
-void swf_DrawerInit(SWFSHAPEDRAWER*draw, TAG*tag)
+
+typedef struct _SWFSHAPEDRAWER
+{
+    SHAPE*shape;
+    TAG*tag;
+    int tagfree;
+    SCOORD lastx;
+    SCOORD lasty;
+    SRECT bbox;
+    char isfinished;
+} SWFSHAPEDRAWER;
+
+static void swf_ShapeDrawerSetLineStyle(drawer_t*draw, void*style);
+static void swf_ShapeDrawerSetFillStyle(drawer_t*draw, void*style);
+static void swf_ShapeDrawerMoveTo(drawer_t*draw, FPOINT * to);
+static void swf_ShapeDrawerLineTo(drawer_t*draw, FPOINT * to);
+static void swf_ShapeDrawerSplineTo(drawer_t*draw, FPOINT * c1, FPOINT*  to);
+static void swf_ShapeDrawerFinish(drawer_t*draw);
+static void swf_ShapeDrawerClear(drawer_t*draw);
+
+static void swf_ShapeDrawerInit(drawer_t*draw, TAG*tag, int fillstylebits, int linestylebits)
 {
-    memset(draw, 0, sizeof(SWFSHAPEDRAWER));
-    draw->tagfree = 0;
+    SWFSHAPEDRAWER*sdraw = malloc(sizeof(SWFSHAPEDRAWER));
+    memset(sdraw, 0, sizeof(SWFSHAPEDRAWER));
+    draw->internal = sdraw;
+
+    draw->setLineStyle = swf_ShapeDrawerSetLineStyle;
+    draw->setFillStyle = swf_ShapeDrawerSetFillStyle;
+    draw->moveTo = swf_ShapeDrawerMoveTo;
+    draw->lineTo = swf_ShapeDrawerLineTo;
+    draw->splineTo = swf_ShapeDrawerSplineTo;
+    draw->finish = swf_ShapeDrawerFinish;
+    draw->dealloc = swf_ShapeDrawerClear;
+    
+    sdraw->tagfree = 0;
     if(tag == 0) {
        tag = swf_InsertTag(0, ST_DEFINESHAPE);
-       draw->tagfree = 1;
+       sdraw->tagfree = 1;
     }
-    draw->tag = tag;
-    swf_ShapeNew(&draw->shape);
+    sdraw->tag = tag;
+    swf_ShapeNew(&sdraw->shape);
     draw->pos.x = 0;
     draw->pos.y = 0;
 
-    swf_SetU8(draw->tag,0);
-    draw->shape->bits.fill = 1;
-    draw->shape->bits.line = 1;
+    swf_SetU8(sdraw->tag,0);
+    sdraw->shape->bits.fill = fillstylebits;
+    sdraw->shape->bits.line = linestylebits;
     
-    draw->bbox.xmin = draw->bbox.ymin = SCOORD_MAX;
-    draw->bbox.xmax = draw->bbox.ymax = SCOORD_MIN;
+    sdraw->bbox.xmin = sdraw->bbox.ymin = SCOORD_MAX;
+    sdraw->bbox.xmax = sdraw->bbox.ymax = SCOORD_MIN;
+
+    sdraw->isfinished = 0;
+
+    swf_ShapeSetStyle(sdraw->tag,sdraw->shape,linestylebits?1:0,fillstylebits?1:0,0/*?*/);
+}
 
-    draw->isfinished = 0;
+void swf_Shape01DrawerInit(drawer_t*draw, TAG*tag)
+{
+    swf_ShapeDrawerInit(draw, tag, 1, 0);
+}
 
-    swf_ShapeSetStyle(draw->tag,draw->shape,1,1,0);
+void swf_Shape11DrawerInit(drawer_t*draw, TAG*tag)
+{
+    swf_ShapeDrawerInit(draw, tag, 1, 1);
+}
+
+static void swf_ShapeDrawerSetLineStyle(drawer_t*draw, void*style)
+{
+    SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
+}
+static void swf_ShapeDrawerSetFillStyle(drawer_t*draw, void*style)
+{
+    SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
 }
-void swf_DrawerMoveTo(SWFSHAPEDRAWER*draw, FPOINT * to)
+static void swf_ShapeDrawerMoveTo(drawer_t*draw, FPOINT * to)
 {
+    SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
     int x = to->x*20;
     int y = to->y*20;
-    swf_ShapeSetMove(draw->tag,draw->shape,x,y);
-    draw->lastx = x;
-    draw->lasty = y;
+    swf_ShapeSetMove(sdraw->tag,sdraw->shape,x,y);
+    sdraw->lastx = x;
+    sdraw->lasty = y;
     draw->pos = *to;
 }
-void swf_DrawerLineTo(SWFSHAPEDRAWER*draw, FPOINT * to)
+static void swf_ShapeDrawerLineTo(drawer_t*draw, FPOINT * to)
 {
+    SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
     int x = to->x*20;
     int y = to->y*20;
-    if(draw->lastx < draw->bbox.xmin) draw->bbox.xmin = draw->lastx;
-    if(draw->lasty < draw->bbox.ymin) draw->bbox.ymin = draw->lasty;
-    if(draw->lastx > draw->bbox.xmax) draw->bbox.xmax = draw->lastx;
-    if(draw->lasty > draw->bbox.ymax) draw->bbox.ymax = draw->lasty;
-    if(x < draw->bbox.xmin) draw->bbox.xmin = x;
-    if(y < draw->bbox.ymin) draw->bbox.ymin = y;
-    if(x > draw->bbox.xmax) draw->bbox.xmax = x;
-    if(y > draw->bbox.ymax) draw->bbox.ymax = y;
-    swf_ShapeSetLine(draw->tag,draw->shape,x-draw->lastx,y-draw->lasty);
-    draw->lastx = x;
-    draw->lasty = y;
+    if(sdraw->lastx < sdraw->bbox.xmin) sdraw->bbox.xmin = sdraw->lastx;
+    if(sdraw->lasty < sdraw->bbox.ymin) sdraw->bbox.ymin = sdraw->lasty;
+    if(sdraw->lastx > sdraw->bbox.xmax) sdraw->bbox.xmax = sdraw->lastx;
+    if(sdraw->lasty > sdraw->bbox.ymax) sdraw->bbox.ymax = sdraw->lasty;
+    if(x < sdraw->bbox.xmin) sdraw->bbox.xmin = x;
+    if(y < sdraw->bbox.ymin) sdraw->bbox.ymin = y;
+    if(x > sdraw->bbox.xmax) sdraw->bbox.xmax = x;
+    if(y > sdraw->bbox.ymax) sdraw->bbox.ymax = y;
+    swf_ShapeSetLine(sdraw->tag,sdraw->shape,x-sdraw->lastx,y-sdraw->lasty);
+    sdraw->lastx = x;
+    sdraw->lasty = y;
     draw->pos = *to;
 }
-void swf_DrawerSplineTo(SWFSHAPEDRAWER*draw, FPOINT * c1, FPOINT*  to)
+static void swf_ShapeDrawerSplineTo(drawer_t*draw, FPOINT * c1, FPOINT*  to)
 {
+    SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
     int tx = c1->x*20;
     int ty = c1->y*20;
     int x = to->x*20;
     int y = to->y*20;
-    if(draw->lastx < draw->bbox.xmin) draw->bbox.xmin = draw->lastx;
-    if(draw->lasty < draw->bbox.ymin) draw->bbox.ymin = draw->lasty;
-    if(draw->lastx > draw->bbox.xmax) draw->bbox.xmax = draw->lastx;
-    if(draw->lasty > draw->bbox.ymax) draw->bbox.ymax = draw->lasty;
-    if(x < draw->bbox.xmin) draw->bbox.xmin = x;
-    if(y < draw->bbox.ymin) draw->bbox.ymin = y;
-    if(x > draw->bbox.xmax) draw->bbox.xmax = x;
-    if(y > draw->bbox.ymax) draw->bbox.ymax = y;
-    if(tx < draw->bbox.xmin) draw->bbox.xmin = tx;
-    if(ty < draw->bbox.ymin) draw->bbox.ymin = ty;
-    if(tx > draw->bbox.xmax) draw->bbox.xmax = tx;
-    if(ty > draw->bbox.ymax) draw->bbox.ymax = ty;
-    swf_ShapeSetCurve(draw->tag,draw->shape, tx-draw->lastx,ty-draw->lasty, x-tx,y-ty);
-    draw->lastx = x;
-    draw->lasty = y;
+    if(sdraw->lastx < sdraw->bbox.xmin) sdraw->bbox.xmin = sdraw->lastx;
+    if(sdraw->lasty < sdraw->bbox.ymin) sdraw->bbox.ymin = sdraw->lasty;
+    if(sdraw->lastx > sdraw->bbox.xmax) sdraw->bbox.xmax = sdraw->lastx;
+    if(sdraw->lasty > sdraw->bbox.ymax) sdraw->bbox.ymax = sdraw->lasty;
+    if(x < sdraw->bbox.xmin) sdraw->bbox.xmin = x;
+    if(y < sdraw->bbox.ymin) sdraw->bbox.ymin = y;
+    if(x > sdraw->bbox.xmax) sdraw->bbox.xmax = x;
+    if(y > sdraw->bbox.ymax) sdraw->bbox.ymax = y;
+    if(tx < sdraw->bbox.xmin) sdraw->bbox.xmin = tx;
+    if(ty < sdraw->bbox.ymin) sdraw->bbox.ymin = ty;
+    if(tx > sdraw->bbox.xmax) sdraw->bbox.xmax = tx;
+    if(ty > sdraw->bbox.ymax) sdraw->bbox.ymax = ty;
+    swf_ShapeSetCurve(sdraw->tag,sdraw->shape, tx-sdraw->lastx,ty-sdraw->lasty, x-tx,y-ty);
+    sdraw->lastx = x;
+    sdraw->lasty = y;
     draw->pos = *to;
 }
-void swf_DrawerCubicTo(SWFSHAPEDRAWER*draw, FPOINT*  c1, FPOINT* c2, FPOINT*  to)
+static void swf_ShapeDrawerFinish(drawer_t*draw)
 {
-    /*TODO*/
-    draw->pos = *to;
-}
-void swf_DrawerConicTo(SWFSHAPEDRAWER*draw, FPOINT* c, FPOINT*  to)
-{
-    FPOINT* pos = &draw->pos;
-    FPOINT c1,c2;
-    c1.x = (pos->x + 2 * c->x) / 3;
-    c1.y = (pos->y + 2 * c->y) / 3;
-    c2.x = (2 * c->x + to->x) / 3;
-    c2.y = (2 * c->y + to->y) / 3;
-    swf_DrawerCubicTo(draw, &c1,&c2,to);
-    draw->pos = *to;
-}
-void swf_DrawerFinish(SWFSHAPEDRAWER*draw)
-{
-    if(draw->bbox.xmin == SCOORD_MAX) {
+    SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
+    if(sdraw->bbox.xmin == SCOORD_MAX) {
        /* no points at all -> empty bounding box */
-       draw->bbox.xmin = draw->bbox.ymin = 
-       draw->bbox.xmax = draw->bbox.ymax = 0;
-    }
-    draw->isfinished = 1;
-    swf_ShapeSetEnd(draw->tag);
-}
-SHAPE* swf_DrawerToShape(SWFSHAPEDRAWER*draw)
-{
-    SHAPE* shape = malloc(sizeof(SHAPE));
-    if(!draw->isfinished) {
-       fprintf(stderr, "Warning: you should Finish() your drawer before calling DrawerToShape");
-       swf_DrawerFinish(draw);
+       sdraw->bbox.xmin = sdraw->bbox.ymin = 
+       sdraw->bbox.xmax = sdraw->bbox.ymax = 0;
     }
-    memcpy(shape, draw->shape, sizeof(SHAPE));
-    shape->bitlen = (draw->tag->len-1)*8;
-    shape->data = (U8*)malloc(draw->tag->len-1);
-    memcpy(shape->data, &draw->tag->data[1], draw->tag->len-1);
-    return shape;
+    sdraw->isfinished = 1;
+    swf_ShapeSetEnd(sdraw->tag);
 }
-void swf_DrawerFree(SWFSHAPEDRAWER*draw)
+
+static void swf_ShapeDrawerClear(drawer_t*draw)
 {
-    if(draw->tagfree) {
-       swf_DeleteTag(draw->tag);
-       draw->tag = 0;
+    SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
+    if(sdraw->tagfree) {
+       swf_DeleteTag(sdraw->tag);
+       sdraw->tag = 0;
     }
-//    swf_ShapeFree(draw->s);
- //   draw->s = 0;
+    swf_ShapeFree(sdraw->shape);
+    sdraw->shape = 0;
+
+    free(draw->internal);
+    draw->internal = 0;
 }
 
-static char* getToken(const char**p)
+SRECT swf_ShapeDrawerGetBBox(drawer_t*draw)
 {
-    const char*start;
-    char*result;
-    while(**p && strchr(" ,\t\n\r", **p)) {
-       (*p)++;
-    } 
-    start = *p;
-    while(**p && !strchr(" ,\t\n\r", **p)) {
-       (*p)++;
-    }
-    result = malloc((*p)-start+1);
-    memcpy(result,start,(*p)-start+1);
-    result[(*p)-start] = 0;
-    return result;
+    SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
+    return sdraw->bbox;
 }
 
-void swf_DrawString(SWFSHAPEDRAWER*draw, const char*string)
+SHAPE* swf_ShapeDrawerToShape(drawer_t*draw)
 {
-    const char*p = string;
-    while(*p) {
-       char*token = getToken(&p);
-       if(!strncmp(token, "moveTo", 6)) {
-           FPOINT to;
-           to.x = atoi(getToken(&p));
-           to.y = atoi(getToken(&p));
-           swf_DrawerMoveTo(draw, &to);
-       }
-       else if(!strncmp(token, "lineTo", 6)) {
-           FPOINT to;
-           to.x = atoi(getToken(&p));
-           to.y = atoi(getToken(&p));
-           swf_DrawerLineTo(draw, &to);
-       }
-       else if(!strncmp(token, "curveTo", 7) || !strncmp(token, "splineTo", 8)) {
-           FPOINT mid,to;
-           mid.x = atoi(getToken(&p));
-           mid.y = atoi(getToken(&p));
-           to.x = atoi(getToken(&p));
-           to.y = atoi(getToken(&p));
-           swf_DrawerSplineTo(draw, &mid, &to);
-       }
-       free(token);
+    SWFSHAPEDRAWER*sdraw = (SWFSHAPEDRAWER*)draw->internal;
+    SHAPE* shape = malloc(sizeof(SHAPE));
+    if(!sdraw->isfinished) {
+       fprintf(stderr, "Warning: you should Finish() your drawer before calling DrawerToShape");
+       swf_ShapeDrawerFinish(draw);
     }
-    //swf_DrawerFinish(draw); // must be done by application.
+    memcpy(shape, sdraw->shape, sizeof(SHAPE));
+    shape->bitlen = (sdraw->tag->len-1)*8;
+    shape->data = (U8*)malloc(sdraw->tag->len-1);
+    memcpy(shape->data, &sdraw->tag->data[1], sdraw->tag->len-1);
+    return shape;
 }