File: text.c

package info (click to toggle)
swftools 0.9.2%2Bgit20130725-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,680 kB
  • ctags: 17,348
  • sloc: ansic: 108,712; sh: 8,494; cpp: 8,040; yacc: 2,260; lisp: 904; makefile: 601; python: 300
file content (255 lines) | stat: -rw-r--r-- 6,820 bytes parent folder | download | duplicates (3)
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
252
253
254
255
/* text.c

   Part of the swftools package.

   Copyright (c) 2006 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>
#include <math.h>
#include <stdarg.h>
#include <unistd.h>
#include <memory.h>
#include "../types.h"
#include "../mem.h"
#include "../gfxdevice.h"
#include "../gfxtools.h"
#include "../utf8.h"

typedef struct _textpage {
    char*text;
    int textsize;
    int textpos;
    struct _textpage*next;
} textpage_t;

typedef struct _internal {
    textpage_t*first_page;
    textpage_t*current_page;
    double currentx;
    double currenty;
    double lastadvance;
} internal_t;

int text_setparameter(gfxdevice_t*dev, const char*key, const char*value)
{
    internal_t*i = (internal_t*)dev->internal;
    return 0;
}
void text_startpage(gfxdevice_t*dev, int width, int height)
{
    internal_t*i = (internal_t*)dev->internal;
    if(!i->first_page) {
	i->first_page = i->current_page = (textpage_t*)malloc(sizeof(textpage_t));
    } else {
	i->current_page->next = (textpage_t*)malloc(sizeof(textpage_t));
	i->current_page = i->current_page->next;
    }
    i->current_page->textsize = 4096;
    i->current_page->text = (char*)malloc(i->current_page->textsize);
    i->current_page->textpos = 0;
    i->current_page->next = 0;
    i->currentx = 0;
    i->currenty = 0;
    i->lastadvance = 0;
}
void text_startclip(gfxdevice_t*dev, gfxline_t*line)
{
    internal_t*i = (internal_t*)dev->internal;
}
void text_endclip(gfxdevice_t*dev)
{
    internal_t*i = (internal_t*)dev->internal;
}
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)
{
    internal_t*i = (internal_t*)dev->internal;
}
void text_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
{
    internal_t*i = (internal_t*)dev->internal;
}
void text_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
{
    internal_t*i = (internal_t*)dev->internal;
}
void text_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
{
    internal_t*i = (internal_t*)dev->internal;
}
void text_addfont(gfxdevice_t*dev, gfxfont_t*font) {}

static void addchar(gfxdevice_t*dev, int unicode)
{
    internal_t*i = (internal_t*)dev->internal;
    if(!i->current_page) {
        text_startpage(dev, 0, 0);
    }
    if(i->current_page->textpos + 10 > i->current_page->textsize) {
	i->current_page->textsize += 4096;
	i->current_page->text = realloc(i->current_page->text, i->current_page->textsize);
    }
    writeUTF8(unicode, &i->current_page->text[i->current_page->textpos]);
    i->current_page->textpos += strlen(&i->current_page->text[i->current_page->textpos]);
}

void text_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
{
    internal_t*i = (internal_t*)dev->internal;
    double xshift = matrix->tx - i->currentx;
    double yshift = matrix->ty - i->currenty;
    i->currentx = matrix->tx;
    i->currenty = matrix->ty;

    if(fabs(yshift)>1.0) {
        addchar(dev, 10);
    } else if(xshift > i->lastadvance*1.3 || xshift<0) {
        addchar(dev, 32);
    }
    int u;
    if(font) {
	i->lastadvance = font->glyphs[glyphnr].advance*matrix->m00;
	u = font->glyphs[glyphnr].unicode;
    } else {
	u = glyphnr;
	i->currentx = 0;i->currenty = 0;
    }
    if(u>13) {
        addchar(dev, u);
    }
}

void text_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action, const char*drawlink)
{
    internal_t*i = (internal_t*)dev->internal;
}

void text_endpage(gfxdevice_t*dev)
{
    internal_t*i = (internal_t*)dev->internal;
}

void text_result_write(gfxresult_t*r, int filedesc)
{
    textpage_t*i= (textpage_t*)r->internal;
}
int text_result_save(gfxresult_t*r, const char*filename)
{
    textpage_t*i= (textpage_t*)r->internal;
    if(!i) {
	return 0; // no pages drawn
    }
    FILE*fi = fopen(filename, "wb");
    if(!fi)
	return 0;
    while(i) {
	fwrite(i->text, i->textpos, 1, fi);
	i = i->next;
    }
    fclose(fi);
    return 1;
}
void*text_result_get(gfxresult_t*r, const char*name)
{
    textpage_t*i= (textpage_t*)r->internal;
    if(!strcmp(name,"text")) {
	textpage_t*j = i;
	int len = 0;
	while(j) {
	    len += i->textpos;
	    j = j->next;
	}
	char*text = (char*)malloc(len);
	int pos = 0;
	j = i;
	while(j) {
	    memcpy(&text[pos], i->text, i->textpos);
	    pos += i->textpos;
	    j = j->next;
	}
	text[pos] = 0;
	return text;
    } else if(!strncmp(name,"page",4)) {
	int pagenr = atoi(&name[4]);
	if(pagenr<0)
	    pagenr=0;
	while(pagenr>0) {
	    i = i->next;
	    if(!i)
		return 0;
	}
	i->text[i->textpos] = 0;
	return strdup(i->text);
    }
    return 0;
}
void text_result_destroy(gfxresult_t*r)
{
    textpage_t*i= (textpage_t*)r->internal;
    r->internal = 0;
    while(i) {
	textpage_t*next = i->next;
	free(i->text);i->text = 0;
	free(i);
	i = next;
    }
    free(r);
}

gfxresult_t* text_finish(struct _gfxdevice*dev)
{
    internal_t*i = (internal_t*)dev->internal;
    
    gfxresult_t* res = (gfxresult_t*)rfx_calloc(sizeof(gfxresult_t));
    
    res->internal = i->first_page;i->first_page = 0;i->current_page=0;
    res->write = text_result_write;
    res->save = text_result_save;
    res->get = text_result_get;
    res->destroy = text_result_destroy;

    free(dev->internal); dev->internal = 0; i = 0;

    return res;
}



void gfxdevice_text_init(gfxdevice_t*dev, gfxdevice_t*out)
{
    internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
    memset(dev, 0, sizeof(gfxdevice_t));

    dev->name = "text";

    dev->internal = i;

    dev->setparameter = text_setparameter;
    dev->startpage = text_startpage;
    dev->startclip = text_startclip;
    dev->endclip = text_endclip;
    dev->stroke = text_stroke;
    dev->fill = text_fill;
    dev->fillbitmap = text_fillbitmap;
    dev->fillgradient = text_fillgradient;
    dev->addfont = text_addfont;
    dev->drawchar = text_drawchar;
    dev->drawlink = text_drawlink;
    dev->endpage = text_endpage;
    dev->finish = text_finish;
}