TFont

Source: source/classes/font.prg

Standalone class (no inheritance)

TFont manages Windows GDI font objects. It encapsulates the creation, selection, and destruction of logical fonts, providing a high-level interface for typeface name, size, weight, style (bold, italic, underline, strikeout), character set, escapement, and orientation. TFont is used throughout the FiveWin framework by every control and window that renders text. Fonts must be explicitly released with End() when no longer needed to avoid GDI resource leaks.

Key DATA Members

DATATypeDescription
hFontNumeric (Handle)Windows GDI font handle. Created by New(), used when activating onto a DC.
cFaceNameCharacterTypeface name (e.g. "Arial", "Segoe UI", "Courier New").
nInpHeightNumericNominal font height in logical units. Negative values indicate character height (em-height); positive values indicate cell height.
nInpWidthNumericAverage character width. Zero lets Windows choose a proportional width.
nWeightNumericFont weight: 0=default, 400=normal, 700=bold. Set automatically when lBold is .T..
nEscapementNumericAngle of the base text line in tenths of degrees (0 = horizontal, 900 = vertical upward).
nOrientationNumericAngle of each character in tenths of degrees.
nCharSetNumericCharacter set identifier: 0=ANSI, 1=DEFAULT, 204=RUSSIAN, 238=EASTEUROPE, 255=OEM.
lBoldLogicalIf .T., the font is created with bold weight (700).
lItalicLogicalIf .T., the font is created with italic style.
lUnderlineLogicalIf .T., the font is created with underline.
lStrikeOutLogicalIf .T., the font is created with strikeout.
lSubstituteLogicalIf .T., the font name is substituted through the font substitution table (FWFontSubst()).
lQualityLogicalIf .T., anti-aliased (CLEARTYPE) quality is requested when available.
CargoAnyGeneral-purpose user data slot.

Methods

MethodDescription
New( cFace, nW, nH, lBold, lItalic, lUnder, lStrike, nEscap, nOrient, nWeight, nCharSet, lQuality, lSubst )Create a new font object. Negative nH specifies character height; positive specifies cell height. nW of 0 selects proportional width. The font handle hFont is created immediately.
End()Release the GDI font handle (DeleteObject( ::hFont )) and free the object. Must be called when the font is no longer needed.
Activate( hDC )Select the font into a device context. Returns the previous font handle. Use DeActivate() to restore.
DeActivate( hDC, hOldFont )Restore the previous font handle into the device context. Call after Activate() to clean up.
Modify( nH, lBold, lItalic, lUnder, lStrike, cFace, nW, nEscap, nOrient, nWeight, nCharSet, lQuality )Change font attributes and recreate the GDI font handle. The old handle is destroyed.
Choose( nRGB )Open the standard Windows font selection dialog. The optional nRGB parameter presets the color. Returns the chosen RGB color or nil if cancelled. The font object is updated to the selection.
Clone()Create an independent copy of the font with a new GDI handle. Useful when the same logical font is needed by different owners.
nSize()Return the font height as a negative value (character height convention).
Rotate( nDeg )Create a rotated version of the font. nDeg is the rotation angle in degrees (not tenths). Sets both escapement and orientation.
Release()Alias for End(). Releases the GDI font handle.
lChanged()Returns .T. if the font attributes have been modified since creation.

Commands: DEFINE FONT

FiveWin provides a preprocessor command for convenient font creation:

DEFINE FONT oFont ;
   NAME cFaceName ;
   SIZE nWidth, nHeight ;
   [ BOLD ] [ ITALIC ] [ UNDERLINE ] [ STRIKEOUT ] ;
   [ CHARSET nCharSet ] ;
   [ ESCAPEMENT nDeg ] ;
   [ ORIENTATION nDeg ] ;
   [ WEIGHT nWeight ] ;
   [ QUALITY ]

The SIZE clause takes width and height. Using a negative height (e.g. 0, -14) specifies character height in points; positive specifies cell height in logical units. Width of 0 lets Windows choose proportionally.

Example: Create Font and Use on a Device Context

#include "FiveWin.ch"

function Main()

   local oWnd, oFont, hOldFont, hDC

   DEFINE WINDOW oWnd TITLE "TFont Demo" SIZE 500, 300

   // Create a bold italic font
   DEFINE FONT oFont NAME "Arial" SIZE 0, -24 BOLD ITALIC

   ACTIVATE WINDOW oWnd CENTERED ;
      ON PAINT ( hDC := oWnd:GetDC(), ;
                 hOldFont := oFont:Activate( hDC ), ;
                 SayText( hDC, "Hello FiveWin!", 50, 50,, oFont:nInpHeight ), ;
                 oFont:DeActivate( hDC, hOldFont ), ;
                 oWnd:ReleaseDC( hDC ) )

   oFont:End()

return nil

Notes

See Also