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