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.
  • R Offline
    R Offline
    romsharkov
    wrote on 8 Aug 2017, 14:19 last edited by romsharkov 8 Aug 2017, 14:22
    #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

    R 1 Reply Last reply 8 Aug 2017, 14:23
    0
    • R romsharkov
      8 Aug 2017, 14:19

      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

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 8 Aug 2017, 14:23 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

      R 1 Reply Last reply 8 Aug 2017, 14:44
      0
      • R raven-worx
        8 Aug 2017, 14:23

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

        R Offline
        R Offline
        romsharkov
        wrote on 8 Aug 2017, 14:44 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?

        R 1 Reply Last reply 9 Aug 2017, 06:42
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on 8 Aug 2017, 14:47 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

          R 1 Reply Last reply 8 Aug 2017, 15:11
          0
          • ? A Former User
            8 Aug 2017, 14:47

            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

            R Offline
            R Offline
            romsharkov
            wrote on 8 Aug 2017, 15:11 last edited by romsharkov 8 Aug 2017, 15:44
            #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 8 Aug 2017, 15:44
            0
            • R romsharkov
              8 Aug 2017, 15:11

              @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 8 Aug 2017, 15:44 last edited by
              #6

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

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 8 Aug 2017, 16:45 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
                • R romsharkov
                  8 Aug 2017, 14:44

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

                  R Offline
                  R Offline
                  raven-worx
                  Moderators
                  wrote on 9 Aug 2017, 06:42 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

                  2/8

                  8 Aug 2017, 14:23

                  topic:navigator.unread, 6
                  • Login

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