Some of my colleagues on the graphics team have been writing graphics dojo demos with impressive speed and with a rainy weekend I thought I’d get into the action.
As I’m regarded as the text guy I thought it would be fitting to have a demo that uses text, so I made a text effect class that animates text fading in and fading out.
One thing that I see a lot of people do is that if they want to zoom into text, they change the fontsize. There is a fundamental problem with that approach as text doesn’t scale linearly (due to hinting). So in Qt4 we have a much simpler and more powerful way of scaling text that fits better in graphics concepts. You simply scale the QPainter during painting.
This means that if you call myPainter.scale(2,2); your 12pt font will suddenly appear as a nicely scaled 24pt font. Or, more accurately, it will show the original text at 200% zoom.
This is very simple and due to us reusing the same font settings we avoid recalculation and text layout. So it will actually be quite fast. So fast that you can animate it quite smoothly. As is shown in the demo.
There is not much more to say about it, so just grab the code and run it!
svn checkout svn://labs.trolltech.com/svn/graphics/dojo/zoomingtext
Possibly related posts:




8 comments
Unfortunately the letters hop left and right by one pixel. Is there a way to fix that? Otherwise it looks quite unprofessional to me…
It looks very wobly to me, and not smooth as advertised in the demo at all. I have to agree with the previous poster.
While the zooming itself is cool, applied this way, it does not look good.
This effect probably relies on anti aliasing the edges of the text. It looks pretty broken for me as well, for some reason QPainter refuses to antialias fonts on this Vista machine (setting the render hints explicitely didn’t help).
Interesting. I always used the font size to animate as well… This should make some of my code a lot simpler. Especially the “fit text to this box” problem should be much simpler to calculate.. Thanks!
By the way, I also see the funny text wiggling, but I think it’s mostly accentuated by the slow animation speeds you have set. It wouldn’t be a problem for my types of apps.
Thanks for the hints, I made the speed higher so any jittering would be gone. svn update to get this change.
I’m not sure what to answer to the first replies, I am not seeing the jittering in any noticable fashion. Total jittering can’t be avoided due to the fact that a monitor doesn’t have that many pixels and you see a move from one pixel to the next.
I’m assuming you have anti-aliased, naturally. As you can see from the screenshots on my Linux machine its fully anti-aliased.
Cheers!
Regaring jitterng: The problem here is that the font rendering systems we use, GDI and FreeType in these cases, don’t do sub-pixel positioning of the glyphs within the generated bit masks. Nor does Qt compensate for this lack of support, hence the glyphs will be snapped to the pixelgrid for each individual scale factor, which leads to jittering during animation. You can fix this by drawing the text outline using a QPainterPath, like we do in the QTDIR/demos/affine demo. This leads to perfectly transformed text, but is significantly slower than normal text drawing, which is why we don’t normally do the QPainterPath approach in the first place.
Regarding Antialising on Vista: Qt doesn’t look too closely at the TextAntialiasing render hint. We tend to follow the system defaults instead. If you specify ClearType antialiasing on windows, we give you that all the way, at least as far as it is possible, which should be always on normal widgets. If you choose Normal antialising in the windows control panel, windows decides that some font ranges, around 8 to 14 or so, look best when rendered aliased. We simply follow Windows on this. The only case the TextAntiasliasing hint is used on Windows is when drawing funky transformed text or really big glyphs (>64 pts or so), because in these cases, Qt does the rendering itself.
The one problem I’ve encountered using QPainter::scale() on text is the very hinting you mention. I’ve found it’s better to render my text at some overly-large font size (32pt+) and QPainter::scale() it down for the “unzoomed” view, because otherwise the curves aren’t smooth at all — they’re hinted the same as if you were trying to render them at the original size.
I would like to be able to disable the text hinting at all. On Windows, there is a really strong hinting enabled by default. Especially for small font sizes, this is not in every case a benefit.
Comments on this entry are closed.