Solved

CDC: Text Width & Height

Posted on 2009-06-30
7
759 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:Invader
  • 3
  • 2
  • 2
7 Comments
 
LVL 30

Assisted Solution

by:Zoppo
Zoppo earned 30 total points
ID: 24744833
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
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 30 total points
ID: 24745252
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
 

Author Comment

by:Invader
ID: 24745421
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 30

Expert Comment

by:Zoppo
ID: 24745692
> 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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 24746101
>>>> 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
 
LVL 30

Expert Comment

by:Zoppo
ID: 24746181
... or analyze the bitmap returned by GetGlyphOutline using GGO_BITMAP.
0
 

Accepted Solution

by:
Invader earned 0 total points
ID: 24767872
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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now