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:
font.setKerning( false );
font.setFixedPitch( true );
for ( char i=32; i <= 'z'; i++ ) {
printf("%c width: %d\n", i, metrics.
width(QChar(i
)));
}
static const int STRINGLENGTH = 25;
for ( int i=1; i<STRINGLENGTH; i++ ) {
char string[STRINGLENGTH];
for ( int j=0; j<i; j++ ) {
string[j] = ( random() % 96 ) + 31;
}
string[i] = '\0';
int width
= metrics.
width(QString(string
));
printf("%s: %d %d (%d)\n", string, width, width/i, width % i);
}
QApplication app(argc, argv);
QFont font = QFont("DejaVu Sans Mono", 12);
font.setKerning( false );
font.setFixedPitch( true );
QFontMetrics metrics = QFontMetrics( font );
for ( char i=32; i <= 'z'; i++ ) {
printf("%c width: %d\n", i, metrics.width(QChar(i)));
}
static const int STRINGLENGTH = 25;
for ( int i=1; i<STRINGLENGTH; i++ ) {
char string[STRINGLENGTH];
for ( int j=0; j<i; j++ ) {
string[j] = ( random() % 96 ) + 31;
}
string[i] = '\0';
int width = metrics.width(QString(string));
printf("%s: %d %d (%d)\n", string, width, width/i, width % i);
}
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)
Bookmarks