SDL_ttf: Add TTF_SetFontDirection and TTF_SetFontScriptName

From 6dcf9e6f2341a482cc6461b885c5063b49096787 Mon Sep 17 00:00:00 2001
From: Sylvain <[EMAIL REDACTED]>
Date: Tue, 10 May 2022 17:39:19 +0200
Subject: [PATCH] Add TTF_SetFontDirection and TTF_SetFontScriptName Exposed
 vertical and horizontal direction.

---
 SDL_ttf.c | 37 +++++++++++++++++++++++++++++++------
 SDL_ttf.h | 39 ++++++++++++++++++++++++++-------------
 2 files changed, 57 insertions(+), 19 deletions(-)

diff --git a/SDL_ttf.c b/SDL_ttf.c
index ab9b561..67281e8 100644
--- a/SDL_ttf.c
+++ b/SDL_ttf.c
@@ -1870,8 +1870,8 @@ TTF_Font* TTF_OpenFontIndexDPIRW(SDL_RWops *src, int freesrc, int ptsize, long i
     hb_ft_font_set_load_flags(font->hb_font, FT_LOAD_DEFAULT | font->ft_load_target);
 
     /* Default value script / direction */
-    TTF_SetFontScript(font, g_hb_script);
-    TTF_SetFontDirection(font, g_hb_direction);
+    font->hb_script = g_hb_script;
+    font->hb_direction = g_hb_direction;
 #endif
 
     if (TTF_SetFontSizeDPI(font, ptsize, hdpi, vdpi) < 0) {
@@ -3053,10 +3053,22 @@ int TTF_GlyphMetrics32(TTF_Font *font, Uint32 ch,
     return 0;
 }
 
-int TTF_SetFontDirection(TTF_Font *font, int direction) /* hb_direction_t */
+int TTF_SetFontDirection(TTF_Font *font, TTF_Direction direction)
 {
 #if TTF_USE_HARFBUZZ
-    font->hb_direction = direction;
+    hb_direction_t dir;
+    if (direction == TTF_DIRECTION_LTR) {
+        dir = HB_DIRECTION_LTR;
+    } else if (direction == TTF_DIRECTION_RTL) {
+        dir = HB_DIRECTION_RTL;
+    } else if (direction == TTF_DIRECTION_TTB) {
+        dir = HB_DIRECTION_TTB;
+    } else if (direction == TTF_DIRECTION_BTT) {
+        dir = HB_DIRECTION_BTT;
+    } else {
+        return -1;
+    }
+    font->hb_direction = dir;
     return 0;
 #else
     (void) direction;
@@ -3064,10 +3076,23 @@ int TTF_SetFontDirection(TTF_Font *font, int direction) /* hb_direction_t */
 #endif
 }
 
-int TTF_SetFontScript(TTF_Font *font, int script) /* hb_script_t */
+int TTF_SetFontScriptName(TTF_Font *font, const char *script)
 {
 #if TTF_USE_HARFBUZZ
-    font->hb_script = script;
+    Uint8 a, b, c, d;
+    hb_script_t scr;
+
+    if (script == NULL || SDL_strlen(script) != 4) {
+        return -1;
+    }
+
+    a = script[0];
+    b = script[1];
+    c = script[2];
+    d = script[3];
+
+    scr = HB_TAG(a, b, c, d);
+    font->hb_script = scr;
     return 0;
 #else
     (void) script;
diff --git a/SDL_ttf.h b/SDL_ttf.h
index 0192433..7aa9bf5 100644
--- a/SDL_ttf.h
+++ b/SDL_ttf.h
@@ -384,19 +384,6 @@ extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderGlyph32_LCD(TTF_Font *font,
 #define TTF_RenderUNICODE(font, text, fg, bg)   \
     TTF_RenderUNICODE_Shaded(font, text, fg, bg)
 
-/* Set Direction and Script to be used for text shaping.
-   - direction is of type hb_direction_t
-   - script is of type hb_script_t
-
-   This functions returns always 0, or -1 if SDL_ttf is not compiled with HarfBuzz
-*/
-extern DECLSPEC int SDLCALL TTF_SetDirection(int direction); /* hb_direction_t */
-extern DECLSPEC int SDLCALL TTF_SetScript(int script); /* hb_script_t */
-
-/* Set direction and script per font */
-extern DECLSPEC int SDLCALL TTF_SetFontDirection(TTF_Font *font, int direction); /* hb_direction_t */
-extern DECLSPEC int SDLCALL TTF_SetFontScript(TTF_Font *font, int script); /* hb_script_t */
-
 /* Close an opened font file */
 extern DECLSPEC void SDLCALL TTF_CloseFont(TTF_Font *font);
 
@@ -426,6 +413,32 @@ extern DECLSPEC SDL_bool TTF_GetFontSDF(const TTF_Font *font);
 #define TTF_SetError    SDL_SetError
 #define TTF_GetError    SDL_GetError
 
+/**
+ * \brief Direction
+ */
+typedef enum
+{
+  TTF_DIRECTION_LTR = 0, /* Left to Right */
+  TTF_DIRECTION_RTL,     /* Right to Left */
+  TTF_DIRECTION_TTB,     /* Top to Bottom */
+  TTF_DIRECTION_BTT      /* Bottom to Top */
+} TTF_Direction;
+
+/* Set Direction and Script to be used for text shaping.
+   These functions return 0, or -1 if SDL_ttf is not compiled with HarfBuzz
+
+   These functions are deprecated. Prefer TTF_SetFontDirection() and TTF_SetFontScriptName()
+*/
+extern DECLSPEC int SDLCALL TTF_SetDirection(int direction); /* hb_direction_t */
+extern DECLSPEC int SDLCALL TTF_SetScript(int script); /* hb_script_t */
+
+/* Set direction and script per font.
+   'script' is null terminated string of exactly 4 characters.
+   These functions return 0, or -1 if SDL_ttf is not compiled with HarfBuzz or invalid parameter
+*/
+extern DECLSPEC int SDLCALL TTF_SetFontDirection(TTF_Font *font, TTF_Direction direction);
+extern DECLSPEC int SDLCALL TTF_SetFontScriptName(TTF_Font *font, const char *script);
+
 /* Ends C function definitions when using C++ */
 #ifdef __cplusplus
 }