SDL_ttf: Setting a text object font to NULL invalidates the text

From e062c11b63d31c81188b149bb7f7adb6d700ae9f Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Fri, 27 Dec 2024 17:51:24 -0800
Subject: [PATCH] Setting a text object font to NULL invalidates the text

Don't try to be fancy and retain text information after the font has been closed or set to NULL. That will just blow up somewhere later when the font is expected to be valid and isn't.
---
 src/SDL_ttf.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/src/SDL_ttf.c b/src/SDL_ttf.c
index e2ae2458..5498377d 100644
--- a/src/SDL_ttf.c
+++ b/src/SDL_ttf.c
@@ -3755,7 +3755,6 @@ SDL_Surface* TTF_RenderText_LCD_Wrapped(TTF_Font *font, const char *text, size_t
 struct TTF_TextLayout
 {
     int font_height;
-    TTF_Direction direction;
     int wrap_length;
     bool wrap_whitespace_visible;
     int *lines;
@@ -4122,11 +4121,11 @@ bool TTF_SetTextFont(TTF_Text *text, TTF_Font *font)
     if (text->internal->font) {
         AddFontTextReference(text->internal->font, text);
 
-        // Only update the text if we have a font available
-        text->internal->needs_layout_update = true;
         text->internal->layout->font_height = font->height;
-        text->internal->layout->direction = TTF_GetFontDirection(font);
+    } else {
+        text->internal->layout->font_height = 0;
     }
+    text->internal->needs_layout_update = true;
 
     return true;
 }
@@ -4636,7 +4635,7 @@ TTF_SubString **TTF_GetTextSubStringsForRange(TTF_Text *text, int offset, int le
         SDL_copyp(substring, &substring1);
         if (length == 0) {
             substring->length = 0;
-            if (text->internal->layout->direction != TTF_DIRECTION_RTL) {
+            if (TTF_GetFontDirection(text->internal->font) != TTF_DIRECTION_RTL) {
                 substring->rect.x += substring->rect.w;
             }
             substring->rect.w = 0;
@@ -4710,7 +4709,7 @@ bool TTF_GetTextSubStringForPoint(TTF_Text *text, int x, int y, TTF_SubString *s
         return true;
     }
 
-    TTF_Direction direction = text->internal->layout->direction;
+    TTF_Direction direction = TTF_GetFontDirection(text->internal->font);
     bool prefer_row = (direction == TTF_DIRECTION_LTR || direction == TTF_DIRECTION_RTL);
     bool line_ends_right = (direction == TTF_DIRECTION_LTR);
     const TTF_SubString *closest = NULL;