(153 intermediate revisions by the same user not shown)
My library for building complex, vibrant textual user interfaces (TUIs) on modern terminal emulators. It does not use [[Ncurses]] (though it does make use of libtinfo from that package), nor is it an X/Open Curses source-compatible replacement.
library for building complex, vibrant textual user interfaces (TUIs) on modern terminal emulators. It does not use [[Ncurses]] (though it does make use of libtinfo from that package), nor is it an X/Open Curses source-compatible replacement.
Source and issue-tracking live at [https://github.com/dankamongmen/notcurses Github].
Source and live at [https://github.com/dankamongmen/notcurses Github].
* [[outcurses]], my effects and widgets library for [[Ncurses]]
* [[outcurses]], my effectswidgets library for [[Ncurses
Latest revision as of 21:34, 14 January 2022
notcurses is a library for building complex, vibrant textual user interfaces (TUIs) on modern terminal emulators. It does not use Ncurses (though it does make use of libtinfo from that package), nor is it an X/Open Curses source-compatible replacement. It is written in C, with C++-safe headers. Rust, C++, and Python wrappers are available.
Multiple cell and pixel blitters, rotation, and arbitrary scaling
The vast majority of functions draw to ncplane objects. A partial order (currently a total order) always exists over the planes. There is always at least one plane, the "standard plane". This plane cannot be resized, deleted, moved relative to the visible area, or reparented. Whenever notcurses updates its idea of the visible area's dimensions, it will resize the standard plane (references to the standard plane are not invalidated). Planes may otherwise be any size (invisible regions will not be rendered, and count only against memory), and can be moved to any position relative to the visible area. All planes, including the standard plane, can be freely moved along the z axis.
A render operation consists of two logical phases: generation of the rendered scene, and blitting this scene to the terminal (these two phases might actually be interleaved, streaming the output as it is rendered). All ncplanes are locked while generating the frame. Frame generation requires determining an extended grapheme cluster, foreground color, background color, and style for each cell of the physical terminal. Writing the scene requires synthesizing a set of UTF-8-encoded characters and escape codes appropriate for the terminal, and writing this sequence to the output.
Each cell can be rendered in isolation, though synthesis of the stream carries dependencies between cells.
Recall that there is a total ordering on the N ncplanes, and that the standard plane always exists, with geometry equal to the physical screen. Each cell of the physical screen is thus intersected by some totally ordered subset of planes P0, P1...Pi, where 0 < i ≤ N. At each cell, rendering starts at the topmost intersecting plane P0. The algorithm descends until either:
it has locked in an extended grapheme cluster, and fore/background colors, or
all i planes have been examined
At each plane P, we consider a cell C. This cell is the intersecting cell, unless that cell has no EGC. In that case, C is the plane's default cell.
If we have not yet determined an EGC, and C has a non-zero EGC, use the EGC and style of C.
If we have not yet locked in a foreground color, and C is not foreground-transparent, use the foreground color of C. If C is CELL_ALPHA_OPAQUE, lock the color in.
If we have not yet locked in a foreground color, and C is not background-transparent, use the background color of C. If C is CELL_ALPHA_OPAQUE, lock the color in.
If the algorithm concludes without an EGC, the cell is rendered with no glyph and a default background. If the algorithm concludes without a color locked in, the color as computed thus far is used.
Notcurses understands Unicode wide characters, and accounts for them. It is not possible to split the colors or styling of a wide glyph. It is not possible to print half of a glyph. It is not possible to print a narrow glyph over half of a wide glyph without obliterating the other half. These are all terminal emulator limitations.
Whether and how a given Unicode codepoint will be rendered depends almost entirely on font support. In general, with sufficient fonts, emoji and other pictographs will be properly rendered as expected. The Linux console has particularly limited fonts, and most characters beyond ASCII are not reliable in that environment. Certain Unicode glyphs are used by notcurses for drawing. Failure to render these glyphs reasonably will have a negative impact on notcurses functionality. Most important of these are the Box Drawing Characters, the Block Elements, and the Braille Patterns.
A wide character can be written at any offset within a row save the very last column. If a character (wide or narrow) exists in the right cell of a wide character, the original character will be destroyed. A narrow character can be written at any offset within a row. If a character (wide or narrow) is written onto the right cell of a wide character, that original character will be destroyed. It is thus possible for a single wide character to obliterate four columns' worth of glyphs: if two wide characters exist next to one another, and a new wide character is written over the right half of the first, both original glyphs will be obliterated. Likewise, if another (higher) plane bisects a wide glyph, the entire glyph is obliterated.
notcurses does not currently handle right-to-left text in any special way, but terminals often apply their own heuristics and stylings. Generally this means that a series of glyphs from right-to-left languages will be reversed in the terminal, but this will not be detectable by notcurses's reflective calls (e.g. ncplane_at_yx()).
Multiple blitters are provided, and can be selected whenever pixel data is being rendered. This includes ncvisual objects and qrcodes. If Notcurses is started in ASCII mode (as opposed to UTF-8), all blitters will degrade to NCBLITTER_1x1 (unless NCVISUAL_OPTION_NODEGRADE is provided).
Uses spaces and sets the background color. The only blitter available in ASCII mode, and the only reliable blitter on the console. Pixel aspect ratio is equivalent to cell aspect ratio, usually resulting in vertical stretching. Lossless. Reliable no matter the font.
Default blitter. Pixel aspect ratio is one-half the cell aspect ratio, which is usually right where you want it. Uses Unicode upper- and lower-half blocks, and spaces. Lossless.
Pixel aspect ratio is equivalent to cell aspect ratio, usually resulting in vertical stretching. Uses Unicode quadrant and three-quarter blocks (in addition to upper- and lower-half blocks, and spaces). Lossy whenever more than two colors are used within a 2x2 pixel square, lossless otherwise (bi- and tri-linear interpolation is used for more than two colors).
Highest quality for most large images. Pixel aspect ratio improves over NCBLITTER_2x2 but is less perfect than NCBLITTER_2x1, leading to slight vertical stretching. Uses Unicode sextants, left and right half blocks, and spaces). Lossy whenever more than two colors are used within a 3x2 pixel square, lossless otherwise (generalized linear interpolation is used for more than two colors).
Uses Braille characters, which have spotty font support.
Pixel blitter (see below)
Some terminals support pixel graphics, including the Sixel system pioneered by DEC. Notcurses can employ pixel graphics using the NCBLIT_PIXEL blitter.
Notcurses provides input from keyboards and mice. Single Unicode codepoints are received from the keyboard, directly encoded as char32_ts. The input system must deal with numerous keyboard signals which do not map to Unicode code points. This includes the keypad arrows and function keys. These "synthesized" codepoints are enumerated in notcurses.h, and mapped into the Supplementary Private Use Area-B (U+100000..U+10FFFD). Mouse button events are similarly mapped into the SPUA-B.
All events carry a ncinput structure with them. For mouse events, the x and y coordinates are reported within this struct. For all events, modifiers (e.g. "Alt") are carried as the modifiers bitfield in this struct.
It is not obvious what "transparency" or "alpha blending" means in a character context. I have assigned my own meanings. Each of the foreground and background channel of an ncplane's cell have two bits dedicated to alpha. Channels are always initialized to 0, and thus the default alpha setting is CELL_ALPHA_OPAQUE. It is important to note that glyph selection is independent of alpha. The first glyph found while descending the planes intersecting a cell will be the glyph used. Only presentation of the glyph is modified by alpha.
Use the fg color unchanged.
Use the bg color unchanged.
Blend the fg color down.
Blend the bg color down.
Select the next fg color.
Select the next bg color.
Complement bg color computed through this plane.
The value 11 is currently forbidden for a bg alpha setting, but might be used in the future. To "blend the color down" means to average the colors encountered until hitting an opaque channel, or running out of planes. To "select the next color" means to ignore this color, and instead take the color as computed by lower planes.
High-contrast text is not strictly defined. notcurses will attempt to make the glyph as readable as possible, given the background color computed at the cell.
If loaded multimedia supports transparency (e.g. PNGs), transparent pixels will be considered as if CELL_ALPHA_TRANSPARENT was used.
Transparency plus bitmaps yield sprites. The "luigi" demo uses three bitmaps from Luigi's walking cycle in Super Mario Bros. Only one is shown at a time; the other two are hidden under the standard plane (onto which a background has been rendered).
notcurses can color lines via linear interpolation between the two endpoints. This is done with ncplane_hline_interp() and ncplane_vline_interp(). If provided two endpoints of the same color, the line will be that single color.
More information is available from the source tree in TERMINALS.md.
The two primary environmental factors affecting notcurses performance are the terminal emulator and the configured fonts.
For performance evaluation, I run notcurses-demo in each terminal three times. Each terminal is configured to use Inconsolata Medium 12 as its primary font, to 70% opacity (if supported), and run at a 70x80 geometry. I report the average of the three wall clock times, and the three FPS (i.e. frames rendered divided by time spent within notcurses_render()) measurements. If DirectColor output could not be rendered, the terminal is reported as a failure (this is perhaps/probably due to my ignorance). Some demos are a fixed number of frames, with a fixed target time. Some are a fixed number of frames, to be rendered as quickly as possible. Some are fixed time, with an intention of rendering as many frames as possible.
For the FPS measurement, higher is better. For the time measurement, lower is better.
Last evaluated 2019-12-21 (notcurses 0.9.1)
Perf FPS / Seconds
eagle and view were very choppy widecolor doesn't colorize flame emoji uniblock ran without box breakage (only terminal to do so)
uniblock doesn't fade in properly uniblock persistent damage on one line boldface seems particularly strong
Note: some of these terminals (e.g. xterm) export an RGB-capable setaf/setab, which notcurses does not use due to its special casing of low values. notcurses instead generates (for seta[bf] only) its own escapes outside of the terminfo framework. I am uncertain as to whether this might have performance effects. The "T.416" column indicates whether this terminal appears to support the colon-delimited RGB escapes from ITU T.416.
The Linux "system console" is a virtual device mapping to some device. We're primarily interested in the VGA text console, and the framebuffer console. The console supports only 256 different glyphs (512 with diminished color support), and never more than 16 colors (8 with 512 glyphs).
You'll typically need to be a member of the video group to run fbterm. It offers much better Unicode glyph coverage than the raw console, but still offers only 16 (programmable) colors.