| 1 |
/* ______ ___ ___ |
| 2 |
* /\ _ \ /\_ \ /\_ \ |
| 3 |
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___ |
| 4 |
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\ |
| 5 |
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \ |
| 6 |
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/ |
| 7 |
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/ |
| 8 |
* /\____/ |
| 9 |
* \_/__/ |
| 10 |
* |
| 11 |
* Read font from a bitmap. |
| 12 |
* |
| 13 |
* By Shawn Hargreaves. |
| 14 |
* |
| 15 |
* See readme.txt for copyright information. |
| 16 |
*/ |
| 17 |
|
| 18 |
|
| 19 |
#define ALLEGRO_INTERNAL_UNSTABLE |
| 20 |
|
| 21 |
#include <string.h> |
| 22 |
|
| 23 |
#include "allegro5/allegro.h" |
| 24 |
|
| 25 |
#include "allegro5/allegro_font.h" |
| 26 |
#include "allegro5/internal/aintern_dtor.h" |
| 27 |
#include "allegro5/internal/aintern_system.h" |
| 28 |
|
| 29 |
#include "font.h" |
| 30 |
|
| 31 |
ALLEGRO_DEBUG_CHANNEL("font") |
| 32 |
|
| 33 |
|
| 34 |
static void font_find_character(uint32_t *data, int pitch, |
| 35 |
int bmp_w, int bmp_h, |
| 36 |
int *x, int *y, int *w, int *h) |
| 37 |
{ |
| 38 |
/* The pixel at position 0/0 is used as background color. */ |
| 39 |
uint32_t c = data[0]; |
| 40 |
pitch >>= 2; |
| 41 |
|
| 42 |
/* look for top left corner of character */ |
| 43 |
while (1) { |
| 44 |
/* Reached border? */ |
| 45 |
if (*x >= bmp_w - 1) { |
| 46 |
*x = 0; |
| 47 |
(*y)++; |
| 48 |
if (*y >= bmp_h - 1) { |
| 49 |
*w = 0; |
| 50 |
*h = 0; |
| 51 |
return; |
| 52 |
} |
| 53 |
} |
| 54 |
if ( |
| 55 |
data[*x + *y * pitch] == c && |
| 56 |
data[(*x + 1) + *y * pitch] == c && |
| 57 |
data[*x + (*y + 1) * pitch] == c && |
| 58 |
data[(*x + 1) + (*y + 1) * pitch] != c) { |
| 59 |
break; |
| 60 |
} |
| 61 |
(*x)++; |
| 62 |
} |
| 63 |
|
| 64 |
/* look for right edge of character */ |
| 65 |
*w = 1; |
| 66 |
while ((*x + *w + 1 < bmp_w) && |
| 67 |
data[(*x + *w + 1) + (*y + 1) * pitch] != c) { |
| 68 |
(*w)++; |
| 69 |
} |
| 70 |
|
| 71 |
/* look for bottom edge of character */ |
| 72 |
*h = 1; |
| 73 |
while ((*y + *h + 1 < bmp_h) && |
| 74 |
data[*x + 1 + (*y + *h + 1) * pitch] != c) { |
| 75 |
(*h)++; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
/* import_bitmap_font_color: |
| 82 |
* Helper for import_bitmap_font, below. |
| 83 |
*/ |
| 84 |
static int import_bitmap_font_color(uint32_t *data, int pitch, |
| 85 |
int bmp_w, int bmp_h, |
| 86 |
ALLEGRO_BITMAP **bits, ALLEGRO_BITMAP *glyphs, int num, |
| 87 |
int *import_x, int *import_y) |
| 88 |
{ |
| 89 |
int w, h, i; |
| 90 |
|
| 91 |
for (i = 0; i < num; i++) { |
| 92 |
font_find_character(data, pitch, bmp_w, bmp_h, |
| 93 |
import_x, import_y, &w, &h); |
| 94 |
if (w <= 0 || h <= 0) { |
| 95 |
ALLEGRO_ERROR("Unable to find character %d\n", i); |
| 96 |
return -1; |
| 97 |
} |
| 98 |
else { |
| 99 |
bits[i] = al_create_sub_bitmap(glyphs, |
| 100 |
*import_x + 1, *import_y + 1, w, h); |
| 101 |
*import_x += w; |
| 102 |
} |
| 103 |
} |
| 104 |
return 0; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
/* bitmap_font_count: |
| 111 |
* Helper for `import_bitmap_font', below. |
| 112 |
*/ |
| 113 |
static int bitmap_font_count(ALLEGRO_BITMAP* bmp) |
| 114 |
{ |
| 115 |
int x = 0, y = 0, w = 0, h = 0; |
| 116 |
int num = 0; |
| 117 |
ALLEGRO_LOCKED_REGION *lock; |
| 118 |
|
| 119 |
lock = al_lock_bitmap(bmp, ALLEGRO_PIXEL_FORMAT_RGBA_8888, |
| 120 |
ALLEGRO_LOCK_READONLY); |
| 121 |
|
| 122 |
while (1) { |
| 123 |
font_find_character(lock->data, lock->pitch, |
| 124 |
al_get_bitmap_width(bmp), al_get_bitmap_height(bmp), |
| 125 |
&x, &y, &w, &h); |
| 126 |
if (w <= 0 || h <= 0) |
| 127 |
break; |
| 128 |
num++; |
| 129 |
x += w; |
| 130 |
} |
| 131 |
|
| 132 |
al_unlock_bitmap(bmp); |
| 133 |
|
| 134 |
return num; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
ALLEGRO_FONT *_al_load_bitmap_font(const char *fname, int size, int font_flags) |
| 140 |
{ |
| 141 |
ALLEGRO_BITMAP *import_bmp; |
| 142 |
ALLEGRO_FONT *f; |
| 143 |
ALLEGRO_STATE backup; |
| 144 |
int range[2]; |
| 145 |
int bmp_flags; |
| 146 |
ASSERT(fname); |
| 147 |
|
| 148 |
(void)size; |
| 149 |
|
| 150 |
bmp_flags = 0; |
| 151 |
if (font_flags & ALLEGRO_NO_PREMULTIPLIED_ALPHA) { |
| 152 |
bmp_flags |= ALLEGRO_NO_PREMULTIPLIED_ALPHA; |
| 153 |
} |
| 154 |
|
| 155 |
al_store_state(&backup, ALLEGRO_STATE_NEW_BITMAP_PARAMETERS); |
| 156 |
al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP); |
| 157 |
al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_ANY_WITH_ALPHA); |
| 158 |
import_bmp = al_load_bitmap_flags(fname, bmp_flags); |
| 159 |
al_restore_state(&backup); |
| 160 |
|
| 161 |
if (!import_bmp) { |
| 162 |
ALLEGRO_ERROR("Couldn't load bitmap from '%s'\n", fname); |
| 163 |
return NULL; |
| 164 |
} |
| 165 |
|
| 166 |
/* We assume a single unicode range, starting at the space |
| 167 |
* character. |
| 168 |
*/ |
| 169 |
range[0] = 32; |
| 170 |
range[1] = 32 + bitmap_font_count(import_bmp) - 1; |
| 171 |
|
| 172 |
f = al_grab_font_from_bitmap(import_bmp, 1, range); |
| 173 |
|
| 174 |
al_destroy_bitmap(import_bmp); |
| 175 |
|
| 176 |
return f; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
/* Function: al_load_bitmap_font |
| 182 |
*/ |
| 183 |
ALLEGRO_FONT *al_load_bitmap_font(const char *fname) |
| 184 |
{ |
| 185 |
int flags = 0; |
| 186 |
|
| 187 |
/* For backwards compatibility with the 5.0 branch. */ |
| 188 |
if (al_get_new_bitmap_flags() & ALLEGRO_NO_PREMULTIPLIED_ALPHA) { |
| 189 |
flags |= ALLEGRO_NO_PREMULTIPLIED_ALPHA; |
| 190 |
} |
| 191 |
|
| 192 |
return _al_load_bitmap_font(fname, 0, flags); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
/* Function: al_load_bitmap_font_flags |
| 198 |
*/ |
| 199 |
ALLEGRO_FONT *al_load_bitmap_font_flags(const char *fname, int flags) |
| 200 |
{ |
| 201 |
return _al_load_bitmap_font(fname, 0, flags); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
/* Function: al_grab_font_from_bitmap |
| 207 |
*/ |
| 208 |
ALLEGRO_FONT *al_grab_font_from_bitmap(ALLEGRO_BITMAP *bmp, |
| 209 |
int ranges_n, const int ranges[]) |
| 210 |
{ |
| 211 |
ALLEGRO_FONT *f; |
| 212 |
ALLEGRO_FONT_COLOR_DATA *cf, *prev = NULL; |
| 213 |
ALLEGRO_STATE backup; |
| 214 |
int i; |
| 215 |
ALLEGRO_COLOR mask = al_get_pixel(bmp, 0, 0); |
| 216 |
ALLEGRO_BITMAP *glyphs = NULL, *unmasked = NULL; |
| 217 |
int import_x = 0, import_y = 0; |
| 218 |
ALLEGRO_LOCKED_REGION *lock = NULL; |
| 219 |
int w, h; |
| 220 |
|
| 221 |
ASSERT(bmp); |
| 222 |
|
| 223 |
w = al_get_bitmap_width(bmp); |
| 224 |
h = al_get_bitmap_height(bmp); |
| 225 |
|
| 226 |
f = al_calloc(1, sizeof *f); |
| 227 |
f->vtable = &_al_font_vtable_color; |
| 228 |
|
| 229 |
al_store_state(&backup, ALLEGRO_STATE_NEW_BITMAP_PARAMETERS); |
| 230 |
al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP); |
| 231 |
al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_ANY_WITH_ALPHA); |
| 232 |
unmasked = al_clone_bitmap(bmp); |
| 233 |
/* At least with OpenGL, texture pixels at the very border of |
| 234 |
* the glyph are sometimes partly sampled from the yellow mask |
| 235 |
* pixels. To work around this, we replace the mask with full |
| 236 |
* transparency. |
| 237 |
* And we best do it on a memory copy to avoid loading back a texture. |
| 238 |
*/ |
| 239 |
al_convert_mask_to_alpha(unmasked, mask); |
| 240 |
al_restore_state(&backup); |
| 241 |
|
| 242 |
al_store_state(&backup, ALLEGRO_STATE_BITMAP | ALLEGRO_STATE_BLENDER); |
| 243 |
// Use the users preferred format, so don't set this below! |
| 244 |
//al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_ANY_WITH_ALPHA); |
| 245 |
|
| 246 |
for (i = 0; i < ranges_n; i++) { |
| 247 |
int first = ranges[i * 2]; |
| 248 |
int last = ranges[i * 2 + 1]; |
| 249 |
int n = 1 + last - first; |
| 250 |
cf = al_calloc(1, sizeof(ALLEGRO_FONT_COLOR_DATA)); |
| 251 |
|
| 252 |
if (prev) |
| 253 |
prev->next = cf; |
| 254 |
else |
| 255 |
f->data = cf; |
| 256 |
|
| 257 |
cf->bitmaps = al_malloc(sizeof(ALLEGRO_BITMAP*) * n); |
| 258 |
cf->bitmaps[0] = NULL; |
| 259 |
|
| 260 |
if (!glyphs) { |
| 261 |
glyphs = al_clone_bitmap(unmasked); |
| 262 |
if (!glyphs) { |
| 263 |
ALLEGRO_ERROR("Unable clone bitmap.\n"); |
| 264 |
goto cleanup_and_fail_on_error; |
| 265 |
} |
| 266 |
|
| 267 |
lock = al_lock_bitmap(bmp, |
| 268 |
ALLEGRO_PIXEL_FORMAT_RGBA_8888, ALLEGRO_LOCK_READONLY); |
| 269 |
} |
| 270 |
cf->glyphs = glyphs; |
| 271 |
|
| 272 |
if (import_bitmap_font_color(lock->data, lock->pitch, w, h, |
| 273 |
cf->bitmaps, cf->glyphs, n, |
| 274 |
&import_x, &import_y)) { |
| 275 |
goto cleanup_and_fail_on_error; |
| 276 |
} |
| 277 |
else { |
| 278 |
cf->begin = first; |
| 279 |
cf->end = last + 1; |
| 280 |
prev = cf; |
| 281 |
} |
| 282 |
} |
| 283 |
al_restore_state(&backup); |
| 284 |
|
| 285 |
cf = f->data; |
| 286 |
if (cf && cf->bitmaps[0]) |
| 287 |
f->height = al_get_bitmap_height(cf->bitmaps[0]); |
| 288 |
|
| 289 |
if (lock) |
| 290 |
al_unlock_bitmap(bmp); |
| 291 |
|
| 292 |
if (unmasked) |
| 293 |
al_destroy_bitmap(unmasked); |
| 294 |
|
| 295 |
f->dtor_item = _al_register_destructor(_al_dtor_list, "font", f, |
| 296 |
(void (*)(void *))al_destroy_font); |
| 297 |
|
| 298 |
return f; |
| 299 |
|
| 300 |
cleanup_and_fail_on_error: |
| 301 |
|
| 302 |
if (lock) |
| 303 |
al_unlock_bitmap(bmp); |
| 304 |
al_restore_state(&backup); |
| 305 |
al_destroy_font(f); |
| 306 |
if (unmasked) |
| 307 |
al_destroy_bitmap(unmasked); |
| 308 |
return NULL; |
| 309 |
} |
| 310 |
|
| 311 |
/* vim: set sts=3 sw=3 et: */ |