diff options
Diffstat (limited to 'src/FbTk/Font.cc')
-rw-r--r-- | src/FbTk/Font.cc | 184 |
1 files changed, 184 insertions, 0 deletions
diff --git a/src/FbTk/Font.cc b/src/FbTk/Font.cc new file mode 100644 index 0000000..949890e --- /dev/null +++ b/src/FbTk/Font.cc | |||
@@ -0,0 +1,184 @@ | |||
1 | // Font.cc | ||
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: Font.cc,v 1.1 2002/11/26 16:01:27 fluxgen Exp $ | ||
23 | |||
24 | |||
25 | #include "Font.hh" | ||
26 | #include "FontImp.hh" | ||
27 | |||
28 | #ifdef HAVE_CONFIG_H | ||
29 | #include "config.h" | ||
30 | #endif // HAVE_CONFIG_H | ||
31 | |||
32 | // for antialias | ||
33 | #ifdef USE_XFT | ||
34 | #include "XftFontImp.hh" | ||
35 | #endif // USE_XFT | ||
36 | |||
37 | // for multibyte support | ||
38 | #ifdef USE_XMB | ||
39 | #include "XmbFontImp.hh" | ||
40 | #endif //USE_XMB | ||
41 | |||
42 | // standard font system | ||
43 | #include "XFontImp.hh" | ||
44 | |||
45 | //use gnu extensions | ||
46 | #ifndef _GNU_SOURCE | ||
47 | #define _GNU_SOURCE | ||
48 | #endif //_GNU_SOURCE | ||
49 | |||
50 | #ifndef __USE_GNU | ||
51 | #define __USE_GNU | ||
52 | #endif //__USE_GNU | ||
53 | |||
54 | #include <iostream> | ||
55 | #include <cstring> | ||
56 | #include <cstdlib> | ||
57 | using namespace std; | ||
58 | |||
59 | #ifdef HAVE_SETLOCALE | ||
60 | #include <locale.h> | ||
61 | #endif //HAVE_SETLOCALE | ||
62 | |||
63 | namespace FbTk { | ||
64 | |||
65 | bool Font::m_multibyte = false; | ||
66 | bool Font::m_utf8mode = false; | ||
67 | |||
68 | Font::Font(const char *name, bool antialias): | ||
69 | m_fontimp(0), | ||
70 | m_antialias(false), m_rotated(false) { | ||
71 | |||
72 | // MB_CUR_MAX returns the size of a char in the current locale | ||
73 | if (MB_CUR_MAX > 1) // more than one byte, then we're multibyte | ||
74 | m_multibyte = true; | ||
75 | |||
76 | char *s; // temporary string for enviroment variable | ||
77 | // check for utf-8 mode | ||
78 | if (((s = getenv("LC_ALL")) && *s) || | ||
79 | ((s = getenv("LC_CTYPE")) && *s) || | ||
80 | ((s = getenv("LANG")) && *s)) { | ||
81 | if (strstr(s, "UTF-8")) | ||
82 | m_utf8mode = true; | ||
83 | } | ||
84 | |||
85 | // create the right font implementation | ||
86 | // antialias is prio 1 | ||
87 | #ifdef USE_XFT | ||
88 | if (antialias) { | ||
89 | m_fontimp.reset(new XftFontImp(0, m_utf8mode)); | ||
90 | m_antialias = true; | ||
91 | } | ||
92 | #endif //USE_XFT | ||
93 | // if we didn't create a Xft font then create basic font | ||
94 | if (m_fontimp.get() == 0) { | ||
95 | #ifdef USE_XMB | ||
96 | if (m_multibyte || m_utf8mode) | ||
97 | m_fontimp.reset(new XmbFontImp(0, m_utf8mode)); | ||
98 | else // basic font implementation | ||
99 | #endif // USE_XMB | ||
100 | m_fontimp.reset(new XFontImp()); | ||
101 | } | ||
102 | |||
103 | if (name != 0) { | ||
104 | load(name); | ||
105 | } | ||
106 | |||
107 | } | ||
108 | |||
109 | Font::~Font() { | ||
110 | |||
111 | } | ||
112 | |||
113 | void Font::setAntialias(bool flag) { | ||
114 | bool loaded = m_fontimp->loaded(); | ||
115 | #ifdef USE_XFT | ||
116 | if (flag && !isAntialias() && !m_rotated) { | ||
117 | m_fontimp.reset(new XftFontImp(m_fontstr.c_str(), m_utf8mode)); | ||
118 | } else if (!flag && isAntialias()) | ||
119 | #endif // USE_XFT | ||
120 | { | ||
121 | #ifdef USE_XMB | ||
122 | if (m_multibyte || m_utf8mode) | ||
123 | m_fontimp.reset(new XmbFontImp(m_fontstr.c_str(), m_utf8mode)); | ||
124 | else | ||
125 | #endif // USE_XMB | ||
126 | m_fontimp.reset(new XFontImp(m_fontstr.c_str())); | ||
127 | } | ||
128 | |||
129 | if (m_fontimp->loaded() != loaded) { // if the new font failed to load, fall back to 'fixed' | ||
130 | if (!m_fontimp->load("fixed")) // if that failes too, output warning | ||
131 | cerr<<"Warning: can't load fallback font 'fixed'."<<endl; | ||
132 | } | ||
133 | |||
134 | m_antialias = flag; | ||
135 | } | ||
136 | |||
137 | bool Font::load(const char *name) { | ||
138 | if (name == 0) | ||
139 | return false; | ||
140 | m_fontstr = name; | ||
141 | return m_fontimp->load(name); | ||
142 | } | ||
143 | |||
144 | unsigned int Font::textWidth(const char * const text, unsigned int size) const { | ||
145 | return m_fontimp->textWidth(text, size); | ||
146 | } | ||
147 | |||
148 | unsigned int Font::height() const { | ||
149 | return m_fontimp->height(); | ||
150 | } | ||
151 | |||
152 | int Font::ascent() const { | ||
153 | return m_fontimp->ascent(); | ||
154 | } | ||
155 | |||
156 | int Font::descent() const { | ||
157 | return m_fontimp->descent(); | ||
158 | } | ||
159 | void Font::drawText(Drawable w, int screen, GC gc, const char *text, size_t len, int x, int y) const { | ||
160 | if (text == 0 || len == 0) | ||
161 | return; | ||
162 | m_fontimp->drawText(w, screen, gc, text, len, x, y); | ||
163 | } | ||
164 | |||
165 | void Font::rotate(float angle) { | ||
166 | #ifdef USE_XFT | ||
167 | // if we are rotated and we are changing to horiz text | ||
168 | // and we were antialiased before we rotated then change to XftFontImp | ||
169 | if (isRotated() && angle == 0 && isAntialias()) | ||
170 | m_fontimp.reset(new XftFontImp(m_fontstr.c_str(), m_utf8mode)); | ||
171 | #endif // USE_XFT | ||
172 | // change to a font imp that handles rotated fonts (i.e just XFontImp at the moment) | ||
173 | // if we're going to rotate this font | ||
174 | if (angle != 0 && isAntialias() && !isRotated()) { | ||
175 | m_fontimp.reset(new XFontImp(m_fontstr.c_str())); | ||
176 | } | ||
177 | |||
178 | //Note: only XFontImp implements FontImp::rotate | ||
179 | m_fontimp->rotate(angle); | ||
180 | |||
181 | m_rotated = (angle == 0 ? false : true); | ||
182 | } | ||
183 | |||
184 | }; | ||