35 _CPFontSystemFacePlaceholder =
"_CPFontSystemFacePlaceholder";
37 var _CPFontCache = {},
38 _CPSystemFontCache = {},
40 _CPFontSystemFontSize = 12,
42 _CPFontStripRegExp =
new RegExp(
"(^\\s*[\"']?|[\"']?\\s*$)",
"g");
45 #define _CPRealFontSize(aSize) (aSize <= 0 ? _CPFontSystemFontSize : aSize) 46 #define _CPFontNormalizedNames(aName) _CPFontNormalizedNameArray(aName).join(", ") 47 #define _CPCachedFont(aName, aSize, isBold, isItalic) _CPFontCache[_CPFontCreateCSSString(_CPFontNormalizedNames(aName), aSize, isBold, isItalic)] 48 #define _CPUserFont(aName, aSize, isBold, isItalic) _CPCachedFont(aName, aSize, isBold, isItalic) || [[CPFont alloc] _initWithName:aName size:aSize bold:isBold italic:isItalic system:NO] 50 #define _CPSystemFontCacheKey(aSize, isBold) (String(aSize) + (isBold ? "b" : "")) 51 #define _CPCachedSystemFont(aSize, isBold) _CPSystemFontCache[_CPSystemFontCacheKey(aSize, isBold)] 52 #define _CPSystemFont(aSize, isBold) (_CPCachedSystemFont(aSize, isBold) || [[CPFont alloc] _initWithName:_CPFontSystemFacePlaceholder size:aSize bold:isBold italic:NO system:YES]) 123 if (
self !== [
CPFont class])
132 _CPFontSystemFontFace = _CPFontNormalizedNames(systemFontFace);
140 _CPFontSystemFontSize = systemFontSize;
148 return _CPFontSystemFontFace;
156 var normalizedFaces = _CPFontNormalizedNames(aFace);
158 if (normalizedFaces === _CPFontSystemFontFace)
161 [
self _invalidateSystemFontCache];
162 _CPFontSystemFontFace = aFace;
168 + (float)systemFontSize
170 return _CPFontSystemFontSize;
173 + (
CPFont)systemFontForControlSize:(CPControlSize)aSize
193 + (float)setSystemFontSize:(
float)size
195 if (size > 0 && size !== _CPFontSystemFontSize)
197 [
self _invalidateSystemFontCache];
198 _CPFontSystemFontSize = size;
202 + (void)_invalidateSystemFontCache
204 var systemSize = String(_CPFontSystemFontSize),
207 for (var key in _CPSystemFontCache)
209 if (_CPSystemFontCache.hasOwnProperty(key) &&
210 (key.indexOf(systemSize) === 0 || key.indexOf(currentSize) === 0))
212 delete _CPSystemFontCache[key];
226 return _CPUserFont(aName, aSize <= 0 ? _CPFontSystemFontSize : aSize, NO, NO);
239 return _CPUserFont(aName, aSize <= 0 ? _CPFontSystemFontSize : aSize, NO, italic);
251 return _CPUserFont(aName, aSize <= 0 ? _CPFontSystemFontSize : aSize, YES, NO);
264 return _CPUserFont(aName, aSize <= 0 ? _CPFontSystemFontSize : aSize, YES, italic);
270 + (
CPFont)_fontWithName:(
CPString)aName size:(
float)aSize bold:(BOOL)bold italic:(BOOL)italic
272 return _CPUserFont(aName, aSize <= 0 ? _CPFontSystemFontSize : aSize, bold, italic);
284 return _CPSystemFont(aSize === 0 ? _CPFontSystemFontSize : aSize, NO);
294 + (
CPFont)boldSystemFontOfSize:(CGSize)aSize
296 return _CPSystemFont(aSize === 0 ? _CPFontSystemFontSize : aSize, YES);
299 - (id)_initWithName:(
CPString)aName size:(
float)aSize bold:(BOOL)isBold italic:(BOOL)isItalic system:(BOOL)isSystem
310 _isItalic = isItalic;
311 _isSystem = isSystem;
316 _cssString = _CPFontCreateCSSString(_CPFontSystemFontFace, _size, _isBold, _isItalic);
317 _CPSystemFontCache[_CPSystemFontCacheKey(_size, _isBold)] =
self;
321 _name = _CPFontNormalizedNames(aName);
322 _cssString = _CPFontCreateCSSString(_name, _size, _isBold, _isItalic);
323 _CPFontCache[_cssString] =
self;
335 var font = _isSystem ? _CPSystemFont(_size, _isBold) :
self;
340 return font._ascender;
349 var font = _isSystem ? _CPSystemFont(_size, _isBold) :
self;
351 if (!font._descender)
354 return font._descender;
362 - (float)defaultLineHeightForFont
364 var font = _isSystem ? _CPSystemFont(_size, _isBold) :
self;
366 if (!font._lineHeight)
369 return font._lineHeight;
377 return _CPRealFontSize(_size);
385 var font = _isSystem ? _CPSystemFont(_size, _isBold) :
self;
387 return font._cssString;
396 return _CPFontSystemFontFace;
408 return [anObject isKindOfClass:[
CPFont class]] && [anObject cssString] === _cssString;
418 return [[
CPFont alloc] _initWithName:_name size:_size bold:_isBold italic:_isItalic system:_isSystem];
425 _ascender = [metrics objectForKey:@"ascender"];
426 _descender = [metrics objectForKey:@"descender"];
427 _lineHeight = [metrics objectForKey:@"lineHeight"];
436 var aName = [fontDescriptor
objectForKey: CPFontNameAttribute] ,
441 return [
self _initWithName:aName size:aSize bold:isBold italic:isItalic system:NO];
446 var aName = [fontDescriptor
objectForKey: CPFontNameAttribute],
450 return [
self _fontWithName:aName size:aSize || [fontDescriptor
pointSize] bold:isBold italic:isItalic];
484 var fontName = [aCoder decodeObjectForKey:CPFontNameKey],
485 size = [aCoder decodeFloatForKey:CPFontSizeKey],
486 isBold = [aCoder decodeBoolForKey:CPFontIsBoldKey],
487 isItalic = [aCoder decodeBoolForKey:CPFontIsItalicKey],
488 isSystem = [aCoder decodeBoolForKey:CPFontIsSystemKey];
490 return [
self _initWithName:fontName size:size bold:isBold italic:isItalic system:isSystem];
499 [aCoder encodeObject:_name forKey:CPFontNameKey];
500 [aCoder encodeFloat:_size forKey:CPFontSizeKey];
501 [aCoder encodeBool:_isBold forKey:CPFontIsBoldKey];
502 [aCoder encodeBool:_isItalic forKey:CPFontIsItalicKey];
503 [aCoder encodeBool:_isSystem forKey:CPFontIsSystemKey];
510 var _CPFontCreateCSSString =
function(aName, aSize, isBold, isItalic)
512 var properties = (isItalic ?
"italic " :
"") + (isBold ?
"bold " :
"") + _CPRealFontSize(aSize) + "px ";
514 return properties + _CPFontConcatNameWithFallback(aName);
517 var _CPFontConcatNameWithFallback =
function(aName)
519 var names = _CPFontNormalizedNameArray(aName),
520 fallbackFaces = _CPFontFallbackFaces.slice(0);
523 for (var i = 0; i < names.length; ++i)
525 for (var j = 0; j < fallbackFaces.length; ++j)
527 if (names[i].toLowerCase() === fallbackFaces[j].toLowerCase())
529 fallbackFaces.splice(j, 1);
534 if (names[i].indexOf(
" ") > 0)
535 names[i] =
'"' + names[i] + '"';
538 return names.concat(fallbackFaces).join(
", ");
541 var _CPFontNormalizedNameArray =
function(aName)
543 var names = aName.split(
",");
545 for (var i = 0; i < names.length; ++i)
546 names[i] = names[i].replace(_CPFontStripRegExp, "");
CPDictionary metricsOfFont:(CPFont aFont)
CPFont systemFontOfSize:(CGSize aSize)
CPFontDefaultSystemFontSize
id objectForKey:(id aKey)
CPFontDescriptor fontDescriptorWithName:size:(CPString fontName, [size] float size)
An immutable string (collection of characters).
CPFontDescriptor fontDescriptorWithSymbolicTraits:(CPFontSymbolicTraits symbolicTraits)
CPFontSymbolicTraits symbolicTraits()
id objectForInfoDictionaryKey:(CPString aKey)
Defines methods for use when archiving & restoring (enc/decoding).
CPControlSize CPRegularControlSize
CPFontDefaultSystemFontFace
CPBundle bundleForClass:(Class aClass)
id stringWithFormat:(CPString format, [,] ...)
FrameUpdater prototype description