font: implement font_dim

This commit is contained in:
Xavier Del Campo Romero 2022-06-29 00:27:33 +02:00
parent 6d5aa57e6d
commit 23f24016da
2 changed files with 46 additions and 16 deletions

View File

@ -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;

View File

@ -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;
}