diff options
Diffstat (limited to 'src/FbTk/Font.cc')
-rw-r--r-- | src/FbTk/Font.cc | 206 |
1 files changed, 206 insertions, 0 deletions
diff --git a/src/FbTk/Font.cc b/src/FbTk/Font.cc new file mode 100644 index 0000000..f1ca707 --- /dev/null +++ b/src/FbTk/Font.cc | |||
@@ -0,0 +1,206 @@ | |||
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.4 2003/01/05 23:00:19 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 | #include <typeinfo> | ||
58 | using namespace std; | ||
59 | |||
60 | #ifdef HAVE_SETLOCALE | ||
61 | #include <locale.h> | ||
62 | #endif //HAVE_SETLOCALE | ||
63 | |||
64 | namespace FbTk { | ||
65 | |||
66 | bool Font::m_multibyte = false; | ||
67 | bool Font::m_utf8mode = false; | ||
68 | |||
69 | Font::Font(const char *name, bool antialias): | ||
70 | m_fontimp(0), | ||
71 | m_antialias(false), m_rotated(false) { | ||
72 | |||
73 | // MB_CUR_MAX returns the size of a char in the current locale | ||
74 | if (MB_CUR_MAX > 1) // more than one byte, then we're multibyte | ||
75 | m_multibyte = true; | ||
76 | |||
77 | char *s; // temporary string for enviroment variable | ||
78 | // check for utf-8 mode | ||
79 | if (((s = getenv("LC_ALL")) && *s) || | ||
80 | ((s = getenv("LC_CTYPE")) && *s) || | ||
81 | ((s = getenv("LANG")) && *s)) { | ||
82 | if (strstr(s, "UTF-8")) | ||
83 | m_utf8mode = true; | ||
84 | } | ||
85 | |||
86 | // create the right font implementation | ||
87 | // antialias is prio 1 | ||
88 | #ifdef USE_XFT | ||
89 | if (antialias) { | ||
90 | m_fontimp.reset(new XftFontImp(0, m_utf8mode)); | ||
91 | m_antialias = true; | ||
92 | } | ||
93 | #endif //USE_XFT | ||
94 | // if we didn't create a Xft font then create basic font | ||
95 | if (m_fontimp.get() == 0) { | ||
96 | #ifdef USE_XMB | ||
97 | if (m_multibyte || m_utf8mode) | ||
98 | m_fontimp.reset(new XmbFontImp(0, m_utf8mode)); | ||
99 | else // basic font implementation | ||
100 | #endif // USE_XMB | ||
101 | m_fontimp.reset(new XFontImp()); | ||
102 | } | ||
103 | |||
104 | if (name != 0) { | ||
105 | load(name); | ||
106 | } | ||
107 | |||
108 | } | ||
109 | |||
110 | Font::~Font() { | ||
111 | |||
112 | } | ||
113 | |||
114 | void Font::setAntialias(bool flag) { | ||
115 | bool loaded = m_fontimp->loaded(); | ||
116 | #ifdef USE_XFT | ||
117 | if (flag && !isAntialias() && !m_rotated) { | ||
118 | m_fontimp.reset(new XftFontImp(m_fontstr.c_str(), m_utf8mode)); | ||
119 | } else if (!flag && isAntialias()) | ||
120 | #endif // USE_XFT | ||
121 | { | ||
122 | #ifdef USE_XMB | ||
123 | if (m_multibyte || m_utf8mode) | ||
124 | m_fontimp.reset(new XmbFontImp(m_fontstr.c_str(), m_utf8mode)); | ||
125 | else | ||
126 | #endif // USE_XMB | ||
127 | m_fontimp.reset(new XFontImp(m_fontstr.c_str())); | ||
128 | } | ||
129 | |||
130 | if (m_fontimp->loaded() != loaded) { // if the new font failed to load, fall back to 'fixed' | ||
131 | if (!m_fontimp->load("fixed")) // if that failes too, output warning | ||
132 | cerr<<"Warning: can't load fallback font 'fixed'."<<endl; | ||
133 | } | ||
134 | |||
135 | m_antialias = flag; | ||
136 | } | ||
137 | |||
138 | bool Font::load(const char *name) { | ||
139 | if (name == 0) | ||
140 | return false; | ||
141 | m_fontstr = name; | ||
142 | return m_fontimp->load(name); | ||
143 | } | ||
144 | |||
145 | unsigned int Font::textWidth(const char * const text, unsigned int size) const { | ||
146 | return m_fontimp->textWidth(text, size); | ||
147 | } | ||
148 | |||
149 | unsigned int Font::height() const { | ||
150 | return m_fontimp->height(); | ||
151 | } | ||
152 | |||
153 | int Font::ascent() const { | ||
154 | return m_fontimp->ascent(); | ||
155 | } | ||
156 | |||
157 | int Font::descent() const { | ||
158 | return m_fontimp->descent(); | ||
159 | } | ||
160 | void Font::drawText(Drawable w, int screen, GC gc, const char *text, size_t len, int x, int y, | ||
161 | bool rotate) const { | ||
162 | if (text == 0 || len == 0) | ||
163 | return; | ||
164 | if (!rotate && isRotated()) { | ||
165 | // if this was called with request to not rotated the text | ||
166 | // we just forward it to the implementation that handles rotation | ||
167 | // currently just XFontImp | ||
168 | // Using dynamic_cast just temporarly until there's a better solution | ||
169 | // to put in FontImp | ||
170 | try { | ||
171 | XFontImp *font = dynamic_cast<XFontImp *>(m_fontimp.get()); | ||
172 | font->setRotate(false); // disable rotation temporarly | ||
173 | font->drawText(w, screen, gc, text, len, x, y); | ||
174 | font->setRotate(true); // enable rotation | ||
175 | } catch (std::bad_cast &bc) { | ||
176 | // draw normal... | ||
177 | m_fontimp->drawText(w, screen, gc, text, len, x, y); | ||
178 | } | ||
179 | |||
180 | } else | ||
181 | m_fontimp->drawText(w, screen, gc, text, len, x, y); | ||
182 | } | ||
183 | |||
184 | void Font::rotate(float angle) { | ||
185 | #ifdef USE_XFT | ||
186 | // if we are rotated and we are changing to horiz text | ||
187 | // and we were antialiased before we rotated then change to XftFontImp | ||
188 | if (isRotated() && angle == 0 && isAntialias()) | ||
189 | m_fontimp.reset(new XftFontImp(m_fontstr.c_str(), m_utf8mode)); | ||
190 | #endif // USE_XFT | ||
191 | // change to a font imp that handles rotated fonts (i.e just XFontImp at the moment) | ||
192 | // if we're going to rotate this font | ||
193 | if (angle != 0 && isAntialias() && !isRotated()) { | ||
194 | m_fontimp.reset(new XFontImp(m_fontstr.c_str())); | ||
195 | if (!m_fontimp->loaded()) // if it failed to load font, try default font fixed | ||
196 | m_fontimp->load("fixed"); | ||
197 | } | ||
198 | |||
199 | //Note: only XFontImp implements FontImp::rotate | ||
200 | m_fontimp->rotate(angle); | ||
201 | |||
202 | m_rotated = (angle == 0 ? false : true); | ||
203 | m_angle = angle; | ||
204 | } | ||
205 | |||
206 | }; | ||