2 * pango-fontmap.c: Font handling
4 * Copyright (C) 2000 Red Hat Software
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 #include "pango-fontmap.h"
24 #include "pango-impl-utils.h"
27 static PangoFontset *pango_font_map_real_load_fontset (PangoFontMap *fontmap,
28 PangoContext *context,
29 const PangoFontDescription *desc,
30 PangoLanguage *language);
33 G_DEFINE_ABSTRACT_TYPE (PangoFontMap, pango_font_map, G_TYPE_OBJECT)
36 pango_font_map_class_init (PangoFontMapClass *class)
38 class->load_fontset = pango_font_map_real_load_fontset;
42 pango_font_map_init (PangoFontMap *fontmap G_GNUC_UNUSED)
47 * pango_font_map_create_context:
48 * @fontmap: a #PangoFontMap
50 * Creates a #PangoContext connected to @fontmap. This is equivalent
51 * to pango_context_new() followed by pango_context_set_font_map().
53 * If you are using Pango as part of a higher-level system,
54 * that system may have it's own way of create a #PangoContext.
55 * For instance, the GTK+ toolkit has, among others,
56 * gdk_pango_context_get_for_screen(), and
57 * gtk_widget_get_pango_context(). Use those instead.
59 * Return value: the newly allocated #PangoContext, which should
60 * be freed with g_object_unref().
65 pango_font_map_create_context (PangoFontMap *fontmap)
67 PangoContext *context;
69 g_return_val_if_fail (fontmap != NULL, NULL);
71 context = pango_context_new ();
72 pango_context_set_font_map (context, fontmap);
78 * pango_font_map_load_font:
79 * @fontmap: a #PangoFontMap
80 * @context: the #PangoContext the font will be used with
81 * @desc: a #PangoFontDescription describing the font to load
83 * Load the font in the fontmap that is the closest match for @desc.
85 * Returns: the font loaded, or %NULL if no font matched.
88 pango_font_map_load_font (PangoFontMap *fontmap,
89 PangoContext *context,
90 const PangoFontDescription *desc)
92 g_return_val_if_fail (fontmap != NULL, NULL);
94 return PANGO_FONT_MAP_GET_CLASS (fontmap)->load_font (fontmap, context, desc);
98 * pango_font_map_list_families:
99 * @fontmap: a #PangoFontMap
100 * @families: location to store a pointer to an array of #PangoFontFamily *.
101 * This array should be freed with g_free().
102 * @n_families: location to store the number of elements in @families
104 * List all families for a fontmap.
107 pango_font_map_list_families (PangoFontMap *fontmap,
108 PangoFontFamily ***families,
111 g_return_if_fail (fontmap != NULL);
113 PANGO_FONT_MAP_GET_CLASS (fontmap)->list_families (fontmap, families, n_families);
117 * pango_font_map_load_fontset:
118 * @fontmap: a #PangoFontMap
119 * @context: the #PangoContext the font will be used with
120 * @desc: a #PangoFontDescription describing the font to load
121 * @language: a #PangoLanguage the fonts will be used for
123 * Load a set of fonts in the fontmap that can be used to render
124 * a font matching @desc.
126 * Returns: the fontset, or %NULL if no font matched.
129 pango_font_map_load_fontset (PangoFontMap *fontmap,
130 PangoContext *context,
131 const PangoFontDescription *desc,
132 PangoLanguage *language)
134 g_return_val_if_fail (fontmap != NULL, NULL);
136 return PANGO_FONT_MAP_GET_CLASS (fontmap)->load_fontset (fontmap, context, desc, language);
140 pango_font_map_fontset_add_fonts (PangoFontMap *fontmap,
141 PangoContext *context,
142 PangoFontsetSimple *fonts,
143 PangoFontDescription *desc,
151 pango_lookup_aliases (family,
157 for (j = 0; j < n_aliases; j++)
159 pango_font_description_set_family_static (desc, aliases[j]);
160 font = pango_font_map_load_font (fontmap, context, desc);
162 pango_fontset_simple_append (fonts, font);
167 pango_font_description_set_family_static (desc, family);
168 font = pango_font_map_load_font (fontmap, context, desc);
170 pango_fontset_simple_append (fonts, font);
174 static PangoFontset *
175 pango_font_map_real_load_fontset (PangoFontMap *fontmap,
176 PangoContext *context,
177 const PangoFontDescription *desc,
178 PangoLanguage *language)
180 PangoFontDescription *tmp_desc = pango_font_description_copy_static (desc);
184 PangoFontsetSimple *fonts;
185 static GHashTable *warned_fonts = NULL;
187 family = pango_font_description_get_family (desc);
188 families = g_strsplit (family ? family : "", ",", -1);
190 fonts = pango_fontset_simple_new (language);
192 for (i = 0; families[i]; i++)
193 pango_font_map_fontset_add_fonts (fontmap,
199 g_strfreev (families);
201 /* The font description was completely unloadable, try with
204 if (pango_fontset_simple_size (fonts) == 0)
208 pango_font_description_set_family_static (tmp_desc,
209 pango_font_description_get_family (desc));
211 ctmp1 = pango_font_description_to_string (desc);
212 pango_font_description_set_family_static (tmp_desc, "Sans");
214 if (!warned_fonts || !g_hash_table_lookup (warned_fonts, ctmp1))
217 warned_fonts = g_hash_table_new (g_str_hash, g_str_equal);
219 g_hash_table_insert (warned_fonts, g_strdup (ctmp1), GINT_TO_POINTER (1));
221 ctmp2 = pango_font_description_to_string (tmp_desc);
222 g_warning ("couldn't load font \"%s\", falling back to \"%s\", "
223 "expect ugly output.", ctmp1, ctmp2);
228 pango_font_map_fontset_add_fonts (fontmap,
235 /* We couldn't try with Sans and the specified style. Try Sans Normal
237 if (pango_fontset_simple_size (fonts) == 0)
241 pango_font_description_set_family_static (tmp_desc, "Sans");
242 ctmp1 = pango_font_description_to_string (tmp_desc);
243 pango_font_description_set_style (tmp_desc, PANGO_STYLE_NORMAL);
244 pango_font_description_set_weight (tmp_desc, PANGO_WEIGHT_NORMAL);
245 pango_font_description_set_variant (tmp_desc, PANGO_VARIANT_NORMAL);
246 pango_font_description_set_stretch (tmp_desc, PANGO_STRETCH_NORMAL);
248 if (!warned_fonts || !g_hash_table_lookup (warned_fonts, ctmp1))
250 g_hash_table_insert (warned_fonts, g_strdup (ctmp1), GINT_TO_POINTER (1));
252 ctmp2 = pango_font_description_to_string (tmp_desc);
254 g_warning ("couldn't load font \"%s\", falling back to \"%s\", "
255 "expect ugly output.", ctmp1, ctmp2);
261 pango_font_map_fontset_add_fonts (fontmap,
268 pango_font_description_free (tmp_desc);
270 /* Everything failed, we are screwed, there is no way to continue,
271 * but lets just not crash here.
273 if (pango_fontset_simple_size (fonts) == 0)
274 g_warning ("All font fallbacks failed!!!!");
276 return PANGO_FONTSET (fonts);
280 * pango_font_map_get_shape_engine_type:
281 * @fontmap: a #PangoFontMap
283 * Returns the render ID for shape engines for this fontmap.
284 * See the <structfield>render_type</structfield> field of
287 * Return value: the ID string for shape engines for
288 * this fontmap. Owned by Pango, should not be modified
293 G_CONST_RETURN char *
294 pango_font_map_get_shape_engine_type (PangoFontMap *fontmap)
296 g_return_val_if_fail (PANGO_IS_FONT_MAP (fontmap), NULL);
298 return PANGO_FONT_MAP_GET_CLASS (fontmap)->shape_engine_type;