CDC: Text Width & Height

I need a way to calculate the actual height of a character drawn with CDC::DrawText.

Take for example the attached picture where the letter "s" is drawn with a grey background.
GetTextExtend returns the height of the character including this background space.
Same goes for DrawText if used with the DT_CALCRECT flag.

Is there a way to get the height of only the actual character?
Just from the top of the "s" to the bottom of it ?

Thanks..







char.JPG
InvaderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZoppoCommented:
Hi Invader,

I'm not sure if it works, but you can try following:

- start creating a path for the DC using BeginPath.
- draw the text with DrawText.
- call EndPath to stop creating the path.
- create a region from the path using PathToRegion.
- use GetRgnBox to get the bounding box of the create region.

Another method (which I even don't know if it works) could be:

- create a memory device context
- use SetBoundsRect, DrawText and GetBoundsRect to get the dimensions.

For both a path and the boundsrect I'm not sure if really only the drawn part of the character is used, therefor I fear you have to test them.

If none of them works you can even try to get the real size of the character using GetGlyphOutline with GGO_METRICS - I never used this before, but if I understand correctly what it does it should return the info you need in a GLYPHMETRICS structure ...


Hope that helps,

ZOPPO
0
itsmeandnobodyelseCommented:
Use the function GetTextExtentPoint32 like

  SIZE siz = { 0 };
  GetTextExtentPoint32(hdc, "s", 1, &siz);

Note, the hdc must be a handle to the device context actually used for drawing the text. So, you best calculate the size when you actually were drawing in one of the drawing handler functions called after a WM_PAINT message.  

>>>> For both a path and the boundsrect I'm not sure if really only the drawn part of the character is used, therefor I fear you have to test them.
Indeed, I share the doubts with Zoppo. So, the width of the "s" string probably is ok, but probably the height will contain some 'internal leading' which is immanent to the font but not to the character 's'.
0
InvaderAuthor Commented:
Zoppo:

I tried both the BeginPath and the SetBoundsRect approach. They work as they should but they still include the background area of the font.

I also did some testing with GetGlyphOutline with no luck but I will do some more. I think this one is the most likely to produce the desired result since its supposed to return the outline of the string.

itsmeandnobodyelse:

GetTextExtentPoint32 is the same as GetTextExtend. If you follow the latter, it calls the first one. However, you are right, the width is correct for any string and the height returned is every time of "fixed" size as required by the selected font.

------
I think best bet, is GetGlyphOutline...if I manage to get it to work..
Although it still has the drawback that it works only for True Type Fonts.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

ZoppoCommented:
> works only for True Type Fonts
I'm not sure, it maybe it works for none-TrueType vector fonts too (AFAIK windows FNT-files can contain raster- or vector fonts).

For raster fonts I guess there's no possibility to get the info you want except maybe analyse the bitmap stored in the FNT file for the character to find the bounding of none-white pixels, since a raster font is just a bunch of bitmaps of same heigth.

ZOPPO
0
itsmeandnobodyelseCommented:
>>>> except maybe analyse the bitmap stored in the FNT file for the character
Maybe it is easier to read the bits of the rectangle on screen into a bitmap. If the background was white or unicolored, it shouldn't be much difficult to find the first non-white pixel when going from top to bottom for all columns.
0
ZoppoCommented:
... or analyze the bitmap returned by GetGlyphOutline using GGO_BITMAP.
0
InvaderAuthor Commented:
Apparenty GetGlyphOutline just returns the points of the outline that is stored in the font.
That is, regardless of font selected.
The font contains the outline, and it is transformed in other sizes when drawn..

I think Im giving up on this..
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.