Drawing antialiased text on Windows very slow
I'm using this code:
Code:
{
x = xPosition(left, hAlign, width, fontMetrics.width(line));
path.addText(x, y, stdFont, line);
y += lineHeight;
}
QPen pen
(font.
color(), font.
borderF(), Qt
::SolidLine, Qt
::RoundCap, Qt
::RoundJoin);
if (font.borderF() > 0)
pen.setColor(font.borderColor());
setPen(pen);
setBrush(brush);
setFont(stdFont);
drawPath(path);
The code is used in a class which inherits from QPainter. On Linux it runs quite well, but on Windows it takes way too long. If antialiasing isn't enabled the speed is OK, but the text looks pretty bad. Do you have any idea?
Re: Drawing antialiased text on Windows very slow
Is there a reason why you use a painter path to draw the text? It should be way faster if you rendered the text directly.
Re: Drawing antialiased text on Windows very slow
Quote:
Originally Posted by
wysota
Is there a reason why you use a painter path to draw the text? It should be way faster if you rendered the text directly.
If I use drawText() from QPainter, there's no border/outline.
Re: Drawing antialiased text on Windows very slow
If you really need it, then I suggest you render the path once to a pixmap and then only render the pixmap to the widget.
Re: Drawing antialiased text on Windows very slow
Quote:
Originally Posted by
wysota
If you really need it, then I suggest you render the path once to a pixmap and then only render the pixmap to the widget.
I'm doing it this way, but in my opinion if 235 characters need nearly 10 seconds (on a Duron with 1,2 GHz) to draw, then it is far too much.
Re: Drawing antialiased text on Windows very slow
They are not characters anymore once you add them to a painter path - they are converted to curves and if the resulting curve is complex (may depend on the font itself) antialiasing it will take very long. On Linux you might be having a lower DPI font or something like that.
Re: Drawing antialiased text on Windows very slow
I can't believe that such a big difference in speed has to do with different fonts.
I've made a little program so you can try it yourself:
main.cpp:
Code:
#include <QApplication>
#include "Widget.h"
int main(int argc, char *argv[])
{
Widget widget;
widget.show();
return app.exec();
}
Widget.h:
Code:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
{
protected:
};
#endif
Widget.cpp
Code:
#include "Widget.h"
#include <QtGui>
{
lines << "safsdafsdaf asdflkj a sdfjöl akj " \
<< "asdflkj asdföjl asdf asdkj ösa" \
<< "asödlfjk lkajs dfa asdfjö asöld föl" \
<< "saödlfkj lkj asdf asdfkj klsjd f " \
<< "asdölfkj lökj asdfasdf sakdj" \
<< "asödlfkj ölkj asdf ajsklö fs" \
<< "asdöflkj lökj ölkasjdföalkj ölkj asdf";
font.setPointSize(30);
int lineHeight = fontMetrics.height();
int x = 10;
int y = lineHeight;
{
path.addText(x, y, font, line);
y += lineHeight;
}
QPen pen
(QColor("#000000"),
0.5, Qt
::SolidLine, Qt
::RoundCap, Qt
::RoundJoin);
p.
setRenderHint(QPainter::Antialiasing);
p.setPen(pen);
p.setBrush(brush);
p.setFont(font);
time.start();
p.drawPath(path);
qDebug() << "Time elapsed for drawing:" << time.elapsed() << "ms.";
}
Either there is another, faster way to draw antialiased, outlined text on Windows with Qt or it needs to be improved... If there isn't another way I would even try to use some text rendering outside of Qt. Is there some free (GPL compatible) library or so?
Re: Drawing antialiased text on Windows very slow
As I said, once you add text to a painter path, it's not text anymore. This is the problem, not drawing antialiased fonts.
Re: Drawing antialiased text on Windows very slow
OK, the problem is that drawing QPainterPath with antialiasing is slow on Windows... but this still doesn't solve my problem.
Re: Drawing antialiased text on Windows very slow
Avoid using painter paths. And if you don't want to, optimise your code. For instance construct the path once and not every time paintEvent is called. The speedup probably won't be very significant, but you have to start with something...
1 Attachment(s)
Re: Drawing antialiased text on Windows very slow
Try this (You may have to adjust the font name). Is it very unacceptable?
Re: Drawing antialiased text on Windows very slow
Your example works fine, but it draws only one word. Just change the source a little bit to draw a little bit more and you'll see the difference.
I've made the font a little bit smaller and replaced
Code:
path.addText(0, 96, fnt, txt);
with
Code:
int lineHeight = fontMetrics.height();
int y = 0;
{
path.addText(0, y, fnt, line);
y += lineHeight;
}
Use a static color for the brush (otherwise the difference is not so big...). Then let the program draw 6 or more lines of text on Linux and on Windows. On Windows it needs a few seconds. Because my application often has to draw outlined text, I need some faster solution although it is drawn once on a QPixmap. Thanks for trying to help even though I still have no solution.
Re: Drawing antialiased text on Windows very slow
I still think you shouldn't use a painter path at all. It's just unreliable for objects where you can't estimate their complexity upfront. And if you do insist on using it - do it once and try to keep the number of nodes in it as small as possible.
Re: Drawing antialiased text on Windows very slow
I don't care whether I use a painter path or not. I just need to draw outlined text in an acceptable time.