diff options
Diffstat (limited to 'src/sbdf.h')
-rw-r--r-- | src/sbdf.h | 54 |
1 files changed, 44 insertions, 10 deletions
@@ -1,6 +1,6 @@ /* BDF simple reader and printer (ASCII Support only) - include stdio.h stddef.h stdlib.h string.h + include stdio.h stdlib.h string.h Should define SBDF_IMPLEMENTATION in only one .c file */ @@ -8,6 +8,7 @@ typedef struct SBDF_Font SBDF_Font; SBDF_Font* SBDF_FontLoad(const char *); void SBDF_FontDestroy(SBDF_Font *); +void SBDF_GetSize(const SBDF_Font *, const char *, int *, int *, int *, int *); #ifdef SBDF_USESDL void SBDF_SDLPrint(const SDL_Renderer *, const SBDF_Font *, const char *, int, int); #endif @@ -24,7 +25,7 @@ struct SBDF_Font { unsigned char *data; }; -struct SBDF_Font* +static struct SBDF_Font* SBDF_FontInit(size_t sz) { struct SBDF_Font *res; int i; @@ -36,8 +37,15 @@ SBDF_FontInit(size_t sz) { free(res); return NULL; } - for (i = 0; i < 256; i++) + for (i = 0; i < 256; i++) { + res->g[i].dx = 0; + res->g[i].dy = 0; + res->g[i].bx = 0; + res->g[i].by = 0; + res->g[i].bw = 0; + res->g[i].bh = 0; res->g[i].s = -1; + } return res; } @@ -187,6 +195,32 @@ SBDF_FontLoad_defer: return NULL; } +void +SBDF_GetSize(const struct SBDF_Font *f, const char *text, int *tx, int *ty, int *w, int *h) { + char *u; + int minw, minh, maxw, maxh, x, y; + x = y = minw = minh = maxw = maxh = 0; + + u = text; + while (*u != '\0') { + x += f->g[(unsigned char)*u].bx; + y += f->g[(unsigned char)*u].by; + minw = (minw < x) ? minw : x; + minh = (minh < y) ? minh : y; + maxw = (maxw > x + f->g[(unsigned char)*u].bw) ? maxw : (x + f->g[(unsigned char)*u].bw); + maxh = (maxh > y + f->g[(unsigned char)*u].bh) ? maxh : (y + f->g[(unsigned char)*u].bh); + + x += f->g[(unsigned char)*u].dx - f->g[(unsigned char)*u].bx; + y += f->g[(unsigned char)*u].dy - f->g[(unsigned char)*u].by; + u++; + } + + if (tx != NULL) *tx = minw; + if (ty != NULL) *ty = minh; + if (w != NULL) *w = maxw; + if (h != NULL) *h = maxh; +} + #ifdef SBDF_USESDL void SBDF_SDLPrint(const SDL_Renderer *r, const struct SBDF_Font *f, const char *text, int x, int y) { @@ -195,18 +229,18 @@ SBDF_SDLPrint(const SDL_Renderer *r, const struct SBDF_Font *f, const char *text int i, j; u = text; while (*u != '\0') { - v = &f->data[f->g[*u].s]; - x += f->g[*u].bx; - y += f->g[*u].by; - for (i = 0; i < f->g[*u].bh; i++) { - for (j = 0; j < f->g[*u].bw; j++) { + v = &f->data[f->g[(unsigned char)*u].s]; + x += f->g[(unsigned char)*u].bx; + y -= f->g[(unsigned char)*u].by + f->g[(unsigned char)*u].bh; + for (i = 0; i < f->g[(unsigned char)*u].bh; i++) { + for (j = 0; j < f->g[(unsigned char)*u].bw; j++) { if (v[j/8]>>(7-(j%8))&1) SDL_RenderDrawPoint(r, x+j, y+i); } v += (j + 7) / 8; } - x += f->g[*u].dx - f->g[*u].bx; - y += f->g[*u].dy - f->g[*u].by; + x += f->g[(unsigned char)*u].dx - f->g[(unsigned char)*u].bx; + y += f->g[(unsigned char)*u].dy + f->g[(unsigned char)*u].by + f->g[(unsigned char)*u].bh; u++; } } |