4 * Copyright (C) 2001 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.
28 #include "pango-types.h"
29 #include "pango-font.h"
30 #include "pango-fontset.h"
31 #include "pango-impl-utils.h"
33 static PangoFontMetrics *pango_fontset_real_get_metrics (PangoFontset *fontset);
36 G_DEFINE_ABSTRACT_TYPE (PangoFontset, pango_fontset, G_TYPE_OBJECT);
39 pango_fontset_init (PangoFontset *self)
44 pango_fontset_class_init (PangoFontsetClass *class)
46 class->get_metrics = pango_fontset_real_get_metrics;
51 * pango_fontset_get_font:
52 * @fontset: a #PangoFontset
53 * @wc: a Unicode character
55 * Returns the font in the fontset that contains the best glyph for the
56 * Unicode character @wc.
58 * Return value: (transfer full): a #PangoFont. The caller must call
59 * g_object_unref when finished with the font.
62 pango_fontset_get_font (PangoFontset *fontset,
66 g_return_val_if_fail (PANGO_IS_FONTSET (fontset), NULL);
68 return PANGO_FONTSET_GET_CLASS (fontset)->get_font (fontset, wc);
72 * pango_fontset_get_metrics:
73 * @fontset: a #PangoFontset
75 * Get overall metric information for the fonts in the fontset.
77 * Return value: a #PangoFontMetrics object. The caller must call pango_font_metrics_unref()
78 * when finished using the object.
81 pango_fontset_get_metrics (PangoFontset *fontset)
83 g_return_val_if_fail (PANGO_IS_FONTSET (fontset), NULL);
85 return PANGO_FONTSET_GET_CLASS (fontset)->get_metrics (fontset);
89 * pango_fontset_foreach:
90 * @fontset: a #PangoFontset
91 * @func: (closure data) (scope call): Callback function
92 * @data: (closure): data to pass to the callback function
94 * Iterates through all the fonts in a fontset, calling @func for
95 * each one. If @func returns %TRUE, that stops the iteration.
100 pango_fontset_foreach (PangoFontset *fontset,
101 PangoFontsetForeachFunc func,
104 g_return_if_fail (PANGO_IS_FONTSET (fontset));
105 g_return_if_fail (func != NULL);
107 PANGO_FONTSET_GET_CLASS (fontset)->foreach (fontset, func, data);
111 get_first_metrics_foreach (PangoFontset *fontset,
115 PangoFontMetrics *fontset_metrics = data;
116 PangoLanguage *language = PANGO_FONTSET_GET_CLASS (fontset)->get_language (fontset);
117 PangoFontMetrics *font_metrics = pango_font_get_metrics (font, language);
118 guint save_ref_count;
120 /* Initialize the fontset metrics to metrics of the first font in the
121 * fontset; saving the refcount and restoring it is a bit of hack but avoids
122 * having to update this code for each metrics addition.
124 save_ref_count = fontset_metrics->ref_count;
125 *fontset_metrics = *font_metrics;
126 fontset_metrics->ref_count = save_ref_count;
128 pango_font_metrics_unref (font_metrics);
130 return TRUE; /* Stops iteration */
133 static PangoFontMetrics *
134 pango_fontset_real_get_metrics (PangoFontset *fontset)
136 PangoFontMetrics *metrics, *raw_metrics;
137 const char *sample_str;
140 GHashTable *fonts_seen;
142 PangoLanguage *language;
144 language = PANGO_FONTSET_GET_CLASS (fontset)->get_language (fontset);
145 sample_str = pango_language_get_sample_string (language);
148 metrics = pango_font_metrics_new ();
149 fonts_seen = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
151 /* Initialize the metrics from the first font in the fontset */
152 pango_fontset_foreach (fontset, get_first_metrics_foreach, metrics);
157 gunichar wc = g_utf8_get_char (p);
158 font = pango_fontset_get_font (fontset, wc);
161 if (g_hash_table_lookup (fonts_seen, font) == NULL)
163 raw_metrics = pango_font_get_metrics (font, language);
164 g_hash_table_insert (fonts_seen, font, font);
168 metrics->ascent = raw_metrics->ascent;
169 metrics->descent = raw_metrics->descent;
170 metrics->approximate_char_width = raw_metrics->approximate_char_width;
171 metrics->approximate_digit_width = raw_metrics->approximate_digit_width;
175 metrics->ascent = MAX (metrics->ascent, raw_metrics->ascent);
176 metrics->descent = MAX (metrics->descent, raw_metrics->descent);
177 metrics->approximate_char_width += raw_metrics->approximate_char_width;
178 metrics->approximate_digit_width += raw_metrics->approximate_digit_width;
181 pango_font_metrics_unref (raw_metrics);
184 g_object_unref (font);
187 p = g_utf8_next_char (p);
190 g_hash_table_destroy (fonts_seen);
194 metrics->approximate_char_width /= count;
195 metrics->approximate_digit_width /= count;
206 #define PANGO_FONTSET_SIMPLE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PANGO_TYPE_FONTSET_SIMPLE, PangoFontsetSimpleClass))
207 #define PANGO_IS_FONTSET_SIMPLE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PANGO_TYPE_FONTSET_SIMPLE))
208 #define PANGO_FONTSET_SIMPLE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PANGO_TYPE_FONTSET_SIMPLE, PangoFontsetSimpleClass))
210 static void pango_fontset_simple_finalize (GObject *object);
211 static PangoFontMetrics *pango_fontset_simple_get_metrics (PangoFontset *fontset);
212 static PangoLanguage * pango_fontset_simple_get_language (PangoFontset *fontset);
213 static PangoFont * pango_fontset_simple_get_font (PangoFontset *fontset,
215 static void pango_fontset_simple_foreach (PangoFontset *fontset,
216 PangoFontsetForeachFunc func,
219 struct _PangoFontsetSimple
221 PangoFontset parent_instance;
224 GPtrArray *coverages;
225 PangoLanguage *language;
228 struct _PangoFontsetSimpleClass
230 PangoFontsetClass parent_class;
234 * pango_fontset_simple_new:
235 * @language: a #PangoLanguage tag
237 * Creates a new #PangoFontsetSimple for the given language.
239 * Return value: the newly allocated #PangoFontsetSimple, which should
240 * be freed with g_object_unref().
243 pango_fontset_simple_new (PangoLanguage *language)
245 PangoFontsetSimple *fontset;
247 fontset = g_object_new (PANGO_TYPE_FONTSET_SIMPLE, NULL);
248 fontset->language = language;
254 G_DEFINE_TYPE (PangoFontsetSimple, pango_fontset_simple, PANGO_TYPE_FONTSET);
257 pango_fontset_simple_class_init (PangoFontsetSimpleClass *class)
259 GObjectClass *object_class = G_OBJECT_CLASS (class);
260 PangoFontsetClass *fontset_class = PANGO_FONTSET_CLASS (class);
262 object_class->finalize = pango_fontset_simple_finalize;
264 fontset_class->get_font = pango_fontset_simple_get_font;
265 fontset_class->get_metrics = pango_fontset_simple_get_metrics;
266 fontset_class->get_language = pango_fontset_simple_get_language;
267 fontset_class->foreach = pango_fontset_simple_foreach;
271 pango_fontset_simple_init (PangoFontsetSimple *fontset)
273 fontset->fonts = g_ptr_array_new ();
274 fontset->coverages = g_ptr_array_new ();
275 fontset->language = NULL;
279 pango_fontset_simple_finalize (GObject *object)
281 PangoFontsetSimple *fontset = PANGO_FONTSET_SIMPLE (object);
282 PangoCoverage *coverage;
285 for (i = 0; i < fontset->fonts->len; i++)
286 g_object_unref (g_ptr_array_index(fontset->fonts, i));
288 g_ptr_array_free (fontset->fonts, TRUE);
290 for (i = 0; i < fontset->coverages->len; i++)
292 coverage = g_ptr_array_index (fontset->coverages, i);
294 pango_coverage_unref (coverage);
297 g_ptr_array_free (fontset->coverages, TRUE);
299 G_OBJECT_CLASS (pango_fontset_simple_parent_class)->finalize (object);
303 * pango_fontset_simple_append:
304 * @fontset: a #PangoFontsetSimple.
305 * @font: a #PangoFont.
307 * Adds a font to the fontset.
310 pango_fontset_simple_append (PangoFontsetSimple *fontset,
313 g_ptr_array_add (fontset->fonts, font);
314 g_ptr_array_add (fontset->coverages, NULL);
318 * pango_fontset_simple_size:
319 * @fontset: a #PangoFontsetSimple.
321 * Returns the number of fonts in the fontset.
323 * Return value: the size of @fontset.
326 pango_fontset_simple_size (PangoFontsetSimple *fontset)
328 return fontset->fonts->len;
331 static PangoLanguage *
332 pango_fontset_simple_get_language (PangoFontset *fontset)
334 PangoFontsetSimple *simple = PANGO_FONTSET_SIMPLE (fontset);
336 return simple->language;
339 static PangoFontMetrics *
340 pango_fontset_simple_get_metrics (PangoFontset *fontset)
342 PangoFontsetSimple *simple = PANGO_FONTSET_SIMPLE (fontset);
344 if (simple->fonts->len == 1)
345 return pango_font_get_metrics (PANGO_FONT (g_ptr_array_index(simple->fonts, 0)),
348 return PANGO_FONTSET_CLASS (pango_fontset_simple_parent_class)->get_metrics (fontset);
352 pango_fontset_simple_get_font (PangoFontset *fontset,
355 PangoFontsetSimple *simple = PANGO_FONTSET_SIMPLE (fontset);
356 PangoCoverageLevel best_level = PANGO_COVERAGE_NONE;
357 PangoCoverageLevel level;
359 PangoCoverage *coverage;
363 for (i = 0; i < simple->fonts->len; i++)
365 coverage = g_ptr_array_index (simple->coverages, i);
367 if (coverage == NULL)
369 font = g_ptr_array_index (simple->fonts, i);
371 coverage = pango_font_get_coverage (font, simple->language);
372 g_ptr_array_index (simple->coverages, i) = coverage;
375 level = pango_coverage_get (coverage, wc);
377 if (result == -1 || level > best_level)
381 if (level == PANGO_COVERAGE_EXACT)
386 if (G_UNLIKELY (result == -1))
389 font = g_ptr_array_index(simple->fonts, result);
390 return g_object_ref (font);
394 pango_fontset_simple_foreach (PangoFontset *fontset,
395 PangoFontsetForeachFunc func,
398 PangoFontsetSimple *simple = PANGO_FONTSET_SIMPLE (fontset);
401 for (i = 0; i < simple->fonts->len; i++)
403 if ((*func) (fontset,
404 g_ptr_array_index (simple->fonts, i),