In the pluma editor (on Linux) and sometimes in a web browser, for unrecognized characters, I see a little box with the Unicode value inside. I have seen 4 digit and 6 digit code boxes.
I WANT these little boxes with numbers (as appropriate.) How do I get these to display in a C++ Qt program?
Can you get a two digit box?
Here is an example, as shown in the pluma editor:
These glyphs are produced by the default fallback font for the system/platform – they are not inherently a feature of Qt.
If you want all characters to be rendered this way, you can use the Unicode BMP Fallback font, which has glyphs for all code points in the basic multilingual plane as hex digits in a box.
Related
Currently coding in C++ in WSL2 with ncursesw.
For the box() or border() functions/macros in ncurses, is it possible to use unicode characters with them, or do they not fit in the category of chtype?
I'm trying to create a border using the double line box drawing characters.
If not, do I have to create a border manually with other functions such as addstr() in for loops?
box and border use chtype's which provide only A_CHARTEXT bits for characters (8 bits in ncurses). To go beyond that, use box_set and border_set. Those use cchar_t structures, which you would initialize with setcchar.
I am attempting to put the final touches on a maze program I have been writing. I have used Unicode to delimit the walls and paths however because of the (horizontal) line spacing I can't quite get it to look compact enough. I have attached two screenshots. I'm just escaping the newline "\n" in order to print each row. Can the distance between lines be adjusted or am I stuck with this "gappy" representation?
My output:
What I am trying to closely represent:
Assuming you aren't printing double newlines, this is outside the scope of standard C++, it does not provide facilities for controlling terminal in a standard way.
Solutions:
You could provide a launcher script, which opens a new terminal window with specific font and runs your app in it.
You could use some platform specific method to change background color (ANSI codes work in unixy terminals, or use Win32 API for Windows terminal, ncurses library on Unix-like environments) and print just spaces in different colors.
Use a GUI library/framework to get complete control on what is drawn (I'd use Qt for C++ GUI app).
TBH if you want pixel-accurate rendering use a proper rendering API, such as OpenGL.
From a text rendering point of view you don't say what you are rendering to. Assuming something like a terminal console or shell window then the layout beyond characters and newlines is nothing to do with your program; the visual representation is entirely determined by the shell you are rendering to.
Firstly, check that you are genuinely printing a line per maze scan line, and not interleaving with spurious newlines. Assuming that is ruled out, the problem is that the unicode glyph is not a full block. So you must somehow set the font or choose another glyph which is a full block.
Usually console windows are 80 characters wide by 22 or 24 characters high, and characters are 8 pixels wide by 19 pixels high. So it's very far from a square grid, and you might want to bias the maze to reflect that and provide a better visual appearance (eg make 2 pixel-wide vertical corridors much more common than 2-pievel wide horizontal corridors).
Do check the binary image library fonts, you might find them useful.
https://github.com/MalcolmMcLean/binaryimagelibrary
I'd like to ask what's the simplest way of writing the chess unicode characters in a console window in C++? (♙♘♗♖♕♔♟♞♝♜♛♚) They are part of the "Miscellaneous Symbols" block in unicode. https://en.wikipedia.org/wiki/Chess_symbols_in_Unicode
I also want to print characters with square size, right now my chess board is not square, because each character is a rectangle and not a square.
It'd also be good to be able to write with ordinary non-square characters below the chess board, but that might be impossible? To mix different fonts/formattings in the same console window?
Ok, thanks in advance! :)
The first part of your question, outputting those characters, is platform-dependent. Linux consoles often use UTF-8, if that is the case you can encode the characters as UTF-8 and write them to standard output. On Windows you can use the Console API (the WriteConsole function):
HANDLE handle = GetStandardHandle(STD_OUTPUT_HANDLE);
DWORD written = 0;
// explicitly call the wide version (which always accepts UTF-16)
WriteConsoleW(handle, L"\u2658", 1, &written, NULL);
One caveat which is hard to work around is that you need a console font containing those characters.
For getting square cells, this is dependent on a lot of specifics about the way the console renders text. If it uses font substitution, then there is a chance the text will not actually be monospaced.
Now, if you have a console font with these characters, and if that font is monospaced, then you may be able to draw a square board by adding some spacing between the characters. You can use block elements like ▌ U+258C — LEFT HALF BLOCK to draw the chequerboard: ▌♘▐█▌ ▐.
I want to add an Unicode Symbol as a list widget item in QT. (Particularly this item : http://www.fileformat.info/info/unicode/char/25B6/index.htm )
I'm using following method for this :
this->addItem( new QListWidgetItem( QString::fromUtf8("\u25B6")) ) ;
But when I open the widget I see only a blank rectangle in place of this unicode symbol. I even tried other unicode symbols too, but they too are showing only blank rectangle.
What's wrong in this method?
EDIT: After following the answer, I changed the font of QListWidgetItem to Serif and it worked.
There are several possibilities, but the most probable one is that the font you are using doesn't implement the glyph at all. Can you see the triangle in eg. an editor when using the same font as your Qt font?
Edit: The fonts (which are stored in the system's font files) are the commands to draw the images of each character on the screen). Many (if not all) fonts are incomplete, which means they are not able to represent all 2,000,000,000+ codes which are possible in the unicode (the numbers which represent the characters). The files would just be to large to be practical.
The triangles you want printed are fairly basic, and should be available in many font sets. Liberation Sans and Liberation Serif are two I just checked.
I suspect Qt uses the font set of the system, which can probably be changed in the System Settings somewhere. If you tell us which distribution you are using (i.e. Ubuntu, Debian, ...), maybe we can help.
I use CreateTextLayout and CreateTextFormat to draw text with DirectWrite (C++), the text is mixed Hebrew/English, is there a way to use a different font/font size for the Latin and Hebrew characters?
Thanks.
When you create the IDWriteTextFormat using CreateTextFormat, you can pass the name of the font family in the first parameter and you can change the size of the font in sixth parameter.
You can get the CreateTextFormat parameters from MSDN.
Here is a list of Microsoft Windows font families, you will find ones in Latin and Hebrew there.
I ended up calling IDWriteFont::HasCharacter to build text ranges inside which all characters use the same font (the hebrew or the latin one).
If that text range uses the hebrew font, I call textLayout->SetFontSize on it to increase the font size.
This amounts to anticipating the font callback DirectWrite will do, which is automatic and cannot be customized.