50b3c3713de6fa6cb0d5fb2f4d7ec381dbf0eeb9
[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 "../types.h"
27 #include "../mem.h"
28 #include "../gfxdevice.h"
29 #include "../gfxtools.h"
30
31 typedef struct _internal {
32     gfxdevice_t*out;
33     int targetwidth;
34     int targetheight;
35     gfxmatrix_t matrix;
36     double zoomwidth;
37 } internal_t;
38
39 static int verbose = 1;
40 static void dbg(char*format, ...)
41 {
42     if(!verbose)
43         return;
44     char buf[1024];
45     int l;
46     va_list arglist;
47     va_start(arglist, format);
48     vsprintf(buf, format, arglist);
49     va_end(arglist);
50     l = strlen(buf);
51     while(l && buf[l-1]=='\n') {
52         buf[l-1] = 0;
53         l--;
54     }
55     printf("(device-rescale) %s\n", buf);
56     fflush(stdout);
57 }
58
59 gfxline_t*transformgfxline(internal_t*i, gfxline_t*line)
60 {
61     gfxline_t*line2 = gfxline_clone(line);
62     gfxline_transform(line2, &i->matrix);
63     return line2;
64 }
65
66 int rescale_setparameter(gfxdevice_t*dev, const char*key, const char*value)
67 {
68     internal_t*i = (internal_t*)dev->internal;
69     return i->out->setparameter(i->out,key,value);
70 }
71
72 void rescale_startpage(gfxdevice_t*dev, int width, int height)
73 {
74     internal_t*i = (internal_t*)dev->internal;
75     i->matrix.m00 = (double)i->targetwidth / (double)width;
76     i->matrix.m11 = (double)i->targetheight / (double)height;
77     i->out->startpage(i->out,i->targetwidth,i->targetheight);
78 }
79
80 void rescale_startclip(gfxdevice_t*dev, gfxline_t*line)
81 {
82     internal_t*i = (internal_t*)dev->internal;
83     gfxline_t*line2 = transformgfxline(i, line);
84     i->out->startclip(i->out,line2);
85     gfxline_free(line2);
86 }
87
88 void rescale_endclip(gfxdevice_t*dev)
89 {
90     internal_t*i = (internal_t*)dev->internal;
91     i->out->endclip(i->out);
92 }
93
94 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)
95 {
96     internal_t*i = (internal_t*)dev->internal;
97     gfxline_t*line2 = transformgfxline(i, line);
98     i->out->stroke(i->out, line2, width*i->zoomwidth, color, cap_style, joint_style, miterLimit);
99     gfxline_free(line2);
100 }
101
102 void rescale_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
103 {
104     internal_t*i = (internal_t*)dev->internal;
105     gfxline_t*line2 = transformgfxline(i, line);
106     i->out->fill(i->out, line2, color);
107     gfxline_free(line2);
108 }
109
110 void rescale_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
111 {
112     internal_t*i = (internal_t*)dev->internal;
113     gfxline_t*line2 = transformgfxline(i, line);
114     gfxmatrix_t m2;
115     gfxmatrix_multiply(&i->matrix, matrix, &m2);
116     i->out->fillbitmap(i->out, line2, img, &m2, cxform);
117     gfxline_free(line2);
118 }
119
120 void rescale_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
121 {
122     internal_t*i = (internal_t*)dev->internal;
123     gfxline_t*line2 = transformgfxline(i, line);
124     i->out->fillgradient(i->out, line, gradient, type, matrix);
125     gfxline_free(line2);
126 }
127
128 void rescale_addfont(gfxdevice_t*dev, gfxfont_t*font)
129 {
130     internal_t*i = (internal_t*)dev->internal;
131     i->out->addfont(i->out, font);
132 }
133
134 void rescale_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
135 {
136     internal_t*i = (internal_t*)dev->internal;
137     gfxmatrix_t m2;
138     gfxmatrix_multiply(&i->matrix, matrix, &m2);
139     i->out->drawchar(i->out, font, glyphnr, color, &m2);
140 }
141
142 void rescale_drawlink(gfxdevice_t*dev, gfxline_t*line, char*action)
143 {
144     internal_t*i = (internal_t*)dev->internal;
145     gfxline_t*line2 = transformgfxline(i, line);
146     i->out->drawlink(i->out, line, action);
147     gfxline_free(line2);
148 }
149
150 void rescale_endpage(gfxdevice_t*dev)
151 {
152     internal_t*i = (internal_t*)dev->internal;
153     i->out->endpage(i->out);
154 }
155
156 gfxresult_t* rescale_finish(gfxdevice_t*dev)
157 {
158     internal_t*i = (internal_t*)dev->internal;
159     gfxdevice_t*out = i->out;
160     free(dev->internal);dev->internal = 0;i=0;
161     return out->finish(out);
162 }
163
164 void gfxdevice_rescale_init(gfxdevice_t*dev, gfxdevice_t*out, int width, int height)
165 {
166     internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
167     memset(dev, 0, sizeof(gfxdevice_t));
168
169     dev->name = "rescale";
170
171     dev->internal = i;
172
173     dev->setparameter = rescale_setparameter;
174     dev->startpage = rescale_startpage;
175     dev->startclip = rescale_startclip;
176     dev->endclip = rescale_endclip;
177     dev->stroke = rescale_stroke;
178     dev->fill = rescale_fill;
179     dev->fillbitmap = rescale_fillbitmap;
180     dev->fillgradient = rescale_fillgradient;
181     dev->addfont = rescale_addfont;
182     dev->drawchar = rescale_drawchar;
183     dev->drawlink = rescale_drawlink;
184     dev->endpage = rescale_endpage;
185     dev->finish = rescale_finish;
186
187     gfxmatrix_unit(&i->matrix);
188     i->targetwidth = width;
189     i->targetheight = height;
190     i->zoomwidth = 1.0;
191
192     i->out = out;
193 }
194