From 45c4536b80b4e18d25e64d12a1b4aa616c408bc5 Mon Sep 17 00:00:00 2001 From: Karsten Blees Date: Thu, 5 Aug 2010 22:45:33 +0000 Subject: [PATCH] Unicode console: fix font warning on Vista and Win7 GetCurrentConsoleFontEx in an atexit routine doesn't work because git closes stdout before exit (which also closes the console handle). Check the console font when we first encounter a non-ascii character and only schedule the warning message to be printed at exit (warnings go to stderr, which is not closed by git). Signed-off-by: Karsten Blees Signed-off-by: Erik Faye-Lund --- compat/winansi.c | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/compat/winansi.c b/compat/winansi.c index a5ca2d9be3..ab38ed95ab 100644 --- a/compat/winansi.c +++ b/compat/winansi.c @@ -28,7 +28,6 @@ static WORD plain_attr; static WORD attr; static int negative; static FILE *last_stream = NULL; -static int non_ascii_used = 0; typedef struct _CONSOLE_FONT_INFOEX { ULONG cbSize; @@ -42,14 +41,23 @@ typedef struct _CONSOLE_FONT_INFOEX { typedef BOOL (WINAPI *PGETCURRENTCONSOLEFONTEX)(HANDLE, BOOL, PCONSOLE_FONT_INFOEX); -static void warn_if_raster_font(void) +static void print_font_warning(void) { + warning("Your console font probably doesn\'t support Unicode. If " + "you experience strange characters in the output, consider " + "switching to a TrueType font such as Lucida Console!"); +} + +static void check_truetype_font(void) +{ + static int truetype_font_checked; DWORD fontFamily = 0; PGETCURRENTCONSOLEFONTEX pGetCurrentConsoleFontEx; - /* don't bother if output was ascii only */ - if (!non_ascii_used) + /* don't do this twice */ + if (truetype_font_checked) return; + truetype_font_checked = 1; /* GetCurrentConsoleFontEx is available since Vista */ pGetCurrentConsoleFontEx = GetProcAddress(GetModuleHandle("kernel32.dll"), @@ -72,9 +80,7 @@ static void warn_if_raster_font(void) } if (!(fontFamily & TMPF_TRUETYPE)) - warning("Your console font probably doesn\'t support " - "Unicode. If you experience strange characters in the output, " - "consider switching to a TrueType font such as Lucida Console!"); + atexit(print_font_warning); } static int is_console(FILE *stream) @@ -104,8 +110,6 @@ static int is_console(FILE *stream) attr = plain_attr = sbi.wAttributes; negative = 0; initialized = 1; - /* check console font on exit */ - atexit(warn_if_raster_font); } console = hcon; @@ -121,9 +125,12 @@ static int write_console(const char *str, size_t len) WriteConsoleW(console, wbuf, wlen, NULL, NULL); - /* remember if non-ascii characters are printed */ + /* + * if non-ascii characters are printed, check that the current console + * font supports this + */ if (wlen != len) - non_ascii_used = 1; + check_truetype_font(); /* return original (utf-8 encoded) length */ return len; -- 2.11.4.GIT