TVSKIN源代碼閱讀日記(五)--- CFONT CLASS (MFC LIB) & LOGFONT STRUCT(GDI)

CFONT

Encapsulates a windows graphics device interface font and provides member functions for manipulating the font.

CFont類是MFC類庫中的類,是封裝了GDI中的LOGFONT結構而來的。

class CFont : public CGdiObject

 

Remarks

To use a CFont object, construct a CFont object and attatch a window front to it with CreateFont,CreateFontInDirect, CreatePointFont, or CreatePointFontIndirect, and then use the object’s member functions to maniipulate the font. The CreatePointFont and CreatePointFontInDirect functions are often easier to use than CreateFont or CreateFontIndirect since they do the conversion for the height of the font from a point size to logical units automatically.

 

CFont::CreateFont

BOOL CreateFont(
   int nHeight,
   int nWidth,
   int nEscapement,
   int nOrientation,
   int nWeight,
   BYTE bItalic,
   BYTE bUnderline,
   BYTE cStrikeOut,
   BYTE nCharSet,
   BYTE nOutPrecision,
   BYTE nClipPrecision,
   BYTE nQuality,
   BYTE nPitchAndFamily,
   LPCTSTR lpszFacename 
);

 

Parameters

nHeight

Specifies the desired height (in logical units) of the font. See the lfHeight member of the LOGFONTstructure in the Platform SDK for a description. The absolute value of nHeight must not exceed 16,384 device units after it is converted. For all height comparisons, the font mapper looks for the largest font that does not exceed the requested size or the smallest font if all the fonts exceed the requested size.

nWidth

Specifies the average width (in logical units) of characters in the font. If nWidth is 0, the aspect ratio of the device will be matched against the digitization aspect ratio of the available fonts to find the closest match, which is determined by the absolute value of the difference.

nEscapement

Specifies the angle (in 0.1-degree units) between the escapement vector and the x-axis of the display surface. The escapement vector is the line through the origins of the first and last characters on a line. The angle is measured counterclockwise from the x-axis. See the lfEscapement member in the LOGFONT structure in the Platform SDK for more information.

nOrientation

Specifies the angle (in 0.1-degree units) between the baseline of a character and the x-axis. The angle is measured counterclockwise from the x-axis for coordinate systems in which the y-direction is down and clockwise from the x-axis for coordinate systems in which the y-direction is up.

nWeight

Specifies the font weight (in inked pixels per 1000). See the lfWeight member in the LOGFONT structure in the Platform SDK for more information. The described values are approximate; the actual appearance depends on the typeface. Some fonts have only FW_NORMAL, FW_REGULAR, and FW_BOLD weights. If FW_DONTCARE is specified, a default weight is used.

bItalic

Specifies whether the font is italic.

bUnderline

Specifies whether the font is underlined.

cStrikeOut

Specifies whether characters in the font are struck out. Specifies a strikeout font if set to a nonzero value.

nCharSet

Specifies the font's character setSee the lfCharSet member in the LOGFONT structure in the Platform SDK for a list of values.

The OEM character set is system-dependent.

Fonts with other character sets may exist in the system. An application that uses a font with an unknown character set must not attempt to translate or interpret strings that are to be rendered with that font. Instead, the strings should be passed directly to the output device driver.

The font mapper does not use the DEFAULT_CHARSET value. An application can use this value to allow the name and size of a font to fully describe the logical font. If a font with the specified name does not exist, a font from any character set can be substituted for the specified font. To avoid unexpected results, applications should use the DEFAULT_CHARSET value sparingly.

nOutPrecision

Specifies the desired output precision. The output precision defines how closely the output must match the requested font's height, width, character orientation, escapement, and pitch. See the lfOutPrecision member in the LOGFONT structure in the Platform SDK for a list of values and more information.

nClipPrecision

Specifies the desired clipping precision. The clipping precision defines how to clip characters that are partially outside the clipping region. See the lfClipPrecision member in the LOGFONT structure in the Platform SDK for a list of values.

To use an embedded read-only font, an application must specify CLIP_ENCAPSULATE.

To achieve consistent rotation of device, TrueType, and vector fonts, an application can use the OR operator to combine the CLIP_LH_ANGLES value with any of the other nClipPrecision values. If the CLIP_LH_ANGLES bit is set, the rotation for all fonts depends on whether the orientation of the coordinate system is left-handed or right-handed. (For more information about the orientation of coordinate systems, see the description of the nOrientation parameter.) If CLIP_LH_ANGLES is not set, device fonts always rotate counterclockwise, but the rotation of other fonts is dependent on the orientation of the coordinate system.

nQuality

Specifies the font's output quality, which defines how carefully the GDI must attempt to match the logical-font attributes to those of an actual physical font. See the lfQuality member in the LOGFONT structure in the Platform SDK for a list of values.

nPitchAndFamily

Specifies the pitch and family of the font. See the lfPitchAndFamily member in the LOGFONT structure in the Platform SDK for a list of values and more information.

lpszFacename

A CString or pointer to a null-terminated string that specifies the typeface name of the font. The length of this string must not exceed 30 characters. The Windows EnumFontFamilies function can be used to enumerate all currently available fonts. If lpszFacename is NULL, the GDI uses a device-independent typeface.

Return value

Nonzero if successful; otherwise 0.

Remarks

The font can subsequently be selected as the font for any device context.

The CreateFont function does not create a new Windows GDI font. It merely selects the closest match from the fonts available in the GDI's pool of physical fonts.

Applications can use the default settings for most of these parameters when creating a logical font. The parameters that should always be given specific values are nHeight and lpszFacename. If nHeight and lpszFacename are not set by the application, the logical font that is created is device-dependent.

When you finish with the CFont object created by the CreateFont function, first select the font out of the device context, then delete the CFont object.

Example

// The code fragment shows how to create a font object,
// select the font object into a DC (device context) for text
// drawing, and finally delete the font object.

// Initializes a CFont object with the specified characteristics. 
CFont font;
VERIFY(font.CreateFont(
   12,                        // nHeight
   0,                         // nWidth
   0,                         // nEscapement
   0,                         // nOrientation
   FW_NORMAL,                 // nWeight
   FALSE,                     // bItalic
   FALSE,                     // bUnderline
   0,                         // cStrikeOut
   ANSI_CHARSET,              // nCharSet
   OUT_DEFAULT_PRECIS,        // nOutPrecision
   CLIP_DEFAULT_PRECIS,       // nClipPrecision
   DEFAULT_QUALITY,           // nQuality
   DEFAULT_PITCH | FF_SWISS,  // nPitchAndFamily
   "Arial"));                 // lpszFacename

// Do something with the font just created...
CClientDC dc(this);  
// get old front object
CFont* def_font = dc.SelectObject(&font);
// use new font format to text out;
dc.TextOut(5, 5, "Hello", 5);
// set old font object to dc
dc.SelectObject(def_font);

// Done with the font.  Delete the font object.
font.DeleteObject(); 

 

CFont::CreatePointFont()

This function provides a simple way to create a font of a specified typeface and point size.

BOOL CreatePointFont(
   int nPointSize,
   LPCTSTR lpszFaceName,
   CDC* pDC = NULL 
);

 

Parameters

nPointSize

Requested font height in tenths of a point. (For instance, pass 120 to request a 12-point font.)

lpszFaceName

A CString or pointer to a null-terminated string that specifies the typeface name of the font. The length of this string must not exceed 30 characters. The Windows EnumFontFamilies function can be used to enumerate all currently available fonts. If lpszFaceName is NULL, the GDI uses a device-independent typeface.

pDC

Pointer to the CDC object to be used to convert the height in nPointSize to logical units. If NULL, a screen device context is used for the conversion.

Return Value

Nonzero if successful, otherwise 0.

Remarks

It automatically converts the height in nPointSize to logical units using the CDC object pointed to by pDC.

When you finish with the CFont object created by the CreatePointFont function, first select the font out of the device context, then delete the CFont object.

Examples

// The code fragment shows how to create a font object,
// select the font object into a DC (device context) for text
// drawing, and finally delete the font object.

CClientDC dc(this);

CFont font;
VERIFY(font.CreatePointFont(120, "Arial", &dc));

// Do something with the font just created...
CFont* def_font = dc.SelectObject(&font);
dc.TextOut(5, 5, "Hello", 5);
dc.SelectObject(def_font);

// Done with the font. Delete the font object.
font.DeleteObject();

 

LOGFONT

The LOGFONT structure defines the attributes of a font.

typedef struct tagLOGFONT {
LONG lfHeight;
LONG lfWidth;
LONG lfEscapement;
LONG lfOrientation;
LONG lfWeight;
BYTE lfItalic;
BYTE lfUnderline;
BYTE lfStrikeOut;
BYTE lfCharSet;
BYTE lfOutPrecision;
BYTE lfClipPrecision;
BYTE lfQuality;
BYTE lfPitchAndFamily;
TCHAR lfFaceName[LF_FACESIZE];
} LOGFONT, *PLOGFONT;

 

Members

lfHeight
Specifies the height, in logical units, of the font's character cell or character. The character height value (also known as the em height) is the character cell height value minus the internal-leading value. The font mapper interprets the value specified in lfHeight in the following manner.

character height = character cell height – internal-leading value

value meaning
>0 The font mapper transforms this value into device units and matches it against the cell height of the available fonts.
0 The font mapper uses a default height value when it searches for a match.
<0 The font mapper transforms this value into device units and matches its absolute value against the character height of the available fonts.

 

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

This mapping occurs when the font is used for the first time.

For the MM_TEXT mapping mode, you can use the following formula to specify a height for a font with a specified point size:

lfHeight = -MulDiv(PointSize, GetDeviceCaps(hDC, LOGPIXELSY), 72);

 

lfWidth

 

Specifies the average width, in logical units, of characters in the font. If lfWidth is zero, the aspect ratio of the device is matched against the digitization aspect ratio of the available fonts to find the closest match, determined by the absolute value of the difference. lfEscapementSpecifies the angle, in tenths of degrees, between the escapement vector and the x-axis of the device. The escapement vector is parallel to the base line of a row of text.

Windows NT/2000/XP: When the graphics mode is set to GM_ADVANCED, you can specify the escapement angle of the string independently of the orientation angle of the string's characters.

When the graphics mode is set to GM_COMPATIBLE, lfEscapement specifies both the escapement and orientation. You should set lfEscapement and lfOrientation to the same value.

Windows 95/98/Me: The lfEscapement member specifies both the escapement and orientation. You should set lfEscapement and lfOrientation to the same value.

 

lfOrientationSpecifies the angle, in tenths of degrees, between each character's base line and the x-axis of the device. lfWeightSpecifies the weight of the font in the range 0 through 1000. For example, 400 is normal and 700 is bold. If this value is zero, a default weight is used.

The following values are defined for convenience.

value weight
FW_DONTCARE 0
FW_THIN 100
FW_EXTRALIGHT 200
FW_ULTRALIGHT 200
FW_LIGHT 300
FW_NORMAL 400
FW_REGULAR 400
FW_MEDIUM 500
FW_SEMIBOLD 600
FW_DEMIBOLD 600
FW_BOLD 700
FW_EXTRABOLD 800
FW_ULTRABOLD 800
FW_HEAVY 900
FW_BLACK 900

 

 

lfItalic

 

Specifies an italic font if set to TRUE. lfUnderlineSpecifies an underlined font if set to TRUE. lfStrikeOutSpecifies a strikeout font if set to TRUE. lfCharSetSpecifies the character set. The following values are predefined.

ANSI_CHARSET
BALTIC_CHARSET
CHINESEBIG5_CHARSET
DEFAULT_CHARSET
EASTEUROPE_CHARSET
GB2312_CHARSET
GREEK_CHARSET
HANGUL_CHARSET
MAC_CHARSET
OEM_CHARSET
RUSSIAN_CHARSET
SHIFTJIS_CHARSET
SYMBOL_CHARSET
TURKISH_CHARSET
VIETNAMESE_CHARSET

Korean language edition of Windows:
JOHAB_CHARSET
Middle East language edition of Windows:
ARABIC_CHARSET
HEBREW_CHARSET
Thai language edition of Windows:
THAI_CHARSET

The OEM_CHARSET value specifies a character set that is operating-system dependent.

Windows 95/98/Me: You can use the DEFAULT_CHARSET value to allow the name and size of a font to fully describe the logical font. If the specified font name does not exist, a font from any character set can be substituted for the specified font, so you should use DEFAULT_CHARSET sparingly to avoid unexpected results.

Windows NT/2000/XP: DEFAULT_CHARSET is set to a value based on the current system locale. For example, when the system locale is English (United States), it is set as ANSI_CHARSET.

Fonts with other character sets may exist in the operating system. If an application uses a font with an unknown character set, it should not attempt to translate or interpret strings that are rendered with that font.

This parameter is important in the font mapping process. To ensure consistent results, specify a specific character set. If you specify a typeface name in the lfFaceName member, make sure that the lfCharSet value matches the character set of the typeface specified in lfFaceName.

 

lfOutPrecision

 

Specifies the output precision. The output precision defines how closely the output must match the requested font's height, width, character orientation, escapement, pitch, and font type. It can be one of the following values.
value meaning
OUT_CHARACTER_PRECIS Not used.
OUT_DEFAULT_PRECIS Specifies the default font mapper behavior.
OUT_DEVICE_PRECIS Instructs the font mapper to choose a Device font when the system contains multiple fonts with the same name.
OUT_OUTLINE_PRECIS Windows NT/2000/XP: This value instructs the font mapper to choose from TrueType and other outline-based fonts.
OUT_PS_ONLY_PRECIS Windows 2000/XP: Instructs the font mapper to choose from only PostScript fonts. If there are no PostScript fonts installed in the system, the font mapper returns to default behavior.
OUT_RASTER_PRECIS Instructs the font mapper to choose a raster font when the system contains multiple fonts with the same name.
OUT_STRING_PRECIS This value is not used by the font mapper, but it is returned when raster fonts are enumerated.
OUT_STROKE_PRECIS

Windows NT/2000/XP: This value is not used by the font mapper, but it is returned when TrueType, other outline-based fonts, and vector fonts are enumerated.

Windows 95:This value is used to map vector fonts, and is returned when TrueType or vector fonts are enumerated.

OUT_TT_ONLY_PRECIS Instructs the font mapper to choose from only TrueType fonts. If there are no TrueType fonts installed in the system, the font mapper returns to default behavior.
OUT_TT_PRECIS Instructs the font mapper to choose a TrueType font when the system contains multiple fonts with the same name.
   

Application can use the OUT_DEVICE_PRECIS, OUT_RASTER_PRECIS, OUT_TT_PRECIS, and OUT_PS_ONLY_PRECIS values to control how the font mapper chooses a font when the operating system contians more than one font with a specified name. For example, if an operating system contains a font names Symbol in raster and TrueType form, specifying OUT_TT_PRECIS forces the font mapper to choose the TrueType version, Specifying OUT_TT_ONLY_PRECIS forces the font mapper to choose a TrueType, even if it must substitute a TrueType font of another name.

 

lfClipPrecision

 

Specifies the clipping precision. The clipping precision defines how to clip characters that are partially outside the clipping region. It can be one or more of the following values.

For more information about the orientation of coordinate systems, see the description of the nOrientation parameter

VALUE MEANING
CLIP_CHARACTER_PRECIS Not used.
CLIP_DEFAULT_PRECIS Specifies default clipping behavior.
CLIP_DFA_DISABLE Windows XP SP1: Turns off font association for the font. Note that this flag is not guaranteed to have any effect on any platform after Windows Server 2003.
CLIPEMBEDDED You must specify this flag to use an embedded read-only font.
CLIP_LH_ANGLES

When this value is used, the rotation for all fonts depends on whether the orientation of the coordinate system is left-handed or right-handed.

If not used, device fonts always rotate counterclockwise, but the rotation of other fonts is dependent on the orientation of the coordinate system.

CLIP_MASK Not used.
CLIP_DFA_OVERRIDE Windows 2000: Turns off font association for the font. This is identical to CLIP_DFA_DISABLE, but it can have problems in some situations; the recommended flag to use is CLIP_DFA_DISABLE.
CLIP_STROKE_PRECIS

Not used by the font mapper, but is returned when raster, vector, or TrueType fonts are enumerated.

Windows NT/2000/XP: For compatibility, this value is always returned when enumerating fonts.

CLIP_TT_ALWAYS NOT USED

 

 

lfQuality

 

Specifies the output quality. The output quality defines how carefully the graphics device interface (GDI) must attempt to match the logical-font attributes to those of an actual physical font. It can be one of the following values.
VALUE MEANING
ANTIALIASED_QUALITY

Windows NT 4.0 and later: Font is always antialiased if the font supports it and the size of the font is not too small or too large.

Windows 95 Plus!, Windows 98/Me: The display must greater than 8-bit color, it must be a single plane device, it cannot be a palette display, and it cannot be in a multiple display monitor setup. In addition, you must select a TrueType font into a screen DC prior to using it in a DIBSection, otherwise antialiasing does not occur.

CLEARTYPE_QUALITY Windows XP: If set, text is rendered (when possible) using ClearType antialiasing method. See Remarks for more information.
DEFAULT_QUALITY Appearance of the font does not matter.
DRAFT_QUALITY Appearance of the font is less important than when PROOF_QUALITY is used. For GDI raster fonts, scaling is enabled, which means that more font sizes are available, but the quality may be lower. Bold, italic, underline, and strikeout fonts are synthesized if necessary.
NONANTIALIASED_QUALITY Windows 95/98/Me, Windows NT 4.0 and later: Font is never antialiased.
PROOF_QUALITY Character quality of the font is more important than exact matching of the logical-font attributes. For GDI raster fonts, scaling is disabled and the font closest in size is chosen. Although the chosen font size may not be mapped exactly when PROOF_QUALITY is used, the quality of the font is high and there is no distortion of appearance. Bold, italic, underline, and strikeout fonts are synthesized if necessary.

If neither ANTIALIASED_QUALITY nor NONANTIALIASED_QUALITY is selected, the font is antialiased only if the user chooses smooth screen fonts in Control Panel.

 

lfPitchAndFamilySpecifies the pitch and family of the font. The two low-order bits specify the pitch of the font and can be one of the following values.

DEFAULT_PITCH
FIXED_PITCH
VARIABLE_PITCH

Bits 4 through 7 of the member specify the font family and can be one of the following values.

FF_DECORATIVE
FF_DONTCARE
FF_MODERN
FF_ROMAN
FF_SCRIPT
FF_SWISS

The proper value can be obtained by using the Boolean OR operator to join one pitch constant with one family constant.

Font families describe the look of a font in a general way. They are intended for specifying fonts when the exact typeface desired is not available. The values for font families are as follows.

VALUE MEANING
FF_DECORATIVE Novelty fonts. Old English is an example.
FF_DONTCARE Use default font.
FF_MODERN Fonts with constant stroke width (monospace), with or without serifs. Monospace fonts are usually modern. Pica, Elite, and CourierNew are examples.
FF_ROMAN Fonts with variable stroke width (proportional) and with serifs. MS Serif is an example.
FF_SCRIPT Fonts designed to look like handwriting. Script and Cursive are examples.
FF_SWISS Fonts with variable stroke width (proportional) and without serifs. MS Sans Serif is an example.
lfFaceName
A null-terminated string that specifies the typeface name of the font. The length of this string must not exceed 32 characters, including the terminating null character. The EnumFontFamiliesEx function can be used to enumerate the typeface names of all currently available fonts. If lfFaceName is an empty string, GDI uses the first font that matches the other specified attributes.

Remarks

Windows XP: The following situations do not support ClearType antialiasing:

  • Text is rendered on a printer.
  • Display set for 256 colors or less.
  • Text is rendered to a terminal server client.
  • The font is not a TrueType font or an OpenType font with TrueType outlines. For example, the following do not support ClearType antialiasing: Type 1 fonts, Postscript OpenType fonts without TrueType outlines, bitmap fonts, vector fonts, and device fonts.
  • The font has tuned embedded bitmaps, for any font sizes that contain the embedded bitmaps. For example, this occurs commonly in East Asian fonts.

lfFaceName A null-terminated string that specifies the typeface name of the font. The length of this string must not exceed 32 characters, including the terminating null character. The EnumFontFamiliesEx function can be used to enumerate the typeface names of all currently available fonts. If lfFaceName is an empty string, GDI uses the first font that matches the other specified attributes.

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章