Results 1 to 7 of 7

Thread: Find maximum size of a given font that will fit a given size

  1. #1
    Join Date
    Jul 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Find maximum size of a given font that will fit a given size

    Hi all,

    I want to render some text with Qt that will be used in a texture in OpenGL. I have the basic setup done using QGraphicsView::render(QPainter...) and QGLWidget::convertToGLFormat(image), and that works. I use a QWidget with a QLabel with setWordWrap(true) inside my QGraphicsScene.

    The problem is that I'm using a fixed font size. What I'd like is that given a string of text and a font family (say Arial), find the maximum size that will fit the texture. This has to consider word wrapping when possible. For example:

    Qt Code:
    1. QFont getMaximumFontSize(const QString& text, const QString& fontFamily, int width, int height)
    2. {
    3. //...
    4. }
    To copy to clipboard, switch view to plain text mode 
    (width and height are the dimensions of the texture)

    I have an iterative approach that kind of works, using QFontMetrics::boundingRect() starting at a large size and going down when it doesn't fit well enough. The problem is that there is no clear indication that the text doesn't fit.

    Qt Code:
    1. QFont getMaxFontSize(const QString& text, const QString& fontFamily, int width, int height)
    2. {
    3. int size = 1000;
    4. QFont font("Arial", size);
    5. QRect max(0,0,width,height);
    6. int widthThreshold = int(float(max.width()) * 0.1);
    7. int heightThreshold = int(float(max.height()) * 0.1);
    8.  
    9. while (size > 1)
    10. {
    11. QFontMetrics metrics(font);
    12. QRect bb = metrics.boundingRect(max, Qt::TextWordWrap, text);
    13. if (bb.width() > (max.width() - widthThreshold) ||
    14. bb.height() > (max.height() - heightThreshold))
    15. {
    16. --size;
    17. font = QFont("Arial", size);
    18. }
    19. else
    20. {
    21. break;
    22. }
    23. }
    24.  
    25. return font;
    26. }
    To copy to clipboard, switch view to plain text mode 
    In addition to being potentially slow (going from 1000 in steps of 1, creating a new QFont and QFontMetrics each iteration), sometimes putting all the text on one line is a better "fit" according to the criteria in my while() loop above. But in most cases it works well.

    Would anyone have some ideas as to how to get the information I want, to improve upon what I have? What I would really like is to get that info in one shot instead of iterating over font sizes. Thanks in advance,

    Skylark

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Find maximum size of a given font that will fit a given size

    One simple approach could be to create a more efficient algorithm for you font decrease. If it is too big use as a new font size the half of the last one. E.g. you start with 100, use 50. If then it is too small increase with the half of your last step, which would be 25. etc. With that you can speed up things a lot.
    Also don't create a new QFont each time. Better alter the old object by using setPointSize().

  3. #3
    Join Date
    Jul 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Find maximum size of a given font that will fit a given size

    Sure I can improve the algorithm to reduce the number of iterations. But I was wondering if there was a way to get exactly the information I need. If not, then I guess I'll do it like that. It's the next best thing.

    Thanks for the suggestion Lykurg.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Find maximum size of a given font that will fit a given size

    Once I had the same problem, but I don't have found a build in function. Another solution could be that you paint the text with a default size (with a few checks if you want the text to be drawn in 1, 2, 3, ... lines) and then simply scale the image. If you can live with the lower quality that could be also an option. I had chosen the solution with finding the best font size.

  5. #5
    Join Date
    Jul 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Find maximum size of a given font that will fit a given size

    OK thanks, good to know someone has been in the same boat as me in the past.

  6. #6
    Join Date
    Mar 2007
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Find maximum size of a given font that will fit a given size

    My requirements are a bit different, but here's my code.

    Qt Code:
    1. void
    2. TextItem::setGeometry(const QRectF& rect)
    3. {
    4. QFont theFont = font();
    5.  
    6. int size = qMin(static_cast<int>(rect.height()), 40);
    7.  
    8. theFont.setPixelSize(size);
    9. QFontMetricsF fm(theFont);
    10. while(fm.width(toPlainText()) > rect.width())
    11. {
    12. size -= 5;
    13. theFont.setPixelSize(size);
    14. fm = QFontMetricsF(theFont);
    15. }
    16.  
    17. setFont(theFont);
    18. setPos(rect.topLeft());
    19. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Find maximum size of a given font that will fit a given size

    I agree with Lykburg; binary searching will be very fast, and will plow through a 1000-point range in less than eight steps on average.

    Another approach: create a string, measure it, divide it into the available space and simply scale the font by that factor. For a fixed-width font, this should work nicely.

Similar Threads

  1. The maximum size of a QList
    By Denarius in forum Newbie
    Replies: 3
    Last Post: 30th November 2012, 11:27
  2. adjust font size to QLabel-size
    By hunsrus in forum Qt Programming
    Replies: 0
    Last Post: 9th July 2008, 15:33
  3. maximum size of xml files
    By freeskydiver in forum Qt Programming
    Replies: 1
    Last Post: 15th January 2008, 11:02
  4. change font size and button size of QMessageBox
    By nass in forum Qt Programming
    Replies: 6
    Last Post: 13th September 2006, 20:16
  5. howto expand frame to maximum size
    By masoroso in forum Newbie
    Replies: 4
    Last Post: 21st April 2006, 10:40

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.