# SDL: Expand the text area if it's too small for the font on Windows

**URL:** <https://discourse.libsdl.org/t/sdl-expand-the-text-area-if-its-too-small-for-the-font-on-windows/70114>\
**Category:** SDL Commits\
**Created:** [November 25, 2026, 5:25am UTC](https://discourse.libsdl.org/t/sdl-expand-the-text-area-if-its-too-small-for-the-font-on-windows/70114 "2026-11-25T05:25:21Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![SDLPushPostBot](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/sdlpushpostbot/32/4370_2.png) [@SDLPushPostBot](https://discourse.libsdl.org/u/SDLPushPostBot)\
**Post date:** [November 25, 2026, 5:25am UTC](https://discourse.libsdl.org/t/sdl-expand-the-text-area-if-its-too-small-for-the-font-on-windows/70114/1 "2026-11-25T05:25:21Z")

</div>

> <https://github.com/libsdl-org/SDL/commit/c189710b32671da8908bbca574d1d873b56ae6f9>
>
> Fixes https://github.com/libsdl-org/SDL/issues/16817

```diff
From c189710b32671da8908bbca574d1d873b56ae6f9 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 24 Nov 2026 16:19:12 -0800
Subject: [PATCH] Expand the text area if it's too small for the font on
 Windows

Fixes https://github.com/libsdl-org/SDL/issues/16817
---
 src/video/windows/SDL_windowskeyboard.c | 60 +++++++++++++++++++++----
 src/video/windows/SDL_windowsvideo.h | 2 +
 2 files changed, 54 insertions(+), 8 deletions(-)

diff --git a/src/video/windows/SDL_windowskeyboard.c b/src/video/windows/SDL_windowskeyboard.c
index 86dd8c888429c..037494d89f713 100644
--- a/src/video/windows/SDL_windowskeyboard.c
+++ b/src/video/windows/SDL_windowskeyboard.c
@@ -714,6 +714,27 @@ static void IME_SetWindow(SDL_VideoData *videodata, SDL_Window *window)
 
 #endif
 
+static int GetFontHeight(HWND hwnd, const LOGFONTW *font)
+{
+ int height = 0;
+
+ HFONT hfont = CreateFontIndirectW(font);
+ if (hfont) {
+ HDC hdc = GetDC(hwnd);
+ if (hdc) {
+ TEXTMETRICW tm;
+ HGDIOBJ old_font = SelectObject(hdc, hfont);
+ if (GetTextMetricsW(hdc, &tm)) {
+ height = tm.tmHeight;
+ }
+ SelectObject(hdc, old_font);
+ ReleaseDC(hwnd, hdc);
+ }
+ DeleteObject(hfont);
+ }
+ return height;
+}
+
 static void IME_SetTextInputArea(SDL_VideoData *videodata, HWND hwnd, const SDL_Rect *rect, int cursor)
 {
     HIMC himc;
@@ -722,21 +743,43 @@ static void IME_SetTextInputArea(SDL_VideoData *videodata, HWND hwnd, const SDL_
     if (himc) {
         COMPOSITIONFORM cof;
         CANDIDATEFORM caf;
- int font_height = rect->h;
+ int font_height = 0;
+ int top, height;
 
         LOGFONTW font;
+ SDL_zero(font);
         if (ImmGetCompositionFontW(himc, &font)) {
- font_height = font.lfHeight;
+ if (videodata->ime_font_height && SDL_memcmp(&font, &videodata->ime_font, sizeof(font)) == 0) {
+ font_height = videodata->ime_font_height;
+ } else {
+ font_height = GetFontHeight(hwnd, &font);
+ if (!font_height) {
+ font_height = SDL_abs(font.lfHeight);
+ }
+ SDL_copyp(&videodata->ime_font, &font);
+ videodata->ime_font_height = font_height;
+ }
+ }
+ if (!font_height) {
+ font_height = rect->h;
+ }
+
+ // The composition text is clipped to the area, so grow it around the center if the font doesn't fit
+ top = rect->y;
+ height = rect->h;
+ if (font_height > height) {
+ top -= (font_height - height) / 2;
+ height = font_height;
         }
 
         SDL_zero(cof);
         cof.dwStyle = CFS_RECT;
         cof.ptCurrentPos.x = rect->x + cursor;
- cof.ptCurrentPos.y = rect->y + (rect->h - font_height) / 2;
+ cof.ptCurrentPos.y = top + (height - font_height) / 2;
         cof.rcArea.left = rect->x;
         cof.rcArea.right = (LONG)rect->x + rect->w;
- cof.rcArea.top = rect->y;
- cof.rcArea.bottom = (LONG)rect->y + rect->h;
+ cof.rcArea.top = top;
+ cof.rcArea.bottom = (LONG)top + height;
         if (SDL_memcmp(&cof, &videodata->ime_composition_area, sizeof(cof)) != 0) {
             SDL_copyp(&videodata->ime_composition_area, &cof);
             ImmSetCompositionWindow(himc, &cof);
@@ -746,11 +789,11 @@ static void IME_SetTextInputArea(SDL_VideoData *videodata, HWND hwnd, const SDL_
         caf.dwIndex = 0;
         caf.dwStyle = CFS_EXCLUDE;
         caf.ptCurrentPos.x = rect->x + cursor;
- caf.ptCurrentPos.y = rect->y;
+ caf.ptCurrentPos.y = top;
         caf.rcArea.left = rect->x;
         caf.rcArea.right = (LONG)rect->x + rect->w;
- caf.rcArea.top = rect->y;
- caf.rcArea.bottom = (LONG)rect->y + rect->h;
+ caf.rcArea.top = top;
+ caf.rcArea.bottom = (LONG)top + height;
         if (SDL_memcmp(&caf, &videodata->ime_candidate_area, sizeof(caf)) != 0) {
             SDL_copyp(&videodata->ime_candidate_area, &caf);
             ImmSetCandidateWindow(himc, &caf);
@@ -1171,6 +1214,7 @@ bool WIN_HandleIMEMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM *lParam, SD
             break;
         case IMN_SETCOMPOSITIONFONT:
             SDL_DebugIMELog("IMN_SETCOMPOSITIONFONT");
+ IME_SetWindow(videodata, data->window);
             break;
         case IMN_SETCANDIDATEPOS:
             SDL_DebugIMELog("IMN_SETCANDIDATEPOS");
diff --git a/src/video/windows/SDL_windowsvideo.h b/src/video/windows/SDL_windowsvideo.h
index 1e1b63f586001..79cc5f4130d13 100644
--- a/src/video/windows/SDL_windowsvideo.h
+++ b/src/video/windows/SDL_windowsvideo.h
@@ -630,6 +630,8 @@ struct SDL_VideoData
 #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
     COMPOSITIONFORM ime_composition_area;
     CANDIDATEFORM ime_candidate_area;
+ LOGFONTW ime_font;
+ int ime_font_height;
 #endif // !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
 
 #ifndef SDL_DISABLE_WINDOWS_IME

```
