Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Unicode console: fix font warning on Vista and Win7
Browse files Browse the repository at this point in the history
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 <blees@dcon.de>
Signed-off-by: Erik Faye-Lund <kusmabite@gmail.com>
  • Loading branch information
kblees authored and kasal committed May 29, 2014
1 parent f0a875b commit 981aa53
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions compat/winansi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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"),
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 981aa53

Please sign in to comment.