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, sans-serif",
00025 _CPWrapRegExp = new RegExp("\\s*,\\s*", "g");
00026
00027
00028 #define _CPCreateCSSString(aName, aSize, isBold) (isBold ? @"bold " : @"") + ROUND(aSize) + @"px " + ((aName === _CPFontSystemFontFace) ? aName : (@"\"" + aName.replace(_CPWrapRegExp, '", "') + @"\", " + _CPFontSystemFontFace))
00029 #define _CPCachedFont(aName, aSize, isBold) _CPFonts[_CPCreateCSSString(aName, aSize, isBold)]
00030
00037 @implementation CPFont : CPObject
00038 {
00039 CPString _name;
00040 float _size;
00041 BOOL _isBold;
00042
00043 CPString _cssString;
00044 }
00045
00052 + (CPFont)fontWithName:(CPString)aName size:(float)aSize
00053 {
00054 return _CPCachedFont(aName, aSize, NO) || [[CPFont alloc] _initWithName:aName size:aSize bold:NO];
00055 }
00056
00063 + (CPFont)boldFontWithName:(CPString)aName size:(float)aSize
00064 {
00065 return _CPCachedFont(aName, aSize, YES) || [[CPFont alloc] _initWithName:aName size:aSize bold:YES];
00066 }
00067
00073 + (CPFont)systemFontOfSize:(CPSize)aSize
00074 {
00075 return _CPCachedFont(_CPFontSystemFontFace, aSize, NO) || [[CPFont alloc] _initWithName:_CPFontSystemFontFace size:aSize bold:NO];
00076 }
00077
00083 + (CPFont)boldSystemFontOfSize:(CPSize)aSize
00084 {
00085 return _CPCachedFont(_CPFontSystemFontFace, aSize, YES) || [[CPFont alloc] _initWithName:_CPFontSystemFontFace size:aSize bold:YES];
00086 }
00087
00088
00089
00090
00091 - (id)_initWithName:(CPString)aName size:(float)aSize bold:(BOOL)isBold
00092 {
00093 self = [super init];
00094
00095 if (self)
00096 {
00097 _name = aName;
00098 _size = aSize;
00099 _isBold = isBold;
00100
00101 _cssString = _CPCreateCSSString(_name, _size, _isBold);
00102
00103 _CPFonts[_cssString] = self;
00104 }
00105
00106 return self;
00107 }
00108
00112 - (float)size
00113 {
00114 return _size;
00115 }
00116
00120 - (CPString)cssString
00121 {
00122 return _cssString;
00123 }
00124
00128 - (CPString)familyName
00129 {
00130 return _name;
00131 }
00132
00133 - (BOOL)isEqual:(id)anObject
00134 {
00135 return [anObject isKindOfClass:[CPFont class]] && [anObject cssString] === [self cssString];
00136 }
00137
00138 - (CPString)description
00139 {
00140 return [CPString stringWithFormat:@"%@ %@ %f pt.", [super description], [self familyName], [self size]];
00141 }
00142
00143 @end
00144
00145 var CPFontNameKey = @"CPFontNameKey",
00146 CPFontSizeKey = @"CPFontSizeKey",
00147 CPFontIsBoldKey = @"CPFontIsBoldKey";
00148
00149 @implementation CPFont (CPCoding)
00150
00156 - (id)initWithCoder:(CPCoder)aCoder
00157 {
00158 return [self _initWithName:[aCoder decodeObjectForKey:CPFontNameKey]
00159 size:[aCoder decodeFloatForKey:CPFontSizeKey]
00160 bold:[aCoder decodeBoolForKey:CPFontIsBoldKey]];
00161 }
00162
00167 - (void)encodeWithCoder:(CPCoder)aCoder
00168 {
00169 [aCoder encodeObject:_name forKey:CPFontNameKey];
00170 [aCoder encodeFloat:_size forKey:CPFontSizeKey];
00171 [aCoder encodeBool:_isBold forKey:CPFontIsBoldKey];
00172 }
00173
00174 @end