Text label with irregular shape?
-
Is there a simple way to format a textual label to follow an irregular border?
Here is an example taken from the current UML standard (2.5.1) illustrating a comment note:
If I draw the text within the green rectangle in the following image, there is always the possibility that it will collide with the turned-down corner at the upper right:
However, is it possible to define a closed path as in the pink shape in the following image and have the text constrained to the enclosed area?
Of course, we have no way of knowing how the original image was created, but for such purposes there should be a way of automatically formatting any kind of text.If I constrain the text to the area of the rectangle whose width fits the distance between the left-hand border and the edge of the turned-down corner, it leaves too much space at the right of the rest of the text.
What is the best way of approaching this?
-
You can use QTextLayout. You define line boxes and it breaks the text apart to fit into these boxes. The docs have an example code how to use it.
For your particular example it's simple enough - just have the first line box a bit shorter and then all the other ones longer.
For a general case of any shape it's similar, but you first have to find the boxes that fit into the shape. There's nothing in Qt (that I know) to do it for you, but the algorithm is well documented, as it's used e.g. in the CSS spec for the shape-inside property. Basically you rasterize the shape into a grid and find the cell groups that don't intersect the shape, as described e.g. here (there's a nice visualization at the bottom).
-
Thanks very much @Chris-Kawa ... this is exactly what I was hoping for.
-