]> Pileus Git - ~andy/gtk/blob - gdk/gdkfont.c
Various name fixes.
[~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  * Deprecated: Use gdk_text_extents() instead.
118  **/
119 gint
120 gdk_char_width (GdkFont *font,
121                 gchar    character)
122 {
123   g_return_val_if_fail (font != NULL, -1);
124
125   return gdk_text_width (font, &character, 1);
126 }
127
128 /**
129  * gdk_char_width_wc:
130  * @font: a #GdkFont
131  * @character: the character to measure.
132  * 
133  * Determines the width of a given wide character. (Encoded
134  * in the wide-character encoding of the current locale).
135  * 
136  * Return value: the width of the character in pixels.
137  **/
138 gint
139 gdk_char_width_wc (GdkFont *font,
140                    GdkWChar character)
141 {
142   g_return_val_if_fail (font != NULL, -1);
143
144   return gdk_text_width_wc (font, &character, 1);
145 }
146
147 /**
148  * gdk_string_measure:
149  * @font: a #GdkFont
150  * @string: the nul-terminated string to measure.
151  * 
152  * Determines the distance from the origin to the rightmost
153  * portion of a nul-terminated string when drawn. This is not the
154  * correct value for determining the origin of the next
155  * portion when drawing text in multiple pieces.
156  * See gdk_string_width().
157  * 
158  * Return value: the right bearing of the string in pixels.
159  **/
160 gint
161 gdk_string_measure (GdkFont     *font,
162                     const gchar *string)
163 {
164   g_return_val_if_fail (font != NULL, -1);
165   g_return_val_if_fail (string != NULL, -1);
166
167   return gdk_text_measure (font, string, _gdk_font_strlen (font, string));
168 }
169
170 /**
171  * gdk_string_extents:
172  * @font: a #GdkFont.
173  * @string: the nul-terminated string to measure.
174  * @lbearing: the left bearing of the string.
175  * @rbearing: the right bearing of the string.
176  * @width: the width of the string.
177  * @ascent: the ascent of the string.
178  * @descent: the descent of the string.
179  * 
180  * Gets the metrics of a nul-terminated string.
181  **/
182 void
183 gdk_string_extents (GdkFont     *font,
184                     const gchar *string,
185                     gint        *lbearing,
186                     gint        *rbearing,
187                     gint        *width,
188                     gint        *ascent,
189                     gint        *descent)
190 {
191   g_return_if_fail (font != NULL);
192   g_return_if_fail (string != NULL);
193
194   gdk_text_extents (font, string, _gdk_font_strlen (font, string),
195                     lbearing, rbearing, width, ascent, descent);
196 }
197
198
199 /**
200  * gdk_text_measure:
201  * @font: a #GdkFont
202  * @text: the text to measure.
203  * @text_length: the length of the text in bytes.
204  * 
205  * Determines the distance from the origin to the rightmost
206  * portion of a string when drawn. This is not the
207  * correct value for determining the origin of the next
208  * portion when drawing text in multiple pieces. 
209  * See gdk_text_width().
210  * 
211  * Return value: the right bearing of the string in pixels.
212  **/
213 gint
214 gdk_text_measure (GdkFont     *font,
215                   const gchar *text,
216                   gint         text_length)
217 {
218   gint rbearing;
219
220   g_return_val_if_fail (font != NULL, -1);
221   g_return_val_if_fail (text != NULL, -1);
222
223   gdk_text_extents (font, text, text_length, NULL, &rbearing, NULL, NULL, NULL);
224   return rbearing;
225 }
226
227 /**
228  * gdk_char_measure:
229  * @font: a #GdkFont
230  * @character: the character to measure.
231  * 
232  * Determines the distance from the origin to the rightmost
233  * portion of a character when drawn. This is not the
234  * correct value for determining the origin of the next
235  * portion when drawing text in multiple pieces. 
236  * 
237  * Return value: the right bearing of the character in pixels.
238  **/
239 gint
240 gdk_char_measure (GdkFont *font,
241                   gchar    character)
242 {
243   g_return_val_if_fail (font != NULL, -1);
244
245   return gdk_text_measure (font, &character, 1);
246 }
247
248 /**
249  * gdk_string_height:
250  * @font: a #GdkFont
251  * @string: the nul-terminated string to measure.
252  * 
253  * Determines the total height of a given nul-terminated
254  * string. This value is not generally useful, because you
255  * cannot determine how this total height will be drawn in
256  * relation to the baseline. See gdk_string_extents().
257  * 
258  * Return value: the height of the string in pixels.
259  **/
260 gint
261 gdk_string_height (GdkFont     *font,
262                    const gchar *string)
263 {
264   g_return_val_if_fail (font != NULL, -1);
265   g_return_val_if_fail (string != NULL, -1);
266
267   return gdk_text_height (font, string, _gdk_font_strlen (font, string));
268 }
269
270 /**
271  * gdk_text_height:
272  * @font: a #GdkFont
273  * @text: the text to measure.
274  * @text_length: the length of the text in bytes.
275  * 
276  * Determines the total height of a given string.
277  * This value is not generally useful, because you cannot
278  * determine how this total height will be drawn in
279  * relation to the baseline. See gdk_text_extents().
280  * 
281  * Return value: the height of the string in pixels.
282  **/
283 gint
284 gdk_text_height (GdkFont     *font,
285                  const gchar *text,
286                  gint         text_length)
287 {
288   gint ascent, descent;
289
290   g_return_val_if_fail (font != NULL, -1);
291   g_return_val_if_fail (text != NULL, -1);
292
293   gdk_text_extents (font, text, text_length, NULL, NULL, NULL, &ascent, &descent);
294   return ascent + descent;
295 }
296
297 /**
298  * gdk_char_height:
299  * @font: a #GdkFont
300  * @character: the character to measure.
301  * 
302  * Determines the total height of a given character.
303  * This value is not generally useful, because you cannot
304  * determine how this total height will be drawn in
305  * relation to the baseline. See gdk_text_extents().
306  * 
307  * Return value: the height of the character in pixels.
308  *
309  * Deprecated: Use gdk_text_extents() instead.
310  **/
311 gint
312 gdk_char_height (GdkFont *font,
313                  gchar    character)
314 {
315   g_return_val_if_fail (font != NULL, -1);
316
317   return gdk_text_height (font, &character, 1);
318 }
319
320 /**
321  * gdk_font_from_description:
322  * @font_desc: a #PangoFontDescription.
323  * 
324  * Load a #GdkFont based on a Pango font description. This font will
325  * only be an approximation of the Pango font, and
326  * internationalization will not be handled correctly. This function
327  * should only be used for legacy code that cannot be easily converted
328  * to use Pango. Using Pango directly will produce better results.
329  * 
330  * Return value: the newly loaded font, or %NULL if the font
331  * cannot be loaded.
332  **/
333 GdkFont*
334 gdk_font_from_description (PangoFontDescription *font_desc)
335 {
336   return gdk_font_from_description_for_display (gdk_display_get_default (),font_desc);
337 }
338
339 /**
340  * gdk_font_load:
341  * @font_name: a XLFD describing the font to load.
342  * 
343  * Loads a font.
344  * 
345  * The font may be newly loaded or looked up the font in a cache. 
346  * You should make no assumptions about the initial reference count.
347  * 
348  * Return value: a #GdkFont, or %NULL if the font could not be loaded.
349  **/
350 GdkFont*
351 gdk_font_load (const gchar *font_name)
352 {  
353    return gdk_font_load_for_display (gdk_display_get_default(), font_name);
354 }
355
356