Fixed bug 2137 - SDL Message Boxes don't cope with fixed width fonts (in windows at least)
authorSam Lantinga <slouken@libsdl.org>
Mon, 14 Aug 2017 23:45:06 -0700
changeset 1130323000b73ce4e
parent 11302 657c10a0a705
child 11304 03d0ffb1f3dd
Fixed bug 2137 - SDL Message Boxes don't cope with fixed width fonts (in windows at least)

Pegasus Epsilon

With the system dialog font set to Arial or Tahoma or another variable-width font, everything works just as expected. When using a fixed-width font, like Courier or DejaVu Sans Mono, the text gets cut off. Example screenshots attached.
src/video/windows/SDL_windowsmessagebox.c
     1.1 --- a/src/video/windows/SDL_windowsmessagebox.c	Mon Aug 14 21:40:40 2017 -0700
     1.2 +++ b/src/video/windows/SDL_windowsmessagebox.c	Mon Aug 14 23:45:06 2017 -0700
     1.3 @@ -411,14 +411,24 @@
     1.4      {
     1.5          /* Get the metrics to try and figure our DLU conversion. */
     1.6          GetTextMetrics(FontDC, &TM);
     1.7 -        s_BaseUnitsX = TM.tmAveCharWidth + 1;
     1.8 +
     1.9 +        /* Calculation from the following documentation:
    1.10 +         * https://support.microsoft.com/en-gb/help/125681/how-to-calculate-dialog-base-units-with-non-system-based-font
    1.11 +         * This fixes bug 2137, dialog box calculation with a fixed-width system font
    1.12 +         */
    1.13 +        {
    1.14 +            SIZE extent;
    1.15 +            GetTextExtentPoint32A(FontDC, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", 52, &extent);
    1.16 +            s_BaseUnitsX = (extent.cx / 26 + 1) / 2;
    1.17 +        }
    1.18 +        /*s_BaseUnitsX = TM.tmAveCharWidth + 1;*/
    1.19          s_BaseUnitsY = TM.tmHeight;
    1.20      }
    1.21  
    1.22      /* Measure the *pixel* size of the string. */
    1.23      wmessage = WIN_UTF8ToString(messageboxdata->message);
    1.24      SDL_zero(TextSize);
    1.25 -    Size.cx = DrawText(FontDC, wmessage, -1, &TextSize, DT_CALCRECT);
    1.26 +    DrawText(FontDC, wmessage, -1, &TextSize, DT_CALCRECT);
    1.27  
    1.28      /* Add some padding for hangs, etc. */
    1.29      TextSize.right += 2;