]> git.sur5r.net Git - glabels/blob - src/font-util.c
Imported Upstream version 3.0.0
[glabels] / src / font-util.c
1 /*
2  *  font-util.h
3  *  Copyright (C) 2009  Jim Evins <evins@snaught.com>.
4  *
5  *  This file is part of gLabels.
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 #include <config.h>
22
23 #include "font-util.h"
24
25 #include <glib.h>
26 #include <pango/pango.h>
27 #include <pango/pangocairo.h>
28
29 #include <libglabels.h>
30
31
32 /****************************************************************************/
33 /* Get list of all available font families.                                 */
34 /****************************************************************************/
35 const GList  *
36 gl_font_util_get_all_families (void)
37 {
38         static GList         *list = NULL;
39         PangoFontMap         *fontmap;
40         PangoContext         *context;
41         PangoFontFamily     **families;
42         gint                  n;
43         gint                  i;
44         gchar                *name;
45
46         if ( !list )
47         {
48                 fontmap = pango_cairo_font_map_new ();
49                 context = pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (fontmap));
50
51                 pango_context_list_families (context, &families, &n);
52
53                 for ( i=0; i<n; i++ )
54                 {
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);
58                 }
59
60                 g_free (families);
61
62                 g_object_unref (context);
63                 g_object_unref (fontmap);
64         }
65
66         return list;
67 }
68
69
70 /****************************************************************************/
71 /* Get list of all available proportional font families.                    */
72 /****************************************************************************/
73 const GList  *
74 gl_font_util_get_proportional_families (void)
75 {
76         static GList         *list = NULL;
77         PangoFontMap         *fontmap;
78         PangoContext         *context;
79         PangoFontFamily     **families;
80         gint                  n;
81         gint                  i;
82         gchar                *name;
83
84         if ( !list )
85         {
86                 fontmap = pango_cairo_font_map_new ();
87                 context = pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (fontmap));
88
89                 pango_context_list_families (context, &families, &n);
90
91                 for ( i=0; i<n; i++ )
92                 {
93                         if ( !pango_font_family_is_monospace (families[i]) )
94                         {
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);
98                         }
99                 }
100
101                 g_free (families);
102
103                 g_object_unref (context);
104                 g_object_unref (fontmap);
105         }
106
107         return list;
108 }
109
110
111 /****************************************************************************/
112 /* Get list of all available fixed-width font families.                     */
113 /****************************************************************************/
114 const GList  *
115 gl_font_util_get_fixed_width_families (void)
116 {
117         static GList         *list = NULL;
118         PangoFontMap         *fontmap;
119         PangoContext         *context;
120         PangoFontFamily     **families;
121         gint                  n;
122         gint                  i;
123         gchar                *name;
124
125         if ( !list )
126         {
127                 fontmap = pango_cairo_font_map_new ();
128                 context = pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (fontmap));
129
130                 pango_context_list_families (context, &families, &n);
131
132                 for ( i=0; i<n; i++ )
133                 {
134                         if ( pango_font_family_is_monospace (families[i]) )
135                         {
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);
139                         }
140                 }
141
142                 g_free (families);
143
144                 g_object_unref (context);
145                 g_object_unref (fontmap);
146         }
147
148         return list;
149 }
150
151
152 /****************************************************************************/
153 /* Make sure we have a valid font.  If not provide a good default.          */
154 /****************************************************************************/
155 gchar *
156 gl_font_util_validate_family (const gchar *family)
157 {
158         const GList *installed_families;
159         gchar       *good_family;
160
161         installed_families = gl_font_util_get_all_families ();
162
163         if (g_list_find_custom ((GList *)installed_families,
164                                 family,
165                                 (GCompareFunc)g_utf8_collate))
166         {
167                 good_family = g_strdup (family);
168         }
169         else if (g_list_find_custom ((GList *)installed_families,
170                                      "Sans",
171                                      (GCompareFunc)g_utf8_collate))
172         {
173                 good_family = g_strdup ("Sans");
174         }
175         else if (installed_families != NULL)
176         {
177                 good_family = g_strdup (installed_families->data); /* 1st entry */
178         }
179         else
180         {
181                 good_family = NULL;
182         }
183
184         return good_family;
185 }
186
187
188 /****************************************************************************/
189 /* Test if font is installed.                                               */
190 /****************************************************************************/
191 gboolean
192 gl_font_util_is_family_installed (const gchar *family)
193 {
194         const GList *installed_families;
195         GList       *p;
196
197         installed_families = gl_font_util_get_all_families ();
198
199         p = g_list_find_custom ((GList *)installed_families,
200                                 family,
201                                 (GCompareFunc)g_utf8_collate);
202
203         return (p != NULL);
204 }
205
206
207
208 /*
209  * Local Variables:       -- emacs
210  * mode: C                -- emacs
211  * c-basic-offset: 8      -- emacs
212  * tab-width: 8           -- emacs
213  * indent-tabs-mode: nil  -- emacs
214  * End:                   -- emacs
215  */