I am writing a C++ class permitting the usage of color in terminal. I want it works on every terminal :
printing with true color (24 bits) on terminals that supports it,
with 256 color (6x6x6) on terminals that supports it,
else with the basic 16 colors.
I wrote once C functions using termcap, and I thought using it in this case. However, the man page says :
The termcap database is an obsolete facility for describing the capabilities of character-cell terminals and printers. It is retained only for capability with old programs; new ones should use the terminfo database and associated libraries.
So I tried to use terminfo, but I could not find how to do this. There is not terminfo.h in my system (I run on Debian).
My questions is :
How can I get the color possibilities of the current terminal in C/C++, using the newest tools (ie not termcap, according to manpage) ?
The short answer is that you could not get the information from terminfo until ncurses 6.1 was released in January 2018.
The longer answer:
to effectively use TrueColor, you need an interface handling 3 parameters (for red, green, blue). Termcap cannot do this. Terminfo can handle multiple parameters, but...
there is no standard terminal capability (a name for a feature which may be a boolean, number or a string) dealing with TrueColor as such.
you could adapt existing capabilities, but they have limitations
Looking at the terminfo(5) manual, you might see these (strings):
initialize_color initc Ic initialize color #1
to (#2,#3,#4)
initialize_pair initp Ip Initialize color
pair #1 to
fg=(#2,#3,#4),
bg=(#5,#6,#7)
which are related to these (numbers):
max_colors colors Co maximum number of
colors on screen
max_pairs pairs pa maximum number of
color-pairs on the
screen
ANSI colors and schemes compatible with those (such as 16-, 88- and 256-colors) assume you are coloring foreground and background in pairs. The reason for that was that long ago, hardware terminals just worked that way. The initialize_color capability is for a different scheme (Tektronix), which might seem useful.
However, terminfo is compiled, and the resulting binary files stored only signed 16-bit integers. You could not use the terminal description to store a suitable max_pairs or max_colors for 24-bit color. (termcap stores everything as strings, but as noted is unsuited for this application).
A couple of years after this question and answer were first written, terminfo was updated to use a new file format that uses signed 32-bit integers, which is enough for expressing the number of colours in 24-bit RGB colour.
More details can be found in release announcement for ncurses 6.1 and in the updated term(5) manual page, which latter notes that there are still restrictions in the old API used by some applications that access the terminfo data directly.
Further reading:
Why only 16 (or 256) colors? (ncurses FAQ)
Can I set a color by its number? (xterm FAQ)
Is ncurses terminfo compatible with my system? (ncurses FAQ)
Related
I just started to use ncurses on Linux. I wanted to use more than 8 colors, but there were only 8 available.
How can I use more colors, or create my own by giving them a names, and set their RGB values?
I tried editing a color with init_color, but that will simply replace one of the current 8 instead of creating new ones.
Note: the value of the global var COLORS is 256, so I believe I can use up to 256 different colors.
If your terminal supports it, you should choose (or customize) a terminal description which has more than 8 colors. As it is, there are a lot of existing terminal descriptions which could be used: most are customized for particular terminals (or terminal emulators).
If your terminal supports it, the corresponding description would have the capability initc. That is used by the library call init_color. The xterm-256color entry has this for instance:
initc=\E]4;%p1%d;rgb\:%p2%{255}%*%{1000}%/%2.2X/%p3%{255}%*%{1000}%/%2.2X/%p4%{255}%*%{1000}%/%2.2X\E\\,
which tells the library how to translate the three parameters into an escape sequence. The terminal entries are built up from reusable parts such as xterm+256color, because some terminals lack the feature you are interested in. For those, xterm+256setaf is appropriate.
For reference,
Terminal Database Most systems have this as base (small) and extra (large) packages.
start_color, init_pair, init_color, has_colors, can_change_color, color_content, pair_content, COLOR_PAIR - curses color manipulation routines
My terminal doesn't recognize color
Why only 16 (or 256) colors?
The constant COLOR_PAIRS defines how many colour pairs a system will allow. See:
http://pubs.opengroup.org/onlinepubs/7908799/xcurses/can_change_color.html.
You can substitute your own numbers in init_color to define new ones. eg init_color(9, 800, 700, 600);
I have a single 8-bit channel buffer whose contents I would like to see in the middle of a debugging session. Using whatever might be available under Xcode/LLDB, can this be done? A solution that uses a separate window (or even dumping to an external file) is fine.
What changes about the answer if the image is 8-bit planar RGBA?
I think you are asking about:
https://developer.apple.com/library/ios/documentation/IDEs/Conceptual/CustomClassDisplay_in_QuickLook/Introduction/Introduction.html
This was answered here in:
How can I Quick Look custom objects with Xcode 5 visual debugger?
You have to be able to convert your image to a UIImage or some other type that the QuickLook system natively understands.
My requirement is to display string of any language on the screen.
Currently we are using opengl to display English characters.
Same APIs are not working for other languages. Instead of characters, boxes are displayed on screen.
Can someone help in understanding opengl and find appropriate APIs to display charterers of any language?
Currently we are using opengl to display English characters.
No, you're not using OpenGL. How do I know this? Because OpenGL does not do text rendering. All it does it points, lines and triangles.
What you're using is some library that knows how to draw characters with points, lines and triangles and then uses OpenGL to get that job done. And the particular library you're using apparently doesn't know, how to deal with characters outside of the ASCII character set.
Of course it's not just that what matters. Encoding matters as well. The most recent versions of C++ support Unicode in program sources (so that you can write unicode in string literals), but that does not automatically give you unicode support in your program – it's just the compiler who knows how to deal with it, but that knowledge does not automatically transpire into the compiled program.
So far there is only one operating system in which Unicode support is so deeply ingrained that no extra work is required; in fact a particular way of encoding Unicode was invented for it, but unfortunately this is one of the most niche OS projects there is around: Plan9
Apart from Unicode, there are also many other character encoding schemes, all incompatible with each other, each for a particular kind of writing. Which means, that it's also impossible to mix characters from different writing systems in texts encoding with such localized characters sets. Hence a universal encoding scheme was invented.
You're most likely on Windows, Linux, BSD, Solaris or MacOS X. And in all of them making non-ASCII-characters work means extra work for you, the programmer. MacOS X is probably the one OS with the least barrier of entry.
So here are the questions you have to answer for yourself:
what character encoding used (hopefully Unicode)?
does the text renderer library used support code points in that encoding?
does the text renderer library come with a layout engine (the thing that positions characters) or does this have to be supplied extra?
Among the existing text renderers that can draw to OpenGL, currently Freetype-GL is the most capable; it has support for Unicode
https://github.com/rougier/freetype-gl
I'm looking for an open source (or cheap commercial) library that handles the difficulty of formatting information for standard label paper (say Avery 5160) for example. I would like to use the library in a Mac and iPhone app. I saw this similar question for a dedicated label printer, but I am looking for a Mac compatible library that will work with label paper loaded into any printer:
What options are available to developers for printing to a dedicated label printer?
Alternatively, I'm interested in suggestions about my best strategy for doing it myself... I'm not up for figuring out the intricacies of many different paper formats - I would probably target just one kind of US Letter label paper and one kind of A4 label paper. Which are the most popular?
Having written a (closed source) Mac codebase that actually does this, I can tell you that it's not the most difficult thing in the world. My codebase only supports Avery labels. I have a resource file in the app with geometry info for each kind of label (number of rows, columns, margin sizes, gutter sizes, etc.). I use this to layout a grids of properly sized subviews of the main print view, where each subview is one label. Each subview is responsible for modifying the layout of its information to fit its size. Conceivably this could be done using Cocoa Autolayout now (my code predates Autolayout).
You can download a CSV version of my label dimensions file here.
EDIT: I might also mention that you'll need to do some significant extra work for a solution that works on both Mac and iOS. The printing APIs are quite different between the two platforms. Even if you use a custom view to do printing on iOS, UIView and NSView aren't exactly the same. Still, with some careful planning, you should be able to come up with something that at least shares common core code on both platforms.
actually i'm thinking of creating a cool interface for my programming assignment , so i go around searching on how to do it so that such an effect can be create , below is the image .
The Question
1.)What is needed in order to create a program that run pseudographic(semigraphic or whatever they called it) that has menu like BIOS wizard did??I always see some program run in console but it could have graphical-like looking , for example blue environment , and user can use keyboard to choose a list of setting in a menu.
Thanks for spending time reading my question.
It's called Text-based user interface. There're several libraries for doing this. I think this is what you're looking for. :)
Cross platform, Interactive text-based interface with command completion
http://www.gnu.org/s/ncurses/
Ncurses(or maybe pdcurses) is probably what you need.
In the days of 16-bit Windows console windows used to support ANSI escape sequences (via the ansi.sys driver), but they no longer do.
For the apparent line graphics you need to use a platform specific solution anyway, so I recommend just writing an abstraction (functions, class) over the Windows APIs console functions.
The line graphics is done by using characters from the original IBM PC character set, codepage 437. At first you can just hardcode the various patterns. In order to make it seem more like line drawing to the code, or from the code's perspective, so to speak, you'll have to abstract things again. As I remember there is some partial but not complete system in the original codepage 437 character codes. But for Windows console you will need to use the Unicode character codes, which probably do not preserve the original system, so perhaps just define a map where these graphics characters are placed more systematically.
Possibly that already exists.
If you don't care about portability, the Windows API for this can be found here. It will supply all the functions you need, without the need to pack additional libraries with your application.
You can also look in to graphics.h, a non-standard Borland extension that a lot of older graphical games used to use. It goes far beyond the normal limits of the console, but is only supported by 16 bit systems, of which Microsoft has all but removed support for from Windows. You'd also need an ancient Borland compiler, or an emulation, though you probably want the original look and feel.