From 34a6389a92c87eeee6f19503a0ef7acc237f5207 Mon Sep 17 00:00:00 2001 From: luddep Date: Fri, 8 Jan 2010 18:05:33 +0100 Subject: [PATCH] CPFont now supports multiple font families. Closes #255. --- AppKit/CPFont.j | 13 ++++++++----- Tests/AppKit/CPFontTest.j | 41 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 Tests/AppKit/CPFontTest.j diff --git a/AppKit/CPFont.j b/AppKit/CPFont.j index b511326f5..a60fc32a7 100644 --- a/AppKit/CPFont.j +++ b/AppKit/CPFont.j @@ -20,10 +20,13 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -var _CPFonts = {}; - _CPFontSystemFontFace = @"Arial"; +var _CPFonts = {}, + _CPFontSystemFontFace = @"Arial, sans-serif", + _CPWrapRegExp = new RegExp("\\s*,\\s*", "g"); -#define _CPCachedFont(aName, aSize, isBold) _CPFonts[(isBold ? @"bold " : @"") + ROUND(aSize) + @"px '" + aName + @"'"] + +#define _CPCreateCSSString(aName, aSize, isBold) (isBold ? @"bold " : @"") + ROUND(aSize) + @"px " + ((aName === _CPFontSystemFontFace) ? aName : (@"\"" + aName.replace(_CPWrapRegExp, '", "') + @"\", " + _CPFontSystemFontFace)) +#define _CPCachedFont(aName, aSize, isBold) _CPFonts[_CPCreateCSSString(aName, aSize, isBold)] /*! @ingroup appkit @@ -95,7 +98,7 @@ var _CPFonts = {}; _size = aSize; _isBold = isBold; - _cssString = (_isBold ? @"bold " : @"") + ROUND(aSize) + @"px '" + aName + @"'"; + _cssString = _CPCreateCSSString(_name, _size, _isBold); _CPFonts[_cssString] = self; } @@ -158,4 +161,4 @@ var CPFontNameKey = @"CPFontNameKey", [aCoder encodeBool:_isBold forKey:CPFontIsBoldKey]; } -@end +@end \ No newline at end of file diff --git a/Tests/AppKit/CPFontTest.j b/Tests/AppKit/CPFontTest.j new file mode 100644 index 000000000..7037be315 --- /dev/null +++ b/Tests/AppKit/CPFontTest.j @@ -0,0 +1,41 @@ +@import + +@implementation CPFontTest : OJTestCase +{ + CPFont _systemFont; + CPFont _boldSystemFont; + + CPFont _customFont; + CPFont _boldCustomFont; +} + +- (void)setUp +{ + _systemFont = [CPFont systemFontOfSize:15]; + _boldSystemFont = [CPFont boldSystemFontOfSize:15]; + + _customFont = [CPFont fontWithName:@"Marker Felt, Lucida Grande, Helvetica" size:30]; + _boldCustomFont = [CPFont boldFontWithName:@"Helvetica" size:30]; +} + +- (void)testSystemFontCSSString +{ + [self assert:[_systemFont cssString] equals:@"15px Arial, sans-serif"]; +} + +- (void)testBoldSystemFontCSSString +{ + [self assert:[_boldSystemFont cssString] equals:@"bold 15px Arial, sans-serif"]; +} + +- (void)testCustomFontCSSString +{ + [self assert:[_customFont cssString] equals:@"30px \"Marker Felt\", \"Lucida Grande\", \"Helvetica\", Arial, sans-serif"]; +} + +- (void)testBoldCustomFontCSSString +{ + [self assert:[_boldCustomFont cssString] equals:@"bold 30px \"Helvetica\", Arial, sans-serif"]; +} + +@end \ No newline at end of file