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 */
29 #include "../gfxdevice.h"
30 #include "../gfxtools.h"
31 #include "../art/libart.h"
33 #include "artsutils.h"
35 typedef struct _clip {
40 typedef struct _internal {
46 static int verbose = 0;
48 static void dbg(char*format, ...)
55 va_start(arglist, format);
56 vsprintf(buf, format, arglist);
59 while(l && buf[l-1]=='\n') {
63 printf("(device-arts) %s\n", buf);
67 int arts_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
69 dbg("arts_setparameter");
70 internal_t*i = (internal_t*)dev->internal;
71 if(i->out) return i->out->setparameter(i->out,key,value);
75 void arts_startpage(struct _gfxdevice*dev, int width, int height)
77 dbg("arts_startpage");
78 internal_t*i = (internal_t*)dev->internal;
79 if(i->out) i->out->startpage(i->out,width,height);
82 void arts_startclip(struct _gfxdevice*dev, gfxline_t*line)
84 dbg("arts_startclip");
85 internal_t*i = (internal_t*)dev->internal;
86 ArtSVP* svp = gfxfillToSVP(line, 1);
88 svp = art_svp_rewind_uncrossed(art_svp_uncross(svp),ART_WIND_RULE_ODDEVEN); /*FIXME*/
91 ArtSVP*old = i->clip->svp;
93 i->clip = (clip_t*)rfx_calloc(sizeof(clip_t));
95 i->clip->svp = art_svp_intersect(svp, old);
98 i->clip = (clip_t*)rfx_calloc(sizeof(clip_t));
103 void arts_endclip(struct _gfxdevice*dev)
106 internal_t*i = (internal_t*)dev->internal;
109 clip_t*old = i->clip;
110 i->clip = i->clip->next;
111 art_svp_free(old->svp);old->svp = 0;
112 old->next = 0;free(old);
114 fprintf(stderr, "Error: endclip without startclip\n");
118 void addtounion(struct _gfxdevice*dev, ArtSVP*svp)
120 internal_t*i = (internal_t*)dev->internal;
122 ArtSVP*old = i->svpunion;
123 i->svpunion = art_svp_union(svp,i->svpunion);
128 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)
131 internal_t*i = (internal_t*)dev->internal;
132 //i->out->stroke(i->out, line, width, color, cap_style, joint_style, miterLimit);
133 ArtSVP* svp = gfxstrokeToSVP(line, width, cap_style, joint_style, miterLimit);
136 svp = art_svp_intersect(svp, i->clip->svp);
140 gfxline_t*gfxline = SVPtogfxline(svp);
141 if(i->out) i->out->fill(i->out, gfxline, color);
146 void arts_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
149 internal_t*i = (internal_t*)dev->internal;
150 ArtSVP* svp = gfxfillToSVP(line, 1);
152 if (svp->n_segs > 500)
155 gfxline_t* lineCursor = line;
156 while(lineCursor != NULL)
158 if(lineCursor->type != gfx_moveTo) ++lineParts;
159 lineCursor = lineCursor->next;
161 fprintf(stderr, "arts_fill abandonning shape with %d segments (%d line parts)\n", svp->n_segs, lineParts);
166 svp = art_svp_rewind_uncrossed(art_svp_uncross(svp),ART_WIND_RULE_ODDEVEN); /*FIXME*/
170 svp = art_svp_intersect(svp, i->clip->svp);
174 gfxline_t*gfxline = SVPtogfxline(svp);
175 if(i->out) i->out->fill(i->out, gfxline, color);
180 void arts_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
182 dbg("arts_fillbitmap");
183 internal_t*i = (internal_t*)dev->internal;
184 ArtSVP* svp = gfxfillToSVP(line, 1);
188 svp = art_svp_intersect(svp, i->clip->svp);
192 gfxline_t*gfxline = SVPtogfxline(svp);
193 if(i->out) i->out->fillbitmap(i->out, gfxline, img, matrix, cxform);
198 void arts_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
200 dbg("arts_fillgradient");
201 internal_t*i = (internal_t*)dev->internal;
202 ArtSVP* svp = gfxfillToSVP(line, 1);
205 svp = art_svp_intersect(svp, i->clip->svp);
209 gfxline_t*gfxline = SVPtogfxline(svp);
210 if(i->out) i->out->fillgradient(i->out, gfxline, gradient, type, matrix);
215 void arts_addfont(struct _gfxdevice*dev, gfxfont_t*font)
218 internal_t*i = (internal_t*)dev->internal;
219 if(i->out) i->out->addfont(i->out, font);
222 void arts_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
224 dbg("arts_drawchar");
227 internal_t*i = (internal_t*)dev->internal;
228 gfxline_t*glyph = gfxline_clone(font->glyphs[glyphnr].line);
229 gfxline_transform(glyph, matrix);
232 gfxbbox_t bbox = gfxline_getbbox(glyph);
233 ArtSVP*dummybox = boxToSVP(bbox.xmin,bbox.ymin,bbox.xmax,bbox.ymax);
234 ArtSVP*svp = art_svp_intersect(dummybox, i->clip->svp);
235 gfxline_t*gfxline = SVPtogfxline(svp);
236 gfxbbox_t bbox2 = gfxline_getbbox(gfxline);
237 double w = bbox2.xmax - bbox2.xmin;
238 double h = bbox2.ymax - bbox2.ymin;
240 addtounion(dev, svp); // TODO: use the whole char, not just the bbox
242 if(w < 0.001 || h < 0.001) /* character was clipped completely */ {
243 } else if(fabs((bbox.xmax - bbox.xmin) - w) > 0.05 ||
244 fabs((bbox.ymax - bbox.ymin) - h) > 0.05) {
245 /* notable change in character size: character was clipped
246 TODO: handle diagonal cuts
248 arts_fill(dev, glyph, color);
250 if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
253 if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
259 void arts_drawlink(struct _gfxdevice*dev, gfxline_t*line, const char*action)
261 dbg("arts_drawlink");
262 internal_t*i = (internal_t*)dev->internal;
263 if(i->out) i->out->drawlink(i->out, line, action);
266 void arts_endpage(struct _gfxdevice*dev)
269 internal_t*i = (internal_t*)dev->internal;
270 if(i->out) i->out->endpage(i->out);
273 gfxresult_t* arts_finish(struct _gfxdevice*dev)
276 internal_t*i = (internal_t*)dev->internal;
278 return i->out->finish(i->out);
284 gfxline_t*gfxdevice_union_getunion(struct _gfxdevice*dev)
286 internal_t*i = (internal_t*)dev->internal;
287 return SVPtogfxline(i->svpunion);
290 void gfxdevice_removeclippings_init(gfxdevice_t*dev, gfxdevice_t*out)
292 dbg("gfxdevice_removeclippings_init");
293 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
294 memset(dev, 0, sizeof(gfxdevice_t));
296 dev->name = "removeclippings";
300 dev->setparameter = arts_setparameter;
301 dev->startpage = arts_startpage;
302 dev->startclip = arts_startclip;
303 dev->endclip = arts_endclip;
304 dev->stroke = arts_stroke;
305 dev->fill = arts_fill;
306 dev->fillbitmap = arts_fillbitmap;
307 dev->fillgradient = arts_fillgradient;
308 dev->addfont = arts_addfont;
309 dev->drawchar = arts_drawchar;
310 dev->drawlink = arts_drawlink;
311 dev->endpage = arts_endpage;
312 dev->finish = arts_finish;
318 void gfxdevice_union_init(gfxdevice_t*dev,gfxdevice_t*out)
320 dbg("gfxdevice_getunion_init");
321 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
322 memset(dev, 0, sizeof(gfxdevice_t));
328 dev->setparameter = arts_setparameter;
329 dev->startpage = arts_startpage;
330 dev->startclip = arts_startclip;
331 dev->endclip = arts_endclip;
332 dev->stroke = arts_stroke;
333 dev->fill = arts_fill;
334 dev->fillbitmap = arts_fillbitmap;
335 dev->fillgradient = arts_fillgradient;
336 dev->addfont = arts_addfont;
337 dev->drawchar = arts_drawchar;
338 dev->drawlink = arts_drawlink;
339 dev->endpage = arts_endpage;
340 dev->finish = arts_finish;
343 i->svpunion = gfxstrokeToSVP(0, 0, gfx_capButt, gfx_joinMiter, 0);