在Windows控制台中显示unicode棋子 [英] Displaying unicode chess pieces in Windows-console

查看:421
本文介绍了在Windows控制台中显示unicode棋子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发一个控制台Chess游戏C ++(使用MVS2010),我似乎面临一个问题,我不能解决我自己。问题是,我需要在控制台中显示以下棋子:
http:// en .wikipedia.org / wiki / Chess_symbols_in_Unicode



我当然经历了大量的论坛,文章和文档,但仍然没有完成任务。我知道一些字符(特别是我需要的)不能使用Windows控制台提供的字体显示。但控制台只支持几种字体:consolas和lucida控制台。最后一个是好的足够显示大量的字符,但不是所有的。下面的代码段最接近我的需求之一:

  #include< Windows.h> 
#include< wchar.h>
int main()
{
U INT 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;
}

当使用它显示以下字符时:\x266B当Lucida控制台在使用)。但是当我尝试显示\ x265B它打印一个空的正方形,而不是棋子。
下面是一个到象棋人物的链接: http://unicode-table.com / ru /#geometric-shapes



下面的代码更清晰,更小,行为类似上面的代码:

  #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\\\
;
std :: wcout<< str<< std :: endl;
return 0;
}

似乎我现在需要的是找到一个可以显示的字体我需要的字符,但问题是,我可以在启动应用程序时,以编程方式配置控制台,以使其能够显示这样的符号?



提前感谢!

解决方案

我能够正确显示棋子。主要的问题是,默认控制台字体不包括棋子的字形。您可以通过安装DejaVu Sans Mono 作为控制台字体。



在这之后,有两种可能的方法(我使用MinGW- w64)。



使用UTF-16



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

请注意, wprintf 。我相信这是因为MS的控制台例程是可怕的,并且MinGW路由这些。



使用UTF-8



< p $ p> SetConsoleOutputCP(65001); //命令提示符UTF-8代码页
char q [] =King:\xE2\x99\x94.\\\
;
printf(q);

Cygwin注意: Cygwin似乎行为不同,取决于你是否有Raster选择字体或TTF字体。用DejaVu Sans Mono也用于Cygwin,两个选项都显示正确。


I've been developing a console Chess-game in C++ (using MVS2010) and I seem to have faced a problem I cannot solve on my own. The matter is that I need to have the following chess pieces displayed in console: http://en.wikipedia.org/wiki/Chess_symbols_in_Unicode

I certainly went through a great amount of forums, articles and documentations and still does not have the task done. I understand that some characters (in particular, the ones I need) cannot be displayed using fonts provided by Windows-console. But console supports only several fonts: consolas and lucida console. The last one is good enought for displaying great amount of characters, but not all of them. 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 using it to display the following character it works: \x266B (only when Lucida console is in use). But when I try to display \x265B it prints an empty square instead of chess piece. Here is a link to chess-characters: http://unicode-table.com/ru/#geometric-shapes

The following code-snipped is even much more clear and small and behaves like the one 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 all I need now is to find out a font that could display the characters I need, but the question is that can I programmatically configure console when starting application to make it able to display such symbols?

Thanks in advance!

解决方案

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

After doing that, 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 );

Note that the wprintf doesn't work. I believe this is because MS's console routines are terrible , and MinGW routes through those.

Using UTF-8

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

Cygwin note: Cygwin seems to behave differently depending on whether you have Raster Font or a TTF font chosen. With DejaVu Sans Mono used for Cygwin also, both options displayed correctly.

这篇关于在Windows控制台中显示unicode棋子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆