510df3e5bf5ac883b73513593f6ea954e53959cf
[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 "../mem.h"
27 #include "../gfxdevice.h"
28 #include "../gfxtools.h"
29 #include "../art/libart.h"
30 #include "artsutils.c"
31
32 typedef struct _clip {
33     ArtSVP*svp;
34     struct _clip*next;
35 } clip_t;
36
37 typedef struct _internal {
38     gfxdevice_t*out;
39     clip_t*clip;
40     ArtSVP*svpunion;
41 } internal_t;
42
43 static int verbose = 0;
44 static void dbg(char*format, ...)
45 {
46     if(!verbose)
47         return;
48     char buf[1024];
49     int l;
50     va_list arglist;
51     va_start(arglist, format);
52     vsprintf(buf, format, arglist);
53     va_end(arglist);
54     l = strlen(buf);
55     while(l && buf[l-1]=='\n') {
56         buf[l-1] = 0;
57         l--;
58     }
59     printf("(device-arts) %s\n", buf);
60     fflush(stdout);
61 }
62
63 int arts_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
64 {
65     dbg("arts_setparameter");
66     internal_t*i = (internal_t*)dev->internal;
67     if(i->out) return i->out->setparameter(i->out,key,value);
68     else return 0;
69 }
70
71 void arts_startpage(struct _gfxdevice*dev, int width, int height)
72 {
73     dbg("arts_startpage");
74     internal_t*i = (internal_t*)dev->internal;
75     if(i->out) i->out->startpage(i->out,width,height);
76 }
77
78 void arts_startclip(struct _gfxdevice*dev, gfxline_t*line)
79 {
80     dbg("arts_startclip");
81     internal_t*i = (internal_t*)dev->internal;
82     ArtSVP* svp = gfxfillToSVP(line, 1);
83
84     svp = art_svp_rewind_uncrossed(art_svp_uncross(svp),ART_WIND_RULE_ODDEVEN); /*FIXME*/
85
86     if(i->clip) {
87         ArtSVP*old = i->clip->svp;
88         clip_t*n = i->clip;
89         i->clip = (clip_t*)rfx_calloc(sizeof(clip_t));
90         i->clip->next = n;
91         i->clip->svp = art_svp_intersect(svp, old);
92         art_svp_free(svp);
93     } else {
94         i->clip = (clip_t*)rfx_calloc(sizeof(clip_t));
95         i->clip->svp = svp;
96     }
97 }
98
99 void arts_endclip(struct _gfxdevice*dev)
100 {
101     dbg("arts_endclip");
102     internal_t*i = (internal_t*)dev->internal;
103
104     if(i->clip) {
105         clip_t*old = i->clip;
106         i->clip = i->clip->next;
107         art_svp_free(old->svp);old->svp = 0;
108         old->next = 0;free(old);
109     } else {
110         fprintf(stderr, "Error: endclip without startclip\n");
111     }
112 }
113
114 void addtounion(struct _gfxdevice*dev, ArtSVP*svp)
115 {
116     internal_t*i = (internal_t*)dev->internal;
117     if(i->svpunion) {
118         ArtSVP*old = i->svpunion;
119         i->svpunion = art_svp_union(svp,i->svpunion);
120         art_svp_free(old);
121     }
122 }
123
124 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)
125 {
126     dbg("arts_stroke");
127     internal_t*i = (internal_t*)dev->internal;
128     //i->out->stroke(i->out, line, width, color, cap_style, joint_style, miterLimit);
129     ArtSVP* svp = gfxstrokeToSVP(line, width, cap_style, joint_style, miterLimit);
130     if(i->clip) {
131         ArtSVP*old = svp;
132         svp = art_svp_intersect(svp, i->clip->svp);
133         art_svp_free(old);
134     }
135     addtounion(dev,svp);
136     gfxline_t*gfxline = SVPtogfxline(svp);
137     if(i->out) i->out->fill(i->out, gfxline, color);
138     free(gfxline);
139     art_svp_free(svp);
140 }
141
142 void arts_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
143 {
144     dbg("arts_fill");
145     internal_t*i = (internal_t*)dev->internal;
146     ArtSVP* svp = gfxfillToSVP(line, 1);
147     
148     if (svp->n_segs > 500)
149     {
150         int lineParts = 0;
151         gfxline_t* lineCursor = line;
152         while(lineCursor != NULL)
153         {
154             if(lineCursor->type != gfx_moveTo) ++lineParts;
155             lineCursor = lineCursor->next;
156         }
157         fprintf(stderr, "arts_fill abandonning shape with %d segments (%d line parts)\n", svp->n_segs, lineParts);
158         art_svp_free(svp);
159         return;
160     }
161
162     svp = art_svp_rewind_uncrossed(art_svp_uncross(svp),ART_WIND_RULE_ODDEVEN); /*FIXME*/
163
164     if(i->clip) {
165         ArtSVP*old = svp;
166         svp = art_svp_intersect(svp, i->clip->svp);
167         art_svp_free(old);
168     }
169     addtounion(dev,svp);
170     gfxline_t*gfxline = SVPtogfxline(svp);
171     if(i->out) i->out->fill(i->out, gfxline, color);
172     free(gfxline);
173     art_svp_free(svp);
174 }
175
176 void arts_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
177 {
178     dbg("arts_fillbitmap");
179     internal_t*i = (internal_t*)dev->internal;
180     ArtSVP* svp = gfxfillToSVP(line, 1);
181
182     // We need to make sure that the SVP we now have bounds an area (i.e. the
183     // source line wound anticlockwise) rather than excludes an area (i.e. the
184     // line wound clockwise). It seems that PDF (or xpdf) is less strict about
185     // this for bitmaps than it is for fill areas.
186     //
187     // To check this, we'll sum the cross products of all pairs of adjacent
188     // lines. If the result is positive, the direction is correct; if not, we
189     // need to reverse the sense of the SVP generated. The easiest way to do
190     // this is to flip the up/down flags of all the segments.
191     //
192     // This is approximate; since the gfxline_t structure can contain any
193     // combination of moveTo, lineTo and splineTo in any order, not all pairs
194     // of lines in the shape that share a point need be described next to each
195     // other in the sequence. For ease, we'll consider only pairs of lines
196     // stored as lineTos and splineTos without intervening moveTos.
197     //
198     // TODO is this a valid algorithm? My vector maths is rusty.
199     //
200     // It may be more correct to instead reverse the line before we feed it
201     // into gfxfilltoSVP. However, this seems equivalent and is easier to
202     // implement!
203     double total_cross_product = 0.0;
204     gfxline_t* cursor = line;
205     if (cursor != NULL)
206     {
207         double x_last = cursor->x;
208         double y_last = cursor->y;
209         cursor = cursor->next;
210
211         while((cursor != NULL) && (cursor->next != NULL))
212         {
213             if (((cursor->type == gfx_lineTo) || (cursor->type == gfx_splineTo))
214                 && ((cursor->next->type == gfx_lineTo) || (cursor->next->type == gfx_splineTo)))
215             {
216                 // Process these lines
217                 //
218                 // In this space (x right, y down) the cross-product is
219                 // (x1 * y0) - (x0 * y1)
220                 double x0 = cursor->x - x_last;
221                 double y0 = cursor->y - y_last;
222                 double x1 = cursor->next->x - cursor->x;
223                 double y1 = cursor->next->y - cursor->y;
224                 total_cross_product += (x1 * y0) - (x0 * y1);
225             }
226
227             x_last = cursor->x;
228             y_last = cursor->y;
229             cursor = cursor->next;
230         }
231     }
232     if (total_cross_product < 0.0)
233     {
234         int i = 0;
235         for(; i < svp->n_segs; ++i)
236         {
237             if (svp->segs[i].dir != 0)
238                 svp->segs[i].dir = 0;
239             else
240                 svp->segs[i].dir = 1;
241         }
242     }
243
244     if(i->clip) {
245         ArtSVP*old = svp;
246         svp = art_svp_intersect(svp, i->clip->svp);
247         art_svp_free(old);
248     }
249     addtounion(dev,svp);
250     gfxline_t*gfxline = SVPtogfxline(svp);
251     if(i->out) i->out->fillbitmap(i->out, gfxline, img, matrix, cxform);
252     free(gfxline);
253     art_svp_free(svp);
254 }
255
256 void arts_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
257 {
258     dbg("arts_fillgradient");
259     internal_t*i = (internal_t*)dev->internal;
260     ArtSVP* svp = gfxfillToSVP(line, 1);
261     if(i->clip) {
262         ArtSVP*old = svp;
263         svp = art_svp_intersect(svp, i->clip->svp);
264         art_svp_free(old);
265     }
266     addtounion(dev,svp);
267     gfxline_t*gfxline = SVPtogfxline(svp);
268     if(i->out) i->out->fillgradient(i->out, gfxline, gradient, type, matrix);
269     free(gfxline);
270     art_svp_free(svp);
271 }
272
273 void arts_addfont(struct _gfxdevice*dev, gfxfont_t*font)
274 {
275     dbg("arts_addfont");
276     internal_t*i = (internal_t*)dev->internal;
277     if(i->out) i->out->addfont(i->out, font);
278 }
279
280 void arts_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
281 {
282     dbg("arts_drawchar");
283     internal_t*i = (internal_t*)dev->internal;
284     gfxline_t*glyph = gfxline_clone(font->glyphs[glyphnr].line);
285     gfxline_transform(glyph, matrix);
286
287     if(i->clip) {
288         gfxbbox_t bbox = gfxline_getbbox(glyph);
289         ArtSVP*dummybox = boxToSVP(bbox.xmin,bbox.ymin,bbox.xmax,bbox.ymax);
290         ArtSVP*svp = art_svp_intersect(dummybox, i->clip->svp);
291         gfxline_t*gfxline = SVPtogfxline(svp);
292         gfxbbox_t bbox2 = gfxline_getbbox(gfxline);
293         double w = bbox2.xmax - bbox2.xmin;
294         double h = bbox2.ymax - bbox2.ymin;
295         
296         addtounion(dev, svp); // TODO: use the whole char, not just the bbox
297
298         if(w < 0.001 || h < 0.001) /* character was clipped completely */ {
299         } else if(fabs((bbox.xmax - bbox.xmin) - w) > 0.05 ||
300                   fabs((bbox.ymax - bbox.ymin) - h) > 0.05) {
301             /* notable change in character size: character was clipped 
302                TODO: handle diagonal cuts
303              */
304             arts_fill(dev, glyph, color);
305         } else {
306             if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
307         }
308     } else {
309         if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
310     }
311     
312     gfxline_free(glyph);
313 }
314
315 void arts_drawlink(struct _gfxdevice*dev, gfxline_t*line, char*action)
316 {
317     dbg("arts_drawlink");
318     internal_t*i = (internal_t*)dev->internal;
319     if(i->out) i->out->drawlink(i->out, line, action);
320 }
321
322 void arts_endpage(struct _gfxdevice*dev)
323 {
324     dbg("arts_endpage");
325     internal_t*i = (internal_t*)dev->internal;
326     if(i->out) i->out->endpage(i->out);
327 }
328
329 gfxresult_t* arts_finish(struct _gfxdevice*dev)
330 {
331     dbg("arts_finish");
332     internal_t*i = (internal_t*)dev->internal;
333     if(i->out) {
334         return i->out->finish(i->out);
335     } else {
336         return 0;
337     }
338 }
339
340 gfxline_t*gfxdevice_union_getunion(struct _gfxdevice*dev)
341 {
342     internal_t*i = (internal_t*)dev->internal;
343     return SVPtogfxline(i->svpunion);
344 }
345
346 void gfxdevice_removeclippings_init(gfxdevice_t*dev, gfxdevice_t*out)
347 {
348     dbg("gfxdevice_removeclippings_init");
349     internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
350     memset(dev, 0, sizeof(gfxdevice_t));
351     
352     dev->name = "removeclippings";
353
354     dev->internal = i;
355
356     dev->setparameter = arts_setparameter;
357     dev->startpage = arts_startpage;
358     dev->startclip = arts_startclip;
359     dev->endclip = arts_endclip;
360     dev->stroke = arts_stroke;
361     dev->fill = arts_fill;
362     dev->fillbitmap = arts_fillbitmap;
363     dev->fillgradient = arts_fillgradient;
364     dev->addfont = arts_addfont;
365     dev->drawchar = arts_drawchar;
366     dev->drawlink = arts_drawlink;
367     dev->endpage = arts_endpage;
368     dev->finish = arts_finish;
369
370     i->out = out;
371     i->svpunion = 0;
372 }
373
374 void gfxdevice_union_init(gfxdevice_t*dev,gfxdevice_t*out)
375 {
376     dbg("gfxdevice_getunion_init");
377     internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
378     memset(dev, 0, sizeof(gfxdevice_t));
379     
380     dev->name = "union";
381
382     dev->internal = i;
383
384     dev->setparameter = arts_setparameter;
385     dev->startpage = arts_startpage;
386     dev->startclip = arts_startclip;
387     dev->endclip = arts_endclip;
388     dev->stroke = arts_stroke;
389     dev->fill = arts_fill;
390     dev->fillbitmap = arts_fillbitmap;
391     dev->fillgradient = arts_fillgradient;
392     dev->addfont = arts_addfont;
393     dev->drawchar = arts_drawchar;
394     dev->drawlink = arts_drawlink;
395     dev->endpage = arts_endpage;
396     dev->finish = arts_finish;
397
398     i->out = out;
399     i->svpunion = gfxstrokeToSVP(0, 0, 0, 0, 0);
400 }
401