Results 1 to 6 of 6

Thread: QPainter::drawText and floating point precision

  1. #1
    Join Date
    Dec 2013
    Posts
    4

    Default Re: QPainter::drawText and floating point precision

    Hi!

    I want to draw text with floating point precision,
    but so far I have not succeeded.

    QPainter::drawText takes a QPointF as first
    argument, but it seems like it could
    just as well just have taken a QPoint as argument instead.

    I've tried to set the SmoothPixmapTransform, Antialiasing,
    and HighQualityAntialiasing rendering hints, but none of them
    seems to make a difference.

    I'm using Qt 4.8.

    Is floating point precision supported in Qt 4.8? (Am I
    just doing something wrong?)

    And in case not, will it help to use Qt 5 instead?

    Thanks for any help and clarification about what Qt does.
    If Qt doesn't support floating point precision when drawing
    text, I'll just have to do it manually instead, but I rather not.

    -K


    Added after 4 minutes:


    I should also add that I'm painting to a QImage.

    I've also tried to explicitly tell Qt to use the
    raster engine, but it makes no difference.

    I'm developing on Linux, but the program
    runs on OSX and Windows as well.
    Last edited by kssm; 15th December 2013 at 21:24.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QPainter::drawText and floating point precision

    Have you tried to use QPainter::TextAntialiasing?

  3. #3
    Join Date
    Dec 2013
    Posts
    4

    Default Re: QPainter::drawText and floating point precision

    Quote Originally Posted by Lykurg View Post
    Have you tried to use QPainter::TextAntialiasing?
    Yes, but since TextAntialiasing is ON by default, I didn't wrote that I had tried that, because then someone
    would comment that TextAntialising is ON by default.


    Added after 1 6 minutes:


    I've dived into the Qt source, and as far as I see, QPainter::drawPath is the
    drawing primitive used to draw text on QImages on Raster, and that method should
    provide floating point precision.

    Unless anyone comes up with a bright idea, I'll just compile Qt myself, and insert
    some debug printings here and there to see what's happening.
    Last edited by kssm; 16th December 2013 at 09:23.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QPainter::drawText and floating point precision

    Quote Originally Posted by kssm View Post
    Yes, but since TextAntialiasing is ON by default, I didn't wrote that I had tried that, because then someone
    would comment that TextAntialising is ON by default.
    Ups, I was not aware that it is turned ON by default.

  5. #5
    Join Date
    Dec 2013
    Posts
    4

    Default Re: QPainter::drawText and floating point precision

    I found a solution. Glyphs painting are cached, but it seems like the keys used for caching
    are in integer format, (or that the cached glyphs are stored in integer format).

    If I comment out line 3195 (or thereabout) in qpaintengine_raster.cpp, which looks like this:
    "if (!drawCachedGlyphs(glyphs.size(), glyphs.constData(), positions.constData(), fontEngine))"

    drawText works properly.

    Is there some other way to turn off caching, besides commenting out that line?

    Or perhaps this should be considered a bug? I guess some programs would run slower
    if the glyph caching used floats for key formats, although float keys would be correct.

    At least I haven't seen this limitation documented anywhere, and that should be considered
    a bug.

  6. #6
    Join Date
    Dec 2013
    Posts
    4

    Default Re: QPainter::drawText and floating point precision

    Using painter.translate() doesn't help either.

    I guess we are supposed to draw text using drawGlyphRun in these situations?
    I hope that works, if not I might have to use a patched version of Qt for the program.

    Using painter.translate() doesn't help either.

    I guess we are supposed to draw text using drawGlyphRun in these situations?
    I hope that works, if not I might have to use a patched version of Qt for the program.

    Using painter.translate() doesn't help either.

    I guess we are supposed to draw text using drawGlyphRun in these situations?
    I hope that works, if not I might have to use a patched version of Qt for the program.

    Using painter.translate() doesn't help either.

    I guess we are supposed to draw text using drawGlyphRun in these situations?
    I hope that works, if not I might have to use a patched version of Qt for the program.


    Added after 19 minutes:


    Nope, drawGlyphRun doesn't work either:

    Qt Code:
    1. static void myDrawText(QPainter *painter, QPointF p, QString text){
    2. QTextLayout textLayout(text, painter->font(), painter->device());
    3.  
    4. //int leading = fontMetrics.leading();
    5. qreal height = 0;
    6. textLayout.beginLayout();
    7. while (1) {
    8. QTextLine line = textLayout.createLine();
    9. if (!line.isValid())
    10. break;
    11.  
    12. line.setLineWidth(50.0f);//lineWidth);
    13. height += 5.4; //leading;
    14. line.setPosition(QPointF(0, height));
    15. height += line.height();
    16. }
    17. textLayout.endLayout();
    18.  
    19.  
    20. //textLayout.draw(painter, p);
    21.  
    22. QList<QGlyphRun> glyphs = textLayout.glyphRuns();
    23.  
    24. QList<QGlyphRun>::iterator i;
    25. for (i = glyphs.begin(); i != glyphs.end(); ++i) {
    26. printf("Drawing something\n");
    27. QGlyphRun glyph = *i;
    28. //glyph.setRawFont(QRawFont::fromFont(painter->font()));
    29. painter->drawGlyphRun(p, (const QGlyphRun &)glyph);
    30. }
    31. }
    To copy to clipboard, switch view to plain text mode 

    Guess I'll just rip out some code from the Qt source and insert it into my program.
    (they're both GPL)


    Added after 43 minutes:


    Got it. This works:

    Qt Code:
    1. void myDrawText(QPainter *painter, QPointF p, QString text){
    2.  
    3. QRawFont rawFont = QRawFont::fromFont(painter->font());
    4. QVector<quint32> indexes = rawFont.glyphIndexesForString(text);
    5.  
    6. painter->save();
    7. printf("num indexes: %d\n",indexes.count());
    8. for(unsigned int i=0; i<indexes.count(); i++){
    9. QPainterPath path = rawFont.pathForGlyph(indexes[i]);
    10. painter->translate(QPointF(p.x(), p.y() + i*0));
    11. painter->fillPath(path,painter->pen().brush());
    12. }
    13. painter->restore();
    14. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by kssm; 16th December 2013 at 16:33.

Similar Threads

  1. Print floating and double precision number
    By marc2050 in forum Newbie
    Replies: 2
    Last Post: 17th May 2011, 08:15
  2. Floating Point Constants
    By johnmauer in forum Qt Programming
    Replies: 2
    Last Post: 9th February 2010, 22:46
  3. Floating point exception.
    By dkt8154 in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 25th November 2008, 08:26
  4. QPainter clipping with precision behind the decimal point
    By Pieter from Belgium in forum Qt Programming
    Replies: 0
    Last Post: 14th March 2007, 13:30
  5. Floating point imprecision
    By hardgeus in forum Qt Programming
    Replies: 5
    Last Post: 3rd August 2006, 15:29

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.