Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Calculating Font Height for printing

71 views
Skip to first unread message

Steve Martin

unread,
Nov 28, 2000, 3:00:00 AM11/28/00
to
I'm having some trouble figuring my font height. Here's what I'm doing to
create the font:


int nHeight = -MulDiv (nFontSize, dc->GetDeviceCaps (LOGPIXELSY), 72);

lFont->CreateFont (nHeight, 0,0,0, FW_NORMAL, FALSE, 0, 0,
DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, strFontName);
dc->SelectObject(lFont);

Now I thought that "-MulDiv" line would work, but it seems the higher my
Point Size, i.e. 10, 14, 16, etc the more off it is.
This is for a laser printer device. Now I iterate through a list<> of
strings like so:

/////////////////////////////////////////////////////
// Walk the list and look for the TAGS

i = lTextOut.begin();
while(i != lTextOut.end())
{
dc->TextOut(xPixel,yPixel, *i);

yPixel += (tm.tmHeight + tm.tmExternalLeading);
i++;
}

//end the page
dc->EndPage();

//End Print job
dc->EndDoc();


Can anyone tell me what I'm doing wrong? I know if I manually add a number
to the yPixel it will work but this should be calculated already in the
first line.
Now this works perfect for Times Roman, point size 10, but decreases with
each size higher.
Any help would be great!

Thanks,
Steve


SNOW CRASH

unread,
Nov 28, 2000, 3:00:00 AM11/28/00
to
On Tue, 28 Nov 2000 12:13:11 GMT, "Steve Martin" <smar...@yahoo.com>
wrote:

Try using LOGPIXELSX instead of LOGPIXELSY in your -MulDiv() function
call. That's how I always do it, and it works fine for me.

Example function follows..

// Get Width/Height of Label Rectangle
SelectFont("Verdana", 8, 0, 0, 0, 0, 0, 0, 0);
CSize sz = GetTextExtent("Example Text");

// CHRIS


long CXCMPrintCtrlCtrl::SelectFont(LPCTSTR szFontName, long nFontSize,


long nEscapement, long nOrientation, long bFontBold, long bFontItalic,


long bStrikeOut, long bUnderline, long nWidth)
{
// Necessary Conditions
if(m_pCDCPrinter == NULL)
return(0);

// Remove any previous font selected
CleanUpFont();

// Create New Font
CFont* pNewFont = new CFont();

LOGFONT lf;
::ZeroMemory(&lf, sizeof(LOGFONT));

lf.lfHeight = - MulDiv( nFontSize,
m_pCDCPrinter->GetDeviceCaps(LOGPIXELSX), 72);
lf.lfWidth = nWidth;
lf.lfEscapement = nEscapement;
lf.lfOrientation = nOrientation;

if(bFontBold)
lf.lfWeight = 800;
else
lf.lfWeight = FW_NORMAL;

if(bFontItalic)
lf.lfItalic = TRUE;
else
lf.lfItalic = FALSE;

if(bUnderline)
lf.lfUnderline = TRUE;
else
lf.lfUnderline = FALSE;

if(bStrikeOut)
lf.lfStrikeOut = TRUE;
else
lf.lfStrikeOut = FALSE;

lf.lfCharSet = DEFAULT_CHARSET;
lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
lf.lfQuality = DEFAULT_QUALITY;
lf.lfPitchAndFamily = DEFAULT_PITCH;

strcpy(lf.lfFaceName, szFontName);

if(!pNewFont->CreateFontIndirect( &lf ))
{
// Error!
delete pNewFont;
return (0);
}

// Assign to m_pCFontSelected
m_pCFontSelected = pNewFont;

// Select Font into printer device
m_pCFontPrev = m_pCDCPrinter->SelectObject(m_pCFontSelected);
return(1);
}


// CHRIS


nmacfarl

unread,
Nov 28, 2000, 3:00:00 AM11/28/00
to
LOGPIXELSY should be correct. Are you in MM_TEXT mode when you make your
MulDiv call ?
If not, you'll be way off on your calculation.


"Steve Martin" <smar...@yahoo.com> wrote in message
news:rjNU5.40168$751.1...@typhoon.ne.mediaone.net...

nmacfarl

unread,
Nov 28, 2000, 3:00:00 AM11/28/00
to
Are you in MM_TEXT mode when you perform MulDiv ?
If not you'll be off. on your calculation.

David Lowndes

unread,
Nov 28, 2000, 7:29:22 PM11/28/00
to
>I'm having some trouble figuring my font height. Here's what I'm doing to
>create the font:
>
> int nHeight = -MulDiv (nFontSize, dc->GetDeviceCaps (LOGPIXELSY), 72);
>
> lFont->CreateFont (nHeight, 0,0,0, FW_NORMAL, FALSE, 0, 0,
> DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
> DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, strFontName);
> dc->SelectObject(lFont);
>
>Now I thought that "-MulDiv" line would work, but it seems the higher my
>Point Size, i.e. 10, 14, 16, etc the more off it is.

Steve,

Precisely what do you mean by "off" ?

When do you call GetTextMetrics?

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
My address is altered to discourage junk mail.
Please post responses to the newsgroup thread,
there's no need for follow-up email copies.

smar...@yahoo.com

unread,
Nov 29, 2000, 3:00:00 AM11/29/00
to
In article <1fj82ts4hlr0qcdsr...@4ax.com>,

Ok I fixed the problem, it seems I wasn't placing a GetTextMetrics in
the correct place, I placed it after the font was selected, i.e. before
my TextOut() and it's fine now. Before as the font size was
increased, my tm calculation didn't seem to increase with it and the
text overlapped line to line:

GetTextMetrics(dc->GetSafeHdc(), &tm);

i = lTextOut.begin();
while(i != lTextOut.end())
{
dc->TextOut(xPixel,yPixel, *i);
yPixel += (tm.tmHeight + tm.tmExternalLeading);
i++;
}

//end the page
dc->EndPage();


Sent via Deja.com http://www.deja.com/
Before you buy.

Dale M. Nurden

unread,
Dec 1, 2000, 3:00:00 AM12/1/00
to
On Tue, 28 Nov 2000 12:13:11 GMT, "Steve Martin" <smar...@yahoo.com>
wrote:

>I'm having some trouble figuring my font height. Here's what I'm doing to
>create the font:
>
>
> int nHeight = -MulDiv (nFontSize, dc->GetDeviceCaps (LOGPIXELSY), 72);
>
> lFont->CreateFont (nHeight, 0,0,0, FW_NORMAL, FALSE, 0, 0,
> DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
> DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, strFontName);
> dc->SelectObject(lFont);
>
>Now I thought that "-MulDiv" line would work, but it seems the higher my
>Point Size, i.e. 10, 14, 16, etc the more off it is.

Take note of this line from the API documentation:

" For all height comparisons, the font mapper looks for the largest
font that does not exceed the requested size. "

In other words, the value you specify to CreateFont() for nHeight is
not necessarily the height of the font that you get out of it. It just
guarantees that the font won't be any larger, but it could be smaller
if the font mapper can't find one that fits exactly.

You should use GetTextMetrics() to get actual size of the font in
device units, examining both the .tmHeight and .tmExternalLeading
members of the TEXTMETRIC structure that this function fills.

-Dale
--
Dale M. Nurden __|__ http://users.iafrica.com/d/da/dalen
dalen-rcis.co.za | Highway Radio, 101.5FM
dalen-iafrica.com | Christian Community Radio
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Author of TClockEx, the FREE taskbar clock enhancer.
Spam proofing in effect: Please use '@' in place of '-'.

0 new messages