SDL_ttf: Handle UNICODE_BOM_NATIVE/SWAPPED when calculating length in UCS2 to UTF-8 conversion (see #230)

From 18caec28da50998b066b2481344d0c178bc7aa08 Mon Sep 17 00:00:00 2001
From: Sylvain <[EMAIL REDACTED]>
Date: Fri, 24 Jun 2022 09:58:57 +0200
Subject: [PATCH] Handle UNICODE_BOM_NATIVE/SWAPPED when calculating length in
 UCS2 to UTF-8 conversion (see #230)

---
 SDL_ttf.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/SDL_ttf.c b/SDL_ttf.c
index 0cc2be7..fbd7eab 100644
--- a/SDL_ttf.c
+++ b/SDL_ttf.c
@@ -2771,9 +2771,21 @@ static size_t LATIN1_to_UTF8_len(const char *text)
 /* Gets the number of bytes needed to convert a UCS2 string to UTF-8 */
 static size_t UCS2_to_UTF8_len(const Uint16 *text)
 {
+    SDL_bool swapped = TTF_byteswapped;
     size_t bytes = 1;
     while (*text) {
         Uint16 ch = *text++;
+        if (ch == UNICODE_BOM_NATIVE) {
+            swapped = SDL_FALSE;
+            continue;
+        }
+        if (ch == UNICODE_BOM_SWAPPED) {
+            swapped = SDL_TRUE;
+            continue;
+        }
+        if (swapped) {
+            ch = SDL_Swap16(ch);
+        }
         if (ch <= 0x7F) {
             bytes += 1;
         } else if (ch <= 0x7FF) {