/
btext.c
308 lines (260 loc) · 6.99 KB
/
btext.c
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include <stdio.h>
#include "btext.h"
#define PIXELS(SURFACE) ((Uint32 *)(SURFACE->pixels))
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
#define RMASK (0xff000000)
#define GMASK (0x00ff0000)
#define BMASK (0x0000ff00)
#define AMASK (0x000000ff)
#else
#define RMASK (0x000000ff)
#define GMASK (0x0000ff00)
#define BMASK (0x00ff0000)
#define AMASK (0xff000000)
#endif
bfont *btext_loadFromSurface(SDL_Surface *fontsurface) {
bfont *f;
Uint32 black;
Uint32 x, y, c;
Uint8 *data;
Uint32 offsets[96];
Uint32 pix, lstart, cpix;
SDL_Surface *surface, *surface32;
if(fontsurface->h < 2)
return(NULL);
/* convert surface to 32 bit for easier operation */
surface32 = SDL_CreateRGBSurface(SDL_SWSURFACE, 1, 1, 32, RMASK, GMASK, BMASK, AMASK);
if(surface32 == NULL)
return(NULL);
surface = SDL_ConvertSurface(fontsurface, surface32->format, SDL_SWSURFACE);
SDL_FreeSurface(surface32);
if(surface == NULL)
return(NULL);
f = malloc(sizeof(bfont));
if(f == NULL) {
SDL_FreeSurface(surface);
return(NULL);
}
f->height = surface->h - 1;
black = SDL_MapRGB(surface->format, 0, 0, 0);
if(SDL_LockSurface(surface) < 0) {
free(f);
SDL_FreeSurface(surface);
return(NULL);
}
/* ----- Find Widths ----- */
for(c = 0; c < 96; c++)
(f->widths)[c] = 1;
c = 0;
offsets[0] = 0;
for(x = 0; (int)x < surface->w; x++) {
if(PIXELS(surface)[x] == black)
((f->widths)[c])++;
else {
c++;
if(c == 96)
break;
offsets[c] = offsets[c - 1] + (f->widths)[c - 1];
}
}
if(c != 96) {
free(f);
return(NULL);
}
data = malloc(sizeof(Uint8) * (x + 1) * f->height);
if(data == NULL) {
free(f);
return(NULL);
}
/* ----- Convert data -----
* Format:
* fontdata[character][pixel data offset]
* Pixel data is stored starting at each char's top left corner and goes to the right for width, then down for height
* Pixel value is 0 for background and 1 for foreground
*/
for(c = 0; c < 96; c++) {
lstart = pix = surface->w + offsets[c]; /* skip top line and set pointer to top left pixel of char and set line start pointer */
cpix = 0; /* set font data pointer to start */
(f->fontData)[c] = &(data[offsets[c] * f->height]); /* allocate memory for glyph */
for(y = 0; y < f->height; y++) {
for(x = 0; x < (f->widths)[c]; x++) {
if(PIXELS(surface)[pix] == black) {
(f->fontData)[c][cpix] = 0;
cpix++;
} else {
(f->fontData)[c][cpix] = 1;
cpix++;
}
pix++; /* next pixel */
}
lstart = pix = lstart + surface->w; /* next line and update line start pointer */
}
}
SDL_UnlockSurface(surface);
SDL_FreeSurface(surface);
return(f);
}
bfont *btext_loadFromBMP(const char *bmpfile) {
bfont *f;
SDL_Surface *surface;
surface = SDL_LoadBMP(bmpfile);
if(surface == NULL)
return(NULL);
if((f = btext_loadFromSurface(surface)) == NULL)
return(NULL);
SDL_FreeSurface(surface);
return(f);
}
void btext_free(bfont *f) {
free((f->fontData)[0]);
free(f);
}
Uint32 btext_calcWidth(bfont *f, const char *text) {
Uint32 width, offset;
unsigned char *utext;
utext = (unsigned char *)text;
offset = width = 0;
while(utext[offset] != '\0') {
if(utext[offset] >= ' ' && utext[offset] <= '\x7f')
width += f->widths[utext[offset] - 32];
else
width += f->widths[95];
offset++;
}
return(width);
}
Uint32 btext_clipTextToWidth(bfont *f, const char *text, Uint32 limit, int overlap) {
Uint32 width, offset;
unsigned char *utext;
utext = (unsigned char *)text;
offset = width = 0;
while(utext[offset] != '\0') {
if(utext[offset] >= ' ' && utext[offset] <= '\x7f')
width += f->widths[utext[offset] - 32];
else
width += f->widths[95];
if(width > limit) {
if(overlap == 0)
return(offset - 1);
return(offset);
}
offset++;
}
return(offset);
}
SDL_Surface *btext_render(bfont *f, const char *text, Uint32 bgval, Uint32 fgval, SDL_Rect *rect, Uint32 flags) {
Uint32 length, width;
Uint32 c, x, y;
Uint32 start, lstart, blitoffset;
Uint32 dataoffset;
unsigned char *utext;
SDL_Surface *surface;
if(rect == NULL) {
length = strlen(text);
width = btext_calcWidth(f, text);
} else {
length = btext_clipTextToWidth(f, text, rect->w, 1);
width = rect->w;
}
if(length == 0)
return(NULL);
surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, f->height, 32, RMASK, GMASK, BMASK, AMASK);
if(surface == NULL)
return(NULL);
if(SDL_LockSurface(surface) < 0)
return(NULL);
utext = (unsigned char *)text;
start = 0;
for(c = 0; c < length; c++) {
lstart = start;
dataoffset = 0;
if(utext[c] >= ' ' && utext[c] <= '\x7f') {
for(y = 0; y < f->height; y++) {
blitoffset = lstart;
for(x = 0; x < (f->widths)[utext[c] - 32]; x++) {
if((f->fontData)[utext[c] - 32][dataoffset] == 0) {
if(!(flags & BTEXT_BGTRANSPARENT))
PIXELS(surface)[blitoffset] = bgval;
} else {
if(!(flags & BTEXT_FGTRANSPARENT))
PIXELS(surface)[blitoffset] = fgval;
}
blitoffset++;
dataoffset++;
}
lstart += surface->w;
}
start += (f->widths)[text[c] - 32];
}
}
SDL_UnlockSurface(surface);
return(surface);
}
int btext_renderToSurface(bfont *f, const char *text, Uint32 bgval, Uint32 fgval, SDL_Surface *surface, SDL_Rect *rect, Uint32 flags) {
Uint32 length;
Uint32 c, x, y;
Uint32 start, lstart, blitoffset;
Uint32 dataoffset;
unsigned char *utext;
if(rect->w == 0 || rect->w > surface->w - rect->x)
rect->w = surface->w - rect->x; /* clip to surface dimensions */
length = btext_clipTextToWidth(f, text, rect->w, 1);
if(length == 0)
return(-1);
if(SDL_LockSurface(surface) < 0)
return(-1);
utext = (unsigned char *)text;
start = (rect->y * surface->w) + rect->x;
for(c = 0; c < length; c++) {
lstart = start;
dataoffset = 0;
if(utext[c] >= ' ' && utext[c] <= '\x7f') {
for(y = 0; y < f->height; y++) {
blitoffset = lstart;
for(x = 0; x < (f->widths)[utext[c] - 32]; x++) {
if((f->fontData)[utext[c] - 32][dataoffset] == 0) {
if(!(flags & BTEXT_BGTRANSPARENT))
PIXELS(surface)[blitoffset] = bgval;
} else {
if(!(flags & BTEXT_FGTRANSPARENT))
PIXELS(surface)[blitoffset] = fgval;
}
blitoffset++;
dataoffset++;
}
lstart += surface->w;
}
start += (f->widths)[text[c] - 32];
}
}
SDL_UnlockSurface(surface);
return(0);
}
int btext_pixelAt(bfont *f, char c, Uint32 x, Uint32 y) {
unsigned char uc = (unsigned char)c;
if(uc < ' ' || uc > '\x7f')
return(-1);
if(x > f->widths[(unsigned int)uc - 32])
return(-1);
if(y > f->height)
return(-1);
return(f->fontData[(unsigned int)uc - 32][y * f->widths[(unsigned int)uc - 32] + x]);
}
void btext_charPoses(bfont *f, const char *text, Uint32 *pos) {
Uint32 accum = 0;
unsigned int i;
const unsigned char *utext = (const unsigned char *)text;
for(i = 0; utext[i] != '\0'; i++) {
pos[i] = accum;
if(utext[i] >= ' ' && utext[i] <= '\x7f')
accum += f->widths[utext[i] - 32];
else
accum += f->widths[95];
}
}
unsigned int btext_getWidth(bfont *f, char c) {
unsigned char uc = (unsigned char)c;
if(uc < ' ' || uc > '\x7f')
return(0);
return(f->widths[uc - 32]);
}