Results 1 to 5 of 5

Thread: Surprising behaviour when rendering monospace text in Qt 4.8 (vs. 4.7 and older)

  1. #1
    Join Date
    Sep 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Surprising behaviour when rendering monospace text in Qt 4.8 (vs. 4.7 and older)

    Hi all,

    I have noticed a peculiar behaviour in Qt 4.8 when rendering text and would like you guys to help me understand it:
    When using a monospace font whose each character has a 'width' of say 10 pixels, I would expect a QString of n character to have a 'width' of n*10 pixels. This was the way it worked pre-4.8.

    Now in 4.8, despite every character having a width of 10 (this is a monospace font after all), each additional character added to a string makes it shorter (a 2 char string has a 19 pixel width, a 3 char string is 29, a 4 char string is 39, a 5 char string is 48....).

    No it is not 'kerning', as it doesn't depend on WHICH character I put on the string, and I've turned kerning off anyway.

    Here is a little experiment:
    Qt Code:
    1. QApplication app(argc, argv);
    2.  
    3. QFont font = QFont("DejaVu Sans Mono", 12);
    4. font.setKerning( false );
    5. font.setFixedPitch( true );
    6. QFontMetrics metrics = QFontMetrics( font );
    7.  
    8. for ( char i=32; i <= 'z'; i++ ) {
    9. printf("%c width: %d\n", i, metrics.width(QChar(i)));
    10. }
    11.  
    12. static const int STRINGLENGTH = 25;
    13. for ( int i=1; i<STRINGLENGTH; i++ ) {
    14. char string[STRINGLENGTH];
    15. for ( int j=0; j<i; j++ ) {
    16. string[j] = ( random() % 96 ) + 31;
    17. }
    18. string[i] = '\0';
    19. int width = metrics.width(QString(string));
    20. printf("%s: %d %d (%d)\n", string, width, width/i, width % i);
    21. }
    To copy to clipboard, switch view to plain text mode 

    On Qt 4.7, all characters are 10 pixel wide (first loop), and the strings are the expected width: 10, 20, 30, 40, 50.....
    On Qt 4.8, all characters are also 10 pixel wide, but the strings are of odd width: 10, 19, 29, 39, 48, 58, 67, 77, 87, 96....

    Maybe the width of a character is not an integer number of pixels? How has it changed from 4.7 to 4.8?

    (I tested that on Ubuntu 12.04, with qt 4.7.4 and 4.8.2 I have built myself, other mono fonts give similar results)

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

    Default Re: Surprising behaviour when rendering monospace text in Qt 4.8 (vs. 4.7 and older)

    Your code can produce strings containing character 31 (when random() == 0), a non-printable, which is probably at least part of the problem. It's possible non-printable chars are handled differently.

    The size specified to QFont is a point size and does not directly translate to a width. The width it does give may be fractional pixels which are rounded to 10 in QFontMetrics::width(). There could be a difference over multiple characters caused summing then rounding: Summing pixel widths of 9.63 will give you figures like the ones you posted.

  3. #3
    Join Date
    Sep 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Surprising behaviour when rendering monospace text in Qt 4.8 (vs. 4.7 and older)

    Quote Originally Posted by ChrisW67 View Post
    The size specified to QFont is a point size and does not directly translate to a width. The width it does give may be fractional pixels which are rounded to 10 in QFontMetrics::width(). There could be a difference over multiple characters caused summing then rounding: Summing pixel widths of 9.63 will give you figures like the ones you posted.
    Yes you're right, but it is surprising the behabiour has changed since 4.7, so maybe Qt was doing the rounding before adding before?

    Is there any posibility of setting the width to an integer number of pixels? (I know we don't set the width directly, but maybe there is a trick, as I think you can set the size in pixels rather than points).

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

    Default Re: Surprising behaviour when rendering monospace text in Qt 4.8 (vs. 4.7 and older)

    On my Linux machine using Qt 4.8.2, Freetype 2.4.9, and fontconfig (2.8.0) library I get even multiples of 10 pixels (when I eliminate the non-printables) with the font you used. I checked using QFontInfo that the font family and size are matched.

  5. #5
    Join Date
    Sep 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Surprising behaviour when rendering monospace text in Qt 4.8 (vs. 4.7 and older)

    I think I start to understand. It seems sub-pixel positionning was implemented in Qt 4.8 and is active when hinting is turned off (or 'slight'). On my Ubuntu box, this is probably the case and I can see the problem. On my Debian sid box, I have even multiple of 10 as well, so I suppose it is due to a different setting of hinting.

    I am still unsure about the relationship between "hinting" and "subpixel positionning" though.

    This blog post talks about that but I'm no typography expert: http://labs.qt.nokia.com/2011/03/14/...e-say-no-more/

    In any case, I found QFont::setStyleStrategy( QFont::ForceIntegerMetrics ) that seems to do exactly what I want: turn off sub-pixel positionning regardless of hinting. Works perfectly and I am back with integer widths.

Similar Threads

  1. Arabic text rendering; issues with parentheses
    By TropicalPenguin in forum Qt Programming
    Replies: 4
    Last Post: 10th September 2013, 09:53
  2. Text Edit rendering
    By VirtualRealty in forum Qt Programming
    Replies: 2
    Last Post: 1st October 2010, 17:16
  3. Text clipped when rendering svg.
    By ocratato in forum Newbie
    Replies: 1
    Last Post: 20th June 2010, 10:03
  4. 2D OpenGL text rendering with existing app
    By bytebucket in forum Newbie
    Replies: 7
    Last Post: 11th March 2010, 20:28
  5. text rendering problem
    By vno4697 in forum Qt Programming
    Replies: 0
    Last Post: 12th August 2008, 14:44

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.