fixed a few bugs in remove_font_transforms filter
[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        UPDATE: this is not needed anymore. The new gfxpoly
110        implementation is stable enough so it always returns
111        a valid result. Still, it's good practice.
112      */
113     if(!poly && !oldclip) {
114         i->out->startclip(i->out,line);
115         currentclip = 0;
116         type = 1;
117     } else if(!poly && oldclip) {
118         gfxline_t*oldclipline = gfxline_from_gfxpoly(oldclip);
119         i->out->startclip(i->out,oldclipline);
120         i->out->startclip(i->out,line);
121         currentclip = 0;
122         type = 2;
123     } else if(poly && oldclip) {
124         gfxpoly_t*intersection = gfxpoly_intersect(poly, oldclip);
125         if(intersection) {
126             i->good_polygons++;
127             // this case is what usually happens 
128             gfxpoly_destroy(poly);poly=0;
129             currentclip = intersection;
130             type = 0;
131         } else {
132             i->bad_polygons++;
133             gfxline_t*oldclipline = gfxline_from_gfxpoly(oldclip);
134             i->out->startclip(i->out, oldclipline);
135             currentclip = poly;
136             type = 1;
137         }
138     } else if(poly && !oldclip) {
139         currentclip = poly;
140         type = 0;
141     }
142
143     clip_t*n = i->clip;
144     i->clip = (clip_t*)rfx_calloc(sizeof(clip_t));
145     i->clip->next = n;
146     i->clip->poly = currentclip;
147     i->clip->openclips = type;
148 }
149
150 void polyops_endclip(struct _gfxdevice*dev)
151 {
152     dbg("polyops_endclip");
153     internal_t*i = (internal_t*)dev->internal;
154
155     if(!i->clip) {
156         msg("<error> endclip without startclip (in: polyops)\n");
157         return;
158     }
159
160     clip_t*old = i->clip;
161     i->clip = i->clip->next;
162     if(old->poly) {
163         gfxpoly_destroy(old->poly);old->poly = 0;
164     }
165     int t;
166     for(t=0;t<old->openclips;t++)
167         i->out->endclip(i->out);
168
169     old->next = 0;free(old);
170 }
171
172 static void addtounion(struct _gfxdevice*dev, gfxpoly_t*poly)
173 {
174     internal_t*i = (internal_t*)dev->internal;
175     if(poly && i->polyunion) {
176         gfxpoly_t*old = i->polyunion;
177         gfxpoly_t*newpoly = gfxpoly_union(poly,i->polyunion);
178         i->polyunion = newpoly;
179         gfxpoly_destroy(old);
180     }
181 }
182
183 static gfxline_t* handle_poly(gfxdevice_t*dev, gfxpoly_t*poly, char*ok)
184 {
185     internal_t*i = (internal_t*)dev->internal;
186     if(i->clip && i->clip->poly) {
187         gfxpoly_t*old = poly;
188         if(poly) {
189             poly = gfxpoly_intersect(poly, i->clip->poly);
190             gfxpoly_destroy(old);
191         }
192     }
193
194     if(poly) 
195         i->good_polygons++;
196     else
197         i->bad_polygons++;
198
199     addtounion(dev, poly);
200     gfxline_t*gfxline = 0;
201     if(poly) {
202         // this is the case where everything went right
203         gfxline_t*line = gfxline_from_gfxpoly(poly);
204         gfxpoly_destroy(poly);
205         *ok = 1;
206         return line;
207     } else {
208         if(i->clip && i->clip->poly) {
209             /* convert current clipping from a polygon to an
210                actual "startclip" written to the output */
211             assert(i->clip->openclips <= 1);
212             gfxline_t*clipline = gfxline_from_gfxpoly(i->clip->poly);
213             i->out->startclip(i->out, clipline);
214             gfxline_free(clipline);
215             gfxpoly_destroy(i->clip->poly);i->clip->poly = 0;
216             i->clip->openclips++;
217             return 0;
218         } else {
219             return 0;
220         }
221     }
222 }
223
224 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)
225 {
226     dbg("polyops_stroke");
227     internal_t*i = (internal_t*)dev->internal;
228
229     gfxpoly_t* poly = gfxpoly_from_stroke(line, width, cap_style, joint_style, miterLimit, DEFAULT_GRID);
230     char ok = 0;
231     gfxline_t*line2 = handle_poly(dev, poly, &ok);
232
233     if(ok) {
234         if(i->out && line2) i->out->fill(i->out, line2, color);
235         gfxline_free(line2);
236     } else {
237         msg("<error> ..");
238         if(i->out) i->out->stroke(i->out, line, width, color, cap_style, joint_style, miterLimit);
239     }
240 }
241
242 void polyops_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
243 {
244     dbg("polyops_fill");
245     internal_t*i = (internal_t*)dev->internal;
246
247     gfxpoly_t*poly = gfxpoly_from_fill(line, DEFAULT_GRID);
248     char ok = 0;
249     gfxline_t*line2 = handle_poly(dev, poly, &ok);
250
251     if(ok) {
252         if(i->out && line2) i->out->fill(i->out, line2, color);
253         gfxline_free(line2);
254     } else {
255         if(i->out) i->out->fill(i->out, line, color);
256     }
257 }
258
259 void polyops_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
260 {
261     dbg("polyops_fillbitmap");
262     internal_t*i = (internal_t*)dev->internal;
263     
264     gfxpoly_t*poly = gfxpoly_from_fill(line, DEFAULT_GRID);
265     char ok = 0;
266     gfxline_t*line2 = handle_poly(dev, poly, &ok);
267
268     if(ok) {
269         if(i->out && line2) i->out->fillbitmap(i->out, line2, img, matrix, cxform);
270         gfxline_free(line2);
271     } else {
272         if(i->out) i->out->fillbitmap(i->out, line, img, matrix, cxform);
273     }
274 }
275
276 void polyops_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
277 {
278     dbg("polyops_fillgradient");
279     internal_t*i = (internal_t*)dev->internal;
280     
281     gfxpoly_t*poly = gfxpoly_from_fill(line, DEFAULT_GRID);
282     char ok = 0;
283     gfxline_t*line2 = handle_poly(dev, poly, &ok);
284
285     if(ok) {
286         if(i->out && line2) i->out->fillgradient(i->out, line2, gradient, type, matrix);
287         gfxline_free(line2);
288     } else {
289         if(i->out) i->out->fillgradient(i->out, line, gradient, type, matrix);
290     }
291 }
292
293 void polyops_addfont(struct _gfxdevice*dev, gfxfont_t*font)
294 {
295     dbg("polyops_addfont");
296     internal_t*i = (internal_t*)dev->internal;
297     if(i->out) i->out->addfont(i->out, font);
298 }
299
300 void polyops_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
301 {
302     dbg("polyops_drawchar");
303     if(!font)
304         return;
305     internal_t*i = (internal_t*)dev->internal;
306     gfxline_t*glyph = gfxline_clone(font->glyphs[glyphnr].line);
307     gfxline_transform(glyph, matrix);
308
309     if(i->clip && i->clip->poly) {
310         gfxbbox_t bbox = gfxline_getbbox(glyph);
311         gfxpoly_t*dummybox = gfxpoly_createbox(bbox.xmin,bbox.ymin,bbox.xmax,bbox.ymax, DEFAULT_GRID);
312         gfxline_t*dummybox2 = gfxline_from_gfxpoly(dummybox);
313         bbox = gfxline_getbbox(dummybox2);
314         gfxline_free(dummybox2);
315
316         char ok=0;
317         gfxline_t*gfxline = handle_poly(dev, dummybox, &ok);
318         if(ok) {
319             gfxbbox_t bbox2 = gfxline_getbbox(gfxline);
320             double w = bbox2.xmax - bbox2.xmin;
321             double h = bbox2.ymax - bbox2.ymin;
322             if(fabs((bbox.xmax - bbox.xmin) - w) > DEFAULT_GRID*2 ||
323                fabs((bbox.ymax - bbox.ymin) - h) > DEFAULT_GRID*2) {
324                 /* notable change in character size: character was clipped 
325                    TODO: how to deal with diagonal cuts?
326                  */
327                 msg("<trace> Character %d was clipped: (%f,%f,%f,%f) -> (%f,%f,%f,%f)",
328                         glyphnr, 
329                         bbox.xmin,bbox.ymin,bbox.xmax,bbox.ymax,
330                         bbox2.xmin,bbox2.ymin,bbox2.xmax,bbox2.ymax);
331                 polyops_fill(dev, glyph, color);
332             } else {
333                 if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
334             }
335         } else {
336             if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
337         }
338         gfxline_free(gfxline);
339     } else {
340         if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
341     }
342     
343     gfxline_free(glyph);
344 }
345
346 void polyops_drawlink(struct _gfxdevice*dev, gfxline_t*line, const char*action)
347 {
348     dbg("polyops_drawlink");
349     internal_t*i = (internal_t*)dev->internal;
350     if(i->out) i->out->drawlink(i->out, line, action);
351 }
352
353 void polyops_endpage(struct _gfxdevice*dev)
354 {
355     dbg("polyops_endpage");
356     internal_t*i = (internal_t*)dev->internal;
357     if(i->out) i->out->endpage(i->out);
358 }
359
360 gfxresult_t* polyops_finish(struct _gfxdevice*dev)
361 {
362     dbg("polyops_finish");
363     internal_t*i = (internal_t*)dev->internal;
364
365     if(i->polyunion) {
366         gfxpoly_destroy(i->polyunion);i->polyunion=0;
367     } else {
368         if(i->bad_polygons) {
369             msg("<notice> --flatten success rate: %.1f%% (%d failed polygons)", i->good_polygons*100.0 / (i->good_polygons + i->bad_polygons), i->bad_polygons);
370         }
371     }
372     gfxdevice_t*out = i->out;
373     free(i);memset(dev, 0, sizeof(gfxdevice_t));
374     if(out) {
375         return out->finish(out);
376     } else {
377         return 0;
378     }
379 }
380
381 gfxline_t*gfxdevice_union_getunion(struct _gfxdevice*dev)
382 {
383     internal_t*i = (internal_t*)dev->internal;
384     return gfxline_from_gfxpoly(i->polyunion);
385 }
386
387 void gfxdevice_removeclippings_init(gfxdevice_t*dev, gfxdevice_t*out)
388 {
389     dbg("gfxdevice_removeclippings_init");
390     internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
391     memset(dev, 0, sizeof(gfxdevice_t));
392     
393     dev->name = "removeclippings";
394
395     dev->internal = i;
396
397     dev->setparameter = polyops_setparameter;
398     dev->startpage = polyops_startpage;
399     dev->startclip = polyops_startclip;
400     dev->endclip = polyops_endclip;
401     dev->stroke = polyops_stroke;
402     dev->fill = polyops_fill;
403     dev->fillbitmap = polyops_fillbitmap;
404     dev->fillgradient = polyops_fillgradient;
405     dev->addfont = polyops_addfont;
406     dev->drawchar = polyops_drawchar;
407     dev->drawlink = polyops_drawlink;
408     dev->endpage = polyops_endpage;
409     dev->finish = polyops_finish;
410
411     i->out = out;
412     i->polyunion = 0;
413 }
414
415 void gfxdevice_union_init(gfxdevice_t*dev,gfxdevice_t*out)
416 {
417     dbg("gfxdevice_getunion_init");
418     internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
419     memset(dev, 0, sizeof(gfxdevice_t));
420     
421     dev->name = "union";
422
423     dev->internal = i;
424
425     dev->setparameter = polyops_setparameter;
426     dev->startpage = polyops_startpage;
427     dev->startclip = polyops_startclip;
428     dev->endclip = polyops_endclip;
429     dev->stroke = polyops_stroke;
430     dev->fill = polyops_fill;
431     dev->fillbitmap = polyops_fillbitmap;
432     dev->fillgradient = polyops_fillgradient;
433     dev->addfont = polyops_addfont;
434     dev->drawchar = polyops_drawchar;
435     dev->drawlink = polyops_drawlink;
436     dev->endpage = polyops_endpage;
437     dev->finish = polyops_finish;
438
439     i->out = out;
440     /* create empty polygon */
441     i->polyunion = gfxpoly_from_stroke(0, 0, gfx_capButt, gfx_joinMiter, 0, DEFAULT_GRID);
442 }
443