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 */
29 #include "../gfxdevice.h"
30 #include "../gfxtools.h"
32 typedef struct _internal {
44 static int verbose = 1;
45 static void dbg(char*format, ...)
52 va_start(arglist, format);
53 vsprintf(buf, format, arglist);
56 while(l && buf[l-1]=='\n') {
60 printf("(device-rescale) %s\n", buf);
64 char gfxline_isRect(gfxline_t*line)
69 double x1=line->x,y1=line->x,x2=line->x,y2=line->y;
70 int nx1=0,nx2=0,ny1=0,ny2=0;
73 if(line->type != gfx_lineTo)
78 } else if(line->y < y1) {
81 } else if(line->x > x2) {
84 } else if(line->y > y1) {
90 return (nx1+nx2)==1 && (ny1+ny2)==1;
93 gfxline_t*transformgfxline(internal_t*i, gfxline_t*line)
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) {
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);
109 gfxline_t*line2 = gfxline_clone(line);
110 gfxline_transform(line2, &i->matrix);
114 int rescale_setparameter(gfxdevice_t*dev, const char*key, const char*value)
116 internal_t*i = (internal_t*)dev->internal;
117 if(!strcmp(key, "keepratio")) {
118 i->keepratio = atoi(value);
120 } else return i->out->setparameter(i->out,key,value);
123 void rescale_startpage(gfxdevice_t*dev, int width, int height)
125 internal_t*i = (internal_t*)dev->internal;
127 i->origwidth = width;
128 i->origheight = height;
131 double rx = (double)i->targetwidth / (double)width;
132 double ry = (double)i->targetheight / (double)height;
138 i->matrix.ty = (i->targetheight - height*rx) / 2;
144 i->matrix.tx = (i->targetwidth - width*ry) / 2;
149 i->matrix.m00 = (double)i->targetwidth / (double)width;
150 i->matrix.m11 = (double)i->targetheight / (double)height;
152 i->out->startpage(i->out,i->targetwidth,i->targetheight);
155 void rescale_startclip(gfxdevice_t*dev, gfxline_t*line)
157 internal_t*i = (internal_t*)dev->internal;
158 gfxline_t*line2 = transformgfxline(i, line);
159 i->out->startclip(i->out,line2);
163 void rescale_endclip(gfxdevice_t*dev)
165 internal_t*i = (internal_t*)dev->internal;
166 i->out->endclip(i->out);
169 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)
171 internal_t*i = (internal_t*)dev->internal;
172 gfxline_t*line2 = transformgfxline(i, line);
173 i->out->stroke(i->out, line2, width*i->zoomwidth, color, cap_style, joint_style, miterLimit);
177 void rescale_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
179 internal_t*i = (internal_t*)dev->internal;
180 gfxline_t*line2 = transformgfxline(i, line);
181 i->out->fill(i->out, line2, color);
185 void rescale_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
187 internal_t*i = (internal_t*)dev->internal;
188 gfxline_t*line2 = transformgfxline(i, line);
190 gfxmatrix_multiply(&i->matrix, matrix, &m2);
191 i->out->fillbitmap(i->out, line2, img, &m2, cxform);
195 void rescale_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
197 internal_t*i = (internal_t*)dev->internal;
198 gfxline_t*line2 = transformgfxline(i, line);
199 i->out->fillgradient(i->out, line, gradient, type, matrix);
203 void rescale_addfont(gfxdevice_t*dev, gfxfont_t*font)
205 internal_t*i = (internal_t*)dev->internal;
206 i->out->addfont(i->out, font);
209 void rescale_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
211 internal_t*i = (internal_t*)dev->internal;
213 gfxmatrix_multiply(&i->matrix, matrix, &m2);
214 i->out->drawchar(i->out, font, glyphnr, color, &m2);
217 void rescale_drawlink(gfxdevice_t*dev, gfxline_t*line, char*action)
219 internal_t*i = (internal_t*)dev->internal;
220 gfxline_t*line2 = transformgfxline(i, line);
221 i->out->drawlink(i->out, line, action);
225 void rescale_endpage(gfxdevice_t*dev)
227 internal_t*i = (internal_t*)dev->internal;
228 i->out->endpage(i->out);
231 gfxresult_t* rescale_finish(gfxdevice_t*dev)
233 internal_t*i = (internal_t*)dev->internal;
234 gfxdevice_t*out = i->out;
235 free(dev->internal);dev->internal = 0;i=0;
236 return out->finish(out);
239 void gfxdevice_rescale_init(gfxdevice_t*dev, gfxdevice_t*out, int width, int height)
241 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
242 memset(dev, 0, sizeof(gfxdevice_t));
244 dev->name = "rescale";
248 dev->setparameter = rescale_setparameter;
249 dev->startpage = rescale_startpage;
250 dev->startclip = rescale_startclip;
251 dev->endclip = rescale_endclip;
252 dev->stroke = rescale_stroke;
253 dev->fill = rescale_fill;
254 dev->fillbitmap = rescale_fillbitmap;
255 dev->fillgradient = rescale_fillgradient;
256 dev->addfont = rescale_addfont;
257 dev->drawchar = rescale_drawchar;
258 dev->drawlink = rescale_drawlink;
259 dev->endpage = rescale_endpage;
260 dev->finish = rescale_finish;
262 gfxmatrix_unit(&i->matrix);
263 i->targetwidth = width;
264 i->targetheight = height;