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"
36 gdk_font_load (const gchar *font_name)
39 GdkFontPrivate *private;
42 TEXTMETRIC textmetric;
44 DWORD fdwItalic, fdwUnderline, fdwStrikeOut, fdwCharSet,
45 fdwOutputPrecision, fdwClipPrecision, fdwQuality, fdwPitchAndFamily;
48 int numfields, n1, n2, tries;
49 char foundry[32], family[100], weight[32], slant[32], set_width[32],
50 spacing[32], registry[32], encoding[32];
51 char pixel_size[10], point_size[10], res_x[10], res_y[10], avg_width[10];
54 int nHeight, nWidth, nEscapement, nOrientation, fnWeight;
57 g_return_val_if_fail (font_name != NULL, NULL);
59 private = g_new (GdkFontPrivate, 1);
60 font = (GdkFont*) private;
62 numfields = sscanf (font_name,
63 "-%30[^-]-%100[^-]-%30[^-]-%30[^-]-%30[^-]-%n",
72 /* Probably a plain Windows font name */
77 fnWeight = FW_DONTCARE;
81 fdwCharSet = ANSI_CHARSET;
82 fdwOutputPrecision = OUT_TT_PRECIS;
83 fdwClipPrecision = CLIP_DEFAULT_PRECIS;
84 fdwQuality = PROOF_QUALITY;
85 fdwPitchAndFamily = DEFAULT_PITCH;
88 else if (numfields != 5)
90 g_warning ("gdk_font_load: font name %s illegal", font_name);
96 /* It must be a XLFD name */
98 /* Check for hex escapes in the font family,
99 * put in there by gtkfontsel.
104 if (*p == '%' && isxdigit (p[1]) && isxdigit (p[2]))
106 sscanf (p+1, "%2x", &c);
113 /* Skip add_style which often is empty in the requested font name */
114 while (font_name[n1] && font_name[n1] != '-')
118 numfields += sscanf (font_name + n1,
119 "-%8[^-]-%8[^-]-%8[^-]-%8[^-]-%30[^-]-%8[^-]-%30[^-]-%30[^-]%n",
130 if (numfields != 14 || font_name[n1 + n2] != '\0')
132 g_warning ("gdk_font_load: font name %s illegal", font_name);
137 logpixelsy = GetDeviceCaps (gdk_DC, LOGPIXELSY);
139 if (strcmp (pixel_size, "*") == 0)
140 if (strcmp (point_size, "*") == 0)
143 nHeight = (int) (((double) atoi (point_size))/720.*logpixelsy);
145 nHeight = atoi (pixel_size);
151 if (g_strcasecmp (weight, "thin") == 0)
153 else if (g_strcasecmp (weight, "extralight") == 0)
154 fnWeight = FW_EXTRALIGHT;
155 else if (g_strcasecmp (weight, "ultralight") == 0)
156 fnWeight = FW_ULTRALIGHT;
157 else if (g_strcasecmp (weight, "light") == 0)
159 else if (g_strcasecmp (weight, "normal") == 0)
160 fnWeight = FW_NORMAL;
161 else if (g_strcasecmp (weight, "regular") == 0)
162 fnWeight = FW_REGULAR;
163 else if (g_strcasecmp (weight, "medium") == 0)
164 fnWeight = FW_MEDIUM;
165 else if (g_strcasecmp (weight, "semibold") == 0)
166 fnWeight = FW_SEMIBOLD;
167 else if (g_strcasecmp (weight, "demibold") == 0)
168 fnWeight = FW_DEMIBOLD;
169 else if (g_strcasecmp (weight, "bold") == 0)
171 else if (g_strcasecmp (weight, "extrabold") == 0)
172 fnWeight = FW_EXTRABOLD;
173 else if (g_strcasecmp (weight, "ultrabold") == 0)
174 fnWeight = FW_ULTRABOLD;
175 else if (g_strcasecmp (weight, "heavy") == 0)
177 else if (g_strcasecmp (weight, "black") == 0)
180 fnWeight = FW_DONTCARE;
182 if (g_strcasecmp (slant, "italic") == 0
183 || g_strcasecmp (slant, "oblique") == 0
184 || g_strcasecmp (slant, "i") == 0
185 || g_strcasecmp (slant, "o") == 0)
189 fdwUnderline = FALSE;
190 fdwStrikeOut = FALSE;
191 if (g_strcasecmp (registry, "iso8859") == 0)
192 if (strcmp (encoding, "1") == 0)
193 fdwCharSet = ANSI_CHARSET;
195 fdwCharSet = ANSI_CHARSET; /* XXX ??? */
196 else if (g_strcasecmp (registry, "windows") == 0)
197 if (g_strcasecmp (encoding, "symbol") == 0)
198 fdwCharSet = SYMBOL_CHARSET;
199 else if (g_strcasecmp (encoding, "shiftjis") == 0)
200 fdwCharSet = SHIFTJIS_CHARSET;
201 else if (g_strcasecmp (encoding, "gb2312") == 0)
202 fdwCharSet = GB2312_CHARSET;
203 else if (g_strcasecmp (encoding, "hangeul") == 0)
204 fdwCharSet = HANGEUL_CHARSET;
205 else if (g_strcasecmp (encoding, "chinesebig5") == 0)
206 fdwCharSet = CHINESEBIG5_CHARSET;
207 else if (g_strcasecmp (encoding, "johab") == 0)
208 fdwCharSet = JOHAB_CHARSET;
209 else if (g_strcasecmp (encoding, "hebrew") == 0)
210 fdwCharSet = HEBREW_CHARSET;
211 else if (g_strcasecmp (encoding, "arabic") == 0)
212 fdwCharSet = ARABIC_CHARSET;
213 else if (g_strcasecmp (encoding, "greek") == 0)
214 fdwCharSet = GREEK_CHARSET;
215 else if (g_strcasecmp (encoding, "turkish") == 0)
216 fdwCharSet = TURKISH_CHARSET;
217 else if (g_strcasecmp (encoding, "easteurope") == 0)
218 fdwCharSet = EASTEUROPE_CHARSET;
219 else if (g_strcasecmp (encoding, "russian") == 0)
220 fdwCharSet = RUSSIAN_CHARSET;
221 else if (g_strcasecmp (encoding, "mac") == 0)
222 fdwCharSet = MAC_CHARSET;
223 else if (g_strcasecmp (encoding, "baltic") == 0)
224 fdwCharSet = BALTIC_CHARSET;
226 fdwCharSet = ANSI_CHARSET; /* XXX ??? */
228 fdwCharSet = ANSI_CHARSET; /* XXX ??? */
229 fdwOutputPrecision = OUT_TT_PRECIS;
230 fdwClipPrecision = CLIP_DEFAULT_PRECIS;
231 fdwQuality = PROOF_QUALITY;
232 if (g_strcasecmp (spacing, "m") == 0)
233 fdwPitchAndFamily = FIXED_PITCH;
234 else if (g_strcasecmp (spacing, "p") == 0)
235 fdwPitchAndFamily = VARIABLE_PITCH;
237 fdwPitchAndFamily = DEFAULT_PITCH;
241 for (tries = 0; ; tries++)
243 GDK_NOTE (MISC, g_print ("gdk_font_load: trying CreateFont(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%#.02x,\"%s\")\n",
244 nHeight, nWidth, nEscapement, nOrientation,
245 fnWeight, fdwItalic, fdwUnderline, fdwStrikeOut,
246 fdwCharSet, fdwOutputPrecision, fdwClipPrecision,
247 fdwQuality, fdwPitchAndFamily, lpszFace));
248 if ((private->xfont =
249 CreateFont (nHeight, nWidth, nEscapement, nOrientation,
250 fnWeight, fdwItalic, fdwUnderline, fdwStrikeOut,
251 fdwCharSet, fdwOutputPrecision, fdwClipPrecision,
252 fdwQuality, fdwPitchAndFamily, lpszFace)) != NULL)
255 /* If we fail, try some similar fonts often found on Windows. */
259 if (g_strcasecmp (family, "helvetica") == 0)
261 else if (g_strcasecmp (family, "new century schoolbook") == 0)
262 lpszFace = "century schoolbook";
263 else if (g_strcasecmp (family, "courier") == 0)
264 lpszFace = "courier new";
265 else if (g_strcasecmp (family, "lucida") == 0)
266 lpszFace = "lucida sans unicode";
267 else if (g_strcasecmp (family, "lucidatypewriter") == 0)
268 lpszFace = "lucida console";
269 else if (g_strcasecmp (family, "times") == 0)
270 lpszFace = "times new roman";
274 if (g_strcasecmp (family, "courier") == 0)
277 fdwPitchAndFamily |= FF_MODERN;
279 else if (g_strcasecmp (family, "times new roman") == 0)
282 fdwPitchAndFamily |= FF_ROMAN;
284 else if (g_strcasecmp (family, "helvetica") == 0
285 || g_strcasecmp (family, "lucida") == 0)
288 fdwPitchAndFamily |= FF_SWISS;
293 fdwPitchAndFamily = (fdwPitchAndFamily & 0x0F) | FF_DONTCARE;
303 g_warning ("gdk_font_load: font %s not found", font_name);
308 private->ref_count = 1;
309 font->type = GDK_FONT_FONT;
310 GetObject (private->xfont, sizeof (logfont), &logfont);
311 oldfont = SelectObject (gdk_DC, private->xfont);
312 GetTextMetrics (gdk_DC, &textmetric);
313 SelectObject (gdk_DC, oldfont);
314 font->ascent = textmetric.tmAscent;
315 font->descent = textmetric.tmDescent;
317 GDK_NOTE (MISC, g_print ("gdk_font_load: %s = %#x asc %d desc %d\n",
318 font_name, private->xfont,
319 font->ascent, font->descent));
321 /* This memory is leaked, so shoot me. */
322 f = g_new (HANDLE, 1);
323 *f = (HANDLE) ((guint) private->xfont + HFONT_DITHER);
324 gdk_xid_table_insert (f, font);
330 gdk_fontset_load (gchar *fontset_name)
332 g_warning ("gdk_fontset_load: Not implemented");
337 gdk_font_ref (GdkFont *font)
339 GdkFontPrivate *private;
341 g_return_val_if_fail (font != NULL, NULL);
343 private = (GdkFontPrivate*) font;
344 private->ref_count += 1;
349 gdk_font_unref (GdkFont *font)
351 GdkFontPrivate *private;
353 g_return_if_fail (font != NULL);
355 private = (GdkFontPrivate*) font;
357 private->ref_count -= 1;
358 if (private->ref_count == 0)
363 GDK_NOTE (MISC, g_print ("gdk_font_unref %#x\n", private->xfont));
365 gdk_xid_table_remove ((HANDLE) ((guint) private->xfont + HFONT_DITHER));
366 DeleteObject (private->xfont);
370 g_assert_not_reached ();
377 gdk_font_id (const GdkFont *font)
379 const GdkFontPrivate *font_private;
381 g_return_val_if_fail (font != NULL, 0);
383 font_private = (const GdkFontPrivate*) font;
385 if (font->type == GDK_FONT_FONT)
386 return (gint) font_private->xfont;
388 g_assert_not_reached ();
393 gdk_font_equal (const GdkFont *fonta,
394 const GdkFont *fontb)
396 const GdkFontPrivate *privatea;
397 const GdkFontPrivate *privateb;
399 g_return_val_if_fail (fonta != NULL, FALSE);
400 g_return_val_if_fail (fontb != NULL, FALSE);
402 privatea = (const GdkFontPrivate*) fonta;
403 privateb = (const GdkFontPrivate*) fontb;
405 if (fonta->type == GDK_FONT_FONT && fontb->type == GDK_FONT_FONT)
406 return (privatea->xfont == privateb->xfont);
408 g_assert_not_reached ();
413 gdk_string_width (GdkFont *font,
416 return gdk_text_width (font, string, strlen (string));
420 gdk_text_width (GdkFont *font,
424 GdkFontPrivate *private;
429 g_return_val_if_fail (font != NULL, -1);
430 g_return_val_if_fail (text != NULL, -1);
432 private = (GdkFontPrivate*) font;
437 oldfont = SelectObject (gdk_DC, private->xfont);
438 GetTextExtentPoint32A (gdk_DC, text, text_length, &size);
439 SelectObject (gdk_DC, oldfont);
444 g_assert_not_reached ();
450 gdk_text_width_wc (GdkFont *font,
451 const GdkWChar *text,
454 GdkFontPrivate *private;
460 g_return_val_if_fail (font != NULL, -1);
461 g_return_val_if_fail (text != NULL, -1);
463 private = (GdkFontPrivate*) font;
468 wcstr = g_new (wchar_t, text_length);
469 for (i = 0; i < text_length; i++)
471 oldfont = SelectObject (gdk_DC, private->xfont);
472 GetTextExtentPoint32W (gdk_DC, wcstr, text_length, &size);
474 SelectObject (gdk_DC, oldfont);
485 gdk_char_width (GdkFont *font,
488 return gdk_text_width (font, &character, 1);
492 gdk_char_width_wc (GdkFont *font,
495 return gdk_text_width_wc (font, &character, 1);
499 gdk_string_measure (GdkFont *font,
502 g_return_val_if_fail (font != NULL, -1);
503 g_return_val_if_fail (string != NULL, -1);
505 return gdk_text_measure (font, string, strlen (string));
509 gdk_text_extents (GdkFont *font,
518 GdkFontPrivate *private;
522 g_return_if_fail (font != NULL);
523 g_return_if_fail (text != NULL);
525 private = (GdkFontPrivate*) font;
530 oldfont = SelectObject (gdk_DC, private->xfont);
531 GetTextExtentPoint32A (gdk_DC, text, text_length, &size);
532 SelectObject (gdk_DC, oldfont);
533 /* XXX This is all quite bogus */
541 *ascent = size.cy + 1;
543 *descent = font->descent + 1;
547 g_assert_not_reached ();
552 gdk_text_extents_wc (GdkFont *font,
553 const GdkWChar *text,
561 GdkFontPrivate *private;
567 g_return_if_fail (font != NULL);
568 g_return_if_fail (text != NULL);
570 private = (GdkFontPrivate*) font;
575 wcstr = g_new (wchar_t, text_length);
576 for (i = 0; i < text_length; i++)
578 oldfont = SelectObject (gdk_DC, private->xfont);
579 GetTextExtentPoint32W (gdk_DC, wcstr, text_length, &size);
581 SelectObject (gdk_DC, oldfont);
583 /* XXX This is all quite bogus */
591 *ascent = size.cy + 1;
593 *descent = font->descent + 1;
597 g_assert_not_reached ();
602 gdk_string_extents (GdkFont *font,
610 g_return_if_fail (font != NULL);
611 g_return_if_fail (string != NULL);
613 gdk_text_extents (font, string, strlen (string),
614 lbearing, rbearing, width, ascent, descent);
619 gdk_text_measure (GdkFont *font,
623 GdkFontPrivate *private;
626 g_return_val_if_fail (font != NULL, -1);
627 g_return_val_if_fail (text != NULL, -1);
629 private = (GdkFontPrivate*) font;
634 return gdk_text_width (font, text, text_length); /* ??? */
638 g_assert_not_reached ();
644 gdk_char_measure (GdkFont *font,
647 g_return_val_if_fail (font != NULL, -1);
649 return gdk_text_measure (font, &character, 1);
653 gdk_string_height (GdkFont *font,
656 g_return_val_if_fail (font != NULL, -1);
657 g_return_val_if_fail (string != NULL, -1);
659 return gdk_text_height (font, string, strlen (string));
663 gdk_text_height (GdkFont *font,
667 GdkFontPrivate *private;
672 g_return_val_if_fail (font != NULL, -1);
673 g_return_val_if_fail (text != NULL, -1);
675 private = (GdkFontPrivate*) font;
680 oldfont = SelectObject (gdk_DC, private->xfont);
681 GetTextExtentPoint32 (gdk_DC, text, text_length, &size);
682 SelectObject (gdk_DC, oldfont);
687 g_error ("font->type = %d", font->type);
693 gdk_char_height (GdkFont *font,
696 g_return_val_if_fail (font != NULL, -1);
698 return gdk_text_height (font, &character, 1);