3 Part of the swftools package.
5 Copyright (c) 2005 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 */
28 #include "../gfxdevice.h"
30 typedef struct _internal {
35 int file_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
37 internal_t*i = (internal_t*)dev->internal;
38 fprintf(i->fi, "setparameter %s=%s\n", key, value);
42 void file_startpage(struct _gfxdevice*dev, int width, int height)
44 internal_t*i = (internal_t*)dev->internal;
45 fprintf(i->fi, "startpage %d %d\n", width, height);
48 static void dumpline(FILE*fi, gfxline_t*line)
51 if(line->type == gfx_moveTo) {
52 fprintf(fi, "\tmoveTo %f %f\n", line->x, line->y);
53 } else if(line->type == gfx_lineTo) {
54 fprintf(fi, "\tlineTo %f %f\n", line->x, line->y);
55 } else if(line->type == gfx_splineTo) {
56 fprintf(fi, "\tsplineTo %f %f %f %f\n", line->sx, line->sy, line->x, line->y);
62 void file_startclip(struct _gfxdevice*dev, gfxline_t*line)
64 internal_t*i = (internal_t*)dev->internal;
65 fprintf(i->fi, "startclip\n");
66 dumpline(i->fi, line);
69 void file_endclip(struct _gfxdevice*dev)
71 internal_t*i = (internal_t*)dev->internal;
72 fprintf(i->fi, "endclip\n");
75 void file_stroke(struct _gfxdevice*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*color, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit)
77 internal_t*i = (internal_t*)dev->internal;
78 char* jointTypes[] = {"joinMiter", "joinRound", "joinBevel"};
79 char* capTypes[] = {"capButt", "capRound", "capSquare"};
81 fprintf(i->fi, "stroke %f %f %s %s %02x%02x%02x%02x\n", width, miterLimit, capTypes[cap_style], jointTypes[joint_style],
82 color->r, color->g, color->b, color->a
84 dumpline(i->fi, line);
87 void file_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
89 internal_t*i = (internal_t*)dev->internal;
90 fprintf(i->fi, "fill %02x%02x%02x%02x\n", color->r, color->g, color->b, color->a);
91 dumpline(i->fi, line);
94 void file_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
96 internal_t*i = (internal_t*)dev->internal;
97 fprintf(i->fi, "fillbitmap\n");
98 dumpline(i->fi, line);
101 void file_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
103 internal_t*i = (internal_t*)dev->internal;
104 fprintf(i->fi, "fillgradient\n");
105 dumpline(i->fi, line);
108 void file_addfont(struct _gfxdevice*dev, gfxfont_t*font)
110 internal_t*i = (internal_t*)dev->internal;
113 void file_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix)
115 internal_t*i = (internal_t*)dev->internal;
118 void file_drawlink(struct _gfxdevice*dev, gfxline_t*line, const char*action)
120 internal_t*i = (internal_t*)dev->internal;
121 fprintf(i->fi, "drawlink %s\n", action);
122 dumpline(i->fi, line);
125 void file_endpage(struct _gfxdevice*dev)
127 internal_t*i = (internal_t*)dev->internal;
128 fprintf(i->fi, "endpage\n");
131 typedef struct gfxresult_internal
135 } gfxresult_internal_t;
137 void fileresult_destroy(struct _gfxresult*gfx)
139 gfxresult_internal_t*i = (gfxresult_internal_t*)gfx->internal;
141 free(i->filename);i->filename = 0;
144 int fileresult_save(struct _gfxresult*gfx, const char*filename)
146 gfxresult_internal_t*i = (gfxresult_internal_t*)gfx->internal;
148 fi = fopen(i->filename, "rb");
153 fo = fopen(filename, "wb");
161 int l = fread(buf, 1, 4096, fi);
163 fwrite(buf, 1, l, fo);
174 void* fileresult_get(struct _gfxresult*gfx, const char*name)
179 gfxresult_t* file_finish(struct _gfxdevice*dev)
181 internal_t*i = (internal_t*)dev->internal;
182 char*filename = strdup(i->filename);
183 gfxresult_t*result = (gfxresult_t*)malloc(sizeof(gfxresult_t));
193 memset(result, 0, sizeof(gfxresult_t));
194 result->save = fileresult_save;
195 result->get = fileresult_get;
196 result->destroy = fileresult_destroy;
197 result->internal = malloc(sizeof(gfxresult_internal_t));
198 ((gfxresult_internal_t*)result->internal)->filename = filename;
203 void gfxdevice_file_init(gfxdevice_t*dev, char*filename)
205 internal_t*i = (internal_t*)malloc(sizeof(internal_t));
206 memset(dev, 0, sizeof(gfxdevice_t));
212 dev->setparameter = file_setparameter;
213 dev->startpage = file_startpage;
214 dev->startclip = file_startclip;
215 dev->endclip = file_endclip;
216 dev->stroke = file_stroke;
217 dev->fill = file_fill;
218 dev->fillbitmap = file_fillbitmap;
219 dev->fillgradient = file_fillgradient;
220 dev->addfont = file_addfont;
221 dev->drawchar = file_drawchar;
222 dev->drawlink = file_drawlink;
223 dev->endpage = file_endpage;
224 dev->finish = file_finish;
226 i->fi = fopen(filename, "wb");
227 i->filename = strdup(filename);
229 fprintf(stderr, "Couldn't open file %s\n", filename);