Displaying Unicode Chess Pieces in Windows Console

I was developing a console chess game in C ++ (using MVS2010) and I seem to be running into a problem that I cannot solve on my own. The thing is, I need the following chess pieces to be displayed in the console: http://en.wikipedia.org/wiki/Chess_symbols_in_Unicode

I have, of course, gone through a large number of forums, articles and documentation and still have not completed the task. I understand that some characters (in particular the ones I need) cannot be displayed using the fonts provided by the Windows console. But the console only supports a few fonts: console and lucida console. The last one is good enough for displaying a lot of characters, but not all. The snippet below is one of the closest to my needs:

#include <Windows.h>
#include <wchar.h>
int main()
{
    UINT oldcp = GetConsoleOutputCP();
    SetConsoleOutputCP(CP_UTF8);

    wchar_t s[] = L"\x266B";
    int bufferSize = WideCharToMultiByte(CP_UTF8, 0, s, -1, NULL, 0, NULL, NULL);
    char* m = new char[bufferSize]; 
    WideCharToMultiByte(CP_UTF8, 0, s, -1, m, bufferSize, NULL, NULL);
    wprintf(L"%S", m); 
    delete[] m; 

    SetConsoleOutputCP(oldcp);

    return 0;
}

      

When used to display the next character, it works: \ x266B (only when using the Lucida console). But when I try to display \ x265B, it prints an empty square instead of a chess piece. Here is a link to the chess symbols: http://unicode-table.com/en/#geometric-shapes

The following code snapshot is even much clearer and smaller and behaves like above:

#include <iostream>
#include <io.h>
#include <fcntl.h>
#include <Windows.h>
int main(){

    //_setmode(_fileno(stdout), _O_U8TEXT);
    //_setmode(_fileno(stdin), _O_U8TEXT);

    _setmode(_fileno(stdout), _O_U16TEXT);
    _setmode(_fileno(stdin), _O_U16TEXT);

    wchar_t * str=L"\x265B\n";
    std::wcout<<str<<std::endl;
    return 0;
}

      

It seems that I only need to figure out a font that can display the characters I want, but the question is, can I programmatically configure the console when the application starts up so that it can display such characters?

Thanks in advance!

+3


source to share


1 answer


I was able to display the chess pieces correctly. The main problem is that the default console font does not contain glyphs for chess pieces. You can fix this by installing DejaVu Sans Mono as a console font.

After this there are two possible approaches (I am using MinGW-w64).

Using UTF-16

HANDLE cons = GetStdHandle(STD_OUTPUT_HANDLE);
wchar_t p[] = L"Queen: \u265B.\n";
// wprintf(p);
DWORD n;
WriteConsoleW(cons, p, wcslen(p), &n, NULL );

      



Please note that wprintf

does not work. I believe this is because the MS console routines are terrible and MinGW routes them.

Using UTF-8

SetConsoleOutputCP(65001);        // Command prompt UTF-8 code page
char q[] = "King: \xE2\x99\x94.\n";
printf(q);

      

Cygwin's note: Cygwin seems to behave differently depending on whether you have a bitmap font or a TTF font of choice. With DejaVu Sans Mono for Cygwin, both options are also displayed.

+4


source







All Articles