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 vsnprintf(buf, sizeof(buf)-1, 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_from_fill(line, DEFAULT_GRID);
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 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.
113 if(!poly && !oldclip) {
114 i->out->startclip(i->out,line);
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);
123 } else if(poly && oldclip) {
124 gfxpoly_t*intersection = gfxpoly_intersect(poly, oldclip);
127 // this case is what usually happens
128 gfxpoly_destroy(poly);poly=0;
129 currentclip = intersection;
133 gfxline_t*oldclipline = gfxline_from_gfxpoly(oldclip);
134 i->out->startclip(i->out, oldclipline);
138 } else if(poly && !oldclip) {
144 i->clip = (clip_t*)rfx_calloc(sizeof(clip_t));
146 i->clip->poly = currentclip;
147 i->clip->openclips = type;
150 void polyops_endclip(struct _gfxdevice*dev)
152 dbg("polyops_endclip");
153 internal_t*i = (internal_t*)dev->internal;
156 msg("<error> endclip without startclip (in: polyops)\n");
160 clip_t*old = i->clip;
161 i->clip = i->clip->next;
163 gfxpoly_destroy(old->poly);old->poly = 0;
166 for(t=0;t<old->openclips;t++)
167 i->out->endclip(i->out);
169 old->next = 0;free(old);
172 static void addtounion(struct _gfxdevice*dev, gfxpoly_t*poly)
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);
183 static gfxline_t* handle_poly(gfxdevice_t*dev, gfxpoly_t*poly, char*ok)
185 internal_t*i = (internal_t*)dev->internal;
186 if(i->clip && i->clip->poly) {
187 gfxpoly_t*old = poly;
189 poly = gfxpoly_intersect(poly, i->clip->poly);
190 gfxpoly_destroy(old);
199 addtounion(dev, poly);
200 gfxline_t*gfxline = 0;
202 // this is the case where everything went right
203 gfxline_t*line = gfxline_from_gfxpoly(poly);
204 gfxpoly_destroy(poly);
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++;
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)
226 dbg("polyops_stroke");
227 internal_t*i = (internal_t*)dev->internal;
229 gfxpoly_t* poly = gfxpoly_from_stroke(line, width, cap_style, joint_style, miterLimit, DEFAULT_GRID);
231 gfxline_t*line2 = handle_poly(dev, poly, &ok);
234 if(i->out && line2) i->out->fill(i->out, line2, color);
238 if(i->out) i->out->stroke(i->out, line, width, color, cap_style, joint_style, miterLimit);
242 void polyops_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
245 internal_t*i = (internal_t*)dev->internal;
247 gfxpoly_t*poly = gfxpoly_from_fill(line, DEFAULT_GRID);
249 gfxline_t*line2 = handle_poly(dev, poly, &ok);
252 if(i->out && line2) i->out->fill(i->out, line2, color);
255 if(i->out) i->out->fill(i->out, line, color);
259 void polyops_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
261 dbg("polyops_fillbitmap");
262 internal_t*i = (internal_t*)dev->internal;
264 gfxpoly_t*poly = gfxpoly_from_fill(line, DEFAULT_GRID);
266 gfxline_t*line2 = handle_poly(dev, poly, &ok);
269 if(i->out && line2) i->out->fillbitmap(i->out, line2, img, matrix, cxform);
272 if(i->out) i->out->fillbitmap(i->out, line, img, matrix, cxform);
276 void polyops_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
278 dbg("polyops_fillgradient");
279 internal_t*i = (internal_t*)dev->internal;
281 gfxpoly_t*poly = gfxpoly_from_fill(line, DEFAULT_GRID);
283 gfxline_t*line2 = handle_poly(dev, poly, &ok);
286 if(i->out && line2) i->out->fillgradient(i->out, line2, gradient, type, matrix);
289 if(i->out) i->out->fillgradient(i->out, line, gradient, type, matrix);
293 void polyops_addfont(struct _gfxdevice*dev, gfxfont_t*font)
295 dbg("polyops_addfont");
296 internal_t*i = (internal_t*)dev->internal;
297 if(i->out) i->out->addfont(i->out, font);
300 void polyops_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
302 dbg("polyops_drawchar");
305 internal_t*i = (internal_t*)dev->internal;
306 gfxline_t*glyph = gfxline_clone(font->glyphs[glyphnr].line);
307 gfxline_transform(glyph, matrix);
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);
317 gfxline_t*gfxline = handle_poly(dev, dummybox, &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?
327 msg("<trace> Character %d was clipped: (%f,%f,%f,%f) -> (%f,%f,%f,%f)",
329 bbox.xmin,bbox.ymin,bbox.xmax,bbox.ymax,
330 bbox2.xmin,bbox2.ymin,bbox2.xmax,bbox2.ymax);
331 polyops_fill(dev, glyph, color);
333 if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
336 if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
338 gfxline_free(gfxline);
340 if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
346 void polyops_drawlink(struct _gfxdevice*dev, gfxline_t*line, const char*action)
348 dbg("polyops_drawlink");
349 internal_t*i = (internal_t*)dev->internal;
350 if(i->out) i->out->drawlink(i->out, line, action);
353 void polyops_endpage(struct _gfxdevice*dev)
355 dbg("polyops_endpage");
356 internal_t*i = (internal_t*)dev->internal;
357 if(i->out) i->out->endpage(i->out);
360 gfxresult_t* polyops_finish(struct _gfxdevice*dev)
362 dbg("polyops_finish");
363 internal_t*i = (internal_t*)dev->internal;
366 gfxpoly_destroy(i->polyunion);i->polyunion=0;
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);
372 gfxdevice_t*out = i->out;
373 free(i);memset(dev, 0, sizeof(gfxdevice_t));
375 return out->finish(out);
381 gfxline_t*gfxdevice_union_getunion(struct _gfxdevice*dev)
383 internal_t*i = (internal_t*)dev->internal;
384 return gfxline_from_gfxpoly(i->polyunion);
387 void gfxdevice_removeclippings_init(gfxdevice_t*dev, gfxdevice_t*out)
389 dbg("gfxdevice_removeclippings_init");
390 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
391 memset(dev, 0, sizeof(gfxdevice_t));
393 dev->name = "removeclippings";
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;
415 void gfxdevice_union_init(gfxdevice_t*dev,gfxdevice_t*out)
417 dbg("gfxdevice_getunion_init");
418 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
419 memset(dev, 0, sizeof(gfxdevice_t));
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;
440 /* create empty polygon */
441 i->polyunion = gfxpoly_from_stroke(0, 0, gfx_capButt, gfx_joinMiter, 0, DEFAULT_GRID);