| 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 |
#include "font.h" |
| 27 |
|
| 28 |
#include <stdio.h> |
| 29 |
#include <string.h> |
| 30 |
|
| 31 |
#include "blit.h" |
| 32 |
#include "log.h" |
| 33 |
#include "pic.h" |
| 34 |
#include "sys_config.h" |
| 35 |
#include "utils.h" |
| 36 |
|
| 37 |
#define FIRST_CHAR 0 |
| 38 |
#define LAST_CHAR 255 |
| 39 |
|
| 40 |
Font gFont; |
| 41 |
|
| 42 |
FontOpts FontOptsNew(void) |
| 43 |
{ |
| 44 |
FontOpts opts; |
| 45 |
memset(&opts, 0, sizeof opts); |
| 46 |
opts.Mask = colorWhite; |
| 47 |
return opts; |
| 48 |
} |
| 49 |
|
| 50 |
void FontLoad( |
| 51 |
Font *f, const char *imgPath, const bool isProportional, |
| 52 |
const struct vec2i spaceSize) |
| 53 |
{ |
| 54 |
char buf[CDOGS_PATH_MAX]; |
| 55 |
GetDataFilePath(buf, imgPath); |
| 56 |
SDL_Surface *image = LoadImgToSurface(buf); |
| 57 |
if (!image) |
| 58 |
{ |
| 59 |
LOG(LM_GFX, LL_ERROR, "Cannot load font image"); |
| 60 |
return; |
| 61 |
} |
| 62 |
if (image->format->BytesPerPixel != 4) |
| 63 |
{ |
| 64 |
perror("Cannot load non-32-bit image"); |
| 65 |
LOG(LM_GFX, LL_ERROR, "Only 32-bit depth images supported"); |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
CArrayInit(&f->Chars, sizeof(Pic)); |
| 70 |
|
| 71 |
|
| 72 |
const struct vec2i step = svec2i( |
| 73 |
f->Size.x + f->Padding.Left + f->Padding.Right, |
| 74 |
f->Size.y + f->Padding.Top + f->Padding.Bottom); |
| 75 |
if (step.x * f->Stride > image->w || step.y > image->h) |
| 76 |
{ |
| 77 |
LOG(LM_GFX, LL_ERROR, |
| 78 |
"Error: font image not big enough for font data " |
| 79 |
"Image %dx%d Size %dx%d Stride %d Padding %d,%d,%d,%d", |
| 80 |
image->w, image->h, f->Size.x, f->Size.y, f->Stride, |
| 81 |
f->Padding.Left, f->Padding.Top, f->Padding.Right, |
| 82 |
f->Padding.Bottom); |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
SDL_LockSurface(image); |
| 88 |
int chars = 0; |
| 89 |
for (struct vec2i pos = svec2i_zero(); |
| 90 |
pos.y + step.y <= image->h && chars < LAST_CHAR - FIRST_CHAR + 1; |
| 91 |
pos.y += step.y) |
| 92 |
{ |
| 93 |
int x = 0; |
| 94 |
for (pos.x = 0; x < f->Stride && chars < LAST_CHAR - FIRST_CHAR + 1; |
| 95 |
pos.x += step.x, x++, chars++) |
| 96 |
{ |
| 97 |
Pic p; |
| 98 |
|
| 99 |
PicLoad( |
| 100 |
&p, f->Size, |
| 101 |
svec2i_add(pos, svec2i(f->Padding.Left, f->Padding.Top)), |
| 102 |
image, false); |
| 103 |
if (chars == ' ') |
| 104 |
{ |
| 105 |
PicShrink(&p, spaceSize, svec2i_zero()); |
| 106 |
} |
| 107 |
else if (isProportional) |
| 108 |
{ |
| 109 |
PicTrim(&p, true, false); |
| 110 |
} |
| 111 |
CArrayPushBack(&f->Chars, &p); |
| 112 |
} |
| 113 |
} |
| 114 |
SDL_UnlockSurface(image); |
| 115 |
} |
| 116 |
void FontTerminate(Font *f) |
| 117 |
{ |
| 118 |
CA_FOREACH(Pic, p, f->Chars) |
| 119 |
PicFree(p); |
| 120 |
CA_FOREACH_END() |
| 121 |
CArrayTerminate(&f->Chars); |
| 122 |
} |
| 123 |
|
| 124 |
int FontW(const char c) |
| 125 |
{ |
| 126 |
const Pic *p = CArrayGet(&gFont.Chars, (int)c - FIRST_CHAR); |
| 127 |
return p->size.x + gFont.Gap.x; |
| 128 |
} |
| 129 |
int FontH(void) |
| 130 |
{ |
| 131 |
return gFont.Size.y + gFont.Gap.y; |
| 132 |
} |
| 133 |
struct vec2i FontChSize(const char c) |
| 134 |
{ |
| 135 |
return svec2i(FontW(c), FontH()); |
| 136 |
} |
| 137 |
int FontStrW(const char *s) |
| 138 |
{ |
| 139 |
return FontSubstrW(s, (int)strlen(s)); |
| 140 |
} |
| 141 |
int FontSubstrW(const char *s, int len) |
| 142 |
{ |
| 143 |
if (len > (int)strlen(s)) |
| 144 |
{ |
| 145 |
len = (int)strlen(s); |
| 146 |
} |
| 147 |
|
| 148 |
int maxWidth = 0; |
| 149 |
int w = 0; |
| 150 |
for (int i = 0; i < len; i++, s++) |
| 151 |
{ |
| 152 |
if (*s == '\n') |
| 153 |
{ |
| 154 |
maxWidth = MAX(maxWidth, w); |
| 155 |
w = 0; |
| 156 |
} |
| 157 |
else |
| 158 |
{ |
| 159 |
w += FontW(*s); |
| 160 |
} |
| 161 |
} |
| 162 |
maxWidth = MAX(maxWidth, w); |
| 163 |
return maxWidth; |
| 164 |
} |
| 165 |
int FontStrH(const char *s) |
| 166 |
{ |
| 167 |
return FontStrNumLines(s) * FontH(); |
| 168 |
} |
| 169 |
struct vec2i FontStrSize(const char *s) |
| 170 |
{ |
| 171 |
struct vec2i size = svec2i_zero(); |
| 172 |
while (*s) |
| 173 |
{ |
| 174 |
char *lineEnd = strchr(s, '\n'); |
| 175 |
size.y += FontH(); |
| 176 |
if (lineEnd) |
| 177 |
{ |
| 178 |
size.x = MAX(size.x, FontSubstrW(s, (int)(lineEnd - s))); |
| 179 |
s = lineEnd + 1; |
| 180 |
} |
| 181 |
else |
| 182 |
{ |
| 183 |
size.x = MAX(size.x, FontStrW(s)); |
| 184 |
s += strlen(s); |
| 185 |
} |
| 186 |
} |
| 187 |
return size; |
| 188 |
} |
| 189 |
int FontStrNumLines(const char *s) |
| 190 |
{ |
| 191 |
int lines; |
| 192 |
for (lines = 0; s != NULL; lines++) |
| 193 |
{ |
| 194 |
s = strchr(s, '\n'); |
| 195 |
if (s) |
| 196 |
{ |
| 197 |
s++; |
| 198 |
} |
| 199 |
} |
| 200 |
return lines; |
| 201 |
} |
| 202 |
|
| 203 |
struct vec2i FontCh(const char c, const struct vec2i pos) |
| 204 |
{ |
| 205 |
return FontChMask(c, pos, colorWhite); |
| 206 |
} |
| 207 |
struct vec2i FontChMask( |
| 208 |
const char c, const struct vec2i pos, const color_t mask) |
| 209 |
{ |
| 210 |
int idx = (int)c - FIRST_CHAR; |
| 211 |
if (idx < 0) |
| 212 |
{ |
| 213 |
idx += 256; |
| 214 |
} |
| 215 |
if (idx < FIRST_CHAR || idx > LAST_CHAR) |
| 216 |
{ |
| 217 |
fprintf(stderr, "invalid char %d\n", idx); |
| 218 |
idx = FIRST_CHAR; |
| 219 |
} |
| 220 |
const Pic *pic = CArrayGet(&gFont.Chars, idx); |
| 221 |
PicRender( |
| 222 |
pic, gGraphicsDevice.gameWindow.renderer, pos, mask, 0, svec2_one(), |
| 223 |
SDL_FLIP_NONE, Rect2iZero()); |
| 224 |
|
| 225 |
return svec2i(pos.x + pic->size.x + gFont.Gap.x, pos.y); |
| 226 |
} |
| 227 |
struct vec2i FontStr(const char *s, struct vec2i pos) |
| 228 |
{ |
| 229 |
return FontStrMask(s, pos, colorWhite); |
| 230 |
} |
| 231 |
struct vec2i FontStrMask(const char *s, struct vec2i pos, const color_t mask) |
| 232 |
{ |
| 233 |
if (s == NULL) |
| 234 |
{ |
| 235 |
return pos; |
| 236 |
} |
| 237 |
int left = pos.x; |
| 238 |
while (*s) |
| 239 |
{ |
| 240 |
if (*s == '\n') |
| 241 |
{ |
| 242 |
pos.x = left; |
| 243 |
pos.y += FontH(); |
| 244 |
} |
| 245 |
else |
| 246 |
{ |
| 247 |
pos = FontChMask(*s, pos, mask); |
| 248 |
} |
| 249 |
s++; |
| 250 |
} |
| 251 |
return pos; |
| 252 |
} |
| 253 |
struct vec2i FontStrMaskWrap( |
| 254 |
const char *s, struct vec2i pos, color_t mask, const int width) |
| 255 |
{ |
| 256 |
char buf[1024]; |
| 257 |
CASSERT(strlen(s) < 1024, "string too long to wrap"); |
| 258 |
FontSplitLines(s, buf, width); |
| 259 |
return FontStrMask(buf, pos, mask); |
| 260 |
} |
| 261 |
static struct vec2i GetStrPos( |
| 262 |
const char *s, struct vec2i pos, const FontOpts opts); |
| 263 |
void FontStrOpt(const char *s, struct vec2i pos, const FontOpts opts) |
| 264 |
{ |
| 265 |
if (s == NULL) |
| 266 |
{ |
| 267 |
return; |
| 268 |
} |
| 269 |
pos = GetStrPos(s, pos, opts); |
| 270 |
FontStrMask(s, pos, opts.Mask); |
| 271 |
} |
| 272 |
static int GetAlign( |
| 273 |
const FontAlign align, const int pos, const int pad, const int area, |
| 274 |
const int size); |
| 275 |
static struct vec2i GetStrPos( |
| 276 |
const char *s, struct vec2i pos, const FontOpts opts) |
| 277 |
{ |
| 278 |
const struct vec2i textSize = FontStrSize(s); |
| 279 |
return svec2i( |
| 280 |
GetAlign(opts.HAlign, pos.x, opts.Pad.x, opts.Area.x, textSize.x), |
| 281 |
GetAlign(opts.VAlign, pos.y, opts.Pad.y, opts.Area.y, textSize.y)); |
| 282 |
} |
| 283 |
static int GetAlign( |
| 284 |
const FontAlign align, const int pos, const int pad, const int area, |
| 285 |
const int size) |
| 286 |
{ |
| 287 |
switch (align) |
| 288 |
{ |
| 289 |
case ALIGN_START: |
| 290 |
return pos + pad; |
| 291 |
break; |
| 292 |
case ALIGN_CENTER: |
| 293 |
return pos + (area - size) / 2; |
| 294 |
break; |
| 295 |
case ALIGN_END: |
| 296 |
return pos + area - size - pad; |
| 297 |
break; |
| 298 |
default: |
| 299 |
CASSERT(false, "unknown align"); |
| 300 |
return pos; |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
void FontStrCenter(const char *s) |
| 305 |
{ |
| 306 |
FontOpts opts = FontOptsNew(); |
| 307 |
opts.HAlign = ALIGN_CENTER; |
| 308 |
opts.VAlign = ALIGN_CENTER; |
| 309 |
opts.Area = gGraphicsDevice.cachedConfig.Res; |
| 310 |
FontStrOpt(s, svec2i_zero(), opts); |
| 311 |
} |
| 312 |
|
| 313 |
void FontSplitLines(const char *text, char *buf, const int width) |
| 314 |
{ |
| 315 |
if (width == 0) |
| 316 |
{ |
| 317 |
strcpy(buf, text); |
| 318 |
return; |
| 319 |
} |
| 320 |
int ix, x; |
| 321 |
const char *ws, *word, *s; |
| 322 |
|
| 323 |
ix = x = CenterX(width); |
| 324 |
s = ws = word = text; |
| 325 |
|
| 326 |
while (*s) |
| 327 |
{ |
| 328 |
|
| 329 |
ws = s; |
| 330 |
while (*s == ' ' || *s == '\n') |
| 331 |
{ |
| 332 |
*buf++ = *s; |
| 333 |
if (*s == '\n') |
| 334 |
{ |
| 335 |
|
| 336 |
ws = s + 1; |
| 337 |
x = ix; |
| 338 |
} |
| 339 |
s++; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
word = s; |
| 344 |
while (*s != 0 && *s != ' ' && *s != '\n') |
| 345 |
{ |
| 346 |
s++; |
| 347 |
} |
| 348 |
|
| 349 |
int w; |
| 350 |
const char *p; |
| 351 |
for (w = 0, p = ws; p < s; p++) |
| 352 |
{ |
| 353 |
w += FontW(*p); |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
if (x + w > width + ix && w < width) |
| 358 |
{ |
| 359 |
x = ix; |
| 360 |
ws = word; |
| 361 |
*buf++ = '\n'; |
| 362 |
} |
| 363 |
|
| 364 |
for (p = ws; p < word; p++) |
| 365 |
{ |
| 366 |
x += FontW(*p); |
| 367 |
} |
| 368 |
|
| 369 |
for (p = word; p < s; p++) |
| 370 |
{ |
| 371 |
*buf++ = *p; |
| 372 |
x += FontW(*p); |
| 373 |
} |
| 374 |
} |
| 375 |
*buf = '\0'; |
| 376 |
} |
| 377 |
|
| 378 |
struct vec2i Vec2iAligned( |
| 379 |
const struct vec2i v, const struct vec2i size, const FontAlign hAlign, |
| 380 |
const FontAlign vAlign, const struct vec2i area) |
| 381 |
{ |
| 382 |
struct vec2i vAligned = v; |
| 383 |
switch (hAlign) |
| 384 |
{ |
| 385 |
case ALIGN_CENTER: |
| 386 |
vAligned.x += (area.x - size.x) / 2; |
| 387 |
break; |
| 388 |
case ALIGN_END: |
| 389 |
vAligned.x = -v.x + area.x - size.x; |
| 390 |
break; |
| 391 |
default: |
| 392 |
break; |
| 393 |
} |
| 394 |
switch (vAlign) |
| 395 |
{ |
| 396 |
case ALIGN_CENTER: |
| 397 |
vAligned.y += (area.y - size.y) / 2; |
| 398 |
break; |
| 399 |
case ALIGN_END: |
| 400 |
vAligned.y = -v.y + area.y - size.y; |
| 401 |
break; |
| 402 |
default: |
| 403 |
break; |
| 404 |
} |
| 405 |
return vAligned; |
| 406 |
} |