]> Pileus Git - ~andy/gtk/blobdiff - gdk/gdkfont.c
Fix #99593: Fix a memory leak when XmbLookupString returns XBufferOverflow
[~andy/gtk] / gdk / gdkfont.c
index 8b45be39404a086fcc038ce92f03f8bffcd79496..02dd2f214b58de733d22639356080bd7570ffa90 100644 (file)
  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
  *
  * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
+ * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Library General Public License for more details.
+ * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU Library General Public
- * License along with this library; if not, write to the Free
- * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
  */
-#include <X11/Xlib.h>
-#include <X11/Xos.h>
-#include "gdk.h"
-#include "gdkprivate.h"
 
-GdkFont*
-gdk_font_load (const gchar *font_name)
-{
-  GdkFont *font;
-  GdkFontPrivate *private;
-
-  private = g_new (GdkFontPrivate, 1);
-  font = (GdkFont*) private;
-
-  private->xdisplay = gdk_display;
-  private->xfont = XLoadQueryFont (private->xdisplay, font_name);
-  private->ref_count = 1;
-
-  if (!private->xfont)
-    {
-      g_free (font);
-      return NULL;
-    }
-  else
-    {
-      font->type = GDK_FONT_FONT;
-      font->ascent =  ((XFontStruct *) private->xfont)->ascent;
-      font->descent = ((XFontStruct *) private->xfont)->descent;
-    }
+/*
+ * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
+ * file for a list of people on the GTK+ Team.  See the ChangeLog
+ * files for a list of changes.  These files are distributed with
+ * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
+ */
 
-  gdk_xid_table_insert (&((XFontStruct *) private->xfont)->fid, font);
+#undef GDK_DISABLE_DEPRECATED
 
-  return font;
-}
+#include "gdkdisplay.h"
+#include "gdkfont.h"
+#include "gdkinternals.h"
 
-GdkFont*
-gdk_fontset_load (gchar *fontset_name)
+GType
+gdk_font_get_type (void)
 {
-  GdkFont *font;
-  GdkFontPrivate *private;
-  XFontSet fontset;
-  gint  missing_charset_count;
-  gchar **missing_charset_list;
-  gchar *def_string;
-
-  private = g_new (GdkFontPrivate, 1);
-  font = (GdkFont*) private;
-
-  private->xdisplay = gdk_display;
-  fontset = XCreateFontSet (gdk_display, fontset_name,
-                           &missing_charset_list, &missing_charset_count,
-                           &def_string);
-
-  if (missing_charset_count)
-    {
-      gint i;
-      g_print ("Missing charsets in FontSet creation\n");
-      for (i=0;i<missing_charset_count;i++)
-       g_print ("    %s\n", missing_charset_list[i]);
-      XFreeStringList (missing_charset_list);
-    }
-
-  private->ref_count = 1;
-
-  if (!fontset)
-    {
-      g_free (font);
-      return NULL;
-    }
-  else
-    {
-      gint num_fonts;
-      gint i;
-      XFontStruct **font_structs;
-      gchar **font_names;
-      
-      private->xfont = fontset;
-      font->type = GDK_FONT_FONTSET;
-      num_fonts = XFontsOfFontSet (fontset, &font_structs, &font_names);
-
-      font->ascent = font->descent = 0;
-      
-      for (i = 0; i < num_fonts; i++)
-       {
-         font->ascent = MAX (font->ascent, font_structs[i]->ascent);
-         font->descent = MAX (font->descent, font_structs[i]->descent);
-       }
-    }
-  return font;
+  static GType our_type = 0;
+  
+  if (our_type == 0)
+    our_type = g_boxed_type_register_static ("GdkFont",
+                                            (GBoxedCopyFunc)gdk_font_ref,
+                                            (GBoxedFreeFunc)gdk_font_unref);
+  return our_type;
 }
 
+/**
+ * gdk_font_ref:
+ * @font: a #GdkFont
+ * 
+ * Increases the reference count of a font by one.
+ * 
+ * Return value: @font
+ **/
 GdkFont*
 gdk_font_ref (GdkFont *font)
 {
@@ -118,269 +62,291 @@ gdk_font_ref (GdkFont *font)
   return font;
 }
 
+/**
+ * gdk_font_unref:
+ * @font: a #GdkFont
+ * 
+ * Decreases the reference count of a font by one.
+ * If the result is zero, destroys the font.
+ **/
 void
 gdk_font_unref (GdkFont *font)
 {
   GdkFontPrivate *private;
+  private = (GdkFontPrivate*) font;
 
   g_return_if_fail (font != NULL);
-
-  private = (GdkFontPrivate*) font;
+  g_return_if_fail (private->ref_count > 0);
 
   private->ref_count -= 1;
   if (private->ref_count == 0)
-    {
-      switch (font->type)
-       {
-       case GDK_FONT_FONT:
-         gdk_xid_table_remove (((XFontStruct *) private->xfont)->fid);
-         XFreeFont (private->xdisplay, (XFontStruct *) private->xfont);
-         break;
-       case GDK_FONT_FONTSET:
-         XFreeFontSet (private->xdisplay, (XFontSet) private->xfont);
-         break;
-       default:
-         g_error ("unknown font type.");
-         break;
-       }
-      g_free (font);
-    }
+    _gdk_font_destroy (font);
 }
 
+/**
+ * gdk_string_width:
+ * @font:  a #GdkFont
+ * @string: the nul-terminated string to measure
+ * 
+ * Determines the width of a nul-terminated string.
+ * (The distance from the origin of the string to the 
+ * point where the next string in a sequence of strings
+ * should be drawn)
+ * 
+ * Return value: the width of the string in pixels.
+ **/
 gint
-gdk_font_id (GdkFont *font)
+gdk_string_width (GdkFont     *font,
+                 const gchar *string)
 {
-  GdkFontPrivate *font_private;
+  g_return_val_if_fail (font != NULL, -1);
+  g_return_val_if_fail (string != NULL, -1);
 
-  g_return_val_if_fail (font != NULL, 0);
+  return gdk_text_width (font, string, _gdk_font_strlen (font, string));
+}
 
-  font_private = (GdkFontPrivate*) font;
+/**
+ * gdk_char_width:
+ * @font: a #GdkFont
+ * @character: the character to measure.
+ * 
+ * Determines the width of a given character.
+ * 
+ * Return value: the width of the character in pixels.
+ **/
+gint
+gdk_char_width (GdkFont *font,
+               gchar    character)
+{
+  g_return_val_if_fail (font != NULL, -1);
 
-  if (font->type == GDK_FONT_FONT)
-    {
-      return ((XFontStruct *) font_private->xfont)->fid;
-    }
-  else
-    {
-      return 0;
-    }
+  return gdk_text_width (font, &character, 1);
 }
 
+/**
+ * gdk_char_width_wc:
+ * @font: a #GdkFont
+ * @character: the character to measure.
+ * 
+ * Determines the width of a given wide character. (Encoded
+ * in the wide-character encoding of the current locale).
+ * 
+ * Return value: the width of the character in pixels.
+ **/
 gint
-gdk_font_equal (GdkFont *fonta,
-                GdkFont *fontb)
+gdk_char_width_wc (GdkFont *font,
+                  GdkWChar character)
 {
-  GdkFontPrivate *privatea;
-  GdkFontPrivate *privateb;
-
-  g_return_val_if_fail (fonta != NULL, FALSE);
-  g_return_val_if_fail (fontb != NULL, FALSE);
-
-  privatea = (GdkFontPrivate*) fonta;
-  privateb = (GdkFontPrivate*) fontb;
-
-  if (fonta->type == GDK_FONT_FONT && fontb->type == GDK_FONT_FONT)
-    {
-      return (((XFontStruct *) privatea->xfont)->fid ==
-             ((XFontStruct *) privateb->xfont)->fid);
-    }
-  else if (fonta->type == GDK_FONT_FONTSET && fontb->type == GDK_FONT_FONTSET)
-    {
-      /* how to compare two fontsets ?? by basename or XFontSet ?? */
-      return (((XFontSet) privatea->xfont) == ((XFontSet) privateb->xfont));
-    }
-  else
-    /* fontset != font */
-    return 0;
+  g_return_val_if_fail (font != NULL, -1);
+
+  return gdk_text_width_wc (font, &character, 1);
 }
 
+/**
+ * gdk_string_measure:
+ * @font: a #GdkFont
+ * @string: the nul-terminated string to measure.
+ * 
+ * Determines the distance from the origin to the rightmost
+ * portion of a nul-terminated string when drawn. This is not the
+ * correct value for determining the origin of the next
+ * portion when drawing text in multiple pieces.
+ * See gdk_string_width().
+ * 
+ * Return value: the right bearing of the string in pixels.
+ **/
 gint
-gdk_string_width (GdkFont     *font,
-                 const gchar *string)
+gdk_string_measure (GdkFont     *font,
+                    const gchar *string)
 {
-  GdkFontPrivate *font_private;
-  gint width;
-  XFontStruct *xfont;
-  XFontSet fontset;
-
   g_return_val_if_fail (font != NULL, -1);
   g_return_val_if_fail (string != NULL, -1);
 
-  font_private = (GdkFontPrivate*) font;
-
-  switch (font->type)
-    {
-    case GDK_FONT_FONT:
-      xfont = (XFontStruct *) font_private->xfont;
-      if ((xfont->min_byte1 == 0) && (xfont->max_byte1 == 0))
-       {
-         width = XTextWidth (xfont, string, strlen (string));
-       }
-      else
-       {
-         width = XTextWidth16 (xfont, (XChar2b *) string, strlen (string) / 2);
-       }
-      break;
-    case GDK_FONT_FONTSET:
-      fontset = (XFontSet) font_private->xfont;
-      width = XmbTextEscapement (fontset, string, strlen(string));
-      break;
-    default:
-      width = 0;
-    }
-
-  return width;
+  return gdk_text_measure (font, string, _gdk_font_strlen (font, string));
+}
+
+/**
+ * gdk_string_extents:
+ * @font: a #GdkFont.
+ * @string: the nul-terminated string to measure.
+ * @lbearing: the left bearing of the string.
+ * @rbearing: the right bearing of the string.
+ * @width: the width of the string.
+ * @ascent: the ascent of the string.
+ * @descent: the descent of the string.
+ * 
+ * Gets the metrics of a nul-terminated string.
+ **/
+void
+gdk_string_extents (GdkFont     *font,
+                   const gchar *string,
+                   gint        *lbearing,
+                   gint        *rbearing,
+                   gint        *width,
+                   gint        *ascent,
+                   gint        *descent)
+{
+  g_return_if_fail (font != NULL);
+  g_return_if_fail (string != NULL);
+
+  gdk_text_extents (font, string, _gdk_font_strlen (font, string),
+                   lbearing, rbearing, width, ascent, descent);
 }
 
+
+/**
+ * gdk_text_measure:
+ * @font: a #GdkFont
+ * @text: the text to measure.
+ * @text_length: the length of the text in bytes.
+ * 
+ * Determines the distance from the origin to the rightmost
+ * portion of a string when drawn. This is not the
+ * correct value for determining the origin of the next
+ * portion when drawing text in multiple pieces. 
+ * See gdk_text_width().
+ * 
+ * Return value: the right bearing of the string in pixels.
+ **/
 gint
-gdk_text_width (GdkFont      *font,
-               const gchar  *text,
-               gint          text_length)
+gdk_text_measure (GdkFont     *font,
+                  const gchar *text,
+                  gint         text_length)
 {
-  GdkFontPrivate *private;
-  gint width;
-  XFontStruct *xfont;
-  XFontSet fontset;
+  gint rbearing;
 
   g_return_val_if_fail (font != NULL, -1);
   g_return_val_if_fail (text != NULL, -1);
 
-  private = (GdkFontPrivate*) font;
-
-  switch (font->type)
-    {
-    case GDK_FONT_FONT:
-      xfont = (XFontStruct *) private->xfont;
-      if ((xfont->min_byte1 == 0) && (xfont->max_byte1 == 0))
-       {
-         width = XTextWidth (xfont, text, text_length);
-       }
-      else
-       {
-         width = XTextWidth16 (xfont, (XChar2b *) text, text_length / 2);
-       }
-      break;
-    case GDK_FONT_FONTSET:
-      fontset = (XFontSet) private->xfont;
-      width = XmbTextEscapement (fontset, text, text_length);
-      break;
-    default:
-      width = 0;
-    }
-  return width;
+  gdk_text_extents (font, text, text_length, NULL, &rbearing, NULL, NULL, NULL);
+  return rbearing;
 }
 
-/* Problem: What if a character is a 16 bits character ?? */
+/**
+ * gdk_char_measure:
+ * @font: a #GdkFont
+ * @character: the character to measure.
+ * 
+ * Determines the distance from the origin to the rightmost
+ * portion of a character when drawn. This is not the
+ * correct value for determining the origin of the next
+ * portion when drawing text in multiple pieces. 
+ * 
+ * Return value: the right bearing of the character in pixels.
+ **/
 gint
-gdk_char_width (GdkFont *font,
-               gchar    character)
+gdk_char_measure (GdkFont *font,
+                  gchar    character)
 {
-  GdkFontPrivate *private;
-  XCharStruct *chars;
-  gint width;
-  guint ch = character & 0xff;  /* get rid of sign-extension */
-  XFontStruct *xfont;
-  XFontSet fontset;
-
   g_return_val_if_fail (font != NULL, -1);
 
-  private = (GdkFontPrivate*) font;
-
-  switch (font->type)
-    {
-    case GDK_FONT_FONT:
-      /* only 8 bits characters are considered here */
-      xfont = (XFontStruct *) private->xfont;
-      if ((xfont->min_byte1 == 0) &&
-         (xfont->max_byte1 == 0) &&
-         (ch >= xfont->min_char_or_byte2) &&
-         (ch <= xfont->max_char_or_byte2))
-       {
-         chars = xfont->per_char;
-         if (chars)
-           width = chars[ch - xfont->min_char_or_byte2].width;
-         else
-           width = xfont->min_bounds.width;
-       }
-      else
-       {
-         width = XTextWidth (xfont, &character, 1);
-       }
-      break;
-    case GDK_FONT_FONTSET:
-      fontset = (XFontSet) private->xfont;
-      width = XmbTextEscapement (fontset, &character, 1) ;
-      break;
-    default:
-      width = 0;
-    }
-  return width;
+  return gdk_text_measure (font, &character, 1);
 }
 
+/**
+ * gdk_string_height:
+ * @font: a #GdkFont
+ * @string: the nul-terminated string to measure.
+ * 
+ * Determines the total height of a given nul-terminated
+ * string. This value is not generally useful, because you
+ * cannot determine how this total height will be drawn in
+ * relation to the baseline. See gdk_string_extents().
+ * 
+ * Return value: the height of the string in pixels.
+ **/
 gint
-gdk_string_measure (GdkFont     *font,
-                    const gchar *string)
+gdk_string_height (GdkFont     *font,
+                  const gchar *string)
 {
   g_return_val_if_fail (font != NULL, -1);
   g_return_val_if_fail (string != NULL, -1);
 
-  return gdk_text_measure (font, string, strlen (string));
+  return gdk_text_height (font, string, _gdk_font_strlen (font, string));
 }
 
+/**
+ * gdk_text_height:
+ * @font: a #GdkFont
+ * @text: the text to measure.
+ * @text_length: the length of the text in bytes.
+ * 
+ * Determines the total height of a given string.
+ * This value is not generally useful, because you cannot
+ * determine how this total height will be drawn in
+ * relation to the baseline. See gdk_text_extents().
+ * 
+ * Return value: the height of the string in pixels.
+ **/
 gint
-gdk_text_measure (GdkFont     *font,
-                  const gchar *text,
-                  gint         text_length)
+gdk_text_height (GdkFont     *font,
+                const gchar *text,
+                gint         text_length)
 {
-  GdkFontPrivate *private;
-  XCharStruct overall;
-  XFontStruct *xfont;
-  XFontSet    fontset;
-  XRectangle  ink, log;
-  int direction;
-  int font_ascent;
-  int font_descent;
-  gint width;
+  gint ascent, descent;
 
   g_return_val_if_fail (font != NULL, -1);
   g_return_val_if_fail (text != NULL, -1);
 
-  private = (GdkFontPrivate*) font;
-
-  switch (font->type)
-    {
-    case GDK_FONT_FONT:
-      xfont = (XFontStruct *) private->xfont;
-      if ((xfont->min_byte1 == 0) && (xfont->max_byte1 == 0))
-       {
-         XTextExtents (xfont, text, text_length,
-                       &direction, &font_ascent, &font_descent,
-                       &overall);
-       }
-      else
-       {
-         XTextExtents16 (xfont, (XChar2b *) text, text_length / 2,
-                         &direction, &font_ascent, &font_descent,
-                         &overall);
-       }
-      width = overall.rbearing;
-      break;
-    case GDK_FONT_FONTSET:
-      fontset = (XFontSet) private->xfont;
-      XmbTextExtents (fontset, text, text_length, &ink, &log);
-      width = log.width;
-      break;
-    default:
-      width = 0;
-    }
-  return width;
+  gdk_text_extents (font, text, text_length, NULL, NULL, NULL, &ascent, &descent);
+  return ascent + descent;
 }
 
+/**
+ * gdk_char_height:
+ * @font: a #GdkFont
+ * @character: the character to measure.
+ * 
+ * Determines the total height of a given character.
+ * This value is not generally useful, because you cannot
+ * determine how this total height will be drawn in
+ * relation to the baseline. See gdk_text_extents().
+ * 
+ * Return value: the height of the character in pixels. 
+ **/
 gint
-gdk_char_measure (GdkFont *font,
-                  gchar    character)
+gdk_char_height (GdkFont *font,
+                gchar    character)
 {
   g_return_val_if_fail (font != NULL, -1);
 
-  return gdk_text_measure (font, &character, 1);
+  return gdk_text_height (font, &character, 1);
 }
+
+/**
+ * gdk_font_from_description:
+ * @font_desc: a #PangoFontDescription.
+ * 
+ * Load a #GdkFont based on a Pango font description. This font will
+ * only be an approximation of the Pango font, and
+ * internationalization will not be handled correctly. This function
+ * should only be used for legacy code that cannot be easily converted
+ * to use Pango. Using Pango directly will produce better results.
+ * 
+ * Return value: the newly loaded font, or %NULL if the font
+ * cannot be loaded.
+ **/
+GdkFont*
+gdk_font_from_description (PangoFontDescription *font_desc)
+{
+  return gdk_font_from_description_for_display (gdk_display_get_default (),font_desc);
+}
+
+/**
+ * gdk_font_load:
+ * @font_name: a XLFD describing the font to load.
+ * 
+ * Loads a font.
+ * 
+ * The font may be newly loaded or looked up the font in a cache. 
+ * You should make no assumptions about the initial reference count.
+ * 
+ * Return value: a #GdkFont, or %NULL if the font could not be loaded.
+ **/
+GdkFont*
+gdk_font_load (const gchar *font_name)
+{  
+   return gdk_font_load_for_display (gdk_display_get_default(), font_name);
+}
+
+