API  1.0.0
CPStringDrawing.j
Go to the documentation of this file.
1 /*
2  * CPStringDrawing.j
3  * AppKit
4  *
5  * Created by Francisco Tolmasky.
6  * Copyright 2008, 280 North, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 
24 
25 
29 
31 
33 
37 + (CPDictionary)metricsOfFont:(CPFont)aFont
38 {
39  return [CPPlatformString metricsOfFont:aFont];
40 }
41 
45 - (CPString)cssString
46 {
47  return self;
48 }
49 
50 - (CGSize)sizeWithFont:(CPFont)aFont
51 {
52  return [self sizeWithFont:aFont inWidth:NULL];
53 }
54 
55 + (void) initialize
56 {
57  if ([self class] != [CPString class])
58  return;
59 
60 #if PLATFORM(DOM)
63 #endif
64 }
65 
66 - (CGSize)_sizeWithFont:(CPFont)aFont inWidth:(float)aWidth
67 {
68  var size;
69 
70 #if PLATFORM(DOM)
72  return [CPPlatformString sizeOfString:self withFont:aFont forWidth:aWidth];
73 
74  var sizeCacheForFont = CPStringSizeWithFontInWidthCache[self];
75 
76  if (sizeCacheForFont === undefined)
77  sizeCacheForFont = CPStringSizeWithFontInWidthCache[self] = [];
78 
79  if (!aWidth)
80  aWidth = '0';
81 
82  var cssString = [aFont cssString],
83  cacheKey = cssString + '_' + aWidth;
84 
85  size = sizeCacheForFont[cacheKey];
86 
87  if (size !== undefined && sizeCacheForFont.hasOwnProperty(cacheKey))
88  return CGSizeMakeCopy(size);
89 
90  if (!CPFeatureIsCompatible(CPHTMLCanvasFeature) || aWidth > 0)
91  size = [CPPlatformString sizeOfString:self withFont:aFont forWidth:aWidth];
92  else
93  {
95  CPStringSizeMeasuringContext.font = cssString;
96 
97  var fontHeight = CPStringSizeWithFontHeightCache[cssString];
98 
99  if (fontHeight === undefined)
100  fontHeight = CPStringSizeWithFontHeightCache[cssString] = [aFont defaultLineHeightForFont];
101 
102  size = CGSizeMake(CPStringSizeMeasuringContext.measureText(self).width, fontHeight);
103  }
104 
105  sizeCacheForFont[cacheKey] = size;
106 #else
107  size = CGSizeMake(0, 0);
108 #endif
109  return CGSizeMakeCopy(size);
110 }
111 
112 - (CGSize)sizeWithFont:(CPFont)aFont inWidth:(float)aWidth
113 {
114  var size = [self _sizeWithFont:aFont inWidth:aWidth];
115  return CGSizeMake(CEIL(size.width), size.height);
116 }
117 
118 @end
Definition: CPFont.h:2
float defaultLineHeightForFont()
Definition: CPFont.j:362
var CPStringSizeWithFontInWidthCache
A mutable key-value pair collection.
Definition: CPDictionary.h:2
var CPStringSizeWithFontHeightCache
CPStringSizeCachingEnabled
An immutable string (collection of characters).
Definition: CPString.h:2
if(CPFeatureIsCompatible(CPHTMLCanvasFeature))
function CPFeatureIsCompatible(aFeature)
CGSize sizeOfString:withFont:forWidth:(CPString aString, [withFont] CPFont aFont, [forWidth] float aWidth)
CPHTMLCanvasFeature
var CPStringSizeMeasuringContext
CPTextSizingAlwaysNeedsSetFontBug
CPString cssString()
Definition: CPFont.j:383
CPDictionary metricsOfFont:(CPFont aFont)
CGSize sizeWithFont:inWidth:(CPFont aFont, [inWidth] float aWidth)
function CPPlatformHasBug(aBug)
function CGBitmapGraphicsContextCreate()
Definition: CGContext.j:136