3 Part of the swftools package.
5 Copyright (c) 2006 Matthias Kramm <kramm@quiss.org>
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.
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.
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 */
33 #include "../gfxdevice.h"
34 #include "../gfxtools.h"
36 typedef struct _internal {
48 static int verbose = 1;
49 static void dbg(char*format, ...)
56 va_start(arglist, format);
57 vsprintf(buf, format, arglist);
60 while(l && buf[l-1]=='\n') {
64 printf("(device-rescale) %s\n", buf);
68 char gfxline_isRect(gfxline_t*line)
73 double x1=line->x,y1=line->x,x2=line->x,y2=line->y;
74 int nx1=0,nx2=0,ny1=0,ny2=0;
77 if(line->type != gfx_lineTo)
82 } else if(line->y < y1) {
85 } else if(line->x > x2) {
88 } else if(line->y > y1) {
94 return (nx1+nx2)==1 && (ny1+ny2)==1;
97 gfxline_t*transformgfxline(internal_t*i, gfxline_t*line)
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) {
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);
113 gfxline_t*line2 = gfxline_clone(line);
114 gfxline_transform(line2, &i->matrix);
118 int rescale_setparameter(gfxdevice_t*dev, const char*key, const char*value)
120 internal_t*i = (internal_t*)dev->internal;
121 if(!strcmp(key, "keepratio")) {
122 i->keepratio = atoi(value);
126 return i->out->setparameter(i->out,key,value);
133 void rescale_startpage(gfxdevice_t*dev, int width, int height)
135 internal_t*i = (internal_t*)dev->internal;
137 i->origwidth = width;
138 i->origheight = height;
140 if(i->targetwidth || i->targetheight) {
141 int targetwidth = i->targetwidth;
143 targetwidth = width*i->targetheight/height;
144 int targetheight = i->targetheight;
146 targetheight = height*i->targetwidth/width;
148 double rx = (double)targetwidth / (double)width;
149 double ry = (double)targetheight / (double)height;
155 i->matrix.ty = (targetheight - height*rx) / 2;
161 i->matrix.tx = (targetwidth - width*ry) / 2;
166 i->matrix.m00 = (double)targetwidth / (double)width;
167 i->matrix.m11 = (double)targetheight / (double)height;
169 i->zoomwidth = sqrt(i->matrix.m00*i->matrix.m11);
170 i->out->startpage(i->out,targetwidth,targetheight);
172 i->out->startpage(i->out,(int)(width*i->matrix.m00),(int)(height*i->matrix.m11));
176 void rescale_startclip(gfxdevice_t*dev, gfxline_t*line)
178 internal_t*i = (internal_t*)dev->internal;
179 gfxline_t*line2 = transformgfxline(i, line);
180 i->out->startclip(i->out,line2);
184 void rescale_endclip(gfxdevice_t*dev)
186 internal_t*i = (internal_t*)dev->internal;
187 i->out->endclip(i->out);
190 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)
192 internal_t*i = (internal_t*)dev->internal;
193 gfxline_t*line2 = transformgfxline(i, line);
194 i->out->stroke(i->out, line2, width*i->zoomwidth, color, cap_style, joint_style, miterLimit);
198 void rescale_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
200 internal_t*i = (internal_t*)dev->internal;
201 gfxline_t*line2 = transformgfxline(i, line);
202 i->out->fill(i->out, line2, color);
206 void rescale_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
208 internal_t*i = (internal_t*)dev->internal;
209 gfxline_t*line2 = transformgfxline(i, line);
211 gfxmatrix_multiply(&i->matrix, matrix, &m2);
212 i->out->fillbitmap(i->out, line2, img, &m2, cxform);
216 void rescale_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
218 internal_t*i = (internal_t*)dev->internal;
219 gfxline_t*line2 = transformgfxline(i, line);
220 i->out->fillgradient(i->out, line2, gradient, type, matrix);
224 void rescale_addfont(gfxdevice_t*dev, gfxfont_t*font)
226 internal_t*i = (internal_t*)dev->internal;
227 i->out->addfont(i->out, font);
230 void rescale_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
232 internal_t*i = (internal_t*)dev->internal;
234 gfxmatrix_multiply(&i->matrix, matrix, &m2);
235 i->out->drawchar(i->out, font, glyphnr, color, &m2);
238 void rescale_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action)
240 internal_t*i = (internal_t*)dev->internal;
241 gfxline_t*line2 = transformgfxline(i, line);
242 i->out->drawlink(i->out, line2, action);
246 void rescale_endpage(gfxdevice_t*dev)
248 internal_t*i = (internal_t*)dev->internal;
249 i->out->endpage(i->out);
252 gfxresult_t* rescale_finish(gfxdevice_t*dev)
254 internal_t*i = (internal_t*)dev->internal;
255 gfxdevice_t*out = i->out;
256 free(dev->internal);dev->internal = 0;i=0;
258 return out->finish(out);
264 void gfxdevice_rescale_init(gfxdevice_t*dev, gfxdevice_t*out, int width, int height, double scale)
266 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
267 memset(dev, 0, sizeof(gfxdevice_t));
269 dev->name = "rescale";
273 dev->setparameter = rescale_setparameter;
274 dev->startpage = rescale_startpage;
275 dev->startclip = rescale_startclip;
276 dev->endclip = rescale_endclip;
277 dev->stroke = rescale_stroke;
278 dev->fill = rescale_fill;
279 dev->fillbitmap = rescale_fillbitmap;
280 dev->fillgradient = rescale_fillgradient;
281 dev->addfont = rescale_addfont;
282 dev->drawchar = rescale_drawchar;
283 dev->drawlink = rescale_drawlink;
284 dev->endpage = rescale_endpage;
285 dev->finish = rescale_finish;
287 gfxmatrix_unit(&i->matrix);
288 i->targetwidth = width;
289 i->targetheight = height;
293 i->matrix.m00 = scale;
295 i->matrix.m11 = scale;
299 i->zoomwidth = scale;
304 void gfxdevice_rescale_setzoom(gfxdevice_t*dev, double scale)
306 internal_t*i = (internal_t*)dev->internal;
307 if(strcmp(dev->name, "rescale")) {
308 fprintf(stderr, "Internal error: can't cast device %s to a rescale device\n", dev->name);
311 i->matrix.m00 = scale;
313 i->matrix.m11 = scale;
317 i->zoomwidth = scale;
319 void gfxdevice_rescale_setdevice(gfxdevice_t*dev, gfxdevice_t*out)
321 internal_t*i = (internal_t*)dev->internal;
322 if(strcmp(dev->name, "rescale")) {
323 fprintf(stderr, "Internal error: can't cast device %s to a rescale device\n", dev->name);
329 gfxdevice_t* gfxdevice_rescale_new(gfxdevice_t*out, int width, int height, double scale)
331 gfxdevice_t* d = (gfxdevice_t*)malloc(sizeof(gfxdevice_t));
332 gfxdevice_rescale_init(d, out, width, height, scale);