Jalaj

May 16, 2007

WinAPI : The Font Object

Filed under: GDI32, Visual Basic, WinAPI — Jalaj @ 12:00 pm

In the previous posts concerning displaying text on Device Contexts (WinAPI : Displaying Text and WinAPI : DrawText & DrawTextEx), we saw nothing of how to write text in a particular font. It’s what we will discuss here.

Each Device Context is associated with a Font object, and for all the text displayed on the Device Context this Font object is used. The font object also carries with it other details as Size, Weight, Underline, Italic Bold, Strikeout etc. So you need to create a Logical Font object using a font from those installed on the system and use SelectObject function to associate it with the Device Context. When you no longer need the object you can remove it from the memory using DeleteObject function. These two functions were already described in previous post WinAPI : The CreatePen Function.

Now to create a Logical Font object you can use CreateFont function defined as below. The function returns the handle to the logical font object which you can pass to SelectObject to associate it with the Device Context.

Public Declare Function CreateFont Lib "gdi32" Alias "CreateFontA" ( _
	ByVal lfHeight As Long, ByVal lfWidth As Long, ByVal lfEscapement As Long, _
	ByVal lfOrientation As Long, ByVal lfWeight As Long, ByVal lfItalic As Long, _
	ByVal lfUnderline As Long, ByVal lfStrikeOut As Long, ByVal lfCharSet As Long, _
	ByVal lfOutPrecision As Long, ByVal lfClipPrecision As Long, _
	ByVal lfQuality As Long, ByVal lfPitchAndFamily As Long, _
	ByVal lfFaceName As String) As Long

lfHeight is the height of the Font’s Character in logical units. If the value passed is zero then the default height is considered, otherwise the absolute value of the parameter passed is considered.

lfWidth is the average width of the characters in the specified font, in logical units.

lfEscapement Specifies the angle, in tenths of degrees, between the escapement vector and the x-axis of the device. Specifying an Escapement vector other than 0 makes your string to rotate through the angle specified.

lfOrientaion Specifies the angle, in tenths of degrees, between each character’s base line and the x-axis of the device. Specifying this parameter to a value other than 0 causes each character to rotate through the angle specified about its baseline. By default this parameter is ignored and the value as defined in the lfEscapement parameter is used. To make system use this parameter separately you need to change graphics mode to GM_ADVANCED (details at the bottom of this post.

lfWeight is the weight of the font in range 0 - 1000 or a constant as defined below.

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

lfItalic specifies an Italic font if set to TRUE ( 1 )

lfUnderline specifies an Underline font if set to TRUE ( 1 )

lfStrikeOut specifies an Strikeout font if set to TRUE ( 1 )

lfCharSet specifies the Character Set and can take value one of the constants defined below.

Public Const ANSI_CHARSET = 0
Public Const DEFAULT_CHARSET = 1
Public Const SYMBOL_CHARSET = 2
Public Const MAC_CHARSET = 77
Public Const SHIFTJIS_CHARSET = 128
Public Const HANGUL_CHARSET = 129
Public Const JOHAB_CHARSET = 130
Public Const GB2312_CHARSET = 134
Public Const CHINESEBIG5_CHARSET = 136
Public Const GREEK_CHARSET = 161
Public Const TURKISH_CHARSET = 162
Public Const VIETNAMESE_CHARSET = 163
Public Const HEBREW_CHARSET = 177
Public Const ARABIC_CHARSET = 178
Public Const BALTIC_CHARSET = 186
Public Const RUSSIAN_CHARSET = 204
Public Const THAI_CHARSET = 222
Public Const EASTEUROPE_CHARSET = 238
Public Const OEM_CHARSET = 255

lfOutPrecision specifies the output precision, which 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 constants as defined below.

Public Const OUT_DEFAULT_PRECIS = 0
Public Const OUT_STRING_PRECIS = 1
Public Const OUT_CHARACTER_PRECIS = 2
Public Const OUT_STROKE_PRECIS = 3
Public Const OUT_TT_PRECIS = 4
Public Const OUT_DEVICE_PRECIS = 5
Public Const OUT_RASTER_PRECIS = 6
Public Const OUT_TT_ONLY_PRECIS = 7
Public Const OUT_OUTLINE_PRECIS = 8
Public Const OUT_PS_ONLY_PRECIS = 10

lfClipPrecision Specifies the clipping precision, which defines how to clip characters that are partially outside the clipping region. It can be one of the constants as defined below.

Public Const CLIP_DEFAULT_PRECIS = 0
Public Const CLIP_CHARACTER_PRECIS = 1
Public Const CLIP_STROKE_PRECIS = 2
Public Const CLIP_MASK = 15
Public Const CLIP_LH_ANGLES = 16
Public Const CLIP_TT_ALWAYS = 32
Public Const CLIP_DFA_DISABLE = 48
Public Const CLIP_DFA_OVERRIDE = 64
Public Const CLIP_EMBEDDED = 128
Public Const CLIP_TO_PATH = 4097

lfQuality specifies the output quality, which defines how carefully GDI must attempt to match the logical-font attributes to those of an actual physical font. It can be one of the constants as defined below.

Public Const DEFAULT_QUALITY = 0
Public Const DRAFT_QUALITY = 1
Public Const PROOF_QUALITY = 2
Public Const NONANTIALIASED_QUALITY = 3
Public Const ANTIALIASED_QUALITY = 4
Public Const CLEARTYPE_QUALITY = 5

lfPitchAndFamily specifies the pitch and family of the font. It will take a value each for low order byte and high order byte from the constants given below.

' For low order bytes
Public Const DEFAULT_PITCH = 0
Public Const FIXED_PITCH = 1
Public Const VARIABLE_PITCH = 2

' For high order bytes
Public Const FF_DONTCARE = 0    ' Don't care or don't know.
Public Const FF_ROMAN = 16      ' Variable stroke width, serifed.
Public Const FF_SWISS = 32      ' Variable stroke width, sans-serifed.
Public Const FF_MODERN = 48     ' Constant stroke width, serifed or sans-serifed.
Public Const FF_SCRIPT = 64     ' Cursive, etc.
Public Const FF_DECORATIVE = 80 ' Old English, etc.

lfFaceName specifies the typeface name for the Font. This string should not exceed 32 characters.

While the above function takes a large number of parameters, a similar function CreateFontIndirect takes a single parameter, a pointer to a LOGFONT structure and creates a logical Font object as CreateFont does. The CreateFontIndirect function and the LOGFONT structure is defined below. The LOGFONT structure contains all the parameters that the CreateFont function required, with a difference that must be noted, that the face name is not a string but a Array of 32 bytes.

Public Declare Function CreateFontIndirect Lib "gdi32" Alias "CreateFontIndirectA" (lpLogFont As LOGFONT) As Long

Public Const LF_FACESIZE = 32

Public Type LOGFONT
        lfHeight As Long
        lfWidth As Long
        lfEscapement As Long
        lfOrientation As Long
        lfWeight As Long
        lfItalic As Byte
        lfUnderline As Byte
        lfStrikeOut As Byte
        lfCharSet As Byte
        lfOutPrecision As Byte
        lfClipPrecision As Byte
        lfQuality As Byte
        lfPitchAndFamily As Byte
        lfFaceName(1 To LF_FACESIZE) As Byte
End Type

You can either initialize each parameter or get the parameter from the logical Font object currently associated with the device context using GetCurrentObject function already discussed in a previous post Modifying Existing Pen in DC. And after doing so you can modify a few parameters as required.

Now let’s go for changing the Graphics mode, which I pointed at description of lfOrientation parameter. By default the graphics mode is set to GM_COMPATIBLE where the lfOrientation parameter is ignored. There, the Orientation is considered same as the Escapement. The first three strings in the figure below show how Escapement works in GM_COMPATIBLE mode, escapement being -900, 450, 900 respectively for the three strings (divide values by 10 to read in terms of Degrees). You can see how each of the characters in the string also turn in same direction, without being told to do so. This behavior can be changed using SetGraphicsMode function.

escapement.jpg

SetGraphicsMode function takes for the first parameter the handle to the Device Context. The second parameter that it takes is the Graphics mode that you need to change to as defined by constants below.

Private Declare Function SetGraphicsMode Lib "gdi32" _
   (ByVal hDC As Long, ByVal iMode As Long) As Long

Public Const GM_COMPATIBLE = 1
Public Const GM_ADVANCED = 2

The fourth string in the figure above is drawn by passing -900 (-90 degrees) for escapement making the string to be displayed from top to bottom in the same way as with the first string, with a difference that since 0 is passed for Orientation each of the character is displayed upright. Hence in this mode you can make the string and each of the characters to rotate differently.

3 Comments »

  1. [...] : Enumerating Installed Fonts Now after the last post WinAPI : The Font Object, let’s see what all fonts are installed on your system. We would do this with the help of [...]

    Pingback by WinAPI : Enumerating Installed Fonts « Jalaj — May 23, 2007 @ 2:11 pm

  2. [...] : More on Font Object In a post posted earlier namely WinAPI : The Font Object, we saw how we can use CreateFont and CreateFontIndirect functions to use fonts installed in the [...]

    Pingback by WinAPI : More on Font Object « Jalaj — June 4, 2007 @ 9:50 am

  3. [...] WinAPI functions saw a slowdown in terms of posts on this blog and subsequent two post were WinAPI : DrawText & DrawTextEx and WinAPI : The Font Object [...]

    Pingback by The Blog Revisited - 7 « Jalaj — November 16, 2007 @ 4:47 am

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.