2 * Utilities to use for font manipulation
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
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>
35 #include <epan/ws_strsplit.h>
40 #include <epan/unicode-utils.h>
45 #include <epan/prefs.h>
47 #include "gtkglobals.h"
49 #include "compat_macros.h"
50 #include "font_utils.h"
51 #include "simple_dialog.h"
53 #include "packet_list.h"
54 #include "proto_draw.h"
55 #include "follow_tcp.h"
59 #if GTK_MAJOR_VERSION < 2
60 guint m_font_height, m_font_width;
62 FONT_TYPE *m_r_font, *m_b_font;
65 /* Get the regular user font.
67 * @return the regular user font
69 FONT_TYPE *user_font_get_regular(void)
74 /* Get the bold user font.
76 * @return the bold user font
78 FONT_TYPE *user_font_get_bold(void)
83 #if GTK_MAJOR_VERSION < 2
84 /* Get the regular user font height.
86 * @return the regular user font height
88 guint user_font_get_regular_height(void)
93 /* Get the regular user font width.
95 * @return the regular user font width
97 guint user_font_get_regular_width(void)
105 set_fonts(FONT_TYPE *regular, FONT_TYPE *bold)
107 /* Yes, assert. The code that loads the font should check
108 * for NULL and provide its own error message. */
109 g_assert(m_r_font && m_b_font);
113 #if GTK_MAJOR_VERSION < 2
114 m_font_height = m_r_font->ascent + m_r_font->descent;
115 m_font_width = gdk_string_width(m_r_font, "0");
120 view_zoom_in_cb(GtkWidget *w _U_, gpointer d _U_)
122 gint save_gui_zoom_level;
124 save_gui_zoom_level = recent.gui_zoom_level;
125 recent.gui_zoom_level++;
126 switch (user_font_apply()) {
131 case FA_FONT_NOT_RESIZEABLE:
132 /* "font_apply()" popped up an alert box. */
133 recent.gui_zoom_level = save_gui_zoom_level; /* undo zoom */
136 case FA_FONT_NOT_AVAILABLE:
137 /* We assume this means that the specified size isn't available. */
138 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
139 "Your current font isn't available in the next larger size.\n");
140 recent.gui_zoom_level = save_gui_zoom_level; /* undo zoom */
146 view_zoom_out_cb(GtkWidget *w _U_, gpointer d _U_)
148 gint save_gui_zoom_level;
150 save_gui_zoom_level = recent.gui_zoom_level;
151 recent.gui_zoom_level--;
152 switch (user_font_apply()) {
157 case FA_FONT_NOT_RESIZEABLE:
158 /* "font_apply()" popped up an alert box. */
159 recent.gui_zoom_level = save_gui_zoom_level; /* undo zoom */
162 case FA_FONT_NOT_AVAILABLE:
163 /* We assume this means that the specified size isn't available. */
164 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
165 "Your current font isn't available in the next smaller size.\n");
166 recent.gui_zoom_level = save_gui_zoom_level; /* undo zoom */
172 view_zoom_100_cb(GtkWidget *w _U_, gpointer d _U_)
174 gint save_gui_zoom_level;
176 save_gui_zoom_level = recent.gui_zoom_level;
177 recent.gui_zoom_level = 0;
178 switch (user_font_apply()) {
183 case FA_FONT_NOT_RESIZEABLE:
184 /* "font_apply()" popped up an alert box. */
185 recent.gui_zoom_level = save_gui_zoom_level; /* undo zoom */
188 case FA_FONT_NOT_AVAILABLE:
189 /* We assume this means that the specified size isn't available.
190 XXX - this "shouldn't happen". */
191 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
192 "Your current font couldn't be reloaded at the size you selected.\n");
193 recent.gui_zoom_level = save_gui_zoom_level; /* undo zoom */
199 #if GTK_MAJOR_VERSION < 2
200 /* Given a font name, construct the name of the next heavier version of
203 #define XLFD_WEIGHT 3 /* index of the "weight" field */
205 /* Map from a given weight to the appropriate weight for the "bold"
207 XXX - the XLFD says these strings shouldn't be used for font matching;
208 can we get the weight, as a number, from GDK, and ask GDK to find us
209 a font just like the given font, but with the appropriate higher
211 static const struct {
215 { "ultralight", "light" },
216 { "extralight", "semilight" },
217 { "light", "medium" },
218 { "semilight", "semibold" },
219 { "medium", "bold" },
220 { "normal", "bold" },
221 { "semibold", "extrabold" },
222 { "bold", "ultrabold" }
224 #define N_WEIGHTS (sizeof weight_map / sizeof weight_map[0])
226 /* Try to convert a font name to it's bold version.
228 * @param the font to convert
229 * @return the bold font
232 user_font_boldify(const char *font_name)
234 char *bold_font_name;
238 /* Is this an XLFD font? If it begins with "-", yes, otherwise no. */
239 if (font_name[0] == '-') {
240 xlfd_tokens = g_strsplit(font_name, "-", XLFD_WEIGHT+1);
243 * Make sure we *have* a weight (this might not be a valid
246 for (i = 0; i < XLFD_WEIGHT+1; i++) {
247 if (xlfd_tokens[i] == NULL) {
249 * We don't, so treat this as a non-XLFD
255 for (i = 0; i < N_WEIGHTS; i++) {
256 if (strcmp(xlfd_tokens[XLFD_WEIGHT],
257 weight_map[i].light) == 0) {
258 g_free(xlfd_tokens[XLFD_WEIGHT]);
259 xlfd_tokens[XLFD_WEIGHT] =
260 g_strdup(weight_map[i].heavier);
264 bold_font_name = g_strjoinv("-", xlfd_tokens);
265 g_strfreev(xlfd_tokens);
266 return bold_font_name;
271 * This isn't an XLFD font name; just append "bold" to the name
274 bold_font_name = g_strconcat(font_name, "bold", NULL);
275 return bold_font_name;
281 user_font_test(gchar *font_name)
283 #if GTK_MAJOR_VERSION < 2
284 gchar *bold_font_name;
286 FONT_TYPE *new_r_font, *new_b_font;
288 #if GTK_MAJOR_VERSION < 2
289 /* Get the name that the boldface version of that font would have. */
290 bold_font_name = user_font_boldify(font_name);
292 /* Now load those fonts, just to make sure we can. */
293 new_r_font = gdk_font_load(font_name);
295 new_r_font = pango_font_description_from_string(font_name);
297 if (new_r_font == NULL) {
298 /* Oops, that font didn't work.
299 Tell the user, but don't tear down the font selection
300 dialog, so that they can try again. */
301 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
302 "The font you selected can't be loaded.");
304 #if GTK_MAJOR_VERSION < 2
305 g_free(bold_font_name);
310 #if GTK_MAJOR_VERSION < 2
311 new_b_font = gdk_font_load(bold_font_name);
313 new_b_font = pango_font_description_copy(new_r_font);
314 pango_font_description_set_weight(new_b_font, PANGO_WEIGHT_BOLD);
316 if (new_b_font == NULL) {
317 /* Oops, that font didn't work.
318 Tell the user, but don't tear down the font selection
319 dialog, so that they can try again. */
320 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
321 "The font you selected doesn't have a boldface version.");
323 #if GTK_MAJOR_VERSION < 2
324 g_free(bold_font_name);
325 gdk_font_unref(new_r_font);
327 pango_font_description_free(new_r_font);
337 /* Given a font name, construct the name of a version of that font with
338 the current zoom factor applied. */
340 font_zoom(char *gui_font_name)
345 long font_point_size_l;
346 #if GTK_MAJOR_VERSION < 2
352 char *font_set_width;
353 char *font_add_style;
354 char *font_pixel_size;
355 char *font_point_size;
359 char *font_aver_width;
360 char *font_charset_reg;
361 char *font_charset_encoding;
364 if (recent.gui_zoom_level == 0) {
365 /* There is no zoom factor - just return the name, so that if
366 this is GTK+ 1.2[.x] and the font name isn't an XLFD font
367 name, we don't fail. */
368 return g_strdup(gui_font_name);
371 font_name_dup = g_strdup(gui_font_name);
372 font_name_p = font_name_dup;
374 #if GTK_MAJOR_VERSION >= 2
375 /* find the start of the font_size string */
376 font_name_p = strrchr(font_name_dup, ' ');
380 /* calculate the new font size */
381 font_point_size_l = strtol(font_name_p, NULL, 10);
382 font_point_size_l += recent.gui_zoom_level;
384 /* build a new font name */
385 new_font_name = g_strdup_printf("%s %ld", font_name_dup, font_point_size_l);
388 /* replace all '-' chars by NUL and count them */
389 while ((font_name_p = strchr(font_name_p, '-')) != NULL) {
395 if (minus_chars != 14) {
397 * Not a valid XLFD font name.
398 * XXX - can we try scaling it by looking for a size at the end
399 * and tweaking that? Unfortunately, some fonts have numbers
400 * at the end that aren't, as far as I know, sizes, e.g. "nil2".
405 /* first element (font name registry) empty */
406 font_name_p = font_name_dup;
407 font_name_p += strlen(font_name_p);
410 /* get pointers to all font name elements */
411 font_foundry = font_name_p;
412 font_name_p += strlen(font_name_p);
415 font_family = font_name_p;
416 font_name_p += strlen(font_name_p);
419 font_weight = font_name_p;
420 font_name_p += strlen(font_name_p);
423 font_slant = font_name_p;
424 font_name_p += strlen(font_name_p);
427 font_set_width = font_name_p;
428 font_name_p += strlen(font_name_p);
431 font_add_style = font_name_p;
432 font_name_p += strlen(font_name_p);
435 font_pixel_size = font_name_p;
436 font_name_p += strlen(font_name_p);
439 font_point_size = font_name_p;
440 font_name_p += strlen(font_name_p);
443 font_res_x = font_name_p;
444 font_name_p += strlen(font_name_p);
447 font_res_y = font_name_p;
448 font_name_p += strlen(font_name_p);
451 font_spacing = font_name_p;
452 font_name_p += strlen(font_name_p);
455 font_aver_width = font_name_p;
456 font_name_p += strlen(font_name_p);
459 font_charset_reg = font_name_p;
460 font_name_p += strlen(font_name_p);
463 font_charset_encoding = font_name_p;
464 font_name_p += strlen(font_name_p);
467 /* calculate the new font size */
468 font_point_size_l = strtol(font_point_size, NULL, 10);
469 font_point_size_l += recent.gui_zoom_level*10;
470 if (font_point_size_l <= 0)
471 font_point_size_l = 10;
473 /* build a new font name */
474 new_font_name = g_strdup_printf("-%s-%s-%s-%s-%s-%s-%s-%ld-%s-%s-%s-%s-%s-%s",
475 font_foundry, font_family, font_weight, font_slant, font_set_width,
476 font_add_style, font_pixel_size, font_point_size_l, font_res_x,
477 font_res_y, font_spacing, font_aver_width, font_charset_reg,
478 font_charset_encoding);
481 g_free(font_name_dup);
483 return new_font_name;
487 user_font_apply(void) {
489 #if GTK_MAJOR_VERSION < 2
490 char *bold_font_name;
492 FONT_TYPE *new_r_font, *new_b_font;
493 FONT_TYPE *old_r_font = NULL, *old_b_font = NULL;
495 /* convert font name to reflect the zoom level */
496 gui_font_name = font_zoom(prefs.PREFS_GUI_FONT_NAME);
497 if (gui_font_name == NULL) {
499 * This means the font name isn't an XLFD font name.
500 * We just report that for now as a font not available in
503 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
504 "Your current font isn't available in any other sizes.\n");
505 return FA_FONT_NOT_RESIZEABLE;
508 /* load normal and bold font */
509 #if GTK_MAJOR_VERSION < 2
510 new_r_font = gdk_font_load(gui_font_name);
511 bold_font_name = user_font_boldify(gui_font_name);
512 new_b_font = gdk_font_load(bold_font_name);
514 new_r_font = pango_font_description_from_string(gui_font_name);
515 new_b_font = pango_font_description_copy(new_r_font);
516 pango_font_description_set_weight(new_b_font, PANGO_WEIGHT_BOLD);
519 if (new_r_font == NULL || new_b_font == NULL) {
520 /* We're no longer using the new fonts; unreference them. */
521 #if GTK_MAJOR_VERSION < 2
522 if (new_r_font != NULL)
523 gdk_font_unref(new_r_font);
524 if (new_b_font != NULL)
525 gdk_font_unref(new_b_font);
527 if (new_r_font != NULL)
528 pango_font_description_free(new_r_font);
529 if (new_b_font != NULL)
530 pango_font_description_free(new_b_font);
532 g_free(gui_font_name);
534 /* We let our caller pop up a dialog box, as the error message
535 depends on the context (did they zoom in or out, or did they
536 do something else? */
537 return FA_FONT_NOT_AVAILABLE;
540 /* the font(s) seem to be ok */
541 packet_list_set_font(new_r_font);
542 set_ptree_font_all(new_r_font);
543 old_r_font = m_r_font;
544 old_b_font = m_b_font;
545 set_fonts(new_r_font, new_b_font);
546 #if GTK_MAJOR_VERSION < 2
547 g_free(bold_font_name);
550 /* Redraw the hex dump windows. */
551 redraw_hex_dump_all();
553 /* Redraw the "Follow TCP Stream" windows. */
554 follow_tcp_redraw_all();
556 /* We're no longer using the old fonts; unreference them. */
557 #if GTK_MAJOR_VERSION < 2
558 if (old_r_font != NULL)
559 gdk_font_unref(old_r_font);
560 if (old_b_font != NULL)
561 gdk_font_unref(old_b_font);
563 if (old_r_font != NULL)
564 pango_font_description_free(old_r_font);
565 if (old_b_font != NULL)
566 pango_font_description_free(old_b_font);
568 g_free(gui_font_name);
576 #define NAME_BUFFER_LEN 32
578 #if GTK_MAJOR_VERSION < 2
581 /* The setting of the MS default font for system stuff (menus, dialogs, ...),
582 * coming from: Allin Cottrell, http://www.ecn.wfu.edu/~cottrell/gtk_win32,
583 * Thank you very much for this! */
584 static int get_windows_font_gtk1(char *fontspec, int fontspec_len)
589 TCHAR name[NAME_BUFFER_LEN];
592 h_dc = CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
593 if (h_dc == NULL) return 1;
594 h_font = GetStockObject(DEFAULT_GUI_FONT);
595 if (h_font == NULL || !SelectObject(h_dc, h_font)) {
599 len = GetTextFace(h_dc, NAME_BUFFER_LEN, name);
604 if (!GetTextMetrics(h_dc, &tm)) {
608 pix_height = tm.tmHeight;
610 g_snprintf(fontspec, fontspec_len, "-*-%s-*-*-*-*-%i-*-*-*-p-*-iso8859-1",
611 utf_16to8(name), pix_height);
615 void app_font_gtk1_init(GtkWidget *top_level_w)
620 style = gtk_widget_get_style(top_level_w);
621 if (get_windows_font_gtk1(winfont, sizeof(winfont)) == 0)
622 style->font = gdk_font_load(winfont);
623 if (style->font) gtk_widget_set_style(top_level_w, style);
627 #else /* GTK_MAJOR_VERSION */
628 static char appfontname[128] = "tahoma 8";
631 set_app_font_gtk2(const char *fontname)
633 GtkSettings *settings;
635 if (fontname != NULL && *fontname == 0) return;
637 settings = gtk_settings_get_default();
639 if (fontname == NULL) {
640 g_object_set(G_OBJECT(settings), "gtk-font-name", appfontname, NULL);
643 PangoFontDescription *pfd;
647 w = gtk_label_new(NULL);
648 pfd = pango_font_description_from_string(fontname);
649 pc = gtk_widget_get_pango_context(w);
650 pfont = pango_context_load_font(pc, pfd);
653 strncpy(appfontname, fontname, 128);
654 appfontname[127] = '\0';
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) {
739 fprintf(stderr, "wireshark: Warning: font %s not found - defaulting to 6x13 and 6x13bold\n",
740 prefs.gui_font_name1);
742 gdk_font_unref(m_r_font);
744 if (m_b_font == NULL) {
745 fprintf(stderr, "wireshark: Warning: font %s not found - defaulting to 6x13 and 6x13bold\n",
748 gdk_font_unref(m_b_font);
750 g_free(bold_font_name);
751 if ((m_r_font = gdk_font_load("6x13")) == NULL) {
752 fprintf(stderr, "wireshark: Error: font 6x13 not found\n");
755 if ((m_b_font = gdk_font_load("6x13bold")) == NULL) {
756 fprintf(stderr, "wireshark: Error: font 6x13bold not found\n");
759 g_free(prefs.gui_font_name1);
760 prefs.gui_font_name1 = g_strdup("6x13");
762 #else /* GTK_MAJOR_VERSION */
763 m_r_font = pango_font_description_from_string(prefs.gui_font_name2);
764 m_b_font = pango_font_description_copy(m_r_font);
765 pango_font_description_set_weight(m_b_font, PANGO_WEIGHT_BOLD);
766 if (m_r_font == NULL || m_b_font == NULL) {
767 /* XXX - pop this up as a dialog box? no */
768 if (m_r_font == NULL) {
769 fprintf(stderr, "wireshark: Warning: font %s not found - defaulting to Monospace 9\n",
770 prefs.gui_font_name2);
772 pango_font_description_free(m_r_font);
774 if (m_b_font == NULL) {
775 fprintf(stderr, "wireshark: Warning: bold font %s not found - defaulting"
776 " to Monospace 9\n", prefs.gui_font_name2);
778 pango_font_description_free(m_b_font);
780 if ((m_r_font = pango_font_description_from_string("Monospace 9")) == NULL)
782 fprintf(stderr, "wireshark: Error: font Monospace 9 not found\n");
785 if ((m_b_font = pango_font_description_copy(m_r_font)) == NULL) {
786 fprintf(stderr, "wireshark: Error: font Monospace 9 bold not found\n");
789 g_free(prefs.gui_font_name2);
790 pango_font_description_set_weight(m_b_font, PANGO_WEIGHT_BOLD);
791 prefs.gui_font_name2 = g_strdup("Monospace 9");
793 #endif /* GTK_MAJOR_VERSION */
795 /* Call this for the side-effects that set_fonts() produces */
796 set_fonts(m_r_font, m_b_font);