Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Unsolved Incorrect Font Metrics

    General and Desktop
    font metrics text qt5.9 qt quick 2.4
    4
    8
    3571
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • romsharkov
      romsharkov last edited by romsharkov

      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

      raven-worx 1 Reply Last reply Reply Quote 0
      • raven-worx
        raven-worx Moderators @romsharkov last edited by

        @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.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        romsharkov 1 Reply Last reply Reply Quote 0
        • romsharkov
          romsharkov @raven-worx last edited by

          @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?

          raven-worx 1 Reply Last reply Reply Quote 0
          • ?
            A Former User last edited by

            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

            romsharkov 1 Reply Last reply Reply Quote 0
            • romsharkov
              romsharkov @Guest last edited by romsharkov

              @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.

              ? 1 Reply Last reply Reply Quote 0
              • ?
                A Former User @romsharkov last edited by

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

                1 Reply Last reply Reply Quote 0
                • mrjj
                  mrjj Lifetime Qt Champion last edited by

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

                  1 Reply Last reply Reply Quote 0
                  • raven-worx
                    raven-worx Moderators @romsharkov last edited by

                    @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?

                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                    If you have a question please use the forum so others can benefit from the solution in the future

                    1 Reply Last reply Reply Quote 2
                    • First post
                      Last post