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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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-1999. 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/.
33 #include "gdkprivate.h"
35 static GHashTable *font_name_hash = NULL;
36 static GHashTable *fontset_name_hash = NULL;
39 gdk_font_hash_insert (GdkFontType type, GdkFont *font, const gchar *font_name)
41 GdkFontPrivate *private = (GdkFontPrivate *)font;
42 GHashTable **hashp = (type == GDK_FONT_FONT) ?
43 &font_name_hash : &fontset_name_hash;
46 *hashp = g_hash_table_new (g_str_hash, g_str_equal);
48 private->names = g_slist_prepend (private->names, g_strdup (font_name));
49 g_hash_table_insert (*hashp, private->names->data, font);
53 gdk_font_hash_remove (GdkFontType type, GdkFont *font)
55 GdkFontPrivate *private = (GdkFontPrivate *)font;
57 GHashTable *hash = (type == GDK_FONT_FONT) ?
58 font_name_hash : fontset_name_hash;
60 tmp_list = private->names;
63 g_hash_table_remove (hash, tmp_list->data);
64 g_free (tmp_list->data);
66 tmp_list = tmp_list->next;
69 g_slist_free (private->names);
70 private->names = NULL;
74 gdk_font_hash_lookup (GdkFontType type, const gchar *font_name)
77 GHashTable *hash = (type == GDK_FONT_FONT) ?
78 font_name_hash : fontset_name_hash;
84 result = g_hash_table_lookup (hash, font_name);
86 gdk_font_ref (result);
93 charset_name (DWORD charset)
97 case ANSI_CHARSET: return "ANSI";
98 case DEFAULT_CHARSET: return "DEFAULT";
99 case SYMBOL_CHARSET: return "SYMBOL";
100 case SHIFTJIS_CHARSET: return "SHIFTJIS";
101 case HANGEUL_CHARSET: return "HANGEUL";
102 case GB2312_CHARSET: return "GB2312";
103 case CHINESEBIG5_CHARSET: return "CHINESEBIG5";
104 case JOHAB_CHARSET: return "JOHAB";
105 case HEBREW_CHARSET: return "HEBREW";
106 case ARABIC_CHARSET: return "ARABIC";
107 case GREEK_CHARSET: return "GREEK";
108 case TURKISH_CHARSET: return "TURKISH";
109 case VIETNAMESE_CHARSET: return "VIETNAMESE";
110 case THAI_CHARSET: return "THAI";
111 case EASTEUROPE_CHARSET: return "EASTEUROPE";
112 case RUSSIAN_CHARSET: return "RUSSIAN";
113 case MAC_CHARSET: return "MAC";
114 case BALTIC_CHARSET: return "BALTIC";
120 gdk_font_load_internal (GdkFontType type,
121 const gchar *font_name)
124 GdkFontPrivate *private;
128 TEXTMETRIC textmetric;
131 DWORD fdwItalic, fdwUnderline, fdwStrikeOut, fdwCharSet,
132 fdwOutputPrecision, fdwClipPrecision, fdwQuality, fdwPitchAndFamily;
133 const char *lpszFace;
135 int numfields, n1, n2, tries;
136 char foundry[32], family[100], weight[32], slant[32], set_width[32],
137 spacing[32], registry[32], encoding[32];
138 char pixel_size[10], point_size[10], res_x[10], res_y[10], avg_width[10];
141 int nHeight, nWidth, nEscapement, nOrientation, fnWeight;
144 g_return_val_if_fail (font_name != NULL, NULL);
146 GDK_NOTE (MISC, g_print ("gdk_font_load_internal: %s\n", font_name));
148 font = gdk_font_hash_lookup (type, font_name);
152 numfields = sscanf (font_name,
153 "-%30[^-]-%100[^-]-%30[^-]-%30[^-]-%30[^-]-%n",
162 /* Probably a plain Windows font name */
167 fnWeight = FW_DONTCARE;
169 fdwUnderline = FALSE;
170 fdwStrikeOut = FALSE;
171 fdwCharSet = ANSI_CHARSET;
172 fdwOutputPrecision = OUT_TT_PRECIS;
173 fdwClipPrecision = CLIP_DEFAULT_PRECIS;
174 fdwQuality = PROOF_QUALITY;
175 fdwPitchAndFamily = DEFAULT_PITCH;
176 lpszFace = font_name;
178 else if (numfields != 5)
180 g_warning ("gdk_font_load: font name %s illegal", font_name);
186 /* It must be a XLFD name */
188 /* Check for hex escapes in the font family,
189 * put in there by gtkfontsel.
194 if (*p == '%' && isxdigit (p[1]) && isxdigit (p[2]))
196 sscanf (p+1, "%2x", &c);
203 /* Skip add_style which often is empty in the requested font name */
204 while (font_name[n1] && font_name[n1] != '-')
208 numfields += sscanf (font_name + n1,
209 "-%8[^-]-%8[^-]-%8[^-]-%8[^-]-%30[^-]-%8[^-]-%30[^-]-%30[^-]%n",
220 if (numfields != 14 || font_name[n1 + n2] != '\0')
222 g_warning ("gdk_font_load: font name %s illegal", font_name);
227 logpixelsy = GetDeviceCaps (gdk_DC, LOGPIXELSY);
229 if (strcmp (pixel_size, "*") == 0)
230 if (strcmp (point_size, "*") == 0)
233 nHeight = (int) (((double) atoi (point_size))/720.*logpixelsy);
235 nHeight = atoi (pixel_size);
241 if (g_strcasecmp (weight, "thin") == 0)
243 else if (g_strcasecmp (weight, "extralight") == 0)
244 fnWeight = FW_EXTRALIGHT;
245 else if (g_strcasecmp (weight, "ultralight") == 0)
247 fnWeight = FW_ULTRALIGHT;
249 fnWeight = FW_EXTRALIGHT; /* In fact, FW_ULTRALIGHT really is
250 * defined as FW_EXTRALIGHT anyway.
253 else if (g_strcasecmp (weight, "light") == 0)
255 else if (g_strcasecmp (weight, "normal") == 0)
256 fnWeight = FW_NORMAL;
257 else if (g_strcasecmp (weight, "regular") == 0)
258 fnWeight = FW_REGULAR;
259 else if (g_strcasecmp (weight, "medium") == 0)
260 fnWeight = FW_MEDIUM;
261 else if (g_strcasecmp (weight, "semibold") == 0)
262 fnWeight = FW_SEMIBOLD;
263 else if (g_strcasecmp (weight, "demibold") == 0)
265 fnWeight = FW_DEMIBOLD;
267 fnWeight = FW_SEMIBOLD; /* As above */
269 else if (g_strcasecmp (weight, "bold") == 0)
271 else if (g_strcasecmp (weight, "extrabold") == 0)
272 fnWeight = FW_EXTRABOLD;
273 else if (g_strcasecmp (weight, "ultrabold") == 0)
275 fnWeight = FW_ULTRABOLD;
277 fnWeight = FW_EXTRABOLD; /* As above */
279 else if (g_strcasecmp (weight, "heavy") == 0)
281 else if (g_strcasecmp (weight, "black") == 0)
285 fnWeight = FW_HEAVY; /* As above */
288 fnWeight = FW_DONTCARE;
290 if (g_strcasecmp (slant, "italic") == 0
291 || g_strcasecmp (slant, "oblique") == 0
292 || g_strcasecmp (slant, "i") == 0
293 || g_strcasecmp (slant, "o") == 0)
297 fdwUnderline = FALSE;
298 fdwStrikeOut = FALSE;
299 if (g_strcasecmp (registry, "iso8859") == 0)
300 if (strcmp (encoding, "1") == 0)
301 fdwCharSet = ANSI_CHARSET;
303 fdwCharSet = ANSI_CHARSET; /* XXX ??? */
304 else if (g_strcasecmp (registry, "windows") == 0)
305 if (g_strcasecmp (encoding, "symbol") == 0)
306 fdwCharSet = SYMBOL_CHARSET;
307 else if (g_strcasecmp (encoding, "shiftjis") == 0)
308 fdwCharSet = SHIFTJIS_CHARSET;
309 else if (g_strcasecmp (encoding, "gb2312") == 0)
310 fdwCharSet = GB2312_CHARSET;
311 else if (g_strcasecmp (encoding, "hangeul") == 0)
312 fdwCharSet = HANGEUL_CHARSET;
313 else if (g_strcasecmp (encoding, "chinesebig5") == 0)
314 fdwCharSet = CHINESEBIG5_CHARSET;
315 else if (g_strcasecmp (encoding, "johab") == 0)
316 fdwCharSet = JOHAB_CHARSET;
317 else if (g_strcasecmp (encoding, "hebrew") == 0)
318 fdwCharSet = HEBREW_CHARSET;
319 else if (g_strcasecmp (encoding, "arabic") == 0)
320 fdwCharSet = ARABIC_CHARSET;
321 else if (g_strcasecmp (encoding, "greek") == 0)
322 fdwCharSet = GREEK_CHARSET;
323 else if (g_strcasecmp (encoding, "turkish") == 0)
324 fdwCharSet = TURKISH_CHARSET;
325 else if (g_strcasecmp (encoding, "easteurope") == 0)
326 fdwCharSet = EASTEUROPE_CHARSET;
327 else if (g_strcasecmp (encoding, "russian") == 0)
328 fdwCharSet = RUSSIAN_CHARSET;
329 else if (g_strcasecmp (encoding, "mac") == 0)
330 fdwCharSet = MAC_CHARSET;
331 else if (g_strcasecmp (encoding, "baltic") == 0)
332 fdwCharSet = BALTIC_CHARSET;
334 fdwCharSet = ANSI_CHARSET; /* XXX ??? */
336 fdwCharSet = ANSI_CHARSET; /* XXX ??? */
337 fdwOutputPrecision = OUT_TT_PRECIS;
338 fdwClipPrecision = CLIP_DEFAULT_PRECIS;
339 fdwQuality = PROOF_QUALITY;
340 if (g_strcasecmp (spacing, "m") == 0)
341 fdwPitchAndFamily = FIXED_PITCH;
342 else if (g_strcasecmp (spacing, "p") == 0)
343 fdwPitchAndFamily = VARIABLE_PITCH;
345 fdwPitchAndFamily = DEFAULT_PITCH;
349 for (tries = 0; ; tries++)
351 GDK_NOTE (MISC, g_print ("...trying CreateFont(%d,%d,%d,%d,"
354 "%d,%#.02x,\"%s\")\n",
355 nHeight, nWidth, nEscapement, nOrientation,
356 fnWeight, fdwItalic, fdwUnderline, fdwStrikeOut,
357 fdwCharSet, fdwOutputPrecision, fdwClipPrecision,
358 fdwQuality, fdwPitchAndFamily, lpszFace));
360 CreateFont (nHeight, nWidth, nEscapement, nOrientation,
361 fnWeight, fdwItalic, fdwUnderline, fdwStrikeOut,
362 fdwCharSet, fdwOutputPrecision, fdwClipPrecision,
363 fdwQuality, fdwPitchAndFamily, lpszFace)) != NULL)
366 /* If we fail, try some similar fonts often found on Windows. */
370 if (g_strcasecmp (family, "helvetica") == 0)
372 else if (g_strcasecmp (family, "new century schoolbook") == 0)
373 lpszFace = "century schoolbook";
374 else if (g_strcasecmp (family, "courier") == 0)
375 lpszFace = "courier new";
376 else if (g_strcasecmp (family, "lucida") == 0)
377 lpszFace = "lucida sans unicode";
378 else if (g_strcasecmp (family, "lucidatypewriter") == 0)
379 lpszFace = "lucida console";
380 else if (g_strcasecmp (family, "times") == 0)
381 lpszFace = "times new roman";
385 if (g_strcasecmp (family, "courier") == 0)
388 fdwPitchAndFamily |= FF_MODERN;
390 else if (g_strcasecmp (family, "times new roman") == 0)
393 fdwPitchAndFamily |= FF_ROMAN;
395 else if (g_strcasecmp (family, "helvetica") == 0
396 || g_strcasecmp (family, "lucida") == 0)
399 fdwPitchAndFamily |= FF_SWISS;
404 fdwPitchAndFamily = (fdwPitchAndFamily & 0x0F) | FF_DONTCARE;
415 private = g_new (GdkFontPrivate, 1);
416 font = (GdkFont*) private;
418 private->xfont = hfont;
419 private->ref_count = 1;
420 private->names = NULL;
421 GetObject (private->xfont, sizeof (logfont), &logfont);
422 oldfont = SelectObject (gdk_DC, private->xfont);
423 GetTextMetrics (gdk_DC, &textmetric);
424 private->charset = GetTextCharsetInfo (gdk_DC, &private->fs, 0);
425 SelectObject (gdk_DC, oldfont);
426 TranslateCharsetInfo ((DWORD *) private->charset, &csi, TCI_SRCCHARSET);
427 private->codepage = csi.ciACP;
428 GetCPInfo (private->codepage, &private->cpinfo);
430 font->ascent = textmetric.tmAscent;
431 font->descent = textmetric.tmDescent;
433 GDK_NOTE (MISC, g_print ("... = %#x charset %s codepage %d (max %d bytes) "
436 charset_name (private->charset),
438 private->cpinfo.MaxCharSize,
439 font->ascent, font->descent));
441 /* This memory is leaked, so shoot me. */
442 f = g_new (HANDLE, 1);
443 *f = (HANDLE) ((guint) private->xfont + HFONT_DITHER);
444 gdk_xid_table_insert (f, font);
446 gdk_font_hash_insert (type, font, font_name);
452 gdk_font_load (const gchar *font_name)
454 return gdk_font_load_internal (GDK_FONT_FONT, font_name);
458 gdk_fontset_load (gchar *fontset_name)
460 return gdk_font_load_internal (GDK_FONT_FONTSET, fontset_name);
464 gdk_font_ref (GdkFont *font)
466 GdkFontPrivate *private;
468 g_return_val_if_fail (font != NULL, NULL);
470 private = (GdkFontPrivate*) font;
471 private->ref_count += 1;
473 GDK_NOTE (MISC, g_print ("gdk_font_ref %#x %d\n",
474 private->xfont, private->ref_count));
479 gdk_font_unref (GdkFont *font)
481 GdkFontPrivate *private;
482 private = (GdkFontPrivate*) font;
484 g_return_if_fail (font != NULL);
485 g_return_if_fail (private->ref_count > 0);
487 private->ref_count -= 1;
489 GDK_NOTE (MISC, g_print ("gdk_font_unref %#x %d%s\n",
492 (private->ref_count == 0 ? " freeing" : "")));
494 if (private->ref_count == 0)
496 gdk_font_hash_remove (font->type, font);
501 case GDK_FONT_FONTSET: /* XXX */
502 gdk_xid_table_remove ((HANDLE) ((guint) private->xfont + HFONT_DITHER));
503 DeleteObject (private->xfont);
507 g_assert_not_reached ();
514 gdk_font_id (const GdkFont *font)
516 const GdkFontPrivate *font_private;
518 g_return_val_if_fail (font != NULL, 0);
520 font_private = (const GdkFontPrivate*) font;
522 if (font->type == GDK_FONT_FONT)
523 return (gint) font_private->xfont;
529 gdk_font_equal (const GdkFont *fonta,
530 const GdkFont *fontb)
532 const GdkFontPrivate *privatea;
533 const GdkFontPrivate *privateb;
535 g_return_val_if_fail (fonta != NULL, FALSE);
536 g_return_val_if_fail (fontb != NULL, FALSE);
538 privatea = (const GdkFontPrivate*) fonta;
539 privateb = (const GdkFontPrivate*) fontb;
541 if (fonta->type == GDK_FONT_FONT && fontb->type == GDK_FONT_FONT)
542 return (privatea->xfont == privateb->xfont);
543 else if (fonta->type == GDK_FONT_FONTSET && fontb->type == GDK_FONT_FONTSET)
544 return (privatea->xfont == privateb->xfont);
550 gdk_string_width (GdkFont *font,
553 return gdk_text_width (font, string, strlen (string));
557 gdk_text_size (GdkFont *font,
562 GdkFontPrivate *private;
567 g_return_val_if_fail (font != NULL, FALSE);
568 g_return_val_if_fail (text != NULL, FALSE);
570 if (text_length == 0)
573 private = (GdkFontPrivate*) font;
575 g_assert (font->type == GDK_FONT_FONT || font->type == GDK_FONT_FONTSET);
577 if ((oldfont = SelectObject (gdk_DC, private->xfont)) == NULL)
579 g_warning ("gdk_text_width: SelectObject failed");
583 wcstr = g_new (wchar_t, text_length);
584 if ((wlen = gdk_nmbstowchar_ts (wcstr, text, text_length, text_length)) == -1)
586 g_warning ("gdk_text_size: gdk_nmbstowchar_ts failed");
590 GetTextExtentPoint32W (gdk_DC, wcstr, wlen, sizep);
593 SelectObject (gdk_DC, oldfont);
599 gdk_text_width (GdkFont *font,
605 if (!gdk_text_size (font, text, text_length, &size))
612 gdk_text_width_wc (GdkFont *font,
613 const GdkWChar *text,
616 GdkFontPrivate *private;
623 g_return_val_if_fail (font != NULL, -1);
624 g_return_val_if_fail (text != NULL, -1);
626 if (text_length == 0)
629 g_assert (font->type == GDK_FONT_FONT || font->type == GDK_FONT_FONTSET);
631 private = (GdkFontPrivate*) font;
633 if ((oldfont = SelectObject (gdk_DC, private->xfont)) == NULL)
635 g_warning ("gdk_text_width_wc: SelectObject failed");
637 if (sizeof (wchar_t) != sizeof (GdkWChar))
639 wcstr = g_new (wchar_t, text_length);
640 for (i = 0; i < text_length; i++)
644 wcstr = (wchar_t *) text;
646 GetTextExtentPoint32W (gdk_DC, wcstr, text_length, &size);
648 if (sizeof (wchar_t) != sizeof (GdkWChar))
652 SelectObject (gdk_DC, oldfont);
658 gdk_char_width (GdkFont *font,
661 if (((guchar) character) >= 128)
663 /* gtktext calls us with non-ASCII characters, sigh */
664 GdkWChar wc = (guchar) character;
665 return gdk_text_width_wc (font, &wc, 1);
667 return gdk_text_width (font, &character, 1);
671 gdk_char_width_wc (GdkFont *font,
674 return gdk_text_width_wc (font, &character, 1);
678 gdk_string_measure (GdkFont *font,
681 g_return_val_if_fail (font != NULL, -1);
682 g_return_val_if_fail (string != NULL, -1);
684 return gdk_text_measure (font, string, strlen (string));
688 gdk_text_extents (GdkFont *font,
697 GdkFontPrivate *private;
703 g_return_if_fail (font != NULL);
704 g_return_if_fail (text != NULL);
706 if (text_length == 0)
721 g_assert (font->type == GDK_FONT_FONT || font->type == GDK_FONT_FONTSET);
723 private = (GdkFontPrivate*) font;
725 if ((oldfont = SelectObject (gdk_DC, private->xfont)) == NULL)
726 g_warning ("gdk_text_extents: SelectObject failed");
728 wcstr = g_new (wchar_t, text_length);
729 if ((wlen = gdk_nmbstowchar_ts (wcstr, text, text_length, text_length)) == -1)
731 g_warning ("gdk_text_extents: gdk_nmbstowchar_ts failed");
736 GetTextExtentPoint32W (gdk_DC, wcstr, wlen, &size);
739 SelectObject (gdk_DC, oldfont);
741 /* XXX This is all quite bogus */
749 *ascent = size.cy + 1;
751 *descent = font->descent + 1;
755 gdk_text_extents_wc (GdkFont *font,
756 const GdkWChar *text,
764 GdkFontPrivate *private;
770 g_return_if_fail (font != NULL);
771 g_return_if_fail (text != NULL);
773 if (text_length == 0)
788 g_assert (font->type == GDK_FONT_FONT || font->type == GDK_FONT_FONTSET);
790 private = (GdkFontPrivate*) font;
792 if (sizeof (wchar_t) != sizeof (GdkWChar))
794 wcstr = g_new (wchar_t, text_length);
795 for (i = 0; i < text_length; i++)
799 wcstr = (wchar_t *) text;
801 if ((oldfont = SelectObject (gdk_DC, private->xfont)) == NULL)
802 g_warning ("gdk_text_extents_wc: SelectObject failed");
804 GetTextExtentPoint32W (gdk_DC, wcstr, text_length, &size);
806 if (sizeof (wchar_t) != sizeof (GdkWChar))
810 SelectObject (gdk_DC, oldfont);
812 /* XXX This is all quite bogus */
820 *ascent = size.cy + 1;
822 *descent = font->descent + 1;
826 gdk_string_extents (GdkFont *font,
834 g_return_if_fail (font != NULL);
835 g_return_if_fail (string != NULL);
837 gdk_text_extents (font, string, strlen (string),
838 lbearing, rbearing, width, ascent, descent);
843 gdk_text_measure (GdkFont *font,
847 return gdk_text_width (font, text, text_length); /* ??? */
851 gdk_char_measure (GdkFont *font,
854 return gdk_text_measure (font, &character, 1);
858 gdk_string_height (GdkFont *font,
861 g_return_val_if_fail (font != NULL, -1);
862 g_return_val_if_fail (string != NULL, -1);
864 return gdk_text_height (font, string, strlen (string));
868 gdk_text_height (GdkFont *font,
874 if (!gdk_text_size (font, text, text_length, &size))
881 gdk_char_height (GdkFont *font,
884 return gdk_text_height (font, &character, 1);