1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
/* gfxdevice_file.cc
Part of the swftools package.
Copyright (c) 2005 Matthias Kramm <kramm@quiss.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#include <stdlib.h>
#include <stdio.h>
#ifndef WIN32
#include <unistd.h>
#endif
#include <string.h>
#include <memory.h>
#include "../gfxdevice.h"
typedef struct _internal {
char*filename;
FILE*fi;
} internal_t;
int file_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
{
internal_t*i = (internal_t*)dev->internal;
fprintf(i->fi, "setparameter %s=%s\n", key, value);
return 1;
}
void file_startpage(struct _gfxdevice*dev, int width, int height)
{
internal_t*i = (internal_t*)dev->internal;
fprintf(i->fi, "startpage %d %d\n", width, height);
}
static void dumpline(FILE*fi, gfxline_t*line)
{
while(line) {
if(line->type == gfx_moveTo) {
fprintf(fi, "\tmoveTo %f %f\n", line->x, line->y);
} else if(line->type == gfx_lineTo) {
fprintf(fi, "\tlineTo %f %f\n", line->x, line->y);
} else if(line->type == gfx_splineTo) {
fprintf(fi, "\tsplineTo %f %f %f %f\n", line->sx, line->sy, line->x, line->y);
}
line = line->next;
}
}
static void dumpmatrix (FILE*fi, gfxmatrix_t*matrix)
{
fprintf(fi, "| %5.2f %5.2f %5.2f\n", matrix->m00, matrix->m10, matrix->tx);
fprintf(fi, "| %5.2f %5.2f %5.2f\n", matrix->m01, matrix->m11, matrix->ty);
}
static void dumpgradient (FILE*fi, gfxgradient_t*gradient)
{
while(gradient) {
fprintf(fi, "pos %f: %02x%02x%02x%02x\n", gradient->pos, gradient->color.r, gradient->color.g, gradient->color.b, gradient->color.a);
gradient = gradient->next;
}
}
void file_startclip(struct _gfxdevice*dev, gfxline_t*line)
{
internal_t*i = (internal_t*)dev->internal;
fprintf(i->fi, "startclip\n");
dumpline(i->fi, line);
}
void file_endclip(struct _gfxdevice*dev)
{
internal_t*i = (internal_t*)dev->internal;
fprintf(i->fi, "endclip\n");
}
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)
{
internal_t*i = (internal_t*)dev->internal;
char* jointTypes[] = {"joinMiter", "joinRound", "joinBevel"};
char* capTypes[] = {"capButt", "capRound", "capSquare"};
fprintf(i->fi, "stroke %f %f %s %s %02x%02x%02x%02x\n", width, miterLimit, capTypes[cap_style], jointTypes[joint_style],
color->r, color->g, color->b, color->a
);
dumpline(i->fi, line);
}
void file_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
{
internal_t*i = (internal_t*)dev->internal;
fprintf(i->fi, "fill %02x%02x%02x%02x\n", color->r, color->g, color->b, color->a);
dumpline(i->fi, line);
}
void file_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
{
internal_t*i = (internal_t*)dev->internal;
fprintf(i->fi, "fillbitmap\n");
dumpmatrix(i->fi, matrix);
dumpline(i->fi, line);
}
void file_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
{
internal_t*i = (internal_t*)dev->internal;
fprintf(i->fi, "fillgradient\n");
dumpmatrix(i->fi, matrix);
dumpgradient(i->fi, gradient);
dumpline(i->fi, line);
}
void file_addfont(struct _gfxdevice*dev, gfxfont_t*font)
{
internal_t*i = (internal_t*)dev->internal;
fprintf(i->fi, "addfont %s\n", font->id);
}
void file_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix)
{
internal_t*i = (internal_t*)dev->internal;
}
void file_drawlink(struct _gfxdevice*dev, gfxline_t*line, const char*action, const char*text)
{
internal_t*i = (internal_t*)dev->internal;
fprintf(i->fi, "drawlink %s\n", action);
dumpline(i->fi, line);
}
void file_endpage(struct _gfxdevice*dev)
{
internal_t*i = (internal_t*)dev->internal;
fprintf(i->fi, "endpage\n");
}
typedef struct gfxresult_internal
{
FILE*fi;
char*filename;
} gfxresult_internal_t;
void fileresult_destroy(struct _gfxresult*gfx)
{
gfxresult_internal_t*i = (gfxresult_internal_t*)gfx->internal;
unlink(i->filename);
free(i->filename);i->filename = 0;
}
int fileresult_save(struct _gfxresult*gfx, const char*filename)
{
gfxresult_internal_t*i = (gfxresult_internal_t*)gfx->internal;
FILE*fi,*fo;
fi = fopen(i->filename, "rb");
if(!fi) {
perror(i->filename);
return 0;
}
fo = fopen(filename, "wb");
if(!fo) {
perror(filename);
return 0;
}
char buf[4096];
while(!feof(fi)) {
int l = fread(buf, 1, 4096, fi);
if(l>0) {
fwrite(buf, 1, l, fo);
} else {
break;
}
}
fclose(fi);
fclose(fo);
return 0;
}
void* fileresult_get(struct _gfxresult*gfx, const char*name)
{
return 0;
}
gfxresult_t* file_finish(struct _gfxdevice*dev)
{
internal_t*i = (internal_t*)dev->internal;
char*filename = strdup(i->filename);
gfxresult_t*result = (gfxresult_t*)malloc(sizeof(gfxresult_t));
fclose(i->fi);
i->fi = 0;
if(i->filename) {
free(i->filename);
i->filename = 0;
}
free(dev->internal);
dev->internal = 0;
memset(result, 0, sizeof(gfxresult_t));
result->save = fileresult_save;
result->get = fileresult_get;
result->destroy = fileresult_destroy;
result->internal = malloc(sizeof(gfxresult_internal_t));
((gfxresult_internal_t*)result->internal)->filename = filename;
return result;
}
void gfxdevice_file_init(gfxdevice_t*dev, char*filename)
{
internal_t*i = (internal_t*)malloc(sizeof(internal_t));
memset(dev, 0, sizeof(gfxdevice_t));
dev->name = "file";
dev->internal = i;
dev->setparameter = file_setparameter;
dev->startpage = file_startpage;
dev->startclip = file_startclip;
dev->endclip = file_endclip;
dev->stroke = file_stroke;
dev->fill = file_fill;
dev->fillbitmap = file_fillbitmap;
dev->fillgradient = file_fillgradient;
dev->addfont = file_addfont;
dev->drawchar = file_drawchar;
dev->drawlink = file_drawlink;
dev->endpage = file_endpage;
dev->finish = file_finish;
i->fi = fopen(filename, "wb");
i->filename = strdup(filename);
if(!i->fi) {
fprintf(stderr, "Couldn't open file %s\n", filename);
exit(1);
}
}
|