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 defualt 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;
/* Store all created fonts for reuse.
ATTENTION: This way a font will never get freed! */
static NSMutableDictionary* globalFontDictionary = nil;
static NSUserDefaults *defaults = nil;
NSFont*
getNSFont(NSString* key, NSString* defaultFontName, float fontSize)
{
NSString* fontName;
fontName = [defaults objectForKey: key];
if (fontName == nil)
fontName = defaultFontName;
if (fontSize == 0)
{
fontSize = [defaults floatForKey:
[NSString stringWithFormat: @"%@Size", key]];
}
return [NSFontClass fontWithName: fontName size: fontSize];
}
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;
globalFontDictionary = [NSMutableDictionary new];
if (defaults == nil)
{
defaults = RETAIN([NSUserDefaults standardUserDefaults]);
}
}
}
/* Getting the preferred user fonts. */
// This is deprecated in MacOSX
+ (NSFont*) boldSystemFontOfSize: (float)fontSize
{
static NSFont *font = nil;
if (fontSize != 0)
{
return getNSFont (@"NSBoldFont", @"Helvetica-Bold", fontSize);
}
else
{
if ((font == nil) || (boldSystemCacheNeedsRecomputing == YES))
{
ASSIGN (font, getNSFont (@"NSBoldFont", @"Helvetica-Bold", 0));
boldSystemCacheNeedsRecomputing = NO;
}
return font;
}
}
// This is deprecated in MacOSX
+ (NSFont*) systemFontOfSize: (float)fontSize
{
static NSFont *font = nil;
if (fontSize != 0)
{
return getNSFont (@"NSFont", @"Helvetica", fontSize);
}
else
{
if ((font == nil) || (systemCacheNeedsRecomputing == YES))
{
ASSIGN (font, getNSFont (@"NSFont", @"Helvetica", 0));
systemCacheNeedsRecomputing = NO;
}
return font;
}
}
+ (NSFont*) userFixedPitchFontOfSize: (float)fontSize
{
static NSFont *font = nil;
if (fontSize != 0)
{
return getNSFont (@"NSUserFixedPitchFont", @"Courier", fontSize);
}
else
{
if ((font == nil) || (userFixedCacheNeedsRecomputing == YES))
{
ASSIGN (font, getNSFont (@"NSUserFixedPitchFont", @"Courier", 0));
userFixedCacheNeedsRecomputing = NO;
}
return font;
}
}
+ (NSFont*) userFontOfSize: (float)fontSize
{
static NSFont *font = nil;
if (fontSize != 0)
{
return getNSFont (@"NSUserFont", @"Helvetica", fontSize);
}
else
{
if ((font == nil) || (userCacheNeedsRecomputing == YES))
{
ASSIGN (font, getNSFont (@"NSUserFont", @"Helvetica", 0));
userCacheNeedsRecomputing = NO;
}
return font;
}
}
+ (NSArray *)preferredFontNames
{
return _preferredFonts;
}
/* Setting the preferred user fonts*/
+ (void) setUserFixedPitchFont: (NSFont*)aFont
{
setNSFont (@"NSUserFixedPitchFont", aFont);
}
+ (void) setUserFont: (NSFont*)aFont
{
setNSFont (@"NSUserFont", aFont);
}
+ (void)setPreferredFontNames:(NSArray *)fontNames
{
ASSIGN(_preferredFonts, fontNames);
}
/* Getting various fonts*/
+ (NSFont*) controlContentFontOfSize: (float)fontSize
{
static NSFont *font = nil;
if (fontSize != 0)
{
return getNSFont (@"NSControlContentFont", @"Helvetica", fontSize);
}
else
{
if ((font == nil) || (userCacheNeedsRecomputing == YES))
{
ASSIGN (font, getNSFont (@"NSControlContentFont", @"Helvetica", 0));
userCacheNeedsRecomputing = NO;
}
return font;
}
}
+ (NSFont*) labelFontOfSize: (float)fontSize
{
static NSFont *font = nil;
if (fontSize != 0)
{
return getNSFont (@"NSLabelFont", @"Helvetica", fontSize);
}
else
{
if ((font == nil) || (userCacheNeedsRecomputing == YES))
{
ASSIGN (font, getNSFont (@"NSLabelFont", @"Helvetica", 0));
userCacheNeedsRecomputing = NO;
}
return font;
}
}
+ (NSFont*) menuFontOfSize: (float)fontSize
{
static NSFont *font = nil;
if (fontSize != 0)
{
return getNSFont (@"NSMenuFont", @"Helvetica", fontSize);
}
else
{
if ((font == nil) || (userCacheNeedsRecomputing == YES))
{
ASSIGN (font, getNSFont (@"NSMenuFont", @"Helvetica", 0));
userCacheNeedsRecomputing = NO;
}
return font;
}
}
+ (NSFont*) titleBarFontOfSize: (float)fontSize
{
static NSFont *font = nil;
if (fontSize != 0)
{
return getNSFont (@"NSTitleBarFont", @"Helvetica-Bold", fontSize);
}
else
{
if ((font == nil) || (boldSystemCacheNeedsRecomputing == YES))
{
ASSIGN (font, getNSFont (@"NSTitleBarFont", @"Helvetica-Bold", 0));
boldSystemCacheNeedsRecomputing = NO;
}
return font;
}
}
+ (NSFont*) messageFontOfSize: (float)fontSize
{
static NSFont *font = nil;
if (fontSize != 0)
{
return getNSFont (@"NSMessageFont", @"Helvetica", fontSize);
}
else
{
if ((font == nil) || (userCacheNeedsRecomputing == YES))
{
ASSIGN (font, getNSFont (@"NSMessageFont", @"Helvetica", 0));
userCacheNeedsRecomputing = NO;
}
return font;
}
}
+ (NSFont*) paletteFontOfSize: (float)fontSize
{
// Not sure on this one.
static NSFont *font = nil;
if (fontSize != 0)
{
return getNSFont (@"NSPaletteFont", @"Helvetica-Bold", fontSize);
}
else
{
if ((font == nil) || (boldSystemCacheNeedsRecomputing == YES))
{
ASSIGN (font, getNSFont (@"NSPaletteFont", @"Helvetica-Bold", 0));
boldSystemCacheNeedsRecomputing = NO;
}
return font;
}
}
+ (NSFont*) toolTipsFontOfSize: (float)fontSize
{
// Not sure on this one.
static NSFont *font = nil;
if (fontSize != 0)
{
return getNSFont (@"NSToolTipsFont", @"Helvetica", fontSize);
}
else
{
if ((font == nil) || (userCacheNeedsRecomputing == YES))
{
ASSIGN (font, getNSFont (@"NSToolTipsFont", @"Helvetica", 0));
userCacheNeedsRecomputing = NO;
}
return font;
}
}
//
// Font Sizes
//
+ (float) labelFontSize
{
float fontSize = [defaults floatForKey: @"NSLabelFontSize"];
if (fontSize == 0)
{
fontSize = 12;
}
return fontSize;
}
+ (float) smallSystemFontSize
{
float fontSize = [defaults floatForKey: @"NSSmallFontSize"];
if (fontSize == 0)
{
fontSize = 9;
}
return fontSize;
}
+ (float) systemFontSize
{
float fontSize = [defaults floatForKey: @"NSFontSize"];
if (fontSize == 0)
{
fontSize = 12;
}
return fontSize;
}
/** Creates a new font with name aFontName and matrix fontMatrix. The
fontMatrix is a standard size element matrix as used in PostScript
to describe the scaling of the font, typically it just includes
the font size as [fontSize 0 0 fontSize 0 0]. You can use the constant
NSFontIdentityMatrix in place of [1 0 0 1 0 0]. If NSFontIdentityMatrix,
then the font will automatically flip itself when set in a
flipped view */
+ (NSFont*) fontWithName: (NSString*)aFontName
matrix: (const float*)fontMatrix
{
NSFont *font;
NSString *nameWithMatrix;
nameWithMatrix = [NSString stringWithFormat:
@"%@ %.3f %.3f %.3f %.3f %.3f %.3f",
aFontName,
fontMatrix[0], fontMatrix[1], fontMatrix[2],
fontMatrix[3], fontMatrix[4], fontMatrix[5]];
/* Check whether the font is cached */
font = [globalFontDictionary objectForKey: nameWithMatrix];
if(font == nil)
{
font = AUTORELEASE([[NSFontClass alloc] initWithName: aFontName
matrix: fontMatrix]);
/* Cache the font for later use */
[globalFontDictionary setObject: font forKey: nameWithMatrix];
}
return font;
}
/** Creates a new font with name aFontName and size fontSize. Fonts created
using this method will automatically flip themselves when set in a flipped
view */
+ (NSFont*) fontWithName: (NSString*)aFontName
size: (float)fontSize
{
NSFont*font;
float fontMatrix[6] = { 0, 0, 0, 0, 0, 0 };
if (fontSize == 0)
{
fontSize = [defaults floatForKey: @"NSUserFontSize"];
if (fontSize == 0)
{
fontSize = 12;
}
}
fontMatrix[0] = fontSize;
fontMatrix[3] = fontSize;
font = [self fontWithName: aFontName matrix: fontMatrix];
font->matrixExplicitlySet = NO;
return font;
}
+ (void) useFont: (NSString*)aFontName
{
[GSCurrentContext() useFont: aFontName];
}
//
// Instance methods
//
/**