Solved

CDC: Text Width & Height

Posted on 2009-06-30
7
796 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 31

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 31

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 31

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

624 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