/
fontbmp.c
309 lines (245 loc) · 7.47 KB
/
fontbmp.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
308
309
/* ______ ___ ___
* /\ _ \ /\_ \ /\_ \
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
* /\____/
* \_/__/
*
* Read font from a bitmap.
*
* By Shawn Hargreaves.
*
* See readme.txt for copyright information.
*/
#include <string.h>
#include "allegro5/allegro.h"
#include "allegro5/allegro_font.h"
#include "allegro5/internal/aintern_dtor.h"
#include "allegro5/internal/aintern_system.h"
#include "font.h"
ALLEGRO_DEBUG_CHANNEL("font")
static void font_find_character(uint32_t *data, int pitch,
int bmp_w, int bmp_h,
int *x, int *y, int *w, int *h)
{
/* The pixel at position 0/0 is used as background color. */
uint32_t c = data[0];
pitch >>= 2;
/* look for top left corner of character */
while (1) {
/* Reached border? */
if (*x >= bmp_w - 1) {
*x = 0;
(*y)++;
if (*y >= bmp_h - 1) {
*w = 0;
*h = 0;
return;
}
}
if (
data[*x + *y * pitch] == c &&
data[(*x + 1) + *y * pitch] == c &&
data[*x + (*y + 1) * pitch] == c &&
data[(*x + 1) + (*y + 1) * pitch] != c) {
break;
}
(*x)++;
}
/* look for right edge of character */
*w = 1;
while ((*x + *w + 1 < bmp_w) &&
data[(*x + *w + 1) + (*y + 1) * pitch] != c) {
(*w)++;
}
/* look for bottom edge of character */
*h = 1;
while ((*y + *h + 1 < bmp_h) &&
data[*x + 1 + (*y + *h + 1) * pitch] != c) {
(*h)++;
}
}
/* import_bitmap_font_color:
* Helper for import_bitmap_font, below.
*/
static int import_bitmap_font_color(uint32_t *data, int pitch,
int bmp_w, int bmp_h,
ALLEGRO_BITMAP **bits, ALLEGRO_BITMAP *glyphs, int num,
int *import_x, int *import_y)
{
int w, h, i;
for (i = 0; i < num; i++) {
font_find_character(data, pitch, bmp_w, bmp_h,
import_x, import_y, &w, &h);
if (w <= 0 || h <= 0) {
ALLEGRO_ERROR("Unable to find character %d\n", i);
return -1;
}
else {
bits[i] = al_create_sub_bitmap(glyphs,
*import_x + 1, *import_y + 1, w, h);
*import_x += w;
}
}
return 0;
}
/* bitmap_font_count:
* Helper for `import_bitmap_font', below.
*/
static int bitmap_font_count(ALLEGRO_BITMAP* bmp)
{
int x = 0, y = 0, w = 0, h = 0;
int num = 0;
ALLEGRO_LOCKED_REGION *lock;
lock = al_lock_bitmap(bmp, ALLEGRO_PIXEL_FORMAT_RGBA_8888,
ALLEGRO_LOCK_READONLY);
while (1) {
font_find_character(lock->data, lock->pitch,
al_get_bitmap_width(bmp), al_get_bitmap_height(bmp),
&x, &y, &w, &h);
if (w <= 0 || h <= 0)
break;
num++;
x += w;
}
al_unlock_bitmap(bmp);
return num;
}
ALLEGRO_FONT *_al_load_bitmap_font(const char *fname, int size, int font_flags)
{
ALLEGRO_BITMAP *import_bmp;
ALLEGRO_FONT *f;
ALLEGRO_STATE backup;
int range[2];
int bmp_flags;
ASSERT(fname);
(void)size;
bmp_flags = 0;
if (font_flags & ALLEGRO_NO_PREMULTIPLIED_ALPHA) {
bmp_flags |= ALLEGRO_NO_PREMULTIPLIED_ALPHA;
}
al_store_state(&backup, ALLEGRO_STATE_NEW_BITMAP_PARAMETERS);
al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_ANY_WITH_ALPHA);
import_bmp = al_load_bitmap_flags(fname, bmp_flags);
al_restore_state(&backup);
if (!import_bmp) {
ALLEGRO_ERROR("Couldn't load bitmap from '%s'\n", fname);
return NULL;
}
/* We assume a single unicode range, starting at the space
* character.
*/
range[0] = 32;
range[1] = 32 + bitmap_font_count(import_bmp) - 1;
f = al_grab_font_from_bitmap(import_bmp, 1, range);
al_destroy_bitmap(import_bmp);
return f;
}
/* Function: al_load_bitmap_font
*/
ALLEGRO_FONT *al_load_bitmap_font(const char *fname)
{
int flags = 0;
/* For backwards compatibility with the 5.0 branch. */
if (al_get_new_bitmap_flags() & ALLEGRO_NO_PREMULTIPLIED_ALPHA) {
flags |= ALLEGRO_NO_PREMULTIPLIED_ALPHA;
}
return _al_load_bitmap_font(fname, 0, flags);
}
/* Function: al_load_bitmap_font_flags
*/
ALLEGRO_FONT *al_load_bitmap_font_flags(const char *fname, int flags)
{
return _al_load_bitmap_font(fname, 0, flags);
}
/* Function: al_grab_font_from_bitmap
*/
ALLEGRO_FONT *al_grab_font_from_bitmap(ALLEGRO_BITMAP *bmp,
int ranges_n, const int ranges[])
{
ALLEGRO_FONT *f;
ALLEGRO_FONT_COLOR_DATA *cf, *prev = NULL;
ALLEGRO_STATE backup;
int i;
ALLEGRO_COLOR mask = al_get_pixel(bmp, 0, 0);
ALLEGRO_BITMAP *glyphs = NULL, *unmasked = NULL;
int import_x = 0, import_y = 0;
ALLEGRO_LOCKED_REGION *lock = NULL;
int w, h;
ASSERT(bmp);
w = al_get_bitmap_width(bmp);
h = al_get_bitmap_height(bmp);
f = al_calloc(1, sizeof *f);
f->vtable = &_al_font_vtable_color;
al_store_state(&backup, ALLEGRO_STATE_NEW_BITMAP_PARAMETERS);
al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_ANY_WITH_ALPHA);
unmasked = al_clone_bitmap(bmp);
/* At least with OpenGL, texture pixels at the very border of
* the glyph are sometimes partly sampled from the yellow mask
* pixels. To work around this, we replace the mask with full
* transparency.
* And we best do it on a memory copy to avoid loading back a texture.
*/
al_convert_mask_to_alpha(unmasked, mask);
al_restore_state(&backup);
al_store_state(&backup, ALLEGRO_STATE_BITMAP | ALLEGRO_STATE_BLENDER);
// Use the users preferred format, so don't set this below!
//al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_ANY_WITH_ALPHA);
for (i = 0; i < ranges_n; i++) {
int first = ranges[i * 2];
int last = ranges[i * 2 + 1];
int n = 1 + last - first;
cf = al_calloc(1, sizeof(ALLEGRO_FONT_COLOR_DATA));
if (prev)
prev->next = cf;
else
f->data = cf;
cf->bitmaps = al_malloc(sizeof(ALLEGRO_BITMAP*) * n);
cf->bitmaps[0] = NULL;
if (!glyphs) {
glyphs = al_clone_bitmap(unmasked);
if (!glyphs) {
ALLEGRO_ERROR("Unable clone bitmap.\n");
goto cleanup_and_fail_on_error;
}
lock = al_lock_bitmap(bmp,
ALLEGRO_PIXEL_FORMAT_RGBA_8888, ALLEGRO_LOCK_READONLY);
}
cf->glyphs = glyphs;
if (import_bitmap_font_color(lock->data, lock->pitch, w, h,
cf->bitmaps, cf->glyphs, n,
&import_x, &import_y)) {
goto cleanup_and_fail_on_error;
}
else {
cf->begin = first;
cf->end = last + 1;
prev = cf;
}
}
al_restore_state(&backup);
cf = f->data;
if (cf && cf->bitmaps[0])
f->height = al_get_bitmap_height(cf->bitmaps[0]);
if (lock)
al_unlock_bitmap(bmp);
if (unmasked)
al_destroy_bitmap(unmasked);
f->dtor_item = _al_register_destructor(_al_dtor_list, "font", f,
(void (*)(void *))al_destroy_font);
return f;
cleanup_and_fail_on_error:
if (lock)
al_unlock_bitmap(bmp);
al_restore_state(&backup);
al_destroy_font(f);
if (unmasked)
al_destroy_bitmap(unmasked);
return NULL;
}
/* vim: set sts=3 sw=3 et: */