fixed a double free and a mem leak
[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 <string.h>
29 #include <math.h>
30 #include "../mem.h"
31 #include "../gfxdevice.h"
32 #include "../gfxtools.h"
33 #include "../gfxpoly.h"
34 #include "polyops.h"
35
36 typedef struct _clip {
37     gfxpoly_t*poly;
38     struct _clip*next;
39 } clip_t;
40
41 typedef struct _internal {
42     gfxdevice_t*out;
43     clip_t*clip;
44     gfxpoly_t*polyunion;
45 } internal_t;
46
47 static int verbose = 0;
48
49 static void dbg(char*format, ...)
50 {
51     if(!verbose)
52         return;
53     char buf[1024];
54     int l;
55     va_list arglist;
56     va_start(arglist, format);
57     vsprintf(buf, format, arglist);
58     va_end(arglist);
59     l = strlen(buf);
60     while(l && buf[l-1]=='\n') {
61         buf[l-1] = 0;
62         l--;
63     }
64     printf("(device-polyops) %s\n", buf);
65     fflush(stdout);
66 }
67
68 int polyops_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
69 {
70     dbg("polyops_setparameter");
71     internal_t*i = (internal_t*)dev->internal;
72     if(i->out) return i->out->setparameter(i->out,key,value);
73     else return 0;
74 }
75
76 void polyops_startpage(struct _gfxdevice*dev, int width, int height)
77 {
78     dbg("polyops_startpage");
79     internal_t*i = (internal_t*)dev->internal;
80     if(i->out) i->out->startpage(i->out,width,height);
81 }
82
83 void polyops_startclip(struct _gfxdevice*dev, gfxline_t*line)
84 {
85     dbg("polyops_startclip");
86     internal_t*i = (internal_t*)dev->internal;
87
88     gfxpoly_t* poly = gfxpoly_fillToPoly(line);
89
90     if(i->clip) {
91         gfxpoly_t*old = i->clip->poly;
92         clip_t*n = i->clip;
93         i->clip = (clip_t*)rfx_calloc(sizeof(clip_t));
94         i->clip->next = n;
95         i->clip->poly = gfxpoly_intersect(poly, old);
96         gfxpoly_free(poly);
97     } else {
98         i->clip = (clip_t*)rfx_calloc(sizeof(clip_t));
99         i->clip->poly = poly;
100     }
101 }
102
103 void polyops_endclip(struct _gfxdevice*dev)
104 {
105     dbg("polyops_endclip");
106     internal_t*i = (internal_t*)dev->internal;
107
108     if(i->clip) {
109         clip_t*old = i->clip;
110         i->clip = i->clip->next;
111         gfxpoly_free(old->poly);old->poly = 0;
112         old->next = 0;free(old);
113     } else {
114         fprintf(stderr, "Error: endclip without startclip\n");
115     }
116 }
117
118 static void addtounion(struct _gfxdevice*dev, gfxpoly_t*poly)
119 {
120     internal_t*i = (internal_t*)dev->internal;
121     if(i->polyunion) {
122         gfxpoly_t*old = i->polyunion;
123         i->polyunion = gfxpoly_union(poly,i->polyunion);
124         gfxpoly_free(old);
125     }
126 }
127
128 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)
129 {
130     dbg("polyops_stroke");
131     internal_t*i = (internal_t*)dev->internal;
132     //i->out->stroke(i->out, line, width, color, cap_style, joint_style, miterLimit);
133     gfxpoly_t* poly = gfxpoly_strokeToPoly(line, width, cap_style, joint_style, miterLimit);
134     if(i->clip) {
135         gfxpoly_t*old = poly;
136         poly = gfxpoly_intersect(poly, i->clip->poly);
137         gfxpoly_free(old);
138     }
139     addtounion(dev, poly);
140     gfxline_t*gfxline = gfxpoly_to_gfxline(poly);
141     if(i->out) i->out->fill(i->out, gfxline, color);
142     gfxline_free(gfxline);
143     gfxpoly_free(poly);
144 }
145
146 void polyops_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
147 {
148     dbg("polyops_fill");
149     internal_t*i = (internal_t*)dev->internal;
150
151     gfxpoly_t*poly = gfxpoly_fillToPoly(line);
152
153     if(i->clip) {
154         gfxpoly_t*old = poly;
155         poly = gfxpoly_intersect(poly, i->clip->poly);
156         gfxpoly_free(old);
157     }
158     addtounion(dev,poly);
159     gfxline_t*gfxline = gfxpoly_to_gfxline(poly);
160     if(i->out) i->out->fill(i->out, gfxline, color);
161     gfxline_free(gfxline);
162     gfxpoly_free(poly);
163 }
164
165 void polyops_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
166 {
167     dbg("polyops_fillbitmap");
168     internal_t*i = (internal_t*)dev->internal;
169     gfxpoly_t* poly = gfxpoly_fillToPoly(line);
170
171     if(i->clip) {
172         gfxpoly_t*old = poly;
173         poly = gfxpoly_intersect(poly, i->clip->poly);
174         gfxpoly_free(old);
175     }
176     addtounion(dev,poly);
177     gfxline_t*gfxline = gfxpoly_to_gfxline(poly);
178     if(i->out) i->out->fillbitmap(i->out, gfxline, img, matrix, cxform);
179     gfxline_free(gfxline);
180     gfxpoly_free(poly);
181 }
182
183 void polyops_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
184 {
185     dbg("polyops_fillgradient");
186     internal_t*i = (internal_t*)dev->internal;
187     gfxpoly_t* poly  = gfxpoly_fillToPoly(line);
188     if(i->clip) {
189         gfxpoly_t*old = poly;
190         poly  = gfxpoly_intersect(poly, i->clip->poly);
191         gfxpoly_free(old);
192     }
193     addtounion(dev,poly);
194     gfxline_t*gfxline = gfxpoly_to_gfxline(poly);
195     if(i->out) i->out->fillgradient(i->out, gfxline, gradient, type, matrix);
196     gfxline_free(gfxline);
197     gfxpoly_free(poly);
198 }
199
200 void polyops_addfont(struct _gfxdevice*dev, gfxfont_t*font)
201 {
202     dbg("polyops_addfont");
203     internal_t*i = (internal_t*)dev->internal;
204     if(i->out) i->out->addfont(i->out, font);
205 }
206
207 void polyops_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
208 {
209     dbg("polyops_drawchar");
210     if(!font)
211         return;
212     internal_t*i = (internal_t*)dev->internal;
213     gfxline_t*glyph = gfxline_clone(font->glyphs[glyphnr].line);
214     gfxline_transform(glyph, matrix);
215
216     if(i->clip) {
217         gfxbbox_t bbox = gfxline_getbbox(glyph);
218         gfxpoly_t*dummybox = gfxpoly_createbox(bbox.xmin,bbox.ymin,bbox.xmax,bbox.ymax);
219         gfxpoly_t*poly = gfxpoly_intersect(dummybox, i->clip->poly);
220         gfxline_t*gfxline = gfxpoly_to_gfxline(poly);
221         gfxbbox_t bbox2 = gfxline_getbbox(gfxline);
222         double w = bbox2.xmax - bbox2.xmin;
223         double h = bbox2.ymax - bbox2.ymin;
224         
225         addtounion(dev, poly); // TODO: use the whole char, not just the bbox?
226
227         if(w < 0.001 || h < 0.001) /* character was clipped completely */ {
228         } else if(fabs((bbox.xmax - bbox.xmin) - w) > 0.05 ||
229                   fabs((bbox.ymax - bbox.ymin) - h) > 0.05) {
230             /* notable change in character size: character was clipped 
231                TODO: handle diagonal cuts
232              */
233             polyops_fill(dev, glyph, color);
234         } else {
235             if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
236         }
237     } else {
238         if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
239     }
240     
241     gfxline_free(glyph);
242 }
243
244 void polyops_drawlink(struct _gfxdevice*dev, gfxline_t*line, const char*action)
245 {
246     dbg("polyops_drawlink");
247     internal_t*i = (internal_t*)dev->internal;
248     if(i->out) i->out->drawlink(i->out, line, action);
249 }
250
251 void polyops_endpage(struct _gfxdevice*dev)
252 {
253     dbg("polyops_endpage");
254     internal_t*i = (internal_t*)dev->internal;
255     if(i->out) i->out->endpage(i->out);
256 }
257
258 gfxresult_t* polyops_finish(struct _gfxdevice*dev)
259 {
260     dbg("polyops_finish");
261     internal_t*i = (internal_t*)dev->internal;
262
263     if(i->polyunion) {
264         gfxpoly_free(i->polyunion);i->polyunion=0;
265     }
266     if(i->out) {
267         return i->out->finish(i->out);
268     } else {
269         return 0;
270     }
271 }
272
273 gfxline_t*gfxdevice_union_getunion(struct _gfxdevice*dev)
274 {
275     internal_t*i = (internal_t*)dev->internal;
276     return gfxpoly_to_gfxline(i->polyunion);
277 }
278
279 void gfxdevice_removeclippings_init(gfxdevice_t*dev, gfxdevice_t*out)
280 {
281     dbg("gfxdevice_removeclippings_init");
282     internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
283     memset(dev, 0, sizeof(gfxdevice_t));
284     
285     dev->name = "removeclippings";
286
287     dev->internal = i;
288
289     dev->setparameter = polyops_setparameter;
290     dev->startpage = polyops_startpage;
291     dev->startclip = polyops_startclip;
292     dev->endclip = polyops_endclip;
293     dev->stroke = polyops_stroke;
294     dev->fill = polyops_fill;
295     dev->fillbitmap = polyops_fillbitmap;
296     dev->fillgradient = polyops_fillgradient;
297     dev->addfont = polyops_addfont;
298     dev->drawchar = polyops_drawchar;
299     dev->drawlink = polyops_drawlink;
300     dev->endpage = polyops_endpage;
301     dev->finish = polyops_finish;
302
303     i->out = out;
304     i->polyunion = 0;
305 }
306
307 void gfxdevice_union_init(gfxdevice_t*dev,gfxdevice_t*out)
308 {
309     dbg("gfxdevice_getunion_init");
310     internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
311     memset(dev, 0, sizeof(gfxdevice_t));
312     
313     dev->name = "union";
314
315     dev->internal = i;
316
317     dev->setparameter = polyops_setparameter;
318     dev->startpage = polyops_startpage;
319     dev->startclip = polyops_startclip;
320     dev->endclip = polyops_endclip;
321     dev->stroke = polyops_stroke;
322     dev->fill = polyops_fill;
323     dev->fillbitmap = polyops_fillbitmap;
324     dev->fillgradient = polyops_fillgradient;
325     dev->addfont = polyops_addfont;
326     dev->drawchar = polyops_drawchar;
327     dev->drawlink = polyops_drawlink;
328     dev->endpage = polyops_endpage;
329     dev->finish = polyops_finish;
330
331     i->out = out;
332     i->polyunion = gfxpoly_strokeToPoly(0, 0, gfx_capButt, gfx_joinMiter, 0);
333 }
334