Fix caching.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@14588 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
CaS 2002-09-27 08:52:04 +00:00
parent ab43ceed49
commit bbe972eef7
3 changed files with 63 additions and 51 deletions

View file

@ -1,3 +1,8 @@
2002-09-27 Richard Frith-Macdonald <rfm@gnu.org>
* 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 <FredKiefer@gmx.de> 2002-09-26 Fred Kiefer <FredKiefer@gmx.de>
* Headers/gnustep/gui/NSBrowserCell.h * Headers/gnustep/gui/NSBrowserCell.h

View file

@ -105,7 +105,7 @@ static GSFontEnumerator *sharedEnumerator = nil;
matrix: (const float *)fmatrix; matrix: (const float *)fmatrix;
{ {
return AUTORELEASE([[fontInfoClass alloc] initWithFontName: nfontName return AUTORELEASE([[fontInfoClass alloc] initWithFontName: nfontName
matrix: fmatrix]); matrix: fmatrix]);
} }
+ (int) weightForString: (NSString *)weightString + (int) weightForString: (NSString *)weightString
@ -116,35 +116,35 @@ static GSFontEnumerator *sharedEnumerator = nil;
if (dict == nil) if (dict == nil)
{ {
dict = [NSDictionary dictionaryWithObjectsAndKeys: dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt: 1], @"ultralight", [NSNumber numberWithInt: 1], @"ultralight",
[NSNumber numberWithInt: 2], @"thin", [NSNumber numberWithInt: 2], @"thin",
[NSNumber numberWithInt: 3], @"light", [NSNumber numberWithInt: 3], @"light",
[NSNumber numberWithInt: 3], @"extralight", [NSNumber numberWithInt: 3], @"extralight",
[NSNumber numberWithInt: 4], @"book", [NSNumber numberWithInt: 4], @"book",
[NSNumber numberWithInt: 5], @"regular", [NSNumber numberWithInt: 5], @"regular",
[NSNumber numberWithInt: 5], @"plain", [NSNumber numberWithInt: 5], @"plain",
[NSNumber numberWithInt: 5], @"display", [NSNumber numberWithInt: 5], @"display",
[NSNumber numberWithInt: 5], @"roman", [NSNumber numberWithInt: 5], @"roman",
[NSNumber numberWithInt: 5], @"semilight", [NSNumber numberWithInt: 5], @"semilight",
[NSNumber numberWithInt: 6], @"medium", [NSNumber numberWithInt: 6], @"medium",
[NSNumber numberWithInt: 7], @"demi", [NSNumber numberWithInt: 7], @"demi",
[NSNumber numberWithInt: 7], @"demibold", [NSNumber numberWithInt: 7], @"demibold",
[NSNumber numberWithInt: 8], @"semi", [NSNumber numberWithInt: 8], @"semi",
[NSNumber numberWithInt: 8], @"semibold", [NSNumber numberWithInt: 8], @"semibold",
[NSNumber numberWithInt: 9], @"bold", [NSNumber numberWithInt: 9], @"bold",
[NSNumber numberWithInt: 10], @"extra", [NSNumber numberWithInt: 10], @"extra",
[NSNumber numberWithInt: 10], @"extrabold", [NSNumber numberWithInt: 10], @"extrabold",
[NSNumber numberWithInt: 11], @"heavy", [NSNumber numberWithInt: 11], @"heavy",
[NSNumber numberWithInt: 11], @"heavyface", [NSNumber numberWithInt: 11], @"heavyface",
[NSNumber numberWithInt: 12], @"ultrabold", [NSNumber numberWithInt: 12], @"ultrabold",
[NSNumber numberWithInt: 12], @"black", [NSNumber numberWithInt: 12], @"black",
[NSNumber numberWithInt: 13], @"ultra", [NSNumber numberWithInt: 13], @"ultra",
[NSNumber numberWithInt: 13], @"ultrablack", [NSNumber numberWithInt: 13], @"ultrablack",
[NSNumber numberWithInt: 13], @"fat", [NSNumber numberWithInt: 13], @"fat",
[NSNumber numberWithInt: 14], @"extrablack", [NSNumber numberWithInt: 14], @"extrablack",
[NSNumber numberWithInt: 14], @"obese", [NSNumber numberWithInt: 14], @"obese",
[NSNumber numberWithInt: 14], @"nord", [NSNumber numberWithInt: 14], @"nord",
nil]; nil];
RETAIN(dict); RETAIN(dict);
} }
@ -166,11 +166,11 @@ static GSFontEnumerator *sharedEnumerator = nil;
if (arr == nil) if (arr == nil)
{ {
arr = [NSArray arrayWithObjects: @"", @"ultralight", arr = [NSArray arrayWithObjects: @"", @"ultralight",
@"thin", @"light", @"book", @"regular", @"thin", @"light", @"book", @"regular",
@"medium", @"demibold", @"semibold", @"medium", @"demibold", @"semibold",
@"bold", @"extrabold", @"heavy", @"bold", @"extrabold", @"heavy",
@"black", @"ultrablack", @"extrablack", @"black", @"ultrablack", @"extrablack",
nil]; nil];
RETAIN(arr); RETAIN(arr);
} }
@ -277,7 +277,7 @@ static GSFontEnumerator *sharedEnumerator = nil;
[super dealloc]; [super dealloc];
} }
- copyWithZone: (NSZone *)zone - (id) copyWithZone: (NSZone *)zone
{ {
GSFontInfo *copy; GSFontInfo *copy;
if (NSShouldRetainWithZone(self, zone)) 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 /* We really want a mutable class for this, but this is quick and easy since
it's not really a public class anyway */ it's not really a public class anyway */
- mutableCopyWithZone: (NSZone *)zone - (id) mutableCopyWithZone: (NSZone *)zone
{ {
GSFontInfo *copy; GSFontInfo *copy;
copy = (GSFontInfo*) NSCopyObject (self, 0, zone); copy = (GSFontInfo*) NSCopyObject (self, 0, zone);
@ -406,7 +406,7 @@ static GSFontEnumerator *sharedEnumerator = nil;
return xHeight; return xHeight;
} }
- (float)defaultLineHeightForFont - (float) defaultLineHeightForFont
{ {
// ascent plus descent plus some suitable linegap // ascent plus descent plus some suitable linegap
return [self ascender] - [self descender] + [self pointSize]/ 11.0; return [self ascender] - [self descender] + [self pointSize]/ 11.0;

View file

@ -30,6 +30,7 @@
#include <Foundation/NSString.h> #include <Foundation/NSString.h>
#include <Foundation/NSUserDefaults.h> #include <Foundation/NSUserDefaults.h>
#include <Foundation/NSSet.h> #include <Foundation/NSSet.h>
#include <Foundation/NSMapTable.h>
#include <AppKit/NSGraphicsContext.h> #include <AppKit/NSGraphicsContext.h>
#include <AppKit/NSFont.h> #include <AppKit/NSFont.h>
@ -115,9 +116,8 @@ NSArray *_preferredFonts;
/* Class for fonts */ /* Class for fonts */
static Class NSFontClass = 0; static Class NSFontClass = 0;
/* Store all created fonts for reuse. /* Cache all created fonts for reuse. */
ATTENTION: This way a font will never get freed! */ static NSMapTable* globalFontMap = 0;
static NSMutableDictionary* globalFontDictionary = nil;
static NSUserDefaults *defaults = nil; static NSUserDefaults *defaults = nil;
@ -149,7 +149,7 @@ setNSFont(NSString* key, NSFont* font)
userCacheNeedsRecomputing = YES; userCacheNeedsRecomputing = YES;
userFixedCacheNeedsRecomputing = YES; userFixedCacheNeedsRecomputing = YES;
/* Don't care about errors*/ /* Don't care about errors */
[defaults synchronize]; [defaults synchronize];
} }
@ -161,7 +161,8 @@ setNSFont(NSString* key, NSFont* font)
if (self == [NSFont class]) if (self == [NSFont class])
{ {
NSFontClass = self; NSFontClass = self;
globalFontDictionary = [NSMutableDictionary new]; globalFontMap = NSCreateMapTable(NSObjectMapKeyCallBacks,
NSNonRetainedObjectMapValueCallBacks, 64);
if (defaults == nil) if (defaults == nil)
{ {
@ -483,19 +484,18 @@ setNSFont(NSString* key, NSFont* font)
NSString *nameWithMatrix; NSString *nameWithMatrix;
nameWithMatrix = [NSString stringWithFormat: nameWithMatrix = [NSString stringWithFormat:
@"%@ %.3f %.3f %.3f %.3f %.3f %.3f", @"%@ %.3f %.3f %.3f %.3f %.3f %.3f", aFontName,
aFontName, fontMatrix[0], fontMatrix[1], fontMatrix[2],
fontMatrix[0], fontMatrix[1], fontMatrix[2], fontMatrix[3], fontMatrix[4], fontMatrix[5]];
fontMatrix[3], fontMatrix[4], fontMatrix[5]];
/* Check whether the font is cached */ /* Check whether the font is cached */
font = [globalFontDictionary objectForKey: nameWithMatrix]; font = (id)NSMapGet(globalFontMap, (void*)nameWithMatrix);
if(font == nil) if (font == nil)
{ {
font = AUTORELEASE([[NSFontClass alloc] initWithName: aFontName font = AUTORELEASE([[NSFontClass alloc] initWithName: aFontName
matrix: fontMatrix]); matrix: fontMatrix]);
/* Cache the font for later use */ /* Cache the font for later use */
[globalFontDictionary setObject: font forKey: nameWithMatrix]; NSMapInsert(globalFontMap, (void*)nameWithMatrix, (void*)font);
} }
return font; return font;
@ -555,6 +555,13 @@ setNSFont(NSString* key, NSFont* font)
- (void) dealloc - (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(fontName);
RELEASE(fontInfo); RELEASE(fontInfo);
[super dealloc]; [super dealloc];