merge font_getstringnsize and font_measurestring

measure string just needed a bit more to be a replacement

Change-Id: I25c760df5edd7224b50c0522b4e44b1f9b574c51
This commit is contained in:
William Wilgus 2026-05-18 01:08:02 -04:00
parent 6e27ba80e4
commit 58f75311d8
3 changed files with 22 additions and 32 deletions

View file

@ -262,7 +262,7 @@ static bool splash_internal(struct screen * screen, const char *fmt, va_list ap,
if (w > width) /* split when it fits */
{
w = width;
next_len = font_measurestring(next, oldlen, &w, fontnum);
next_len = font_measurestring(next, oldlen, w, &w, NULL, fontnum);
store = next + next_len;
}

View file

@ -132,7 +132,7 @@ void font_disable_all(void);
void font_enable_all(void);
struct font* font_get(int font);
int font_measurestring(const unsigned char *str, size_t maxbytes, int *max_width, int fontnum);
int font_measurestring(const unsigned char *str, size_t maxbytes, size_t maxwidth, int *w, int *h, int fontnum);
int font_getstringnsize(const unsigned char *str, size_t maxbytes, int *w, int *h, int fontnumber);
int font_getstringsize(const unsigned char *str, int *w, int *h, int fontnumber);
int font_get_width(struct font* ft, ucschar_t ch);

View file

@ -1106,16 +1106,20 @@ const unsigned char* font_get_bits(struct font* pf, ucschar_t char_code)
/*
* Returns the length (in bytes) of a given NULL terminated string
* stops after max_width, maxbytes or NULL (\0) whichever occurs first.
* stops after maxwidth, maxbytes or NULL (\0) whichever occurs first.
* maxbytes = -1 ignores maxbytes and relies on NULL terminator (\0)
* to terminate the string
* maxwidth = -1 ignores maxwidth
* stringsize pixel width and height will be returned in *w and *h
*/
int font_measurestring(const unsigned char *str, size_t maxbytes, int *max_width, int fontnum)
int font_measurestring(const unsigned char *str, size_t maxbytes, size_t maxwidth, int *w, int *h, int fontnum)
{
const unsigned char *start = str;
size_t bytes;
struct font* pf = font_get(fontnum);
font_lock( fontnum, true );
ucschar_t ch;
int width = 0;
size_t width = 0;
while (true)
{
@ -1127,15 +1131,20 @@ int font_measurestring(const unsigned char *str, size_t maxbytes, int *max_width
}
if (IS_DIACRITIC(ch))
continue;
int w = font_get_width(pf,ch);
if (width + w > *max_width)
int fw = font_get_width(pf,ch);
width += fw;
if (width > maxwidth)
{
width -= fw;
break;
}
width += w;
}
*max_width = width;
if ( h )
*h = pf->height;
if ( w )
*w = width;
font_lock( fontnum, false );
return bytes;
@ -1149,30 +1158,11 @@ int font_measurestring(const unsigned char *str, size_t maxbytes, int *max_width
*/
int font_getstringnsize(const unsigned char *str, size_t maxbytes, int *w, int *h, int fontnum)
{
const unsigned char *start = str;
size_t bytes;
struct font* pf = font_get(fontnum);
font_lock( fontnum, true );
ucschar_t ch;
int width = 0;
while (true)
{
bytes = str - start;
str = utf8decode(str, &ch);
if (ch == 0 || bytes >= maxbytes)
break;
if (IS_DIACRITIC(ch))
continue;
/* get proportional width and glyph bits*/
width += font_get_width(pf,ch);
}
if ( w )
*w = width;
if ( h )
*h = pf->height;
font_lock( fontnum, false );
return width;
if (!w)
w = &width;
font_measurestring(str, maxbytes, -1, w, h, fontnum);
return *w;
}
/*