[comp.windows.ms.programmer] Font madness...

tom@mims-iris.waterloo.edu (Tom Haapanen) (02/26/91)

I'm trying to do some font handling, and having great difficulty
despite RTFM, RTFM and RTFM again (all SDK manuals, Petzold and Yao).

If I do EnumFonts() I can figure out what point sizes are available
on a given hDC (by doing a GetTextMetrics() and other magic to convert
a LOGFONT height into points).  But if I know what pointsize I want
(say, 8-point Helv), how do I select that size?  CreateFontIndirect()
and CreateFont() both want the height in device pixels, and including
the internal leading of the font.  How do I get 8-point?

Grumble grumble documentation grumble manual grumble grumble...

Thanks in advance...

[ \tom haapanen --- university of waterloo --- tom@mims-iris.waterloo.edu ]
[ "i don't even know what street canada is on"               -- al capone ]

tom@mims-iris.waterloo.edu (Tom Haapanen) (02/26/91)

I previously wrote:
> If I do EnumFonts() I can figure out what point sizes are available
> on a given hDC (by doing a GetTextMetrics() and other magic to convert
> a LOGFONT height into points).  But if I know what pointsize I want
> (say, 8-point Helv), how do I select that size?  CreateFontIndirect()
> and CreateFont() both want the height in device pixels, and including
> the internal leading of the font.  How do I get 8-point?

Well, it appears (after some CodeView-style investigation) that the "user
units" are just twips after all, and it's quite easy to get the right size
font.  I only wish that they referred to the units as twips instead of
this ambiguous "user unit".

This also now explains something else that I had wondered about...  Way
back, when I was using Excel 2.0, when I changed from an EGA display to
a VGA display, all of a sudden my worksheet font specs started getting
saved as 8.25, 9.75 and 13.75 points; I couldn't figure out why.  But it
appears that those are the actual available sizes for Helv using the VGA
driver; Excel wasn't doing the appropriate rounding...

Sorry to waste everybody's time!

[ \tom haapanen --- university of waterloo --- tom@mims-iris.waterloo.edu ]
[ "i don't even know what street canada is on"               -- al capone ]

lenp@microsoft.UUCP (Len POPP) (02/27/91)

In article <1991Feb25.204847.18731@watserv1.waterloo.edu>,
tom@mims-iris.waterloo.edu (Tom Haapanen) writes:
>I'm trying to do some font handling, and having great difficulty
>despite RTFM, RTFM and RTFM again (all SDK manuals, Petzold and Yao).
>
>If I do EnumFonts() I can figure out what point sizes are available
>on a given hDC (by doing a GetTextMetrics() and other magic to convert
>a LOGFONT height into points).  But if I know what pointsize I want
>(say, 8-point Helv), how do I select that size?  CreateFontIndirect()
>and CreateFont() both want the height in device pixels, and including
>the internal leading of the font.  How do I get 8-point?

I recently had to deal with this problem myself.  The answer is ... RTFM!  :-)

In the Windows 3.0 SDK Reference vol. 1, under CreateFont, it says:

    nHeight	int	Specifies the desired height (in logical units) of
		the font.  The font height can be specified in three ways:
		if nHeight is greater than zero, it is transformed into
		device units and matched against the cell height of the
		available fonts.  [...]  If it is less than zero, it is
		transformed into device units and the absolute value is
		matched against the character height of the available fonts.
		[etc.]

(Similar words are found under LOGFONT in vol. 2.)

So:

1. The font height is given in logical units, ie. according to the current
   mapping mode of the hdc.  That may be something like MM_TWIPS, which is
   easy (8 pts == 160 twips), or it may be MM_TEXT, in which case you have
   to look at GetDeviceCaps(hdc, LOGPIXELSY) to figure out how big 8 points is.

2. If nHeight > 0, it's the "cell height"; if nHeight < 0, it's the
   "character height".  (This is a bit confusing, because these aren't the
   same terms that are defined in section 2.12.  Oh well.)  What this really
   means is that if nHeight is positive it includes internal leading, and if
   it's negative it doesn't.  So in your case, use a negative number to get
   an 8-point font.

Simple!

Disclaimer: I don't speak officially for Microsoft Corp.  I didn't write
Windows.  I do use it an awful lot, though.

		Len Popp
		uunet!microsoft!lenp, lenp@microsoft.uucp

P.S.  Who's your pick for the F1 championship this year, Tom?