initial revision
[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 <unistd.h>
24 #include <memory.h>
25 #include "../mem.h"
26 #include "../gfxdevice.h"
27 #include "../gfxtools.h"
28 #include "../art/libart.h"
29 #include "artsutils.c"
30
31 typedef struct _clip {
32     ArtSVP*svp;
33     struct _clip*next;
34 } clip_t;
35
36 typedef struct _internal {
37     gfxdevice_t*out;
38     clip_t*clip;
39 } internal_t;
40
41 int arts_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
42 {
43     internal_t*i = (internal_t*)dev->internal;
44     return i->out->setparameter(i->out,key,value);
45 }
46
47 void arts_startpage(struct _gfxdevice*dev, int width, int height)
48 {
49     internal_t*i = (internal_t*)dev->internal;
50     i->out->startpage(i->out,width,height);
51 }
52
53 void arts_startclip(struct _gfxdevice*dev, gfxline_t*line)
54 {
55     internal_t*i = (internal_t*)dev->internal;
56     ArtSVP* svp = gfxfillToSVP(line, 1);
57
58     svp = art_svp_rewind_uncrossed(art_svp_uncross(svp),ART_WIND_RULE_ODDEVEN); /*FIXME*/
59
60     if(i->clip) {
61         ArtSVP*old = i->clip->svp;
62         clip_t*n = i->clip;
63         i->clip = (clip_t*)rfx_calloc(sizeof(clip_t));
64         i->clip->next = n;
65         i->clip->svp = art_svp_intersect(svp, old);
66         art_svp_free(svp);
67     } else {
68         i->clip = rfx_calloc(sizeof(clip_t));
69         i->clip->svp = svp;
70     }
71 }
72
73 void arts_endclip(struct _gfxdevice*dev)
74 {
75     internal_t*i = (internal_t*)dev->internal;
76
77     if(i->clip) {
78         clip_t*old = i->clip;
79         i->clip = i->clip->next;
80         art_svp_free(old->svp);old->svp = 0;
81         old->next = 0;free(old);
82     } else {
83         fprintf(stderr, "Error: endclip without startclip\n");
84     }
85 }
86
87 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)
88 {
89     internal_t*i = (internal_t*)dev->internal;
90     //i->out->stroke(i->out, line, width, color, cap_style, joint_style, miterLimit);
91     ArtSVP* svp = gfxstrokeToSVP(line, width, cap_style, joint_style, miterLimit);
92     if(i->clip) {
93         ArtSVP*old = svp;
94         svp = art_svp_intersect(svp, i->clip->svp);
95         art_svp_free(old);
96     }
97     gfxline_t*gfxline = SVPtogfxline(svp);
98     i->out->fill(i->out, gfxline, color);
99     free(gfxline);
100     art_svp_free(svp);
101 }
102
103 void arts_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
104 {
105     internal_t*i = (internal_t*)dev->internal;
106     ArtSVP* svp = gfxfillToSVP(line, 1);
107     
108     svp = art_svp_rewind_uncrossed(art_svp_uncross(svp),ART_WIND_RULE_ODDEVEN); /*FIXME*/
109
110     if(i->clip) {
111         ArtSVP*old = svp;
112         svp = art_svp_intersect(svp, i->clip->svp);
113         art_svp_free(old);
114     }
115     gfxline_t*gfxline = SVPtogfxline(svp);
116     i->out->fill(i->out, gfxline, color);
117     free(gfxline);
118     art_svp_free(svp);
119 }
120
121 void arts_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
122 {
123     internal_t*i = (internal_t*)dev->internal;
124     ArtSVP* svp = gfxfillToSVP(line, 1);
125     if(i->clip) {
126         ArtSVP*old = svp;
127         svp = art_svp_intersect(svp, i->clip->svp);
128         art_svp_free(old);
129     }
130     gfxline_t*gfxline = SVPtogfxline(svp);
131     i->out->fillbitmap(i->out, gfxline, img, matrix, cxform);
132     free(gfxline);
133     art_svp_free(svp);
134 }
135
136 void arts_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
137 {
138     internal_t*i = (internal_t*)dev->internal;
139     ArtSVP* svp = gfxfillToSVP(line, 1);
140     if(i->clip) {
141         ArtSVP*old = svp;
142         svp = art_svp_intersect(svp, i->clip->svp);
143         art_svp_free(old);
144     }
145     gfxline_t*gfxline = SVPtogfxline(svp);
146     i->out->fillgradient(i->out, gfxline, gradient, type, matrix);
147     free(gfxline);
148     art_svp_free(svp);
149 }
150
151 void arts_addfont(struct _gfxdevice*dev, gfxfont_t*font)
152 {
153     internal_t*i = (internal_t*)dev->internal;
154     i->out->addfont(i->out, font);
155 }
156
157 void arts_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
158 {
159     internal_t*i = (internal_t*)dev->internal;
160     gfxline_t*glyph = gfxline_clone(font->glyphs[glyphnr].line);
161     gfxline_transform(glyph, matrix);
162
163     if(i->clip) {
164         gfxbbox_t bbox = gfxline_getbbox(glyph);
165         ArtSVP*dummybox = boxToSVP(bbox.xmin,bbox.ymin,bbox.xmax,bbox.ymax);
166         ArtSVP*svp = art_svp_intersect(dummybox, i->clip->svp);
167         gfxline_t*gfxline = SVPtogfxline(svp);
168         gfxbbox_t bbox2 = gfxline_getbbox(gfxline);
169         double w = bbox2.xmax - bbox2.xmin;
170         double h = bbox2.ymax - bbox2.ymin;
171         if(w < 0.001 || h < 0.001) /* character was clipped completely */ {
172         } else if(fabs((bbox.xmax - bbox.xmin) - w) > 0.05 ||
173                   fabs((bbox.ymax - bbox.ymin) - h) > 0.05) {
174             /* notable change in character size: character was clipped 
175                TODO: handle diagonal cuts
176              */
177             arts_fill(dev, glyph, color);
178         } else {
179             i->out->drawchar(i->out, font, glyphnr, color, matrix);
180         }
181     } else {
182         i->out->drawchar(i->out, font, glyphnr, color, matrix);
183     }
184     
185     gfxline_free(glyph);
186 }
187
188 void arts_drawlink(struct _gfxdevice*dev, gfxline_t*line, char*action)
189 {
190     internal_t*i = (internal_t*)dev->internal;
191     i->out->drawlink(i->out, line, action);
192 }
193
194 void arts_endpage(struct _gfxdevice*dev)
195 {
196     internal_t*i = (internal_t*)dev->internal;
197     i->out->endpage(i->out);
198 }
199
200 gfxresult_t* arts_finish(struct _gfxdevice*dev)
201 {
202     internal_t*i = (internal_t*)dev->internal;
203     return i->out->finish(i->out);
204 }
205
206 void gfxdevice_arts_init(gfxdevice_t*dev, gfxdevice_t*out)
207 {
208     internal_t*i = rfx_calloc(sizeof(internal_t));
209     memset(dev, 0, sizeof(gfxdevice_t));
210     dev->internal = i;
211
212     dev->setparameter = arts_setparameter;
213     dev->startpage = arts_startpage;
214     dev->startclip = arts_startclip;
215     dev->endclip = arts_endclip;
216     dev->stroke = arts_stroke;
217     dev->fill = arts_fill;
218     dev->fillbitmap = arts_fillbitmap;
219     dev->fillgradient = arts_fillgradient;
220     dev->addfont = arts_addfont;
221     dev->drawchar = arts_drawchar;
222     dev->drawlink = arts_drawlink;
223     dev->endpage = arts_endpage;
224     dev->finish = arts_finish;
225
226     i->out = out;
227 }
228