Results 1 to 7 of 7

Thread: Font Metrics

  1. #1
    Join Date
    Jan 2007
    Posts
    26
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Font Metrics

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Font Metrics

    No. You will have to use QFontMetrics::width() in a loop to generate the cumulative values, e.g:

    Qt Code:
    1. std::vector<int> charExtents;
    2. QFontMetrics fm(font());
    3. for (int n = 0; n < text.length(); ++n)
    4. charExtents.push_back(fm.width(text, n));
    To copy to clipboard, switch view to plain text mode 
    Save yourself some pain. Learn C++ before learning Qt.

  3. The following user says thank you to Chicken Blood Machine for this useful post:

    ChasW (23rd January 2007)

  4. #3
    Join Date
    Jan 2007
    Posts
    26
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Font Metrics

    Quote Originally Posted by Chicken Blood Machine View Post
    No. You will have to use QFontMetrics::width() in a loop to generate the cumulative values, e.g:

    Qt Code:
    1. std::vector<int> charExtents;
    2. QFontMetrics fm(font());
    3. for (int n = 0; n < text.length(); ++n)
    4. charExtents.push_back(fm.width(text, n));
    To copy to clipboard, switch view to plain text mode 
    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.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Font Metrics

    Use
    Qt Code:
    1. charExtents.push_back(fm.width(text[n]));
    To copy to clipboard, switch view to plain text mode 
    instead.

  6. #5
    Join Date
    Jan 2007
    Posts
    26
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Font Metrics

    Quote Originally Posted by wysota View Post
    Use
    Qt Code:
    1. charExtents.push_back(fm.width(text[n]));
    To copy to clipboard, switch view to plain text mode 
    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?

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Font Metrics

    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:
    Qt Code:
    1. int QFontMetrics::width(const QString &str, int len) const
    2. {
    3. if (len < 0)
    4. len = str.length();
    5. if (len == 0)
    6. return 0;
    7. //...
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to wysota for this useful post:

    ChasW (24th January 2007)

  9. #7
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Font Metrics

    Quote Originally Posted by ChasW View Post
    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:
    Qt Code:
    1. std::vector<int> charExtents;
    2. QFontMetrics fm(font());
    3.  
    4. for (int n = 0; n < text.length(); ++n)
    5. charExtents.push_back(fm.width(text, n + 1));
    To copy to clipboard, switch view to plain text mode 
    Save yourself some pain. Learn C++ before learning Qt.

Similar Threads

  1. Font not Antialiasing
    By ChasW in forum Qt Programming
    Replies: 6
    Last Post: 21st January 2007, 18:12
  2. Qt renders wrong font
    By durbrak in forum Qt Programming
    Replies: 8
    Last Post: 2nd November 2006, 14:36
  3. how to use user define font
    By numen in forum Qt Programming
    Replies: 1
    Last Post: 20th April 2006, 10:25
  4. Why QStyle dosen't changes the standard font?
    By Dark_Tower in forum Newbie
    Replies: 8
    Last Post: 31st March 2006, 02:49
  5. Determine system font
    By niala in forum Qt Programming
    Replies: 2
    Last Post: 21st March 2006, 22:45

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.