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)