Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How do I get font width and height in pixels ?

How do I get font width and height in pixels ?

Scheduled Pinned Locked Moved C / C++ / MFC
question
9 Posts 3 Posters 0 Views 1 Watching
  • 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.
  • U Offline
    U Offline
    udiraz
    wrote on last edited by
    #1

    Hi All, I want to draw text inside a bounding rect.Therefore I want to change the font size to fit the rect. I am trying to find a way to calculate the text width and height, knowing it's font. Why do different fonts with the same size have different height? Thanks, Udi Raz

    B 1 Reply Last reply
    0
    • U udiraz

      Hi All, I want to draw text inside a bounding rect.Therefore I want to change the font size to fit the rect. I am trying to find a way to calculate the text width and height, knowing it's font. Why do different fonts with the same size have different height? Thanks, Udi Raz

      B Offline
      B Offline
      basementman
      wrote on last edited by
      #2

      See DrawText() with the DT_CALCRECT flag, GetTextMetrics(), GetCharABCWidths(), GetCharWidthFloat() and GetCharWidth32  onwards and upwards...

      U 1 Reply Last reply
      0
      • B basementman

        See DrawText() with the DT_CALCRECT flag, GetTextMetrics(), GetCharABCWidths(), GetCharWidthFloat() and GetCharWidth32  onwards and upwards...

        U Offline
        U Offline
        udiraz
        wrote on last edited by
        #3

        Thanks but already tried it all and everything else I found in the msdn regarding fonts. The answer for the height in all methods is the font height. The problem is that fonts don't use the whole width to draw their characters. As a result, if I create a rectangle with the size x, a certain font with the size of x will be drawn perfectly well inside my rectangle but a second font (for Hindi fonts) will be drawn very small. What I need (and start to think there is no such a method) is a method that gives me the true height of the character. Any Ideas?

        B 1 Reply Last reply
        0
        • U udiraz

          Thanks but already tried it all and everything else I found in the msdn regarding fonts. The answer for the height in all methods is the font height. The problem is that fonts don't use the whole width to draw their characters. As a result, if I create a rectangle with the size x, a certain font with the size of x will be drawn perfectly well inside my rectangle but a second font (for Hindi fonts) will be drawn very small. What I need (and start to think there is no such a method) is a method that gives me the true height of the character. Any Ideas?

          B Offline
          B Offline
          basementman
          wrote on last edited by
          #4

          Use GetTextMetrics(). Read the info on MSDN on font layout. You can then calculate the actual line height via the members in the TEXTMETRIC struct that is filled out from the call to GetTextMetrics(). Specifically, the tmHeight and tmOverhang members.  onwards and upwards...

          U 1 Reply Last reply
          0
          • B basementman

            Use GetTextMetrics(). Read the info on MSDN on font layout. You can then calculate the actual line height via the members in the TEXTMETRIC struct that is filled out from the call to GetTextMetrics(). Specifically, the tmHeight and tmOverhang members.  onwards and upwards...

            U Offline
            U Offline
            udiraz
            wrote on last edited by
            #5

            Thanks again for your patience. I checked each of the TEXTMETRIC members and didn't see the answer. tmHeight is the font size and tmOverhang is the extra width if I use Italic or Bold which I use none of them. Still, If I choose "Ariel" with the size X and "Comic Sans MS" with the same size, the display font height are different. In addition, Using the same font, the letter 'a' and the letter 'b' have a different height. Therefore to know the real height of a specific font, the query method needs to the recieve the specific text in addition to the HDC. I am not sure there is such a method and I will be satisfied with known the difference between fonts please advise Thanks in advance, Udi Raz

            S 1 Reply Last reply
            0
            • U udiraz

              Thanks again for your patience. I checked each of the TEXTMETRIC members and didn't see the answer. tmHeight is the font size and tmOverhang is the extra width if I use Italic or Bold which I use none of them. Still, If I choose "Ariel" with the size X and "Comic Sans MS" with the same size, the display font height are different. In addition, Using the same font, the letter 'a' and the letter 'b' have a different height. Therefore to know the real height of a specific font, the query method needs to the recieve the specific text in addition to the HDC. I am not sure there is such a method and I will be satisfied with known the difference between fonts please advise Thanks in advance, Udi Raz

              S Offline
              S Offline
              Shraddhan
              wrote on last edited by
              #6

              I hope you don't mind me sneaking in here. I have the same problem with showing numbers in CEdit controls. How to find the TRUE pixel width of the text? I need to know how many digits can be shown in the control so that I can put in as many as possible, and round the number correctly. It is easy enough to use DrawText() with the DT_CALCRECT flag, making sure that SetFont is done correctly, and for small font sizes the code works well. But as the fonts get bigger, DrawText's calculations just do not agree with those of CEdit. I'm stumped too, and have temporarily had to restrict the font face and size, and apply magic numbers to get the stuff to work. Shraddhan

              U 1 Reply Last reply
              0
              • S Shraddhan

                I hope you don't mind me sneaking in here. I have the same problem with showing numbers in CEdit controls. How to find the TRUE pixel width of the text? I need to know how many digits can be shown in the control so that I can put in as many as possible, and round the number correctly. It is easy enough to use DrawText() with the DT_CALCRECT flag, making sure that SetFont is done correctly, and for small font sizes the code works well. But as the fonts get bigger, DrawText's calculations just do not agree with those of CEdit. I'm stumped too, and have temporarily had to restrict the font face and size, and apply magic numbers to get the stuff to work. Shraddhan

                U Offline
                U Offline
                udiraz
                wrote on last edited by
                #7

                Hi Shraddhan, I get the filling that there is no way to get the real height, but as regarding the width, although I can't help you right now, I think in one of my attempts I got the real width using one of the following methods GetTextMetrics(), GetTextExtentPoint32(). It depands whether you are using a raster font or a open type/true type font. I think that is works for me, but don't remember how. I still trying to find the real height... Udi Raz

                S 1 Reply Last reply
                0
                • U udiraz

                  Hi Shraddhan, I get the filling that there is no way to get the real height, but as regarding the width, although I can't help you right now, I think in one of my attempts I got the real width using one of the following methods GetTextMetrics(), GetTextExtentPoint32(). It depands whether you are using a raster font or a open type/true type font. I think that is works for me, but don't remember how. I still trying to find the real height... Udi Raz

                  S Offline
                  S Offline
                  Shraddhan
                  wrote on last edited by
                  #8

                  I couldn't find information on GetTextExtentPoint32. GetTextMetrics returns a TEXTMETRIC structure that gives average and maximum character width, which are no good for my purposes. Under GetTextExtent and GetOutputTextExtent, Microsoft notes: Since some devices do not place characters in regular cell arrays (that is, they carry out kerning), the sum of the extents of the characters in a string may not be equal to the extent of the string. So clearly ambiguous! My notes on my problem with fitting strings into CEdit controls mention that the text is shown with white space on either side. In fact, with large font sizes. the output is chopped on the right if it gets anywhere near the edge of the edit box. But I have no idea what the whitespace allowance should be. It depends on the face as well as the size. So is this a feature of CEdit controls? As for finding the overall height, one last ditch method occurred to me: draw the text to a bitmap and measure the height of the drawn area by scanning the pixels. Don't ask me how, though. Shraddhan

                  U 1 Reply Last reply
                  0
                  • S Shraddhan

                    I couldn't find information on GetTextExtentPoint32. GetTextMetrics returns a TEXTMETRIC structure that gives average and maximum character width, which are no good for my purposes. Under GetTextExtent and GetOutputTextExtent, Microsoft notes: Since some devices do not place characters in regular cell arrays (that is, they carry out kerning), the sum of the extents of the characters in a string may not be equal to the extent of the string. So clearly ambiguous! My notes on my problem with fitting strings into CEdit controls mention that the text is shown with white space on either side. In fact, with large font sizes. the output is chopped on the right if it gets anywhere near the edge of the edit box. But I have no idea what the whitespace allowance should be. It depends on the face as well as the size. So is this a feature of CEdit controls? As for finding the overall height, one last ditch method occurred to me: draw the text to a bitmap and measure the height of the drawn area by scanning the pixels. Don't ask me how, though. Shraddhan

                    U Offline
                    U Offline
                    udiraz
                    wrote on last edited by
                    #9

                    Thanks, I know how ( by using DC::Getpixel() ) but will not take a reasonable time...

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups