NSFont
The NSFont class allows control of the fonts used for displaying
text anywhere on the screen. The primary methods for getting a
particular font are -fontWithName:matrix: and -fontWithName:size: which
take the name and size of a particular font and return the NSFont object
associated with that font. In addition there are several convenience
mathods which make it easier to get certain types of fonts.
In particular, there are several methods to get the standard fonts
used by the Application to display text for a partiuclar purpose. See
the class methods listed below for more information. These default
fonts can be set using the user defaults system. The default
font names available are:
- NSBoldFont Helvetica-Bold
- NSControlContentFont Helvetica
- NSFont Helvetica (System Font)
- NSLabelFont Helvetica
- NSMenuFont Helvetica
- NSMessageFont Helvetica
- NSPaletteFont Helvetica-Bold
- NSTitleBarFont Helvetica-Bold
- NSToolTipsFont Helvetica
- NSUserFixedPitchFont Courier
- NSUserFont Helvetica
The default sizes are:
- NSBoldFontSize (none)
- NSControlContentFontSize (none)
- NSFontSize 12 (System Font Size)
- NSLabelFontSize 12
- NSMenuFontSize (none)
- NSMessageFontSize (none)
- NSPaletteFontSize (none)
- NSSmallFontSize 9
- NSTitleBarFontSize (none)
- NSToolTipsFontSize (none)
- NSUserFixedPitchFontSize (none)
- NSUserFontSize (none)
Font sizes list with (none) default to NSFontSize.
*/
@implementation NSFont
/* Class variables*/
/* Fonts that are preferred by the application */
NSArray *_preferredFonts;
/* Class for fonts */
static Class NSFontClass = 0;
/* Cache all created fonts for reuse. */
static NSMapTable* globalFontMap = 0;
static NSUserDefaults *defaults = nil;
NSFont*
getNSFont(NSString* key, NSString* defaultFontName, float fontSize)
{
NSString *fontName;
NSFont *font;
fontName = [defaults objectForKey: key];
if (fontName == nil)
{
fontName = defaultFontName;
}
if (fontSize == 0)
{
fontSize = [defaults floatForKey:
[NSString stringWithFormat: @"%@Size", key]];
}
font = [NSFontClass fontWithName: fontName size: fontSize];
/* That font couldn't be found (?). */
if (font == nil)
{
/* Try the same size, but the defaultFontName. */
font = [NSFontClass fontWithName: defaultFontName size: fontSize];
if (font == nil)
{
/* Try the default font name and size. */
fontSize = [defaults floatForKey:
[NSString stringWithFormat: @"%@Size", key]];
font = [NSFontClass fontWithName: defaultFontName size: fontSize];
/* It seems we can't get any font here! Try some well known
* fonts as a last resort. */
if (font == nil)
{
font = [NSFontClass fontWithName: @"Helvetica" size: 12.];
}
if (font == nil)
{
font = [NSFontClass fontWithName: @"Courier" size: 12.];
}
if (font == nil)
{
font = [NSFontClass fontWithName: @"Fixed" size: 12.];
}
}
}
return font;
}
void
setNSFont(NSString* key, NSFont* font)
{
[defaults setObject: [font fontName] forKey: key];
systemCacheNeedsRecomputing = YES;
boldSystemCacheNeedsRecomputing = YES;
userCacheNeedsRecomputing = YES;
userFixedCacheNeedsRecomputing = YES;
/* Don't care about errors */
[defaults synchronize];
}
//
// Class methods
//
+ (void) initialize
{
if (self == [NSFont class])
{
NSFontClass = self;
/*
* The placeHolder is a dummy NSFont instance which is never used
* as a font ... the initialiser knows that whenever it gets the
* placeHolder it should either return a cached font or return a
* newly allocated font to replace it. This mechanism stops the
* +fontWithName:... methods from having to allocete fonts instances
* which would immediately have to be released for replacement by
* a cache object.
*/
placeHolder = [self alloc];
globalFontMap = NSCreateMapTable(NSObjectMapKeyCallBacks,
NSNonRetainedObjectMapValueCallBacks, 64);
if (defaults == nil)
{
defaults = RETAIN([NSUserDefaults standardUserDefaults]);
}
[self setVersion: currentVersion];
}
}
/* Getting the preferred user fonts. */
/**
* Return the default bold font for use in menus and heading in standard
* gui components.