new parameter -s textonly
[swftools.git] / lib / gfxpoly / test_stroke.c
1 #include <math.h>
2 #include "../gfxtools.h"
3 #include "convert.h"
4 #include "stroke.h"
5 #include "convert.h"
6
7 int test_stroke1()
8 {
9     gfxline_t l[512], f[256*5];
10
11     int width=700,height=700;
12     gfxdevice_t dev;
13     gfxdevice_swf_init(&dev);
14     dev.setparameter(&dev, "framerate", "25.0");
15     dev.setparameter(&dev, "disable_polygon_conversion", "1");
16     int t;
17     for(t=0;t<300;t++) {
18         dev.startpage(&dev, 700,700);
19         /*gfxline_t*g = l;
20         while(g) {
21             g->x += g->sx;
22             g->y += g->sy;
23             if(g->sx || g->sy) {
24                 if(g->x<200) {g->x=400-g->x;g->sx=-g->sx;}
25                 if(g->y<200) {g->y=400-g->y;g->sy=-g->sy;}
26                 if(g->x>500) {g->x=1000-g->x;g->sx=-g->sx;}
27                 if(g->y>500) {g->y=1000-g->y;g->sy=-g->sy;}
28             }
29             g = g->next;
30         }*/
31
32         int i;
33         for(i=0;i<512;i++) {
34             double a = i*0.05+t*M_PI/150;
35             double r = 50+i*0.5;
36             l[i].x = cos(a)*r + width/2;
37             l[i].y = sin(a)*r + height/2;
38             l[i].sx = (int)((l[i].x-width/2)/30);
39             l[i].sy = (int)((l[i].y-height/2)/30);
40             l[i].sx = l[i].sy = 0;
41             l[i].type = gfx_lineTo;
42             l[i].next = &l[i+1];
43         }
44         l[0].type = gfx_moveTo;
45         l[i-1].next = 0;
46
47         int xx,yy;
48         i = 0;
49         for(yy=0;yy<16;yy++) 
50         for(xx=0;xx<16;xx++) if((xx^yy)&1) {
51             double x = -128+xx*64+t*128.0/300;
52             double y = -128+yy*64;//+t*64.0/300;
53             f[i].x = x;
54             f[i].y = y;
55             f[i].next = &f[i+1];f[i++].type = gfx_moveTo;
56             f[i].x = x+64;
57             f[i].y = y;
58             f[i].next = &f[i+1];f[i++].type = gfx_lineTo;
59             f[i].x = x+64;
60             f[i].y = y+64;
61             f[i].next = &f[i+1];f[i++].type = gfx_lineTo;
62             f[i].x = x;
63             f[i].y = y+64;
64             f[i].next = &f[i+1];f[i++].type = gfx_lineTo;
65             f[i].x = x;
66             f[i].y = y;
67             f[i].next = &f[i+1];f[i++].type = gfx_lineTo;
68         }
69         f[i-1].next = 0;
70
71         double width = 40;
72         
73         //gfxdrawer_t d;
74         //gfxdrawer_target_gfxline(&d);
75         //draw_stroke(l, &d, width, gfx_capRound, gfx_joinBevel, 500);
76         //gfxline_t*line = (gfxline_t*)d.result(&d);
77
78         //gfxpoly_t*p = gfxpoly_fromstroke(l, width, gfx_capRound, gfx_joinRound, 500);
79         gfxpoly_t*p1 = gfxpoly_from_stroke(l, width, gfx_capRound, gfx_joinRound, 500, 0.05);
80         assert(gfxpoly_check(p1, 1));
81         
82         //gfxpoly_t*p2 = gfxpoly_from_fill(f, 0.05);
83         gfxline_t*l2 = gfxline_clone(l);
84
85         double c = cos(t*M_PI/50.0);
86         double s = sin(t*M_PI/50.0);
87         static int x1 = 0, xdir = 1;
88         static int y1 = 0, ydir = 5;
89         x1 = sin(t*M_PI/60.0)*50;
90         y1 = -sin(t*M_PI/50.0)*50;
91         gfxmatrix_t m = { c,  s,  -(350+x1)*c-(350+y1)*s+350,
92                           s, -c,  -(350+x1)*s+(350+y1)*c+350};
93         gfxline_transform(l2, &m);
94         gfxpoly_t*p2 = gfxpoly_from_stroke(l2, width, gfx_capRound, gfx_joinRound, 500, 0.05);
95         assert(gfxpoly_check(p2, 1));
96
97         gfxpoly_t*p3 = gfxpoly_intersect(p1, p2);
98         assert(gfxpoly_check(p3, 1));
99         
100         //gfxpoly_t*p4 = gfxpoly_from_fill(f, 0.05);
101         //gfxpoly_t*p5 = gfxpoly_intersect(p1, p4);
102
103         gfxline_t*line = gfxline_from_gfxpoly(p3);
104         gfxpoly_destroy(p1);
105         gfxpoly_destroy(p2);
106         gfxpoly_destroy(p3);
107
108         //gfxline_dump(line, stdout, "");
109
110         gfxcolor_t blue = {255,0,0,255};
111         gfxline_t*r = gfxline_makerectangle(0,0,700,700);
112         dev.fill(&dev, r, &blue);
113
114         gfxcolor_t black = {255,0,0,64};
115         gfxcolor_t white = {255,255,255,255};
116         gfxcolor_t cyan = {255,0,192,192};
117         //dev.stroke(&dev, l, 2, &black, gfx_capRound, gfx_joinRound, 0);
118         //dev.stroke(&dev, line, 2, &cyan, gfx_capRound, gfx_joinRound, 0);
119         dev.fill(&dev, line, &black);
120         gfxmatrix_t m2 = {1.0,   0, -10,
121                            0, 1.0, -10};
122         gfxline_transform(line, &m2);
123         dev.fill(&dev, line, &cyan);
124         dev.stroke(&dev, line, 2.0, &white, gfx_capRound, gfx_joinRound, 0);
125
126         gfxline_free(line);
127         dev.endpage(&dev);
128     }
129
130     gfxresult_t* result = dev.finish(&dev);
131     result->save(result, "test.swf");
132     result->destroy(result);
133 }
134
135 int test_stroke2()
136 {
137     gfxline_t l[4];
138     l[0].type = gfx_moveTo;
139     l[0].x = 100;l[0].sx=2;
140     l[0].y = 100;l[0].sy=2;
141     l[0].next = &l[1];
142     l[1].type = gfx_lineTo;
143     l[1].x = 100;l[1].sx=2;
144     l[1].y = 200;l[1].sy=-2;
145     l[1].next = &l[2];
146     l[2].type = gfx_lineTo;
147     l[2].x = 250;l[2].sx=4;
148     l[2].y = 200;l[2].sy=0;
149     l[2].next = &l[3];
150     l[3].type = gfx_lineTo;
151     l[3].x = 200;l[3].sx=0;
152     l[3].y = 150;l[3].sy=4;
153     l[3].next = 0;
154
155
156     gfxdevice_t dev;
157     gfxdevice_swf_init(&dev);
158     dev.setparameter(&dev, "framerate", "25.0");
159     int t;
160     for(t=0;t<300;t++) {
161         dev.startpage(&dev, 700,700);
162         gfxline_t*g = l;
163         while(g) {
164             g->x += g->sx;
165             g->y += g->sy;
166             if(g->x<200) {g->x=400-g->x;g->sx=-g->sx;}
167             if(g->y<200) {g->y=400-g->y;g->sy=-g->sy;}
168             if(g->x>500) {g->x=1000-g->x;g->sx=-g->sx;}
169             if(g->y>500) {g->y=1000-g->y;g->sy=-g->sy;}
170             g = g->next;
171         }
172         //l[3].x = l[0].x;
173         //l[3].y = l[0].y;
174
175         gfxdrawer_t d;
176         gfxdrawer_target_gfxline(&d);
177         double width = t/3.0;
178         if(width>50) width=100-width;
179         width = 40;
180
181         draw_stroke(l, &d, width, gfx_capSquare, gfx_joinMiter, 500);
182         gfxline_t*line = (gfxline_t*)d.result(&d);
183         //gfxline_dump(line, stdout, "");
184
185         gfxcolor_t black = {255,0,0,0};
186         gfxcolor_t cyan = {255,0,128,128};
187         dev.stroke(&dev, l, 2, &black, gfx_capRound, gfx_joinRound, 0);
188         dev.stroke(&dev, line, 2, &cyan, gfx_capRound, gfx_joinRound, 0);
189         gfxline_free(line);
190         dev.endpage(&dev);
191     }
192
193     gfxresult_t* result = dev.finish(&dev);
194     result->save(result, "test.swf");
195     result->destroy(result);
196 }
197
198 int main()
199 {
200     test_stroke2();
201 }