File: XGFontManager.m

package info (click to toggle)
gnustep-back 0.18.0-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,920 kB
  • ctags: 1,015
  • sloc: objc: 36,805; ansic: 6,054; makefile: 218; sh: 151
file content (212 lines) | stat: -rw-r--r-- 5,939 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
   XGFontManager.m

   NSFontManager helper for GNUstep GUI X/GPS Backend

   Copyright (C) 1996 Free Software Foundation, Inc.

   Author: Ovidiu Predescu <ovidiu@bx.logicnet.ro>
   Date: February 1997
   A completely rewritten version of the original source of Scott Christley.
   Modified:  Fred Kiefer <FredKiefer@gmx.de>
   Date: Febuary 2000
   Added some X calls and changed the overall structure
 
   This file is part of the GNUstep GUI X/GPS Library.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; see the file COPYING.LIB.
   If not, see <http://www.gnu.org/licenses/> or write to the 
   Free Software Foundation, 51 Franklin Street, Fifth Floor, 
   Boston, MA 02110-1301, USA.
*/

#include <config.h>
#include <stdio.h>

#include <GNUstepGUI/GSFontInfo.h>
#include <Foundation/NSArchiver.h>
#include <Foundation/NSBundle.h>
#include <Foundation/NSException.h>
#include <Foundation/NSFileManager.h>
#include <Foundation/NSPathUtilities.h>
#include <Foundation/NSProcessInfo.h>
#include <Foundation/NSTask.h>
#include <Foundation/NSValue.h>
#include "xlib/XGContext.h"
#include "xlib/XGPrivate.h"
#include "x11/XGServer.h"

#define stringify_it(X) #X

static NSMutableDictionary* creationDictionary;

// Fills in the size into an creation string to make it an X font name
NSString *XGXFontName(NSString *fontName, float size)
{
  NSString *creationName = [creationDictionary objectForKey: fontName];

  if (creationName != nil)
    return [NSString stringWithFormat: creationName, (int)size];
  else
    return nil;
}

@implementation XGFontEnumerator

static NSDictionary	*cache;

static NSString* 
cache_name()
{
  static NSString *cacheName = nil;

  if (cacheName == nil)
    {
      NSFileManager *mgr;
      BOOL flag;
      Display *dpy = [XGServer currentXDisplay];
      NSString *file_name;
      NSArray *paths;
      NSString *path = nil;

      file_name = XGFontCacheName(dpy);

      paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
                                                  NSUserDomainMask, YES);
      if ((paths != nil) && ([paths count] > 0))
        {
          path = [paths objectAtIndex: 0];
        }
      /*
       * If standard search paths are not set up, try a default location.
       */
      if (path == nil)
	{
	  path = [[NSHomeDirectory() stringByAppendingPathComponent:
	    @"GNUstep"] stringByAppendingPathComponent: @"Library"];
	}

      mgr = [NSFileManager defaultManager];
      if ([mgr fileExistsAtPath: path isDirectory: &flag] == NO || flag == NO)
	{
	  NSLog(@"Library directory '%@' not available!", path);
	  return NO;
	}
      path = [path stringByAppendingPathComponent: @"Fonts"];
      if ([mgr fileExistsAtPath: path] == NO)
	{
	  [mgr createDirectoryAtPath: path attributes: nil];
	}
      if ([mgr fileExistsAtPath: path isDirectory: &flag] == NO || flag == NO)
	{
	  NSLog(@"Fonts directory '%@' not available!", path);
	  return NO;
	}
      path = [path stringByAppendingPathComponent: @"Cache"];
      if ([mgr fileExistsAtPath: path] == NO)
	{
	  [mgr createDirectoryAtPath: path attributes: nil];
	}
      if ([mgr fileExistsAtPath: path isDirectory: &flag] == NO || flag == NO)
	{
	  NSLog(@"Fonts directory '%@' not available!", path);
	  return NO;
	}
      cacheName = [path stringByAppendingPathComponent: file_name];
      RETAIN(cacheName);
    }

  return cacheName;
}

static BOOL
load_cache(NSString *cacheName, BOOL async)
{
  NSNumber	*v;
  id		o;

  NS_DURING
    {
      o = [NSUnarchiver unarchiveObjectWithFile: cacheName];
    }
  NS_HANDLER
    {
      NSLog(@"Exception while attempting to load font cache file %@ - %@: %@",
	    cacheName, [localException name], [localException reason]);
      o = nil;
    }
  NS_ENDHANDLER

  if ((o == nil)
    || ((v = [o objectForKey: @"Version"]) == nil)
    || ([v intValue] != 3))
    {
      NSString *file_name = [cacheName lastPathComponent];
      NSString *path;
      NSTask *task;
   
      if (async == NO)
	{
	  NSLog(@"No font cache available - building new one - this may "
	    @"take several seconds (or minutes on a slow machine with "
	    @"lots of fonts)");
	}
      path = [NSBundle _absolutePathOfExecutable: @"font_cacher"];
      if (path == nil)
	{
	  NSLog(@"Could not find font_cacher program. Please run manually");
	  return NO;
	}
      NSLog(@"Running %@", path);
      task = [NSTask launchedTaskWithLaunchPath: path
	arguments: [NSArray arrayWithObject: file_name]];
      if (task == nil || async == YES)
	{
	  return NO;
	}
      [task waitUntilExit];
      o = [NSUnarchiver unarchiveObjectWithFile: cacheName];
      if (o == nil)
	{
	  NSLog(@"Error - font cache doesn't exist");
	  return NO;
	}
    }
  else
    {
      // Ensure archive is written in latest format.
      // No longer needed as NSString coding format did not change in 
      // the last two years. 
      //[NSArchiver archiveRootObject: o toFile: cacheName];
    }

  ASSIGN(cache, o);
  return YES;
}

- (void) enumerateFontsAndFamilies
{
  if (cache == nil)
    {
      if (load_cache(cache_name(), NO))
        {
	  allFontNames = RETAIN([[cache objectForKey: @"AllFontNames"] allObjects]);
	  allFontFamilies = [cache objectForKey: @"AllFontFamilies"];
	  // This dictionary stores the XLFD for each font
	  creationDictionary = [cache objectForKey: @"CreationDictionary"];
	}
    }
}

@end