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 {
50 static int verbose = 0;
52 static void dbg(char*format, ...)
59 va_start(arglist, format);
60 vsprintf(buf, format, arglist);
63 while(l && buf[l-1]=='\n') {
67 printf("(device-polyops) %s\n", buf);
71 int polyops_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
73 dbg("polyops_setparameter");
74 internal_t*i = (internal_t*)dev->internal;
75 if(i->out) return i->out->setparameter(i->out,key,value);
79 void polyops_startpage(struct _gfxdevice*dev, int width, int height)
81 dbg("polyops_startpage");
82 internal_t*i = (internal_t*)dev->internal;
83 if(i->out) i->out->startpage(i->out,width,height);
86 void polyops_startclip(struct _gfxdevice*dev, gfxline_t*line)
88 dbg("polyops_startclip");
89 internal_t*i = (internal_t*)dev->internal;
91 gfxpoly_t* oldclip = i->clip?i->clip->poly:0;
92 gfxpoly_t* poly = gfxpoly_fillToPoly(line);
93 gfxpoly_t* currentclip = 0;
96 /* we can't rely on gfxpoly actually being able to convert
97 a gfxline into a gfxpoly- for polygons which are too
98 complex or just degenerate, this might fail. So handle
99 all the cases where polygon conversion or intersection
101 if(!poly && !oldclip) {
102 i->out->startclip(i->out,line);
105 } else if(!poly && oldclip) {
106 gfxline_t*oldclipline = gfxpoly_to_gfxline(oldclip);
107 i->out->startclip(i->out,oldclipline);
108 i->out->startclip(i->out,line);
111 } else if(poly && oldclip) {
112 gfxpoly_t*intersection = gfxpoly_intersect(poly, oldclip);
114 // this case is what usually happens
115 gfxpoly_free(poly);poly=0;
116 currentclip = intersection;
119 gfxline_t*oldclipline = gfxpoly_to_gfxline(oldclip);
120 i->out->startclip(i->out, oldclipline);
124 } else if(poly && !oldclip) {
130 i->clip = (clip_t*)rfx_calloc(sizeof(clip_t));
132 i->clip->poly = currentclip;
133 i->clip->openclips = type;
136 void polyops_endclip(struct _gfxdevice*dev)
138 dbg("polyops_endclip");
139 internal_t*i = (internal_t*)dev->internal;
142 msg("<error> endclip without startclip (in: polyops)\n");
146 clip_t*old = i->clip;
147 i->clip = i->clip->next;
149 gfxpoly_free(old->poly);old->poly = 0;
152 for(t=0;t<old->openclips;t++)
153 i->out->endclip(i->out);
155 old->next = 0;free(old);
158 static void addtounion(struct _gfxdevice*dev, gfxpoly_t*poly)
160 internal_t*i = (internal_t*)dev->internal;
161 if(poly && i->polyunion) {
162 gfxpoly_t*old = i->polyunion;
163 gfxpoly_t*newpoly = gfxpoly_union(poly,i->polyunion);
164 i->polyunion = newpoly;
169 static gfxline_t* handle_poly(gfxdevice_t*dev, gfxpoly_t*poly, char*ok)
171 internal_t*i = (internal_t*)dev->internal;
172 if(i->clip && i->clip->poly) {
173 gfxpoly_t*old = poly;
175 poly = gfxpoly_intersect(poly, i->clip->poly);
179 addtounion(dev, poly);
180 gfxline_t*gfxline = 0;
182 // this is the case where everything went right
183 gfxline_t*line = gfxpoly_to_gfxline(poly);
188 if(i->clip && i->clip->poly) {
189 /* convert current clipping from a polygon to an
190 actual "startclip" written to the output */
191 assert(i->clip->openclips <= 1);
192 gfxline_t*clipline = gfxpoly_to_gfxline(i->clip->poly);
193 i->out->startclip(i->out, clipline);
194 gfxline_free(clipline);
195 gfxpoly_free(i->clip->poly);i->clip->poly = 0;
196 i->clip->openclips++;
204 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)
206 dbg("polyops_stroke");
207 internal_t*i = (internal_t*)dev->internal;
209 gfxpoly_t* poly = gfxpoly_strokeToPoly(line, width, cap_style, joint_style, miterLimit);
211 gfxline_t*line2 = handle_poly(dev, poly, &ok);
214 if(i->out && line2) i->out->fill(i->out, line2, color);
218 if(i->out) i->out->stroke(i->out, line, width, color, cap_style, joint_style, miterLimit);
222 void polyops_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
225 internal_t*i = (internal_t*)dev->internal;
227 gfxpoly_t*poly = gfxpoly_fillToPoly(line);
229 gfxline_t*line2 = handle_poly(dev, poly, &ok);
232 if(i->out && line2) i->out->fill(i->out, line2, color);
235 if(i->out) i->out->fill(i->out, line, color);
239 void polyops_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
241 dbg("polyops_fillbitmap");
242 internal_t*i = (internal_t*)dev->internal;
244 gfxpoly_t*poly = gfxpoly_fillToPoly(line);
246 gfxline_t*line2 = handle_poly(dev, poly, &ok);
249 if(i->out && line2) i->out->fillbitmap(i->out, line2, img, matrix, cxform);
252 if(i->out) i->out->fillbitmap(i->out, line, img, matrix, cxform);
256 void polyops_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
258 dbg("polyops_fillgradient");
259 internal_t*i = (internal_t*)dev->internal;
261 gfxpoly_t*poly = gfxpoly_fillToPoly(line);
263 gfxline_t*line2 = handle_poly(dev, poly, &ok);
266 if(i->out && line2) i->out->fillgradient(i->out, line2, gradient, type, matrix);
269 if(i->out) i->out->fillgradient(i->out, line, gradient, type, matrix);
273 void polyops_addfont(struct _gfxdevice*dev, gfxfont_t*font)
275 dbg("polyops_addfont");
276 internal_t*i = (internal_t*)dev->internal;
277 if(i->out) i->out->addfont(i->out, font);
280 void polyops_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
282 dbg("polyops_drawchar");
285 internal_t*i = (internal_t*)dev->internal;
286 gfxline_t*glyph = gfxline_clone(font->glyphs[glyphnr].line);
287 gfxline_transform(glyph, matrix);
289 if(i->clip && i->clip->poly) {
290 gfxbbox_t bbox = gfxline_getbbox(glyph);
291 gfxpoly_t*dummybox = gfxpoly_createbox(bbox.xmin,bbox.ymin,bbox.xmax,bbox.ymax);
294 gfxline_t*gfxline = handle_poly(dev, dummybox, &ok);
296 gfxbbox_t bbox2 = gfxline_getbbox(gfxline);
297 double w = bbox2.xmax - bbox2.xmin;
298 double h = bbox2.ymax - bbox2.ymin;
299 if(w < 0.001 || h < 0.001) /* character was clipped completely */ {
300 } else if(fabs((bbox.xmax - bbox.xmin) - w) > 0.05 ||
301 fabs((bbox.ymax - bbox.ymin) - h) > 0.05) {
302 /* notable change in character size: character was clipped
303 TODO: how to deal with diagonal cuts?
305 polyops_fill(dev, glyph, color);
307 if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
310 if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
313 if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
319 void polyops_drawlink(struct _gfxdevice*dev, gfxline_t*line, const char*action)
321 dbg("polyops_drawlink");
322 internal_t*i = (internal_t*)dev->internal;
323 if(i->out) i->out->drawlink(i->out, line, action);
326 void polyops_endpage(struct _gfxdevice*dev)
328 dbg("polyops_endpage");
329 internal_t*i = (internal_t*)dev->internal;
330 if(i->out) i->out->endpage(i->out);
333 gfxresult_t* polyops_finish(struct _gfxdevice*dev)
335 dbg("polyops_finish");
336 internal_t*i = (internal_t*)dev->internal;
339 gfxpoly_free(i->polyunion);i->polyunion=0;
342 return i->out->finish(i->out);
348 gfxline_t*gfxdevice_union_getunion(struct _gfxdevice*dev)
350 internal_t*i = (internal_t*)dev->internal;
351 return gfxpoly_to_gfxline(i->polyunion);
354 void gfxdevice_removeclippings_init(gfxdevice_t*dev, gfxdevice_t*out)
356 dbg("gfxdevice_removeclippings_init");
357 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
358 memset(dev, 0, sizeof(gfxdevice_t));
360 dev->name = "removeclippings";
364 dev->setparameter = polyops_setparameter;
365 dev->startpage = polyops_startpage;
366 dev->startclip = polyops_startclip;
367 dev->endclip = polyops_endclip;
368 dev->stroke = polyops_stroke;
369 dev->fill = polyops_fill;
370 dev->fillbitmap = polyops_fillbitmap;
371 dev->fillgradient = polyops_fillgradient;
372 dev->addfont = polyops_addfont;
373 dev->drawchar = polyops_drawchar;
374 dev->drawlink = polyops_drawlink;
375 dev->endpage = polyops_endpage;
376 dev->finish = polyops_finish;
382 void gfxdevice_union_init(gfxdevice_t*dev,gfxdevice_t*out)
384 dbg("gfxdevice_getunion_init");
385 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
386 memset(dev, 0, sizeof(gfxdevice_t));
392 dev->setparameter = polyops_setparameter;
393 dev->startpage = polyops_startpage;
394 dev->startclip = polyops_startclip;
395 dev->endclip = polyops_endclip;
396 dev->stroke = polyops_stroke;
397 dev->fill = polyops_fill;
398 dev->fillbitmap = polyops_fillbitmap;
399 dev->fillgradient = polyops_fillgradient;
400 dev->addfont = polyops_addfont;
401 dev->drawchar = polyops_drawchar;
402 dev->drawlink = polyops_drawlink;
403 dev->endpage = polyops_endpage;
404 dev->finish = polyops_finish;
407 i->polyunion = gfxpoly_strokeToPoly(0, 0, gfx_capButt, gfx_joinMiter, 0);