Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Incorrect Font Metrics
QtWS25 Last Chance

Incorrect Font Metrics

Scheduled Pinned Locked Moved Unsolved General and Desktop
fontmetricstextqt5.9qt quick 2.4
8 Posts 4 Posters 4.7k Views
  • 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.
  • romsharkovR Offline
    romsharkovR Offline
    romsharkov
    wrote on last edited by romsharkov
    #1

    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-worxR 1 Reply Last reply
    0
    • romsharkovR 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-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @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

      romsharkovR 1 Reply Last reply
      0
      • raven-worxR raven-worx

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

        romsharkovR Offline
        romsharkovR Offline
        romsharkov
        wrote on last edited by
        #3

        @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-worxR 1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          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

          romsharkovR 1 Reply Last reply
          0
          • ? A Former User

            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

            romsharkovR Offline
            romsharkovR Offline
            romsharkov
            wrote on last edited by romsharkov
            #5

            @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
            0
            • romsharkovR 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.

              ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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
                0
                • romsharkovR romsharkov

                  @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-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by
                  #8

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

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved