Click here to Skip to main content
15,902,447 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Extracting info from a web page Pin
RobJones2-Oct-03 7:07
RobJones2-Oct-03 7:07 
GeneralCRecordset input parameters Pin
pocovnicu1-Oct-03 23:19
pocovnicu1-Oct-03 23:19 
GeneralRe: CRecordset input parameters Pin
vcplusplus2-Oct-03 1:50
vcplusplus2-Oct-03 1:50 
GeneralRe: CRecordset input parameters Pin
pocovnicu2-Oct-03 2:50
pocovnicu2-Oct-03 2:50 
GeneralRe: CRecordset input parameters Pin
David Crow2-Oct-03 3:06
David Crow2-Oct-03 3:06 
GeneralRe: CRecordset input parameters Pin
pocovnicu2-Oct-03 3:17
pocovnicu2-Oct-03 3:17 
GeneralRe: CRecordset input parameters Pin
David Crow2-Oct-03 3:33
David Crow2-Oct-03 3:33 
Questionhow to set the height of a truetype font ? Pin
JP GOBLET1-Oct-03 23:19
JP GOBLET1-Oct-03 23:19 
For a technical drafting program, i need to draw truetype fonts with total control on their height : for example i want a text of exactly 10mm high.

The problem is in interpretation of height; for truetype fonts, this height is made of 3 arts : Descent (part under the baseline), Ascent (part over the baseline), InternalLeading (some space above the character). All these values are returned by pDC->GetTextMetrics() when you have selected a font in the pDC.

In my case, when i say i want a font 10 mm high, it means a font whose characters above the baseline (such as digits) will be 10 mm high. This convention is used by programs such as Autocad, MicroStation, ...

For creating a CFont object, you have to fill a LOGFONT struct with the properties of the font, such as font name, height, weight, ... But as said before, the height specified here include descent + ascent + leading. Here is how i try to get what i want :

CFont *pOldFont, newfont;
TEXTMETRIC Metrics;
LOGFONT lf;
double HcarFromFont, HcarWanted;

memset(&lf, 0, sizeof(LOGFONT));
strcpy(lf.lfFaceName, "Arial");

// HcarWanted is the required height (say 10 mm) converted in pixels;
// the height must be specified in logical units, which are pixels because the the current mapMode is MM_TEXT.
HcarWanted = 10 /* mm */ * ConversionFactor; // ConversionFactor translates mm in pixels

// set the height - this is just a first try :
lf.lfHeight = - (long)HcarWanted; // note the minus sign, this is normal (see doc. for LOGFONT)

... // set others font properties

newfont.CreateFontIndirect(&lf);
pOldFont = pDC->SelectObject(&newfont);
pDC->GetTextMetrics(&Metrics); // get metrics (in pixels) for the font just created
// read the current height for ascent part :
HcarFromFont = Metrics.tmAscent - Metrics.tmInternalLeading;

// here is the trick : the height of the ascent part is of course smaller than the required height;
// so i correct the total height (lf.lfHeight) so that the ascent part grows up to the required height :
lf.lfHeight = (long) (lf.lfHeight * (HcarWanted / HcarFromFont));

// example : let's say that HcarWanted is 100 pixels and that GetTextMetrics()
// returned HcarFromFont = Metrics.tmAscent - Metrics.tmInternalLeading = 60;
// so the new lf.lfHeight is multiplied by * 100/60 = 1.66

newfont.Detach();
newfont.DeleteObject();
newfont.CreateFontIndirect(&lf); // recreate the font with the corrected height
pDC->SelectObject(&newfont);
// the following lines just check that the new height of the ascent part is very near of the required height :
pDC->GetTextMetrics(&Metrics);
HcarFromFont = Metrics.tmHeight - Metrics.tmInternalLeading;


Problem : the resulting height on screen is near the required height but there is still an error up to 20 %; for a same specified height, some fonts are smaller, some are bigger.



JPG
AnswerRe: how to set the height of a truetype font ? Pin
Mike Dimmick2-Oct-03 4:25
Mike Dimmick2-Oct-03 4:25 
GeneralRe: how to set the height of a truetype font ? Pin
JP GOBLET2-Oct-03 4:48
JP GOBLET2-Oct-03 4:48 
AnswerRe: how to set the height of a truetype font ? Pin
G. Steudtel2-Oct-03 5:02
G. Steudtel2-Oct-03 5:02 
GeneralRe: how to set the height of a truetype font ? Pin
JP GOBLET2-Oct-03 5:51
JP GOBLET2-Oct-03 5:51 
GeneralRe: how to set the height of a truetype font ? Pin
G. Steudtel2-Oct-03 6:06
G. Steudtel2-Oct-03 6:06 
GeneralRe: how to set the height of a truetype font ? Pin
JP GOBLET4-Oct-03 5:34
JP GOBLET4-Oct-03 5:34 
GeneralAccessing document properties from a VC++ application Pin
jopett1-Oct-03 21:15
jopett1-Oct-03 21:15 
GeneralRe: Accessing document properties from a VC++ application Pin
Michael P Butler2-Oct-03 1:32
Michael P Butler2-Oct-03 1:32 
GeneralRe: Accessing document properties from a VC++ application Pin
jopett2-Oct-03 20:57
jopett2-Oct-03 20:57 
GeneralRide Control System Pin
immanis1-Oct-03 20:54
immanis1-Oct-03 20:54 
GeneralRe: Ride Control System Pin
immanis1-Oct-03 22:58
immanis1-Oct-03 22:58 
GeneralRe: Ride Control System Pin
jhwurmbach1-Oct-03 23:44
jhwurmbach1-Oct-03 23:44 
GeneralRe: Ride Control System Pin
David Chamberlain2-Oct-03 3:09
David Chamberlain2-Oct-03 3:09 
GeneralRe: Ride Control System Pin
jhwurmbach2-Oct-03 5:36
jhwurmbach2-Oct-03 5:36 
GeneralRe: Ride Control System Pin
David Chamberlain2-Oct-03 6:31
David Chamberlain2-Oct-03 6:31 
GeneralRe: Ride Control System Pin
immanis2-Oct-03 5:47
immanis2-Oct-03 5:47 
GeneralRe: Ride Control System Pin
David Chamberlain2-Oct-03 6:49
David Chamberlain2-Oct-03 6:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.