added external write access for zoom & output device settings
[swftools.git] / lib / devices / rescale.c
1 /* rescale.c
2
3    Part of the swftools package.
4
5    Copyright (c) 2006 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 <math.h>
27 #include "../types.h"
28 #include "../mem.h"
29 #include "../gfxdevice.h"
30 #include "../gfxtools.h"
31
32 typedef struct _internal {
33     gfxdevice_t*out;
34     int origwidth;
35     int origheight;
36     int targetwidth;
37     int targetheight;
38     int centerx, centery;
39     gfxmatrix_t matrix;
40     double zoomwidth;
41     int keepratio;
42 } internal_t;
43
44 static int verbose = 1;
45 static void dbg(char*format, ...)
46 {
47     if(!verbose)
48         return;
49     char buf[1024];
50     int l;
51     va_list arglist;
52     va_start(arglist, format);
53     vsprintf(buf, format, arglist);
54     va_end(arglist);
55     l = strlen(buf);
56     while(l && buf[l-1]=='\n') {
57         buf[l-1] = 0;
58         l--;
59     }
60     printf("(device-rescale) %s\n", buf);
61     fflush(stdout);
62 }
63
64 char gfxline_isRect(gfxline_t*line)
65 {
66     return 1;
67     if(!line)
68         return 0;
69     double x1=line->x,y1=line->x,x2=line->x,y2=line->y;
70     int nx1=0,nx2=0,ny1=0,ny2=0;
71     line = line->next;
72     while(line) {
73         if(line->type != gfx_lineTo)
74             return 0;
75         if(line->x < x1) {
76             x1 = line->x;
77             nx1++;
78         } else if(line->y < y1) {
79             y1 = line->y;
80             ny1++;
81         } else if(line->x > x2) {
82             x2 = line->x;
83             nx2++;
84         } else if(line->y > y1) {
85             y2 = line->y;
86             ny2++;
87         }
88         line = line->next;
89     }
90     return (nx1+nx2)==1 && (ny1+ny2)==1;
91 }
92
93 gfxline_t*transformgfxline(internal_t*i, gfxline_t*line)
94 {
95     /* special case: transformed rectangle 
96     if(gfxline_isRect(line)) {
97         gfxbbox_t bbox = gfxline_getbbox(line);
98         if(fabs(bbox.xmin)<0.1 && fabs(bbox.ymin)<0.1 &&
99            fabs(bbox.ymax-i->origwidth)<0.1 && fabs(bbox.ymax-i->origheight)<0.1) {
100             gfxline_t r[5];
101             r[0].x = 0;             r[0].y = 0;           r[0].type = gfx_moveTo;r[0].next = &r[1];
102             r[1].x = i->targetwidth;r[1].y = 0;           r[1].type = gfx_lineTo;r[1].next = &r[2];
103             r[2].x = i->targetwidth;r[2].y = i->targetheight;r[2].type = gfx_lineTo;r[2].next = &r[3];
104             r[3].x = 0;             r[3].y = i->targetheight;r[3].type = gfx_lineTo;r[3].next = &r[4];
105             r[4].x = 0;             r[4].y = 0;           r[4].type = gfx_lineTo;r[4].next = 0;
106             return gfxline_clone(r);
107         }
108     } */
109     gfxline_t*line2 = gfxline_clone(line);
110     gfxline_transform(line2, &i->matrix);
111     return line2;
112 }
113
114 int rescale_setparameter(gfxdevice_t*dev, const char*key, const char*value)
115 {
116     internal_t*i = (internal_t*)dev->internal;
117     if(!strcmp(key, "keepratio")) {
118         i->keepratio = atoi(value);
119         return 1;
120     } else {
121         if(i->out) {
122             return i->out->setparameter(i->out,key,value);
123         } else {
124             return 0;
125         }
126     }
127 }
128
129 void rescale_startpage(gfxdevice_t*dev, int width, int height)
130 {
131     internal_t*i = (internal_t*)dev->internal;
132
133     i->origwidth = width;
134     i->origheight = height;
135
136     if(i->targetwidth && i->targetheight) {
137         if(i->keepratio) {
138             double rx = (double)i->targetwidth / (double)width;
139             double ry = (double)i->targetheight / (double)height;
140             if(rx<ry) {
141                 i->matrix.m00 = rx;
142                 i->matrix.m11 = rx;
143                 i->matrix.tx = 0;
144                 if(i->centery) {
145                     i->matrix.ty = (i->targetheight - height*rx) / 2;
146                 }
147             } else {
148                 i->matrix.m00 = ry;
149                 i->matrix.m11 = ry;
150                 if(i->centerx) {
151                     i->matrix.tx = (i->targetwidth - width*ry) / 2;
152                 }
153                 i->matrix.ty = 0;
154             }
155         } else {
156             i->matrix.m00 = (double)i->targetwidth / (double)width;
157             i->matrix.m11 = (double)i->targetheight / (double)height;
158         }
159         i->zoomwidth = sqrt(i->matrix.m00*i->matrix.m11);
160         i->out->startpage(i->out,i->targetwidth,i->targetheight);
161     } else {
162         i->out->startpage(i->out,(int)(width*i->matrix.m00),(int)(height*i->matrix.m11));
163     }
164 }
165
166 void rescale_startclip(gfxdevice_t*dev, gfxline_t*line)
167 {
168     internal_t*i = (internal_t*)dev->internal;
169     gfxline_t*line2 = transformgfxline(i, line);
170     i->out->startclip(i->out,line2);
171     gfxline_free(line2);
172 }
173
174 void rescale_endclip(gfxdevice_t*dev)
175 {
176     internal_t*i = (internal_t*)dev->internal;
177     i->out->endclip(i->out);
178 }
179
180 void rescale_stroke(gfxdevice_t*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*color, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit)
181 {
182     internal_t*i = (internal_t*)dev->internal;
183     gfxline_t*line2 = transformgfxline(i, line);
184     i->out->stroke(i->out, line2, width*i->zoomwidth, color, cap_style, joint_style, miterLimit);
185     gfxline_free(line2);
186 }
187
188 void rescale_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
189 {
190     internal_t*i = (internal_t*)dev->internal;
191     gfxline_t*line2 = transformgfxline(i, line);
192     i->out->fill(i->out, line2, color);
193     gfxline_free(line2);
194 }
195
196 void rescale_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
197 {
198     internal_t*i = (internal_t*)dev->internal;
199     gfxline_t*line2 = transformgfxline(i, line);
200     gfxmatrix_t m2;
201     gfxmatrix_multiply(&i->matrix, matrix, &m2);
202     i->out->fillbitmap(i->out, line2, img, &m2, cxform);
203     gfxline_free(line2);
204 }
205
206 void rescale_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
207 {
208     internal_t*i = (internal_t*)dev->internal;
209     gfxline_t*line2 = transformgfxline(i, line);
210     i->out->fillgradient(i->out, line, gradient, type, matrix);
211     gfxline_free(line2);
212 }
213
214 void rescale_addfont(gfxdevice_t*dev, gfxfont_t*font)
215 {
216     internal_t*i = (internal_t*)dev->internal;
217     i->out->addfont(i->out, font);
218 }
219
220 void rescale_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
221 {
222     internal_t*i = (internal_t*)dev->internal;
223     gfxmatrix_t m2;
224     gfxmatrix_multiply(&i->matrix, matrix, &m2);
225     i->out->drawchar(i->out, font, glyphnr, color, &m2);
226 }
227
228 void rescale_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action)
229 {
230     internal_t*i = (internal_t*)dev->internal;
231     gfxline_t*line2 = transformgfxline(i, line);
232     i->out->drawlink(i->out, line, action);
233     gfxline_free(line2);
234 }
235
236 void rescale_endpage(gfxdevice_t*dev)
237 {
238     internal_t*i = (internal_t*)dev->internal;
239     i->out->endpage(i->out);
240 }
241
242 gfxresult_t* rescale_finish(gfxdevice_t*dev)
243 {
244     internal_t*i = (internal_t*)dev->internal;
245     gfxdevice_t*out = i->out;
246     free(dev->internal);dev->internal = 0;i=0;
247     if(out) {
248         return out->finish(out);
249     } else {
250         return 0;
251     }
252 }
253
254 void gfxdevice_rescale_init(gfxdevice_t*dev, gfxdevice_t*out, int width, int height, double scale)
255 {
256     internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
257     memset(dev, 0, sizeof(gfxdevice_t));
258
259     dev->name = "rescale";
260
261     dev->internal = i;
262
263     dev->setparameter = rescale_setparameter;
264     dev->startpage = rescale_startpage;
265     dev->startclip = rescale_startclip;
266     dev->endclip = rescale_endclip;
267     dev->stroke = rescale_stroke;
268     dev->fill = rescale_fill;
269     dev->fillbitmap = rescale_fillbitmap;
270     dev->fillgradient = rescale_fillgradient;
271     dev->addfont = rescale_addfont;
272     dev->drawchar = rescale_drawchar;
273     dev->drawlink = rescale_drawlink;
274     dev->endpage = rescale_endpage;
275     dev->finish = rescale_finish;
276
277     gfxmatrix_unit(&i->matrix);
278     i->targetwidth = width;
279     i->targetheight = height;
280     i->zoomwidth = 1.0;
281     i->centerx = 1;
282         
283     i->matrix.m00 = scale;
284     i->matrix.m01 = 0;
285     i->matrix.m11 = scale;
286     i->matrix.m10 = 0;
287     i->matrix.tx = 0;
288     i->matrix.ty = 0;
289     i->zoomwidth = scale;
290
291     i->out = out;
292 }
293
294 void gfxdevice_rescale_setzoom(gfxdevice_t*dev, double scale)
295 {
296     internal_t*i = (internal_t*)dev->internal;
297     if(strcmp(dev->name, "rescale")) {
298         fprintf(stderr, "Internal error: can't cast device %s to a rescale device\n", dev->name);
299         return;
300     }
301     i->matrix.m00 = scale;
302     i->matrix.m01 = 0;
303     i->matrix.m11 = scale;
304     i->matrix.m10 = 0;
305     i->matrix.tx = 0;
306     i->matrix.ty = 0;
307     i->zoomwidth = scale;
308 }
309 void gfxdevice_rescale_setdevice(gfxdevice_t*dev, gfxdevice_t*out)
310 {
311     internal_t*i = (internal_t*)dev->internal;
312     if(strcmp(dev->name, "rescale")) {
313         fprintf(stderr, "Internal error: can't cast device %s to a rescale device\n", dev->name);
314         return;
315     }
316     i->out = out;
317 }