added NSFontDescriptor, implementation and related methods

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@25174 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Riccardo Mottola 2007-05-16 15:50:08 +00:00
parent 59cc6718c4
commit 63a7dfcef5
5 changed files with 311 additions and 2 deletions

View file

@ -496,6 +496,32 @@ static void setNSFont(NSString *key, NSFont *font)
return getNSFont(fontSize, RoleUserFont);
}
+ (NSFont *) fontWithDescriptor:(NSFontDescriptor *) descriptor size:(float) size;
{
return [self fontWithDescriptor:descriptor size:size textTransform:nil];
}
// the transform/matrix can be used to rotate/scale/shear the whole font (independently of the CTM!)
+ (NSFont *) fontWithDescriptor:(NSFontDescriptor *) descriptor size:(float) size textTransform:(NSAffineTransform *) transform;
{
NSArray *a;
if (size == 0.0)
size=[NSFont systemFontSize]; // default
descriptor=[descriptor fontDescriptorWithSize:size];
if (transform)
descriptor=[descriptor fontDescriptorByAddingAttributes:[NSDictionary dictionaryWithObject:transform forKey:NSFontMatrixAttribute]];
a=[descriptor matchingFontDescriptorsWithMandatoryKeys:[NSSet setWithArray:[[descriptor fontAttributes] allKeys]]]; // match all keys
if([a count] == 0)
return nil;
return [a objectAtIndex:0]; // return first matching font
}
/**<p>Returns an array of the names of preferred fonts.</p>
<p>See Also: +setPreferredFontNames:</p>
*/
@ -829,7 +855,7 @@ static void setNSFont(NSString *key, NSFont *font)
return NO;
if ([[anObject fontName] isEqual: fontName] == NO)
return NO;
obj_matrix = [anObject matrix];
obj_matrix = [(NSFontDescriptor*)anObject matrix];
for (i = 0; i < 6; i++)
if (obj_matrix[i] != matrix[i])
return NO;
@ -1392,3 +1418,154 @@ int NSConvertGlyphsToPackedGlyphs(NSGlyph *glBuf,
return j;
}
NSString *NSFontFamilyAttribute=@"Family";
NSString *NSFontNameAttribute=@"Name";
NSString *NSFontFaceAttribute=@"Face";
NSString *NSFontSizeAttribute=@"Size";
NSString *NSFontVisibleNameAttribute=@"VisibleName";
NSString *NSFontColorAttribute=@"Color";
NSString *NSFontMatrixAttribute=@"Matrix";
NSString *NSFontVariationAttribute=@"Variation";
NSString *NSFontCharacterSetAttribute=@"CharacterSet";
NSString *NSFontCascadeListAttribute=@"CascadeList";
NSString *NSFontTraitsAttribute=@"Traits";
NSString *NSFontFixedAdvanceAttribute=@"FixedAdvance";
NSString *NSFontSymbolicTrait=@"SymbolicTrait";
NSString *NSFontWeightTrait=@"WeightTrait";
NSString *NSFontWidthTrait=@"WidthTrait";
NSString *NSFontSlantTrait=@"SlantTrait";
NSString *NSFontVariationAxisIdentifierKey=@"VariationAxisIdentifier";
NSString *NSFontVariationAxisMinimumValueKey=@"VariationAxisMinimumValue";
NSString *NSFontVariationAxisMaximumValueKey=@"VariationAxisMaximumValue";
NSString *NSFontVariationAxisDefaultValueKey=@"VariationAxisDefaultValue";
NSString *NSFontVariationAxisNameKey=@"VariationAxisName";
@implementation NSFontDescriptor
+ (id) fontDescriptorWithFontAttributes:(NSDictionary *) attributes;
{
return [[[self alloc] initWithFontAttributes:attributes] autorelease];
}
+ (id) fontDescriptorWithName:(NSString *) name matrix:(NSAffineTransform *) matrix;
{
return [self fontDescriptorWithFontAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
name, NSFontNameAttribute,
matrix, NSFontMatrixAttribute,
nil]];
}
+ (id) fontDescriptorWithName:(NSString *) name size:(float) size;
{
return [self fontDescriptorWithFontAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
name, NSFontNameAttribute,
[NSString stringWithFormat:@"%f", size], NSFontSizeAttribute,
nil]];
}
- (NSDictionary *) fontAttributes; { return _attributes; }
- (NSFontDescriptor *) fontDescriptorByAddingAttributes:(NSDictionary *) attributes;
{
NSFontDescriptor *fd=[super copy]; // make a copy
if(fd)
{
fd->_attributes=[_attributes mutableCopy]; // current attributes
[(NSMutableDictionary *) fd->_attributes addEntriesFromDictionary:attributes]; // change
}
return fd;
}
- (NSFontDescriptor *) fontDescriptorWithFace:(NSString *) face;
{
return [self fontDescriptorByAddingAttributes:[NSDictionary dictionaryWithObject:face forKey:NSFontFaceAttribute]];
}
- (NSFontDescriptor *) fontDescriptorWithFamily:(NSString *) family;
{
return [self fontDescriptorByAddingAttributes:[NSDictionary dictionaryWithObject:family forKey:NSFontFamilyAttribute]];
}
- (NSFontDescriptor *) fontDescriptorWithMatrix:(NSAffineTransform *) matrix;
{
return [self fontDescriptorByAddingAttributes:[NSDictionary dictionaryWithObject:matrix forKey:NSFontMatrixAttribute]];
}
- (NSFontDescriptor *) fontDescriptorWithSize:(float) size;
{
return [self fontDescriptorByAddingAttributes:[NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:size] forKey:NSFontSizeAttribute]];
}
- (NSFontDescriptor *) fontDescriptorWithSymbolicTraits:(NSFontSymbolicTraits) traits;
{
return [self fontDescriptorByAddingAttributes:[NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedInt:traits] forKey:NSFontSymbolicTrait]];
}
- (id) initWithFontAttributes:(NSDictionary *) attributes;
{
if((self=[super init]))
{
if(attributes)
_attributes=[attributes retain];
else
_attributes=[[NSDictionary alloc] init]; // empty dictionary
}
return self;
}
- (void) encodeWithCoder:(NSCoder *)aCoder
{
}
- (id) initWithCoder:(NSCoder *)aDecoder
{
if(![aDecoder allowsKeyedCoding])
; // TODO FIXME (copied from mgstep)
_attributes = [[aDecoder decodeObjectForKey:@"NSAttributes"] retain];
return self;
}
- (void) dealloc;
{
[_attributes release];
[super dealloc];
}
- (id) copyWithZone:(NSZone *)z
{
NSFontDescriptor *f=[isa allocWithZone:z];
if(f)
f->_attributes=[_attributes copyWithZone:z];
return f;
}
/*
TODO FIXME: how to port this from mgStep ?
- (NSArray *) matchingFontDescriptorsWithMandatoryKeys:(NSSet *) keys; // this is the core font search engine that knows about font directories
{
return BACKEND;
}
*/
- (NSAffineTransform *) matrix; { return [_attributes objectForKey:NSFontMatrixAttribute]; }
- (id) objectForKey:(NSString *) attribute; { return [_attributes objectForKey:attribute]; }
- (float) pointSize; { return [[_attributes objectForKey:NSFontSizeAttribute] floatValue]; }
- (NSFontSymbolicTraits) symbolicTraits; { return [[_attributes objectForKey:NSFontSymbolicTrait] unsignedIntValue]; }
- (NSString *) postscriptName;
{
NSMutableString *family=[[[self objectForKey:NSFontFamilyAttribute] mutableCopy] autorelease];
NSString *face=[self objectForKey:NSFontFaceAttribute];
[family replaceOccurrencesOfString:@" " withString:@"" options:0 range:NSMakeRange(0, [family length])];
if([face isEqualToString:@"Regular"])
return family;
return [NSString stringWithFormat:@"%@-%@", family, face];
}
@end