Results 1 to 2 of 2

Thread: QGraphicsTextItem and text on pixmap caching problem

  1. #1
    Join Date
    Aug 2012
    Qt products
    Qt4 Qt/Embedded
    MacOS X Unix/X11

    Default QGraphicsTextItem and text on pixmap caching problem


    I've inherited some code that uses a pixmap texture to cache glyphs in the ascii/latin1 set based on font and size of the text. Each face/size gets its own texture and the positioning metadata is stored in a glyph struct. When preparing to paint, a subclass of QGraphicsTextItem consults the glyph metadata about its position on the pixmap. If all the string characters in the text are in the pixmap, the texture positions are passed to a tree that gets rendered via OpenGL.

    When the glyph falls outside the ascii/latin1 range, the texture is not used. Instead, QGraphicsTextItem:aint() renders the text.

    The difficulty I'm having is that, depending on the glyph code point(s) that fall outside the range, I get varying amounts of offset from the baseline, from 5 to 10 pixels on a 32 pixel size font. When two QGraphicsTextItems are supposed to be on the same baseline, it looks bad when they aren't.

    The pixmap was created with QPainter::drawText(x, y + ascent, QString) for each glyph. The ascent value was obtained from QFontMetrics. The x and y values were obtained from QFontMetrics::boundingRect. I also had to offset the ascent, because the font doesn't always honor it. The offset is also stored in the metadata and used to calculate the y in the QGraphicsTextItem preparation for painting.

    I've double checked the document margin. I don't think that is the problem because different glyphs produce different baselines. QGraphicsTextItem seems to be positioning based on the tallest bounding rectangle enclosing the glyphs in the string. Can someone confirm this?

    The performance improvement is about 40% when the cache is used, so it would be great to keep it and resort to QGraphicsTextItem in the small number of cases where glyph code points fall outside the range.

    Could someone point me in the right direction for fixing this?

    Thanks in advance.
    Jeff Barnes

  2. #2
    Join Date
    Jan 2008
    Alameda, CA, USA
    Thanked 808 Times in 797 Posts
    Qt products
    Windows Android

    Default Re: QGraphicsTextItem and text on pixmap caching problem

    A QGraphicItem's internal position (origin) is always at (0,0) - the upper left corner of the bounding box, which is then transformed relative to its parent into a parent coordinate position. So setPos() is basically moving the upper left corner of the box within the parent's coordinate space.

    The QGraphicsItem::boundingRect() is the largest rect that encloses the graphics item and all of its children. In the case of text, it would probably be the largest box that encloses the particular string, and would vary depending on the length and height of the characters in the string, including both ascent and descent.

    My guess is that you will have to deconstruct each string and determine the maximum ascent and descent by examining each character, and use that to position the text item on the baseline. Seems a bit silly that this functionality isn't already present in QGraphicsTextItem, to solve exactly the problem you are facing.

    EDIT: Also see this earlier post.
    Last edited by d_stranz; 11th August 2012 at 17:04.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. QGraphicsTextItem Vertical text alignment
    By mbra in forum Qt Programming
    Replies: 2
    Last Post: 15th October 2011, 03:12
  2. Replies: 1
    Last Post: 19th April 2011, 11:17
  3. QGraphicsTextItem: How to show the text cursor?
    By breezeight in forum Qt Programming
    Replies: 1
    Last Post: 17th November 2009, 16:44
  4. Interactive text formatting in QGraphicsTextItem
    By olelukoie in forum Qt Programming
    Replies: 18
    Last Post: 29th October 2009, 09:49
  5. Rich text in QGraphicsTextItem
    By maverick_pol in forum Qt Programming
    Replies: 3
    Last Post: 29th September 2008, 20:08

Tags for this Thread


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.