aboutsummaryrefslogtreecommitdiffstats
path: root/ui/qt/monospace_font.cpp
blob: acecc6382c778914f14e311ff194dfe6f8710539 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* monospace_font.cpp
 *
 * $Id$
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "monospace_font.h"

#include "wireshark_application.h"

#include <QFontMetrics>
#include <QString>
#include <QStringList>
#include <QDebug>

//
// XXX We should probably move this to wsApp.
//

//MonospaceFont::MonospaceFont(QObject *parent) :
//    QFont(parent)
//{
//}

QFont m_r_font, m_b_font;

//void MonospaceFont::propagate() {
//    emit(monospaceFontChanged(self));
//}

// http://en.wikipedia.org/wiki/Category:Monospaced_typefaces
#define WIN_DEF_FONT "Consolas"
#define WIN_ALT_FONTS "Lucida Console"
#define OSX_DEF_FONT "Menlo"
#define OSX_ALT_FONTS "Monaco"
#define X11_DEF_FONT "Bitstream Vera Sans Mono"
#define X11_ALT_FONTS "Liberation Mono" << "DejaVu Sans Mono"
#define FALLBACK_FONTS "Lucida Sans Typewriter" << "Inconsolata" << "Droid Sans Mono" << "Andale Mono" << "Courier New" << "monospace"

void
font_init(void) {
    QStringList substitutes;

    // Try to pick the latest, shiniest fixed-width font for our OS.
#if defined(Q_WS_WIN)
#define DEF_FONT WIN_DEF_FONT
#define FONT_SIZE_ADJUST 2
    substitutes = QStringList() << WIN_ALT_FONTS << OSX_DEF_FONT << OSX_ALT_FONTS << X11_DEF_FONT << X11_ALT_FONTS << FALLBACK_FONTS;
#elif defined(Q_WS_MAC)
#define DEF_FONT OSX_DEF_FONT
#define FONT_SIZE_ADJUST 0
    substitutes = QStringList() << OSX_ALT_FONTS << WIN_DEF_FONT << WIN_ALT_FONTS << X11_DEF_FONT << X11_ALT_FONTS << FALLBACK_FONTS;
#else
#define DEF_FONT X11_DEF_FONT
#define FONT_SIZE_ADJUST 0
    substitutes = QStringList() << X11_ALT_FONTS << WIN_DEF_FONT << WIN_ALT_FONTS << OSX_DEF_FONT << OSX_ALT_FONTS << FALLBACK_FONTS;
#endif

    m_r_font.setFamily(DEF_FONT);
    m_r_font.insertSubstitutions(DEF_FONT, substitutes);
    m_r_font.setPointSize(wsApp->font().pointSize() + FONT_SIZE_ADJUST);
#if QT_VERSION >= 0x040700
     m_b_font.setStyleHint(QFont::Monospace);
 #else
     m_b_font.setStyleHint(QFont::TypeWriter);
 #endif

    m_b_font.setFamily(DEF_FONT);
    m_b_font.insertSubstitutions(DEF_FONT, substitutes);
    m_b_font.setPointSize(wsApp->font().pointSize() + FONT_SIZE_ADJUST);
#if QT_VERSION >= 0x040700
     m_b_font.setStyleHint(QFont::Monospace);
 #else
     m_b_font.setStyleHint(QFont::TypeWriter);
 #endif
    m_b_font.setWeight(QFont::Bold);
}

fa_ret_t
user_font_apply(void) {
//    char *gui_font_name;
//    PangoFontDescription *new_r_font, *new_b_font;
//    PangoFontDescription *old_r_font = NULL, *old_b_font = NULL;

    /* convert font name to reflect the zoom level */
//    gui_font_name = font_zoom(prefs.gui_font_name);
//    if (gui_font_name == NULL) {
//        /*
//         * This means the font name isn't an XLFD font name.
//         * We just report that for now as a font not available in
//         * multiple sizes.
//         */
//        simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
//            "Your current font isn't available in any other sizes.\n");
//        return FA_FONT_NOT_RESIZEABLE;
//    }

//    /* load normal and bold font */
//    new_r_font = pango_font_description_from_string(gui_font_name);
//    new_b_font = pango_font_description_copy(new_r_font);
//    pango_font_description_set_weight(new_b_font, PANGO_WEIGHT_BOLD);

//    if (new_r_font == NULL || new_b_font == NULL) {
//        /* We're no longer using the new fonts; unreference them. */
//        if (new_r_font != NULL)
//            pango_font_description_free(new_r_font);
//        if (new_b_font != NULL)
//            pango_font_description_free(new_b_font);
//        g_free(gui_font_name);

//        /* We let our caller pop up a dialog box, as the error message
//           depends on the context (did they zoom in or out, or did they
//           do something else? */
//        return FA_FONT_NOT_AVAILABLE;
//    }

//    /* the font(s) seem to be ok */
//    new_packet_list_set_font(new_r_font);
//    set_ptree_font_all(new_r_font);
//    old_r_font = m_r_font;
//    old_b_font = m_b_font;
//    set_fonts(new_r_font, new_b_font);

//    /* Redraw the packet bytes windows. */
//    redraw_packet_bytes_all();

//    /* Redraw the "Follow TCP Stream" windows. */
//    follow_tcp_redraw_all();

//    /* We're no longer using the old fonts; unreference them. */
//    if (old_r_font != NULL)
//        pango_font_description_free(old_r_font);
//    if (old_b_font != NULL)
//        pango_font_description_free(old_b_font);
//    g_free(gui_font_name);

    return FA_SUCCESS;
}

// XXX - We might want to use a signal/slot for this (or just us a global variable).
QFont get_monospace_font(void) {
    return m_r_font;
}

int get_monospace_text_size(const char *str, bool regular) {
    QFontMetrics *fm;

    if (regular)
        fm = new QFontMetrics(m_r_font);
    else
        fm = new QFontMetrics(m_b_font);

    return fm->width(str);
}