moved from gfxdevice_*
[swftools.git] / lib / devices / file.c
1 /* gfxdevice_file.cc
2
3    Part of the swftools package.
4
5    Copyright (c) 2005 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 <memory.h>
24 #include "../gfxdevice.h"
25
26 typedef struct _internal {
27     FILE*fi;
28 } internal_t;
29
30 int file_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
31 {
32     internal_t*i = (internal_t*)dev->internal;
33     fprintf(i->fi, "setparameter %s=%s\n", key, value);
34     return 0;
35 }
36
37 void file_startpage(struct _gfxdevice*dev, int width, int height)
38 {
39     internal_t*i = (internal_t*)dev->internal;
40     fprintf(i->fi, "startpage %d %d\n", width, height);
41 }
42
43 static void dumpline(FILE*fi, gfxline_t*line)
44 {
45     while(line) {
46         if(line->type == gfx_moveTo) {
47             fprintf(fi, "\tmoveTo %f %f\n", line->x, line->y);
48         } else if(line->type == gfx_lineTo) {
49             fprintf(fi, "\tlineTo %f %f\n", line->x, line->y);
50         } else if(line->type == gfx_splineTo) {
51             fprintf(fi, "\tsplineTo %f %f %f %f\n", line->sx, line->sy, line->x, line->y);
52         }
53     }
54 }
55
56 void file_startclip(struct _gfxdevice*dev, gfxline_t*line)
57 {
58     internal_t*i = (internal_t*)dev->internal;
59     fprintf(i->fi, "startclip\n");
60     dumpline(i->fi, line);
61 }
62
63 void file_endclip(struct _gfxdevice*dev)
64 {
65     internal_t*i = (internal_t*)dev->internal;
66     fprintf(i->fi, "endclip\n");
67 }
68
69 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)
70 {
71     internal_t*i = (internal_t*)dev->internal;
72     char* jointTypes[] = {"joinMiter", "joinRound", "joinBevel"};
73     char* capTypes[] = {"capButt", "capRound", "capSquare"};
74
75     fprintf(i->fi, "stroke %f %f %s %s %02x%02x%02x%02x\n", width, miterLimit, capTypes[cap_style], jointTypes[joint_style],
76                 color->r, color->g, color->b, color->a
77             );
78     dumpline(i->fi, line);
79 }
80
81 void file_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
82 {
83     internal_t*i = (internal_t*)dev->internal;
84     fprintf(i->fi, "fill %02x%02x%02x%02x\n", color->r, color->g, color->b, color->a);
85     dumpline(i->fi, line);
86 }
87
88 void file_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
89 {
90     internal_t*i = (internal_t*)dev->internal;
91     fprintf(i->fi, "fillbitmap\n");
92     dumpline(i->fi, line);
93 }
94
95 void file_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
96 {
97     internal_t*i = (internal_t*)dev->internal;
98     fprintf(i->fi, "fillgradient\n");
99     dumpline(i->fi, line);
100 }
101
102 void file_addfont(struct _gfxdevice*dev, char*fontid, gfxfont_t*font)
103 {
104     internal_t*i = (internal_t*)dev->internal;
105 }
106
107 void file_drawchar(struct _gfxdevice*dev, char*fontid, int glyph, gfxcolor_t*color, gfxmatrix_t*matrix)
108 {
109     internal_t*i = (internal_t*)dev->internal;
110 }
111
112 void file_drawlink(struct _gfxdevice*dev, gfxline_t*line, char*action)
113 {
114     internal_t*i = (internal_t*)dev->internal;
115     fprintf(i->fi, "drawlink %s\n", action);
116     dumpline(i->fi, line);
117 }
118
119 void file_endpage(struct _gfxdevice*dev)
120 {
121     internal_t*i = (internal_t*)dev->internal;
122     fprintf(i->fi, "endpage\n");
123 }
124
125 gfxresult_t* file_finish(struct _gfxdevice*dev)
126 {
127     internal_t*i = (internal_t*)dev->internal;
128     fclose(i->fi);
129     i->fi = 0;
130     free(dev->internal);
131     dev->internal = 0;
132     return 0;
133 }
134
135 void gfxdevice_file_init(gfxdevice_t*dev, char*filename)
136 {
137     internal_t*i = malloc(sizeof(internal_t));
138     memset(dev, 0, sizeof(gfxdevice_t));
139     dev->internal = i;
140
141     dev->setparameter = file_setparameter;
142     dev->startpage = file_startpage;
143     dev->startclip = file_startclip;
144     dev->endclip = file_endclip;
145     dev->stroke = file_stroke;
146     dev->fill = file_fill;
147     dev->fillbitmap = file_fillbitmap;
148     dev->fillgradient = file_fillgradient;
149     dev->addfont = file_addfont;
150     dev->drawchar = file_drawchar;
151     dev->drawlink = file_drawlink;
152     dev->endpage = file_endpage;
153     dev->finish = file_finish;
154
155     i->fi = fopen(filename, "wb");
156     if(!i->fi) {
157         fprintf(stderr, "Couldn't open file %s\n", filename);
158         exit(1);
159     }
160 }
161