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 gfxline_t* handle_poly(gfxdevice_t*dev, gfxpoly_t*poly)
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);
187 if(i->clip && i->clip->poly) {
188 /* convert current clipping from a polygon to an
189 actual "startclip" written to the output */
190 assert(i->clip->openclips <= 1);
191 gfxline_t*clipline = gfxpoly_to_gfxline(i->clip->poly);
192 i->out->startclip(i->out, clipline);
193 gfxline_free(clipline);
194 gfxpoly_free(i->clip->poly);i->clip->poly = 0;
195 i->clip->openclips++;
203 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)
205 dbg("polyops_stroke");
206 internal_t*i = (internal_t*)dev->internal;
208 gfxpoly_t* poly = gfxpoly_strokeToPoly(line, width, cap_style, joint_style, miterLimit);
209 gfxline_t*line2 = handle_poly(dev, poly);
212 if(i->out) i->out->fill(i->out, line2, color);
215 if(i->out) i->out->stroke(i->out, line, width, color, cap_style, joint_style, miterLimit);
219 void polyops_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
222 internal_t*i = (internal_t*)dev->internal;
224 gfxpoly_t*poly = gfxpoly_fillToPoly(line);
225 gfxline_t*line2 = handle_poly(dev, poly);
228 if(i->out) i->out->fill(i->out, line2, color);
231 if(i->out) i->out->fill(i->out, line, color);
235 void polyops_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
237 dbg("polyops_fillbitmap");
238 internal_t*i = (internal_t*)dev->internal;
240 gfxpoly_t*poly = gfxpoly_fillToPoly(line);
241 gfxline_t*line2 = handle_poly(dev, poly);
244 if(i->out) i->out->fillbitmap(i->out, line2, img, matrix, cxform);
247 if(i->out) i->out->fillbitmap(i->out, line, img, matrix, cxform);
251 void polyops_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
253 dbg("polyops_fillgradient");
254 internal_t*i = (internal_t*)dev->internal;
256 gfxpoly_t*poly = gfxpoly_fillToPoly(line);
257 gfxline_t*line2 = handle_poly(dev, poly);
260 if(i->out) i->out->fillgradient(i->out, line2, gradient, type, matrix);
263 if(i->out) i->out->fillgradient(i->out, line, gradient, type, matrix);
267 void polyops_addfont(struct _gfxdevice*dev, gfxfont_t*font)
269 dbg("polyops_addfont");
270 internal_t*i = (internal_t*)dev->internal;
271 if(i->out) i->out->addfont(i->out, font);
274 void polyops_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
276 dbg("polyops_drawchar");
279 internal_t*i = (internal_t*)dev->internal;
280 gfxline_t*glyph = gfxline_clone(font->glyphs[glyphnr].line);
281 gfxline_transform(glyph, matrix);
283 if(i->clip && i->clip->poly) {
284 gfxbbox_t bbox = gfxline_getbbox(glyph);
285 gfxpoly_t*dummybox = gfxpoly_createbox(bbox.xmin,bbox.ymin,bbox.xmax,bbox.ymax);
287 gfxline_t*gfxline = handle_poly(dev, dummybox);
289 gfxbbox_t bbox2 = gfxline_getbbox(gfxline);
290 double w = bbox2.xmax - bbox2.xmin;
291 double h = bbox2.ymax - bbox2.ymin;
292 if(w < 0.001 || h < 0.001) /* character was clipped completely */ {
293 } else if(fabs((bbox.xmax - bbox.xmin) - w) > 0.05 ||
294 fabs((bbox.ymax - bbox.ymin) - h) > 0.05) {
295 /* notable change in character size: character was clipped
296 TODO: how to deal with diagonal cuts?
298 polyops_fill(dev, glyph, color);
300 if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
303 if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
306 if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
312 void polyops_drawlink(struct _gfxdevice*dev, gfxline_t*line, const char*action)
314 dbg("polyops_drawlink");
315 internal_t*i = (internal_t*)dev->internal;
316 if(i->out) i->out->drawlink(i->out, line, action);
319 void polyops_endpage(struct _gfxdevice*dev)
321 dbg("polyops_endpage");
322 internal_t*i = (internal_t*)dev->internal;
323 if(i->out) i->out->endpage(i->out);
326 gfxresult_t* polyops_finish(struct _gfxdevice*dev)
328 dbg("polyops_finish");
329 internal_t*i = (internal_t*)dev->internal;
332 gfxpoly_free(i->polyunion);i->polyunion=0;
335 return i->out->finish(i->out);
341 gfxline_t*gfxdevice_union_getunion(struct _gfxdevice*dev)
343 internal_t*i = (internal_t*)dev->internal;
344 return gfxpoly_to_gfxline(i->polyunion);
347 void gfxdevice_removeclippings_init(gfxdevice_t*dev, gfxdevice_t*out)
349 dbg("gfxdevice_removeclippings_init");
350 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
351 memset(dev, 0, sizeof(gfxdevice_t));
353 dev->name = "removeclippings";
357 dev->setparameter = polyops_setparameter;
358 dev->startpage = polyops_startpage;
359 dev->startclip = polyops_startclip;
360 dev->endclip = polyops_endclip;
361 dev->stroke = polyops_stroke;
362 dev->fill = polyops_fill;
363 dev->fillbitmap = polyops_fillbitmap;
364 dev->fillgradient = polyops_fillgradient;
365 dev->addfont = polyops_addfont;
366 dev->drawchar = polyops_drawchar;
367 dev->drawlink = polyops_drawlink;
368 dev->endpage = polyops_endpage;
369 dev->finish = polyops_finish;
375 void gfxdevice_union_init(gfxdevice_t*dev,gfxdevice_t*out)
377 dbg("gfxdevice_getunion_init");
378 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
379 memset(dev, 0, sizeof(gfxdevice_t));
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;
400 i->polyunion = gfxpoly_strokeToPoly(0, 0, gfx_capButt, gfx_joinMiter, 0);