在windows终端中输出unicode字符

发布于 2024-08-10 20:42:32 字数 1701 浏览 16 评论 0原文

在过去的一周里,我一直在和一个朋友一起用 C++ 开发一个 roguelike 游戏。主要也是学习语言。

我正在使用:

随时随地输出 wchar_t我想在控制台中。我已经成功地输入了一些 unicode 字符,例如 \u263B (☻),但其他字符,例如 \u2638 (☸) 最终只会显示为问号 (?)。

这是我用于输出的相关代码。

// 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);
}

我当然可以依靠无聊的 ANSI 字符。但如果能使用完整的 unicode 字符集那就太棒了。

总结一下:如何确保 unicode 字符正确输出?


编辑:

好的,所以我发现我的编码工作正常。问题是我需要强制终端切换到更丰富的unicode 字体。有没有跨平台的方法来做到这一点?有没有一种特定于Windows的方法来做到这一点?

Over the past week I've been working on a roguelike game in C++ along with a friend. Mostly too learn the language.

I'm using:

To output wchar_t's wherever I want to in the console. I have succeeded in otuputting some unicode characters such as \u263B (☻), but others such as \u2638 (☸) will just end up as question marks(?).

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. But it would be really awesome to have the complete unicode-set of characters to play with.

To sum it up: How do you make sure that unicode characters are outputted correctly?


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. Is there a cross-platform way to do this? is there even a windows specific way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

溺渁∝ 2024-08-17 20:42:32

问题是我需要强制终端切换到更丰富的 unicode 字体。有没有跨平台的方法来做到这一点?有没有特定于 Windows 的方法来执行此操作?

我查了一下,但找不到 Windows API 调用来执行此操作(当然,这可能意味着我没有找到它)。我不希望找到一种跨平台的方法来做到这一点。

我能想到的最好的解决方案是使用专门构造的 Shell 链接 (.LNK) 文件。如果您阅读了文件格式文档,您会看到它允许您指定字体。

但你的问题还不止于此。 Windows 的西方语言环境安装提供 Lucida Console,但是字体仅提供有限的字素子集。我假设您可以在日语 Windows PC 上的控制台中输出/输入日语文本。如果您想确保它可以在日语 Windows 上运行,您需要检查哪些功能可以在日语 Windows 上使用。

Linux(至少是 Ubuntu)似乎在这里有更好的支持,使用 UTF-8 并提供具有广泛字形支持的字体。我没有检查其他发行版来了解其中的故事,也没有检查终端中如何解析字体(无论是 X 事物、Gnome 事物还是其他事物)。

The problem is that I need to force the terminal to switch to a more unicode-rich font face. Is there a cross-platform way to do this? is there even a windows specific way to do this?

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). 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. 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. I assume that you can output/input Japanese text in the console on a Japanese Windows PC. You'd need to check what is available on a Japanese Windows if you wanted to be sure it would work there.

Linux (Ubuntu, at least) seems to have much better support here, using UTF-8 and providing a font with broad grapheme support. 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).

不必在意 2024-08-17 20:42:32

原则上,控制台支持所有 unicode 字符。您看到问号的事实可能与这些字符的字体支持有关。尝试将控制台字体切换为具有良好 unicode 支持的字体。

In principle, all unicode characters are supported on the console. 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.

戏剧牡丹亭 2024-08-17 20:42:32

您需要正确设置代码页。这里有一篇关于此的非常好的文章: 链接

You need to set the code page properly. There's a pretty good article about that here: link

我恋#小黄人 2024-08-17 20:42:32

虽然这是一个相当大的切换,但您可以尝试使用替代控制台,特别是如果您正在玩 Roguelike 游戏。 libtcod 是一个相当流行的库,用 C 语言编写,带有 C++ 绑定,它提供了一个全彩控制台,您可以使用它可以设置您自己的字体和字形。这应该会给您带来更多的灵活性,而不是受制于 Windows 控制台提供的功能。

Although it's a pretty big switch, you might try using an alternative console, especially if you are doing a 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文