Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.
GETFONT()
This function invokes FoxPro’s Font dialog and returns information about the chosen font.
cFontInfo = GETFONT( [ cFontName [, nFontSize
[, cFontStyle [, nCharSet ] ] ] ] )
The developers of FoxPro faced a dilemma of sorts with this function. There are as many as four pieces of information to return about a font—its name, its size, its style, and its underlying character set—but a function can return only one item. They couldn’t just let you pass a parameter telling which one you want, because the value returned depends on user input. So they did the best they could, and the function returns a single, comma-delimited string containing three or four pieces of information.
GETFONT()
offers the user a choice of all fonts installed on the machine, but does not display printer fonts (fonts installed only on the printer, also called “soft fonts”).
If the user chooses Cancel, an empty string is returned; you can test for this case with EMPTY()
.
In VFP 6 and later, you can prime the pump. Passing one or more of the parameters sets up the dialog with the items you pass initially highlighted. Very handy if you want to use “Beesknees ITC” (a font we really have installed, though we’re not sure why), but aren’t sure whether it’s TrueType and, if not, what sizes and styles it allows. If you specify a font that doesn’t exist, MS Sans Serif (one of our least favorite fonts of all time) is highlighted. If you specify a font, but no size, 10 point is highlighted. If you don’t specify a style, Regular is highlighted.
VFP 7 adds the nCharSet parameter, which lets you and the user choose a character set as well as a font. Passing a value for this parameter enables the Script dropdown in the dialog. The user’s choices in the dropdown vary with the currently selected font. Be aware that, when opened, the dropdown shows two values, even when more are available. If you omit the nCharSet parameter, no value is returned for character set—that’s the Fox team’s way of ensuring that old code doesn’t break.
GETFONT() can't handle the empty string for the font name. It coughs up an error message. This is particularly strange, since it can deal with empty values for the other three parameters and stupid values for the font name. |
Since every command we can think of that needs font information wants the items separately, a routine to parse GETFONT()
’s return value seems useful. That’s the example below. The procedure calls GETFONT()
, then sets its parameters to the four components of the return value.
* ParsFont.PRG
* VFP 7 version. See previous editions for a version of
* this routine that works in VFP 6 and earlier.
* Call GETFONT() and parse the return value into its four
* components: name, size, style, charset
* Sample Call:
* DO ParsFont WITH cFontName, nFontSize, cFontStyle, nCharSet
* or:
* ParsFont(@cFontName, @nFontSize, @cFontStyle, @nCharSet)
LPARAMETERS cFontName, nFontSize, cFontStyle, nCharSet
LOCAL cFontInfo, aFontInfo[1], nLines
* Call varies depending on parameters passed
DO CASE
CASE EMPTY(cFontName) OR TYPE("cFontName")<>"C"
cFontInfo=GETFONT()
CASE TYPE("nFontSize")<>"N" OR nFontSize = 0
cFontInfo = GETFONT(cFontName)
CASE EMPTY(cFontStyle) OR TYPE("cFontStyle")<>"C"
cFontInfo = GETFONT(cFontName, nFontSize)
CASE TYPE("nCharSet") <> "N" OR nCharSet = 0
cFontInfo = GETFONT(cFontName, nFontSize, cFontStyle)
OTHERWISE
cFontInfo = GETFONT(cFontName, nFontSize, cFontStyle, ;
nCharSet)
ENDCASE
IF NOT EMPTY(cFontInfo)
nLines = ALINES( aFontInfo, cFontInfo, .T., ",")
cFontName = aFontInfo[ 1 ]
nFontSize = INT(VAL( aFontInfo[ 2 ] ))
cFontStyle = aFontInfo[ 3 ]
IF nLines = 4
nCharSet = INT(VAL( aFontInfo [ 4 ] ))
ENDIF
ENDIF
RETURN
Be aware, too, that the Windows Common Dialogs control offers an alternative to this function.