View Full Version : Font Metrics
ChasW
22nd January 2007, 21:19
Would there happen to be a font metrics related function that would return an array of values corresponding to the cumulative string widths for each character position in a string? i.e. similar to Win32's GetTextExtentExPoint
Thanks in advance,
Charles
Chicken Blood Machine
23rd January 2007, 05:40
No. You will have to use QFontMetrics::width() in a loop to generate the cumulative values, e.g:
std::vector<int> charExtents;
QFontMetrics fm(font());
for (int n = 0; n < text.length(); ++n)
charExtents.push_back(fm.width(text, n));
ChasW
24th January 2007, 19:16
No. You will have to use QFontMetrics::width() in a loop to generate the cumulative values, e.g:
std::vector<int> charExtents;
QFontMetrics fm(font());
for (int n = 0; n < text.length(); ++n)
charExtents.push_back(fm.width(text, n));
Just for clarification, given the code above, doesn't the first character in the string get skipped? In other words, for length "0" which is what n would be for the first iteration, on my system, width() is returning 0. As well the final value is not being stored. Is that what should be happening?
example: using the above code on my system, given that text is a string "abc" for a fixed font whose width is 7 pixels, my vector is getting these values, 0, 7, 14.
In order to get the 0th element of the charExtents vector to be the pixel width for that character, I am using fm.width(text, n+1). In other words, to get 7, 14, 21.
Is this correct, or could I be doing something wrong?
Thank you in advance for any clarification.
wysota
24th January 2007, 20:06
Use
charExtents.push_back(fm.width(text[n])); instead.
ChasW
24th January 2007, 20:26
Use
charExtents.push_back(fm.width(text[n])); instead.
Given the above string, this populates the vector with 7,7,7
I was just looking for a clarification as to how to correctly get 7, 14, 21.
I think my observation above was a correct one.
My specific question is this: given the docs here http://doc.trolltech.com/4.2/qfontmetrics.html#width
We know what happens when the int len is -1, but what about the case of int len is 0. Can we assume that when we pass 0 for len that we will always get 0 in return?
wysota
24th January 2007, 21:50
Yes, I think it is safe to assume that :)
EDIT:
And if you don't want to "assume", but "be sure" instead, just look into the sources:
int QFontMetrics::width(const QString &str, int len) const
{
if (len < 0)
len = str.length();
if (len == 0)
return 0;
//...
Chicken Blood Machine
24th January 2007, 22:10
Just for clarification, given the code above, doesn't the first character in the string get skipped? In other words, for length "0" which is what n would be for the first iteration, on my system, width() is returning 0. As well the final value is not being stored. Is that what should be happening?
example: using the above code on my system, given that text is a string "abc" for a fixed font whose width is 7 pixels, my vector is getting these values, 0, 7, 14.
In order to get the 0th element of the charExtents vector to be the pixel width for that character, I am using fm.width(text, n+1). In other words, to get 7, 14, 21.
Is this correct, or could I be doing something wrong?
Thank you in advance for any clarification.
Yes, you're right. It should be:
std::vector<int> charExtents;
QFontMetrics fm(font());
for (int n = 0; n < text.length(); ++n)
charExtents.push_back(fm.width(text, n + 1));
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.