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 */
31 #include "../gfxdevice.h"
32 #include "../gfxtools.h"
33 #include "../gfxpoly.h"
36 typedef struct _clip {
41 typedef struct _internal {
47 static int verbose = 0;
49 static void dbg(char*format, ...)
56 va_start(arglist, format);
57 vsprintf(buf, format, arglist);
60 while(l && buf[l-1]=='\n') {
64 printf("(device-polyops) %s\n", buf);
68 int polyops_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
70 dbg("polyops_setparameter");
71 internal_t*i = (internal_t*)dev->internal;
72 if(i->out) return i->out->setparameter(i->out,key,value);
76 void polyops_startpage(struct _gfxdevice*dev, int width, int height)
78 dbg("polyops_startpage");
79 internal_t*i = (internal_t*)dev->internal;
80 if(i->out) i->out->startpage(i->out,width,height);
83 void polyops_startclip(struct _gfxdevice*dev, gfxline_t*line)
85 dbg("polyops_startclip");
86 internal_t*i = (internal_t*)dev->internal;
88 gfxpoly_t* poly = gfxpoly_fillToPoly(line);
91 gfxpoly_t*old = i->clip->poly;
93 i->clip = (clip_t*)rfx_calloc(sizeof(clip_t));
95 i->clip->poly = gfxpoly_intersect(poly, old);
98 i->clip = (clip_t*)rfx_calloc(sizeof(clip_t));
103 void polyops_endclip(struct _gfxdevice*dev)
105 dbg("polyops_endclip");
106 internal_t*i = (internal_t*)dev->internal;
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);
114 fprintf(stderr, "Error: endclip without startclip\n");
118 static void addtounion(struct _gfxdevice*dev, gfxpoly_t*poly)
120 internal_t*i = (internal_t*)dev->internal;
122 gfxpoly_t*old = i->polyunion;
123 i->polyunion = gfxpoly_union(poly,i->polyunion);
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)
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);
135 gfxpoly_t*old = poly;
136 poly = gfxpoly_intersect(poly, i->clip->poly);
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);
146 void polyops_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
149 internal_t*i = (internal_t*)dev->internal;
151 gfxpoly_t*poly = gfxpoly_fillToPoly(line);
154 gfxpoly_t*old = poly;
155 poly = gfxpoly_intersect(poly, i->clip->poly);
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);
165 void polyops_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
167 dbg("polyops_fillbitmap");
168 internal_t*i = (internal_t*)dev->internal;
169 gfxpoly_t* poly = gfxpoly_fillToPoly(line);
172 gfxpoly_t*old = poly;
173 poly = gfxpoly_intersect(poly, i->clip->poly);
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);
183 void polyops_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
185 dbg("polyops_fillgradient");
186 internal_t*i = (internal_t*)dev->internal;
187 gfxpoly_t* poly = gfxpoly_fillToPoly(line);
189 gfxpoly_t*old = poly;
190 poly = gfxpoly_intersect(poly, i->clip->poly);
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);
200 void polyops_addfont(struct _gfxdevice*dev, gfxfont_t*font)
202 dbg("polyops_addfont");
203 internal_t*i = (internal_t*)dev->internal;
204 if(i->out) i->out->addfont(i->out, font);
207 void polyops_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
209 dbg("polyops_drawchar");
212 internal_t*i = (internal_t*)dev->internal;
213 gfxline_t*glyph = gfxline_clone(font->glyphs[glyphnr].line);
214 gfxline_transform(glyph, matrix);
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;
225 addtounion(dev, poly); // TODO: use the whole char, not just the bbox?
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
233 polyops_fill(dev, glyph, color);
235 if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
238 if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
244 void polyops_drawlink(struct _gfxdevice*dev, gfxline_t*line, const char*action)
246 dbg("polyops_drawlink");
247 internal_t*i = (internal_t*)dev->internal;
248 if(i->out) i->out->drawlink(i->out, line, action);
251 void polyops_endpage(struct _gfxdevice*dev)
253 dbg("polyops_endpage");
254 internal_t*i = (internal_t*)dev->internal;
255 if(i->out) i->out->endpage(i->out);
258 gfxresult_t* polyops_finish(struct _gfxdevice*dev)
260 dbg("polyops_finish");
261 internal_t*i = (internal_t*)dev->internal;
263 return i->out->finish(i->out);
269 gfxline_t*gfxdevice_union_getunion(struct _gfxdevice*dev)
271 internal_t*i = (internal_t*)dev->internal;
272 return gfxpoly_to_gfxline(i->polyunion);
275 void gfxdevice_removeclippings_init(gfxdevice_t*dev, gfxdevice_t*out)
277 dbg("gfxdevice_removeclippings_init");
278 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
279 memset(dev, 0, sizeof(gfxdevice_t));
281 dev->name = "removeclippings";
285 dev->setparameter = polyops_setparameter;
286 dev->startpage = polyops_startpage;
287 dev->startclip = polyops_startclip;
288 dev->endclip = polyops_endclip;
289 dev->stroke = polyops_stroke;
290 dev->fill = polyops_fill;
291 dev->fillbitmap = polyops_fillbitmap;
292 dev->fillgradient = polyops_fillgradient;
293 dev->addfont = polyops_addfont;
294 dev->drawchar = polyops_drawchar;
295 dev->drawlink = polyops_drawlink;
296 dev->endpage = polyops_endpage;
297 dev->finish = polyops_finish;
303 void gfxdevice_union_init(gfxdevice_t*dev,gfxdevice_t*out)
305 dbg("gfxdevice_getunion_init");
306 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
307 memset(dev, 0, sizeof(gfxdevice_t));
313 dev->setparameter = polyops_setparameter;
314 dev->startpage = polyops_startpage;
315 dev->startclip = polyops_startclip;
316 dev->endclip = polyops_endclip;
317 dev->stroke = polyops_stroke;
318 dev->fill = polyops_fill;
319 dev->fillbitmap = polyops_fillbitmap;
320 dev->fillgradient = polyops_fillgradient;
321 dev->addfont = polyops_addfont;
322 dev->drawchar = polyops_drawchar;
323 dev->drawlink = polyops_drawlink;
324 dev->endpage = polyops_endpage;
325 dev->finish = polyops_finish;
328 i->polyunion = gfxpoly_strokeToPoly(0, 0, gfx_capButt, gfx_joinMiter, 0);