我怎么知道角色是否适合网格,如果不符合,它需要多少空格?

时间:2017-10-22 07:29:15

标签: unicode ncurses

使用ncurses,我如何知道某个角色是否适合网格?我认为这是依赖于字体的,我根本不确定如何做到这一点。

example text rendered in terminal emulator

所以在上面的例子中,我正在寻找的函数是:

grid_spaces_per_char(L"字") => 2
grid_spaces_per_char(L"G") => 1
grid_spaces_per_char(L"") => 2
grid_spaces_per_char(L"Q") => 1
grid_spaces_per_char(L"。") => 2

我需要知道这一点,以便我可以在支持UTF-8的C ++ Slack ncurses应用程序中实现自动换行。

如果不能单独使用ncurses,我应该怎么做才能获得这些信息?

2 个答案:

答案 0 :(得分:1)

wcwidth只是解决方案的一部分:ncurses将控制字符(除了空格)扩展为两个字符。 “简单”的方法是将字符写入未显示的窗口,并使用前/后位置找到ncurses在可见(refresh ed)窗口上使用的实际宽度。可以创建一个窗口,用于工作区并删除,而不会影响屏幕上显示的内容。

该技术用于Lynx

    /*
     * Determine the number of cells the given string would take up on the screen,
     * limited (in the case of wide characters) by the maxCells parameter.
     *
     * If the returnCellNum parameter is TRUE, return the number of cells;
     * otherwise, return the length (limited by the len parameter) of the prefix of
     * the string that fits in maxCells cells.
     */

以及注释

的ncurses-examples程序view
    /*
     * Use the curses library for rendering, including tab-conversion.  This
     * will not make the resulting array's indices correspond to column for
     * lines containing double-width cells because the "in_wch" functions will
     * ignore the skipped cells.  Use pads for that sort of thing.
     */

顺便说一下:

  • wcwidth在不同系统上给出不同的结果,它实际上可能与终端上显示的内容不一致。这是一种限制,因为标准的引入方式,而不是建立一个标准,存在相互矛盾的解释,不完整的文档等。

  • 它也应该(但显然很少)依赖于语言环境,因为某些字符在不同的语言环境中具有不同的宽度。在xterm中,这两个问题都与line-drawing

  • 有关
    /*
     * Solaris 10 wcwidth() returns "2" for all of the line-drawing (page
     * 0x2500) and most of the geometric shapes (a few are excluded, just
     * to make it more difficult to use).  Do a sanity check to avoid using
     * it.
     */

soft-hyphens

        /*
         * Regarding the soft-hyphen aberration, see
         * http://archives.miloush.net/michkap/archive/2006/09/02/736881.html
         */

答案 1 :(得分:0)

我通过使用一些不同的关键字找到了答案 - 答案是使用wcswidth中的wcwidthwchar.h

有一些警告,例如,Windows不包含此功能,有时可能会过时。更多警告解释了herehere

相关问题