代码之家  ›  专栏  ›  技术社区  ›  Fredrick Brennan

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

  •  1
  • Fredrick Brennan  · 技术社区  · 8 年前

    使用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应用程序中实现文字包装。

    如果单靠网络课程无法做到这一点,我应该做些什么来获取这些信息?

    2 回复  |  直到 8 年前
        1
  •  1
  •   Thomas Dickey    8 年前

    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示例计划 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.
         */
    

    顺便说一句:

    • 在不同的系统上给出不同的结果,它可能实际上与终端上显示的不一致。这是由于引入标准的方式而造成的限制,并且存在相互冲突的解释、不完整的文档等,而不是一个接一个地构建标准。

    • 它还应该(但显然很少)依赖于区域设置,因为某些字符在不同的区域设置中具有不同的宽度。在里面 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
             */
    
        2
  •  0
  •   Fredrick Brennan    8 年前

    我自己通过使用一些不同的关键字找到了答案——答案是使用 wcswidth wcwidth 在中找到 wchar.h .

    有一些注意事项,例如,Windows不包括此功能,有时它可能已经过时。解释了更多注意事项 here here .

    推荐文章