From 16259deb20558a14037ad5fd49aadf4a5cc3805f Mon Sep 17 00:00:00 2001
From: Lephe <sebastien.michelland@protonmail.com>
Date: Sun, 4 Jun 2023 23:23:11 +0200
Subject: [PATCH] render: expose some internal text rendering utilities

---
 include/gint/display.h | 22 ++++++++++++++++++++++
 include/gint/image.h   |  8 ++++----
 src/render-cg/topti.c  |  6 +++---
 src/render-fx/topti.c  |  6 +++---
 src/render/render.h    | 19 -------------------
 src/render/topti.c     | 24 ++++++++----------------
 6 files changed, 40 insertions(+), 45 deletions(-)

diff --git a/include/gint/display.h b/include/gint/display.h
index 9f5a93b..32235dc 100644
--- a/include/gint/display.h
+++ b/include/gint/display.h
@@ -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)
 //---
diff --git a/include/gint/image.h b/include/gint/image.h
index a27cec2..025649c 100644
--- a/include/gint/image.h
+++ b/include/gint/image.h
@@ -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
diff --git a/src/render-cg/topti.c b/src/render-cg/topti.c
index 49ee6e4..31fdb39 100644
--- a/src/render-cg/topti.c
+++ b/src/render-cg/topti.c
@@ -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 */
 
diff --git a/src/render-fx/topti.c b/src/render-fx/topti.c
index 4dc3dd7..af09bdc 100644
--- a/src/render-fx/topti.c
+++ b/src/render-fx/topti.c
@@ -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;
diff --git a/src/render/render.h b/src/render/render.h
index 9f34060..fcac146 100644
--- a/src/render/render.h
+++ b/src/render/render.h
@@ -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 */
diff --git a/src/render/topti.c b/src/render/topti.c
index f7f88e4..15e1370 100644
--- a/src/render/topti.c
+++ b/src/render/topti.c
@@ -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];
 		}
 	}