win32: use the Windows message font like GTK 3

Segoe UI 9, or Tahoma 8 on XP. Before pango 1.43 this is left to pango
for any font but Segoe UI. A theme that sets a font still wins.

Backported from GTK 3 commit 9e7cc89f4a by Christoph Reiter.
This commit is contained in:
Daemonratte 2026-10-18 10:41:33 +02:00
commit 726e282cff

View file

@ -29,6 +29,7 @@
#include <string.h>
#include <stdlib.h>
#include <glib/gprintf.h>
#include <pango/pangowin32.h>
#include "gdkscreen.h"
#include "gdkproperty.h"
@ -311,6 +312,49 @@ gdk_property_delete (GdkWindow *window,
}
}
/* the message font of Windows as a pango font name with its size in
* points, or NULL; bitmap fonts are left out, pango can not use them */
static gchar *
get_system_font_name (HDC hdc)
{
NONCLIENTMETRICSW ncm;
PangoFontDescription *font_desc;
gchar *result, *font_desc_string;
int logpixelsy;
gint font_size;
/* without iPaddedBorderWidth, which Windows before Vista refuses */
ncm.cbSize = G_STRUCT_OFFSET (NONCLIENTMETRICSW, lfMessageFont) + sizeof (LOGFONTW);
if (!SystemParametersInfoW (SPI_GETNONCLIENTMETRICS, ncm.cbSize, &ncm, 0))
return NULL;
if (ncm.lfMessageFont.lfFaceName[0] == 0 ||
ncm.lfMessageFont.lfOutPrecision == OUT_STRING_PRECIS ||
ncm.lfMessageFont.lfHeight >= 0)
return NULL;
logpixelsy = GetDeviceCaps (hdc, LOGPIXELSY);
if (logpixelsy <= 0)
return NULL;
font_desc = pango_win32_font_description_from_logfontw (&ncm.lfMessageFont);
pango_font_description_unset_fields (font_desc, PANGO_FONT_MASK_SIZE);
font_desc_string = pango_font_description_to_string (font_desc);
pango_font_description_free (font_desc);
font_size = -MulDiv (ncm.lfMessageFont.lfHeight, 72, logpixelsy);
if (font_size <= 0)
{
g_free (font_desc_string);
return NULL;
}
result = g_strdup_printf ("%s %d", font_desc_string, font_size);
g_free (font_desc_string);
return result;
}
/*
For reference, from gdk/x11/gdksettings.c:
@ -426,41 +470,29 @@ gdk_screen_get_setting (GdkScreen *screen,
g_value_set_boolean (value, TRUE);
return TRUE;
}
#if 0
/*
* With 'MS Sans Serif' as windows menu font (default on win98se) you'll get a
* bunch of :
* WARNING **: Couldn't load font "MS Sans Serif 8" falling back to "Sans 8"
* at least with testfilechooser (regardless of the bitmap check below)
* so just disabling this code seems to be the best we can do --hb
*/
else if (strcmp ("gtk-font-name", name) == 0)
{
NONCLIENTMETRICS ncm;
ncm.cbSize = sizeof(NONCLIENTMETRICS);
if (SystemParametersInfo (SPI_GETNONCLIENTMETRICS, ncm.cbSize, &ncm, FALSE))
{
/* Pango finally uses GetDeviceCaps to scale, we use simple
* approximation here.
*/
int nHeight = (0 > ncm.lfMenuFont.lfHeight ? -3*ncm.lfMenuFont.lfHeight/4 : 10);
if (OUT_STRING_PRECIS == ncm.lfMenuFont.lfOutPrecision)
GDK_NOTE(MISC, g_print("gdk_screen_get_setting(%s) : ignoring bitmap font '%s'\n",
name, ncm.lfMenuFont.lfFaceName));
else if (ncm.lfMenuFont.lfFaceName && strlen(ncm.lfMenuFont.lfFaceName) > 0 &&
/* Avoid issues like those described in bug #135098 */
g_utf8_validate (ncm.lfMenuFont.lfFaceName, -1, NULL))
{
char* s = g_strdup_printf ("%s %d", ncm.lfMenuFont.lfFaceName, nHeight);
GDK_NOTE(MISC, g_print("gdk_screen_get_setting(%s) : %s\n", name, s));
g_value_set_string (value, s);
/* The message font of Windows ("Segoe UI 9", "Tahoma 8" on XP), as
* in GTK+ 3. A theme that sets gtk-font-name still wins. Before
* pango 1.43 the fallback list of pango only gave a good coverage
* of glyphs for "Segoe UI", so any other font is left to pango. */
gchar *font_name = get_system_font_name (_gdk_display_hdc);
g_free(s);
return TRUE;
}
if (font_name &&
pango_version_check (1, 43, 0) != NULL &&
g_ascii_strncasecmp (font_name, "Segoe UI", strlen ("Segoe UI")) != 0)
{
g_free (font_name);
font_name = NULL;
}
if (font_name)
{
GDK_NOTE(MISC, g_print("gdk_screen_get_setting(\"%s\") : %s\n", name, font_name));
g_value_take_string (value, font_name);
return TRUE;
}
}
#endif
return FALSE;
}