9893d1d68a600ad0e38b66a7092affed03a76376
[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     vsnprintf(buf, sizeof(buf)-1, 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_from_fill(line, DEFAULT_GRID);
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 = gfxline_from_gfxpoly(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_destroy(poly);poly=0;
125             currentclip = intersection;
126             type = 0;
127         } else {
128             i->bad_polygons++;
129             gfxline_t*oldclipline = gfxline_from_gfxpoly(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_destroy(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_destroy(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_destroy(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 = gfxline_from_gfxpoly(poly);
200         gfxpoly_destroy(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 = gfxline_from_gfxpoly(i->clip->poly);
209             i->out->startclip(i->out, clipline);
210             gfxline_free(clipline);
211             gfxpoly_destroy(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_from_stroke(line, width, cap_style, joint_style, miterLimit, DEFAULT_GRID);
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_from_fill(line, DEFAULT_GRID);
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_from_fill(line, DEFAULT_GRID);
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_from_fill(line, DEFAULT_GRID);
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, DEFAULT_GRID);
308         gfxline_t*dummybox2 = gfxline_from_gfxpoly(dummybox);
309         bbox = gfxline_getbbox(dummybox2);
310         gfxline_free(dummybox2);
311
312         char ok=0;
313         gfxline_t*gfxline = handle_poly(dev, dummybox, &ok);
314         if(ok) {
315             gfxbbox_t bbox2 = gfxline_getbbox(gfxline);
316             double w = bbox2.xmax - bbox2.xmin;
317             double h = bbox2.ymax - bbox2.ymin;
318             if(w < 0.001 || h < 0.001) /* character was clipped completely */ {
319             } else if(fabs((bbox.xmax - bbox.xmin) - w) > DEFAULT_GRID*2 ||
320                       fabs((bbox.ymax - bbox.ymin) - h) > DEFAULT_GRID*2) {
321                 /* notable change in character size: character was clipped 
322                    TODO: how to deal with diagonal cuts?
323                  */
324                 polyops_fill(dev, glyph, color);
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         gfxline_free(gfxline);
332     } else {
333         if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
334     }
335     
336     gfxline_free(glyph);
337 }
338
339 void polyops_drawlink(struct _gfxdevice*dev, gfxline_t*line, const char*action)
340 {
341     dbg("polyops_drawlink");
342     internal_t*i = (internal_t*)dev->internal;
343     if(i->out) i->out->drawlink(i->out, line, action);
344 }
345
346 void polyops_endpage(struct _gfxdevice*dev)
347 {
348     dbg("polyops_endpage");
349     internal_t*i = (internal_t*)dev->internal;
350     if(i->out) i->out->endpage(i->out);
351 }
352
353 gfxresult_t* polyops_finish(struct _gfxdevice*dev)
354 {
355     dbg("polyops_finish");
356     internal_t*i = (internal_t*)dev->internal;
357
358     if(i->polyunion) {
359         gfxpoly_destroy(i->polyunion);i->polyunion=0;
360     } else {
361         if(i->bad_polygons) {
362             msg("<notice> --flatten success rate: %.1f%% (%d failed polygons)", i->good_polygons*100.0 / (i->good_polygons + i->bad_polygons), i->bad_polygons);
363         }
364     }
365     gfxdevice_t*out = i->out;
366     free(i);memset(dev, 0, sizeof(gfxdevice_t));
367     if(out) {
368         return out->finish(out);
369     } else {
370         return 0;
371     }
372 }
373
374 gfxline_t*gfxdevice_union_getunion(struct _gfxdevice*dev)
375 {
376     internal_t*i = (internal_t*)dev->internal;
377     return gfxline_from_gfxpoly(i->polyunion);
378 }
379
380 void gfxdevice_removeclippings_init(gfxdevice_t*dev, gfxdevice_t*out)
381 {
382     dbg("gfxdevice_removeclippings_init");
383     internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
384     memset(dev, 0, sizeof(gfxdevice_t));
385     
386     dev->name = "removeclippings";
387
388     dev->internal = i;
389
390     dev->setparameter = polyops_setparameter;
391     dev->startpage = polyops_startpage;
392     dev->startclip = polyops_startclip;
393     dev->endclip = polyops_endclip;
394     dev->stroke = polyops_stroke;
395     dev->fill = polyops_fill;
396     dev->fillbitmap = polyops_fillbitmap;
397     dev->fillgradient = polyops_fillgradient;
398     dev->addfont = polyops_addfont;
399     dev->drawchar = polyops_drawchar;
400     dev->drawlink = polyops_drawlink;
401     dev->endpage = polyops_endpage;
402     dev->finish = polyops_finish;
403
404     i->out = out;
405     i->polyunion = 0;
406 }
407
408 void gfxdevice_union_init(gfxdevice_t*dev,gfxdevice_t*out)
409 {
410     dbg("gfxdevice_getunion_init");
411     internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
412     memset(dev, 0, sizeof(gfxdevice_t));
413     
414     dev->name = "union";
415
416     dev->internal = i;
417
418     dev->setparameter = polyops_setparameter;
419     dev->startpage = polyops_startpage;
420     dev->startclip = polyops_startclip;
421     dev->endclip = polyops_endclip;
422     dev->stroke = polyops_stroke;
423     dev->fill = polyops_fill;
424     dev->fillbitmap = polyops_fillbitmap;
425     dev->fillgradient = polyops_fillgradient;
426     dev->addfont = polyops_addfont;
427     dev->drawchar = polyops_drawchar;
428     dev->drawlink = polyops_drawlink;
429     dev->endpage = polyops_endpage;
430     dev->finish = polyops_finish;
431
432     i->out = out;
433     /* create empty polygon */
434     i->polyunion = gfxpoly_from_stroke(0, 0, gfx_capButt, gfx_joinMiter, 0, DEFAULT_GRID);
435 }
436