Results 1 to 10 of 10

Thread: Trying to render two text strings on alternating lines and determining glyph height.

  1. #1
    Join Date
    Apr 2014
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Trying to render two text strings on alternating lines and determining glyph height.

    What I want to do is to make a simple word processor that will handle Tibetan Unicode and allow the user to specify some simple layout and be able to print everything out to paper. Code: https://gist.github.com/ironhouzi/10564915. Output: qtextlayout_test1.jpg

    One of the problems I have to solve is that Tibetan being both horizontally and vertically oriented, can in rare cases have stacks that extend past the bottom line of the font (font descent). This is demonstrated in the beginning of the first two lines in the attached image. I have had to manually shorten and move the English line below to give room for the tall Tibetan glyph stack. I need to find a way to programatically detect when such a glyph stack is present so I can make the necessary line adjustments.

    I've come across this example: http://stackoverflow.com/questions/1...-a-qt-document

    So I need to use QTextDocument, but how do I add my two QTextLayouts to the QTextDocument? I've tried to use QTextDocument.setDocumentLayout(), but this takes a QAbstractTextDocument as argument.

    I could really need some guidance here, since there's not that much information on the lower level text rendering parts of Qt.

    Am I going down a dead end path?

    If anybody knows any books that explains the finer points of text rendering in Qt in great detail, I would love to know about it.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Trying to render two text strings on alternating lines and determining glyph heig

    Once you have determined the number of characters that will be drawn on one line you could possibly use QPainter::boundingRect() or QTextLayout::boundingRect() with the relevant part of the text to determine how high the line will render. If this gives you a sane height for the Tibetan stacks then you can ensure the following English line is rendered below that and not have to try to avoid the descending stacks.

  3. #3
    Join Date
    Apr 2014
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Trying to render two text strings on alternating lines and determining glyph heig

    Thank you for your reply.
    Sadly, boundingRect() does not follow the actual printed glyphs, rather it seems as if the base of the rectangle returned from boundingRect() is equal to the value returned from QFontMetrics().descent(). The rendered glyph stack extends far below the rectangle of boundingRect().

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Trying to render two text strings on alternating lines and determining glyph heig

    The problem is that the red box should enclose the text in this image:
    test.png
    from the test code:
    Qt Code:
    1. #include <QApplication>
    2. #include <QPixmap>
    3. #include <QPainter>
    4. #include <QFont>
    5. #include <QDebug>
    6.  
    7. int main(int argc, char **argv)
    8. {
    9. QApplication app(argc, argv);
    10.  
    11. const QString text = QString::fromUtf8(
    12. // Your text
    13. "\xE0\xBD\x96\xE0\xBD\xA6\xE0\xBE\x90\xE0\xBE\xB1\xE0\xBD\xBC\xE0\xBD\x84\xE0\xBD\xA6"
    14. // Your complex text
    15. "\xE0\xBD\xA7\xE0\xBE\xB9\xE0\xBE\xA8\xE0\xBE\xB3\xE0\xBE\xBA\xE0\xBE\xBC"
    16. "\xE0\xBE\xBB\xE0\xBE\x83\xE0\xBC\x8B\xE0\xBD\x8A\xE0\xBE\x9B\xE0\xBE\x9C"
    17. "\xE0\xBE\x9D\xE0\xBE\x9E\xE0\xBD\xB1\xE0\xBC\x8B"
    18. );
    19.  
    20. QFont font("Microsoft Himalaya", 30);
    21.  
    22. QPixmap pixmap(200, 100);
    23. pixmap.fill(Qt::white);
    24. QPainter p(&pixmap);
    25. p.setFont(font);
    26. QRect br;
    27. p.drawText(pixmap.rect(), Qt::AlignLeft | Qt::AlignTop, text, &br);
    28. p.setPen(Qt::red);
    29. p.drawRect(br);
    30. p.end();
    31. qDebug() << br;
    32. pixmap.save("test.png");
    33.  
    34. return 0;
    35. }
    To copy to clipboard, switch view to plain text mode 
    and it does not (Linux Qt 4.8.5 and 5.2.0). I would call this a bug but perhaps someone with more non-Latin text rendering experience has an insight.

    I used to think boustrophedon was exotic.

  5. #5
    Join Date
    Apr 2014
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Trying to render two text strings on alternating lines and determining glyph heig

    While I'm certainly a beginner with regards to the esoteric arts of font rendering on computers, I might think that this problem stems from the font designer breaking the rule for the descent value http://qt-project.org/doc/qt-4.8/qfo...f.html#descent, but it's hard to know for sure when I don't really know much about this subject matter.

    I do suspect that the font designer broke the rule for the right reasons though. These tall glyph stacks are clearly corner cases and the rectangle should enclose most Tibetan words. MS Himalaya is the only font I know that will handle these tall glyph stacks correctly and I suspect it is due to breaking this rule of allowing glyphs to be rendered beyond the descent boundary. But these are mere speculations unfortunately.
    Last edited by houzi; 14th April 2014 at 05:18.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Trying to render two text strings on alternating lines and determining glyph heig

    I assume the exceptional glyph stacks are transliterations of things like foreign names.

    I have had a bit more of a tinker and all the ways I can see of getting the rendered text size give the same result.

  7. #7
    Join Date
    Apr 2014
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Trying to render two text strings on alternating lines and determining glyph heig

    Your assumptions are correct. The tall glyph stacks are transliterations of Sanskrit words.
    Anyways, thank you so much for your replies.
    Perhaps I need to utilize some external libraries that can query the font somehow.
    Last edited by houzi; 14th April 2014 at 13:41.

  8. #8
    Join Date
    Apr 2014
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Trying to render two text strings on alternating lines and determining glyph heig

    I found something that might work. QFontMatricsF.boundingRect(QFontMatricsF(QFont('hi malaya', 30)), "ཧྐྵྨླྺྼྻྃ ¼‹à½Šà¾›à¾œà¾à¾žà½±à¼‹ ") does in fact draw a rectangle with the correct width and height, but the y coordinate is off. This call produced QRectF(0, -22, 44, 66.656). If I draw a rectangle for the coordinates (0, 11, 44, 67), they match up perfectly to the rendered glyph stacks.

    Does anybody know why the y coordinate is completely off?

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Trying to render two text strings on alternating lines and determining glyph heig

    I had noticed something similar but could not exploit it to get the expected result.

    As I understand the Tibetan script the stacks are made of glyphs that can either be subscripted below existing ones, or superscripted above existing ones. Does your example have a superscripted glyph that might be the cause of the negative?

  10. The following user says thank you to ChrisW67 for this useful post:

    houzi (18th April 2014)

  11. #10
    Join Date
    Apr 2014
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Trying to render two text strings on alternating lines and determining glyph heig

    Tibetan glyph stacks can often have root letters with both superscribed and subscribed letters. Thanks for your input, I will try to remove the superscribed glyph and see how the rectangle changes.

Similar Threads

  1. Replies: 0
    Last Post: 2nd December 2013, 20:56
  2. Replies: 1
    Last Post: 29th April 2011, 13:26
  3. Determining the height of an item in a QListWidget
    By negritot in forum Qt Programming
    Replies: 5
    Last Post: 13th May 2009, 20:18
  4. QDomElement::text() returns corrupted strings
    By sladecek in forum Qt Programming
    Replies: 6
    Last Post: 7th March 2009, 15:07
  5. All font library has same glyph index for glyph ??
    By Gaurav K SIngh in forum Qt for Embedded and Mobile
    Replies: 16
    Last Post: 1st August 2007, 16:12

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.