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