summaryrefslogtreecommitdiff
path: root/libnautilus-private/nautilus-font-factory.c
blob: 136a6171cc5d3cfc532312948327c0f183556fe1 (plain)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-

   nautilus-font-factory.c: Class for obtaining fonts.
 
   Copyright (C) 2000 Eazel, Inc.
  
   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.
  
   This program 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
   General Public License for more details.
  
   You should have received a copy of the GNU General Public
   License along with this program; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
  
   Authors: Ramiro Estrugo <ramiro@eazel.com>
*/

#include <config.h>
#include "nautilus-font-factory.h"

#include "nautilus-global-preferences.h"
#include "nautilus-gtk-macros.h"
#include "nautilus-string.h"
#include <pthread.h>
#include <unistd.h>

#define NAUTILUS_TYPE_FONT_FACTORY \
	(nautilus_font_factory_get_type ())
#define NAUTILUS_FONT_FACTORY(obj) \
	(GTK_CHECK_CAST ((obj), NAUTILUS_TYPE_FONT_FACTORY, NautilusFontFactory))
#define NAUTILUS_FONT_FACTORY_CLASS(klass) \
	(GTK_CHECK_CLASS_CAST ((klass), NAUTILUS_TYPE_FONT_FACTORY, NautilusFontFactoryClass))
#define NAUTILUS_IS_FONT_FACTORY(obj) \
	(GTK_CHECK_TYPE ((obj), NAUTILUS_TYPE_FONT_FACTORY))
#define NAUTILUS_IS_FONT_FACTORY_CLASS(klass) \
	(GTK_CHECK_CLASS_TYPE ((klass), NAUTILUS_TYPE_FONT_FACTORY))

/* The font factory */
typedef struct {
	GtkObject object;

	GHashTable *fonts;
} NautilusFontFactory;

typedef struct {
	GtkObjectClass parent_class;
} NautilusFontFactoryClass;

/* FontHashNode */
typedef struct {
	char		*name;
	GdkFont		*font;
} FontHashNode;

static GdkFont *fixed_font;
static NautilusFontFactory *global_font_factory = NULL;

static GtkType nautilus_font_factory_get_type         (void);
static void    nautilus_font_factory_initialize_class (NautilusFontFactoryClass *class);
static void    nautilus_font_factory_initialize       (NautilusFontFactory      *factory);
static void    destroy                                (GtkObject                *object);

NAUTILUS_DEFINE_CLASS_BOILERPLATE (NautilusFontFactory,
				   nautilus_font_factory,
				   GTK_TYPE_OBJECT)

static void
unref_global_font_factory (void)
{
	gtk_object_unref (GTK_OBJECT (global_font_factory));
}

/* Return a pointer to the single global font factory. */
static NautilusFontFactory *
nautilus_get_current_font_factory (void)
{
        if (global_font_factory == NULL) {
		global_font_factory = NAUTILUS_FONT_FACTORY (gtk_object_new (nautilus_font_factory_get_type (), NULL));
		gtk_object_ref (GTK_OBJECT (global_font_factory));
		gtk_object_sink (GTK_OBJECT (global_font_factory));
		g_atexit (unref_global_font_factory);
        }

        return global_font_factory;
}

GtkObject *
nautilus_font_factory_get (void)
{
	return GTK_OBJECT (nautilus_get_current_font_factory ());
}

static void
nautilus_font_factory_initialize (NautilusFontFactory *factory)
{
        factory->fonts = g_hash_table_new (g_str_hash, g_str_equal);
}

static void
nautilus_font_factory_initialize_class (NautilusFontFactoryClass *class)
{
	GtkObjectClass *object_class;

	object_class = GTK_OBJECT_CLASS (class);
	object_class->destroy = destroy;
}

static FontHashNode *
font_hash_node_alloc (const char *name)
{
	FontHashNode *node;
	
	g_assert (name != NULL);

	node = g_new0 (FontHashNode, 1);
	node->name = g_strdup (name);

	return node;
}

static void
font_hash_node_free (FontHashNode *node)
{
	g_assert (node != NULL);

	g_free (node->name);
	gdk_font_unref (node->font);

	g_free (node);
}

static void
free_one_hash_node (gpointer key, gpointer value, gpointer callback_data)
{
	FontHashNode *node;

	g_assert (key != NULL);
	g_assert (value != NULL);
	g_assert (callback_data == NULL);

	node = value;

	g_assert (node->name == key);

	font_hash_node_free (node);
}

static void
destroy (GtkObject *object)
{
	NautilusFontFactory *factory;

	factory = NAUTILUS_FONT_FACTORY (object);

	g_hash_table_foreach (factory->fonts, free_one_hash_node, NULL);
	g_hash_table_destroy (factory->fonts);

	NAUTILUS_CALL_PARENT_CLASS (GTK_OBJECT_CLASS, destroy, (object));
}

static FontHashNode *
font_hash_node_lookup (const char *name)
{
	NautilusFontFactory *factory;
	
	g_assert (name != NULL);

	factory = nautilus_get_current_font_factory ();
	return (FontHashNode *) g_hash_table_lookup (factory->fonts, name);
}

static FontHashNode *
font_hash_node_lookup_with_insertion (const char *name)
{
	NautilusFontFactory *factory;
	FontHashNode *node;
	GdkFont *font;

	g_assert (name != NULL);

	factory = nautilus_get_current_font_factory ();
	node = font_hash_node_lookup (name);

	if (node == NULL) {
		font = gdk_font_load (name);
		
		if (font != NULL) {
			node = font_hash_node_alloc (name);
			node->font = font;
			g_hash_table_insert (factory->fonts, node->name, node);
		}
	}
	
	return node;
}

static char *
make_font_name_string (const char *foundry,
		       const char *family,
		       const char *weight,
		       const char *slant,
		       const char *set_width,
		       const char *add_style,
		       guint size_in_pixels)
{
	char *font_name;

        const char *points = "*";
        const char *hor_res = "*";
        const char *ver_res = "*";
        const char *spacing = "*";
        const char *average_width = "*";
        const char *char_set_registry = "*";
        const char *char_set_encoding = "*";


	/*                             +---------------------------------------------------- foundry
	                               |  +------------------------------------------------- family
				       |  |  +---------------------------------------------- weight
				       |  |  |  +------------------------------------------- slant 
				       |  |  |  |  +---------------------------------------- sel_width
				       |  |  |  |  |  +------------------------------------- add-style
				       |  |  |  |  |  |  +---------------------------------- pixels   	
				       |  |  |  |  |  |  |  +------------------------------- points  
				       |  |  |  |  |  |  |  |  +---------------------------- hor_res        
				       |  |  |  |  |  |  |  |  |  +------------------------- ver_res        
				       |  |  |  |  |  |  |  |  |  |  +---------------------- spacing        
				       |  |  |  |  |  |  |  |  |  |  |  +------------------- average_width        
				       |  |  |  |  |  |  |  |  |  |  |  |  +---------------- char_set_registry
				       |  |  |  |  |  |  |  |  |  |  |  |  |  +------------- char_set_encoding */
	font_name = g_strdup_printf ("-%s-%s-%s-%s-%s-%s-%d-%s-%s-%s-%s-%s-%s-%s",
				     foundry,
				     family,
				     weight,
				     slant,
				     set_width,
				     add_style,
				     size_in_pixels,
				     points,
				     hor_res,
				     ver_res,
				     spacing,
				     average_width,
				     char_set_registry,
				     char_set_encoding);
	
	return font_name;
}

/* Public functions */
GdkFont *
nautilus_font_factory_get_font_by_family (const char *family,
					  guint       size_in_pixels)
{
	NautilusFontFactory *factory;
	GdkFont *font;
	FontHashNode *node;
	char *font_name;

	g_return_val_if_fail (family != NULL, NULL);
	g_return_val_if_fail (size_in_pixels > 0, NULL);

	factory = nautilus_get_current_font_factory ();
	font_name = make_font_name_string ("*", 
					   family,
					   "medium",
					   "r",
					   "normal",
					   "*",
					   size_in_pixels);

	node = font_hash_node_lookup_with_insertion (font_name);

	if (node != NULL) {
		g_assert (node->font != NULL);
		font = node->font;
		gdk_font_ref (font);
	} else {
		font = nautilus_font_factory_get_fallback_font ();
	}

	g_free (font_name);

	return font;
}

GdkFont *
nautilus_font_factory_get_font_from_preferences (guint size_in_pixels)
{
	char	*family;
	GdkFont *font;

	family = nautilus_preferences_get (NAUTILUS_PREFERENCES_DIRECTORY_VIEW_FONT_FAMILY, "helvetica");

	font = nautilus_font_factory_get_font_by_family (family, size_in_pixels);

	g_free (family);

	return font;
}

static void
unref_fixed_font (void)
{
	gdk_font_unref (fixed_font);
}

GdkFont *
nautilus_font_factory_get_fallback_font (void)
{
	if (fixed_font == NULL) {
		fixed_font = gdk_font_load ("fixed");
		g_assert (fixed_font != NULL);
		g_atexit (unref_fixed_font);
	}

	gdk_font_ref (fixed_font);
	return fixed_font;
}