Add cachedScreenFont ivar and use it. Use more efficient keys in the globalFontMap.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@18575 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Alexander Malmberg 2004-02-10 20:52:11 +00:00
parent 1b7ccd821d
commit 6b59a4f9c8
3 changed files with 120 additions and 36 deletions

View file

@ -1,3 +1,13 @@
2004-02-10 21:48 Alexander Malmberg <alexander@malmberg.org>
* Headers/AppKit/NSFont.h: Add cachedScreenFont ivar.
* Source/NSFont.m: Add GSFontMapKey class and use it to find fonts
in globalFontMap. Replace newNameWithMatrix function with keyForFont.
Update callers.
(-initWithName:matrix:fix:screenFont:role:): Initialize
cachedScreenFont.
(-screenFont): Use cachedScreenFont.
2004-02-09 03:10 Alexander Malmberg <alexander@malmberg.org> 2004-02-09 03:10 Alexander Malmberg <alexander@malmberg.org>
* configure.ac: Check for X. Add --enable-ungif/--disable-ungif and * configure.ac: Check for X. Add --enable-ungif/--disable-ungif and

View file

@ -77,6 +77,13 @@ APPKIT_EXPORT const float *NSFontIdentityMatrix;
stored here. stored here.
*/ */
int role; int role;
/*
For printer fonts, this is a cache of the corresponding screen font.
It is initialized to placeHolder, and is created for real on demand in
-screenFont (and retained). For screen fonts, it's nil.
*/
NSFont *cachedScreenFont;
} }
// //

View file

@ -49,28 +49,80 @@
screenFont: (BOOL)screenFont screenFont: (BOOL)screenFont
role: (int)role; role: (int)role;
+ (NSFont*) _fontWithName: (NSString*)aFontName + (NSFont*) _fontWithName: (NSString*)aFontName
size: (float)fontSize size: (float)fontSize
role: (int)role; role: (int)role;
@end @end
static int currentVersion = 3; static int currentVersion = 3;
/*
* Just to ensure that we use a standard name in the cache.
*/
static NSString*
newNameWithMatrix(NSString *name, const float *matrix, BOOL fix,
BOOL screenFont, int role)
{
NSString *nameWithMatrix;
nameWithMatrix = [[NSString alloc] initWithFormat: /*
@"%@ %.3f %.3f %.3f %.3f %.3f %.3f %c %c %i", name, Instances of GSFontMapKey are used to find cached font instances in
matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5], globalFontMap.
(fix == NO) ? 'N' : 'Y', */
screenFont ? 'S' : 'P', @interface GSFontMapKey : NSObject
role]; {
return nameWithMatrix; @public
NSString *name;
BOOL screenFont;
int role;
int fix;
int matrix[6];
unsigned int hash;
}
@end
@implementation GSFontMapKey
-(unsigned int) hash
{
return hash;
}
-(BOOL) isEqual: (id)other
{
GSFontMapKey *o;
if (![other isKindOfClass: isa])
return NO;
o = other;
if (hash != o->hash || screenFont != o->screenFont || role != o->role
|| fix != o->fix)
return NO;
if (![name isEqualToString: o->name])
return NO;
if (matrix[0] != o->matrix[0]
|| matrix[1] != o->matrix[1]
|| matrix[2] != o->matrix[2]
|| matrix[3] != o->matrix[3]
|| matrix[4] != o->matrix[4]
|| matrix[5] != o->matrix[5])
return NO;
return YES;
}
-(void) dealloc
{
DESTROY(name);
[super dealloc];
}
@end
static GSFontMapKey *
keyForFont(NSString *name, const float *matrix, BOOL fix,
BOOL screenFont, int role)
{
GSFontMapKey *d;
d=[GSFontMapKey alloc];
d->name = [name copy];
d->screenFont = screenFont;
d->role = role;
d->fix = fix;
d->matrix[0] = matrix[0] * 1000;
d->matrix[1] = matrix[1] * 1000;
d->matrix[2] = matrix[2] * 1000;
d->matrix[3] = matrix[3] * 1000;
d->matrix[4] = matrix[4] * 1000;
d->matrix[5] = matrix[5] * 1000;
d->hash = [d->name hash] + screenFont + role * 4 + fix * 2
+ d->matrix[0] + d->matrix[1] + d->matrix[2] + d->matrix[3];
return d;
} }
/** /**
@ -632,16 +684,16 @@ static void setNSFont(NSString *key, NSFont *font)
screenFont: (BOOL)screen screenFont: (BOOL)screen
role: (int)aRole role: (int)aRole
{ {
NSString *nameWithMatrix; GSFontMapKey *key;
NSFont *font; NSFont *font;
/* Should never be called on an initialised font! */ /* Should never be called on an initialised font! */
NSAssert(fontName == nil, NSInternalInconsistencyException); NSAssert(fontName == nil, NSInternalInconsistencyException);
/* Check whether the font is cached */ /* Check whether the font is cached */
nameWithMatrix = newNameWithMatrix(name, fontMatrix, explicitlySet, key = keyForFont(name, fontMatrix, explicitlySet,
screen, aRole); screen, aRole);
font = (id)NSMapGet(globalFontMap, (void*)nameWithMatrix); font = (id)NSMapGet(globalFontMap, (void *)key);
if (font == nil) if (font == nil)
{ {
if (self == placeHolder) if (self == placeHolder)
@ -661,16 +713,18 @@ static void setNSFont(NSString *key, NSFont *font)
fontInfo = RETAIN([GSFontInfo fontInfoForFontName: fontName fontInfo = RETAIN([GSFontInfo fontInfoForFontName: fontName
matrix: fontMatrix matrix: fontMatrix
screenFont: screen]); screenFont: screen]);
if (!screenFont)
cachedScreenFont = placeHolder;
if (fontInfo == nil) if (fontInfo == nil)
{ {
DESTROY(fontName); DESTROY(fontName);
DESTROY(nameWithMatrix); DESTROY(key);
RELEASE(self); RELEASE(self);
return nil; return nil;
} }
/* Cache the font for later use */ /* Cache the font for later use */
NSMapInsert(globalFontMap, (void*)nameWithMatrix, (void*)self); NSMapInsert(globalFontMap, (void *)key, (void *)self);
} }
else else
{ {
@ -680,7 +734,7 @@ static void setNSFont(NSString *key, NSFont *font)
} }
self = RETAIN(font); self = RETAIN(font);
} }
RELEASE(nameWithMatrix); RELEASE(key);
return self; return self;
} }
@ -689,16 +743,18 @@ static void setNSFont(NSString *key, NSFont *font)
{ {
if (fontName != nil) if (fontName != nil)
{ {
NSString *nameWithMatrix; GSFontMapKey *key;
nameWithMatrix = newNameWithMatrix(fontName, matrix, key = keyForFont(fontName, matrix,
matrixExplicitlySet, screenFont, matrixExplicitlySet, screenFont,
role); role);
NSMapRemove(globalFontMap, (void*)nameWithMatrix); NSMapRemove(globalFontMap, (void *)key);
RELEASE(nameWithMatrix); RELEASE(key);
RELEASE(fontName); RELEASE(fontName);
} }
TEST_RELEASE(fontInfo); TEST_RELEASE(fontInfo);
if (cachedScreenFont != placeHolder)
DESTROY(cachedScreenFont);
[super dealloc]; [super dealloc];
} }
@ -707,8 +763,12 @@ static void setNSFont(NSString *key, NSFont *font)
NSString *nameWithMatrix; NSString *nameWithMatrix;
NSString *description; NSString *description;
nameWithMatrix = newNameWithMatrix(fontName, matrix, matrixExplicitlySet, nameWithMatrix = [[NSString alloc] initWithFormat:
screenFont, role); @"%@ %.3f %.3f %.3f %.3f %.3f %.3f %c %c %i", fontName,
matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5],
(matrixExplicitlySet == NO) ? 'N' : 'Y',
screenFont ? 'S' : 'P',
role];
description = [[super description] stringByAppendingFormat: @" %@", description = [[super description] stringByAppendingFormat: @" %@",
nameWithMatrix]; nameWithMatrix];
RELEASE(nameWithMatrix); RELEASE(nameWithMatrix);
@ -737,7 +797,7 @@ static void setNSFont(NSString *key, NSFont *font)
int i, sum; int i, sum;
sum = 0; sum = 0;
for (i = 0; i < 6; i++) for (i = 0; i < 6; i++)
sum += matrix[i]* ((i+1)* 17); sum += matrix[i] * ((i+1) * 17);
return ([fontName hash] + sum); return ([fontName hash] + sum);
} }
@ -811,21 +871,28 @@ static BOOL flip_hack;
{ {
if (!screenFont) if (!screenFont)
return self; return self;
return [placeHolder initWithName: fontName return AUTORELEASE([placeHolder initWithName: fontName
matrix: matrix matrix: matrix
fix: matrixExplicitlySet fix: matrixExplicitlySet
screenFont: NO screenFont: NO
role: role]; role: role]);
} }
- (NSFont*) screenFont - (NSFont*) screenFont
{ {
if (screenFont) if (screenFont)
return self; return self;
return [placeHolder initWithName: fontName /*
If we haven't already created the real screen font instance, do so now.
Note that if the font has no corresponding screen font, cachedScreenFont
will be set to nil.
*/
if (cachedScreenFont == placeHolder)
cachedScreenFont = [placeHolder initWithName: fontName
matrix: matrix matrix: matrix
fix: matrixExplicitlySet fix: matrixExplicitlySet
screenFont: YES screenFont: YES
role: role]; role: role];
return AUTORELEASE(RETAIN(cachedScreenFont));
} }
- (float) ascender { return [fontInfo ascender]; } - (float) ascender { return [fontInfo ascender]; }