Results 1 to 14 of 14

Thread: Drawing antialiased text on Windows very slow

  1. #1
    Join Date
    Jan 2007
    Posts
    9

    Default Drawing antialiased text on Windows very slow

    I'm using this code:
    Qt Code:
    1. foreach(QString line, lines)
    2. {
    3. x = xPosition(left, hAlign, width, fontMetrics.width(line));
    4. path.addText(x, y, stdFont, line);
    5. y += lineHeight;
    6. }
    7. QBrush brush(font.color());
    8. QPen pen(font.color(), font.borderF(), Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
    9. if (font.borderF() > 0)
    10. pen.setColor(font.borderColor());
    11. setRenderHint(QPainter::Antialiasing);
    12. setPen(pen);
    13. setBrush(brush);
    14. setFont(stdFont);
    15. drawPath(path);
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.

  3. #3
    Join Date
    Jan 2007
    Posts
    9

    Default Re: Drawing antialiased text on Windows very slow

    Quote Originally Posted by wysota View Post
    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.

  5. #5
    Join Date
    Jan 2007
    Posts
    9

    Default Re: Drawing antialiased text on Windows very slow

    Quote Originally Posted by wysota View Post
    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.

  7. #7
    Join Date
    Jan 2007
    Posts
    9

    Default 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:
    Qt Code:
    1. #include <QApplication>
    2. #include "Widget.h"
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication app(argc, argv);
    6. Widget widget;
    7. widget.show();
    8. return app.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 

    Widget.h:
    Qt Code:
    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include <QWidget>
    4. class Widget : public QWidget
    5. {
    6. protected:
    7. virtual void paintEvent(QPaintEvent* event);
    8. };
    9. #endif
    To copy to clipboard, switch view to plain text mode 

    Widget.cpp
    Qt Code:
    1. #include "Widget.h"
    2. #include <QtGui>
    3. void Widget::paintEvent(QPaintEvent* /* event */)
    4. {
    5. QStringList lines;
    6. lines << "safsdafsdaf asdflkj a sdfjöl akj " \
    7. << "asdflkj asdföjl asdf asdkj ösa" \
    8. << "asödlfjk lkajs dfa asdfjö asöld föl" \
    9. << "saödlfkj lkj asdf asdfkj klsjd f " \
    10. << "asdölfkj lökj asdfasdf sakdj" \
    11. << "asödlfkj ölkj asdf ajsklö fs" \
    12. << "asdöflkj lökj ölkasjdföalkj ölkj asdf";
    13. QFont font;
    14. font.setPointSize(30);
    15. QPainter p(this);
    16. QFontMetrics fontMetrics(font, p.device());
    17. int lineHeight = fontMetrics.height();
    18. int x = 10;
    19. int y = lineHeight;
    20. foreach(QString line, lines)
    21. {
    22. path.addText(x, y, font, line);
    23. y += lineHeight;
    24. }
    25. QBrush brush(QColor("#FFFFFF"));
    26. QPen pen(QColor("#000000"), 0.5, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
    27. p.setRenderHint(QPainter::Antialiasing);
    28. p.setPen(pen);
    29. p.setBrush(brush);
    30. p.setFont(font);
    31. QTime time;
    32. time.start();
    33. p.drawPath(path);
    34. qDebug() << "Time elapsed for drawing:" << time.elapsed() << "ms.";
    35. }
    To copy to clipboard, switch view to plain text mode 

    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?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.

  9. #9
    Join Date
    Jan 2007
    Posts
    9

    Default 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.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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...

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drawing antialiased text on Windows very slow

    Try this (You may have to adjust the font name). Is it very unacceptable?
    Attached Files Attached Files

  12. #12
    Join Date
    Jan 2007
    Posts
    9

    Default 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
    Qt Code:
    1. path.addText(0, 96, fnt, txt);
    To copy to clipboard, switch view to plain text mode 
    with
    Qt Code:
    1. QStringList lines = txt.split("\n");
    2. QFontMetrics fontMetrics(fnt);
    3. int lineHeight = fontMetrics.height();
    4. int y = 0;
    5. foreach(QString line, lines)
    6. {
    7. path.addText(0, y, fnt, line);
    8. y += lineHeight;
    9. }
    To copy to clipboard, switch view to plain text mode 
    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.

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.

  14. #14
    Join Date
    Jan 2007
    Posts
    9

    Default 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.

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Replies: 2
    Last Post: 23rd July 2012, 08:42
  3. Problem pasting text into a QTextEdit
    By Spockmeat in forum Qt Programming
    Replies: 8
    Last Post: 14th March 2009, 14:36
  4. QProcess extremely slow on Windows?
    By Pepe in forum Qt Programming
    Replies: 2
    Last Post: 26th March 2007, 00:25
  5. Editable text in QGraphicsView
    By wysota in forum Qt Programming
    Replies: 8
    Last Post: 24th February 2007, 15:30

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.