]> git.sur5r.net Git - glabels/blob - glabels2/src/font-util.c
1bb54b77aa0609cce49b1d69f1d92b02543d6021
[glabels] / glabels2 / 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 <pango/pango.h>
26 #include <pango/pangocairo.h>
27 #include <glib/gstrfuncs.h>
28 #include <libglabels/str.h>
29
30
31 /****************************************************************************/
32 /* Get list of all available font families.                                 */
33 /****************************************************************************/
34 const GList  *
35 gl_font_util_get_all_families (void)
36 {
37         static GList         *list = NULL;
38         PangoFontMap         *fontmap;
39         PangoContext         *context;
40         PangoFontFamily     **families;
41         gint                  n;
42         gint                  i;
43         gchar                *name;
44
45         if ( !list )
46         {
47                 fontmap = pango_cairo_font_map_new ();
48                 context = pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (fontmap));
49
50                 pango_context_list_families (context, &families, &n);
51
52                 for ( i=0; i<n; i++ )
53                 {
54                         name = g_strdup (pango_font_family_get_name (families[i]));
55                         list = g_list_insert_sorted (list, name,
56                                                      (GCompareFunc)lgl_str_utf8_casecmp);
57                 }
58
59                 g_free (families);
60
61                 g_object_unref (context);
62                 g_object_unref (fontmap);
63         }
64
65         return list;
66 }
67
68
69 /****************************************************************************/
70 /* Get list of all available proportional font families.                    */
71 /****************************************************************************/
72 const GList  *
73 gl_font_util_get_proportional_families (void)
74 {
75         static GList         *list = NULL;
76         PangoFontMap         *fontmap;
77         PangoContext         *context;
78         PangoFontFamily     **families;
79         gint                  n;
80         gint                  i;
81         gchar                *name;
82
83         if ( !list )
84         {
85                 fontmap = pango_cairo_font_map_new ();
86                 context = pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (fontmap));
87
88                 pango_context_list_families (context, &families, &n);
89
90                 for ( i=0; i<n; i++ )
91                 {
92                         if ( !pango_font_family_is_monospace (families[i]) )
93                         {
94                                 name = g_strdup (pango_font_family_get_name (families[i]));
95                                 list = g_list_insert_sorted (list, name,
96                                                              (GCompareFunc)lgl_str_utf8_casecmp);
97                         }
98                 }
99
100                 g_free (families);
101
102                 g_object_unref (context);
103                 g_object_unref (fontmap);
104         }
105
106         return list;
107 }
108
109
110 /****************************************************************************/
111 /* Get list of all available fixed-width font families.                     */
112 /****************************************************************************/
113 const GList  *
114 gl_font_util_get_fixed_width_families (void)
115 {
116         static GList         *list = NULL;
117         PangoFontMap         *fontmap;
118         PangoContext         *context;
119         PangoFontFamily     **families;
120         gint                  n;
121         gint                  i;
122         gchar                *name;
123
124         if ( !list )
125         {
126                 fontmap = pango_cairo_font_map_new ();
127                 context = pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (fontmap));
128
129                 pango_context_list_families (context, &families, &n);
130
131                 for ( i=0; i<n; i++ )
132                 {
133                         if ( pango_font_family_is_monospace (families[i]) )
134                         {
135                                 name = g_strdup (pango_font_family_get_name (families[i]));
136                                 list = g_list_insert_sorted (list, name,
137                                                              (GCompareFunc)lgl_str_utf8_casecmp);
138                         }
139                 }
140
141                 g_free (families);
142
143                 g_object_unref (context);
144                 g_object_unref (fontmap);
145         }
146
147         return list;
148 }
149
150
151 /****************************************************************************/
152 /* Make sure we have a valid font.  If not provide a good default.          */
153 /****************************************************************************/
154 gchar *
155 gl_font_util_validate_family (const gchar *family)
156 {
157         const GList *installed_families;
158         gchar       *good_family;
159
160         installed_families = gl_font_util_get_all_families ();
161
162         if (g_list_find_custom ((GList *)installed_families,
163                                 family,
164                                 (GCompareFunc)g_utf8_collate))
165         {
166                 good_family = g_strdup (family);
167         }
168         else if (g_list_find_custom ((GList *)installed_families,
169                                      "Sans",
170                                      (GCompareFunc)g_utf8_collate))
171         {
172                 good_family = g_strdup ("Sans");
173         }
174         else if (installed_families != NULL)
175         {
176                 good_family = g_strdup (installed_families->data); /* 1st entry */
177         }
178         else
179         {
180                 good_family = NULL;
181         }
182
183         return good_family;
184 }
185
186
187
188 /*
189  * Local Variables:       -- emacs
190  * mode: C                -- emacs
191  * c-basic-offset: 8      -- emacs
192  * tab-width: 8           -- emacs
193  * indent-tabs-mode: nil  -- emacs
194  * End:                   -- emacs
195  */