From 7f96b51959ac3922c9cf51f8a7ecbd8736cb6f2c 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 bf514f9de5..bec6713b74 100644 --- a/compat/winansi.c +++ b/compat/winansi.c @@ -29,7 +29,6 @@ static WORD plain_attr; static WORD attr; static int negative; static FILE *last_stream = NULL; -static int non_ascii_used = 0; #ifdef __MINGW32__ typedef struct _CONSOLE_FONT_INFOEX { @@ -45,14 +44,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 = (PGETCURRENTCONSOLEFONTEX) GetProcAddress( @@ -75,9 +83,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) @@ -107,8 +113,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; @@ -124,9 +128,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