]> Pileus Git - ~andy/gtk/blob - gdk/gdkfont.c
Fix #99593: Fix a memory leak when XmbLookupString returns XBufferOverflow
[~andy/gtk] / gdk / gdkfont.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #undef GDK_DISABLE_DEPRECATED
28
29 #include "gdkdisplay.h"
30 #include "gdkfont.h"
31 #include "gdkinternals.h"
32
33 GType
34 gdk_font_get_type (void)
35 {
36   static GType our_type = 0;
37   
38   if (our_type == 0)
39     our_type = g_boxed_type_register_static ("GdkFont",
40                                              (GBoxedCopyFunc)gdk_font_ref,
41                                              (GBoxedFreeFunc)gdk_font_unref);
42   return our_type;
43 }
44
45 /**
46  * gdk_font_ref:
47  * @font: a #GdkFont
48  * 
49  * Increases the reference count of a font by one.
50  * 
51  * Return value: @font
52  **/
53 GdkFont*
54 gdk_font_ref (GdkFont *font)
55 {
56   GdkFontPrivate *private;
57
58   g_return_val_if_fail (font != NULL, NULL);
59
60   private = (GdkFontPrivate*) font;
61   private->ref_count += 1;
62   return font;
63 }
64
65 /**
66  * gdk_font_unref:
67  * @font: a #GdkFont
68  * 
69  * Decreases the reference count of a font by one.
70  * If the result is zero, destroys the font.
71  **/
72 void
73 gdk_font_unref (GdkFont *font)
74 {
75   GdkFontPrivate *private;
76   private = (GdkFontPrivate*) font;
77
78   g_return_if_fail (font != NULL);
79   g_return_if_fail (private->ref_count > 0);
80
81   private->ref_count -= 1;
82   if (private->ref_count == 0)
83     _gdk_font_destroy (font);
84 }
85
86 /**
87  * gdk_string_width:
88  * @font:  a #GdkFont
89  * @string: the nul-terminated string to measure
90  * 
91  * Determines the width of a nul-terminated string.
92  * (The distance from the origin of the string to the 
93  * point where the next string in a sequence of strings
94  * should be drawn)
95  * 
96  * Return value: the width of the string in pixels.
97  **/
98 gint
99 gdk_string_width (GdkFont     *font,
100                   const gchar *string)
101 {
102   g_return_val_if_fail (font != NULL, -1);
103   g_return_val_if_fail (string != NULL, -1);
104
105   return gdk_text_width (font, string, _gdk_font_strlen (font, string));
106 }
107
108 /**
109  * gdk_char_width:
110  * @font: a #GdkFont
111  * @character: the character to measure.
112  * 
113  * Determines the width of a given character.
114  * 
115  * Return value: the width of the character in pixels.
116  **/
117 gint
118 gdk_char_width (GdkFont *font,
119                 gchar    character)
120 {
121   g_return_val_if_fail (font != NULL, -1);
122
123   return gdk_text_width (font, &character, 1);
124 }
125
126 /**
127  * gdk_char_width_wc:
128  * @font: a #GdkFont
129  * @character: the character to measure.
130  * 
131  * Determines the width of a given wide character. (Encoded
132  * in the wide-character encoding of the current locale).
133  * 
134  * Return value: the width of the character in pixels.
135  **/
136 gint
137 gdk_char_width_wc (GdkFont *font,
138                    GdkWChar character)
139 {
140   g_return_val_if_fail (font != NULL, -1);
141
142   return gdk_text_width_wc (font, &character, 1);
143 }
144
145 /**
146  * gdk_string_measure:
147  * @font: a #GdkFont
148  * @string: the nul-terminated string to measure.
149  * 
150  * Determines the distance from the origin to the rightmost
151  * portion of a nul-terminated string when drawn. This is not the
152  * correct value for determining the origin of the next
153  * portion when drawing text in multiple pieces.
154  * See gdk_string_width().
155  * 
156  * Return value: the right bearing of the string in pixels.
157  **/
158 gint
159 gdk_string_measure (GdkFont     *font,
160                     const gchar *string)
161 {
162   g_return_val_if_fail (font != NULL, -1);
163   g_return_val_if_fail (string != NULL, -1);
164
165   return gdk_text_measure (font, string, _gdk_font_strlen (font, string));
166 }
167
168 /**
169  * gdk_string_extents:
170  * @font: a #GdkFont.
171  * @string: the nul-terminated string to measure.
172  * @lbearing: the left bearing of the string.
173  * @rbearing: the right bearing of the string.
174  * @width: the width of the string.
175  * @ascent: the ascent of the string.
176  * @descent: the descent of the string.
177  * 
178  * Gets the metrics of a nul-terminated string.
179  **/
180 void
181 gdk_string_extents (GdkFont     *font,
182                     const gchar *string,
183                     gint        *lbearing,
184                     gint        *rbearing,
185                     gint        *width,
186                     gint        *ascent,
187                     gint        *descent)
188 {
189   g_return_if_fail (font != NULL);
190   g_return_if_fail (string != NULL);
191
192   gdk_text_extents (font, string, _gdk_font_strlen (font, string),
193                     lbearing, rbearing, width, ascent, descent);
194 }
195
196
197 /**
198  * gdk_text_measure:
199  * @font: a #GdkFont
200  * @text: the text to measure.
201  * @text_length: the length of the text in bytes.
202  * 
203  * Determines the distance from the origin to the rightmost
204  * portion of a string when drawn. This is not the
205  * correct value for determining the origin of the next
206  * portion when drawing text in multiple pieces. 
207  * See gdk_text_width().
208  * 
209  * Return value: the right bearing of the string in pixels.
210  **/
211 gint
212 gdk_text_measure (GdkFont     *font,
213                   const gchar *text,
214                   gint         text_length)
215 {
216   gint rbearing;
217
218   g_return_val_if_fail (font != NULL, -1);
219   g_return_val_if_fail (text != NULL, -1);
220
221   gdk_text_extents (font, text, text_length, NULL, &rbearing, NULL, NULL, NULL);
222   return rbearing;
223 }
224
225 /**
226  * gdk_char_measure:
227  * @font: a #GdkFont
228  * @character: the character to measure.
229  * 
230  * Determines the distance from the origin to the rightmost
231  * portion of a character when drawn. This is not the
232  * correct value for determining the origin of the next
233  * portion when drawing text in multiple pieces. 
234  * 
235  * Return value: the right bearing of the character in pixels.
236  **/
237 gint
238 gdk_char_measure (GdkFont *font,
239                   gchar    character)
240 {
241   g_return_val_if_fail (font != NULL, -1);
242
243   return gdk_text_measure (font, &character, 1);
244 }
245
246 /**
247  * gdk_string_height:
248  * @font: a #GdkFont
249  * @string: the nul-terminated string to measure.
250  * 
251  * Determines the total height of a given nul-terminated
252  * string. This value is not generally useful, because you
253  * cannot determine how this total height will be drawn in
254  * relation to the baseline. See gdk_string_extents().
255  * 
256  * Return value: the height of the string in pixels.
257  **/
258 gint
259 gdk_string_height (GdkFont     *font,
260                    const gchar *string)
261 {
262   g_return_val_if_fail (font != NULL, -1);
263   g_return_val_if_fail (string != NULL, -1);
264
265   return gdk_text_height (font, string, _gdk_font_strlen (font, string));
266 }
267
268 /**
269  * gdk_text_height:
270  * @font: a #GdkFont
271  * @text: the text to measure.
272  * @text_length: the length of the text in bytes.
273  * 
274  * Determines the total height of a given string.
275  * This value is not generally useful, because you cannot
276  * determine how this total height will be drawn in
277  * relation to the baseline. See gdk_text_extents().
278  * 
279  * Return value: the height of the string in pixels.
280  **/
281 gint
282 gdk_text_height (GdkFont     *font,
283                  const gchar *text,
284                  gint         text_length)
285 {
286   gint ascent, descent;
287
288   g_return_val_if_fail (font != NULL, -1);
289   g_return_val_if_fail (text != NULL, -1);
290
291   gdk_text_extents (font, text, text_length, NULL, NULL, NULL, &ascent, &descent);
292   return ascent + descent;
293 }
294
295 /**
296  * gdk_char_height:
297  * @font: a #GdkFont
298  * @character: the character to measure.
299  * 
300  * Determines the total height of a given character.
301  * This value is not generally useful, because you cannot
302  * determine how this total height will be drawn in
303  * relation to the baseline. See gdk_text_extents().
304  * 
305  * Return value: the height of the character in pixels. 
306  **/
307 gint
308 gdk_char_height (GdkFont *font,
309                  gchar    character)
310 {
311   g_return_val_if_fail (font != NULL, -1);
312
313   return gdk_text_height (font, &character, 1);
314 }
315
316 /**
317  * gdk_font_from_description:
318  * @font_desc: a #PangoFontDescription.
319  * 
320  * Load a #GdkFont based on a Pango font description. This font will
321  * only be an approximation of the Pango font, and
322  * internationalization will not be handled correctly. This function
323  * should only be used for legacy code that cannot be easily converted
324  * to use Pango. Using Pango directly will produce better results.
325  * 
326  * Return value: the newly loaded font, or %NULL if the font
327  * cannot be loaded.
328  **/
329 GdkFont*
330 gdk_font_from_description (PangoFontDescription *font_desc)
331 {
332   return gdk_font_from_description_for_display (gdk_display_get_default (),font_desc);
333 }
334
335 /**
336  * gdk_font_load:
337  * @font_name: a XLFD describing the font to load.
338  * 
339  * Loads a font.
340  * 
341  * The font may be newly loaded or looked up the font in a cache. 
342  * You should make no assumptions about the initial reference count.
343  * 
344  * Return value: a #GdkFont, or %NULL if the font could not be loaded.
345  **/
346 GdkFont*
347 gdk_font_load (const gchar *font_name)
348 {  
349    return gdk_font_load_for_display (gdk_display_get_default(), font_name);
350 }
351
352