Qt draw outline for multiline text - c++

QPainterPath can paint outline for the text
QPainter can draw multiline text
Is there any solution to make use of both of those features?
I also looked up for QLabel styles, and still didn't find anything outline-related.

Unfortunately, no such function exists in (at least, in Qt 5.15). What I did was wrote a function that takes a string and breaks it up into multiple lines, breaking on spaces or \r or \n. Loop over the string one word at a time, measuring it 'till it gets bigger than desired width (or you hit a line break), then back up one word, trunc any space, and call that one line. stick that into your vector of lines. repeat for each line. then in a separate function, you can draw each line one at a time, using stroke/fill from QPainterPath technique, advancing vertically by metrics.height() each line

Related

how to create an outline of a text with fonts with separate polygons for each letter?

I want the outline of text with the standard Windows font "Bahnschrift". I tried to convert it with "Path->Object to Path" and then setting fill to X and stroke to black, but looks like the font is constructed with separate polygons for each letter. See this example:
Top is Bahnschrift, and bottom is Arial which works fine.
Is it possible to calculate somehow automatically the outline of the top text, without intersections, and without doing it all manually for all the letters? And including the right outline for letters with holes, like the "e"?
Some letters are even kind of twisted internally, looks pretty bad:
So I guess an algorithm to detect if it is a real hole, or if it is a hole from such twists and to ignore it, could be difficult.
Select the complete character in question. Then select from the drop down menu:
Path intersection followed by path union. This fixes the character font outline problem where the outline crosses into the body of the character.

How to get correct position in the std::string?

I am creating a custom single line edit control, with a custom font in win32 api on windows 7, the font is not a fixed width font, and I need to move caret according to the mouse click, The edit control is not empty and if I know the horizontal position of the mouse click within the window, how do I calculate the number of characters after which I need to move caret to ?
I really am out of ideas, if it was a fixed width font, I would have divided the horizontal mouse click position with average character width, that would have been simpler, doing the same with not a fixed width font, is prone to errors.
Given that it's a single-line control, you probably don't plan on working with immensely long input (at least normally). That being the case, one possibility would be to just store the character positions in an array (or vector, etc.) Then you can use (for example) a binary search in that array to find character positions. Of course, you can do the same even for longer strings--though it can increase storage requirements quite a bit.
This is a familiar problem. You are in essence trying to do hit testing on text and for that you need the location on the screen of each character of the text.
My preferred strategy is to calculate an array of RECT, one for each character of displayed text. The array needs to be updated when text is added or deleted, but it easily handles single or multiple lines. The function GetCharWidth32 retrieves all the widths for a string of text in a particular font selected into a DC. For single line one call is enough, and calculating the array of RECTs is simple. It's not much harder to do multiline.
Handle the mouse down message, loop through the array and find the right character. A brute force search is plenty fast enough.
This method is simple and easily generalises to a range of similar problems.

Correct way to render cursor in custom text area?

I am using SDL and libfreetype and constructing a very basic GUI, I'm implementing a textBox, but I haven't been able to figure out how I'm supposed to generate the standard blinking cursor part. It doesn't seem to be exactly the same as a | character. And moreover if I draw it as a | character that changes the text width.
What's the canonically correct way to render text in a textbox with a cursor?
The easiest way is to just draw a line primitive, this gives you a lot more control over the spacing, length and width of the caret.
And if you want to keep it as a text character in your font system, you can do a render-to-texture and copy it out, or do a simple memory blit onto your font atlas (so you can can keep pipe character separate, an use a control char like 0x01 for the caret).

Qt, QPlainTextEdit and non-printable characters

I wonder if there is any possibility to render special characters (0-31 ASCII, for example) my own way in Qt/QPlainTextEdit? I want to render them as small rectangles as seen in this screenshot: ...or as Notepad++ is doing it. My goal is to be able see all characters. So if a character fails to render with current font (there is no such char for example), a small square should be rendered instead.
Qt does have ways to represent non-characters in QTextDocument which is used in QTextEdit and QPlainTextEditor. There's a sample on inserting an SVG object into a text edit:
http://doc.qt.io/archives/qt-4.7/richtext-textobject.html
Or you can use your own QAbstractTextDocumentLayout to handle the drawing of various text objects in the QTextDocument.

How can I change font leading (linespacing) in wxWidgets?

Then I use wxDC::DrawLabel for drawing multiline text I want to control font leading (linespacing). Is it possible?
There is no existing function to do that.
You can create your own. Use DrawText() to draw the individual lines with the spacing you want, but you draw each line individually. Use GetTextExtent() to find the height of the lines, and hence how much linespacing you'll need.