3 Part of the swftools package.
5 Copyright (c) 2008 Matthias Kramm <kramm@quiss.org>
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.
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.
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 */
32 #include "../gfxdevice.h"
33 #include "../gfxtools.h"
34 #include "../gfxpoly.h"
38 typedef struct _clip {
44 typedef struct _internal {
53 static int verbose = 0;
55 static void dbg(char*format, ...)
62 va_start(arglist, format);
63 vsprintf(buf, format, arglist);
66 while(l && buf[l-1]=='\n') {
70 printf("(device-polyops) %s\n", buf);
74 int polyops_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
76 dbg("polyops_setparameter");
77 internal_t*i = (internal_t*)dev->internal;
78 if(i->out) return i->out->setparameter(i->out,key,value);
82 void polyops_startpage(struct _gfxdevice*dev, int width, int height)
84 dbg("polyops_startpage");
85 internal_t*i = (internal_t*)dev->internal;
86 if(i->out) i->out->startpage(i->out,width,height);
89 void polyops_startclip(struct _gfxdevice*dev, gfxline_t*line)
91 dbg("polyops_startclip");
92 internal_t*i = (internal_t*)dev->internal;
94 gfxpoly_t* oldclip = i->clip?i->clip->poly:0;
95 gfxpoly_t* poly = gfxpoly_fillToPoly(line);
101 gfxpoly_t* currentclip = 0;
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
109 if(!poly && !oldclip) {
110 i->out->startclip(i->out,line);
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);
119 } else if(poly && oldclip) {
120 gfxpoly_t*intersection = gfxpoly_intersect(poly, oldclip);
123 // this case is what usually happens
124 gfxpoly_free(poly);poly=0;
125 currentclip = intersection;
129 gfxline_t*oldclipline = gfxpoly_to_gfxline(oldclip);
130 i->out->startclip(i->out, oldclipline);
134 } else if(poly && !oldclip) {
140 i->clip = (clip_t*)rfx_calloc(sizeof(clip_t));
142 i->clip->poly = currentclip;
143 i->clip->openclips = type;
146 void polyops_endclip(struct _gfxdevice*dev)
148 dbg("polyops_endclip");
149 internal_t*i = (internal_t*)dev->internal;
152 msg("<error> endclip without startclip (in: polyops)\n");
156 clip_t*old = i->clip;
157 i->clip = i->clip->next;
159 gfxpoly_free(old->poly);old->poly = 0;
162 for(t=0;t<old->openclips;t++)
163 i->out->endclip(i->out);
165 old->next = 0;free(old);
168 static void addtounion(struct _gfxdevice*dev, gfxpoly_t*poly)
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;
179 static gfxline_t* handle_poly(gfxdevice_t*dev, gfxpoly_t*poly, char*ok)
181 internal_t*i = (internal_t*)dev->internal;
182 if(i->clip && i->clip->poly) {
183 gfxpoly_t*old = poly;
185 poly = gfxpoly_intersect(poly, i->clip->poly);
195 addtounion(dev, poly);
196 gfxline_t*gfxline = 0;
198 // this is the case where everything went right
199 gfxline_t*line = gfxpoly_to_gfxline(poly);
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++;
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)
222 dbg("polyops_stroke");
223 internal_t*i = (internal_t*)dev->internal;
225 gfxpoly_t* poly = gfxpoly_strokeToPoly(line, width, cap_style, joint_style, miterLimit);
227 gfxline_t*line2 = handle_poly(dev, poly, &ok);
230 if(i->out && line2) i->out->fill(i->out, line2, color);
234 if(i->out) i->out->stroke(i->out, line, width, color, cap_style, joint_style, miterLimit);
238 void polyops_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
241 internal_t*i = (internal_t*)dev->internal;
243 gfxpoly_t*poly = gfxpoly_fillToPoly(line);
245 gfxline_t*line2 = handle_poly(dev, poly, &ok);
248 if(i->out && line2) i->out->fill(i->out, line2, color);
251 if(i->out) i->out->fill(i->out, line, color);
255 void polyops_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
257 dbg("polyops_fillbitmap");
258 internal_t*i = (internal_t*)dev->internal;
260 gfxpoly_t*poly = gfxpoly_fillToPoly(line);
262 gfxline_t*line2 = handle_poly(dev, poly, &ok);
265 if(i->out && line2) i->out->fillbitmap(i->out, line2, img, matrix, cxform);
268 if(i->out) i->out->fillbitmap(i->out, line, img, matrix, cxform);
272 void polyops_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
274 dbg("polyops_fillgradient");
275 internal_t*i = (internal_t*)dev->internal;
277 gfxpoly_t*poly = gfxpoly_fillToPoly(line);
279 gfxline_t*line2 = handle_poly(dev, poly, &ok);
282 if(i->out && line2) i->out->fillgradient(i->out, line2, gradient, type, matrix);
285 if(i->out) i->out->fillgradient(i->out, line, gradient, type, matrix);
289 void polyops_addfont(struct _gfxdevice*dev, gfxfont_t*font)
291 dbg("polyops_addfont");
292 internal_t*i = (internal_t*)dev->internal;
293 if(i->out) i->out->addfont(i->out, font);
296 void polyops_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
298 dbg("polyops_drawchar");
301 internal_t*i = (internal_t*)dev->internal;
302 gfxline_t*glyph = gfxline_clone(font->glyphs[glyphnr].line);
303 gfxline_transform(glyph, matrix);
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);
310 gfxline_t*gfxline = handle_poly(dev, dummybox, &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?
321 polyops_fill(dev, glyph, color);
323 if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
326 if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
329 if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
335 void polyops_drawlink(struct _gfxdevice*dev, gfxline_t*line, const char*action)
337 dbg("polyops_drawlink");
338 internal_t*i = (internal_t*)dev->internal;
339 if(i->out) i->out->drawlink(i->out, line, action);
342 void polyops_endpage(struct _gfxdevice*dev)
344 dbg("polyops_endpage");
345 internal_t*i = (internal_t*)dev->internal;
346 if(i->out) i->out->endpage(i->out);
349 gfxresult_t* polyops_finish(struct _gfxdevice*dev)
351 dbg("polyops_finish");
352 internal_t*i = (internal_t*)dev->internal;
356 gfxpoly_free(i->polyunion);i->polyunion=0;
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);
363 return i->out->finish(i->out);
369 gfxline_t*gfxdevice_union_getunion(struct _gfxdevice*dev)
371 internal_t*i = (internal_t*)dev->internal;
372 return gfxpoly_to_gfxline(i->polyunion);
375 void gfxdevice_removeclippings_init(gfxdevice_t*dev, gfxdevice_t*out)
377 dbg("gfxdevice_removeclippings_init");
378 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
379 memset(dev, 0, sizeof(gfxdevice_t));
381 dev->name = "removeclippings";
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;
403 void gfxdevice_union_init(gfxdevice_t*dev,gfxdevice_t*out)
405 dbg("gfxdevice_getunion_init");
406 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
407 memset(dev, 0, sizeof(gfxdevice_t));
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;
428 i->polyunion = gfxpoly_strokeToPoly(0, 0, gfx_capButt, gfx_joinMiter, 0);