mirror of
https://github.com/gnustep/libs-gui.git
synced 2025-04-23 07:00:46 +00:00
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:
parent
59cc6718c4
commit
63a7dfcef5
5 changed files with 311 additions and 2 deletions
|
@ -1,3 +1,11 @@
|
|||
2007-05-16 Riccardo Mottola <rmottola@users.sf.net>
|
||||
|
||||
* Headers/AppKit/NSFontDescriptor.h : added from mgstep
|
||||
* Source/NSFont.* : added font descriptor related methods and
|
||||
NSFontDescriptor implementation
|
||||
* GNUMakefile: added NSFontDescriptor
|
||||
|
||||
|
||||
2007-05-16 Richard Frith-Macdonald <rfm@gnu.org>
|
||||
|
||||
* Source/NSTextAttachment.m: For MacOS-X compatibility, don't call
|
||||
|
|
|
@ -38,10 +38,10 @@
|
|||
#include <AppKit/AppKitDefines.h>
|
||||
// For NSControlSize
|
||||
#include <AppKit/NSColor.h>
|
||||
#import <AppKit/NSFontDescriptor.h>
|
||||
|
||||
@class NSDictionary;
|
||||
@class NSCharacterSet;
|
||||
@class NSFontDescriptor;
|
||||
|
||||
typedef unsigned int NSGlyph;
|
||||
|
||||
|
@ -121,6 +121,9 @@ APPKIT_EXPORT const float *NSFontIdentityMatrix;
|
|||
+ (NSFont*) controlContentFontOfSize: (float)fontSize;
|
||||
+ (NSFont*) labelFontOfSize: (float)fontSize;
|
||||
+ (NSFont*) menuBarFontOfSize: (float)fontSize;
|
||||
+ (NSFont *) fontWithDescriptor:(NSFontDescriptor *) descriptor size:(float) size;
|
||||
+ (NSFont *) fontWithDescriptor:(NSFontDescriptor *) descriptor size:(float) size
|
||||
textTransform:(NSAffineTransform *) transform;
|
||||
#endif
|
||||
|
||||
//
|
||||
|
|
120
Headers/AppKit/NSFontDescriptor.h
Normal file
120
Headers/AppKit/NSFontDescriptor.h
Normal file
|
@ -0,0 +1,120 @@
|
|||
/*
|
||||
NSFontDescriptor.h
|
||||
|
||||
Holds an image to use as a cursor
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc.
|
||||
|
||||
Author: Dr. H. Nikolaus Schaller <hns@computer.org>
|
||||
Date: 2006
|
||||
|
||||
This file is part of the GNUstep GUI Library.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Library General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Library General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Library General Public
|
||||
License along with this library; see the file COPYING.LIB.
|
||||
If not, write to the Free Software Foundation,
|
||||
51 Franklin Street, Fifth Floor,
|
||||
Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _GNUstep_H_NSFontDescriptor
|
||||
#define _GNUstep_H_NSFontDescriptor
|
||||
|
||||
#import "Foundation/NSSet.h"
|
||||
#import "AppKit/NSController.h"
|
||||
#import "AppKit/NSAffineTransform.h"
|
||||
|
||||
@class NSString;
|
||||
@class NSCoder;
|
||||
|
||||
typedef uint32_t NSFontSymbolicTraits;
|
||||
|
||||
typedef enum _NSFontFamilyClass
|
||||
{
|
||||
NSFontUnknownClass = 0 << 28,
|
||||
NSFontOldStyleSerifsClass = 1 << 28,
|
||||
NSFontTransitionalSerifsClass = 2 << 28,
|
||||
NSFontModernSerifsClass = 3 << 28,
|
||||
NSFontClarendonSerifsClass = 4 << 28,
|
||||
NSFontSlabSerifsClass = 5 << 28,
|
||||
NSFontFreeformSerifsClass = 7 << 28,
|
||||
NSFontSansSerifClass = 8 << 28,
|
||||
NSFontOrnamentalsClass = 9 << 28,
|
||||
NSFontScriptsClass = 10 << 28,
|
||||
NSFontSymbolicClass = 12 << 28
|
||||
} NSFontFamilyClass;
|
||||
|
||||
enum _NSFontTrait
|
||||
{
|
||||
NSFontItalicTrait = 0x0001,
|
||||
NSFontBoldTrait = 0x0002,
|
||||
NSFontExpandedTrait = 0x0020,
|
||||
NSFontCondensedTrait = 0x0040,
|
||||
NSFontMonoSpaceTrait = 0x0400,
|
||||
NSFontVerticalTrait = 0x0800,
|
||||
NSFontUIOptimizedTrait = 0x1000
|
||||
};
|
||||
|
||||
extern NSString *NSFontFamilyAttribute;
|
||||
extern NSString *NSFontNameAttribute;
|
||||
extern NSString *NSFontFaceAttribute;
|
||||
extern NSString *NSFontSizeAttribute;
|
||||
extern NSString *NSFontVisibleNameAttribute;
|
||||
extern NSString *NSFontColorAttribute;
|
||||
extern NSString *NSFontMatrixAttribute;
|
||||
extern NSString *NSFontVariationAttribute;
|
||||
extern NSString *NSFontCharacterSetAttribute;
|
||||
extern NSString *NSFontCascadeListAttribute;
|
||||
extern NSString *NSFontTraitsAttribute;
|
||||
extern NSString *NSFontFixedAdvanceAttribute;
|
||||
|
||||
extern NSString *NSFontSymbolicTrait;
|
||||
extern NSString *NSFontWeightTrait;
|
||||
extern NSString *NSFontWidthTrait;
|
||||
extern NSString *NSFontSlantTrait;
|
||||
|
||||
extern NSString *NSFontVariationAxisIdentifierKey;
|
||||
extern NSString *NSFontVariationAxisMinimumValueKey;
|
||||
extern NSString *NSFontVariationAxisMaximumValueKey;
|
||||
extern NSString *NSFontVariationAxisDefaultValueKey;
|
||||
extern NSString *NSFontVariationAxisNameKey;
|
||||
|
||||
@interface NSFontDescriptor : NSObject <NSCoding>
|
||||
{
|
||||
NSDictionary *_attributes;
|
||||
void *_backendPrivate; // caches an FT_Face
|
||||
}
|
||||
|
||||
+ (id) fontDescriptorWithFontAttributes:(NSDictionary *) attributes;
|
||||
+ (id) fontDescriptorWithName:(NSString *) name matrix:(NSAffineTransform *) matrix;
|
||||
+ (id) fontDescriptorWithName:(NSString *) name size:(float) size;
|
||||
|
||||
- (NSDictionary *) fontAttributes;
|
||||
- (NSFontDescriptor *) fontDescriptorByAddingAttributes:(NSDictionary *) attributes;
|
||||
- (NSFontDescriptor *) fontDescriptorWithFace:(NSString *) face;
|
||||
- (NSFontDescriptor *) fontDescriptorWithFamily:(NSString *) family;
|
||||
- (NSFontDescriptor *) fontDescriptorWithMatrix:(NSAffineTransform *) matrix;
|
||||
- (NSFontDescriptor *) fontDescriptorWithSize:(float) size;
|
||||
- (NSFontDescriptor *) fontDescriptorWithSymbolicTraits:(NSFontSymbolicTraits) traits;
|
||||
- (id) initWithFontAttributes:(NSDictionary *) attributes;
|
||||
- (NSArray *) matchingFontDescriptorsWithMandatoryKeys:(NSSet *) keys;
|
||||
- (NSAffineTransform *) matrix;
|
||||
- (id) objectForKey:(NSString *) attribute;
|
||||
- (float) pointSize;
|
||||
- (NSString *) postscriptName;
|
||||
- (NSFontSymbolicTraits) symbolicTraits;
|
||||
|
||||
@end
|
||||
|
||||
#endif /* _GNUstep_H_NSFontDescriptor */
|
|
@ -270,6 +270,7 @@ NSEPSImageRep.h \
|
|||
NSEvent.h \
|
||||
NSFileWrapper.h \
|
||||
NSFont.h \
|
||||
NSFontDescriptor.h \
|
||||
NSFontManager.h \
|
||||
NSFontPanel.h \
|
||||
NSForm.h \
|
||||
|
|
179
Source/NSFont.m
179
Source/NSFont.m
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue