3 Part of the swftools package.
5 Copyright (c) 2006 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"
29 #include "../gfxtools.h"
32 typedef struct _textpage {
36 struct _textpage*next;
39 typedef struct _internal {
40 textpage_t*first_page;
41 textpage_t*current_page;
44 int text_setparameter(gfxdevice_t*dev, const char*key, const char*value)
46 internal_t*i = (internal_t*)dev->internal;
48 void text_startpage(gfxdevice_t*dev, int width, int height)
50 internal_t*i = (internal_t*)dev->internal;
52 i->first_page = i->current_page = (textpage_t*)malloc(sizeof(textpage_t));
54 i->current_page->next = (textpage_t*)malloc(sizeof(textpage_t));
55 i->current_page = i->current_page->next;
57 i->current_page->textsize = 4096;
58 i->current_page->text = malloc(i->current_page->textsize);
60 void text_startclip(gfxdevice_t*dev, gfxline_t*line)
62 internal_t*i = (internal_t*)dev->internal;
64 void text_endclip(gfxdevice_t*dev)
66 internal_t*i = (internal_t*)dev->internal;
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)
70 internal_t*i = (internal_t*)dev->internal;
72 void text_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
74 internal_t*i = (internal_t*)dev->internal;
76 void text_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
78 internal_t*i = (internal_t*)dev->internal;
80 void text_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
82 internal_t*i = (internal_t*)dev->internal;
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)
87 internal_t*i = (internal_t*)dev->internal;
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);
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]);
100 void text_drawlink(gfxdevice_t*dev, gfxline_t*line, char*action)
102 internal_t*i = (internal_t*)dev->internal;
105 void text_endpage(gfxdevice_t*dev)
107 internal_t*i = (internal_t*)dev->internal;
110 void text_result_write(gfxresult_t*r, int filedesc)
112 textpage_t*i= (textpage_t*)r->internal;
114 int text_result_save(gfxresult_t*r, char*filename)
116 textpage_t*i= (textpage_t*)r->internal;
118 return 0; // no pages drawn
120 FILE*fi = fopen(filename, "wb");
124 fwrite(i->text, i->textpos, 1, fi);
130 void*text_result_get(gfxresult_t*r, char*name)
132 textpage_t*i= (textpage_t*)r->internal;
133 if(!strcmp(name,"text")) {
140 char*text = malloc(len);
144 memcpy(&text[pos], i->text, i->textpos);
150 } else if(!strncmp(name,"page",4)) {
151 int pagenr = atoi(&name[4]);
159 i->text[i->textpos] = 0;
160 return strdup(i->text);
164 void text_result_destroy(gfxresult_t*r)
166 textpage_t*i= (textpage_t*)r->internal;
169 textpage_t*next = i->next;
170 free(i->text);i->text = 0;
177 gfxresult_t* text_finish(struct _gfxdevice*dev)
179 internal_t*i = (internal_t*)dev->internal;
181 gfxresult_t* res = (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
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;
189 free(dev->internal); dev->internal = 0; i = 0;
196 void gfxdevice_text_init(gfxdevice_t*dev, gfxdevice_t*out)
198 internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
199 memset(dev, 0, sizeof(gfxdevice_t));
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;