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