render: expose some internal text rendering utilities

This commit is contained in:
Lephe 2023-06-04 23:23:11 +02:00
parent dcb876dfe0
commit 16259deb20
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
6 changed files with 40 additions and 45 deletions

View File

@ -413,6 +413,28 @@ void dprint_opt(int x, int y, int fg, int bg, int halign, int valign,
Calls dprint_opt() with bg=C_NONE, halign=DTEXT_LEFT and valign=DTEXT_TOP */
void dprint(int x, int y, int fg, char const *format, ...);
//---
// Text rendering utilities
//---
/* dfont_glyph_index(): Obtain the glyph index of a Unicode code point
Returns the position of code_point in the character table of the given font,
or -1 if code_point is not part of that set. */
int dfont_glyph_index(font_t const *font, uint32_t code_point);
/* topti_glyph_offset(): Use a font index to find the location of a glyph
The provided glyph value (usuall obtained by dfont_glyph_index()) must be
nonnegative. Returns the offset the this glyph's data in the font's data
array. When using a proportional font, the size array is ignored. */
int dfont_glyph_offset(font_t const *font, uint glyph_number);
/* dtext_utf8_next(): Read the next UTF-8 code point of a string
Returns the next code point and advances the string. Returns 0 (NUL) at the
end of the string. */
uint32_t dtext_utf8_next(uint8_t const **str_pointer);
//---
// Image rendering (bopti)
//---

View File

@ -414,7 +414,7 @@ image_t *image_sub(image_t const *src, int x, int y, int w, int h,
/* Make the last parameter optional */
#define image_sub1(src, x, y, w, h, dst, ...) image_sub(src, x, y, w, h, dst)
#define image_sub(...) image_sub(__VA_ARGS__, NULL)
#define image_sub(...) image_sub1(__VA_ARGS__, NULL)
/* image_at(): Build a reference to a position within a sub-image */
#define image_at(img, x, y) image_sub(img, x, y, -1, -1)
@ -434,9 +434,9 @@ image_t *image_sub(image_t const *src, int x, int y, int w, int h,
// that allocation can fail, so you need to check whether the returned image is
// valid.
//
// (You can still pass an invalid image to libimg functions when chaining
// transforms. The invalid image will be ignored or returned unchanged, so you
// can check for it at the end of any large chain.)
// (You can still pass invalid images to transform functions. The invalid image
// will be ignored or returned unchanged, so you can chain calls and check for
// validity at the end of the chain.)
//
// Some functions support in-place transforms. This means they can be called
// with the source as destination, and will transform the image without needing

View File

@ -78,10 +78,10 @@ static void topti_render(int x, int y, char const *str_char, font_t const *f,
/* Read each character from the input string */
while(1)
{
uint32_t code_point = topti_utf8_next(&str);
uint32_t code_point = dtext_utf8_next(&str);
if(!code_point || (size >= 0 && str - str0 > size)) break;
int glyph = topti_glyph_index(f, code_point);
int glyph = dfont_glyph_index(f, code_point);
if(glyph < 0) continue;
int dataw = f->prop ? f->glyph_width[glyph] : f->width;
@ -91,7 +91,7 @@ static void topti_render(int x, int y, char const *str_char, font_t const *f,
x += space;
if(x >= dwindow.right) break;
int index = topti_offset(f, glyph);
int index = dfont_glyph_offset(f, glyph);
/* Compute horizontal intersection between glyph and screen */

View File

@ -134,13 +134,13 @@ void topti_render(int x, int y, char const *str_char, font_t const *f,
/* Pull each character into the operator buffer */
while(1)
{
uint32_t code_point = topti_utf8_next(&str);
uint32_t code_point = dtext_utf8_next(&str);
if(!code_point || (size >= 0 && str - str0 > size)) break;
int glyph = topti_glyph_index(f, code_point);
int glyph = dfont_glyph_index(f, code_point);
if(glyph < 0) continue;
int index = topti_offset(f, glyph);
int index = dfont_glyph_offset(f, glyph);
/* Put glyph data into the operators */
int width = f->prop ? f->glyph_width[glyph] : f->width;

View File

@ -26,23 +26,4 @@ extern font_t const *topti_font;
/* Default font */
extern font_t const *gint_default_font;
/* topti_utf8_next(): Read the next UTF-8 code point of a string
Returns the next code point and advances the string. Returns 0 (NUL) at the
end of the string. */
uint32_t topti_utf8_next(uint8_t const **str_pointer);
/* topti_glyph_index(): Obtain the glyph index of a Unicode code point
Returns the position of code_point in the character table of the given font,
or -1 if code_point is not part of that set.
@f Font object
@code_point Unicode code point to locate the glyph for */
int topti_glyph_index(font_t const *f, uint32_t code_point);
/* topti_offset(): Use a font index to find the location of a glyph
@f Font object
@glyph Glyph number obtained by charset_decode(), must be nonnegative.
Returns the offset the this glyph's data in the font's data array. When
using a proportional font, the size array is not heeded for. */
int topti_offset(font_t const *f, uint glyph);
#endif /* RENDER_COMMON */

View File

@ -3,7 +3,6 @@
#include "../render/render.h"
/* dfont(): Set the default font for text rendering */
font_t const *dfont(font_t const * font)
{
font_t const *old_font = topti_font;
@ -12,14 +11,12 @@ font_t const *dfont(font_t const * font)
return old_font;
}
/* dfont_default(): Get gint's default font */
font_t const *dfont_default(void)
{
return gint_default_font;
}
/* topti_glyph_index(): Obtain the glyph index of a Unicode code point */
int topti_glyph_index(font_t const *f, uint32_t code_point)
int dfont_glyph_index(font_t const *f, uint32_t code_point)
{
int glyph_start = 0;
@ -37,8 +34,7 @@ int topti_glyph_index(font_t const *f, uint32_t code_point)
return -1;
}
/* topti_offset(): Use a font index to find the location of a glyph */
int topti_offset(font_t const *f, uint glyph)
int dfont_glyph_offset(font_t const *f, uint glyph)
{
/* Non-proportional fonts don't need an index */
if(!f->prop) return glyph * f->storage_size;
@ -57,8 +53,7 @@ int topti_offset(font_t const *f, uint glyph)
return offset;
}
/* topti_utf8_next(): Read the next UTF-8 code point of a string */
uint32_t topti_utf8_next(uint8_t const **str_pointer)
uint32_t dtext_utf8_next(uint8_t const **str_pointer)
{
uint8_t const *str = *str_pointer;
uint8_t lead = *str++;
@ -103,7 +98,6 @@ uint32_t topti_utf8_next(uint8_t const **str_pointer)
return 0x20;
}
/* dnsize(): Get the width and height of rendered text, with character limit */
void dnsize(char const *str_char, int size, font_t const *f, int *w, int *h)
{
uint8_t const *str = (void *)str_char;
@ -121,7 +115,7 @@ void dnsize(char const *str_char, int size, font_t const *f, int *w, int *h)
int length = 0;
while(1)
{
code_point = topti_utf8_next(&str);
code_point = dtext_utf8_next(&str);
if(!code_point || (size >= 0 && str - str0 > size))
break;
length++;
@ -136,23 +130,21 @@ void dnsize(char const *str_char, int size, font_t const *f, int *w, int *h)
while(1)
{
code_point = topti_utf8_next(&str);
code_point = dtext_utf8_next(&str);
if(!code_point || (size >= 0 && str - str0 > size)) break;
int glyph = topti_glyph_index(f, code_point);
int glyph = dfont_glyph_index(f, code_point);
if(glyph >= 0)
width += f->glyph_width[glyph] + f->char_spacing;
}
*w = width - f->char_spacing;
}
/* dsize(): Get the width and height of rendered text */
void dsize(char const *str_char, font_t const *f, int *w, int *h)
{
return dnsize(str_char, -1, f, w, h);
}
/* drsize(): Get width of rendered text with reverse size limit */
char const *drsize(char const *str_char, font_t const *f, int width, int *w)
{
uint8_t const *str = (void *)str_char;
@ -166,7 +158,7 @@ char const *drsize(char const *str_char, font_t const *f, int width, int *w)
/* Record that last glyph considered fits */
str_char = (void *)str;
code_point = topti_utf8_next(&str);
code_point = dtext_utf8_next(&str);
if(!code_point)
{
break;
@ -179,7 +171,7 @@ char const *drsize(char const *str_char, font_t const *f, int width, int *w)
}
else
{
int glyph = topti_glyph_index(f, code_point);
int glyph = dfont_glyph_index(f, code_point);
if(glyph >= 0) used_width += f->glyph_width[glyph];
}
}