c++ fixes
[swftools.git] / lib / devices / arts.c
1 /* arts.c
2
3    Part of the swftools package.
4
5    Copyright (c) 2005 Matthias Kramm <kramm@quiss.org> 
6  
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.
11
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.
16
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 */
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <unistd.h>
25 #include <memory.h>
26 #include "../mem.h"
27 #include "../gfxdevice.h"
28 #include "../gfxtools.h"
29 #include "../art/libart.h"
30 #include "artsutils.c"
31
32 typedef struct _clip {
33     ArtSVP*svp;
34     struct _clip*next;
35 } clip_t;
36
37 typedef struct _internal {
38     gfxdevice_t*out;
39     clip_t*clip;
40 } internal_t;
41
42 static int verbose = 1;
43 static void dbg(char*format, ...)
44 {
45     if(!verbose)
46         return;
47     char buf[1024];
48     int l;
49     va_list arglist;
50     va_start(arglist, format);
51     vsprintf(buf, format, arglist);
52     va_end(arglist);
53     l = strlen(buf);
54     while(l && buf[l-1]=='\n') {
55         buf[l-1] = 0;
56         l--;
57     }
58     printf("(device-arts) %s\n", buf);
59     fflush(stdout);
60 }
61
62 int arts_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
63 {
64     dbg("arts_setparameter");
65     internal_t*i = (internal_t*)dev->internal;
66     return i->out->setparameter(i->out,key,value);
67 }
68
69 void arts_startpage(struct _gfxdevice*dev, int width, int height)
70 {
71     dbg("arts_startpage");
72     internal_t*i = (internal_t*)dev->internal;
73     i->out->startpage(i->out,width,height);
74 }
75
76 void arts_startclip(struct _gfxdevice*dev, gfxline_t*line)
77 {
78     dbg("arts_startclip");
79     internal_t*i = (internal_t*)dev->internal;
80     ArtSVP* svp = gfxfillToSVP(line, 1);
81
82     svp = art_svp_rewind_uncrossed(art_svp_uncross(svp),ART_WIND_RULE_ODDEVEN); /*FIXME*/
83
84     if(i->clip) {
85         ArtSVP*old = i->clip->svp;
86         clip_t*n = i->clip;
87         i->clip = (clip_t*)rfx_calloc(sizeof(clip_t));
88         i->clip->next = n;
89         i->clip->svp = art_svp_intersect(svp, old);
90         art_svp_free(svp);
91     } else {
92         i->clip = (clip_t*)rfx_calloc(sizeof(clip_t));
93         i->clip->svp = svp;
94     }
95 }
96
97 void arts_endclip(struct _gfxdevice*dev)
98 {
99     dbg("arts_endclip");
100     internal_t*i = (internal_t*)dev->internal;
101
102     if(i->clip) {
103         clip_t*old = i->clip;
104         i->clip = i->clip->next;
105         art_svp_free(old->svp);old->svp = 0;
106         old->next = 0;free(old);
107     } else {
108         fprintf(stderr, "Error: endclip without startclip\n");
109     }
110 }
111
112 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)
113 {
114     dbg("arts_stroke");
115     internal_t*i = (internal_t*)dev->internal;
116     //i->out->stroke(i->out, line, width, color, cap_style, joint_style, miterLimit);
117     ArtSVP* svp = gfxstrokeToSVP(line, width, cap_style, joint_style, miterLimit);
118     if(i->clip) {
119         ArtSVP*old = svp;
120         svp = art_svp_intersect(svp, i->clip->svp);
121         art_svp_free(old);
122     }
123     gfxline_t*gfxline = SVPtogfxline(svp);
124     i->out->fill(i->out, gfxline, color);
125     free(gfxline);
126     art_svp_free(svp);
127 }
128
129 void arts_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
130 {
131     dbg("arts_fill");
132     internal_t*i = (internal_t*)dev->internal;
133     ArtSVP* svp = gfxfillToSVP(line, 1);
134     
135     svp = art_svp_rewind_uncrossed(art_svp_uncross(svp),ART_WIND_RULE_ODDEVEN); /*FIXME*/
136
137     if(i->clip) {
138         ArtSVP*old = svp;
139         svp = art_svp_intersect(svp, i->clip->svp);
140         art_svp_free(old);
141     }
142     gfxline_t*gfxline = SVPtogfxline(svp);
143     i->out->fill(i->out, gfxline, color);
144     free(gfxline);
145     art_svp_free(svp);
146 }
147
148 void arts_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
149 {
150     dbg("arts_fillbitmap");
151     internal_t*i = (internal_t*)dev->internal;
152     ArtSVP* svp = gfxfillToSVP(line, 1);
153     if(i->clip) {
154         ArtSVP*old = svp;
155         svp = art_svp_intersect(svp, i->clip->svp);
156         art_svp_free(old);
157     }
158     gfxline_t*gfxline = SVPtogfxline(svp);
159     i->out->fillbitmap(i->out, gfxline, img, matrix, cxform);
160     free(gfxline);
161     art_svp_free(svp);
162 }
163
164 void arts_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
165 {
166     dbg("arts_fillgradient");
167     internal_t*i = (internal_t*)dev->internal;
168     ArtSVP* svp = gfxfillToSVP(line, 1);
169     if(i->clip) {
170         ArtSVP*old = svp;
171         svp = art_svp_intersect(svp, i->clip->svp);
172         art_svp_free(old);
173     }
174     gfxline_t*gfxline = SVPtogfxline(svp);
175     i->out->fillgradient(i->out, gfxline, gradient, type, matrix);
176     free(gfxline);
177     art_svp_free(svp);
178 }
179
180 void arts_addfont(struct _gfxdevice*dev, gfxfont_t*font)
181 {
182     dbg("arts_addfont");
183     internal_t*i = (internal_t*)dev->internal;
184     i->out->addfont(i->out, font);
185 }
186
187 void arts_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
188 {
189     dbg("arts_drawchar");
190     internal_t*i = (internal_t*)dev->internal;
191     gfxline_t*glyph = gfxline_clone(font->glyphs[glyphnr].line);
192     gfxline_transform(glyph, matrix);
193
194     if(i->clip) {
195         gfxbbox_t bbox = gfxline_getbbox(glyph);
196         ArtSVP*dummybox = boxToSVP(bbox.xmin,bbox.ymin,bbox.xmax,bbox.ymax);
197         ArtSVP*svp = art_svp_intersect(dummybox, i->clip->svp);
198         gfxline_t*gfxline = SVPtogfxline(svp);
199         gfxbbox_t bbox2 = gfxline_getbbox(gfxline);
200         double w = bbox2.xmax - bbox2.xmin;
201         double h = bbox2.ymax - bbox2.ymin;
202         if(w < 0.001 || h < 0.001) /* character was clipped completely */ {
203         } else if(fabs((bbox.xmax - bbox.xmin) - w) > 0.05 ||
204                   fabs((bbox.ymax - bbox.ymin) - h) > 0.05) {
205             /* notable change in character size: character was clipped 
206                TODO: handle diagonal cuts
207              */
208             arts_fill(dev, glyph, color);
209         } else {
210             i->out->drawchar(i->out, font, glyphnr, color, matrix);
211         }
212     } else {
213         i->out->drawchar(i->out, font, glyphnr, color, matrix);
214     }
215     
216     gfxline_free(glyph);
217 }
218
219 void arts_drawlink(struct _gfxdevice*dev, gfxline_t*line, char*action)
220 {
221     dbg("arts_drawlink");
222     internal_t*i = (internal_t*)dev->internal;
223     i->out->drawlink(i->out, line, action);
224 }
225
226 void arts_endpage(struct _gfxdevice*dev)
227 {
228     dbg("arts_endpage");
229     internal_t*i = (internal_t*)dev->internal;
230     i->out->endpage(i->out);
231 }
232
233 gfxresult_t* arts_finish(struct _gfxdevice*dev)
234 {
235     dbg("arts_finish");
236     internal_t*i = (internal_t*)dev->internal;
237     return i->out->finish(i->out);
238 }
239
240 void gfxdevice_arts_init(gfxdevice_t*dev, gfxdevice_t*out)
241 {
242     dbg("gfxdevice_arts_init");
243     internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
244     memset(dev, 0, sizeof(gfxdevice_t));
245     dev->internal = i;
246
247     dev->setparameter = arts_setparameter;
248     dev->startpage = arts_startpage;
249     dev->startclip = arts_startclip;
250     dev->endclip = arts_endclip;
251     dev->stroke = arts_stroke;
252     dev->fill = arts_fill;
253     dev->fillbitmap = arts_fillbitmap;
254     dev->fillgradient = arts_fillgradient;
255     dev->addfont = arts_addfont;
256     dev->drawchar = arts_drawchar;
257     dev->drawlink = arts_drawlink;
258     dev->endpage = arts_endpage;
259     dev->finish = arts_finish;
260
261     i->out = out;
262 }
263