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
| DATA | Type | Description |
|---|---|---|
hFont | Numeric (Handle) | Windows GDI font handle. Created by New(), used when activating onto a DC. |
cFaceName | Character | Typeface name (e.g. "Arial", "Segoe UI", "Courier New"). |
nInpHeight | Numeric | Nominal font height in logical units. Negative values indicate character height (em-height); positive values indicate cell height. |
nInpWidth | Numeric | Average character width. Zero lets Windows choose a proportional width. |
nWeight | Numeric | Font weight: 0=default, 400=normal, 700=bold. Set automatically when lBold is .T.. |
nEscapement | Numeric | Angle of the base text line in tenths of degrees (0 = horizontal, 900 = vertical upward). |
nOrientation | Numeric | Angle of each character in tenths of degrees. |
nCharSet | Numeric | Character set identifier: 0=ANSI, 1=DEFAULT, 204=RUSSIAN, 238=EASTEUROPE, 255=OEM. |
lBold | Logical | If .T., the font is created with bold weight (700). |
lItalic | Logical | If .T., the font is created with italic style. |
lUnderline | Logical | If .T., the font is created with underline. |
lStrikeOut | Logical | If .T., the font is created with strikeout. |
lSubstitute | Logical | If .T., the font name is substituted through the font substitution table (FWFontSubst()). |
lQuality | Logical | If .T., anti-aliased (CLEARTYPE) quality is requested when available. |
Cargo | Any | General-purpose user data slot. |
Methods
| Method | Description |
|---|---|
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
- TFont is a standalone class — it does not inherit from TWindow or TControl.
- Every TFont object must be paired with an
End()call. GDI font handles are a limited system resource; failure to release them causes visible degradation in all running applications. - The
Activate()/DeActivate()pair should bracket any custom drawing that uses the font. Never leave a font selected into a DC after drawing is complete. - Use the
Choose()method to let users pick fonts interactively. The font dialog shows all installed fonts on the system. - Font substitution (controlled by
lSubstitute) allows mapping unavailable font names to alternatives through theFWFontSubst()table. - To use a font with a control, assign it via the
oFontDATA (e.g.oSay:oFont := oFont) rather than manually activating on a DC. - For high-DPI awareness, specify font sizes in points using negative height values (e.g.
-14for 10-point text).