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>
39 #include <epan/strutil.h>
44 #include <epan/prefs.h>
46 #include "gtkglobals.h"
48 #include "compat_macros.h"
49 #include "font_utils.h"
50 #include "simple_dialog.h"
52 #include "packet_list.h"
53 #include "proto_draw.h"
54 #include "follow_dlg.h"
58 #if GTK_MAJOR_VERSION < 2
59 guint m_font_height, m_font_width;
61 FONT_TYPE *m_r_font, *m_b_font;
64 /* Get the regular user font.
66 * @return the regular user font
68 FONT_TYPE *user_font_get_regular(void)
73 /* Get the bold user font.
75 * @return the bold user font
77 FONT_TYPE *user_font_get_bold(void)
82 #if GTK_MAJOR_VERSION < 2
83 /* Get the regular user font height.
85 * @return the regular user font height
87 guint user_font_get_regular_height(void)
92 /* Get the regular user font width.
94 * @return the regular user font width
96 guint user_font_get_regular_width(void)
104 set_fonts(FONT_TYPE *regular, FONT_TYPE *bold)
106 /* Yes, assert. The code that loads the font should check
107 * for NULL and provide its own error message. */
108 g_assert(m_r_font && m_b_font);
112 #if GTK_MAJOR_VERSION < 2
113 m_font_height = m_r_font->ascent + m_r_font->descent;
114 m_font_width = gdk_string_width(m_r_font, "0");
119 view_zoom_in_cb(GtkWidget *w _U_, gpointer d _U_)
121 gint save_gui_zoom_level;
123 save_gui_zoom_level = recent.gui_zoom_level;
124 recent.gui_zoom_level++;
125 switch (user_font_apply()) {
130 case FA_FONT_NOT_RESIZEABLE:
131 /* "font_apply()" popped up an alert box. */
132 recent.gui_zoom_level = save_gui_zoom_level; /* undo zoom */
135 case FA_FONT_NOT_AVAILABLE:
136 /* We assume this means that the specified size isn't available. */
137 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
138 "Your current font isn't available in the next larger size.\n");
139 recent.gui_zoom_level = save_gui_zoom_level; /* undo zoom */
145 view_zoom_out_cb(GtkWidget *w _U_, gpointer d _U_)
147 gint save_gui_zoom_level;
149 save_gui_zoom_level = recent.gui_zoom_level;
150 recent.gui_zoom_level--;
151 switch (user_font_apply()) {
156 case FA_FONT_NOT_RESIZEABLE:
157 /* "font_apply()" popped up an alert box. */
158 recent.gui_zoom_level = save_gui_zoom_level; /* undo zoom */
161 case FA_FONT_NOT_AVAILABLE:
162 /* We assume this means that the specified size isn't available. */
163 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
164 "Your current font isn't available in the next smaller size.\n");
165 recent.gui_zoom_level = save_gui_zoom_level; /* undo zoom */
171 view_zoom_100_cb(GtkWidget *w _U_, gpointer d _U_)
173 gint save_gui_zoom_level;
175 save_gui_zoom_level = recent.gui_zoom_level;
176 recent.gui_zoom_level = 0;
177 switch (user_font_apply()) {
182 case FA_FONT_NOT_RESIZEABLE:
183 /* "font_apply()" popped up an alert box. */
184 recent.gui_zoom_level = save_gui_zoom_level; /* undo zoom */
187 case FA_FONT_NOT_AVAILABLE:
188 /* We assume this means that the specified size isn't available.
189 XXX - this "shouldn't happen". */
190 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
191 "Your current font couldn't be reloaded at the size you selected.\n");
192 recent.gui_zoom_level = save_gui_zoom_level; /* undo zoom */
198 #if GTK_MAJOR_VERSION < 2
199 /* Given a font name, construct the name of the next heavier version of
202 #define XLFD_WEIGHT 3 /* index of the "weight" field */
204 /* Map from a given weight to the appropriate weight for the "bold"
206 XXX - the XLFD says these strings shouldn't be used for font matching;
207 can we get the weight, as a number, from GDK, and ask GDK to find us
208 a font just like the given font, but with the appropriate higher
210 static const struct {
214 { "ultralight", "light" },
215 { "extralight", "semilight" },
216 { "light", "medium" },
217 { "semilight", "semibold" },
218 { "medium", "bold" },
219 { "normal", "bold" },
220 { "semibold", "extrabold" },
221 { "bold", "ultrabold" }
223 #define N_WEIGHTS (sizeof weight_map / sizeof weight_map[0])
225 /* Try to convert a font name to it's bold version.
227 * @param the font to convert
228 * @return the bold font
231 user_font_boldify(const char *font_name)
233 char *bold_font_name;
237 /* Is this an XLFD font? If it begins with "-", yes, otherwise no. */
238 if (font_name[0] == '-') {
239 xlfd_tokens = g_strsplit(font_name, "-", XLFD_WEIGHT+1);
242 * Make sure we *have* a weight (this might not be a valid
245 for (i = 0; i < XLFD_WEIGHT+1; i++) {
246 if (xlfd_tokens[i] == NULL) {
248 * We don't, so treat this as a non-XLFD
254 for (i = 0; i < N_WEIGHTS; i++) {
255 if (strcmp(xlfd_tokens[XLFD_WEIGHT],
256 weight_map[i].light) == 0) {
257 g_free(xlfd_tokens[XLFD_WEIGHT]);
258 xlfd_tokens[XLFD_WEIGHT] =
259 g_strdup(weight_map[i].heavier);
263 bold_font_name = g_strjoinv("-", xlfd_tokens);
264 g_strfreev(xlfd_tokens);
265 return bold_font_name;
270 * This isn't an XLFD font name; just append "bold" to the name
273 bold_font_name = g_strconcat(font_name, "bold", NULL);
274 return bold_font_name;
280 user_font_test(gchar *font_name)
282 #if GTK_MAJOR_VERSION < 2
283 gchar *bold_font_name;
285 FONT_TYPE *new_r_font, *new_b_font;
287 #if GTK_MAJOR_VERSION < 2
288 /* Get the name that the boldface version of that font would have. */
289 bold_font_name = user_font_boldify(font_name);
291 /* Now load those fonts, just to make sure we can. */
292 new_r_font = gdk_font_load(font_name);
294 new_r_font = pango_font_description_from_string(font_name);
296 if (new_r_font == NULL) {
297 /* Oops, that font didn't work.
298 Tell the user, but don't tear down the font selection
299 dialog, so that they can try again. */
300 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
301 "The font you selected can't be loaded.");
303 #if GTK_MAJOR_VERSION < 2
304 g_free(bold_font_name);
309 #if GTK_MAJOR_VERSION < 2
310 new_b_font = gdk_font_load(bold_font_name);
312 new_b_font = pango_font_description_copy(new_r_font);
313 pango_font_description_set_weight(new_b_font, PANGO_WEIGHT_BOLD);
315 if (new_b_font == NULL) {
316 /* Oops, that font didn't work.
317 Tell the user, but don't tear down the font selection
318 dialog, so that they can try again. */
319 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
320 "The font you selected doesn't have a boldface version.");
322 #if GTK_MAJOR_VERSION < 2
323 g_free(bold_font_name);
324 gdk_font_unref(new_r_font);
326 pango_font_description_free(new_r_font);
336 /* Given a font name, construct the name of a version of that font with
337 the current zoom factor applied. */
339 font_zoom(char *gui_font_name)
344 long font_point_size_l;
345 #if GTK_MAJOR_VERSION < 2
351 char *font_set_width;
352 char *font_add_style;
353 char *font_pixel_size;
354 char *font_point_size;
358 char *font_aver_width;
359 char *font_charset_reg;
360 char *font_charset_encoding;
363 if (recent.gui_zoom_level == 0) {
364 /* There is no zoom factor - just return the name, so that if
365 this is GTK+ 1.2[.x] and the font name isn't an XLFD font
366 name, we don't fail. */
367 return g_strdup(gui_font_name);
370 font_name_dup = g_strdup(gui_font_name);
371 font_name_p = font_name_dup;
373 #if GTK_MAJOR_VERSION >= 2
374 /* find the start of the font_size string */
375 font_name_p = strrchr(font_name_dup, ' ');
379 /* calculate the new font size */
380 font_point_size_l = strtol(font_name_p, NULL, 10);
381 font_point_size_l += recent.gui_zoom_level;
383 /* build a new font name */
384 new_font_name = g_strdup_printf("%s %ld", font_name_dup, font_point_size_l);
387 /* replace all '-' chars by NUL and count them */
388 while ((font_name_p = strchr(font_name_p, '-')) != NULL) {
394 if (minus_chars != 14) {
396 * Not a valid XLFD font name.
397 * XXX - can we try scaling it by looking for a size at the end
398 * and tweaking that? Unfortunately, some fonts have numbers
399 * at the end that aren't, as far as I know, sizes, e.g. "nil2".
404 /* first element (font name registry) empty */
405 font_name_p = font_name_dup;
406 font_name_p += strlen(font_name_p);
409 /* get pointers to all font name elements */
410 font_foundry = font_name_p;
411 font_name_p += strlen(font_name_p);
414 font_family = font_name_p;
415 font_name_p += strlen(font_name_p);
418 font_weight = font_name_p;
419 font_name_p += strlen(font_name_p);
422 font_slant = font_name_p;
423 font_name_p += strlen(font_name_p);
426 font_set_width = font_name_p;
427 font_name_p += strlen(font_name_p);
430 font_add_style = font_name_p;
431 font_name_p += strlen(font_name_p);
434 font_pixel_size = font_name_p;
435 font_name_p += strlen(font_name_p);
438 font_point_size = font_name_p;
439 font_name_p += strlen(font_name_p);
442 font_res_x = font_name_p;
443 font_name_p += strlen(font_name_p);
446 font_res_y = font_name_p;
447 font_name_p += strlen(font_name_p);
450 font_spacing = font_name_p;
451 font_name_p += strlen(font_name_p);
454 font_aver_width = font_name_p;
455 font_name_p += strlen(font_name_p);
458 font_charset_reg = font_name_p;
459 font_name_p += strlen(font_name_p);
462 font_charset_encoding = font_name_p;
463 font_name_p += strlen(font_name_p);
466 /* calculate the new font size */
467 font_point_size_l = strtol(font_point_size, NULL, 10);
468 font_point_size_l += recent.gui_zoom_level*10;
469 if (font_point_size_l <= 0)
470 font_point_size_l = 10;
472 /* build a new font name */
473 new_font_name = g_strdup_printf("-%s-%s-%s-%s-%s-%s-%s-%ld-%s-%s-%s-%s-%s-%s",
474 font_foundry, font_family, font_weight, font_slant, font_set_width,
475 font_add_style, font_pixel_size, font_point_size_l, font_res_x,
476 font_res_y, font_spacing, font_aver_width, font_charset_reg,
477 font_charset_encoding);
480 g_free(font_name_dup);
482 return new_font_name;
486 user_font_apply(void) {
488 #if GTK_MAJOR_VERSION < 2
489 char *bold_font_name;
491 FONT_TYPE *new_r_font, *new_b_font;
492 FONT_TYPE *old_r_font = NULL, *old_b_font = NULL;
494 /* convert font name to reflect the zoom level */
495 gui_font_name = font_zoom(prefs.PREFS_GUI_FONT_NAME);
496 if (gui_font_name == NULL) {
498 * This means the font name isn't an XLFD font name.
499 * We just report that for now as a font not available in
502 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
503 "Your current font isn't available in any other sizes.\n");
504 return FA_FONT_NOT_RESIZEABLE;
507 /* load normal and bold font */
508 #if GTK_MAJOR_VERSION < 2
509 new_r_font = gdk_font_load(gui_font_name);
510 bold_font_name = user_font_boldify(gui_font_name);
511 new_b_font = gdk_font_load(bold_font_name);
513 new_r_font = pango_font_description_from_string(gui_font_name);
514 new_b_font = pango_font_description_copy(new_r_font);
515 pango_font_description_set_weight(new_b_font, PANGO_WEIGHT_BOLD);
518 if (new_r_font == NULL || new_b_font == NULL) {
519 /* We're no longer using the new fonts; unreference them. */
520 #if GTK_MAJOR_VERSION < 2
521 if (new_r_font != NULL)
522 gdk_font_unref(new_r_font);
523 if (new_b_font != NULL)
524 gdk_font_unref(new_b_font);
526 if (new_r_font != NULL)
527 pango_font_description_free(new_r_font);
528 if (new_b_font != NULL)
529 pango_font_description_free(new_b_font);
531 g_free(gui_font_name);
533 /* We let our caller pop up a dialog box, as the error message
534 depends on the context (did they zoom in or out, or did they
535 do something else? */
536 return FA_FONT_NOT_AVAILABLE;
539 /* the font(s) seem to be ok */
540 packet_list_set_font(new_r_font);
541 set_ptree_font_all(new_r_font);
542 old_r_font = m_r_font;
543 old_b_font = m_b_font;
544 set_fonts(new_r_font, new_b_font);
545 #if GTK_MAJOR_VERSION < 2
546 g_free(bold_font_name);
549 /* Redraw the hex dump windows. */
550 redraw_hex_dump_all();
552 /* Redraw the "Follow TCP Stream" windows. */
555 /* We're no longer using the old fonts; unreference them. */
556 #if GTK_MAJOR_VERSION < 2
557 if (old_r_font != NULL)
558 gdk_font_unref(old_r_font);
559 if (old_b_font != NULL)
560 gdk_font_unref(old_b_font);
562 if (old_r_font != NULL)
563 pango_font_description_free(old_r_font);
564 if (old_b_font != NULL)
565 pango_font_description_free(old_b_font);
567 g_free(gui_font_name);
575 #define NAME_BUFFER_LEN 32
577 #if GTK_MAJOR_VERSION < 2
580 /* The setting of the MS default font for system stuff (menus, dialogs, ...),
581 * coming from: Allin Cottrell, http://www.ecn.wfu.edu/~cottrell/gtk_win32,
582 * Thank you very much for this! */
583 static int get_windows_font_gtk1(char *fontspec, int fontspec_len)
588 TCHAR name[NAME_BUFFER_LEN];
591 h_dc = CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
592 if (h_dc == NULL) return 1;
593 h_font = GetStockObject(DEFAULT_GUI_FONT);
594 if (h_font == NULL || !SelectObject(h_dc, h_font)) {
598 len = GetTextFace(h_dc, NAME_BUFFER_LEN, name);
603 if (!GetTextMetrics(h_dc, &tm)) {
607 pix_height = tm.tmHeight;
609 g_snprintf(fontspec, fontspec_len, "-*-%s-*-*-*-*-%i-*-*-*-p-*-iso8859-1",
610 utf_16to8(name), pix_height);
614 void app_font_gtk1_init(GtkWidget *top_level_w)
619 style = gtk_widget_get_style(top_level_w);
620 if (get_windows_font_gtk1(winfont, sizeof(winfont)) == 0)
621 style->font = gdk_font_load(winfont);
622 if (style->font) gtk_widget_set_style(top_level_w, style);
626 #else /* GTK_MAJOR_VERSION */
627 static char appfontname[128] = "tahoma 8";
630 set_app_font_gtk2(const char *fontname)
632 GtkSettings *settings;
634 if (fontname != NULL && *fontname == 0) return;
636 settings = gtk_settings_get_default();
638 if (fontname == NULL) {
639 g_object_set(G_OBJECT(settings), "gtk-font-name", appfontname, NULL);
642 PangoFontDescription *pfd;
646 w = gtk_label_new(NULL);
647 pfd = pango_font_description_from_string(fontname);
648 pc = gtk_widget_get_pango_context(w);
649 pfont = pango_context_load_font(pc, pfd);
652 strcpy(appfontname, fontname);
653 g_object_set(G_OBJECT(settings), "gtk-font-name", appfontname, NULL);
656 gtk_widget_destroy(w);
657 pango_font_description_free(pfd);
661 static char *default_windows_menu_fontspec_gtk2(void)
663 gchar *fontspec = NULL;
664 NONCLIENTMETRICS ncm;
666 memset(&ncm, 0, sizeof ncm);
667 ncm.cbSize = sizeof ncm;
669 if (SystemParametersInfo(SPI_GETNONCLIENTMETRICS, ncm.cbSize, &ncm, 0)) {
670 HDC screen = GetDC(0);
671 double y_scale = 72.0 / GetDeviceCaps(screen, LOGPIXELSY);
672 int point_size = (int) (ncm.lfMenuFont.lfHeight * y_scale);
674 if (point_size < 0) point_size = -point_size;
675 fontspec = g_strdup_printf("%s %d", ncm.lfMenuFont.lfFaceName,
677 ReleaseDC(0, screen);
683 static void try_to_get_windows_font_gtk2(void)
687 fontspec = default_windows_menu_fontspec_gtk2();
689 if (fontspec != NULL) {
691 PangoFontDescription *pfd;
696 pfd = pango_font_description_from_string(fontspec);
698 w = gtk_label_new(NULL);
699 pc = gtk_widget_get_pango_context(w);
700 pfont = pango_context_load_font(pc, pfd);
701 match = (pfont != NULL);
703 pango_font_description_free(pfd);
704 g_object_unref(G_OBJECT(pc));
705 gtk_widget_destroy(w);
707 if (match) set_app_font_gtk2(fontspec);
711 #endif /* GTK_MAJOR_VERSION */
718 #if GTK_MAJOR_VERSION < 2
719 gchar *bold_font_name;
723 #if GTK_MAJOR_VERSION >= 2
724 /* try to load the application font for GTK2 */
725 try_to_get_windows_font_gtk2();
729 /* Try to load the regular and boldface fixed-width fonts */
730 #if GTK_MAJOR_VERSION < 2
731 bold_font_name = user_font_boldify(prefs.gui_font_name1);
732 m_r_font = gdk_font_load(prefs.gui_font_name1);
733 m_b_font = gdk_font_load(bold_font_name);
734 if (m_r_font == NULL || m_b_font == NULL) {
735 /* XXX - pop this up as a dialog box? no */
736 if (m_r_font == NULL) {
737 fprintf(stderr, "ethereal: Warning: font %s not found - defaulting to 6x13 and 6x13bold\n",
738 prefs.gui_font_name1);
740 gdk_font_unref(m_r_font);
742 if (m_b_font == NULL) {
743 fprintf(stderr, "ethereal: Warning: font %s not found - defaulting to 6x13 and 6x13bold\n",
746 gdk_font_unref(m_b_font);
748 g_free(bold_font_name);
749 if ((m_r_font = gdk_font_load("6x13")) == NULL) {
750 fprintf(stderr, "ethereal: Error: font 6x13 not found\n");
753 if ((m_b_font = gdk_font_load("6x13bold")) == NULL) {
754 fprintf(stderr, "ethereal: Error: font 6x13bold not found\n");
757 g_free(prefs.gui_font_name1);
758 prefs.gui_font_name1 = g_strdup("6x13");
760 #else /* GTK_MAJOR_VERSION */
761 m_r_font = pango_font_description_from_string(prefs.gui_font_name2);
762 m_b_font = pango_font_description_copy(m_r_font);
763 pango_font_description_set_weight(m_b_font, PANGO_WEIGHT_BOLD);
764 if (m_r_font == NULL || m_b_font == NULL) {
765 /* XXX - pop this up as a dialog box? no */
766 if (m_r_font == NULL) {
767 fprintf(stderr, "ethereal: Warning: font %s not found - defaulting to Monospace 9\n",
768 prefs.gui_font_name2);
770 pango_font_description_free(m_r_font);
772 if (m_b_font == NULL) {
773 fprintf(stderr, "ethereal: Warning: bold font %s not found - defaulting"
774 " to Monospace 9\n", prefs.gui_font_name2);
776 pango_font_description_free(m_b_font);
778 if ((m_r_font = pango_font_description_from_string("Monospace 9")) == NULL)
780 fprintf(stderr, "ethereal: Error: font Monospace 9 not found\n");
783 if ((m_b_font = pango_font_description_copy(m_r_font)) == NULL) {
784 fprintf(stderr, "ethereal: Error: font Monospace 9 bold not found\n");
787 g_free(prefs.gui_font_name2);
788 pango_font_description_set_weight(m_b_font, PANGO_WEIGHT_BOLD);
789 prefs.gui_font_name2 = g_strdup("Monospace 9");
791 #endif /* GTK_MAJOR_VERSION */
793 /* Call this for the side-effects that set_fonts() produces */
794 set_fonts(m_r_font, m_b_font);