2 * Utilities to use for font manipulation
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 #include <epan/packet.h>
41 #include <epan/prefs.h>
43 #include "gtkglobals.h"
45 #include "compat_macros.h"
46 #include "font_utils.h"
47 #include "simple_dialog.h"
49 #include "packet_list.h"
50 #include "proto_draw.h"
51 #include "follow_dlg.h"
54 /* XXX - use capture.h instead */
55 /*#include "capture.h"*/
56 extern gboolean capture_child;
60 #if GTK_MAJOR_VERSION < 2
61 guint m_font_height, m_font_width;
63 FONT_TYPE *m_r_font, *m_b_font;
66 /* Get the regular user font.
68 * @return the regular user font
70 FONT_TYPE *user_font_get_regular(void)
75 /* Get the bold user font.
77 * @return the bold user font
79 FONT_TYPE *user_font_get_bold(void)
84 #if GTK_MAJOR_VERSION < 2
85 /* Get the regular user font height.
87 * @return the regular user font height
89 guint user_font_get_regular_height(void)
94 /* Get the regular user font width.
96 * @return the regular user font width
98 guint user_font_get_regular_width(void)
106 set_fonts(FONT_TYPE *regular, FONT_TYPE *bold)
108 /* Yes, assert. The code that loads the font should check
109 * for NULL and provide its own error message. */
110 g_assert(m_r_font && m_b_font);
114 #if GTK_MAJOR_VERSION < 2
115 m_font_height = m_r_font->ascent + m_r_font->descent;
116 m_font_width = gdk_string_width(m_r_font, "0");
121 view_zoom_in_cb(GtkWidget *w _U_, gpointer d _U_)
123 gint save_gui_zoom_level;
125 save_gui_zoom_level = recent.gui_zoom_level;
126 recent.gui_zoom_level++;
127 switch (user_font_apply()) {
132 case FA_FONT_NOT_RESIZEABLE:
133 /* "font_apply()" popped up an alert box. */
134 recent.gui_zoom_level = save_gui_zoom_level; /* undo zoom */
137 case FA_FONT_NOT_AVAILABLE:
138 /* We assume this means that the specified size isn't available. */
139 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
140 "Your current font isn't available in the next larger size.\n");
141 recent.gui_zoom_level = save_gui_zoom_level; /* undo zoom */
147 view_zoom_out_cb(GtkWidget *w _U_, gpointer d _U_)
149 gint save_gui_zoom_level;
151 save_gui_zoom_level = recent.gui_zoom_level;
152 recent.gui_zoom_level--;
153 switch (user_font_apply()) {
158 case FA_FONT_NOT_RESIZEABLE:
159 /* "font_apply()" popped up an alert box. */
160 recent.gui_zoom_level = save_gui_zoom_level; /* undo zoom */
163 case FA_FONT_NOT_AVAILABLE:
164 /* We assume this means that the specified size isn't available. */
165 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
166 "Your current font isn't available in the next smaller size.\n");
167 recent.gui_zoom_level = save_gui_zoom_level; /* undo zoom */
173 view_zoom_100_cb(GtkWidget *w _U_, gpointer d _U_)
175 gint save_gui_zoom_level;
177 save_gui_zoom_level = recent.gui_zoom_level;
178 recent.gui_zoom_level = 0;
179 switch (user_font_apply()) {
184 case FA_FONT_NOT_RESIZEABLE:
185 /* "font_apply()" popped up an alert box. */
186 recent.gui_zoom_level = save_gui_zoom_level; /* undo zoom */
189 case FA_FONT_NOT_AVAILABLE:
190 /* We assume this means that the specified size isn't available.
191 XXX - this "shouldn't happen". */
192 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
193 "Your current font couldn't be reloaded at the size you selected.\n");
194 recent.gui_zoom_level = save_gui_zoom_level; /* undo zoom */
200 #if GTK_MAJOR_VERSION < 2
201 /* Given a font name, construct the name of the next heavier version of
204 #define XLFD_WEIGHT 3 /* index of the "weight" field */
206 /* Map from a given weight to the appropriate weight for the "bold"
208 XXX - the XLFD says these strings shouldn't be used for font matching;
209 can we get the weight, as a number, from GDK, and ask GDK to find us
210 a font just like the given font, but with the appropriate higher
212 static const struct {
216 { "ultralight", "light" },
217 { "extralight", "semilight" },
218 { "light", "medium" },
219 { "semilight", "semibold" },
220 { "medium", "bold" },
221 { "normal", "bold" },
222 { "semibold", "extrabold" },
223 { "bold", "ultrabold" }
225 #define N_WEIGHTS (sizeof weight_map / sizeof weight_map[0])
227 /* Try to convert a font name to it's bold version.
229 * @param the font to convert
230 * @return the bold font
233 user_font_boldify(const char *font_name)
235 char *bold_font_name;
239 /* Is this an XLFD font? If it begins with "-", yes, otherwise no. */
240 if (font_name[0] == '-') {
241 xlfd_tokens = g_strsplit(font_name, "-", XLFD_WEIGHT+1);
244 * Make sure we *have* a weight (this might not be a valid
247 for (i = 0; i < XLFD_WEIGHT+1; i++) {
248 if (xlfd_tokens[i] == NULL) {
250 * We don't, so treat this as a non-XLFD
256 for (i = 0; i < N_WEIGHTS; i++) {
257 if (strcmp(xlfd_tokens[XLFD_WEIGHT],
258 weight_map[i].light) == 0) {
259 g_free(xlfd_tokens[XLFD_WEIGHT]);
260 xlfd_tokens[XLFD_WEIGHT] =
261 g_strdup(weight_map[i].heavier);
265 bold_font_name = g_strjoinv("-", xlfd_tokens);
266 g_strfreev(xlfd_tokens);
267 return bold_font_name;
272 * This isn't an XLFD font name; just append "bold" to the name
275 bold_font_name = g_strconcat(font_name, "bold", NULL);
276 return bold_font_name;
282 user_font_test(gchar *font_name)
284 #if GTK_MAJOR_VERSION < 2
285 gchar *bold_font_name;
287 FONT_TYPE *new_r_font, *new_b_font;
289 #if GTK_MAJOR_VERSION < 2
290 /* Get the name that the boldface version of that font would have. */
291 bold_font_name = user_font_boldify(font_name);
293 /* Now load those fonts, just to make sure we can. */
294 new_r_font = gdk_font_load(font_name);
296 new_r_font = pango_font_description_from_string(font_name);
298 if (new_r_font == NULL) {
299 /* Oops, that font didn't work.
300 Tell the user, but don't tear down the font selection
301 dialog, so that they can try again. */
302 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
303 "The font you selected can't be loaded.");
305 #if GTK_MAJOR_VERSION < 2
306 g_free(bold_font_name);
311 #if GTK_MAJOR_VERSION < 2
312 new_b_font = gdk_font_load(bold_font_name);
314 new_b_font = pango_font_description_copy(new_r_font);
315 pango_font_description_set_weight(new_b_font, PANGO_WEIGHT_BOLD);
317 if (new_b_font == NULL) {
318 /* Oops, that font didn't work.
319 Tell the user, but don't tear down the font selection
320 dialog, so that they can try again. */
321 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
322 "The font you selected doesn't have a boldface version.");
324 #if GTK_MAJOR_VERSION < 2
325 g_free(bold_font_name);
326 gdk_font_unref(new_r_font);
328 pango_font_description_free(new_r_font);
338 /* Given a font name, construct the name of a version of that font with
339 the current zoom factor applied. */
341 font_zoom(char *gui_font_name)
346 long font_point_size_l;
347 #if GTK_MAJOR_VERSION < 2
353 char *font_set_width;
354 char *font_add_style;
355 char *font_pixel_size;
356 char *font_point_size;
360 char *font_aver_width;
361 char *font_charset_reg;
362 char *font_charset_encoding;
365 if (recent.gui_zoom_level == 0) {
366 /* There is no zoom factor - just return the name, so that if
367 this is GTK+ 1.2[.x] and the font name isn't an XLFD font
368 name, we don't fail. */
369 return g_strdup(gui_font_name);
372 font_name_dup = g_strdup(gui_font_name);
373 font_name_p = font_name_dup;
375 #if GTK_MAJOR_VERSION >= 2
376 /* find the start of the font_size string */
377 font_name_p = strrchr(font_name_dup, ' ');
381 /* calculate the new font size */
382 font_point_size_l = strtol(font_name_p, NULL, 10);
383 font_point_size_l += recent.gui_zoom_level;
385 /* build a new font name */
386 new_font_name = g_strdup_printf("%s %ld", font_name_dup, font_point_size_l);
389 /* replace all '-' chars by NUL and count them */
390 while ((font_name_p = strchr(font_name_p, '-')) != NULL) {
396 if (minus_chars != 14) {
398 * Not a valid XLFD font name.
399 * XXX - can we try scaling it by looking for a size at the end
400 * and tweaking that? Unfortunately, some fonts have numbers
401 * at the end that aren't, as far as I know, sizes, e.g. "nil2".
406 /* first element (font name registry) empty */
407 font_name_p = font_name_dup;
408 font_name_p += strlen(font_name_p);
411 /* get pointers to all font name elements */
412 font_foundry = font_name_p;
413 font_name_p += strlen(font_name_p);
416 font_family = font_name_p;
417 font_name_p += strlen(font_name_p);
420 font_weight = font_name_p;
421 font_name_p += strlen(font_name_p);
424 font_slant = font_name_p;
425 font_name_p += strlen(font_name_p);
428 font_set_width = font_name_p;
429 font_name_p += strlen(font_name_p);
432 font_add_style = font_name_p;
433 font_name_p += strlen(font_name_p);
436 font_pixel_size = font_name_p;
437 font_name_p += strlen(font_name_p);
440 font_point_size = font_name_p;
441 font_name_p += strlen(font_name_p);
444 font_res_x = font_name_p;
445 font_name_p += strlen(font_name_p);
448 font_res_y = font_name_p;
449 font_name_p += strlen(font_name_p);
452 font_spacing = font_name_p;
453 font_name_p += strlen(font_name_p);
456 font_aver_width = font_name_p;
457 font_name_p += strlen(font_name_p);
460 font_charset_reg = font_name_p;
461 font_name_p += strlen(font_name_p);
464 font_charset_encoding = font_name_p;
465 font_name_p += strlen(font_name_p);
468 /* calculate the new font size */
469 font_point_size_l = strtol(font_point_size, NULL, 10);
470 font_point_size_l += recent.gui_zoom_level*10;
471 if (font_point_size_l <= 0)
472 font_point_size_l = 10;
474 /* build a new font name */
475 new_font_name = g_strdup_printf("-%s-%s-%s-%s-%s-%s-%s-%ld-%s-%s-%s-%s-%s-%s",
476 font_foundry, font_family, font_weight, font_slant, font_set_width,
477 font_add_style, font_pixel_size, font_point_size_l, font_res_x,
478 font_res_y, font_spacing, font_aver_width, font_charset_reg,
479 font_charset_encoding);
482 g_free(font_name_dup);
484 return new_font_name;
488 user_font_apply(void) {
490 #if GTK_MAJOR_VERSION < 2
491 char *bold_font_name;
493 FONT_TYPE *new_r_font, *new_b_font;
494 FONT_TYPE *old_r_font = NULL, *old_b_font = NULL;
496 /* convert font name to reflect the zoom level */
497 gui_font_name = font_zoom(prefs.PREFS_GUI_FONT_NAME);
498 if (gui_font_name == NULL) {
500 * This means the font name isn't an XLFD font name.
501 * We just report that for now as a font not available in
504 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
505 "Your current font isn't available in any other sizes.\n");
506 return FA_FONT_NOT_RESIZEABLE;
509 /* load normal and bold font */
510 #if GTK_MAJOR_VERSION < 2
511 new_r_font = gdk_font_load(gui_font_name);
512 bold_font_name = user_font_boldify(gui_font_name);
513 new_b_font = gdk_font_load(bold_font_name);
515 new_r_font = pango_font_description_from_string(gui_font_name);
516 new_b_font = pango_font_description_copy(new_r_font);
517 pango_font_description_set_weight(new_b_font, PANGO_WEIGHT_BOLD);
520 if (new_r_font == NULL || new_b_font == NULL) {
521 /* We're no longer using the new fonts; unreference them. */
522 #if GTK_MAJOR_VERSION < 2
523 if (new_r_font != NULL)
524 gdk_font_unref(new_r_font);
525 if (new_b_font != NULL)
526 gdk_font_unref(new_b_font);
528 if (new_r_font != NULL)
529 pango_font_description_free(new_r_font);
530 if (new_b_font != NULL)
531 pango_font_description_free(new_b_font);
533 g_free(gui_font_name);
535 /* We let our caller pop up a dialog box, as the error message
536 depends on the context (did they zoom in or out, or did they
537 do something else? */
538 return FA_FONT_NOT_AVAILABLE;
541 /* the font(s) seem to be ok */
542 set_plist_font(new_r_font);
543 set_ptree_font_all(new_r_font);
544 old_r_font = m_r_font;
545 old_b_font = m_b_font;
546 set_fonts(new_r_font, new_b_font);
547 #if GTK_MAJOR_VERSION < 2
548 g_free(bold_font_name);
551 /* Redraw the hex dump windows. */
552 redraw_hex_dump_all();
554 /* Redraw the "Follow TCP Stream" windows. */
557 /* We're no longer using the old fonts; unreference them. */
558 #if GTK_MAJOR_VERSION < 2
559 if (old_r_font != NULL)
560 gdk_font_unref(old_r_font);
561 if (old_b_font != NULL)
562 gdk_font_unref(old_b_font);
564 if (old_r_font != NULL)
565 pango_font_description_free(old_r_font);
566 if (old_b_font != NULL)
567 pango_font_description_free(old_b_font);
569 g_free(gui_font_name);
577 #define NAME_BUFFER_LEN 32
579 #if GTK_MAJOR_VERSION < 2
582 /* The setting of the MS default font for system stuff (menus, dialogs, ...),
583 * coming from: Allin Cottrell, http://www.ecn.wfu.edu/~cottrell/gtk_win32,
584 * Thank you very much for this! */
585 static int get_windows_font_gtk1(char *fontspec, int fontspec_len)
590 char name[NAME_BUFFER_LEN];
593 h_dc = CreateDC("DISPLAY", NULL, NULL, NULL);
594 if (h_dc == NULL) return 1;
595 h_font = GetStockObject(DEFAULT_GUI_FONT);
596 if (h_font == NULL || !SelectObject(h_dc, h_font)) {
600 len = GetTextFace(h_dc, NAME_BUFFER_LEN, name);
605 if (!GetTextMetrics(h_dc, &tm)) {
609 pix_height = tm.tmHeight;
611 g_snprintf(fontspec, fontspec_len, "-*-%s-*-*-*-*-%i-*-*-*-p-*-iso8859-1", name,
616 void app_font_gtk1_init(GtkWidget *top_level_w)
621 style = gtk_widget_get_style(top_level_w);
622 if (get_windows_font_gtk1(winfont, sizeof(winfont)) == 0)
623 style->font = gdk_font_load(winfont);
624 if (style->font) gtk_widget_set_style(top_level_w, style);
628 #else /* GTK_MAJOR_VERSION */
629 static char appfontname[128] = "tahoma 8";
632 set_app_font_gtk2(const char *fontname)
634 GtkSettings *settings;
636 if (fontname != NULL && *fontname == 0) return;
638 settings = gtk_settings_get_default();
640 if (fontname == NULL) {
641 g_object_set(G_OBJECT(settings), "gtk-font-name", appfontname, NULL);
644 PangoFontDescription *pfd;
648 w = gtk_label_new(NULL);
649 pfd = pango_font_description_from_string(fontname);
650 pc = gtk_widget_get_pango_context(w);
651 pfont = pango_context_load_font(pc, pfd);
654 strcpy(appfontname, fontname);
655 g_object_set(G_OBJECT(settings), "gtk-font-name", appfontname, NULL);
658 gtk_widget_destroy(w);
659 pango_font_description_free(pfd);
663 static char *default_windows_menu_fontspec_gtk2(void)
665 gchar *fontspec = NULL;
666 NONCLIENTMETRICS ncm;
668 memset(&ncm, 0, sizeof ncm);
669 ncm.cbSize = sizeof ncm;
671 if (SystemParametersInfo(SPI_GETNONCLIENTMETRICS, ncm.cbSize, &ncm, 0)) {
672 HDC screen = GetDC(0);
673 double y_scale = 72.0 / GetDeviceCaps(screen, LOGPIXELSY);
674 int point_size = (int) (ncm.lfMenuFont.lfHeight * y_scale);
676 if (point_size < 0) point_size = -point_size;
677 fontspec = g_strdup_printf("%s %d", ncm.lfMenuFont.lfFaceName,
679 ReleaseDC(0, screen);
685 static void try_to_get_windows_font_gtk2(void)
689 fontspec = default_windows_menu_fontspec_gtk2();
691 if (fontspec != NULL) {
693 PangoFontDescription *pfd;
698 pfd = pango_font_description_from_string(fontspec);
700 w = gtk_label_new(NULL);
701 pc = gtk_widget_get_pango_context(w);
702 pfont = pango_context_load_font(pc, pfd);
703 match = (pfont != NULL);
705 pango_font_description_free(pfd);
706 g_object_unref(G_OBJECT(pc));
707 gtk_widget_destroy(w);
709 if (match) set_app_font_gtk2(fontspec);
713 #endif /* GTK_MAJOR_VERSION */
720 #if GTK_MAJOR_VERSION < 2
721 gchar *bold_font_name;
725 #if GTK_MAJOR_VERSION >= 2
726 /* try to load the application font for GTK2 */
727 try_to_get_windows_font_gtk2();
731 /* Try to load the regular and boldface fixed-width fonts */
732 #if GTK_MAJOR_VERSION < 2
733 bold_font_name = user_font_boldify(prefs.gui_font_name1);
734 m_r_font = gdk_font_load(prefs.gui_font_name1);
735 m_b_font = gdk_font_load(bold_font_name);
736 if (m_r_font == NULL || m_b_font == NULL) {
737 /* XXX - pop this up as a dialog box? no */
738 if (m_r_font == NULL) {
742 fprintf(stderr, "ethereal: Warning: font %s not found - defaulting to 6x13 and 6x13bold\n",
743 prefs.gui_font_name1);
745 gdk_font_unref(m_r_font);
747 if (m_b_font == NULL) {
751 fprintf(stderr, "ethereal: Warning: font %s not found - defaulting to 6x13 and 6x13bold\n",
754 gdk_font_unref(m_b_font);
756 g_free(bold_font_name);
757 if ((m_r_font = gdk_font_load("6x13")) == NULL) {
758 fprintf(stderr, "ethereal: Error: font 6x13 not found\n");
761 if ((m_b_font = gdk_font_load("6x13bold")) == NULL) {
762 fprintf(stderr, "ethereal: Error: font 6x13bold not found\n");
765 g_free(prefs.gui_font_name1);
766 prefs.gui_font_name1 = g_strdup("6x13");
768 #else /* GTK_MAJOR_VERSION */
769 m_r_font = pango_font_description_from_string(prefs.gui_font_name2);
770 m_b_font = pango_font_description_copy(m_r_font);
771 pango_font_description_set_weight(m_b_font, PANGO_WEIGHT_BOLD);
772 if (m_r_font == NULL || m_b_font == NULL) {
773 /* XXX - pop this up as a dialog box? no */
774 if (m_r_font == NULL) {
778 fprintf(stderr, "ethereal: Warning: font %s not found - defaulting to Monospace 9\n",
779 prefs.gui_font_name2);
781 pango_font_description_free(m_r_font);
783 if (m_b_font == NULL) {
787 fprintf(stderr, "ethereal: Warning: bold font %s not found - defaulting"
788 " to Monospace 9\n", prefs.gui_font_name2);
790 pango_font_description_free(m_b_font);
792 if ((m_r_font = pango_font_description_from_string("Monospace 9")) == NULL)
794 fprintf(stderr, "ethereal: Error: font Monospace 9 not found\n");
797 if ((m_b_font = pango_font_description_copy(m_r_font)) == NULL) {
798 fprintf(stderr, "ethereal: Error: font Monospace 9 bold not found\n");
801 g_free(prefs.gui_font_name2);
802 pango_font_description_set_weight(m_b_font, PANGO_WEIGHT_BOLD);
803 prefs.gui_font_name2 = g_strdup("Monospace 9");
805 #endif /* GTK_MAJOR_VERSION */
807 /* Call this for the side-effects that set_fonts() produces */
808 set_fonts(m_r_font, m_b_font);