API  0.9.10
CPFontDescriptor.j
Go to the documentation of this file.
1 /*
2  * CPFontDescriptor.j
3  * AppKit
4  *
5  * Created by Emmanuel Maillard on 07/03/10.
6  * Copyright Emmanuel Maillard 2010.
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  Font descriptor dictionary keys
25 */
26 
27 /*
28  CPFontNameAttribute contains a CPString that specified the font name
29  (may be an name list like: 'Marker Felt, Lucida Grande, Helvetica')
30 */
31 CPFontNameAttribute = @"CPFontNameAttribute";
32 /*
33  CPFontSizeAttribute contains a CPString that specified the font size
34  (as a float value)
35 */
36 CPFontSizeAttribute = @"CPFontSizeAttribute";
37 /*
38  CPFontTraitsAttribute a CPDictionary that contains font traits keys
39  (CPFontSymbolicTrait or CPFontWeightTrait)
40 */
41 CPFontTraitsAttribute = @"CPFontTraitsAttribute";
42 
43 // Font traits dictionary keys
44 /*
45  CPFontSymbolicTrait a CPNumber that contains CPFontFamilyClass and
46  typeface information flags.
47 */
48 CPFontSymbolicTrait = @"CPFontSymbolicTrait";
49 
50 /*
51  CPFontWeightTrait
52  We use CPString with CSS string values for font weight
53  (normal | bold | bolder | lighter | 100 | 200 | 300 | 400
54  | 500 | 600 | 700 | 800 | 900)
55  NOTE: Cocoa compatibility issue: NSFontWeightTrait are NSNumber for
56  font weight (from -1.0 to 1.0, 0.0 for normal weight).
57 */
58 CPFontWeightTrait = @"CPFontWeightTrait";
59 
60 /*
61  CPFontFamilyClass
62 */
71 
75 
76 CPFontFamilyClassMask = 0xF0000000;
77 
78 /*
79  Typeface information
80 */
82 CPFontBoldTrait = 1 << 1;
83 CPFontExpandedTrait = 1 << 5; /* TODO: CCS 3 font-stretch */
86 
90 @implementation CPFontDescriptor : CPObject
91 {
92  CPDictionary _attributes;
93 }
94 
101 + (CPFontDescriptor)fontDescriptorWithFontAttributes:(CPDictionary)attributes
102 {
103  return [[CPFontDescriptor alloc] initWithFontAttributes:attributes];
104 }
105 
113 + (CPFontDescriptor)fontDescriptorWithName:(CPString)fontName size:(float)size
114 {
115  return [[CPFontDescriptor alloc] initWithFontAttributes:[CPDictionary dictionaryWithObjects:[fontName, [CPString stringWithString:size + '']] forKeys:[CPFontNameAttribute,CPFontSizeAttribute]]];
116 }
117 
124 - (id)initWithFontAttributes:(CPDictionary)attributes
125 {
126  if (self = [super init])
127  {
128  _attributes = [[CPMutableDictionary alloc] init];
129 
130  if (attributes)
131  [_attributes addEntriesFromDictionary:attributes];
132  }
133 
134  return self;
135 }
136 
144 - (CPFontDescriptor)fontDescriptorByAddingAttributes:(CPDictionary)attributes
145 {
146  var attrib = [_attributes copy];
147 
148  [attrib addEntriesFromDictionary:attributes];
149 
151 }
152 
159 - (CPFontDescriptor)fontDescriptorWithSize:(float)aSize
160 {
161  var attrib = [_attributes copy];
162 
163  [attrib setObject:[CPString stringWithString:aSize + ''] forKey:CPFontSizeAttribute];
164 
166 }
167 
175 - (CPFontDescriptor)fontDescriptorWithSymbolicTraits:(CPFontSymbolicTraits)symbolicTraits
176 {
177  var attrib = [_attributes copy];
178 
179  if ([attrib objectForKey:CPFontTraitsAttribute])
180  [[attrib objectForKey:CPFontTraitsAttribute] setObject:[CPNumber numberWithUnsignedInt:symbolicTraits]
181  forKey:CPFontSymbolicTrait];
182  else
183  [attrib setObject:[CPDictionary dictionaryWithObject:[CPNumber numberWithUnsignedInt:symbolicTraits]
184  forKey:CPFontSymbolicTrait] forKey:CPFontTraitsAttribute];
185 
187 }
188 
189 - (id)objectForKey:(id)aKey
190 {
191  return [_attributes objectForKey:aKey];
192 }
193 
195 {
196  return _attributes;
197 }
198 
199 - (float)pointSize
200 {
201  var value = [_attributes objectForKey:CPFontSizeAttribute];
202 
203  return value ? [value floatValue] : 0.0;
204 }
205 
206 - (CPFontSymbolicTraits)symbolicTraits
207 {
208  var traits = [_attributes objectForKey:CPFontTraitsAttribute];
209 
210  return (traits && [traits objectForKey:CPFontSymbolicTrait]) ? [[traits objectForKey:CPFontSymbolicTrait] unsignedIntValue] : 0;
211 }
212 
213 @end
214 
215 var CPFontDescriptorAttributesKey = @"CPFontDescriptorAttributesKey";
216 
218 
225 - (id)initWithCoder:(CPCoder)aCoder
226 {
227  return [self initWithFontAttributes:[aCoder decodeObjectForKey:CPFontDescriptorAttributesKey]];
228 }
229 
235 - (void)encodeWithCoder:(CPCoder)aCoder
236 {
237  [aCoder encodeObject:_attributes forKey:CPFontDescriptorAttributesKey];
238 }
239 
240 @end
241 
242 var _wrapNameRegEx = new RegExp(/(\w+\s+\w+)(,*)/g);
243 
244 /*
245  Helper methods to CPFont for generating CSS font style
246 */
248 
250 {
251  return [self symbolicTraits] & CPFontItalicTrait ? @"italic" : @"normal";
252 }
253 
255 {
256  var traitsAttributes = [_attributes objectForKey:CPFontTraitsAttribute];
257 
258  if (traitsAttributes)
259  {
260  /* give preference to CPFontWeightTrait */
261  if ([traitsAttributes objectForKey:CPFontWeightTrait])
262  return [traitsAttributes objectForKey:CPFontWeightTrait];
263  /* else fallback to facetype symbolic traits */
264  if ([self symbolicTraits] & CPFontBoldTrait)
265  return @"bold";
266  }
267 
268  return @"normal";
269 }
270 
272 {
273  return [_attributes objectForKey:CPFontSizeAttribute] ? [[_attributes objectForKey:CPFontSizeAttribute] intValue] + "px" : @"";
274 }
275 
277 {
278  var aName = @"";
279 
280  if ([_attributes objectForKey:CPFontNameAttribute])
281  aName += [_attributes objectForKey:CPFontNameAttribute].replace(_wrapNameRegEx, '"$1"$2');
282 
283  var symbolicTraits = [self symbolicTraits];
284 
285  if (symbolicTraits)
286  {
288  aName += @", sans-serif";
289  else if ((symbolicTraits & CPFontFamilyClassMask) & CPFontSerifClass)
290  aName += @", serif";
291  }
292 
293  return aName;
294 }
295 
297 {
299  return @"small-caps";
300 
301  return @"normal";
302 }
303 
305 {
306  return [CPString stringWithString:[self fontStyleCSSString] + " "
307  + [self fontVariantCSSString] + " "
308  + [self fontWeightCSSString] + " "
309  + [self fontSizeCSSString] + " "
310  + [self fontFamilyCSSString]];
311 }
312 
313 @end
CPFontCondensedTrait
CPFontSerifClass
CPString fontSizeCSSString()
CPFontUnknownClass
CPFontOldStyleSerifsClass
CPFontSmallCapsTrait
CPFontExpandedTrait
id numberWithUnsignedInt:(unsigned anUnsignedInt)
Definition: CPNumber.j:102
A mutable key-value pair collection.
Definition: CPDictionary.h:2
CPFontModernSerifsClass
CPString fontWeightCSSString()
id stringWithString:(CPString aString)
Definition: CPString.j:119
An immutable string (collection of characters).
Definition: CPString.h:2
var CPFontDescriptorAttributesKey
CPString fontVariantCSSString()
CPFontSizeAttribute
CPFontSymbolicTrait
CPFontTransitionalSerifsClass
CPFontSansSerifClass
CPFontFamilyClassMask
CPFontSymbolicTraits symbolicTraits()
CPFontBoldTrait
CPDictionary fontAttributes()
CPFontTraitsAttribute
Defines methods for use when archiving & restoring (enc/decoding).
Definition: CPCoder.h:2
id dictionaryWithObjects:forKeys:(CPArray objects, [forKeys] CPArray keys)
Definition: CPDictionary.j:93
CPFontItalicTrait
id init()
Definition: CPObject.j:145
CPString fontStyleCSSString()
id initWithFontAttributes:(CPDictionary attributes)
CPFontSlabSerifsClass
CPFontNameAttribute
CPFontFreeformSerifsClass
CPString fontFamilyCSSString()
A bridged object to native Javascript numbers.
Definition: CPNumber.h:2
CPFontWeightTrait
id alloc()
Definition: CPObject.j:130
CPFontClarendonSerifsClass
id dictionaryWithObject:forKey:(id anObject, [forKey] id aKey)
Definition: CPDictionary.j:81