Add better support for font descriptor. Requires a recompile of the backend.

This should be enough for Emacs to compile with GNUstep.


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@28525 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Fred Kiefer 2009-08-23 21:39:28 +00:00
parent 9f6848cace
commit 56d4f7938d
7 changed files with 281 additions and 64 deletions

View file

@ -1,3 +1,13 @@
2009-08-23 Fred Kiefer <FredKiefer@gmx.de>
* Headers/Additions/GNUstepGUI/GSFontInfo.h,
* Headers/AppKit/NSFontDescriptor.h,
* Source/NSFontDescriptor.m,
* Source/NSFontManager.m,
* Source/NSFont.m,
* Source/GSFontInfo.m: Add better support for font
descriptor. Requires a recompile of the backend.
2009-08-23 14:23-EDT Gregory John Casamento <greg.casamento@gmail.com>
* Headers/Additions/GNUstepGUI/GSTheme.h: Add image parameter,

View file

@ -36,11 +36,13 @@
@class NSMutableDictionary;
@class NSArray;
@class NSBezierPath;
@class NSFontDescriptor;
@interface GSFontEnumerator : NSObject
{
NSArray *allFontNames;
NSMutableDictionary *allFontFamilies;
NSArray *allFontDescriptors;
}
+ (void) setDefaultClass: (Class)defaultClass;
@ -49,6 +51,9 @@
- (NSArray*) availableFonts;
- (NSArray*) availableFontFamilies;
- (NSArray*) availableMembersOfFontFamily: (NSString*)family;
- (NSArray*) availableFontDescriptors;
- (NSArray *) availableFontNamesMatchingFontDescriptor: (NSFontDescriptor *)descriptor;
- (NSArray *) matchingFontDescriptorsFor: (NSDictionary *)attributes;
/* Note that these are only called once. NSFont will remember the returned
values. Backends may override these. */
@ -81,8 +86,9 @@ values. Backends may override these. */
BOOL isBaseFont;
int weight;
NSFontTraitMask traits;
unsigned numberOfGlyphs;
NSCharacterSet *coveredCharacterSet;
unsigned numberOfGlyphs;
NSCharacterSet *coveredCharacterSet;
NSFontDescriptor *fontDescriptor;
}
+ (GSFontInfo*) fontInfoForFontName: (NSString*)fontName
@ -144,6 +150,7 @@ values. Backends may override these. */
- (float) widthOfString: (NSString*)string;
- (float) xHeight;
- (NSGlyph) glyphForCharacter: (unichar)theChar;
- (NSFontDescriptor*) fontDescriptor;
@end

View file

@ -102,7 +102,7 @@ extern NSString *NSFontVariationAxisMaximumValueKey;
extern NSString *NSFontVariationAxisDefaultValueKey;
extern NSString *NSFontVariationAxisNameKey;
@interface NSFontDescriptor : NSObject <NSCoding>
@interface NSFontDescriptor : NSObject <NSCoding, NSCopying>
{
NSDictionary *_attributes;
}

View file

@ -29,14 +29,19 @@
#include <math.h>
#include "GNUstepGUI/GSFontInfo.h"
#include "Foundation/NSAffineTransform.h"
#include <Foundation/NSArray.h>
#include <Foundation/NSCharacterSet.h>
#include <Foundation/NSDictionary.h>
#include <Foundation/NSString.h>
#include <Foundation/NSArray.h>
#include <Foundation/NSSet.h>
#include <Foundation/NSValue.h>
#include <Foundation/NSEnumerator.h>
#include <Foundation/NSException.h>
#include <Foundation/NSSet.h>
#include <Foundation/NSString.h>
#include <Foundation/NSValue.h>
#include "AppKit/NSFontDescriptor.h"
#include "GNUstepGUI/GSFontInfo.h"
static Class fontEnumeratorClass = Nil;
static Class fontInfoClass = Nil;
@ -98,6 +103,151 @@ static GSFontEnumerator *sharedEnumerator = nil;
return [allFontFamilies objectForKey: family];
}
- (NSArray*) availableFontDescriptors
{
if (allFontDescriptors == nil)
{
NSMutableArray *fontDescriptors;
NSEnumerator *keyEnumerator;
NSString *family;
fontDescriptors = [NSMutableArray array];
keyEnumerator = [allFontFamilies keyEnumerator];
while ((family = [keyEnumerator nextObject]) != nil)
{
NSArray *fontDefs = [allFontFamilies objectForKey: family];
NSEnumerator *fdEnumerator;
NSArray *fontDef;
fdEnumerator = [fontDefs objectEnumerator];
while ((fontDef = [fdEnumerator nextObject]) != nil)
{
NSFontDescriptor *fd;
NSDictionary *attributes;
NSNumber *weight = [fontDef objectAtIndex: 2];
NSNumber *traits = [fontDef objectAtIndex: 3];
NSDictionary *fontTraits;
float fweight = ([weight intValue] - 6) / 6.0;
fontTraits = [NSDictionary dictionaryWithObjectsAndKeys:
traits, NSFontSymbolicTrait,
[NSNumber numberWithFloat: fweight],
NSFontWeightTrait,
nil];
attributes = [NSDictionary dictionaryWithObjectsAndKeys:
family, NSFontFamilyAttribute,
[fontDef objectAtIndex: 0], NSFontNameAttribute,
[fontDef objectAtIndex: 1], NSFontFaceAttribute,
fontTraits, NSFontTraitsAttribute,
nil];
fd = [[NSFontDescriptor alloc] initWithFontAttributes: attributes];
[fontDescriptors addObject: fd];
RELEASE(fd);
}
}
allFontDescriptors = fontDescriptors;
}
return allFontDescriptors;
}
- (NSArray *) availableFontNamesMatchingFontDescriptor: (NSFontDescriptor *)descriptor
{
NSString *fontName;
NSArray *fds;
NSMutableArray *found;
NSEnumerator *fdEnumerator;
NSFontDescriptor *fd;
fontName = [descriptor objectForKey: NSFontNameAttribute];
if (fontName != nil)
{
return [NSArray arrayWithObject: fontName];
}
found = [NSMutableArray array];
// Normalize the font descriptor
fds = [descriptor matchingFontDescriptorsWithMandatoryKeys: nil];
fdEnumerator = [fds objectEnumerator];
while ((fd = [fdEnumerator nextObject]) != nil)
{
fontName = [fd objectForKey: NSFontNameAttribute];
if (fontName != nil)
{
[found addObject: fontName];
}
}
return found;
}
- (NSArray *) matchingFontDescriptorsFor: (NSDictionary *)attributes
{
NSMutableArray *found;
NSEnumerator *fdEnumerator;
NSFontDescriptor *fd;
NSArray *keys = [attributes allKeys];
found = [NSMutableArray arrayWithCapacity: 3];
// Get an enumerator for all available font descriptors
fdEnumerator = [[self availableFontDescriptors] objectEnumerator];
while ((fd = [fdEnumerator nextObject]) != nil)
{
NSEnumerator *keyEnumerator;
NSString *key;
BOOL match = YES;
keyEnumerator = [keys objectEnumerator];
while ((key = [keyEnumerator nextObject]) != nil)
{
id valueA = [attributes objectForKey: key];
if (valueA != nil)
{
id valueB = [fd objectForKey: key];
if (valueB == nil)
{
match = NO;
break;
}
// Special handling for NSFontTraitsAttribute
if ([key isEqual: NSFontTraitsAttribute])
{
NSNumber *traitsA = [valueA objectForKey: NSFontSymbolicTrait];
NSNumber *traitsB = [valueB objectForKey: NSFontSymbolicTrait];
// FIXME: For now we only compare symbolic traits
if ((traitsA != nil) &&
((traitsB == nil) ||
([traitsA unsignedIntValue] != [traitsB unsignedIntValue])))
{
match = NO;
break;
}
}
else
{
if (![valueA isEqual: valueB])
{
match = NO;
break;
}
}
}
}
if (match)
{
[found addObject: fd];
}
}
return found;
}
- (NSString *) defaultSystemFontName
{
@ -229,6 +379,7 @@ static GSFontEnumerator *sharedEnumerator = nil;
RELEASE(fontName);
RELEASE(familyName);
RELEASE(encodingScheme);
TEST_RELEASE(fontDescriptor);
[super dealloc];
}
@ -244,6 +395,7 @@ static GSFontEnumerator *sharedEnumerator = nil;
copy->fontName = [fontName copyWithZone: zone];
copy->familyName = [familyName copyWithZone: zone];
copy->encodingScheme = [encodingScheme copyWithZone: zone];
copy->fontDescriptor = [fontDescriptor copyWithZone: zone];
}
return copy;
}
@ -258,6 +410,7 @@ static GSFontEnumerator *sharedEnumerator = nil;
copy->fontName = [fontName copyWithZone: zone];
copy->familyName = [familyName copyWithZone: zone];
copy->encodingScheme = [encodingScheme copyWithZone: zone];
copy->fontDescriptor = [fontDescriptor copyWithZone: zone];
return copy;
}
@ -601,4 +754,46 @@ static GSFontEnumerator *sharedEnumerator = nil;
return NSNullGlyph;
}
- (NSFontDescriptor*) fontDescriptor
{
if (fontDescriptor == nil)
{
// Create a new one
NSAffineTransform *transform = [NSAffineTransform new];
NSAffineTransformStruct ats;
NSDictionary *attributes;
NSDictionary *fontTraits;
float fweight = (weight - 6) / 6.0;
float fslant = italicAngle / 30.0;
ats.m11 = matrix[0];
ats.m12 = matrix[1];
ats.m21 = matrix[2];
ats.m22 = matrix[3];
ats.tX = matrix[4];
ats.tY = matrix[5];
[transform setTransformStruct: ats];
fontTraits = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithUnsignedInt: traits],
NSFontSymbolicTrait,
[NSNumber numberWithFloat: fweight],
NSFontWeightTrait,
[NSNumber numberWithFloat: fslant],
NSFontSlantTrait,
nil];
attributes = [NSDictionary dictionaryWithObjectsAndKeys:
familyName, NSFontFamilyAttribute,
fontName, NSFontNameAttribute,
//fontFace, NSFontFaceAttribute,
fontTraits, NSFontTraitsAttribute,
transform, NSFontMatrixAttribute,
nil];
RELEASE(transform);
fontDescriptor = [[NSFontDescriptor alloc] initWithFontAttributes: attributes];
}
return fontDescriptor;
}
@end

View file

@ -527,6 +527,7 @@ static void setNSFont(NSString *key, NSFont *font)
{
NSArray *a;
float fontMatrix[6];
NSAffineTransformStruct ats;
descriptor = [descriptor matchingFontDescriptorWithMandatoryKeys:
[NSSet setWithArray: [[descriptor fontAttributes] allKeys]]];
@ -539,8 +540,13 @@ static void setNSFont(NSString *key, NSFont *font)
if ((a == nil) || ([a count] == 0))
return nil;
// FIXME: This method is deprecated
[transform getMatrix: fontMatrix];
ats = [transform transformStruct];
fontMatrix[0] = ats.m11;
fontMatrix[1] = ats.m12;
fontMatrix[2] = ats.m21;
fontMatrix[3] = ats.m22;
fontMatrix[4] = ats.tX;
fontMatrix[5] = ats.tY;
return [self fontWithName: [a objectAtIndex: 0]
matrix: fontMatrix];
@ -1140,8 +1146,7 @@ static BOOL flip_hack;
- (NSFontDescriptor*) fontDescriptor
{
// FIXME
return nil;
return [fontInfo fontDescriptor];
}
/* The following methods have to be implemented by backends */

View file

@ -37,8 +37,12 @@
#include <Foundation/NSString.h>
#include <Foundation/NSValue.h>
#include "AppKit/NSAffineTransform.h"
#include "AppKit/NSFontDescriptor.h"
#include "AppKit/NSFontManager.h"
@interface NSFontManager (GNUstep)
- (NSArray *) matchingFontDescriptorsFor: (NSDictionary *)attributes;
@end
@implementation NSFontDescriptor
@ -135,7 +139,7 @@
forKey: NSFontTraitsAttribute]];
}
- (id) initWithFontAttributes: (NSDictionary *) attributes
- (id) initWithFontAttributes: (NSDictionary *)attributes
{
if ((self = [super init]) != nil)
{
@ -189,45 +193,30 @@
return f;
}
// this is the core font search engine that knows about font directories
- (NSArray *) matchingFontDescriptorsWithMandatoryKeys: (NSSet *)keys
{
NSMutableArray *found;
NSEnumerator *fdEnumerator;
NSFontDescriptor *fd;
NSMutableDictionary *attributes= [NSMutableDictionary dictionaryWithCapacity: 4];
NSEnumerator *keyEnumerator;
NSString *key;
found = [NSMutableArray arrayWithCapacity: 3];
// FIXME: Get an enumerator for all available font descriptors
fdEnumerator = nil;
while ((fd = [fdEnumerator nextObject]) != nil)
if (keys == nil)
{
NSEnumerator *keyEnumerator;
NSString *key;
BOOL match = YES;
keyEnumerator = [keys objectEnumerator];
while ((key = [keyEnumerator nextObject]) != nil)
{
id value = [self objectForKey: key];
if (value != nil)
{
// FIXME: Special handling for NSFontTraitsAttribute
if (![value isEqual: [fd objectForKey: key]])
{
match = NO;
break;
}
}
}
if (match)
{
[found addObject: fd];
}
keys = [NSSet setWithObjects: NSFontNameAttribute, NSFontFamilyAttribute,
NSFontFaceAttribute, NSFontTraitsAttribute, nil];
}
return found;
keyEnumerator = [keys objectEnumerator];
while ((key = [keyEnumerator nextObject]) != nil)
{
id value = [_attributes objectForKey: key];
if (value != nil)
{
[attributes setObject: value forKey: key];
}
}
return [[NSFontManager sharedFontManager] matchingFontDescriptorsFor: attributes];
}
- (NSFontDescriptor *) matchingFontDescriptorWithMandatoryKeys: (NSSet *)keys;

View file

@ -153,6 +153,11 @@ static Class fontPanelClass = Nil;
NSMutableArray *fontNames = [NSMutableArray array];
NSFontTraitMask traits;
if (fontTraitMask == (NSUnitalicFontMask | NSUnboldFontMask))
{
fontTraitMask = 0;
}
for (i = 0; i < [fontFamilies count]; i++)
{
NSArray *fontDefs = [self availableMembersOfFontFamily:
@ -160,7 +165,7 @@ static Class fontPanelClass = Nil;
for (j = 0; j < [fontDefs count]; j++)
{
NSArray *fontDef = [fontDefs objectAtIndex: j];
NSArray *fontDef = [fontDefs objectAtIndex: j];
traits = [[fontDef objectAtIndex: 3] unsignedIntValue];
// Check if the font has exactly the given mask
@ -177,6 +182,17 @@ static Class fontPanelClass = Nil;
return [_fontEnumerator availableMembersOfFontFamily: family];
}
- (NSArray *) availableFontNamesMatchingFontDescriptor: (NSFontDescriptor *)descriptor
{
return [_fontEnumerator availableFontNamesMatchingFontDescriptor: descriptor];
}
// GNUstep extension
- (NSArray *) matchingFontDescriptorsFor: (NSDictionary *)attributes
{
return [_fontEnumerator matchingFontDescriptorsFor: attributes];
}
- (NSString*) localizedNameForFamily: (NSString*)family
face: (NSString*)face
{
@ -477,16 +493,6 @@ static Class fontPanelClass = Nil;
// If already have that trait then just return it
return fontObject;
}
else if (trait == NSUnboldFontMask)
{
return [self convertFont: fontObject
toNotHaveTrait: NSBoldFontMask];
}
else if (trait == NSUnitalicFontMask)
{
return [self convertFont: fontObject
toNotHaveTrait: NSItalicFontMask];
}
else
{
// Else convert it
@ -502,10 +508,20 @@ static Class fontPanelClass = Nil;
weight = 9;
t = t & ~NSUnboldFontMask;
}
else if (trait & NSUnboldFontMask)
{
// We cannot reuse the weight in an unbold
weight = 5;
t = t & ~NSBoldFontMask;
}
if (trait == NSItalicFontMask)
{
t = t & ~NSUnitalicFontMask;
}
else if (trait & NSUnitalicFontMask)
{
t = t & ~NSItalicFontMask;
}
t = t | trait;
@ -1058,17 +1074,12 @@ static Class fontPanelClass = Nil;
[a removeObject: descriptor];
}
}
- (NSArray *) fontDescriptorsInCollection: (NSString *)collection
{
return [_collections objectForKey: collection];
}
- (NSArray *) availableFontNamesMatchingFontDescriptor: (NSFontDescriptor *)descriptor
{
// FIXME
return nil;
}
- (NSDictionary *) convertAttributes: (NSDictionary *)attributes
{
NSMutableDictionary *newAttributes;