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.
106 gdk_font_get_display (GdkFont* font)
108 return ((GdkFontPrivateX *)font)->display;
112 gdk_font_hash_insert (GdkFontType type,
114 const gchar *font_name)
116 GdkFontPrivateX *private = (GdkFontPrivateX *)font;
117 GHashTable *hash = (type == GDK_FONT_FONT) ?
118 gdk_font_name_hash_get (private->display) : gdk_fontset_name_hash_get (private->display);
120 private->names = g_slist_prepend (private->names, g_strdup (font_name));
121 g_hash_table_insert (hash, private->names->data, font);
125 gdk_font_hash_remove (GdkFontType type,
128 GdkFontPrivateX *private = (GdkFontPrivateX *)font;
130 GHashTable *hash = (type == GDK_FONT_FONT) ?
131 gdk_font_name_hash_get (private->display) : gdk_fontset_name_hash_get (private->display);
133 tmp_list = private->names;
136 g_hash_table_remove (hash, tmp_list->data);
137 g_free (tmp_list->data);
139 tmp_list = tmp_list->next;
142 g_slist_free (private->names);
143 private->names = NULL;
147 gdk_font_hash_lookup (GdkDisplay *display,
149 const gchar *font_name)
153 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
155 hash = (type == GDK_FONT_FONT) ? gdk_font_name_hash_get (display) :
156 gdk_fontset_name_hash_get (display);
161 result = g_hash_table_lookup (hash, font_name);
163 gdk_font_ref (result);
170 * gdk_font_load_for_display:
171 * @display: a #GdkDisplay
172 * @font_name: a XLFD describing the font to load.
173 * @returns: a #GdkFont, or %NULL if the font could not be loaded.
175 * Loads a font for use on @display.
177 * The font may be newly loaded or looked up the font in a cache.
178 * You should make no assumptions about the initial reference count.
183 gdk_font_load_for_display (GdkDisplay *display,
184 const gchar *font_name)
187 GdkFontPrivateX *private;
190 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
191 g_return_val_if_fail (font_name != NULL, NULL);
193 font = gdk_font_hash_lookup (display, GDK_FONT_FONT, font_name);
197 xfont = XLoadQueryFont (GDK_DISPLAY_XDISPLAY (display), font_name);
201 font = gdk_font_lookup_for_display (display, xfont->fid);
204 private = (GdkFontPrivateX *) font;
205 if (xfont != private->xfont)
206 XFreeFont (GDK_DISPLAY_XDISPLAY (display), xfont);
212 private = g_new (GdkFontPrivateX, 1);
213 private->display = display;
214 private->xfont = xfont;
215 private->base.ref_count = 1;
216 private->names = NULL;
218 font = (GdkFont*) private;
219 font->type = GDK_FONT_FONT;
220 font->ascent = xfont->ascent;
221 font->descent = xfont->descent;
223 _gdk_xid_table_insert (display, &xfont->fid, font);
226 gdk_font_hash_insert (GDK_FONT_FONT, font, font_name);
232 gdk_font_charset_for_locale (void)
234 static char *charset_map[][2] = {
235 { "ANSI_X3.4-1968", "iso8859-1" },
236 { "US-ASCII", "iso8859-1" },
237 { "ISO-8859-1", "iso8859-1" },
238 { "ISO-8859-2", "iso8859-2" },
239 { "ISO-8859-3", "iso8859-3" },
240 { "ISO-8859-4", "iso8859-4" },
241 { "ISO-8859-5", "iso8859-5" },
242 { "ISO-8859-6", "iso8859-6" },
243 { "ISO-8859-7", "iso8859-7" },
244 { "ISO-8859-8", "iso8859-8" },
245 { "ISO-8859-9", "iso8859-9" },
246 { "UTF-8", "iso8859-1" }
253 g_get_charset (&codeset);
255 for (i=0; i < G_N_ELEMENTS (charset_map); i++)
256 if (strcmp (charset_map[i][0], codeset) == 0)
258 result = charset_map[i][1];
263 return g_strdup (result);
265 return g_strdup ("iso8859-1");
269 * gdk_font_from_description_for_display:
270 * @display: a #GdkDisplay
271 * @font_desc: a #PangoFontDescription.
273 * Loads a #GdkFont based on a Pango font description for use on @display.
274 * This font will only be an approximation of the Pango font, and
275 * internationalization will not be handled correctly. This function
276 * should only be used for legacy code that cannot be easily converted
277 * to use Pango. Using Pango directly will produce better results.
279 * Return value: the newly loaded font, or %NULL if the font
285 gdk_font_from_description_for_display (GdkDisplay *display,
286 PangoFontDescription *font_desc)
288 PangoFontMap *font_map;
290 GdkFont *result = NULL;
292 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
293 g_return_val_if_fail (font_desc != NULL, NULL);
295 font_map = pango_x_font_map_for_display (GDK_DISPLAY_XDISPLAY (display));
296 font = pango_font_map_load_font (font_map, NULL, font_desc);
300 gchar *charset = gdk_font_charset_for_locale ();
302 PangoXSubfont *subfont_ids;
303 gint *subfont_charsets;
305 n_subfonts = pango_x_list_subfonts (font, &charset, 1,
306 &subfont_ids, &subfont_charsets);
309 gchar *xlfd = pango_x_font_subfont_xlfd (font, subfont_ids[0]);
310 result = gdk_font_load_for_display (display, xlfd);
315 g_free (subfont_ids);
317 g_free (subfont_charsets);
320 g_object_unref (font);
327 * gdk_fontset_load_for_display:
328 * @display: a #GdkDisplay
329 * @fontset_name: a comma-separated list of XLFDs describing
330 * the component fonts of the fontset to load.
331 * @returns: a #GdkFont, or %NULL if the fontset could not be loaded.
333 * Loads a fontset for use on @display.
335 * The fontset may be newly loaded or looked up in a cache.
336 * You should make no assumptions about the initial reference count.
341 gdk_fontset_load_for_display (GdkDisplay *display,
342 const gchar *fontset_name)
345 GdkFontPrivateX *private;
347 gint missing_charset_count;
348 gchar **missing_charset_list;
351 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
353 font = gdk_font_hash_lookup (display, GDK_FONT_FONTSET, fontset_name);
357 private = g_new (GdkFontPrivateX, 1);
358 font = (GdkFont*) private;
360 private->display = display;
361 fontset = XCreateFontSet (GDK_DISPLAY_XDISPLAY (display), fontset_name,
362 &missing_charset_list, &missing_charset_count,
365 if (missing_charset_count)
368 g_printerr ("The font \"%s\" does not support all the required character sets for the current locale \"%s\"\n",
369 fontset_name, setlocale (LC_ALL, NULL));
370 for (i=0;i<missing_charset_count;i++)
371 g_printerr (" (Missing character set \"%s\")\n",
372 missing_charset_list[i]);
373 XFreeStringList (missing_charset_list);
376 private->base.ref_count = 1;
387 XFontStruct **font_structs;
390 private->xfont = fontset;
391 font->type = GDK_FONT_FONTSET;
392 num_fonts = XFontsOfFontSet (fontset, &font_structs, &font_names);
394 font->ascent = font->descent = 0;
396 for (i = 0; i < num_fonts; i++)
398 font->ascent = MAX (font->ascent, font_structs[i]->ascent);
399 font->descent = MAX (font->descent, font_structs[i]->descent);
402 private->names = NULL;
403 gdk_font_hash_insert (GDK_FONT_FONTSET, font, fontset_name);
411 * @fontset_name: a comma-separated list of XLFDs describing
412 * the component fonts of the fontset to load.
416 * The fontset may be newly loaded or looked up in a cache.
417 * You should make no assumptions about the initial reference count.
419 * Return value: a #GdkFont, or %NULL if the fontset could not be loaded.
422 gdk_fontset_load (const gchar *fontset_name)
424 return gdk_fontset_load_for_display (gdk_display_get_default (), fontset_name);
428 _gdk_font_destroy (GdkFont *font)
430 GdkFontPrivateX *private = (GdkFontPrivateX *)font;
432 gdk_font_hash_remove (font->type, font);
437 _gdk_xid_table_remove (private->display, ((XFontStruct *) private->xfont)->fid);
438 XFreeFont (GDK_DISPLAY_XDISPLAY (private->display),
439 (XFontStruct *) private->xfont);
441 case GDK_FONT_FONTSET:
442 XFreeFontSet (GDK_DISPLAY_XDISPLAY (private->display),
443 (XFontSet) private->xfont);
446 g_error ("unknown font type.");
453 _gdk_font_strlen (GdkFont *font,
456 GdkFontPrivateX *font_private;
459 g_return_val_if_fail (font != NULL, -1);
460 g_return_val_if_fail (str != NULL, -1);
462 font_private = (GdkFontPrivateX*) font;
464 if (font->type == GDK_FONT_FONT)
466 XFontStruct *xfont = (XFontStruct *) font_private->xfont;
467 if ((xfont->min_byte1 == 0) && (xfont->max_byte1 == 0))
469 length = strlen (str);
473 guint16 *string_2b = (guint16 *)str;
475 while (*(string_2b++))
479 else if (font->type == GDK_FONT_FONTSET)
481 length = strlen (str);
484 g_error("undefined font type\n");
493 * Returns the X Font ID for the given font.
495 * Return value: the numeric X Font ID
498 gdk_font_id (const GdkFont *font)
500 const GdkFontPrivateX *font_private;
502 g_return_val_if_fail (font != NULL, 0);
504 font_private = (const GdkFontPrivateX*) font;
506 if (font->type == GDK_FONT_FONT)
508 return ((XFontStruct *) font_private->xfont)->fid;
518 * @fonta: a #GdkFont.
519 * @fontb: another #GdkFont.
521 * Compares two fonts for equality. Single fonts compare equal
522 * if they have the same X font ID. This operation does
523 * not currently work correctly for fontsets.
525 * Return value: %TRUE if the fonts are equal.
528 gdk_font_equal (const GdkFont *fonta,
529 const GdkFont *fontb)
531 const GdkFontPrivateX *privatea;
532 const GdkFontPrivateX *privateb;
534 g_return_val_if_fail (fonta != NULL, FALSE);
535 g_return_val_if_fail (fontb != NULL, FALSE);
537 privatea = (const GdkFontPrivateX*) fonta;
538 privateb = (const GdkFontPrivateX*) fontb;
540 if (fonta->type == GDK_FONT_FONT && fontb->type == GDK_FONT_FONT)
542 return (((XFontStruct *) privatea->xfont)->fid ==
543 ((XFontStruct *) privateb->xfont)->fid);
545 else if (fonta->type == GDK_FONT_FONTSET && fontb->type == GDK_FONT_FONTSET)
547 gchar *namea, *nameb;
549 namea = XBaseFontNameListOfFontSet((XFontSet) privatea->xfont);
550 nameb = XBaseFontNameListOfFontSet((XFontSet) privateb->xfont);
552 return (strcmp(namea, nameb) == 0);
555 /* fontset != font */
562 * @text: the text to measure.
563 * @text_length: the length of the text in bytes.
565 * Determines the width of a given string.
567 * Return value: the width of the string in pixels.
570 gdk_text_width (GdkFont *font,
574 GdkFontPrivateX *private;
579 g_return_val_if_fail (font != NULL, -1);
580 g_return_val_if_fail (text != NULL, -1);
582 private = (GdkFontPrivateX*) font;
587 xfont = (XFontStruct *) private->xfont;
588 if ((xfont->min_byte1 == 0) && (xfont->max_byte1 == 0))
590 width = XTextWidth (xfont, text, text_length);
594 width = XTextWidth16 (xfont, (XChar2b *) text, text_length / 2);
597 case GDK_FONT_FONTSET:
598 fontset = (XFontSet) private->xfont;
599 width = XmbTextEscapement (fontset, text, text_length);
610 * @text: the text to measure.
611 * @text_length: the length of the text in characters.
613 * Determines the width of a given wide-character string.
615 * Return value: the width of the string in pixels.
618 gdk_text_width_wc (GdkFont *font,
619 const GdkWChar *text,
622 GdkFontPrivateX *private;
627 g_return_val_if_fail (font != NULL, -1);
628 g_return_val_if_fail (text != NULL, -1);
630 private = (GdkFontPrivateX*) font;
635 xfont = (XFontStruct *) private->xfont;
636 if ((xfont->min_byte1 == 0) && (xfont->max_byte1 == 0))
640 text_8bit = g_new (gchar, text_length);
641 for (i=0; i<text_length; i++) text_8bit[i] = text[i];
642 width = XTextWidth (xfont, text_8bit, text_length);
650 case GDK_FONT_FONTSET:
651 if (sizeof(GdkWChar) == sizeof(wchar_t))
653 fontset = (XFontSet) private->xfont;
654 width = XwcTextEscapement (fontset, (wchar_t *)text, text_length);
660 fontset = (XFontSet) private->xfont;
661 text_wchar = g_new(wchar_t, text_length);
662 for (i=0; i<text_length; i++) text_wchar[i] = text[i];
663 width = XwcTextEscapement (fontset, text_wchar, text_length);
676 * @text: the text to measure
677 * @text_length: the length of the text in bytes. (If the
678 * font is a 16-bit font, this is twice the length
679 * of the text in characters.)
680 * @lbearing: the left bearing of the string.
681 * @rbearing: the right bearing of the string.
682 * @width: the width of the string.
683 * @ascent: the ascent of the string.
684 * @descent: the descent of the string.
686 * Gets the metrics of a string.
689 gdk_text_extents (GdkFont *font,
698 GdkFontPrivateX *private;
702 XRectangle ink, logical;
707 g_return_if_fail (font != NULL);
708 g_return_if_fail (text != NULL);
710 private = (GdkFontPrivateX*) font;
715 xfont = (XFontStruct *) private->xfont;
716 if ((xfont->min_byte1 == 0) && (xfont->max_byte1 == 0))
718 XTextExtents (xfont, text, text_length,
719 &direction, &font_ascent, &font_descent,
724 XTextExtents16 (xfont, (XChar2b *) text, text_length / 2,
725 &direction, &font_ascent, &font_descent,
729 *lbearing = overall.lbearing;
731 *rbearing = overall.rbearing;
733 *width = overall.width;
735 *ascent = overall.ascent;
737 *descent = overall.descent;
739 case GDK_FONT_FONTSET:
740 fontset = (XFontSet) private->xfont;
741 XmbTextExtents (fontset, text, text_length, &ink, &logical);
745 *rbearing = ink.x + ink.width;
747 *width = logical.width;
751 *descent = ink.y + ink.height;
758 * gdk_text_extents_wc:
760 * @text: the text to measure.
761 * @text_length: the length of the text in character.
762 * @lbearing: the left bearing of the string.
763 * @rbearing: the right bearing of the string.
764 * @width: the width of the string.
765 * @ascent: the ascent of the string.
766 * @descent: the descent of the string.
768 * Gets the metrics of a string of wide characters.
771 gdk_text_extents_wc (GdkFont *font,
772 const GdkWChar *text,
780 GdkFontPrivateX *private;
784 XRectangle ink, logical;
789 g_return_if_fail (font != NULL);
790 g_return_if_fail (text != NULL);
792 private = (GdkFontPrivateX*) font;
801 xfont = (XFontStruct *) private->xfont;
802 g_return_if_fail ((xfont->min_byte1 == 0) && (xfont->max_byte1 == 0));
804 text_8bit = g_new (gchar, text_length);
805 for (i=0; i<text_length; i++)
806 text_8bit[i] = text[i];
808 XTextExtents (xfont, text_8bit, text_length,
809 &direction, &font_ascent, &font_descent,
814 *lbearing = overall.lbearing;
816 *rbearing = overall.rbearing;
818 *width = overall.width;
820 *ascent = overall.ascent;
822 *descent = overall.descent;
825 case GDK_FONT_FONTSET:
826 fontset = (XFontSet) private->xfont;
828 if (sizeof(GdkWChar) == sizeof(wchar_t))
829 XwcTextExtents (fontset, (wchar_t *)text, text_length, &ink, &logical);
835 text_wchar = g_new (wchar_t, text_length);
836 for (i = 0; i < text_length; i++)
837 text_wchar[i] = text[i];
838 XwcTextExtents (fontset, text_wchar, text_length, &ink, &logical);
844 *rbearing = ink.x + ink.width;
846 *width = logical.width;
850 *descent = ink.y + ink.height;
857 * gdk_x11_font_get_xdisplay:
860 * Returns the display of a #GdkFont.
862 * Return value: an Xlib <type>Display*</type>.
865 gdk_x11_font_get_xdisplay (GdkFont *font)
867 g_return_val_if_fail (font != NULL, NULL);
869 return GDK_DISPLAY_XDISPLAY (((GdkFontPrivateX *)font)->display);
873 * gdk_x11_font_get_xfont:
876 * Returns the X font belonging to a #GdkFont.
878 * Return value: an Xlib <type>XFontStruct*</type> or an <type>XFontSet</type>.
881 gdk_x11_font_get_xfont (GdkFont *font)
883 g_return_val_if_fail (font != NULL, NULL);
885 return ((GdkFontPrivateX *)font)->xfont;
889 * gdk_x11_font_get_name:
892 * Return the X Logical Font Description (for font->type == GDK_FONT_FONT)
893 * or comma separated list of XLFDs (for font->type == GDK_FONT_FONTSET)
894 * that was used to load the font. If the same font was loaded
895 * via multiple names, which name is returned is undefined.
897 * Return value: the name of the font. This string is owned
898 * by GDK and must not be modified or freed.
900 G_CONST_RETURN char *
901 gdk_x11_font_get_name (GdkFont *font)
903 GdkFontPrivateX *private = (GdkFontPrivateX *)font;
905 g_return_val_if_fail (font != NULL, NULL);
907 g_assert (private->names);
909 return private->names->data;