From 689d8faa8638c1d14f79c7960afe3139bd042f96 Mon Sep 17 00:00:00 2001 From: Krow Savcik Date: Fri, 19 Jul 2024 16:03:17 +0300 Subject: fix: update sbdf file --- src/sbdf.h | 54 ++++++++++++++++++++++++++++++++++++++++++++---------- src/ui.c | 14 +++++++++++--- 2 files changed, 55 insertions(+), 13 deletions(-) diff --git a/src/sbdf.h b/src/sbdf.h index ba6d413..f812208 100644 --- a/src/sbdf.h +++ b/src/sbdf.h @@ -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++; } } diff --git a/src/ui.c b/src/ui.c index 633235b..d126215 100644 --- a/src/ui.c +++ b/src/ui.c @@ -754,7 +754,7 @@ ui_panel_timeline_redraw(UIPanelTimeline *p) dest.w = 140; dest.h = 24; SDL_RenderSetViewport(ren, &dest); - SBDF_SDLPrint(ren, font, cur_canvas->layers[i]->name, 2, 5); + SBDF_SDLPrint(ren, font, cur_canvas->layers[i]->name, 2, 17); } /* Draw Frame row */ @@ -774,7 +774,7 @@ ui_panel_timeline_redraw(UIPanelTimeline *p) SDL_RenderDrawLine(ren, 22, 2, 22, 21); SDL_RenderDrawLine(ren, 23, 2, 23, 21); SDL_SetRenderDrawColor(ren, INTTOCOLA(0x000000ff)); - j = i; k = 2; + j = i+1; k = 2; text[3] = '\0'; while (j) { text[k--] = (j%10) + '0'; @@ -786,7 +786,15 @@ ui_panel_timeline_redraw(UIPanelTimeline *p) text[k] = text[j]; } - SBDF_SDLPrint(ren, font, text, 9, 5); + SBDF_GetSize(font, text, &k, NULL, &j, NULL); + j -= k; + if (j > 24) { + j = 9; + } else { + j = (24 - j)/2 - k; + } + + SBDF_SDLPrint(ren, font, text, j, 17); } } SDL_RenderSetViewport(ren, NULL); -- cgit v1.2.3