/* -*- 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 */ #include #include "nautilus-font-factory.h" #include "nautilus-global-preferences.h" #include "nautilus-gtk-macros.h" #include "nautilus-string.h" #include #include #include #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_fontset_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_fontset_load (_("fixed")); g_assert (fixed_font != NULL); g_atexit (unref_fixed_font); } gdk_font_ref (fixed_font); return fixed_font; }