aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/XmbFontImp.cc
blob: 557708af415a1f50d4853e7e4c13bc3b4dd89890 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// XmbFontImp.cc for FbTk fluxbox toolkit
// Copyright (c) 2002 - 2006 Henrik Kinnunen (fluxgen at fluxbox dot org)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

#include "XmbFontImp.hh"

#include "App.hh"
#include "TextUtils.hh"
#include "StringUtil.hh"
#include "FbPixmap.hh"
#include "GContext.hh"

#ifdef HAVE_SETLOCALE
#include <locale.h>
#endif // HAVE_SETLOCALE

#ifdef HAVE_CSTDIO
  #include <cstdio>
#else
  #include <stdio.h>
#endif
#ifdef HAVE_CSTDARG
  #include <cstdarg>
#else
  #include <stdarg.h>
#endif
#ifdef HAVE_CSTRING
  #include <cstring>
#else
  #include <string.h>
#endif

using std::string;

namespace {

XFontSet createFontSet(const char *fontname, bool& utf8mode) {
    Display *display = FbTk::App::instance()->display();
    XFontSet fs;
    char **missing;
    const char *constdef = "-";
    char *def = const_cast<char *>(constdef);
    int nmissing;
    string orig_locale = "";

#ifdef HAVE_SETLOCALE
    if (utf8mode) {
        orig_locale = setlocale(LC_CTYPE, NULL);
        if (setlocale(LC_CTYPE, "UTF-8") == NULL) {
            utf8mode = false;
        }
    }
#endif // HAVE_SETLOCALE
    fs = XCreateFontSet(display,
                        fontname, &missing, &nmissing, &def);

    if (fs && (! nmissing)) {
#ifdef HAVE_SETLOCALE
        if (utf8mode)
            setlocale(LC_CTYPE, orig_locale.c_str());
#endif // HAVE_SETLOCALE
        return fs;
    }

#ifdef HAVE_SETLOCALE
    if (! fs) {
        if (nmissing)
            XFreeStringList(missing);

        setlocale(LC_CTYPE, "C");
        fs = XCreateFontSet(display, fontname,
                            &missing, &nmissing, &def);
        setlocale(LC_CTYPE, orig_locale.c_str());
        return fs;
    }
    if (utf8mode)
        setlocale(LC_CTYPE, orig_locale.c_str());
#endif // HAVE_SETLOCALE

    // set to false because our strings won't be utf8-happy
    utf8mode = false;

    return fs;
}

}

namespace FbTk {

XmbFontImp::XmbFontImp(const char *filename, bool utf8) : m_fontset(0), m_setextents(0), m_utf8mode(utf8) {
    if (filename != 0)
        load(filename);
}

XmbFontImp::~XmbFontImp() {
    if (m_fontset != 0)
        XFreeFontSet(App::instance()->display(), m_fontset);
}

bool XmbFontImp::load(const string &fontname) {
    if (fontname.empty())
        return false;

    XFontSet set = createFontSet(fontname.c_str(), m_utf8mode);
    if (set == 0)
        return false;

    if (m_fontset != 0)
        XFreeFontSet(App::instance()->display(), m_fontset);

    m_fontset = set;
    m_setextents = XExtentsOfFontSet(m_fontset);

    return true;
}

void XmbFontImp::drawText(const FbDrawable &d, int screen, GC main_gc, const char* text,
                          size_t len, int x, int y, FbTk::Orientation orient) {

    if (!text || !*text || m_fontset == 0)
        return;

    if (orient == ROT0) {
#ifdef X_HAVE_UTF8_STRING
        if (m_utf8mode) {
            Xutf8DrawString(d.display(), d.drawable(), m_fontset,
                            main_gc, x, y,
                            text, len);
        } else
#endif //X_HAVE_UTF8_STRING
            {
                std::string localestr = FbStringUtil::FbStrToLocale(FbString(text, len));
                XmbDrawString(d.display(), d.drawable(), m_fontset,
                              main_gc, x, y,
                              localestr.data(), localestr.size());
            }
        return;
    }

    Display *dpy = App::instance()->display();

    int xpos = x, ypos = y;
    unsigned int w = d.width();
    unsigned int h = d.height();

    translateSize(orient, w, h);
    untranslateCoords(orient, xpos, ypos, w, h);

    // not straight forward, we actually draw it elsewhere, then rotate it
    FbTk::FbPixmap canvas(d.drawable(), w, h, 1);

    // create graphic context for our canvas
    FbTk::GContext font_gc(canvas);
    font_gc.setBackground(None);
    font_gc.setForeground(None);

    XFillRectangle(dpy, canvas.drawable(), font_gc.gc(), 0, 0, canvas.width(), canvas.height());
    font_gc.setForeground(1);


#ifdef X_HAVE_UTF8_STRING
    if (m_utf8mode) {
        Xutf8DrawString(dpy, canvas.drawable(), m_fontset,
                             font_gc.gc(), xpos, ypos,
                             text, len);
    } else
#endif //X_HAVE_UTF8_STRING
    {
        std::string localestr = FbStringUtil::FbStrToLocale(FbString(text, len));
        XmbDrawString(dpy, canvas.drawable(), m_fontset,
                           font_gc.gc(), xpos, ypos,
                           localestr.data(), localestr.size());
    }

    canvas.rotate(orient);

    GC my_gc = XCreateGC(dpy, d.drawable(), 0, 0);

    XCopyGC(dpy, main_gc, GCForeground|GCBackground, my_gc);

    // vertical or upside down
    XSetFillStyle(dpy, my_gc, FillStippled);
    XSetStipple(dpy, my_gc, canvas.drawable());
    XSetTSOrigin(dpy, my_gc, 0, 0);

    XFillRectangle(dpy, d.drawable(), my_gc, 0, 0,
                   canvas.width(),
                   canvas.height());

    XFreeGC(dpy, my_gc);

}

unsigned int XmbFontImp::textWidth(const char* text, unsigned int len) const {

    if (m_fontset == 0)
        return 0;

    XRectangle ink, logical;
#ifdef X_HAVE_UTF8_STRING
    if (m_utf8mode) {
        Xutf8TextExtents(m_fontset, text, len, &ink, &logical);
        if (logical.width != 0)
            return logical.width;
    }
#endif // X_HAVE_UTF8_STRING

    std::string localestr = FbStringUtil::FbStrToLocale(FbString(text, len));
    XmbTextExtents(m_fontset, localestr.data(), localestr.size(),
                   &ink, &logical);
    return logical.width;
}

unsigned int XmbFontImp::height() const {
    if (m_fontset == 0)
        return 0;
    return m_setextents->max_ink_extent.height;
}

} // end namespace FbTk