QPainter::rotate disables antialiasing of drawn text
Asked Answered
C

2

8

I use QPainter::setRenderHint(QPainter::Antialiasing, true) to tell Qt that I want it to antialias any drawing I do (in this case, text drawn with drawText()).

This works fine and the text looks good, until I want to rotate the pixmap I'm drawing to e.g.

Painter.translate(0, height());
Painter.rotate(-90);

(to rotate the QPainter 90 degrees counterclockwise and bring it back into view)

The call to rotate() seems to disable antialiasing for any text drawn - the text is drawn at the correct rotation but without antialiasing. Other things seem unaffected - e.g. drawLine() still draws a nicely antialiased line.

Any idea what I'm doing wrong?

EDIT: Unsurprisingly, adding the text to a path and then filling that path gives me antialiased, rotatated text. I'd rather avoid this route if possible though.

EDIT (again): I've tried using QFont::setStyleStrategy(QFont::PreferAntialias) on the font I'm using, with no effect. However, some more experimentation shows that a basic font like Arial will still produce antialiased text when rotated, whereas my custom font (Swiss721 BlkCn BT for anyone who's interested) will not. Moreover, while this problem exists on Windows 7, I don't have the same issue when running on Ubuntu. This FAQ article would seem to suggest that Qt looks to the host OS to handle font antialiasing, so what kind of issues might Windows have in handling the rendering of this particular font (which is a TrueType, just like Arial)?

EDIT (last time, I promise): Upping the font size to 16pt or above kills the problem. It would seem that the issue is with rendering my particular font below 16pt - perhaps something to do with what was mentioned in the above blog article?:

On Windows 2000 fonts are usually not antialiased within a certain range (say sizes 8-16) to make text more crisp and readable.

Cumulation answered 3/11, 2011 at 13:28 Comment(1)
I think I may have answered my own question here, but if anyone has any further or related information I'd still be very interested.Cumulation
A
4

I've actually had occasion to be in this part of the Qt code recently, and I think the behavior you are seeing is related to the following two bugs in Qt:

If I remember correctly (not 100%) sure, what you are actually seeing is the loss of the ClearType rendering on Windows. When there is a transformation applied, Qt gets the glyph pixels in a way that throws out the ClearType information, so things look more jagged.

If you wanted to look at the code yourself, the most likely place is /src/gui/text/qfontengine_win.cpp. You could also try turning off ClearType and see if they look similar.

Ault answered 3/11, 2011 at 19:14 Comment(1)
That makes a lot of sense Dave, thank you. Turning off cleartype does indeed result in characters being drawn with identical pixels when rotated. This forum thread (esp. the comments by Ingvarr) as well as the Wikipedia article explain how the underlying principle that makes ClearType work also limits it to only working with horizontal text.Cumulation
F
0

One "guess" concerns your RenderHint. You use QPainter::Antialiasing here. Docu: "Indicates that the engine should antialias edges of primitives if possible." Primitives like lines, rects, etc. Try QPainter::TextAntialiasing instead.

Note: The RenderHints are flags, so you can bit-OR them if need be (and sounds like it is).

Frierson answered 3/11, 2011 at 16:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.