summaryrefslogtreecommitdiff
path: root/src/glibext/gfontcache.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/glibext/gfontcache.c')
-rw-r--r--src/glibext/gfontcache.c294
1 files changed, 294 insertions, 0 deletions
diff --git a/src/glibext/gfontcache.c b/src/glibext/gfontcache.c
new file mode 100644
index 0000000..2441d27
--- /dev/null
+++ b/src/glibext/gfontcache.c
@@ -0,0 +1,294 @@
+
+/* OpenIDA - Outil d'analyse de fichiers binaires
+ * gfontcache.c - mise en cache avec actualisation des polices
+ *
+ * Copyright (C) 2012 Cyrille Bagard
+ *
+ * This file is part of OpenIDA.
+ *
+ * OpenIDA 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * OpenIDA 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#include "gfontcache.h"
+
+
+
+/* ----------------------- MISE EN PLACE D'UN CONTEXTE GLOBAL ----------------------- */
+
+
+/* Utilisable par toutes les impressions. */
+static PangoContext *_context = NULL;
+
+
+
+/* ---------------------------- CACHE DE POLICES PAR VUE ---------------------------- */
+
+
+/* Description d'un cache pour polices (instance) */
+struct _GFontCache
+{
+ GObject parent; /* A laisser en premier */
+
+ GHashTable *table; /* Associations desc./police */
+
+ GtkWidget *widget;
+
+};
+
+/* Description d'un cache pour polices (classe) */
+struct _GFontCacheClass
+{
+ GObjectClass parent; /* A laisser en premier */
+
+};
+
+
+/* Initialise la classe des caches pour polices. */
+static void g_font_cache_class_init(GFontCacheClass *);
+
+/* Initialise un cache pour polices. */
+static void g_font_cache_init(GFontCache *);
+
+/* Supprime toutes les références externes. */
+static void g_font_cache_dispose(GFontCache *);
+
+/* Procède à la libération totale de la mémoire. */
+static void g_font_cache_finalize(GFontCache *);
+
+
+
+/* ---------------------------------------------------------------------------------- */
+/* MISE EN PLACE D'UN CONTEXTE GLOBAL */
+/* ---------------------------------------------------------------------------------- */
+
+
+/******************************************************************************
+* *
+* Paramètres : - *
+* *
+* Description : Initialise le contexte Pango pour la gestion des textes. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void init_global_pango_context(void)
+{
+ PangoFontDescription *desc; /* Police de caractères */
+
+ _context = gdk_pango_context_get();
+
+ desc = pango_font_description_from_string("mono 10");
+ pango_context_set_font_description(_context, desc);
+ pango_font_description_free(desc);
+
+ pango_context_set_base_dir(_context, PANGO_DIRECTION_LTR);
+ pango_context_set_language(_context, gtk_get_default_language());
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : - *
+* *
+* Description : Fournit le contexte Pango global pour l'impression. *
+* *
+* Retour : Contexte prêt à emploi. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+PangoContext *get_global_pango_context(void)
+{
+ return _context;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : - *
+* *
+* Description : Supprime le contexte Pango pour la gestion des textes. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void exit_global_pango_context(void)
+{
+ g_object_unref(G_OBJECT(_context));
+
+}
+
+
+
+/* ---------------------------------------------------------------------------------- */
+/* CACHE DE POLICES PAR VUE */
+/* ---------------------------------------------------------------------------------- */
+
+
+
+/* Indique le type défini pour un cache pour polices. */
+G_DEFINE_TYPE(GFontCache, g_font_cache, G_TYPE_OBJECT);
+
+
+/******************************************************************************
+* *
+* Paramètres : class = classe à initialiser. *
+* *
+* Description : Initialise la classe des caches pour polices. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_font_cache_class_init(GFontCacheClass *class)
+{
+ GObjectClass *object; /* Autre version de la classe */
+
+ object = G_OBJECT_CLASS(class);
+
+ object->dispose = (GObjectFinalizeFunc/* ! */)g_font_cache_dispose;
+ object->finalize = (GObjectFinalizeFunc)g_font_cache_finalize;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : fcache = instance à initialiser. *
+* *
+* Description : Initialise un cache pour polices. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_font_cache_init(GFontCache *fcache)
+{
+ fcache->table = g_hash_table_new_full((GHashFunc)pango_font_description_hash,
+ (GEqualFunc)pango_font_description_equal,
+ (GDestroyNotify)pango_font_description_free,
+ (GDestroyNotify)g_object_unref);
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : fcache = instance d'objet GLib à traiter. *
+* *
+* Description : Supprime toutes les références externes. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_font_cache_dispose(GFontCache *fcache)
+{
+ g_hash_table_destroy(fcache->table);
+
+ G_OBJECT_CLASS(g_font_cache_parent_class)->dispose(G_OBJECT(fcache));
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : fcache = instance d'objet GLib à traiter. *
+* *
+* Description : Procède à la libération totale de la mémoire. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void g_font_cache_finalize(GFontCache *fcache)
+{
+ G_OBJECT_CLASS(g_font_cache_parent_class)->finalize(G_OBJECT(fcache));
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : - *
+* *
+* Description : Crée un bloc virtuel d'instructions. *
+* *
+* Retour : Adresse de la structure mise en place. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+GFontCache *g_font_cache_new(void)
+{
+ GFontCache *result; /* Structure à retourner */
+
+ result = g_object_new(G_TYPE_FONT_CACHE, NULL);
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : fcache = instance à consulter, voire compléter. *
+* desc = description de la police recherchée. *
+* *
+* Description : Fournit une police utilisable répondant au besoin. *
+* *
+* Retour : Police prête à usage. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+PangoFont *g_font_cache_lookup(GFontCache *fcache, const PangoFontDescription *desc)
+{
+ PangoFont *result; /* Instance à retourner */
+ PangoFontDescription *key; /* Copie de la description */
+
+ result = (PangoFont *)g_hash_table_lookup(fcache->table, desc);
+
+ if (result == NULL)
+ {
+ result = pango_context_load_font(get_global_pango_context(), desc);
+
+ key = pango_font_description_copy(desc);
+ g_hash_table_insert(fcache->table, key, result);
+
+ }
+
+ return result;
+
+}