gdkfont.c 8.76 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* GDK - The GIMP Drawing Kit
 * 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
 * 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.
 *
 * You should have received a copy of the GNU Library General Public
15 16 17
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
 */
#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;
    }

  gdk_xid_table_insert (&((XFontStruct *) private->xfont)->fid, font);

  return font;
}

GdkFont*
55
gdk_fontset_load (gchar *fontset_name)
Elliot Lee's avatar
Elliot Lee committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
{
  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)
    {
74 75 76 77
      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]);
Elliot Lee's avatar
Elliot Lee committed
78 79 80 81 82 83 84 85 86 87 88 89
      XFreeStringList (missing_charset_list);
    }

  private->ref_count = 1;

  if (!fontset)
    {
      g_free (font);
      return NULL;
    }
  else
    {
90 91 92 93 94
      gint num_fonts;
      gint i;
      XFontStruct **font_structs;
      gchar **font_names;
      
Elliot Lee's avatar
Elliot Lee committed
95 96
      private->xfont = fontset;
      font->type = GDK_FONT_FONTSET;
97 98 99 100 101 102 103 104 105
      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);
	}
Elliot Lee's avatar
Elliot Lee committed
106 107 108
    }
  return font;
}
109 110 111

GdkFont*
gdk_font_ref (GdkFont *font)
Elliot Lee's avatar
Elliot Lee committed
112 113 114
{
  GdkFontPrivate *private;

115
  g_return_val_if_fail (font != NULL, NULL);
Elliot Lee's avatar
Elliot Lee committed
116 117

  private = (GdkFontPrivate*) font;
118 119
  private->ref_count += 1;
  return font;
Elliot Lee's avatar
Elliot Lee committed
120 121 122
}

void
123
gdk_font_unref (GdkFont *font)
Elliot Lee's avatar
Elliot Lee committed
124 125 126 127 128 129 130 131 132 133
{
  GdkFontPrivate *private;

  g_return_if_fail (font != NULL);

  private = (GdkFontPrivate*) font;

  private->ref_count -= 1;
  if (private->ref_count == 0)
    {
134 135 136 137 138 139 140 141 142 143 144 145 146
      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;
	}
Elliot Lee's avatar
Elliot Lee committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
      g_free (font);
    }
}

gint
gdk_font_id (GdkFont *font)
{
  GdkFontPrivate *font_private;

  g_return_val_if_fail (font != NULL, 0);

  font_private = (GdkFontPrivate*) font;

  if (font->type == GDK_FONT_FONT)
    {
      return ((XFontStruct *) font_private->xfont)->fid;
    }
  else
    {
      return 0;
    }
}

gint
gdk_font_equal (GdkFont *fonta,
                GdkFont *fontb)
{
  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;
}

gint
gdk_string_width (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;
}

gint
gdk_text_width (GdkFont      *font,
		const gchar  *text,
		gint          text_length)
{
  GdkFontPrivate *private;
  gint width;
  XFontStruct *xfont;
  XFontSet fontset;

  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;
}

/* Problem: What if a character is a 16 bits character ?? */
gint
gdk_char_width (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;
}

gint
gdk_string_measure (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));
}

gint
gdk_text_measure (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;

  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;
}

gint
gdk_char_measure (GdkFont *font,
                  gchar    character)
{
  g_return_val_if_fail (font != NULL, -1);

  return gdk_text_measure (font, &character, 1);
}