Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Rotate Each Character in a Text (drawText)



  • Hello,

    I need to draw some text in almost a Japanese style vertical writing (column) but in the left-to-right direction like so:

    alt text

    Can someone please suggest a reasonable way to do this with drawText?

    Many many thank yous!


  • Lifetime Qt Champion

    Hi,

    From the top of my head, I would go with using QPainter::rotate to draw the text vertically in a QImage and the rotate that image.



  • I've actually done that before when this app was written in C# on Windows. I effectively measured the QRect of each letter/character, make a QImage for it, rotated it, and draw that image on to the main canvas. Apart from being quite cumbersome, the main problem is that the Image rasterises the text which makes it quite ugly in small font sizes.

    I was hoping for a more direct to main canvas approach.

    That example image I added was done in Photoshop with two clicks but they obviously have a crazy powerful font drawing engine in there.


  • Lifetime Qt Champion

    How are you going to use that text ? QWidget ? QGraphicsView ?



  • @SGaist QGraphicsView and eventually with QPrinter.


  • Lifetime Qt Champion

    In that case, you should be able to just rotate the item itself after having drawn the text vertically.

    Are you using a QGraphicsTextItem ?



  • @SGaist I'm not using a QGraphicsTextItem but I can. Would it make it easier?

    This would involve a hell of a lot of rotations and translations to position the text and maintain centre alignment 🤯


  • Lifetime Qt Champion

    Can you show an example of the result you are looking for ?



  • @SGaist It's exactly like how that image in the original post is.

    This is what I've done on Windows with C# and the equivalent of QImage:

    alt text

    The normal left-to-right text is drawn with the equivalent of drawText and it scales and renders perfectly regardless of scale level or device DPI; but because of the QImage, the rotated vertical writing text rasterises and gets pixilated if it's too small or when printing.

    In this code, every character is measured, drawn centre-aligned, rotated, copied to the main canvas, and so on. Doing it like this with a QImage like you suggested is actually simpler than worrying about the math of rotating and transferring the whole canvas to position each character.

    I was just hoping there was a smarter solution than this. Japanese is essentially written in vertical writing (literal translation of "Tategaki") so I thought there would be a common efficient solution to this problem.


  • Lifetime Qt Champion

    Just had an untested silly idea, why not have your vertical text be a string where each letter is follow by a line break ? Then you would only have to rotate the painter and not have to do more complicated stuff.



  • @SGaist

    alt text

    Why didn't I think of that?!


Log in to reply