ncurses 如何输出非ascii 字符? [英] How does ncurses output non-ascii characters?

查看:83
本文介绍了ncurses 如何输出非ascii 字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 ncurses(一个 c 库)如何设法放置像 这样的字符,尽管它们(据我所知)不是 ASCII 的一部分.

I'd like to know how ncurses (a c library) manages to put characters like , despite them not (to the best of my knowledge) being part of ASCII.

我认为它只是逐个像素地绘制它们,但您可以将它们复制/粘贴到终端之外(在 MacOS 中).

I would have assumed it was just drawing them pixel by pixel, but you can copy/paste them out of the terminal (in MacOS).

推荐答案

ncurses 通过假设您的语言环境环境变量(LC_ALL 和/or LC_CTYPE) 匹配您正在显示的终端.环境变量表示编码(例如,UTF-8).还有其他编码和终端支持这些编码,但一般来说,您大多会看到 UTF-8.如果环境和终端配合,事情就正常工作":

ncurses puts characters such as ├ on the screen by assuming that your locale environment variables (LC_ALL and/or LC_CTYPE) match the terminal on which you are displaying. The environment variables indicate the encoding (e.g., UTF-8). There are other encodings and terminals which support those encodings, but generally speaking you'll mostly see UTF-8. If the environment and terminal cooperate, things "just work":

  • 在启动时,ncurses 通过 setlocale 检查程序已初始化的语言环境,并确定它是否使用 UTF-8.它稍后会使用该信息.
  • 当程序添加字符串时,例如使用 addstr,ncurses 使用字符类型信息(设置为调用 setlocale 的副作用),并使用用于组合构成多字节字符的字节序列并将其转换为宽字符的标准 C 库函数.它在内部存储这些宽字符,并且
  • 写入终端时,ncurses 会反转该过程,从宽字符转换为使用终端支持的假定编码(假设您的语言环境与终端匹配).
  • at startup, ncurses checks for the locale which a program has initialized, via setlocale, and determines if that uses UTF-8. It uses that information later.
  • when a program adds character strings, e.g., using addstr, ncurses uses the character-type information (set as a side-effect of calling setlocale), and uses standard C library functions for combining sequences of bytes which make up a multi-byte character, and converting those into wide characters. It stores those wide characters internally, and
  • when writing to the terminal, ncurses reverses the process, converting from wide characters to use the encoding assumed to be supported by the terminal (assuming that your locale environment matches the terminal).

然而—

表示的字符恰好是一个特例.这是用于画线的图形字符之一,早于 Unicode 和 UTF-8.curses 为这些图形字符命名,以便于引用它们,例如,ACS_LTEE(是左球座):

The character indicated happens to be a special case. That is one of the graphic characters used for line-drawing, which predate Unicode and UTF-8. curses has names for these graphic characters, making it simple to refer to them, e.g., ACS_LTEE (the is a left-tee):

  • 在 UTF-8 出现使事情变得复杂之前,开发人员通过调整用于 VT100(1970 年代后期)和 AT&T 4410 和 5410 终端(显然是1980 年代早期,因为后者在 1984 年使用)用于绘制图形字符.
  • AT&T SystemV curses 从 1980 年代中期开始支持这些图形字符.BSD 诅咒从来没有这样做过......
  • Unicode(大约 1990 年及以后)使用不同的编码提供了大部分相同的字形.有一些遗漏(最明显的是用于水平线的扫描线上方/下方的扫描线),但是一旦 UTF-8 在 2000 年代初开始使用,扩展 ncurses 是合乎逻辑的使用这些字符.
  • ncurses 查看语言环境设置,但更喜欢对这些图形字符使用终端描述,除非已知这种方式不起作用 —并且将假设如果终端假定使用 UTF-8,终端可以显示这些字符的 Unicode 等效项.为此,它使用了一个表(SystemV curses 及其后继 X/Open Curses 没有执行任何此类操作——NetBSD curses 在 2010 年之后的某个时间从 ncurses 改编了该表).
  • Before UTF-8 came along to complicate things, developers came up with a scheme using a table of these graphic characters by adapting the escape sequences used for the VT100 (late 1970s) and the AT&T 4410 and 5410 terminals (apparently the early 1980s since the latter were in use by 1984) for drawing their graphic characters.
  • AT&T SystemV curses provided support for these graphic characters from the mid-1980s. BSD curses never did that...
  • Unicode (roughly 1990 and later) provided most of the same glyphs using a different encoding. There are a few omissions (the most noticeable are the scan lines above/below the one used for horizontal lines), but once UTF-8 got into use in the early 2000s, it was logical to extend ncurses to use these characters.
  • ncurses looks at the locale settings, but prefers using the terminal description for these graphic characters except for cases where that is known to not work — and will assume that the terminal can display the Unicode equivalents for these characters if the terminal is assumed to use UTF-8. It uses a table for this purpose (SystemV curses and its successor X/Open Curses didn't do any of this — NetBSD curses adapted the table from ncurses sometime after 2010).

进一步阅读:

  • NCURSES_NO_UTF8_ACS
  • Line Graphics (in curs_addch(3x))
  • Line Graphics (in curs_add_wch(3x))

这篇关于ncurses 如何输出非ascii 字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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