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: (transfer full): the newly allocated #PangoContext,
60 * which should 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: (transfer full): the newly allocated #PangoFont loaded,
86 * or %NULL if no font matched.
89 pango_font_map_load_font (PangoFontMap *fontmap,
90 PangoContext *context,
91 const PangoFontDescription *desc)
93 g_return_val_if_fail (fontmap != NULL, NULL);
95 return PANGO_FONT_MAP_GET_CLASS (fontmap)->load_font (fontmap, context, desc);
99 * pango_font_map_list_families:
100 * @fontmap: a #PangoFontMap
101 * @families: (out) (array length=n_families) (transfer container): location to store a pointer to an array of #PangoFontFamily *.
102 * This array should be freed with g_free().
103 * @n_families: (out): location to store the number of elements in @families
105 * List all families for a fontmap.
108 pango_font_map_list_families (PangoFontMap *fontmap,
109 PangoFontFamily ***families,
112 g_return_if_fail (fontmap != NULL);
114 PANGO_FONT_MAP_GET_CLASS (fontmap)->list_families (fontmap, families, n_families);
118 * pango_font_map_load_fontset:
119 * @fontmap: a #PangoFontMap
120 * @context: the #PangoContext the font will be used with
121 * @desc: a #PangoFontDescription describing the font to load
122 * @language: a #PangoLanguage the fonts will be used for
124 * Load a set of fonts in the fontmap that can be used to render
125 * a font matching @desc.
127 * Returns: (transfer full): the newly allocated #PangoFontset
128 * loaded, or %NULL if no font matched.
131 pango_font_map_load_fontset (PangoFontMap *fontmap,
132 PangoContext *context,
133 const PangoFontDescription *desc,
134 PangoLanguage *language)
136 g_return_val_if_fail (fontmap != NULL, NULL);
138 return PANGO_FONT_MAP_GET_CLASS (fontmap)->load_fontset (fontmap, context, desc, language);
142 pango_font_map_fontset_add_fonts (PangoFontMap *fontmap,
143 PangoContext *context,
144 PangoFontsetSimple *fonts,
145 PangoFontDescription *desc,
153 pango_lookup_aliases (family,
159 for (j = 0; j < n_aliases; j++)
161 pango_font_description_set_family_static (desc, aliases[j]);
162 font = pango_font_map_load_font (fontmap, context, desc);
164 pango_fontset_simple_append (fonts, font);
169 pango_font_description_set_family_static (desc, family);
170 font = pango_font_map_load_font (fontmap, context, desc);
172 pango_fontset_simple_append (fonts, font);
176 static PangoFontset *
177 pango_font_map_real_load_fontset (PangoFontMap *fontmap,
178 PangoContext *context,
179 const PangoFontDescription *desc,
180 PangoLanguage *language)
182 PangoFontDescription *tmp_desc = pango_font_description_copy_static (desc);
186 PangoFontsetSimple *fonts;
187 static GHashTable *warned_fonts = NULL;
189 family = pango_font_description_get_family (desc);
190 families = g_strsplit (family ? family : "", ",", -1);
192 fonts = pango_fontset_simple_new (language);
194 for (i = 0; families[i]; i++)
195 pango_font_map_fontset_add_fonts (fontmap,
201 g_strfreev (families);
203 /* The font description was completely unloadable, try with
206 if (pango_fontset_simple_size (fonts) == 0)
210 pango_font_description_set_family_static (tmp_desc,
211 pango_font_description_get_family (desc));
213 ctmp1 = pango_font_description_to_string (desc);
214 pango_font_description_set_family_static (tmp_desc, "Sans");
216 if (!warned_fonts || !g_hash_table_lookup (warned_fonts, ctmp1))
219 warned_fonts = g_hash_table_new (g_str_hash, g_str_equal);
221 g_hash_table_insert (warned_fonts, g_strdup (ctmp1), GINT_TO_POINTER (1));
223 ctmp2 = pango_font_description_to_string (tmp_desc);
224 g_warning ("couldn't load font \"%s\", falling back to \"%s\", "
225 "expect ugly output.", ctmp1, ctmp2);
230 pango_font_map_fontset_add_fonts (fontmap,
237 /* We couldn't try with Sans and the specified style. Try Sans Normal
239 if (pango_fontset_simple_size (fonts) == 0)
243 pango_font_description_set_family_static (tmp_desc, "Sans");
244 ctmp1 = pango_font_description_to_string (tmp_desc);
245 pango_font_description_set_style (tmp_desc, PANGO_STYLE_NORMAL);
246 pango_font_description_set_weight (tmp_desc, PANGO_WEIGHT_NORMAL);
247 pango_font_description_set_variant (tmp_desc, PANGO_VARIANT_NORMAL);
248 pango_font_description_set_stretch (tmp_desc, PANGO_STRETCH_NORMAL);
250 if (!warned_fonts || !g_hash_table_lookup (warned_fonts, ctmp1))
252 g_hash_table_insert (warned_fonts, g_strdup (ctmp1), GINT_TO_POINTER (1));
254 ctmp2 = pango_font_description_to_string (tmp_desc);
256 g_warning ("couldn't load font \"%s\", falling back to \"%s\", "
257 "expect ugly output.", ctmp1, ctmp2);
263 pango_font_map_fontset_add_fonts (fontmap,
270 pango_font_description_free (tmp_desc);
272 /* Everything failed, we are screwed, there is no way to continue,
273 * but lets just not crash here.
275 if (pango_fontset_simple_size (fonts) == 0)
276 g_warning ("All font fallbacks failed!!!!");
278 return PANGO_FONTSET (fonts);
282 * pango_font_map_get_shape_engine_type:
283 * @fontmap: a #PangoFontMap
285 * Returns the render ID for shape engines for this fontmap.
286 * See the <structfield>render_type</structfield> field of
289 * Return value: the ID string for shape engines for
290 * this fontmap. Owned by Pango, should not be modified
296 pango_font_map_get_shape_engine_type (PangoFontMap *fontmap)
298 g_return_val_if_fail (PANGO_IS_FONT_MAP (fontmap), NULL);
300 return PANGO_FONT_MAP_GET_CLASS (fontmap)->shape_engine_type;