00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 var _CPFonts = {};
00024 _CPFontSystemFontFace = @"Arial";
00025
00026 #define _CPCachedFont(aName, aSize, isBold) _CPFonts[(isBold ? @"bold " : @"") + ROUND(aSize) + @"px '" + aName + @"'"]
00027
00032 @implementation CPFont : CPObject
00033 {
00034 CPString _name;
00035 float _size;
00036 BOOL _isBold;
00037
00038 CPString _cssString;
00039 }
00040
00047 + (CPFont)fontWithName:(CPString)aName size:(float)aSize
00048 {
00049 return _CPCachedFont(aName, aSize, NO) || [[CPFont alloc] _initWithName:aName size:aSize bold:NO];
00050 }
00051
00058 + (CPFont)boldFontWithName:(CPString)aName size:(float)aSize
00059 {
00060 return _CPCachedFont(aName, aSize, YES) || [[CPFont alloc] _initWithName:aName size:aSize bold:YES];
00061 }
00062
00068 + (CPFont)systemFontOfSize:(CPSize)aSize
00069 {
00070 return _CPCachedFont(_CPFontSystemFontFace, aSize, NO) || [[CPFont alloc] _initWithName:_CPFontSystemFontFace size:aSize bold:NO];
00071 }
00072
00078 + (CPFont)boldSystemFontOfSize:(CPSize)aSize
00079 {
00080 return _CPCachedFont(_CPFontSystemFontFace, aSize, YES) || [[CPFont alloc] _initWithName:_CPFontSystemFontFace size:aSize bold:YES];
00081 }
00082
00083
00084
00085
00086 - (id)_initWithName:(CPString)aName size:(float)aSize bold:(BOOL)isBold
00087 {
00088 self = [super init];
00089
00090 if (self)
00091 {
00092 _name = aName;
00093 _size = aSize;
00094 _isBold = isBold;
00095
00096 _cssString = (_isBold ? @"bold " : @"") + ROUND(aSize) + @"px '" + aName + @"'";
00097
00098 _CPFonts[_cssString] = self;
00099 }
00100
00101 return self;
00102 }
00103
00107 - (float)size
00108 {
00109 return _size;
00110 }
00111
00115 - (CPString)cssString
00116 {
00117 return _cssString;
00118 }
00119
00123 - (CPString)familyName
00124 {
00125 return _name;
00126 }
00127
00128 @end
00129
00130 var CPFontNameKey = @"CPFontNameKey",
00131 CPFontSizeKey = @"CPFontSizeKey",
00132 CPFontIsBoldKey = @"CPFontIsBoldKey";
00133
00134 @implementation CPFont (CPCoding)
00135
00141 - (id)initWithCoder:(CPCoder)aCoder
00142 {
00143 return [self _initWithName:[aCoder decodeObjectForKey:CPFontNameKey]
00144 size:[aCoder decodeFloatForKey:CPFontSizeKey]
00145 bold:[aCoder decodeBoolForKey:CPFontIsBoldKey]];
00146 }
00147
00152 - (void)encodeWithCoder:(CPCoder)aCoder
00153 {
00154 [aCoder encodeObject:_name forKey:CPFontNameKey];
00155 [aCoder encodeFloat:_size forKey:CPFontSizeKey];
00156 [aCoder encodeBool:_isBold forKey:CPFontIsBoldKey];
00157 }
00158
00159 @end