text device
[swftools.git] / lib / devices / text.c
1 /* text.c
2
3    Part of the swftools package.
4
5    Copyright (c) 2006 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 <stdarg.h>
24 #include <unistd.h>
25 #include <memory.h>
26 #include "../types.h"
27 #include "../mem.h"
28 #include "../gfxdevice.h"
29 #include "../gfxtools.h"
30 #include "../utf8.h"
31
32 typedef struct _textpage {
33     char*text;
34     int textsize;
35     int textpos;
36     struct _textpage*next;
37 } textpage_t;
38
39 typedef struct _internal {
40     textpage_t*first_page;
41     textpage_t*current_page;
42 } internal_t;
43
44 int text_setparameter(gfxdevice_t*dev, const char*key, const char*value)
45 {
46     internal_t*i = (internal_t*)dev->internal;
47 }
48 void text_startpage(gfxdevice_t*dev, int width, int height)
49 {
50     internal_t*i = (internal_t*)dev->internal;
51     if(i->first_page) {
52         i->first_page = i->current_page = (textpage_t*)malloc(sizeof(textpage_t));
53     } else {
54         i->current_page->next = (textpage_t*)malloc(sizeof(textpage_t));
55         i->current_page = i->current_page->next;
56     }
57     i->current_page->textsize = 4096;
58     i->current_page->text = malloc(i->current_page->textsize);
59 }
60 void text_startclip(gfxdevice_t*dev, gfxline_t*line)
61 {
62     internal_t*i = (internal_t*)dev->internal;
63 }
64 void text_endclip(gfxdevice_t*dev)
65 {
66     internal_t*i = (internal_t*)dev->internal;
67 }
68 void text_stroke(gfxdevice_t*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*color, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit)
69 {
70     internal_t*i = (internal_t*)dev->internal;
71 }
72 void text_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
73 {
74     internal_t*i = (internal_t*)dev->internal;
75 }
76 void text_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
77 {
78     internal_t*i = (internal_t*)dev->internal;
79 }
80 void text_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
81 {
82     internal_t*i = (internal_t*)dev->internal;
83 }
84 void text_addfont(gfxdevice_t*dev, gfxfont_t*font) {}
85 void text_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
86 {
87     internal_t*i = (internal_t*)dev->internal;
88
89     if(i->current_page->textpos + 10 > i->current_page->textsize) {
90         i->current_page->textsize += 4096;
91         i->current_page->text = realloc(i->current_page->text, i->current_page->textsize);
92     }
93
94     gfxglyph_t*glyph = &font->glyphs[glyphnr];
95     writeUTF8(glyph->unicode, &i->current_page->text[i->current_page->textpos]);
96     i->current_page->textpos += strlen(&i->current_page->text[i->current_page->textpos]);
97     return;
98 }
99
100 void text_drawlink(gfxdevice_t*dev, gfxline_t*line, char*action)
101 {
102     internal_t*i = (internal_t*)dev->internal;
103 }
104
105 void text_endpage(gfxdevice_t*dev)
106 {
107     internal_t*i = (internal_t*)dev->internal;
108 }
109
110 void text_result_write(gfxresult_t*r, int filedesc)
111 {
112     textpage_t*i= (textpage_t*)r->internal;
113 }
114 int text_result_save(gfxresult_t*r, char*filename)
115 {
116     textpage_t*i= (textpage_t*)r->internal;
117     if(!i) {
118         return 0; // no pages drawn
119     }
120     FILE*fi = fopen(filename, "wb");
121     if(!fi)
122         return 0;
123     while(i) {
124         fwrite(i->text, i->textpos, 1, fi);
125         i = i->next;
126     }
127     fclose(fi);
128     return 1;
129 }
130 void*text_result_get(gfxresult_t*r, char*name)
131 {
132     textpage_t*i= (textpage_t*)r->internal;
133     if(!strcmp(name,"text")) {
134         textpage_t*j = i;
135         int len = 0;
136         while(j) {
137             len += i->textpos;
138             j = j->next;
139         }
140         char*text = malloc(len);
141         int pos = 0;
142         j = i;
143         while(j) {
144             memcpy(&text[pos], i->text, i->textpos);
145             pos += i->textpos;
146             j = j->next;
147         }
148         text[pos] = 0;
149         return text;
150     } else if(!strncmp(name,"page",4)) {
151         int pagenr = atoi(&name[4]);
152         if(pagenr<0)
153             pagenr=0;
154         while(pagenr>0) {
155             i = i->next;
156             if(!i)
157                 return 0;
158         }
159         i->text[i->textpos] = 0;
160         return strdup(i->text);
161     }
162     return 0;
163 }
164 void text_result_destroy(gfxresult_t*r)
165 {
166     textpage_t*i= (textpage_t*)r->internal;
167     r->internal = 0;
168     while(i) {
169         textpage_t*next = i->next;
170         free(i->text);i->text = 0;
171         free(i);
172         i = next;
173     }
174     free(r);
175 }
176
177 gfxresult_t* text_finish(struct _gfxdevice*dev)
178 {
179     internal_t*i = (internal_t*)dev->internal;
180     
181     gfxresult_t* res = (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
182     
183     res->internal = i->first_page;i->first_page = 0;i->current_page=0;
184     res->write = text_result_write;
185     res->save = text_result_save;
186     res->get = text_result_get;
187     res->destroy = text_result_destroy;
188
189     free(dev->internal); dev->internal = 0; i = 0;
190
191     return res;
192 }
193
194
195
196 void gfxdevice_text_init(gfxdevice_t*dev, gfxdevice_t*out)
197 {
198     internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
199     memset(dev, 0, sizeof(gfxdevice_t));
200
201     dev->name = "text";
202
203     dev->internal = i;
204
205     dev->setparameter = text_setparameter;
206     dev->startpage = text_startpage;
207     dev->startclip = text_startclip;
208     dev->endclip = text_endclip;
209     dev->stroke = text_stroke;
210     dev->fill = text_fill;
211     dev->fillbitmap = text_fillbitmap;
212     dev->fillgradient = text_fillgradient;
213     dev->addfont = text_addfont;
214     dev->drawchar = text_drawchar;
215     dev->drawlink = text_drawlink;
216     dev->endpage = text_endpage;
217     dev->finish = text_finish;
218 }
219