简体   繁体   English

在windows终端中输出unicode字符

[英]Outputting unicode characters in windows terminal

Over the past week I've been working on a roguelike game in C++ along with a friend.在过去的一周里,我一直在和一个朋友一起用 C++ 开发一个roguelike游戏。 Mostly too learn the language.大多数情况下也学习语言。

I'm using:我正在使用:

To output wchar_t 's wherever I want to in the console.在控制台中我想要的任何地方输出wchar_t I have succeeded in otuputting some unicode characters such as \☻ (☻), but others such as \☸ (☸) will just end up as question marks(?).我已经成功地输出了一些 unicode 字符,例如 \☻ (☻),但是其他的例如 \☸ (☸) 最终会变成问号 (?)。

Here's the relevant code I use for output.这是我用于输出的相关代码。

// Container of room information

struct RoomInfo
{
    wchar_t * layout;
    int width;
    int height;
};

// The following function builds RoomInfo 

RoomInfo Room::examine(IActor * examinor)
{
    RoomInfo ri;
    ri.width = this->width;
    ri.height = this->height;
    ri.layout = new wchar_t[height * width];
    for(unsigned int y = 0; y < height; y++)
    {
        for(unsigned int x = 0; x < width; x++)
        {
            ri.layout[y*width + x] = L'\u263B'; // works
            //ri.layout[y*width + x] = L'\u2638'; // will not work
        }
    }
}

// The following function outputs RoomInfo

void CursesConsole::printRoom(RoomInfo room)
{
    int w = room.width;
    int h = room.height;

    WINDOW * mapw = newwin(h, w, 1, 0);
    for(int y = 0; y < h; y++)
    {
        wmove(mapw, y, 0);
        for(int x = 0; x < w; x++)
        {
            int c = y*w + x;
            waddch(mapw, room.layout[c]);
        }
    }

    wrefresh(mapw);
    delwin(mapw);
}

I could of course fall back on boring ANSI-characters.我当然可以依靠无聊的 ANSI 字符。 But it would be really awesome to have the complete unicode-set of characters to play with.但是拥有完整的 unicode 字符集真的很棒。

To sum it up: How do you make sure that unicode characters are outputted correctly?总结一下:如何确保unicode字符正确输出?


Edit:编辑:

Ok, so I figured out my encoding is working correctly.好的,所以我发现我的编码工作正常。 The problem is that I need to force the terminal to switch to a more unicode-rich font face .问题是我需要强制终端切换到更 unicode-rich font face Is there a cross-platform way to do this?有没有跨平台的方法来做到这一点? is there even a windows specific way to do this?甚至有一种特定于 Windows 的方法来做到这一点吗?

In principle, all unicode characters are supported on the console.原则上,控制台支持所有 unicode 字符。 The fact that you're seeing question marks probably has to do with font support for those characters.您看到问号的事实可能与对这些字符的字体支持有关。 Try switching the console font to something with very good unicode support.尝试将控制台字体切换为具有非常好的 unicode 支持的字体。

You need to set the code page properly.您需要正确设置代码页。 There's a pretty good article about that here: link这里有一篇很好的文章: 链接

The problem is that I need to force the terminal to switch to a more unicode-rich font face.问题是我需要强制终端切换到更丰富的 unicode 字体。 Is there a cross-platform way to do this?有没有跨平台的方法来做到这一点? is there even a windows specific way to do this?甚至有一种特定于 Windows 的方法来做到这一点吗?

I had a look for this, but couldn't find a Windows API call to do it (which may just mean I didn't find it, of course).我找了找这个,但找不到 Windows API 调用来做它(这可能只是意味着我没有找到它,当然)。 I would not expect to find a cross-platform way to do it.我不希望找到一种跨平台的方式来做到这一点。

The best solution I can think of is to launch the console using a specially constructed Shell Link (.LNK) file.我能想到的最佳解决方案是使用特殊构造的Shell Link (.LNK) 文件启动控制台。 If you read the file format documentation , you'll see that it allows you to specify a font.如果您阅读文件格式文档,您会看到它允许您指定字体。

But your problems don't end there.但是你的问题还不止这些。 A Western locale install of Windows provides Lucida Console , but that font only provides a limited subset of graphemes. Windows 的西方语言环境安装提供Lucida Console ,但该字体仅提供有限的字素子集。 I assume that you can output/input Japanese text in the console on a Japanese Windows PC.我假设您可以在日语 Windows PC 上的控制台中输出/输入日语文本。 You'd need to check what is available on a Japanese Windows if you wanted to be sure it would work there.如果您想确保它可以在那里工作,您需要检查日语 Windows 上可用的内容。

Linux (Ubuntu, at least) seems to have much better support here, using UTF-8 and providing a font with broad grapheme support. Linux(至少是 Ubuntu)在这里似乎有更好的支持,使用 UTF-8 并提供具有广泛字素支持的字体。 I haven't checked other distros to see what the story is there, nor have I checked how fonts are resolved in the terminal (whether it's an X thing, a Gnome thing or whatever).我没有检查其他发行版以了解那里的故事,也没有检查终端中字体的解析方式(无论是 X 的东西,Gnome 的东西还是其他什么)。

Although it's a pretty big switch, you might try using an alternative console, especially if you are doing a roguelike.尽管这是一个相当大的切换,但您可以尝试使用替代控制台,尤其是在您进行 roguelike 游戏时。 libtcod is a moderately popular library, written in C with C++ bindings, that provides a full color console which you can set up your own fonts and glyphs in. That should give you a lot more flexibility than being stuck with what the Windows console gives you. libtcod是一个中等流行的库,用 C 语言编写并带有 C++ 绑定,它提供了一个全彩色控制台,您可以在其中设置自己的字体和字形。这应该给您更多的灵活性,而不是被困在 Windows 控制台给您的东西上.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM