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