3 * Copyright (C) 2009 Jim Evins <evins@snaught.com>.
5 * This file is part of gLabels.
7 * gLabels is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * gLabels is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with gLabels. If not, see <http://www.gnu.org/licenses/>.
23 #include "font-util.h"
26 #include <pango/pango.h>
27 #include <pango/pangocairo.h>
29 #include <libglabels/libglabels.h>
32 /****************************************************************************/
33 /* Get list of all available font families. */
34 /****************************************************************************/
36 gl_font_util_get_all_families (void)
38 static GList *list = NULL;
39 PangoFontMap *fontmap;
40 PangoContext *context;
41 PangoFontFamily **families;
48 fontmap = pango_cairo_font_map_new ();
49 context = pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (fontmap));
51 pango_context_list_families (context, &families, &n);
55 name = g_strdup (pango_font_family_get_name (families[i]));
56 list = g_list_insert_sorted (list, name,
57 (GCompareFunc)lgl_str_utf8_casecmp);
62 g_object_unref (context);
63 g_object_unref (fontmap);
70 /****************************************************************************/
71 /* Get list of all available proportional font families. */
72 /****************************************************************************/
74 gl_font_util_get_proportional_families (void)
76 static GList *list = NULL;
77 PangoFontMap *fontmap;
78 PangoContext *context;
79 PangoFontFamily **families;
86 fontmap = pango_cairo_font_map_new ();
87 context = pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (fontmap));
89 pango_context_list_families (context, &families, &n);
93 if ( !pango_font_family_is_monospace (families[i]) )
95 name = g_strdup (pango_font_family_get_name (families[i]));
96 list = g_list_insert_sorted (list, name,
97 (GCompareFunc)lgl_str_utf8_casecmp);
103 g_object_unref (context);
104 g_object_unref (fontmap);
111 /****************************************************************************/
112 /* Get list of all available fixed-width font families. */
113 /****************************************************************************/
115 gl_font_util_get_fixed_width_families (void)
117 static GList *list = NULL;
118 PangoFontMap *fontmap;
119 PangoContext *context;
120 PangoFontFamily **families;
127 fontmap = pango_cairo_font_map_new ();
128 context = pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (fontmap));
130 pango_context_list_families (context, &families, &n);
132 for ( i=0; i<n; i++ )
134 if ( pango_font_family_is_monospace (families[i]) )
136 name = g_strdup (pango_font_family_get_name (families[i]));
137 list = g_list_insert_sorted (list, name,
138 (GCompareFunc)lgl_str_utf8_casecmp);
144 g_object_unref (context);
145 g_object_unref (fontmap);
152 /****************************************************************************/
153 /* Make sure we have a valid font. If not provide a good default. */
154 /****************************************************************************/
156 gl_font_util_validate_family (const gchar *family)
158 const GList *installed_families;
161 installed_families = gl_font_util_get_all_families ();
163 if (g_list_find_custom ((GList *)installed_families,
165 (GCompareFunc)g_utf8_collate))
167 good_family = g_strdup (family);
169 else if (g_list_find_custom ((GList *)installed_families,
171 (GCompareFunc)g_utf8_collate))
173 good_family = g_strdup ("Sans");
175 else if (installed_families != NULL)
177 good_family = g_strdup (installed_families->data); /* 1st entry */
188 /****************************************************************************/
189 /* Test if font is installed. */
190 /****************************************************************************/
192 gl_font_util_is_family_installed (const gchar *family)
194 const GList *installed_families;
197 installed_families = gl_font_util_get_all_families ();
199 p = g_list_find_custom ((GList *)installed_families,
201 (GCompareFunc)g_utf8_collate);
209 * Local Variables: -- emacs
211 * c-basic-offset: 8 -- emacs
212 * tab-width: 8 -- emacs
213 * indent-tabs-mode: nil -- emacs