diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-06-29 00:27:33 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-06-29 00:27:33 +0200 |
| commit | 23f24016da8e019de747bbc513202240b0d0ebba (patch) | |
| tree | cf16f1a64e2697a6d0782bc6c831e62cc3f2b0ec | |
| parent | 6d5aa57e6d893e15a77e85490c0f1a761c30924e (diff) | |
font: implement font_dim
| -rw-r--r-- | src/font/inc/font.h | 1 | ||||
| -rw-r--r-- | src/font/src/font.c | 61 |
2 files changed, 46 insertions, 16 deletions
diff --git a/src/font/inc/font.h b/src/font/inc/font.h index 524355d..b074089 100644 --- a/src/font/inc/font.h +++ b/src/font/inc/font.h @@ -12,6 +12,7 @@ enum font }; int font_puts(enum font, short x, short y, const char *s); +int font_dim(enum font f, const char *str, short *x, short *y); extern struct sprite font_sprite; diff --git a/src/font/src/font.c b/src/font/src/font.c index 765151b..1e1bb6c 100644 --- a/src/font/src/font.c +++ b/src/font/src/font.c @@ -5,7 +5,9 @@ struct sprite font_sprite; -static int renderstr(const enum font f, const short x, short y, const char *str) +static int renderstr(const enum font f, const short x, short y, + const char *str, const bool render, short *const max_x, + short *const max_y) { static const struct cfg { @@ -26,46 +28,73 @@ static int renderstr(const enum font f, const short x, short y, const char *str) char c; short rx = x; + if (max_x) + *max_x = rx; + + if (max_y) + *max_y = y + cfg->fh; + while ((c = *str++)) { if (c == ' ') { rx += cfg->fs; + + if (max_x && rx >= *max_x) + *max_x = rx; + continue; } else if (c == '\n' || c == '\r') { rx = x; y += cfg->fh; + + if (max_y) + *max_y = y; + continue; } - sprite_get_or_ret(s, -1); + if (render) + { + sprite_get_or_ret(s, -1); - if (sprite_clone(cfg->s, s)) - return -1; + if (sprite_clone(cfg->s, s)) + return -1; - s->w = cfg->fw; - s->h = cfg->fh; + s->w = cfg->fw; + s->h = cfg->fh; - /* Substract non-printable characters (NUL to SP). */ - const char ch = c - '!'; - const short u = (cfg->fw * ch) % cfg->s->w; - const short v = cfg->fh * ((cfg->fw * ch) / cfg->s->w); + /* Substract non-printable characters (NUL to SP). */ + const char ch = c - '!'; + const short u = (cfg->fw * ch) % cfg->s->w; + const short v = cfg->fh * ((cfg->fw * ch) / cfg->s->w); + + s->u += u; + s->v += v; + s->x = rx; + s->y = y; + sprite_sort(s); + } - s->u += u; - s->v += v; - s->x = rx; - s->y = y; - sprite_sort(s); rx += cfg->fs; + + if (max_x && rx >= *max_x) + *max_x = rx; } return 0; } +int font_dim(const enum font f, const char *const str, short *const x, + short *const y) +{ + return renderstr(f, 0, 0, str, false, x, y); +} + int font_puts(const enum font f, const short x, const short y, const char *const str) { - return renderstr(f, x, y, str) ? EOF : 0; + return renderstr(f, x, y, str, true, NULL, NULL) ? EOF : 0; } |
