| 1 |
/* ______ ___ ___ |
| 2 |
* /\ _ \ /\_ \ /\_ \ |
| 3 |
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___ |
| 4 |
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\ |
| 5 |
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \ |
| 6 |
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/ |
| 7 |
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/ |
| 8 |
* /\____/ |
| 9 |
* \_/__/ |
| 10 |
* |
| 11 |
* Text drawing routines. |
| 12 |
* |
| 13 |
* By Shawn Hargreaves. |
| 14 |
* |
| 15 |
* textout_justify() by Seymour Shlien. |
| 16 |
* |
| 17 |
* Laurence Withers added the textout_ex() function family. |
| 18 |
* |
| 19 |
* See readme.txt for copyright information. |
| 20 |
*/ |
| 21 |
|
| 22 |
|
| 23 |
#define ALLEGRO_INTERNAL_UNSTABLE |
| 24 |
|
| 25 |
#include <math.h> |
| 26 |
#include <ctype.h> |
| 27 |
#include "allegro5/allegro.h" |
| 28 |
|
| 29 |
#include "allegro5/allegro_font.h" |
| 30 |
#include "allegro5/internal/aintern_dtor.h" |
| 31 |
#include "allegro5/internal/aintern_font.h" |
| 32 |
#include "allegro5/internal/aintern_system.h" |
| 33 |
|
| 34 |
/* If you call this, you're probably making a mistake. */ |
| 35 |
/* |
| 36 |
#define strlen(s) __are_you_sure__ |
| 37 |
*/ |
| 38 |
/* Removed the above define since some compilers seem to use some |
| 39 |
* preprocessor magic when calling strcmp() that inserts a call to strlen. |
| 40 |
* There might be a better way to do this. |
| 41 |
*/ |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
/* Text usually looks best when aligned to pixels - |
| 46 |
* but if x is 0.5 it may very well end up at an integer |
| 47 |
* position if the current transformation scales by 2 or |
| 48 |
* translated x by 0.5. So we simply apply the transformation, |
| 49 |
* round to nearest integer, and backtransform that. |
| 50 |
*/ |
| 51 |
static void align_to_integer_pixel_inner( |
| 52 |
ALLEGRO_TRANSFORM const *fwd, |
| 53 |
ALLEGRO_TRANSFORM const *inv, |
| 54 |
float *x, float *y) |
| 55 |
{ |
| 56 |
al_transform_coordinates(fwd, x, y); |
| 57 |
*x = floorf(*x + 0.5f); |
| 58 |
*y = floorf(*y + 0.5f); |
| 59 |
al_transform_coordinates(inv, x, y); |
| 60 |
} |
| 61 |
|
| 62 |
static void align_to_integer_pixel(float *x, float *y) |
| 63 |
{ |
| 64 |
ALLEGRO_TRANSFORM const *fwd; |
| 65 |
ALLEGRO_TRANSFORM inv; |
| 66 |
|
| 67 |
fwd = al_get_current_transform(); |
| 68 |
al_copy_transform(&inv, fwd); |
| 69 |
al_invert_transform(&inv); |
| 70 |
align_to_integer_pixel_inner(fwd, &inv, x, y); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
/* Function: al_draw_ustr |
| 76 |
*/ |
| 77 |
void al_draw_ustr(const ALLEGRO_FONT *font, |
| 78 |
ALLEGRO_COLOR color, float x, float y, int flags, |
| 79 |
const ALLEGRO_USTR *ustr) |
| 80 |
{ |
| 81 |
ASSERT(font); |
| 82 |
ASSERT(ustr); |
| 83 |
|
| 84 |
if (flags & ALLEGRO_ALIGN_CENTRE) { |
| 85 |
/* Use integer division to avoid introducing a fractional |
| 86 |
* component to an integer x value. |
| 87 |
*/ |
| 88 |
x -= font->vtable->text_length(font, ustr) / 2; |
| 89 |
} |
| 90 |
else if (flags & ALLEGRO_ALIGN_RIGHT) { |
| 91 |
x -= font->vtable->text_length(font, ustr); |
| 92 |
} |
| 93 |
|
| 94 |
if (flags & ALLEGRO_ALIGN_INTEGER) |
| 95 |
align_to_integer_pixel(&x, &y); |
| 96 |
|
| 97 |
font->vtable->render(font, color, ustr, x, y); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
/* Function: al_draw_text |
| 103 |
*/ |
| 104 |
void al_draw_text(const ALLEGRO_FONT *font, |
| 105 |
ALLEGRO_COLOR color, float x, float y, int flags, |
| 106 |
char const *text) |
| 107 |
{ |
| 108 |
ALLEGRO_USTR_INFO info; |
| 109 |
ASSERT(text); |
| 110 |
al_draw_ustr(font, color, x, y, flags, al_ref_cstr(&info, text)); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
/* Function: al_draw_justified_ustr |
| 116 |
*/ |
| 117 |
void al_draw_justified_ustr(const ALLEGRO_FONT *font, |
| 118 |
ALLEGRO_COLOR color, float x1, float x2, |
| 119 |
float y, float diff, int flags, const ALLEGRO_USTR *ustr) |
| 120 |
{ |
| 121 |
const char *whitespace = " \t\n\r"; |
| 122 |
ALLEGRO_USTR_INFO word_info; |
| 123 |
const ALLEGRO_USTR *word; |
| 124 |
int pos1, pos2; |
| 125 |
int minlen; |
| 126 |
int num_words; |
| 127 |
int space; |
| 128 |
float fleft, finc; |
| 129 |
int advance; |
| 130 |
ALLEGRO_TRANSFORM const *fwd = NULL; |
| 131 |
ALLEGRO_TRANSFORM inv; |
| 132 |
|
| 133 |
ASSERT(font); |
| 134 |
|
| 135 |
/* count words and measure min length (without spaces) */ |
| 136 |
num_words = 0; |
| 137 |
minlen = 0; |
| 138 |
pos1 = 0; |
| 139 |
for (;;) { |
| 140 |
pos1 = al_ustr_find_cset_cstr(ustr, pos1, whitespace); |
| 141 |
if (pos1 == -1) |
| 142 |
break; |
| 143 |
pos2 = al_ustr_find_set_cstr(ustr, pos1, whitespace); |
| 144 |
if (pos2 == -1) |
| 145 |
pos2 = al_ustr_size(ustr); |
| 146 |
|
| 147 |
word = al_ref_ustr(&word_info, ustr, pos1, pos2); |
| 148 |
minlen += font->vtable->text_length(font, word); |
| 149 |
num_words++; |
| 150 |
|
| 151 |
pos1 = pos2; |
| 152 |
} |
| 153 |
|
| 154 |
/* amount of room for space between words */ |
| 155 |
space = x2 - x1 - minlen; |
| 156 |
|
| 157 |
if ((space <= 0) || (space > diff) || (num_words < 2)) { |
| 158 |
/* can't justify */ |
| 159 |
if (flags & ALLEGRO_ALIGN_INTEGER) |
| 160 |
align_to_integer_pixel(&x1, &y); |
| 161 |
font->vtable->render(font, color, ustr, x1, y); |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
/* distribute space left evenly between words */ |
| 166 |
fleft = (float)x1; |
| 167 |
finc = (float)space / (float)(num_words-1); |
| 168 |
pos1 = 0; |
| 169 |
|
| 170 |
if (flags & ALLEGRO_ALIGN_INTEGER) { |
| 171 |
fwd = al_get_current_transform(); |
| 172 |
al_copy_transform(&inv, fwd); |
| 173 |
al_invert_transform(&inv); |
| 174 |
} |
| 175 |
|
| 176 |
for (;;) { |
| 177 |
pos1 = al_ustr_find_cset_cstr(ustr, pos1, whitespace); |
| 178 |
if (pos1 == -1) |
| 179 |
break; |
| 180 |
pos2 = al_ustr_find_set_cstr(ustr, pos1, whitespace); |
| 181 |
if (pos2 == -1) |
| 182 |
pos2 = al_ustr_size(ustr); |
| 183 |
|
| 184 |
word = al_ref_ustr(&word_info, ustr, pos1, pos2); |
| 185 |
if (flags & ALLEGRO_ALIGN_INTEGER) { |
| 186 |
float drawx = fleft; |
| 187 |
float drawy = y; |
| 188 |
align_to_integer_pixel_inner(fwd, &inv, &drawx, &drawy); |
| 189 |
advance = font->vtable->render(font, color, word, drawx, drawy); |
| 190 |
} |
| 191 |
else { |
| 192 |
advance = font->vtable->render(font, color, word, fleft, y); |
| 193 |
} |
| 194 |
fleft += advance + finc; |
| 195 |
pos1 = pos2; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
/* Function: al_draw_justified_text |
| 201 |
*/ |
| 202 |
void al_draw_justified_text(const ALLEGRO_FONT *font, |
| 203 |
ALLEGRO_COLOR color, float x1, float x2, |
| 204 |
float y, float diff, int flags, const char *text) |
| 205 |
{ |
| 206 |
ALLEGRO_USTR_INFO info; |
| 207 |
ASSERT(text); |
| 208 |
al_draw_justified_ustr(font, color, x1, x2, y, diff, flags, |
| 209 |
al_ref_cstr(&info, text)); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
/* Function: al_draw_textf |
| 214 |
*/ |
| 215 |
void al_draw_textf(const ALLEGRO_FONT *font, ALLEGRO_COLOR color, |
| 216 |
float x, float y, int flags, |
| 217 |
const char *format, ...) |
| 218 |
{ |
| 219 |
ALLEGRO_USTR *buf; |
| 220 |
va_list ap; |
| 221 |
const char *s; |
| 222 |
ASSERT(font); |
| 223 |
ASSERT(format); |
| 224 |
|
| 225 |
/* Fast path for common case. */ |
| 226 |
if (0 == strcmp(format, "%s")) { |
| 227 |
va_start(ap, format); |
| 228 |
s = va_arg(ap, const char *); |
| 229 |
al_draw_text(font, color, x, y, flags, s); |
| 230 |
va_end(ap); |
| 231 |
return; |
| 232 |
} |
| 233 |
|
| 234 |
va_start(ap, format); |
| 235 |
buf = al_ustr_new(""); |
| 236 |
al_ustr_vappendf(buf, format, ap); |
| 237 |
va_end(ap); |
| 238 |
|
| 239 |
al_draw_ustr(font, color, x, y, flags, buf); |
| 240 |
|
| 241 |
al_ustr_free(buf); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
/* Function: al_draw_justified_textf |
| 247 |
*/ |
| 248 |
void al_draw_justified_textf(const ALLEGRO_FONT *f, |
| 249 |
ALLEGRO_COLOR color, float x1, float x2, float y, |
| 250 |
float diff, int flags, const char *format, ...) |
| 251 |
{ |
| 252 |
ALLEGRO_USTR *buf; |
| 253 |
va_list ap; |
| 254 |
ASSERT(f); |
| 255 |
ASSERT(format); |
| 256 |
|
| 257 |
va_start(ap, format); |
| 258 |
buf = al_ustr_new(""); |
| 259 |
al_ustr_vappendf(buf, format, ap); |
| 260 |
va_end(ap); |
| 261 |
|
| 262 |
al_draw_justified_ustr(f, color, x1, x2, y, diff, flags, buf); |
| 263 |
|
| 264 |
al_ustr_free(buf); |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
/* Function: al_get_ustr_width |
| 270 |
*/ |
| 271 |
int al_get_ustr_width(const ALLEGRO_FONT *f, ALLEGRO_USTR const *ustr) |
| 272 |
{ |
| 273 |
ASSERT(f); |
| 274 |
ASSERT(ustr); |
| 275 |
|
| 276 |
return f->vtable->text_length(f, ustr); |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
/* Function: al_get_text_width |
| 282 |
*/ |
| 283 |
int al_get_text_width(const ALLEGRO_FONT *f, const char *str) |
| 284 |
{ |
| 285 |
ALLEGRO_USTR_INFO str_info; |
| 286 |
const ALLEGRO_USTR *ustr; |
| 287 |
ASSERT(f); |
| 288 |
ASSERT(str); |
| 289 |
|
| 290 |
ustr = al_ref_cstr(&str_info, str); |
| 291 |
|
| 292 |
return f->vtable->text_length(f, ustr); |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
/* Function: al_get_font_line_height |
| 298 |
*/ |
| 299 |
int al_get_font_line_height(const ALLEGRO_FONT *f) |
| 300 |
{ |
| 301 |
ASSERT(f); |
| 302 |
return f->vtable->font_height(f); |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
/* Function: al_get_font_ascent |
| 308 |
*/ |
| 309 |
int al_get_font_ascent(const ALLEGRO_FONT *f) |
| 310 |
{ |
| 311 |
ASSERT(f); |
| 312 |
return f->vtable->font_ascent(f); |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
/* Function: al_get_font_descent |
| 318 |
*/ |
| 319 |
int al_get_font_descent(const ALLEGRO_FONT *f) |
| 320 |
{ |
| 321 |
ASSERT(f); |
| 322 |
return f->vtable->font_descent(f); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
/* Function: al_get_ustr_dimensions |
| 328 |
*/ |
| 329 |
void al_get_ustr_dimensions(const ALLEGRO_FONT *f, |
| 330 |
ALLEGRO_USTR const *ustr, |
| 331 |
int *bbx, int *bby, int *bbw, int *bbh) |
| 332 |
{ |
| 333 |
ASSERT(f); |
| 334 |
ASSERT(ustr); |
| 335 |
f->vtable->get_text_dimensions(f, ustr, bbx, bby, |
| 336 |
bbw, bbh); |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
/* Function: al_get_text_dimensions |
| 342 |
*/ |
| 343 |
void al_get_text_dimensions(const ALLEGRO_FONT *f, |
| 344 |
char const *text, |
| 345 |
int *bbx, int *bby, int *bbw, int *bbh) |
| 346 |
{ |
| 347 |
ALLEGRO_USTR_INFO info; |
| 348 |
ASSERT(f); |
| 349 |
ASSERT(text); |
| 350 |
|
| 351 |
f->vtable->get_text_dimensions(f, al_ref_cstr(&info, text), bbx, bby, |
| 352 |
bbw, bbh); |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
/* Function: al_destroy_font |
| 358 |
*/ |
| 359 |
void al_destroy_font(ALLEGRO_FONT *f) |
| 360 |
{ |
| 361 |
if (!f) |
| 362 |
return; |
| 363 |
|
| 364 |
_al_unregister_destructor(_al_dtor_list, f->dtor_item); |
| 365 |
|
| 366 |
f->vtable->destroy(f); |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
/* Function: al_get_font_ranges |
| 371 |
*/ |
| 372 |
int al_get_font_ranges(ALLEGRO_FONT *f, int ranges_count, int *ranges) |
| 373 |
{ |
| 374 |
return f->vtable->get_font_ranges(f, ranges_count, ranges); |
| 375 |
} |
| 376 |
|
| 377 |
/* Function: al_draw_glyph |
| 378 |
*/ |
| 379 |
void al_draw_glyph(const ALLEGRO_FONT *f, ALLEGRO_COLOR color, float x, float y, |
| 380 |
int codepoint) |
| 381 |
{ |
| 382 |
f->vtable->render_char(f, color, codepoint, x, y); |
| 383 |
}; |
| 384 |
|
| 385 |
/* Function: al_get_glyph_width |
| 386 |
*/ |
| 387 |
int al_get_glyph_width(const ALLEGRO_FONT *f, int codepoint) |
| 388 |
{ |
| 389 |
return f->vtable->char_length(f, codepoint); |
| 390 |
} |
| 391 |
|
| 392 |
/* Function: al_get_glyph_dimensions |
| 393 |
*/ |
| 394 |
bool al_get_glyph_dimensions(const ALLEGRO_FONT *f, |
| 395 |
int codepoint, int *bbx, int *bby, int *bbw, int *bbh) |
| 396 |
{ |
| 397 |
return f->vtable->get_glyph_dimensions(f, codepoint, bbx, bby, bbw, bbh); |
| 398 |
} |
| 399 |
|
| 400 |
/* Function: al_get_glyph_advance |
| 401 |
*/ |
| 402 |
int al_get_glyph_advance(const ALLEGRO_FONT *f, int codepoint1, int codepoint2) |
| 403 |
{ |
| 404 |
return f->vtable->get_glyph_advance(f, codepoint1, codepoint2); |
| 405 |
} |
| 406 |
|
| 407 |
/* Function: al_get_glyph |
| 408 |
*/ |
| 409 |
bool al_get_glyph(const ALLEGRO_FONT *f, int prev_codepoint, int codepoint, ALLEGRO_GLYPH *glyph) |
| 410 |
{ |
| 411 |
return f->vtable->get_glyph(f, prev_codepoint, codepoint, glyph); |
| 412 |
}; |
| 413 |
|
| 414 |
|
| 415 |
/* This helper function helps splitting an ustr in several delimited parts. |
| 416 |
* It returns an ustr that refers to the next part of the string that |
| 417 |
* is delimited by the delimiters in delimiter. |
| 418 |
* Returns NULL at the end of the string. |
| 419 |
* Pos is updated to byte index of character after the delimiter or |
| 420 |
* to the end of the string. |
| 421 |
*/ |
| 422 |
static const ALLEGRO_USTR *ustr_split_next(const ALLEGRO_USTR *ustr, |
| 423 |
ALLEGRO_USTR_INFO *info, int *pos, const char *delimiter) |
| 424 |
{ |
| 425 |
const ALLEGRO_USTR *result; |
| 426 |
int end, size; |
| 427 |
|
| 428 |
size = al_ustr_size(ustr); |
| 429 |
if (*pos >= size) { |
| 430 |
return NULL; |
| 431 |
} |
| 432 |
|
| 433 |
end = al_ustr_find_set_cstr(ustr, *pos, delimiter); |
| 434 |
if (end == -1) |
| 435 |
end = size; |
| 436 |
|
| 437 |
result = al_ref_ustr(info, ustr, *pos, end); |
| 438 |
/* Set pos to character AFTER delimiter */ |
| 439 |
al_ustr_next(ustr, &end); |
| 440 |
(*pos) = end; |
| 441 |
return result; |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
/* This returns the next "soft" line of text from ustr |
| 447 |
* that will fit in max_with using the font font, starting at pos *pos. |
| 448 |
* These are "soft" lines because they are broken up if needed at a space |
| 449 |
* or tab character. |
| 450 |
* This function updates pos if needed, and returns the next "soft" line, |
| 451 |
* or NULL if no more soft lines. |
| 452 |
* The soft line will not include the trailing space where the |
| 453 |
* line was split, but pos will be set to point to after that trailing |
| 454 |
* space so iteration can continue easily. |
| 455 |
*/ |
| 456 |
static const ALLEGRO_USTR *get_next_soft_line(const ALLEGRO_USTR *ustr, |
| 457 |
ALLEGRO_USTR_INFO *info, int *pos, |
| 458 |
const ALLEGRO_FONT *font, float max_width) |
| 459 |
{ |
| 460 |
const ALLEGRO_USTR *result = NULL; |
| 461 |
const char *whitespace = " \t"; |
| 462 |
int old_end = 0; |
| 463 |
int end = 0; |
| 464 |
int size = al_ustr_size(ustr); |
| 465 |
bool first_word = true; |
| 466 |
|
| 467 |
if (*pos >= size) { |
| 468 |
return NULL; |
| 469 |
} |
| 470 |
|
| 471 |
end = *pos; |
| 472 |
old_end = end; |
| 473 |
do { |
| 474 |
/* On to the next word. */ |
| 475 |
end = al_ustr_find_set_cstr(ustr, end, whitespace); |
| 476 |
if (end < 0) |
| 477 |
end = size; |
| 478 |
|
| 479 |
/* Reference to the line that is being built. */ |
| 480 |
result = al_ref_ustr(info, ustr, *pos, end); |
| 481 |
|
| 482 |
/* Check if the line is too long. If it is, return a soft line. */ |
| 483 |
if (al_get_ustr_width(font, result) > max_width) { |
| 484 |
/* Corner case: a single word may not even fit the line. |
| 485 |
* In that case, return the word/line anyway as the "soft line", |
| 486 |
* the user can set a clip rectangle to cut it. */ |
| 487 |
|
| 488 |
if (first_word) { |
| 489 |
/* Set pos to character AFTER end to allow easy iteration. */ |
| 490 |
al_ustr_next(ustr, &end); |
| 491 |
*pos = end; |
| 492 |
return result; |
| 493 |
} |
| 494 |
else { |
| 495 |
/* Not first word, return old end position without the new word */ |
| 496 |
result = al_ref_ustr(info, ustr, *pos, old_end); |
| 497 |
/* Set pos to character AFTER end to allow easy iteration. */ |
| 498 |
al_ustr_next(ustr, &old_end); |
| 499 |
*pos = old_end; |
| 500 |
return result; |
| 501 |
} |
| 502 |
} |
| 503 |
first_word = false; |
| 504 |
old_end = end; |
| 505 |
/* Skip the character at end which normally is whitespace. */ |
| 506 |
al_ustr_next(ustr, &end); |
| 507 |
} while (end < size); |
| 508 |
|
| 509 |
/* If we get here the whole ustr will fit.*/ |
| 510 |
result = al_ref_ustr(info, ustr, *pos, size); |
| 511 |
*pos = size; |
| 512 |
return result; |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
/* Function: al_do_multiline_ustr |
| 517 |
*/ |
| 518 |
void al_do_multiline_ustr(const ALLEGRO_FONT *font, float max_width, |
| 519 |
const ALLEGRO_USTR *ustr, |
| 520 |
bool (*cb)(int line_num, const ALLEGRO_USTR * line, void *extra), |
| 521 |
void *extra) |
| 522 |
{ |
| 523 |
const char *linebreak = "\n"; |
| 524 |
const ALLEGRO_USTR *hard_line, *soft_line; |
| 525 |
ALLEGRO_USTR_INFO hard_line_info, soft_line_info; |
| 526 |
int hard_line_pos = 0, soft_line_pos = 0; |
| 527 |
int line_num = 0; |
| 528 |
bool proceed; |
| 529 |
|
| 530 |
/* For every "hard" line separated by a newline character... */ |
| 531 |
hard_line = ustr_split_next(ustr, &hard_line_info, &hard_line_pos, |
| 532 |
linebreak); |
| 533 |
while (hard_line) { |
| 534 |
/* For every "soft" line in the "hard" line... */ |
| 535 |
soft_line_pos = 0; |
| 536 |
soft_line = |
| 537 |
get_next_soft_line(hard_line, &soft_line_info, &soft_line_pos, font, |
| 538 |
max_width); |
| 539 |
/* No soft line here because it's an empty hard line. */ |
| 540 |
if (!soft_line) { |
| 541 |
/* Call the callback with empty string to indicate an empty line. */ |
| 542 |
proceed = cb(line_num, al_ustr_empty_string(), extra); |
| 543 |
if (!proceed) return; |
| 544 |
line_num ++; |
| 545 |
} |
| 546 |
while(soft_line) { |
| 547 |
/* Call the callback on the next soft line. */ |
| 548 |
proceed = cb(line_num, soft_line, extra); |
| 549 |
if (!proceed) return; |
| 550 |
line_num++; |
| 551 |
|
| 552 |
soft_line = get_next_soft_line(hard_line, &soft_line_info, |
| 553 |
&soft_line_pos, font, max_width); |
| 554 |
} |
| 555 |
hard_line = ustr_split_next(ustr, &hard_line_info, &hard_line_pos, |
| 556 |
linebreak); |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
/* Helper struct for al_do_multiline_text. */ |
| 563 |
typedef struct DO_MULTILINE_TEXT_EXTRA { |
| 564 |
bool (*callback)(int line_num, const char *line, int size, void *extra); |
| 565 |
void *extra; |
| 566 |
} DO_MULTILINE_TEXT_EXTRA; |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
/* The functions do_multiline_text_cb is the helper callback |
| 571 |
* that "adapts" al_do_multiline_ustr to al_do_multiline_text. |
| 572 |
*/ |
| 573 |
static bool do_multiline_text_cb(int line_num, const ALLEGRO_USTR *line, |
| 574 |
void *extra) { |
| 575 |
DO_MULTILINE_TEXT_EXTRA *s = extra; |
| 576 |
|
| 577 |
return s->callback(line_num, al_cstr(line), al_ustr_size(line), s->extra); |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
/* Function: al_do_multiline_text |
| 583 |
*/ |
| 584 |
void al_do_multiline_text(const ALLEGRO_FONT *font, |
| 585 |
float max_width, const char *text, |
| 586 |
bool (*cb)(int line_num, const char *line, int size, void *extra), |
| 587 |
void *extra) |
| 588 |
{ |
| 589 |
ALLEGRO_USTR_INFO info; |
| 590 |
DO_MULTILINE_TEXT_EXTRA extra2; |
| 591 |
ASSERT(font); |
| 592 |
ASSERT(text); |
| 593 |
|
| 594 |
extra2.callback = cb; |
| 595 |
extra2.extra = extra; |
| 596 |
al_do_multiline_ustr(font, max_width, al_ref_cstr(&info, text), |
| 597 |
do_multiline_text_cb, &extra2); |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
/* Helper struct for al_draw_multiline_ustr. */ |
| 603 |
typedef struct DRAW_MULTILINE_USTR_EXTRA { |
| 604 |
const ALLEGRO_FONT *font; |
| 605 |
ALLEGRO_COLOR color; |
| 606 |
float x; |
| 607 |
float y; |
| 608 |
float line_height; |
| 609 |
int flags; |
| 610 |
} DRAW_MULTILINE_USTR_EXTRA; |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
/* The function draw_multiline_ustr_cb is the helper callback |
| 615 |
* that implements the actual drawing for al_draw_multiline_ustr. |
| 616 |
*/ |
| 617 |
static bool draw_multiline_ustr_cb(int line_num, const ALLEGRO_USTR *line, |
| 618 |
void *extra) { |
| 619 |
DRAW_MULTILINE_USTR_EXTRA *s = extra; |
| 620 |
float y; |
| 621 |
|
| 622 |
y = s->y + (s->line_height * line_num); |
| 623 |
al_draw_ustr(s->font, s->color, s->x, y, s->flags, line); |
| 624 |
return true; |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
/* Function: al_draw_multiline_ustr |
| 630 |
*/ |
| 631 |
void al_draw_multiline_ustr(const ALLEGRO_FONT *font, |
| 632 |
ALLEGRO_COLOR color, float x, float y, float max_width, float line_height, |
| 633 |
int flags, const ALLEGRO_USTR *ustr) |
| 634 |
{ |
| 635 |
DRAW_MULTILINE_USTR_EXTRA extra; |
| 636 |
ASSERT(font); |
| 637 |
ASSERT(ustr); |
| 638 |
|
| 639 |
extra.font = font; |
| 640 |
extra.color = color; |
| 641 |
extra.x = x; |
| 642 |
extra.y = y; |
| 643 |
if (line_height < 1) { |
| 644 |
extra.line_height = al_get_font_line_height(font); |
| 645 |
} |
| 646 |
else { |
| 647 |
extra.line_height = line_height; |
| 648 |
} |
| 649 |
extra.flags = flags; |
| 650 |
|
| 651 |
al_do_multiline_ustr(font, max_width, ustr, draw_multiline_ustr_cb, &extra); |
| 652 |
} |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
/* Function: al_draw_multiline_text |
| 657 |
*/ |
| 658 |
void al_draw_multiline_text(const ALLEGRO_FONT *font, |
| 659 |
ALLEGRO_COLOR color, float x, float y, float max_width, float line_height, |
| 660 |
int flags, const char *text) |
| 661 |
{ |
| 662 |
ALLEGRO_USTR_INFO info; |
| 663 |
ASSERT(font); |
| 664 |
ASSERT(text); |
| 665 |
|
| 666 |
al_draw_multiline_ustr(font, color, x, y, max_width, line_height, flags, |
| 667 |
al_ref_cstr(&info, text)); |
| 668 |
} |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
/* Function: al_draw_multiline_textf |
| 673 |
*/ |
| 674 |
void al_draw_multiline_textf(const ALLEGRO_FONT *font, |
| 675 |
ALLEGRO_COLOR color, float x, float y, float max_width, float line_height, |
| 676 |
int flags, const char *format, ...) |
| 677 |
{ |
| 678 |
ALLEGRO_USTR *buf; |
| 679 |
va_list ap; |
| 680 |
ASSERT(font); |
| 681 |
ASSERT(format); |
| 682 |
|
| 683 |
va_start(ap, format); |
| 684 |
buf = al_ustr_new(""); |
| 685 |
al_ustr_vappendf(buf, format, ap); |
| 686 |
va_end(ap); |
| 687 |
|
| 688 |
al_draw_multiline_ustr(font, color, x, y, max_width, line_height, flags, |
| 689 |
buf); |
| 690 |
|
| 691 |
al_ustr_free(buf); |
| 692 |
} |
| 693 |
|
| 694 |
|
| 695 |
/* Function: al_set_fallback_font |
| 696 |
*/ |
| 697 |
void al_set_fallback_font(ALLEGRO_FONT *font, ALLEGRO_FONT *fallback) |
| 698 |
{ |
| 699 |
font->fallback = fallback; |
| 700 |
} |
| 701 |
|
| 702 |
/* Function: al_get_fallback_font |
| 703 |
*/ |
| 704 |
ALLEGRO_FONT *al_get_fallback_font(ALLEGRO_FONT *font) |
| 705 |
{ |
| 706 |
return font->fallback; |
| 707 |
} |
| 708 |
|
| 709 |
|
| 710 |
/* vim: set sts=3 sw=3 et: */ |