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
00034 @implementation CPFont : CPObject
00035 {
00036 CPString _name;
00037 float _size;
00038 BOOL _isBold;
00039
00040 CPString _cssString;
00041 }
00042
00049 + (CPFont)fontWithName:(CPString)aName size:(float)aSize
00050 {
00051 return _CPCachedFont(aName, aSize, NO) || [[CPFont alloc] _initWithName:aName size:aSize bold:NO];
00052 }
00053
00060 + (CPFont)boldFontWithName:(CPString)aName size:(float)aSize
00061 {
00062 return _CPCachedFont(aName, aSize, YES) || [[CPFont alloc] _initWithName:aName size:aSize bold:YES];
00063 }
00064
00070 + (CPFont)systemFontOfSize:(CPSize)aSize
00071 {
00072 return _CPCachedFont(_CPFontSystemFontFace, aSize, NO) || [[CPFont alloc] _initWithName:_CPFontSystemFontFace size:aSize bold:NO];
00073 }
00074
00080 + (CPFont)boldSystemFontOfSize:(CPSize)aSize
00081 {
00082 return _CPCachedFont(_CPFontSystemFontFace, aSize, YES) || [[CPFont alloc] _initWithName:_CPFontSystemFontFace size:aSize bold:YES];
00083 }
00084
00085
00086
00087
00088 - (id)_initWithName:(CPString)aName size:(float)aSize bold:(BOOL)isBold
00089 {
00090 self = [super init];
00091
00092 if (self)
00093 {
00094 _name = aName;
00095 _size = aSize;
00096 _isBold = isBold;
00097
00098 _cssString = (_isBold ? @"bold " : @"") + ROUND(aSize) + @"px '" + aName + @"'";
00099
00100 _CPFonts[_cssString] = self;
00101 }
00102
00103 return self;
00104 }
00105
00109 - (float)size
00110 {
00111 return _size;
00112 }
00113
00117 - (CPString)cssString
00118 {
00119 return _cssString;
00120 }
00121
00125 - (CPString)familyName
00126 {
00127 return _name;
00128 }
00129
00130 @end
00131
00132 var CPFontNameKey = @"CPFontNameKey",
00133 CPFontSizeKey = @"CPFontSizeKey",
00134 CPFontIsBoldKey = @"CPFontIsBoldKey";
00135
00136 @implementation CPFont (CPCoding)
00137
00143 - (id)initWithCoder:(CPCoder)aCoder
00144 {
00145 return [self _initWithName:[aCoder decodeObjectForKey:CPFontNameKey]
00146 size:[aCoder decodeFloatForKey:CPFontSizeKey]
00147 bold:[aCoder decodeBoolForKey:CPFontIsBoldKey]];
00148 }
00149
00154 - (void)encodeWithCoder:(CPCoder)aCoder
00155 {
00156 [aCoder encodeObject:_name forKey:CPFontNameKey];
00157 [aCoder encodeFloat:_size forKey:CPFontSizeKey];
00158 [aCoder encodeBool:_isBold forKey:CPFontIsBoldKey];
00159 }
00160
00161 @end