3 Part of the swftools package.
5 Copyright (c) 2005 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 "../art/libart.h"
35 #include "artsutils.h"
37 typedef struct _clip {
42 typedef struct _internal {
48 static int verbose = 0;
50 static void dbg(char*format, ...)
57 va_start(arglist, format);
58 vsprintf(buf, format, arglist);
61 while(l && buf[l-1]=='\n') {
65 printf("(device-arts) %s\n", buf);
69 int arts_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
71 dbg("arts_setparameter");
72 internal_t*i = (internal_t*)dev->internal;
73 if(i->out) return i->out->setparameter(i->out,key,value);
77 void arts_startpage(struct _gfxdevice*dev, int width, int height)
79 dbg("arts_startpage");
80 internal_t*i = (internal_t*)dev->internal;
81 if(i->out) i->out->startpage(i->out,width,height);
84 void arts_startclip(struct _gfxdevice*dev, gfxline_t*line)
86 dbg("arts_startclip");
87 internal_t*i = (internal_t*)dev->internal;
88 ArtSVP* svp = gfxfillToSVP(line, 1);
90 svp = art_svp_rewind_uncrossed(art_svp_uncross(svp),ART_WIND_RULE_ODDEVEN); /*FIXME*/
93 ArtSVP*old = i->clip->svp;
95 i->clip = (clip_t*)rfx_calloc(sizeof(clip_t));
97 i->clip->svp = art_svp_intersect(svp, old);
100 i->clip = (clip_t*)rfx_calloc(sizeof(clip_t));
105 void arts_endclip(struct _gfxdevice*dev)
108 internal_t*i = (internal_t*)dev->internal;
111 clip_t*old = i->clip;
112 i->clip = i->clip->next;
113 art_svp_free(old->svp);old->svp = 0;
114 old->next = 0;free(old);
116 fprintf(stderr, "Error: endclip without startclip\n");
120 void addtounion(struct _gfxdevice*dev, ArtSVP*svp)
122 internal_t*i = (internal_t*)dev->internal;
124 ArtSVP*old = i->svpunion;
125 i->svpunion = art_svp_union(svp,i->svpunion);
130 void arts_stroke(struct _gfxdevice*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*color, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit)
133 internal_t*i = (internal_t*)dev->internal;
134 //i->out->stroke(i->out, line, width, color, cap_style, joint_style, miterLimit);
135 ArtSVP* svp = gfxstrokeToSVP(line, width, cap_style, joint_style, miterLimit);
138 svp = art_svp_intersect(svp, i->clip->svp);
142 gfxline_t*gfxline = SVPtogfxline(svp);
143 if(i->out) i->out->fill(i->out, gfxline, color);
148 void arts_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
151 internal_t*i = (internal_t*)dev->internal;
152 ArtSVP* svp = gfxfillToSVP(line, 1);
154 if (svp->n_segs > 500)
157 gfxline_t* lineCursor = line;
158 while(lineCursor != NULL)
160 if(lineCursor->type != gfx_moveTo) ++lineParts;
161 lineCursor = lineCursor->next;
163 fprintf(stderr, "arts_fill abandonning shape with %d segments (%d line parts)\n", svp->n_segs, lineParts);
168 svp = art_svp_rewind_uncrossed(art_svp_uncross(svp),ART_WIND_RULE_ODDEVEN); /*FIXME*/
172 svp = art_svp_intersect(svp, i->clip->svp);
176 gfxline_t*gfxline = SVPtogfxline(svp);
177 if(i->out) i->out->fill(i->out, gfxline, color);
182 void arts_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
184 dbg("arts_fillbitmap");
185 internal_t*i = (internal_t*)dev->internal;
186 ArtSVP* svp = gfxfillToSVP(line, 1);
190 svp = art_svp_intersect(svp, i->clip->svp);
194 gfxline_t*gfxline = SVPtogfxline(svp);
195 if(i->out) i->out->fillbitmap(i->out, gfxline, img, matrix, cxform);
200 void arts_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
202 dbg("arts_fillgradient");
203 internal_t*i = (internal_t*)dev->internal;
204 ArtSVP* svp = gfxfillToSVP(line, 1);
207 svp = art_svp_intersect(svp, i->clip->svp);
211 gfxline_t*gfxline = SVPtogfxline(svp);
212 if(i->out) i->out->fillgradient(i->out, gfxline, gradient, type, matrix);
217 void arts_addfont(struct _gfxdevice*dev, gfxfont_t*font)
220 internal_t*i = (internal_t*)dev->internal;
221 if(i->out) i->out->addfont(i->out, font);
224 void arts_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
226 dbg("arts_drawchar");
229 internal_t*i = (internal_t*)dev->internal;
230 gfxline_t*glyph = gfxline_clone(font->glyphs[glyphnr].line);
231 gfxline_transform(glyph, matrix);
234 gfxbbox_t bbox = gfxline_getbbox(glyph);
235 ArtSVP*dummybox = boxToSVP(bbox.xmin,bbox.ymin,bbox.xmax,bbox.ymax);
236 ArtSVP*svp = art_svp_intersect(dummybox, i->clip->svp);
237 gfxline_t*gfxline = SVPtogfxline(svp);
238 gfxbbox_t bbox2 = gfxline_getbbox(gfxline);
239 double w = bbox2.xmax - bbox2.xmin;
240 double h = bbox2.ymax - bbox2.ymin;
242 addtounion(dev, svp); // TODO: use the whole char, not just the bbox
244 if(w < 0.001 || h < 0.001) /* character was clipped completely */ {
245 } else if(fabs((bbox.xmax - bbox.xmin) - w) > 0.05 ||
246 fabs((bbox.ymax - bbox.ymin) - h) > 0.05) {
247 /* notable change in character size: character was clipped
248 TODO: handle diagonal cuts
250 arts_fill(dev, glyph, color);
252 if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
255 if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
261 void arts_drawlink(struct _gfxdevice*dev, gfxline_t*line, const char*action)
263 dbg("arts_drawlink");
264 internal_t*i = (internal_t*)dev->internal;
265 if(i->out) i->out->drawlink(i->out, line, action);
268 void arts_endpage(struct _gfxdevice*dev)
271 internal_t*i = (internal_t*)dev->internal;
272 if(i->out) i->out->endpage(i->out);
275 gfxresult_t* arts_finish(struct _gfxdevice*dev)
278 internal_t*i = (internal_t*)dev->internal;
280 return i->out->finish(i->out);
286 gfxline_t*gfxdevice_union_getunion(struct _gfxdevice*dev)
288 internal_t*i = (internal_t*)dev->internal;
289 return SVPtogfxline(i->svpunion);
292 void gfxdevice_removeclippings_init(gfxdevice_t*dev, gfxdevice_t*out)
294 dbg("gfxdevice_removeclippings_init");
295 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
296 memset(dev, 0, sizeof(gfxdevice_t));
298 dev->name = "removeclippings";
302 dev->setparameter = arts_setparameter;
303 dev->startpage = arts_startpage;
304 dev->startclip = arts_startclip;
305 dev->endclip = arts_endclip;
306 dev->stroke = arts_stroke;
307 dev->fill = arts_fill;
308 dev->fillbitmap = arts_fillbitmap;
309 dev->fillgradient = arts_fillgradient;
310 dev->addfont = arts_addfont;
311 dev->drawchar = arts_drawchar;
312 dev->drawlink = arts_drawlink;
313 dev->endpage = arts_endpage;
314 dev->finish = arts_finish;
320 void gfxdevice_union_init(gfxdevice_t*dev,gfxdevice_t*out)
322 dbg("gfxdevice_getunion_init");
323 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
324 memset(dev, 0, sizeof(gfxdevice_t));
330 dev->setparameter = arts_setparameter;
331 dev->startpage = arts_startpage;
332 dev->startclip = arts_startclip;
333 dev->endclip = arts_endclip;
334 dev->stroke = arts_stroke;
335 dev->fill = arts_fill;
336 dev->fillbitmap = arts_fillbitmap;
337 dev->fillgradient = arts_fillgradient;
338 dev->addfont = arts_addfont;
339 dev->drawchar = arts_drawchar;
340 dev->drawlink = arts_drawlink;
341 dev->endpage = arts_endpage;
342 dev->finish = arts_finish;
345 i->svpunion = gfxstrokeToSVP(0, 0, gfx_capButt, gfx_joinMiter, 0);