1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
27 #undef GDK_DISABLE_DEPRECATED
33 #include <pango/pangox.h>
37 #include "gdkprivate-x11.h"
38 #include "gdkinternals.h"
39 #include "gdkdisplay-x11.h"
40 #include "gdkscreen-x11.h"
42 typedef struct _GdkFontPrivateX GdkFontPrivateX;
44 struct _GdkFontPrivateX
47 /* XFontStruct *xfont; */
48 /* generic pointer point to XFontStruct or XFontSet */
56 gdk_font_name_hash_get (GdkDisplay *display)
59 static GQuark font_name_quark = 0;
62 font_name_quark = g_quark_from_static_string ("gdk-font-hash");
64 result = g_object_get_qdata (G_OBJECT (display), font_name_quark);
68 result = g_hash_table_new (g_str_hash, g_str_equal);
69 g_object_set_qdata (G_OBJECT (display), font_name_quark, result);
76 gdk_fontset_name_hash_get (GdkDisplay *display)
79 static GQuark fontset_name_quark = 0;
81 if (!fontset_name_quark)
82 fontset_name_quark = g_quark_from_static_string ("gdk-fontset-hash");
84 result = g_object_get_qdata (G_OBJECT (display), fontset_name_quark);
88 result = g_hash_table_new (g_str_hash, g_str_equal);
89 g_object_set_qdata (G_OBJECT (display), fontset_name_quark, result);
96 * gdk_font_get_display:
97 * @font: the #GdkFont.
99 * Returns the #GdkDisplay for @font.
101 * Returns : the corresponding #GdkDisplay.
104 gdk_font_get_display (GdkFont* font)
106 return ((GdkFontPrivateX *)font)->display;
110 gdk_font_hash_insert (GdkFontType type,
112 const gchar *font_name)
114 GdkFontPrivateX *private = (GdkFontPrivateX *)font;
115 GHashTable *hash = (type == GDK_FONT_FONT) ?
116 gdk_font_name_hash_get (private->display) : gdk_fontset_name_hash_get (private->display);
118 private->names = g_slist_prepend (private->names, g_strdup (font_name));
119 g_hash_table_insert (hash, private->names->data, font);
123 gdk_font_hash_remove (GdkFontType type,
126 GdkFontPrivateX *private = (GdkFontPrivateX *)font;
128 GHashTable *hash = (type == GDK_FONT_FONT) ?
129 gdk_font_name_hash_get (private->display) : gdk_fontset_name_hash_get (private->display);
131 tmp_list = private->names;
134 g_hash_table_remove (hash, tmp_list->data);
135 g_free (tmp_list->data);
137 tmp_list = tmp_list->next;
140 g_slist_free (private->names);
141 private->names = NULL;
145 gdk_font_hash_lookup (GdkDisplay *display,
147 const gchar *font_name)
151 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
153 hash = (type == GDK_FONT_FONT) ? gdk_font_name_hash_get (display) :
154 gdk_fontset_name_hash_get (display);
159 result = g_hash_table_lookup (hash, font_name);
161 gdk_font_ref (result);
168 * gdk_font_load_for_display:
169 * @display: a #GdkDisplay
170 * @font_name: a XLFD describing the font to load.
171 * @returns: a #GdkFont, or %NULL if the font could not be loaded.
173 * Loads a font for use on @display.
175 * The font may be newly loaded or looked up the font in a cache.
176 * You should make no assumptions about the initial reference count.
179 gdk_font_load_for_display (GdkDisplay *display,
180 const gchar *font_name)
183 GdkFontPrivateX *private;
186 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
187 g_return_val_if_fail (font_name != NULL, NULL);
189 font = gdk_font_hash_lookup (display, GDK_FONT_FONT, font_name);
193 xfont = XLoadQueryFont (GDK_DISPLAY_XDISPLAY (display), font_name);
197 font = gdk_font_lookup_for_display (display, xfont->fid);
200 private = (GdkFontPrivateX *) font;
201 if (xfont != private->xfont)
202 XFreeFont (GDK_DISPLAY_XDISPLAY (display), xfont);
208 private = g_new (GdkFontPrivateX, 1);
209 private->display = display;
210 private->xfont = xfont;
211 private->base.ref_count = 1;
212 private->names = NULL;
214 font = (GdkFont*) private;
215 font->type = GDK_FONT_FONT;
216 font->ascent = xfont->ascent;
217 font->descent = xfont->descent;
219 _gdk_xid_table_insert (display, &xfont->fid, font);
222 gdk_font_hash_insert (GDK_FONT_FONT, font, font_name);
228 gdk_font_charset_for_locale (void)
230 static char *charset_map[][2] = {
231 { "ANSI_X3.4-1968", "iso8859-1" },
232 { "US-ASCII", "iso8859-1" },
233 { "ISO-8859-1", "iso8859-1" },
234 { "ISO-8859-2", "iso8859-2" },
235 { "ISO-8859-3", "iso8859-3" },
236 { "ISO-8859-4", "iso8859-4" },
237 { "ISO-8859-5", "iso8859-5" },
238 { "ISO-8859-6", "iso8859-6" },
239 { "ISO-8859-7", "iso8859-7" },
240 { "ISO-8859-8", "iso8859-8" },
241 { "ISO-8859-9", "iso8859-9" },
242 { "UTF-8", "iso8859-1" }
249 g_get_charset (&codeset);
251 for (i=0; i < G_N_ELEMENTS (charset_map); i++)
252 if (strcmp (charset_map[i][0], codeset) == 0)
254 result = charset_map[i][1];
259 return g_strdup (result);
261 return g_strdup ("iso8859-1");
265 * gdk_font_from_description_for_display:
266 * @display: a #GdkDisplay
267 * @font_desc: a #PangoFontDescription.
269 * Loads a #GdkFont based on a Pango font description for use on @display.
270 * This font will only be an approximation of the Pango font, and
271 * internationalization will not be handled correctly. This function
272 * should only be used for legacy code that cannot be easily converted
273 * to use Pango. Using Pango directly will produce better results.
275 * Return value: the newly loaded font, or %NULL if the font
279 gdk_font_from_description_for_display (GdkDisplay *display,
280 PangoFontDescription *font_desc)
282 PangoFontMap *font_map;
284 GdkFont *result = NULL;
286 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
287 g_return_val_if_fail (font_desc != NULL, NULL);
289 font_map = pango_x_font_map_for_display (GDK_DISPLAY_XDISPLAY (display));
290 font = pango_font_map_load_font (font_map, NULL, font_desc);
294 gchar *charset = gdk_font_charset_for_locale ();
296 PangoXSubfont *subfont_ids;
297 gint *subfont_charsets;
299 n_subfonts = pango_x_list_subfonts (font, &charset, 1,
300 &subfont_ids, &subfont_charsets);
303 gchar *xlfd = pango_x_font_subfont_xlfd (font, subfont_ids[0]);
304 result = gdk_font_load_for_display (display, xlfd);
309 g_free (subfont_ids);
311 g_free (subfont_charsets);
314 g_object_unref (font);
321 * gdk_fontset_load_for_display:
322 * @display: a #GdkDisplay
323 * @fontset_name: a comma-separated list of XLFDs describing
324 * the component fonts of the fontset to load.
325 * @returns: a #GdkFont, or %NULL if the fontset could not be loaded.
327 * Loads a fontset for use on @display.
329 * The fontset may be newly loaded or looked up in a cache.
330 * You should make no assumptions about the initial reference count.
333 gdk_fontset_load_for_display (GdkDisplay *display,
334 const gchar *fontset_name)
337 GdkFontPrivateX *private;
339 gint missing_charset_count;
340 gchar **missing_charset_list;
343 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
345 font = gdk_font_hash_lookup (display, GDK_FONT_FONTSET, fontset_name);
349 private = g_new (GdkFontPrivateX, 1);
350 font = (GdkFont*) private;
352 private->display = display;
353 fontset = XCreateFontSet (GDK_DISPLAY_XDISPLAY (display), fontset_name,
354 &missing_charset_list, &missing_charset_count,
357 if (missing_charset_count)
360 g_printerr ("The font \"%s\" does not support all the required character sets for the current locale \"%s\"\n",
361 fontset_name, setlocale (LC_ALL, NULL));
362 for (i=0;i<missing_charset_count;i++)
363 g_printerr (" (Missing character set \"%s\")\n",
364 missing_charset_list[i]);
365 XFreeStringList (missing_charset_list);
368 private->base.ref_count = 1;
379 XFontStruct **font_structs;
382 private->xfont = fontset;
383 font->type = GDK_FONT_FONTSET;
384 num_fonts = XFontsOfFontSet (fontset, &font_structs, &font_names);
386 font->ascent = font->descent = 0;
388 for (i = 0; i < num_fonts; i++)
390 font->ascent = MAX (font->ascent, font_structs[i]->ascent);
391 font->descent = MAX (font->descent, font_structs[i]->descent);
394 private->names = NULL;
395 gdk_font_hash_insert (GDK_FONT_FONTSET, font, fontset_name);
403 * @fontset_name: a comma-separated list of XLFDs describing
404 * the component fonts of the fontset to load.
408 * The fontset may be newly loaded or looked up in a cache.
409 * You should make no assumptions about the initial reference count.
411 * Return value: a #GdkFont, or %NULL if the fontset could not be loaded.
414 gdk_fontset_load (const gchar *fontset_name)
416 return gdk_fontset_load_for_display (gdk_display_get_default (), fontset_name);
420 _gdk_font_destroy (GdkFont *font)
422 GdkFontPrivateX *private = (GdkFontPrivateX *)font;
424 gdk_font_hash_remove (font->type, font);
429 _gdk_xid_table_remove (private->display, ((XFontStruct *) private->xfont)->fid);
430 XFreeFont (GDK_DISPLAY_XDISPLAY (private->display),
431 (XFontStruct *) private->xfont);
433 case GDK_FONT_FONTSET:
434 XFreeFontSet (GDK_DISPLAY_XDISPLAY (private->display),
435 (XFontSet) private->xfont);
438 g_error ("unknown font type.");
445 _gdk_font_strlen (GdkFont *font,
448 GdkFontPrivateX *font_private;
451 g_return_val_if_fail (font != NULL, -1);
452 g_return_val_if_fail (str != NULL, -1);
454 font_private = (GdkFontPrivateX*) font;
456 if (font->type == GDK_FONT_FONT)
458 XFontStruct *xfont = (XFontStruct *) font_private->xfont;
459 if ((xfont->min_byte1 == 0) && (xfont->max_byte1 == 0))
461 length = strlen (str);
465 guint16 *string_2b = (guint16 *)str;
467 while (*(string_2b++))
471 else if (font->type == GDK_FONT_FONTSET)
473 length = strlen (str);
476 g_error("undefined font type\n");
485 * Returns the X Font ID for the given font.
487 * Return value: the numeric X Font ID
490 gdk_font_id (const GdkFont *font)
492 const GdkFontPrivateX *font_private;
494 g_return_val_if_fail (font != NULL, 0);
496 font_private = (const GdkFontPrivateX*) font;
498 if (font->type == GDK_FONT_FONT)
500 return ((XFontStruct *) font_private->xfont)->fid;
510 * @fonta: a #GdkFont.
511 * @fontb: another #GdkFont.
513 * Compares two fonts for equality. Single fonts compare equal
514 * if they have the same X font ID. This operation does
515 * not currently work correctly for fontsets.
517 * Return value: %TRUE if the fonts are equal.
520 gdk_font_equal (const GdkFont *fonta,
521 const GdkFont *fontb)
523 const GdkFontPrivateX *privatea;
524 const GdkFontPrivateX *privateb;
526 g_return_val_if_fail (fonta != NULL, FALSE);
527 g_return_val_if_fail (fontb != NULL, FALSE);
529 privatea = (const GdkFontPrivateX*) fonta;
530 privateb = (const GdkFontPrivateX*) fontb;
532 if (fonta->type == GDK_FONT_FONT && fontb->type == GDK_FONT_FONT)
534 return (((XFontStruct *) privatea->xfont)->fid ==
535 ((XFontStruct *) privateb->xfont)->fid);
537 else if (fonta->type == GDK_FONT_FONTSET && fontb->type == GDK_FONT_FONTSET)
539 gchar *namea, *nameb;
541 namea = XBaseFontNameListOfFontSet((XFontSet) privatea->xfont);
542 nameb = XBaseFontNameListOfFontSet((XFontSet) privateb->xfont);
544 return (strcmp(namea, nameb) == 0);
547 /* fontset != font */
554 * @text: the text to measure.
555 * @text_length: the length of the text in bytes.
557 * Determines the width of a given string.
559 * Return value: the width of the string in pixels.
562 gdk_text_width (GdkFont *font,
566 GdkFontPrivateX *private;
571 g_return_val_if_fail (font != NULL, -1);
572 g_return_val_if_fail (text != NULL, -1);
574 private = (GdkFontPrivateX*) font;
579 xfont = (XFontStruct *) private->xfont;
580 if ((xfont->min_byte1 == 0) && (xfont->max_byte1 == 0))
582 width = XTextWidth (xfont, text, text_length);
586 width = XTextWidth16 (xfont, (XChar2b *) text, text_length / 2);
589 case GDK_FONT_FONTSET:
590 fontset = (XFontSet) private->xfont;
591 width = XmbTextEscapement (fontset, text, text_length);
602 * @text: the text to measure.
603 * @text_length: the length of the text in characters.
605 * Determines the width of a given wide-character string.
607 * Return value: the width of the string in pixels.
610 gdk_text_width_wc (GdkFont *font,
611 const GdkWChar *text,
614 GdkFontPrivateX *private;
619 g_return_val_if_fail (font != NULL, -1);
620 g_return_val_if_fail (text != NULL, -1);
622 private = (GdkFontPrivateX*) font;
627 xfont = (XFontStruct *) private->xfont;
628 if ((xfont->min_byte1 == 0) && (xfont->max_byte1 == 0))
632 text_8bit = g_new (gchar, text_length);
633 for (i=0; i<text_length; i++) text_8bit[i] = text[i];
634 width = XTextWidth (xfont, text_8bit, text_length);
642 case GDK_FONT_FONTSET:
643 if (sizeof(GdkWChar) == sizeof(wchar_t))
645 fontset = (XFontSet) private->xfont;
646 width = XwcTextEscapement (fontset, (wchar_t *)text, text_length);
652 fontset = (XFontSet) private->xfont;
653 text_wchar = g_new(wchar_t, text_length);
654 for (i=0; i<text_length; i++) text_wchar[i] = text[i];
655 width = XwcTextEscapement (fontset, text_wchar, text_length);
668 * @text: the text to measure
669 * @text_length: the length of the text in bytes. (If the
670 * font is a 16-bit font, this is twice the length
671 * of the text in characters.)
672 * @lbearing: the left bearing of the string.
673 * @rbearing: the right bearing of the string.
674 * @width: the width of the string.
675 * @ascent: the ascent of the string.
676 * @descent: the descent of the string.
678 * Gets the metrics of a string.
681 gdk_text_extents (GdkFont *font,
690 GdkFontPrivateX *private;
694 XRectangle ink, logical;
699 g_return_if_fail (font != NULL);
700 g_return_if_fail (text != NULL);
702 private = (GdkFontPrivateX*) font;
707 xfont = (XFontStruct *) private->xfont;
708 if ((xfont->min_byte1 == 0) && (xfont->max_byte1 == 0))
710 XTextExtents (xfont, text, text_length,
711 &direction, &font_ascent, &font_descent,
716 XTextExtents16 (xfont, (XChar2b *) text, text_length / 2,
717 &direction, &font_ascent, &font_descent,
721 *lbearing = overall.lbearing;
723 *rbearing = overall.rbearing;
725 *width = overall.width;
727 *ascent = overall.ascent;
729 *descent = overall.descent;
731 case GDK_FONT_FONTSET:
732 fontset = (XFontSet) private->xfont;
733 XmbTextExtents (fontset, text, text_length, &ink, &logical);
737 *rbearing = ink.x + ink.width;
739 *width = logical.width;
743 *descent = ink.y + ink.height;
750 * gdk_text_extents_wc:
752 * @text: the text to measure.
753 * @text_length: the length of the text in character.
754 * @lbearing: the left bearing of the string.
755 * @rbearing: the right bearing of the string.
756 * @width: the width of the string.
757 * @ascent: the ascent of the string.
758 * @descent: the descent of the string.
760 * Gets the metrics of a string of wide characters.
763 gdk_text_extents_wc (GdkFont *font,
764 const GdkWChar *text,
772 GdkFontPrivateX *private;
776 XRectangle ink, logical;
781 g_return_if_fail (font != NULL);
782 g_return_if_fail (text != NULL);
784 private = (GdkFontPrivateX*) font;
793 xfont = (XFontStruct *) private->xfont;
794 g_return_if_fail ((xfont->min_byte1 == 0) && (xfont->max_byte1 == 0));
796 text_8bit = g_new (gchar, text_length);
797 for (i=0; i<text_length; i++)
798 text_8bit[i] = text[i];
800 XTextExtents (xfont, text_8bit, text_length,
801 &direction, &font_ascent, &font_descent,
806 *lbearing = overall.lbearing;
808 *rbearing = overall.rbearing;
810 *width = overall.width;
812 *ascent = overall.ascent;
814 *descent = overall.descent;
817 case GDK_FONT_FONTSET:
818 fontset = (XFontSet) private->xfont;
820 if (sizeof(GdkWChar) == sizeof(wchar_t))
821 XwcTextExtents (fontset, (wchar_t *)text, text_length, &ink, &logical);
827 text_wchar = g_new (wchar_t, text_length);
828 for (i = 0; i < text_length; i++)
829 text_wchar[i] = text[i];
830 XwcTextExtents (fontset, text_wchar, text_length, &ink, &logical);
836 *rbearing = ink.x + ink.width;
838 *width = logical.width;
842 *descent = ink.y + ink.height;
849 * gdk_x11_font_get_xdisplay:
852 * Returns the display of a #GdkFont.
854 * Return value: an Xlib <type>Display*</type>.
857 gdk_x11_font_get_xdisplay (GdkFont *font)
859 g_return_val_if_fail (font != NULL, NULL);
861 return GDK_DISPLAY_XDISPLAY (((GdkFontPrivateX *)font)->display);
865 * gdk_x11_font_get_xfont:
868 * Returns the X font belonging to a #GdkFont.
870 * Return value: an Xlib <type>XFontStruct*</type> or an <type>XFontSet</type>.
873 gdk_x11_font_get_xfont (GdkFont *font)
875 g_return_val_if_fail (font != NULL, NULL);
877 return ((GdkFontPrivateX *)font)->xfont;
881 * gdk_x11_font_get_name:
884 * Return the X Logical Font Description (for font->type == GDK_FONT_FONT)
885 * or comma separated list of XLFDs (for font->type == GDK_FONT_FONTSET)
886 * that was used to load the font. If the same font was loaded
887 * via multiple names, which name is returned is undefined.
889 * Return value: the name of the font. This string is owned
890 * by GDK and must not be modified or freed.
892 G_CONST_RETURN char *
893 gdk_x11_font_get_name (GdkFont *font)
895 GdkFontPrivateX *private = (GdkFontPrivateX *)font;
897 g_return_val_if_fail (font != NULL, NULL);
899 g_assert (private->names);
901 return private->names->data;