When I discussed mapping modes in Chapter 11, you might have thought the MM_TWIPS mapping mode would be used by programs that make heavy use of formatted text. In this mapping mode, logical units are in terms of 1/20 point. However, you probably won't want to use MM_TWIPS for the video display, because the mapping mode is based on real inches rather than logical inches. As a result, your program won't be able to equate the correct point sizes (8, 10, 12, 14, 18, and 24) of the available screen fonts to their heights in MM_TWIPS units.
You'll be better off if you define your mapping mode based on the logical-pixels-per-inch dimensions available from GetDeviceCaps. I call this the ”Logical Twips“ mapping mode; here's all you need to set it:
SetMapMode (hdc, MM_ANISOTROPIC) ;
SetWindowExt (hdc, 1440, 1440) ;
SetViewportExt (hdc, GetDeviceCaps (hdc, LOGPIXELSX),
GetDeviceCaps (hdc, LOGPIXELSY)) ;
Because the pixels-per-logical-inch values are always divisors of 1440, the scaling factor for this mapping mode is an integer. With this mapping mode set, if you want to request a font with 12-point line spacing (as we'll do shortly), you can specify the height of the font as 240 (12 times 20) logical units.
If you select a font into your device context and call GetTextMetrics to obtain the dimensions of the font, you can calculate the type size in points by using the formula:
(tm.tmHeight - tm.tmInternalLeading) / 20
The line spacing in points is equal to:
tm.tmHeight / 20
For some smaller fonts on low-resolution devices, the size and spacing of the type might actually involve a fraction of a point—for example, 8-point type with 8.5-point line spacing. To round to the nearest integer point size, you might instead want to use the formulas:
(tm.tmHeight - tm.tmInternalLeading + 10) / 20
and:
(tm.tmHeight + 10) / 20
We'll use the ”Logical Twips“ mapping mode in the JUSTIFY program toward the end of this chapter.
Once again, remember that the discrepancy between logical inches and real inches occurs only for the display. If you use the ”Logical Twips“ mapping mode with a printer, you'll simply duplicate the MM_TWIPS mapping mode.