aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/FontImp.hh52
-rw-r--r--src/XFontImp.cc70
-rw-r--r--src/XFontImp.hh41
-rw-r--r--src/XmbFontImp.cc225
-rw-r--r--src/XmbFontImp.hh43
5 files changed, 431 insertions, 0 deletions
diff --git a/src/FontImp.hh b/src/FontImp.hh
new file mode 100644
index 0000000..59042ad
--- /dev/null
+++ b/src/FontImp.hh
@@ -0,0 +1,52 @@
1// FontImp.cc for FbTk
2// Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org)
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22// $Id: FontImp.hh,v 1.1 2002/10/13 22:22:14 fluxgen Exp $
23
24#ifndef FBTK_FONTIMP_HH
25#define FBTK_FONTIMP_HH
26
27#include "Color.hh"
28
29#include <X11/Xlib.h>
30
31#include <string>
32
33namespace FbTk {
34
35/**
36 FontImp, second part of the bridge pattern for fonts
37 pure interface class.
38*/
39class FontImp {
40public:
41 virtual ~FontImp() { }
42 virtual bool load(const std::string &name) = 0;
43 virtual void drawText(Drawable w, int screen, GC gc, const char *text, size_t len, int x, int y) const = 0;
44 virtual unsigned int textWidth(const char * const text, unsigned int size) const = 0;
45 virtual unsigned int height() const = 0;
46protected:
47 FontImp() { }
48};
49
50}; // end namespace FbTk
51
52#endif // FBTK_FONTIMP_HH
diff --git a/src/XFontImp.cc b/src/XFontImp.cc
new file mode 100644
index 0000000..9a57cc7
--- /dev/null
+++ b/src/XFontImp.cc
@@ -0,0 +1,70 @@
1// XFontImp.cc for FbTk fluxbox toolkit
2// Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org)
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22// $Id: XFontImp.cc,v 1.1 2002/10/13 22:22:14 fluxgen Exp $
23
24#include "XFontImp.hh"
25#include "BaseDisplay.hh"
26
27#include <iostream>
28using namespace std;
29
30XFontImp::XFontImp(const char *fontname):m_fontstruct(0) {
31 if (fontname != 0)
32 load(fontname);
33}
34
35XFontImp::~XFontImp() {
36 if (m_fontstruct != 0)
37 XFreeFont(BaseDisplay::getXDisplay(), m_fontstruct);
38}
39
40bool XFontImp::load(const std::string &fontname) {
41 XFontStruct *font = XLoadQueryFont(BaseDisplay::getXDisplay(), fontname.c_str());
42 if (font == 0)
43 return false;
44 if (m_fontstruct != 0) // free old font struct, if any
45 XFreeFont(BaseDisplay::getXDisplay(), m_fontstruct);
46
47 m_fontstruct = font; //set new font
48 return true;
49}
50
51void XFontImp::drawText(Drawable w, int screen, GC gc, const char *text, size_t len, int x, int y) const {
52 if (m_fontstruct == 0)
53 return;
54 Display *disp = BaseDisplay::getXDisplay();
55 XSetFont(disp, gc, m_fontstruct->fid);
56 XDrawString(disp, w, gc, x, y, text, len);
57}
58
59unsigned int XFontImp::textWidth(const char * const text, unsigned int size) const {
60 if (m_fontstruct == 0)
61 return 0;
62 return XTextWidth(m_fontstruct, text, size);
63}
64
65unsigned int XFontImp::height() const {
66 if (m_fontstruct == 0)
67 return 0;
68
69 return m_fontstruct->ascent + m_fontstruct->descent;
70}
diff --git a/src/XFontImp.hh b/src/XFontImp.hh
new file mode 100644
index 0000000..d48bd89
--- /dev/null
+++ b/src/XFontImp.hh
@@ -0,0 +1,41 @@
1// XFontImp.hh for FbTk fluxbox toolkit
2// Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org)
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22// $Id: XFontImp.hh,v 1.1 2002/10/13 22:22:14 fluxgen Exp $
23
24#ifndef XFONTIMP_HH
25#define XFONTIMP_HH
26
27#include "FontImp.hh"
28
29class XFontImp:public FbTk::FontImp {
30public:
31 explicit XFontImp(const char *filename = 0);
32 ~XFontImp();
33 bool load(const std::string &filename);
34 unsigned int textWidth(const char * const text, unsigned int size) const;
35 unsigned int height() const;
36 void drawText(Drawable w, int screen, GC gc, const char *text, size_t len, int x, int y) const;
37private:
38 XFontStruct *m_fontstruct;
39};
40
41#endif // XFONTIMP_HH
diff --git a/src/XmbFontImp.cc b/src/XmbFontImp.cc
new file mode 100644
index 0000000..b39e463
--- /dev/null
+++ b/src/XmbFontImp.cc
@@ -0,0 +1,225 @@
1// XmbFontImp.cc for FbTk fluxbox toolkit
2// Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org)
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22// $Id: XmbFontImp.cc,v 1.1 2002/10/13 22:22:14 fluxgen Exp $
23
24#include "XmbFontImp.hh"
25
26#include "BaseDisplay.hh"
27#include "StringUtil.hh"
28
29#ifdef HAVE_CONFIG_H
30#include "config.h"
31#endif //HAVE_CONFIG_H
32
33#ifdef HAVE_SETLOCALE
34#include <locale.h>
35#endif // HAVE_SETLOCALE
36
37#include <cstdarg>
38
39namespace {
40
41const char *getFontSize(const char *pattern, int *size) {
42 const char *p;
43 const char *p2=0;
44 int n=0;
45
46 for (p=pattern; 1; p++) {
47 if (!*p) {
48 if (p2!=0 && n>1 && n<72) {
49 *size = n; return p2+1;
50 } else {
51 *size = 16; return 0;
52 }
53 } else if (*p=='-') {
54 if (n>1 && n<72 && p2!=0) {
55 *size = n;
56 return p2+1;
57 }
58 p2=p; n=0;
59 } else if (*p>='0' && *p<='9' && p2!=0) {
60 n *= 10;
61 n += *p-'0';
62 } else {
63 p2=0; n=0;
64 }
65 }
66}
67
68const char *getFontElement(const char *pattern, char *buf, int bufsiz, ...) {
69 const char *p, *v;
70 char *p2;
71 va_list va;
72
73 va_start(va, bufsiz);
74 buf[bufsiz-1] = 0;
75 buf[bufsiz-2] = '*';
76 while((v = va_arg(va, char *)) != 0) {
77 p = StringUtil::strcasestr(pattern, v);
78 if (p) {
79 std::strncpy(buf, p+1, bufsiz-2);
80 p2 = strchr(buf, '-');
81 if (p2) *p2=0;
82 va_end(va);
83 return p;
84 }
85 }
86 va_end(va);
87 std::strncpy(buf, "*", bufsiz);
88 return 0;
89}
90
91XFontSet createFontSet(const char *fontname) {
92 Display *display = BaseDisplay::getXDisplay();
93 XFontSet fs;
94 const int FONT_ELEMENT_SIZE=50;
95 char **missing, *def = "-";
96 int nmissing, pixel_size = 0, buf_size = 0;
97 char weight[FONT_ELEMENT_SIZE], slant[FONT_ELEMENT_SIZE];
98
99 fs = XCreateFontSet(display,
100 fontname, &missing, &nmissing, &def);
101 if (fs && (! nmissing)) return fs;
102
103#ifdef HAVE_SETLOCALE
104 if (! fs) {
105 if (nmissing) XFreeStringList(missing);
106
107 setlocale(LC_CTYPE, "C");
108 fs = XCreateFontSet(display, fontname,
109 &missing, &nmissing, &def);
110 setlocale(LC_CTYPE, "");
111 }
112#endif // HAVE_SETLOCALE
113
114 if (fs) {
115 XFontStruct **fontstructs;
116 char **fontnames;
117 XFontsOfFontSet(fs, &fontstructs, &fontnames);
118 fontname = fontnames[0];
119 }
120
121 getFontElement(fontname, weight, FONT_ELEMENT_SIZE,
122 "-medium-", "-bold-", "-demibold-", "-regular-", 0);
123 getFontElement(fontname, slant, FONT_ELEMENT_SIZE,
124 "-r-", "-i-", "-o-", "-ri-", "-ro-", 0);
125 getFontSize(fontname, &pixel_size);
126
127 if (! strcmp(weight, "*"))
128 std::strncpy(weight, "medium", FONT_ELEMENT_SIZE);
129 if (! strcmp(slant, "*"))
130 std::strncpy(slant, "r", FONT_ELEMENT_SIZE);
131 if (pixel_size < 3)
132 pixel_size = 3;
133 else if (pixel_size > 97)
134 pixel_size = 97;
135
136 buf_size = strlen(fontname) + (FONT_ELEMENT_SIZE * 2) + 64;
137 char *pattern2 = new char[buf_size];
138 snprintf(pattern2, buf_size - 1,
139 "%s,"
140 "-*-*-%s-%s-*-*-%d-*-*-*-*-*-*-*,"
141 "-*-*-*-*-*-*-%d-*-*-*-*-*-*-*,*",
142 fontname, weight, slant, pixel_size, pixel_size);
143 fontname = pattern2;
144
145 if (nmissing)
146 XFreeStringList(missing);
147 if (fs)
148 XFreeFontSet(display, fs);
149
150 fs = XCreateFontSet(display, fontname,
151 &missing, &nmissing, &def);
152 delete [] pattern2;
153
154 return fs;
155}
156
157};
158
159XmbFontImp::XmbFontImp(const char *filename, bool utf8):m_fontset(0), m_utf8mode(utf8) {
160 if (filename != 0)
161 load(filename);
162}
163
164XmbFontImp::~XmbFontImp() {
165 if (m_fontset != 0)
166 XFreeFontSet(BaseDisplay::getXDisplay(), m_fontset);
167}
168
169bool XmbFontImp::load(const std::string &fontname) {
170 if (fontname.size() == 0)
171 return false;
172 XFontSet set = createFontSet(fontname.c_str());
173 if (set == 0)
174 return false;
175 if (m_fontset != 0)
176 XFreeFontSet(BaseDisplay::getXDisplay(), m_fontset);
177 m_fontset = set;
178 m_setextents = XExtentsOfFontSet(m_fontset);
179
180 return true;
181}
182
183void XmbFontImp::drawText(Drawable w, int screen, GC gc, const char *text,
184 size_t len, int x, int y) const {
185
186 if (text == 0 || len == 0 || w == 0 || m_fontset == 0)
187 return;
188#ifdef X_HAVE_UTF8_STRING
189 if (m_utf8mode) {
190 Xutf8DrawString(BaseDisplay::getXDisplay(), w, m_fontset,
191 gc, x, y,
192 text, len);
193 } else
194#endif //X_HAVE_UTF8_STRING
195 {
196 XmbDrawString(BaseDisplay::getXDisplay(), w, m_fontset,
197 gc, x, y,
198 text, len);
199 }
200}
201
202unsigned int XmbFontImp::textWidth(const char *text, unsigned int len) const {
203 if (m_fontset == 0)
204 return 0;
205 XRectangle ink, logical;
206#ifdef X_HAVE_UTF8_STRING
207 if (m_utf8mode) {
208 Xutf8TextExtents(m_fontset, text, len,
209 &ink, &logical);
210 } else
211#endif // X_HAVE_UTF8_STRING
212 {
213 XmbTextExtents(m_fontset, text, len,
214 &ink, &logical);
215 }
216
217 return logical.width;
218}
219
220unsigned int XmbFontImp::height() const {
221 if (m_fontset == 0)
222 return 0;
223 return m_setextents->max_ink_extent.height;
224}
225
diff --git a/src/XmbFontImp.hh b/src/XmbFontImp.hh
new file mode 100644
index 0000000..d941af6
--- /dev/null
+++ b/src/XmbFontImp.hh
@@ -0,0 +1,43 @@
1// XmbFontImp.hh for FbTk fluxbox toolkit
2// Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org)
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22// $Id: XmbFontImp.hh,v 1.1 2002/10/13 22:22:14 fluxgen Exp $
23
24#ifndef XMBFONTIMP_HH
25#define XMBFONTIMP_HH
26
27#include "FontImp.hh"
28
29class XmbFontImp:public FbTk::FontImp {
30public:
31 XmbFontImp(const char *fontname, bool utf8);
32 ~XmbFontImp();
33 bool load(const std::string &name);
34 virtual void drawText(Drawable w, int screen, GC gc, const char *text, size_t len, int x, int y) const;
35 unsigned int textWidth(const char *text, unsigned int len) const;
36 unsigned int height() const;
37private:
38 XFontSet m_fontset;
39 XFontSetExtents *m_setextents;
40 const bool m_utf8mode;
41};
42
43#endif // XMBFONTIMP_HH