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

Commit aa7bfb8

Browse files
kbleeskasal
authored andcommitted
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 <[email protected]> Signed-off-by: Erik Faye-Lund <[email protected]>
1 parent 005aa6e commit aa7bfb8

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

compat/winansi.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ static WORD plain_attr;
2828
static WORD attr;
2929
static int negative;
3030
static FILE *last_stream = NULL;
31-
static int non_ascii_used = 0;
3231

3332
typedef struct _CONSOLE_FONT_INFOEX {
3433
ULONG cbSize;
@@ -42,14 +41,23 @@ typedef struct _CONSOLE_FONT_INFOEX {
4241
typedef BOOL (WINAPI *PGETCURRENTCONSOLEFONTEX)(HANDLE, BOOL,
4342
PCONSOLE_FONT_INFOEX);
4443

45-
static void warn_if_raster_font(void)
44+
static void print_font_warning(void)
4645
{
46+
warning("Your console font probably doesn\'t support Unicode. If "
47+
"you experience strange characters in the output, consider "
48+
"switching to a TrueType font such as Lucida Console!");
49+
}
50+
51+
static void check_truetype_font(void)
52+
{
53+
static int truetype_font_checked;
4754
DWORD fontFamily = 0;
4855
PGETCURRENTCONSOLEFONTEX pGetCurrentConsoleFontEx;
4956

50-
/* don't bother if output was ascii only */
51-
if (!non_ascii_used)
57+
/* don't do this twice */
58+
if (truetype_font_checked)
5259
return;
60+
truetype_font_checked = 1;
5361

5462
/* GetCurrentConsoleFontEx is available since Vista */
5563
pGetCurrentConsoleFontEx = GetProcAddress(GetModuleHandle("kernel32.dll"),
@@ -72,9 +80,7 @@ static void warn_if_raster_font(void)
7280
}
7381

7482
if (!(fontFamily & TMPF_TRUETYPE))
75-
warning("Your console font probably doesn\'t support "
76-
"Unicode. If you experience strange characters in the output, "
77-
"consider switching to a TrueType font such as Lucida Console!");
83+
atexit(print_font_warning);
7884
}
7985

8086
static int is_console(FILE *stream)
@@ -104,8 +110,6 @@ static int is_console(FILE *stream)
104110
attr = plain_attr = sbi.wAttributes;
105111
negative = 0;
106112
initialized = 1;
107-
/* check console font on exit */
108-
atexit(warn_if_raster_font);
109113
}
110114

111115
console = hcon;
@@ -121,9 +125,12 @@ static int write_console(const char *str, size_t len)
121125

122126
WriteConsoleW(console, wbuf, wlen, NULL, NULL);
123127

124-
/* remember if non-ascii characters are printed */
128+
/*
129+
* if non-ascii characters are printed, check that the current console
130+
* font supports this
131+
*/
125132
if (wlen != len)
126-
non_ascii_used = 1;
133+
check_truetype_font();
127134

128135
/* return original (utf-8 encoded) length */
129136
return len;

0 commit comments

Comments
 (0)