win32 compile fixes
[swftools.git] / lib / devices / arts.c
1 /* arts.c
2
3    Part of the swftools package.
4
5    Copyright (c) 2005 Matthias Kramm <kramm@quiss.org> 
6  
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <stdarg.h>
24 #ifndef WIN32
25 #include <unistd.h>
26 #endif
27 #include <memory.h>
28 #include <string.h>
29 #include <math.h>
30 #include "../mem.h"
31 #include "../gfxdevice.h"
32 #include "../gfxtools.h"
33 #include "../art/libart.h"
34 #include "arts.h"
35 #include "artsutils.h"
36
37 typedef struct _clip {
38     ArtSVP*svp;
39     struct _clip*next;
40 } clip_t;
41
42 typedef struct _internal {
43     gfxdevice_t*out;
44     clip_t*clip;
45     ArtSVP*svpunion;
46 } internal_t;
47
48 static int verbose = 0;
49
50 static void dbg(char*format, ...)
51 {
52     if(!verbose)
53         return;
54     char buf[1024];
55     int l;
56     va_list arglist;
57     va_start(arglist, format);
58     vsprintf(buf, format, arglist);
59     va_end(arglist);
60     l = strlen(buf);
61     while(l && buf[l-1]=='\n') {
62         buf[l-1] = 0;
63         l--;
64     }
65     printf("(device-arts) %s\n", buf);
66     fflush(stdout);
67 }
68
69 int arts_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
70 {
71     dbg("arts_setparameter");
72     internal_t*i = (internal_t*)dev->internal;
73     if(i->out) return i->out->setparameter(i->out,key,value);
74     else return 0;
75 }
76
77 void arts_startpage(struct _gfxdevice*dev, int width, int height)
78 {
79     dbg("arts_startpage");
80     internal_t*i = (internal_t*)dev->internal;
81     if(i->out) i->out->startpage(i->out,width,height);
82 }
83
84 void arts_startclip(struct _gfxdevice*dev, gfxline_t*line)
85 {
86     dbg("arts_startclip");
87     internal_t*i = (internal_t*)dev->internal;
88     ArtSVP* svp = gfxfillToSVP(line, 1);
89
90     svp = art_svp_rewind_uncrossed(art_svp_uncross(svp),ART_WIND_RULE_ODDEVEN); /*FIXME*/
91
92     if(i->clip) {
93         ArtSVP*old = i->clip->svp;
94         clip_t*n = i->clip;
95         i->clip = (clip_t*)rfx_calloc(sizeof(clip_t));
96         i->clip->next = n;
97         i->clip->svp = art_svp_intersect(svp, old);
98         art_svp_free(svp);
99     } else {
100         i->clip = (clip_t*)rfx_calloc(sizeof(clip_t));
101         i->clip->svp = svp;
102     }
103 }
104
105 void arts_endclip(struct _gfxdevice*dev)
106 {
107     dbg("arts_endclip");
108     internal_t*i = (internal_t*)dev->internal;
109
110     if(i->clip) {
111         clip_t*old = i->clip;
112         i->clip = i->clip->next;
113         art_svp_free(old->svp);old->svp = 0;
114         old->next = 0;free(old);
115     } else {
116         fprintf(stderr, "Error: endclip without startclip\n");
117     }
118 }
119
120 void addtounion(struct _gfxdevice*dev, ArtSVP*svp)
121 {
122     internal_t*i = (internal_t*)dev->internal;
123     if(i->svpunion) {
124         ArtSVP*old = i->svpunion;
125         i->svpunion = art_svp_union(svp,i->svpunion);
126         art_svp_free(old);
127     }
128 }
129
130 void arts_stroke(struct _gfxdevice*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*color, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit)
131 {
132     dbg("arts_stroke");
133     internal_t*i = (internal_t*)dev->internal;
134     //i->out->stroke(i->out, line, width, color, cap_style, joint_style, miterLimit);
135     ArtSVP* svp = gfxstrokeToSVP(line, width, cap_style, joint_style, miterLimit);
136     if(i->clip) {
137         ArtSVP*old = svp;
138         svp = art_svp_intersect(svp, i->clip->svp);
139         art_svp_free(old);
140     }
141     addtounion(dev,svp);
142     gfxline_t*gfxline = SVPtogfxline(svp);
143     if(i->out) i->out->fill(i->out, gfxline, color);
144     free(gfxline);
145     art_svp_free(svp);
146 }
147
148 void arts_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
149 {
150     dbg("arts_fill");
151     internal_t*i = (internal_t*)dev->internal;
152     ArtSVP* svp = gfxfillToSVP(line, 1);
153     
154     if (svp->n_segs > 500)
155     {
156         int lineParts = 0;
157         gfxline_t* lineCursor = line;
158         while(lineCursor != NULL)
159         {
160             if(lineCursor->type != gfx_moveTo) ++lineParts;
161             lineCursor = lineCursor->next;
162         }
163         fprintf(stderr, "arts_fill abandonning shape with %d segments (%d line parts)\n", svp->n_segs, lineParts);
164         art_svp_free(svp);
165         return;
166     }
167
168     svp = art_svp_rewind_uncrossed(art_svp_uncross(svp),ART_WIND_RULE_ODDEVEN); /*FIXME*/
169
170     if(i->clip) {
171         ArtSVP*old = svp;
172         svp = art_svp_intersect(svp, i->clip->svp);
173         art_svp_free(old);
174     }
175     addtounion(dev,svp);
176     gfxline_t*gfxline = SVPtogfxline(svp);
177     if(i->out) i->out->fill(i->out, gfxline, color);
178     free(gfxline);
179     art_svp_free(svp);
180 }
181
182 void arts_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
183 {
184     dbg("arts_fillbitmap");
185     internal_t*i = (internal_t*)dev->internal;
186     ArtSVP* svp = gfxfillToSVP(line, 1);
187
188     if(i->clip) {
189         ArtSVP*old = svp;
190         svp = art_svp_intersect(svp, i->clip->svp);
191         art_svp_free(old);
192     }
193     addtounion(dev,svp);
194     gfxline_t*gfxline = SVPtogfxline(svp);
195     if(i->out) i->out->fillbitmap(i->out, gfxline, img, matrix, cxform);
196     free(gfxline);
197     art_svp_free(svp);
198 }
199
200 void arts_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
201 {
202     dbg("arts_fillgradient");
203     internal_t*i = (internal_t*)dev->internal;
204     ArtSVP* svp = gfxfillToSVP(line, 1);
205     if(i->clip) {
206         ArtSVP*old = svp;
207         svp = art_svp_intersect(svp, i->clip->svp);
208         art_svp_free(old);
209     }
210     addtounion(dev,svp);
211     gfxline_t*gfxline = SVPtogfxline(svp);
212     if(i->out) i->out->fillgradient(i->out, gfxline, gradient, type, matrix);
213     free(gfxline);
214     art_svp_free(svp);
215 }
216
217 void arts_addfont(struct _gfxdevice*dev, gfxfont_t*font)
218 {
219     dbg("arts_addfont");
220     internal_t*i = (internal_t*)dev->internal;
221     if(i->out) i->out->addfont(i->out, font);
222 }
223
224 void arts_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
225 {
226     dbg("arts_drawchar");
227     if(!font)
228         return;
229     internal_t*i = (internal_t*)dev->internal;
230     gfxline_t*glyph = gfxline_clone(font->glyphs[glyphnr].line);
231     gfxline_transform(glyph, matrix);
232
233     if(i->clip) {
234         gfxbbox_t bbox = gfxline_getbbox(glyph);
235         ArtSVP*dummybox = boxToSVP(bbox.xmin,bbox.ymin,bbox.xmax,bbox.ymax);
236         ArtSVP*svp = art_svp_intersect(dummybox, i->clip->svp);
237         gfxline_t*gfxline = SVPtogfxline(svp);
238         gfxbbox_t bbox2 = gfxline_getbbox(gfxline);
239         double w = bbox2.xmax - bbox2.xmin;
240         double h = bbox2.ymax - bbox2.ymin;
241         
242         addtounion(dev, svp); // TODO: use the whole char, not just the bbox
243
244         if(w < 0.001 || h < 0.001) /* character was clipped completely */ {
245         } else if(fabs((bbox.xmax - bbox.xmin) - w) > 0.05 ||
246                   fabs((bbox.ymax - bbox.ymin) - h) > 0.05) {
247             /* notable change in character size: character was clipped 
248                TODO: handle diagonal cuts
249              */
250             arts_fill(dev, glyph, color);
251         } else {
252             if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
253         }
254     } else {
255         if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
256     }
257     
258     gfxline_free(glyph);
259 }
260
261 void arts_drawlink(struct _gfxdevice*dev, gfxline_t*line, const char*action)
262 {
263     dbg("arts_drawlink");
264     internal_t*i = (internal_t*)dev->internal;
265     if(i->out) i->out->drawlink(i->out, line, action);
266 }
267
268 void arts_endpage(struct _gfxdevice*dev)
269 {
270     dbg("arts_endpage");
271     internal_t*i = (internal_t*)dev->internal;
272     if(i->out) i->out->endpage(i->out);
273 }
274
275 gfxresult_t* arts_finish(struct _gfxdevice*dev)
276 {
277     dbg("arts_finish");
278     internal_t*i = (internal_t*)dev->internal;
279     if(i->out) {
280         return i->out->finish(i->out);
281     } else {
282         return 0;
283     }
284 }
285
286 gfxline_t*gfxdevice_union_getunion(struct _gfxdevice*dev)
287 {
288     internal_t*i = (internal_t*)dev->internal;
289     return SVPtogfxline(i->svpunion);
290 }
291
292 void gfxdevice_removeclippings_init(gfxdevice_t*dev, gfxdevice_t*out)
293 {
294     dbg("gfxdevice_removeclippings_init");
295     internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
296     memset(dev, 0, sizeof(gfxdevice_t));
297     
298     dev->name = "removeclippings";
299
300     dev->internal = i;
301
302     dev->setparameter = arts_setparameter;
303     dev->startpage = arts_startpage;
304     dev->startclip = arts_startclip;
305     dev->endclip = arts_endclip;
306     dev->stroke = arts_stroke;
307     dev->fill = arts_fill;
308     dev->fillbitmap = arts_fillbitmap;
309     dev->fillgradient = arts_fillgradient;
310     dev->addfont = arts_addfont;
311     dev->drawchar = arts_drawchar;
312     dev->drawlink = arts_drawlink;
313     dev->endpage = arts_endpage;
314     dev->finish = arts_finish;
315
316     i->out = out;
317     i->svpunion = 0;
318 }
319
320 void gfxdevice_union_init(gfxdevice_t*dev,gfxdevice_t*out)
321 {
322     dbg("gfxdevice_getunion_init");
323     internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
324     memset(dev, 0, sizeof(gfxdevice_t));
325     
326     dev->name = "union";
327
328     dev->internal = i;
329
330     dev->setparameter = arts_setparameter;
331     dev->startpage = arts_startpage;
332     dev->startclip = arts_startclip;
333     dev->endclip = arts_endclip;
334     dev->stroke = arts_stroke;
335     dev->fill = arts_fill;
336     dev->fillbitmap = arts_fillbitmap;
337     dev->fillgradient = arts_fillgradient;
338     dev->addfont = arts_addfont;
339     dev->drawchar = arts_drawchar;
340     dev->drawlink = arts_drawlink;
341     dev->endpage = arts_endpage;
342     dev->finish = arts_finish;
343
344     i->out = out;
345     i->svpunion = gfxstrokeToSVP(0, 0, gfx_capButt, gfx_joinMiter, 0);
346 }
347