fix for cases where a clip/shape intersection is zero
[swftools.git] / lib / devices / polyops.c
1 /* polyops.c
2
3    Part of the swftools package.
4
5    Copyright (c) 2008 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 <assert.h>
29 #include <string.h>
30 #include <math.h>
31 #include "../mem.h"
32 #include "../gfxdevice.h"
33 #include "../gfxtools.h"
34 #include "../gfxpoly.h"
35 #include "../log.h"
36 #include "polyops.h"
37
38 typedef struct _clip {
39     gfxpoly_t*poly;
40     int openclips;
41     struct _clip*next;
42 } clip_t;
43
44 typedef struct _internal {
45     gfxdevice_t*out;
46     clip_t*clip;
47     gfxpoly_t*polyunion;
48 } internal_t;
49
50 static int verbose = 0;
51
52 static void dbg(char*format, ...)
53 {
54     if(!verbose)
55         return;
56     char buf[1024];
57     int l;
58     va_list arglist;
59     va_start(arglist, format);
60     vsprintf(buf, format, arglist);
61     va_end(arglist);
62     l = strlen(buf);
63     while(l && buf[l-1]=='\n') {
64         buf[l-1] = 0;
65         l--;
66     }
67     printf("(device-polyops) %s\n", buf);
68     fflush(stdout);
69 }
70
71 int polyops_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
72 {
73     dbg("polyops_setparameter");
74     internal_t*i = (internal_t*)dev->internal;
75     if(i->out) return i->out->setparameter(i->out,key,value);
76     else return 0;
77 }
78
79 void polyops_startpage(struct _gfxdevice*dev, int width, int height)
80 {
81     dbg("polyops_startpage");
82     internal_t*i = (internal_t*)dev->internal;
83     if(i->out) i->out->startpage(i->out,width,height);
84 }
85
86 void polyops_startclip(struct _gfxdevice*dev, gfxline_t*line)
87 {
88     dbg("polyops_startclip");
89     internal_t*i = (internal_t*)dev->internal;
90
91     gfxpoly_t* oldclip = i->clip?i->clip->poly:0;
92     gfxpoly_t* poly = gfxpoly_fillToPoly(line);
93     gfxpoly_t* currentclip = 0;
94     int type = 0;
95
96     /* we can't rely on gfxpoly actually being able to convert
97        a gfxline into a gfxpoly- for polygons which are too
98        complex or just degenerate, this might fail. So handle
99        all the cases where polygon conversion or intersection
100        might go awry */
101     if(!poly && !oldclip) {
102         i->out->startclip(i->out,line);
103         currentclip = 0;
104         type = 1;
105     } else if(!poly && oldclip) {
106         gfxline_t*oldclipline = gfxpoly_to_gfxline(oldclip);
107         i->out->startclip(i->out,oldclipline);
108         i->out->startclip(i->out,line);
109         currentclip = 0;
110         type = 2;
111     } else if(poly && oldclip) {
112         gfxpoly_t*intersection = gfxpoly_intersect(poly, oldclip);
113         if(intersection) {
114             // this case is what usually happens 
115             gfxpoly_free(poly);poly=0;
116             currentclip = intersection;
117             type = 0;
118         } else {
119             gfxline_t*oldclipline = gfxpoly_to_gfxline(oldclip);
120             i->out->startclip(i->out, oldclipline);
121             currentclip = poly;
122             type = 1;
123         }
124     } else if(poly && !oldclip) {
125         currentclip = poly;
126         type = 0;
127     }
128
129     clip_t*n = i->clip;
130     i->clip = (clip_t*)rfx_calloc(sizeof(clip_t));
131     i->clip->next = n;
132     i->clip->poly = currentclip;
133     i->clip->openclips = type;
134 }
135
136 void polyops_endclip(struct _gfxdevice*dev)
137 {
138     dbg("polyops_endclip");
139     internal_t*i = (internal_t*)dev->internal;
140
141     if(!i->clip) {
142         msg("<error> endclip without startclip (in: polyops)\n");
143         return;
144     }
145
146     clip_t*old = i->clip;
147     i->clip = i->clip->next;
148     if(old->poly) {
149         gfxpoly_free(old->poly);old->poly = 0;
150     }
151     int t;
152     for(t=0;t<old->openclips;t++)
153         i->out->endclip(i->out);
154
155     old->next = 0;free(old);
156 }
157
158 static void addtounion(struct _gfxdevice*dev, gfxpoly_t*poly)
159 {
160     internal_t*i = (internal_t*)dev->internal;
161     if(poly && i->polyunion) {
162         gfxpoly_t*old = i->polyunion;
163         gfxpoly_t*newpoly = gfxpoly_union(poly,i->polyunion);
164         i->polyunion = newpoly;
165         gfxpoly_free(old);
166     }
167 }
168
169 static gfxline_t* handle_poly(gfxdevice_t*dev, gfxpoly_t*poly, char*ok)
170 {
171     internal_t*i = (internal_t*)dev->internal;
172     if(i->clip && i->clip->poly) {
173         gfxpoly_t*old = poly;
174         if(poly) {
175             poly = gfxpoly_intersect(poly, i->clip->poly);
176             gfxpoly_free(old);
177         }
178     }
179     addtounion(dev, poly);
180     gfxline_t*gfxline = 0;
181     if(poly) {
182         // this is the case where everything went right
183         gfxline_t*line = gfxpoly_to_gfxline(poly);
184         gfxpoly_free(poly);
185         *ok = 1;
186         return line;
187     } else {
188         if(i->clip && i->clip->poly) {
189             /* convert current clipping from a polygon to an
190                actual "startclip" written to the output */
191             assert(i->clip->openclips <= 1);
192             gfxline_t*clipline = gfxpoly_to_gfxline(i->clip->poly);
193             i->out->startclip(i->out, clipline);
194             gfxline_free(clipline);
195             gfxpoly_free(i->clip->poly);i->clip->poly = 0;
196             i->clip->openclips++;
197             return 0;
198         } else {
199             return 0;
200         }
201     }
202 }
203
204 void polyops_stroke(struct _gfxdevice*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*color, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit)
205 {
206     dbg("polyops_stroke");
207     internal_t*i = (internal_t*)dev->internal;
208
209     gfxpoly_t* poly = gfxpoly_strokeToPoly(line, width, cap_style, joint_style, miterLimit);
210     char ok = 0;
211     gfxline_t*line2 = handle_poly(dev, poly, &ok);
212
213     if(ok) {
214         if(i->out && line2) i->out->fill(i->out, line2, color);
215         gfxline_free(line2);
216     } else {
217         msg("<error> ..");
218         if(i->out) i->out->stroke(i->out, line, width, color, cap_style, joint_style, miterLimit);
219     }
220 }
221
222 void polyops_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
223 {
224     dbg("polyops_fill");
225     internal_t*i = (internal_t*)dev->internal;
226
227     gfxpoly_t*poly = gfxpoly_fillToPoly(line);
228     char ok = 0;
229     gfxline_t*line2 = handle_poly(dev, poly, &ok);
230
231     if(ok) {
232         if(i->out && line2) i->out->fill(i->out, line2, color);
233         gfxline_free(line2);
234     } else {
235         if(i->out) i->out->fill(i->out, line, color);
236     }
237 }
238
239 void polyops_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
240 {
241     dbg("polyops_fillbitmap");
242     internal_t*i = (internal_t*)dev->internal;
243     
244     gfxpoly_t*poly = gfxpoly_fillToPoly(line);
245     char ok = 0;
246     gfxline_t*line2 = handle_poly(dev, poly, &ok);
247
248     if(ok) {
249         if(i->out && line2) i->out->fillbitmap(i->out, line2, img, matrix, cxform);
250         gfxline_free(line2);
251     } else {
252         if(i->out) i->out->fillbitmap(i->out, line, img, matrix, cxform);
253     }
254 }
255
256 void polyops_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
257 {
258     dbg("polyops_fillgradient");
259     internal_t*i = (internal_t*)dev->internal;
260     
261     gfxpoly_t*poly = gfxpoly_fillToPoly(line);
262     char ok = 0;
263     gfxline_t*line2 = handle_poly(dev, poly, &ok);
264
265     if(ok) {
266         if(i->out && line2) i->out->fillgradient(i->out, line2, gradient, type, matrix);
267         gfxline_free(line2);
268     } else {
269         if(i->out) i->out->fillgradient(i->out, line, gradient, type, matrix);
270     }
271 }
272
273 void polyops_addfont(struct _gfxdevice*dev, gfxfont_t*font)
274 {
275     dbg("polyops_addfont");
276     internal_t*i = (internal_t*)dev->internal;
277     if(i->out) i->out->addfont(i->out, font);
278 }
279
280 void polyops_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
281 {
282     dbg("polyops_drawchar");
283     if(!font)
284         return;
285     internal_t*i = (internal_t*)dev->internal;
286     gfxline_t*glyph = gfxline_clone(font->glyphs[glyphnr].line);
287     gfxline_transform(glyph, matrix);
288
289     if(i->clip && i->clip->poly) {
290         gfxbbox_t bbox = gfxline_getbbox(glyph);
291         gfxpoly_t*dummybox = gfxpoly_createbox(bbox.xmin,bbox.ymin,bbox.xmax,bbox.ymax);
292
293         char ok=0;
294         gfxline_t*gfxline = handle_poly(dev, dummybox, &ok);
295         if(ok) {
296             gfxbbox_t bbox2 = gfxline_getbbox(gfxline);
297             double w = bbox2.xmax - bbox2.xmin;
298             double h = bbox2.ymax - bbox2.ymin;
299             if(w < 0.001 || h < 0.001) /* character was clipped completely */ {
300             } else if(fabs((bbox.xmax - bbox.xmin) - w) > 0.05 ||
301                       fabs((bbox.ymax - bbox.ymin) - h) > 0.05) {
302                 /* notable change in character size: character was clipped 
303                    TODO: how to deal with diagonal cuts?
304                  */
305                 polyops_fill(dev, glyph, color);
306             } else {
307                 if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
308             }
309         } else {
310             if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
311         }
312     } else {
313         if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
314     }
315     
316     gfxline_free(glyph);
317 }
318
319 void polyops_drawlink(struct _gfxdevice*dev, gfxline_t*line, const char*action)
320 {
321     dbg("polyops_drawlink");
322     internal_t*i = (internal_t*)dev->internal;
323     if(i->out) i->out->drawlink(i->out, line, action);
324 }
325
326 void polyops_endpage(struct _gfxdevice*dev)
327 {
328     dbg("polyops_endpage");
329     internal_t*i = (internal_t*)dev->internal;
330     if(i->out) i->out->endpage(i->out);
331 }
332
333 gfxresult_t* polyops_finish(struct _gfxdevice*dev)
334 {
335     dbg("polyops_finish");
336     internal_t*i = (internal_t*)dev->internal;
337
338     if(i->polyunion) {
339         gfxpoly_free(i->polyunion);i->polyunion=0;
340     }
341     if(i->out) {
342         return i->out->finish(i->out);
343     } else {
344         return 0;
345     }
346 }
347
348 gfxline_t*gfxdevice_union_getunion(struct _gfxdevice*dev)
349 {
350     internal_t*i = (internal_t*)dev->internal;
351     return gfxpoly_to_gfxline(i->polyunion);
352 }
353
354 void gfxdevice_removeclippings_init(gfxdevice_t*dev, gfxdevice_t*out)
355 {
356     dbg("gfxdevice_removeclippings_init");
357     internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
358     memset(dev, 0, sizeof(gfxdevice_t));
359     
360     dev->name = "removeclippings";
361
362     dev->internal = i;
363
364     dev->setparameter = polyops_setparameter;
365     dev->startpage = polyops_startpage;
366     dev->startclip = polyops_startclip;
367     dev->endclip = polyops_endclip;
368     dev->stroke = polyops_stroke;
369     dev->fill = polyops_fill;
370     dev->fillbitmap = polyops_fillbitmap;
371     dev->fillgradient = polyops_fillgradient;
372     dev->addfont = polyops_addfont;
373     dev->drawchar = polyops_drawchar;
374     dev->drawlink = polyops_drawlink;
375     dev->endpage = polyops_endpage;
376     dev->finish = polyops_finish;
377
378     i->out = out;
379     i->polyunion = 0;
380 }
381
382 void gfxdevice_union_init(gfxdevice_t*dev,gfxdevice_t*out)
383 {
384     dbg("gfxdevice_getunion_init");
385     internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
386     memset(dev, 0, sizeof(gfxdevice_t));
387     
388     dev->name = "union";
389
390     dev->internal = i;
391
392     dev->setparameter = polyops_setparameter;
393     dev->startpage = polyops_startpage;
394     dev->startclip = polyops_startclip;
395     dev->endclip = polyops_endclip;
396     dev->stroke = polyops_stroke;
397     dev->fill = polyops_fill;
398     dev->fillbitmap = polyops_fillbitmap;
399     dev->fillgradient = polyops_fillgradient;
400     dev->addfont = polyops_addfont;
401     dev->drawchar = polyops_drawchar;
402     dev->drawlink = polyops_drawlink;
403     dev->endpage = polyops_endpage;
404     dev->finish = polyops_finish;
405
406     i->out = out;
407     i->polyunion = gfxpoly_strokeToPoly(0, 0, gfx_capButt, gfx_joinMiter, 0);
408 }
409