Per Cocoa, 0 size means create a static font in the current system font size. Added tests.

This commit is contained in:
Aparajita Fishman
2012-07-10 16:40:44 -07:00
parent 309463f15d
commit ec4bfa00ee
2 changed files with 67 additions and 20 deletions
+26 -16
View File
@@ -28,8 +28,10 @@
CPFontDefaultSystemFontFace = @"Arial, sans-serif";
CPFontDefaultSystemFontSize = 12;
// To create a font of a size that will always reflect the current
// system font size, use this for the size argument.
/*!
To create a font of a size that will dynamically reflect the current
system font size, use this for the size argument.
*/
CPFontCurrentSystemSize = -1;
// For internal use only by this class and subclasses
@@ -43,7 +45,7 @@ var _CPFontCache = {},
_CPFontStripRegExp = new RegExp("(^\\s*[\"']?|[\"']?\\s*$)", "g");
#define _CPRealFontSize(aSize) (aSize === CPFontCurrentSystemSize ? _CPFontSystemFontSize : aSize)
#define _CPRealFontSize(aSize) (aSize <= 0 ? _CPFontSystemFontSize : aSize)
#define _CPFontNormalizedNames(aName) _CPFontNormalizedNameArray(aName).join(", ")
#define _CPCachedFont(aName, aSize, isBold, isItalic) _CPFontCache[_CPFontCreateCSSString(_CPFontNormalizedNames(aName), aSize, isBold, isItalic)]
#define _CPUserFont(aName, aSize, isBold, isItalic) _CPCachedFont(aName, aSize, isBold, isItalic) || [[CPFont alloc] _initWithName:aName size:aSize bold:isBold italic:isItalic system:NO]
@@ -202,67 +204,75 @@ following:
/*!
Returns a font with the specified name and size.
@param aName the name of the font
@param aSize the size of the font (in px)
@param aSize the size of the font (in px). 0 or negative will create a font
in the current system font size.
@return the requested font
*/
+ (CPFont)fontWithName:(CPString)aName size:(float)aSize
{
return _CPUserFont(aName, aSize, NO, NO);
return _CPUserFont(aName, aSize <= 0 ? _CPFontSystemFontSize : aSize, NO, NO);
}
/*!
Returns a font with the specified name, size and style.
@param aName the name of the font
@param aSize the size of the font (in px)
@param aSize the size of the font (in px). 0 or negative will create a font
in the current system font size.
@param italic whether the font should be italicized
@return the requested font
*/
+ (CPFont)fontWithName:(CPString)aName size:(float)aSize italic:(BOOL)italic
{
return _CPUserFont(aName, aSize, NO, italic);
return _CPUserFont(aName, aSize <= 0 ? _CPFontSystemFontSize : aSize, NO, italic);
}
/*!
Returns a bold font with the specified name and size.
@param aName the name of the font
@param aSize the size of the font (in px)
@param aSize the size of the font (in px). 0 or negative will create a font
in the current system font size.
@return the requested bold font
*/
+ (CPFont)boldFontWithName:(CPString)aName size:(float)aSize
{
return _CPUserFont(aName, aSize, YES, NO);
return _CPUserFont(aName, aSize <= 0 ? _CPFontSystemFontSize : aSize, YES, NO);
}
/*!
Returns a bold font with the specified name, size and style.
@param aName the name of the font
@param aSize the size of the font (in px)
@param aSize the size of the font (in px). 0 or negative will create a font
in the current system font size.
@param italic whether the font should be italicized
@return the requested font
*/
+ (CPFont)boldFontWithName:(CPString)aName size:(float)aSize italic:(BOOL)italic
{
return _CPUserFont(aName, aSize, YES, italic);
return _CPUserFont(aName, aSize <= 0 ? _CPFontSystemFontSize : aSize, YES, italic);
}
/*!
Returns the system font scaled to the specified size
@param aSize the size of the font (in px)
@param aSize the size of the font (in px). 0 creates a static font
in the current system font size. Negative creates a font
that dynamically tracks the current system font size.
@return the requested system font
*/
+ (CPFont)systemFontOfSize:(CPSize)aSize
{
return _CPSystemFont(aSize, NO);
return _CPSystemFont(aSize === 0 ? _CPFontSystemFontSize : aSize, NO);
}
/*!
Returns the bold system font scaled to the specified size
@param aSize the size of the font (in px)
@param aSize the size of the font (in px). 0 creates a static font
in the current system font size. Negative creates a font
that dynamically tracks the current system font size.
@return the requested bold system font
*/
+ (CPFont)boldSystemFontOfSize:(CPSize)aSize
{
return _CPSystemFont(aSize, YES);
return _CPSystemFont(aSize === 0 ? _CPFontSystemFontSize : aSize, YES);
}
- (id)_initWithName:(CPString)aName size:(float)aSize bold:(BOOL)isBold italic:(BOOL)isItalic system:(BOOL)isSystem
@@ -369,7 +379,7 @@ following:
- (BOOL)isSystemSize
{
return _size === CPFontCurrentSystemSize;
return _size <= 0;
}
- (BOOL)isEqual:(id)anObject
+41 -4
View File
@@ -68,22 +68,59 @@
{
[inv setArgument:arg atIndex:idx + 2];
}];
[inv invoke];
var font = [inv returnValue];
[self assertTrue:([font isBold] == bold) message: [font description] + " should be bold: " + bold];
[self assertTrue:([font isItalic] == italic) message: [font description] + " should be italic: " + italic];
// get from cache
[inv invoke];
var cachedFont = [inv returnValue];
[self assert:cachedFont equals:font];
[self assertTrue:([cachedFont isBold] == bold) message:" cached " + [cachedFont description] + " should be bold: " + bold];
[self assertTrue:([cachedFont isItalic] == italic) message:" cached " + [cachedFont description] + " should be italic: " + italic];
}
- (void)testSizes
{
var font = [CPFont fontWithName:@"Arial" size:0],
systemSize = [CPFont systemFontSize];
[self assertTrue:([font size] === systemSize) message:" font size should be system size (" + systemSize + ")"];
var sysfont1 = [CPFont systemFontOfSize:12];
[self assertTrue:([sysfont1 size] === 12) message:" font size should be 12"];
var sysfont2 = [CPFont systemFontOfSize:0];
[self assertTrue:([sysfont2 size] === systemSize) message:" font size should be system size (" + systemSize + ")"];
var sysfont3 = [CPFont systemFontOfSize:-1];
[self assertTrue:([sysfont3 size] === systemSize) message:" font size should be system size (" + systemSize + ")"];
var newSystemSize = 13;
[CPFont setSystemFontSize:newSystemSize];
[self assertTrue:([sysfont2 size] === systemSize) message:" font size should be old system size (" + systemSize + ")"];
[self assertTrue:([sysfont3 size] === newSystemSize) message:" font size should be new system size (" + newSystemSize + ")"];
}
- (void)testFaces
{
var font = [CPFont fontWithName:@"Georgia" size:0],
systemFace = [CPFont systemFontFace];
[self assertTrue:([font familyName] === @"Georgia") message:" font face should be Georgia"];
var sysfont = [CPFont systemFontOfSize:12];
[self assertTrue:([sysfont familyName] === systemFace) message:" font face should be system face (" + systemFace + ")"];
var newSystemFace = @"Georgia";
[CPFont setSystemFontFace:newSystemFace];
[self assertTrue:([sysfont familyName] === newSystemFace) message:" font face should be new system face (" + newSystemFace + ")"];
}
@end
var _CPFontStripRegExp = new RegExp("(^\\s*[\"']?|[\"']?\\s*$)", "g");