From bbe972eef77236a6b3a966addff16b44298426d8 Mon Sep 17 00:00:00 2001 From: CaS Date: Fri, 27 Sep 2002 08:52:04 +0000 Subject: [PATCH] Fix caching. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@14588 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 5 +++ Source/GSFontInfo.m | 76 ++++++++++++++++++++++----------------------- Source/NSFont.m | 33 ++++++++++++-------- 3 files changed, 63 insertions(+), 51 deletions(-) diff --git a/ChangeLog b/ChangeLog index d78745cd5..7f4839f90 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2002-09-27 Richard Frith-Macdonald + + * Source/NSFont.m: Replace caching code which never released fonts + with a cache which only holds fonts as long as something is using them. + 2002-09-26 Fred Kiefer * Headers/gnustep/gui/NSBrowserCell.h diff --git a/Source/GSFontInfo.m b/Source/GSFontInfo.m index 7ddf0f453..c503c09a6 100644 --- a/Source/GSFontInfo.m +++ b/Source/GSFontInfo.m @@ -105,7 +105,7 @@ static GSFontEnumerator *sharedEnumerator = nil; matrix: (const float *)fmatrix; { return AUTORELEASE([[fontInfoClass alloc] initWithFontName: nfontName - matrix: fmatrix]); + matrix: fmatrix]); } + (int) weightForString: (NSString *)weightString @@ -116,35 +116,35 @@ static GSFontEnumerator *sharedEnumerator = nil; if (dict == nil) { dict = [NSDictionary dictionaryWithObjectsAndKeys: - [NSNumber numberWithInt: 1], @"ultralight", - [NSNumber numberWithInt: 2], @"thin", - [NSNumber numberWithInt: 3], @"light", - [NSNumber numberWithInt: 3], @"extralight", - [NSNumber numberWithInt: 4], @"book", - [NSNumber numberWithInt: 5], @"regular", - [NSNumber numberWithInt: 5], @"plain", - [NSNumber numberWithInt: 5], @"display", - [NSNumber numberWithInt: 5], @"roman", - [NSNumber numberWithInt: 5], @"semilight", - [NSNumber numberWithInt: 6], @"medium", - [NSNumber numberWithInt: 7], @"demi", - [NSNumber numberWithInt: 7], @"demibold", - [NSNumber numberWithInt: 8], @"semi", - [NSNumber numberWithInt: 8], @"semibold", - [NSNumber numberWithInt: 9], @"bold", - [NSNumber numberWithInt: 10], @"extra", - [NSNumber numberWithInt: 10], @"extrabold", - [NSNumber numberWithInt: 11], @"heavy", - [NSNumber numberWithInt: 11], @"heavyface", - [NSNumber numberWithInt: 12], @"ultrabold", - [NSNumber numberWithInt: 12], @"black", - [NSNumber numberWithInt: 13], @"ultra", - [NSNumber numberWithInt: 13], @"ultrablack", - [NSNumber numberWithInt: 13], @"fat", - [NSNumber numberWithInt: 14], @"extrablack", - [NSNumber numberWithInt: 14], @"obese", - [NSNumber numberWithInt: 14], @"nord", - nil]; + [NSNumber numberWithInt: 1], @"ultralight", + [NSNumber numberWithInt: 2], @"thin", + [NSNumber numberWithInt: 3], @"light", + [NSNumber numberWithInt: 3], @"extralight", + [NSNumber numberWithInt: 4], @"book", + [NSNumber numberWithInt: 5], @"regular", + [NSNumber numberWithInt: 5], @"plain", + [NSNumber numberWithInt: 5], @"display", + [NSNumber numberWithInt: 5], @"roman", + [NSNumber numberWithInt: 5], @"semilight", + [NSNumber numberWithInt: 6], @"medium", + [NSNumber numberWithInt: 7], @"demi", + [NSNumber numberWithInt: 7], @"demibold", + [NSNumber numberWithInt: 8], @"semi", + [NSNumber numberWithInt: 8], @"semibold", + [NSNumber numberWithInt: 9], @"bold", + [NSNumber numberWithInt: 10], @"extra", + [NSNumber numberWithInt: 10], @"extrabold", + [NSNumber numberWithInt: 11], @"heavy", + [NSNumber numberWithInt: 11], @"heavyface", + [NSNumber numberWithInt: 12], @"ultrabold", + [NSNumber numberWithInt: 12], @"black", + [NSNumber numberWithInt: 13], @"ultra", + [NSNumber numberWithInt: 13], @"ultrablack", + [NSNumber numberWithInt: 13], @"fat", + [NSNumber numberWithInt: 14], @"extrablack", + [NSNumber numberWithInt: 14], @"obese", + [NSNumber numberWithInt: 14], @"nord", + nil]; RETAIN(dict); } @@ -166,11 +166,11 @@ static GSFontEnumerator *sharedEnumerator = nil; if (arr == nil) { arr = [NSArray arrayWithObjects: @"", @"ultralight", - @"thin", @"light", @"book", @"regular", - @"medium", @"demibold", @"semibold", - @"bold", @"extrabold", @"heavy", - @"black", @"ultrablack", @"extrablack", - nil]; + @"thin", @"light", @"book", @"regular", + @"medium", @"demibold", @"semibold", + @"bold", @"extrabold", @"heavy", + @"black", @"ultrablack", @"extrablack", + nil]; RETAIN(arr); } @@ -277,7 +277,7 @@ static GSFontEnumerator *sharedEnumerator = nil; [super dealloc]; } -- copyWithZone: (NSZone *)zone +- (id) copyWithZone: (NSZone *)zone { GSFontInfo *copy; if (NSShouldRetainWithZone(self, zone)) @@ -295,7 +295,7 @@ static GSFontEnumerator *sharedEnumerator = nil; /* We really want a mutable class for this, but this is quick and easy since it's not really a public class anyway */ -- mutableCopyWithZone: (NSZone *)zone +- (id) mutableCopyWithZone: (NSZone *)zone { GSFontInfo *copy; copy = (GSFontInfo*) NSCopyObject (self, 0, zone); @@ -406,7 +406,7 @@ static GSFontEnumerator *sharedEnumerator = nil; return xHeight; } -- (float)defaultLineHeightForFont +- (float) defaultLineHeightForFont { // ascent plus descent plus some suitable linegap return [self ascender] - [self descender] + [self pointSize]/ 11.0; diff --git a/Source/NSFont.m b/Source/NSFont.m index a4b1f5412..778b217fb 100644 --- a/Source/NSFont.m +++ b/Source/NSFont.m @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -115,9 +116,8 @@ NSArray *_preferredFonts; /* Class for fonts */ static Class NSFontClass = 0; -/* Store all created fonts for reuse. - ATTENTION: This way a font will never get freed! */ -static NSMutableDictionary* globalFontDictionary = nil; +/* Cache all created fonts for reuse. */ +static NSMapTable* globalFontMap = 0; static NSUserDefaults *defaults = nil; @@ -149,7 +149,7 @@ setNSFont(NSString* key, NSFont* font) userCacheNeedsRecomputing = YES; userFixedCacheNeedsRecomputing = YES; - /* Don't care about errors*/ + /* Don't care about errors */ [defaults synchronize]; } @@ -161,7 +161,8 @@ setNSFont(NSString* key, NSFont* font) if (self == [NSFont class]) { NSFontClass = self; - globalFontDictionary = [NSMutableDictionary new]; + globalFontMap = NSCreateMapTable(NSObjectMapKeyCallBacks, + NSNonRetainedObjectMapValueCallBacks, 64); if (defaults == nil) { @@ -483,19 +484,18 @@ setNSFont(NSString* key, NSFont* font) NSString *nameWithMatrix; nameWithMatrix = [NSString stringWithFormat: - @"%@ %.3f %.3f %.3f %.3f %.3f %.3f", - aFontName, - fontMatrix[0], fontMatrix[1], fontMatrix[2], - fontMatrix[3], fontMatrix[4], fontMatrix[5]]; + @"%@ %.3f %.3f %.3f %.3f %.3f %.3f", aFontName, + fontMatrix[0], fontMatrix[1], fontMatrix[2], + fontMatrix[3], fontMatrix[4], fontMatrix[5]]; /* Check whether the font is cached */ - font = [globalFontDictionary objectForKey: nameWithMatrix]; - if(font == nil) + font = (id)NSMapGet(globalFontMap, (void*)nameWithMatrix); + if (font == nil) { font = AUTORELEASE([[NSFontClass alloc] initWithName: aFontName - matrix: fontMatrix]); + matrix: fontMatrix]); /* Cache the font for later use */ - [globalFontDictionary setObject: font forKey: nameWithMatrix]; + NSMapInsert(globalFontMap, (void*)nameWithMatrix, (void*)font); } return font; @@ -555,6 +555,13 @@ setNSFont(NSString* key, NSFont* font) - (void) dealloc { + NSString *nameWithMatrix = [NSString alloc]; + + nameWithMatrix = [nameWithMatrix initWithFormat: + @"%@ %.3f %.3f %.3f %.3f %.3f %.3f", fontName, + matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]]; + NSMapRemove(globalFontMap, (void*)nameWithMatrix); + RELEASE(nameWithMatrix); RELEASE(fontName); RELEASE(fontInfo); [super dealloc];