Incorrect Font Metrics



  • I'm trying to get the size of the bounding box that covers all the pixels of an arbitrary text, therefore I use the QFontMetricsF class passing it the correct font family and size:

    QFontMetricsF metrics(QFont("Freestyle Script", 64));
    metrics.boundingRect(text).size()
    

    Problem
    When using the Freestyle Script font the bounding box calculation performed by QFontMetricsF is incorrect! The first letter never fits in.
    I've tryed everything.. QFontMetricsF::width(), QFontMetricsF::size().. none solved the problem.

    P.S.
    I also noticed neither Microsoft Word nor Chrome (as SVG) managed to correctly draw a text in the Freestyle Script on Windows 10 suffering from the exact same problem. Still I hope there's any chance to get this fixed..

    2_1502201499208_insane4.png 1_1502201499208_insane3.png 0_1502201499207_insane2.png


  • Moderators

    @romsharkov
    are you sure this isn't correct.
    Definitely there are font out there with "overlapping" bounding boxes. This is the way they are designed.

    Check what QFontMetricsF::leftBearing() / QFontMetrics::rightBearing() returns for the characters you use for drawing.



  • @raven-worx Didn't know about those methods, just tested it, for the lower case letter "g":
    QFontMetricsF::leftBearing() returns: -4.17188
    QFontMetricsF::rightBearing() returns: 0.6875

    But even if I add the (negated) leftBearing to the total text width it still doesn't cover the tail entirely, so what's wrong here?


  • Moderators

    Hi! After fiddling around with the same problem for a while I decided to put simplicity over performance and resorted to this: https://forum.qt.io/post/409012



  • @Wieland I just tried out your approach with the offscreen painter, unfortunatelly it didn't solve the problem at all, the dimensions of the image remain the same as with QFontMetricsF.

    By the way.. the actual goal here is to generate and export an SVG image of a certain size (which is 200x100 as depicted on the screenshots above) entirely filled with an arbitrary text (maximized) in an arbitrary font, in other words make the text scale to fit the SVG canvas and export it.


  • Moderators

    @romsharkov Hmm, sorry then. I really thought this would work in all cases.


  • Qt Champions 2016

    If Word cant get it right , there might be some issues with the actual font table or
    its simply by design.


  • Moderators

    @romsharkov said in Incorrect Font Metrics:

    @raven-worx Didn't know about those methods, just tested it, for the lower case letter "g":
    QFontMetricsF::leftBearing() returns: -4.17188
    QFontMetricsF::rightBearing() returns: 0.6875
    But even if I add the (negated) leftBearing to the total text width it still doesn't cover the tail entirely, so what's wrong here?

    are you sure?!
    Those values seem to be correct / make sense according to your posted screens.

    Make sure that you add the absolute value (qAbs() for example). Also keep in mind that those values are only correct for the given font-size and that you can't add negative values to a image width.
    I guess you are just doing incorrect calculations. Can you show the code of the calculation?



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.