SDL_ttf: Added TTF_GetFontScript()

From c827d6181c5bcd9d6b8915e3bdc1d2fa5d2c6ae4 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sun, 26 Jan 2025 22:05:17 -0800
Subject: [PATCH] Added TTF_GetFontScript()

---
 include/SDL3_ttf/SDL_ttf.h | 23 +++++++++++++++++++++++
 src/SDL_ttf.c              | 26 ++++++++++++++++++++++++++
 src/SDL_ttf.sym            |  1 +
 3 files changed, 50 insertions(+)

diff --git a/include/SDL3_ttf/SDL_ttf.h b/include/SDL3_ttf/SDL_ttf.h
index 2eac841a..501334d9 100644
--- a/include/SDL3_ttf/SDL_ttf.h
+++ b/include/SDL3_ttf/SDL_ttf.h
@@ -933,6 +933,29 @@ extern SDL_DECLSPEC TTF_Direction SDLCALL TTF_GetFontDirection(TTF_Font *font);
  */
 extern SDL_DECLSPEC bool SDLCALL TTF_SetFontScript(TTF_Font *font, const char *script);
 
+/**
+ * Get the script used for text shaping a font.
+ *
+ * The supplied script value will be a null-terminated string of exactly four
+ * characters.
+ *
+ * If SDL_ttf was not built with HarfBuzz support, this function returns
+ * false.
+ *
+ * \param font the font to query.
+ * \param script a pointer filled in with the script used by `ch`.
+ * \param script_size the size of the script buffer, which must be at least 5
+ *                    characters.
+ * \returns true on success or false on failure; call SDL_GetError() for more
+ *          information.
+ *
+ * \threadsafety This function should be called on the thread that created the
+ *               font.
+ *
+ * \since This function is available since SDL_ttf 3.0.0.
+ */
+extern SDL_DECLSPEC bool SDLCALL TTF_GetFontScript(TTF_Font *font, char *script, size_t script_size);
+
 /**
  * Get the script used by a 32-bit codepoint.
  *
diff --git a/src/SDL_ttf.c b/src/SDL_ttf.c
index 137fabc5..8159597c 100644
--- a/src/SDL_ttf.c
+++ b/src/SDL_ttf.c
@@ -5700,6 +5700,32 @@ bool TTF_SetFontScript(TTF_Font *font, const char *script)
 #endif
 }
 
+bool TTF_GetFontScript(TTF_Font *font, char *script, size_t script_size)
+{
+    TTF_CHECK_FONT(font, false);
+
+#if TTF_USE_HARFBUZZ
+    TTF_CHECK_POINTER("script", script, false);
+
+    if (script_size < 5) {
+        return SDL_SetError("Insufficient script buffer size");
+    }
+
+    uint8_t untagged_script[4] = { HB_UNTAG(font->hb_script) };
+    script[0] = (char)untagged_script[0];
+    script[1] = (char)untagged_script[1];
+    script[2] = (char)untagged_script[2];
+    script[3] = (char)untagged_script[3];
+    script[4] = '\0';
+    return true;
+
+#else
+    (void) script;
+    (void) script_size;
+    return SDL_Unsupported();
+#endif
+}
+
 bool TTF_GetGlyphScript(Uint32 ch, char *script, size_t script_size)
 {
 #if TTF_USE_HARFBUZZ
diff --git a/src/SDL_ttf.sym b/src/SDL_ttf.sym
index ef188a7b..d31a993e 100644
--- a/src/SDL_ttf.sym
+++ b/src/SDL_ttf.sym
@@ -30,6 +30,7 @@ SDL3_ttf_0.0.0 {
     TTF_GetFontLineSkip;
     TTF_GetFontOutline;
     TTF_GetFontProperties;
+    TTF_GetFontScript;
     TTF_GetFontSDF;
     TTF_GetFontSize;
     TTF_GetFontStyle;