summaryrefslogtreecommitdiff
path: root/src/FbTk/XmbFontImp.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/FbTk/XmbFontImp.cc')
-rw-r--r--src/FbTk/XmbFontImp.cc300
1 files changed, 150 insertions, 150 deletions
diff --git a/src/FbTk/XmbFontImp.cc b/src/FbTk/XmbFontImp.cc
index c9a87d0..215bc53 100644
--- a/src/FbTk/XmbFontImp.cc
+++ b/src/FbTk/XmbFontImp.cc
@@ -19,7 +19,7 @@
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE. 20// DEALINGS IN THE SOFTWARE.
21 21
22// $Id: XmbFontImp.cc,v 1.2 2002/11/26 20:04:15 fluxgen Exp $ 22// $Id: XmbFontImp.cc,v 1.3 2002/12/01 13:42:15 rathnor Exp $
23 23
24#include "XmbFontImp.hh" 24#include "XmbFontImp.hh"
25 25
@@ -50,131 +50,131 @@ namespace {
50// ignoring the case of the characters 50// ignoring the case of the characters
51// Returns 0 on success else pointer to str. 51// Returns 0 on success else pointer to str.
52const char *strcasestr(const char *str, const char *ptn) { 52const char *strcasestr(const char *str, const char *ptn) {
53 const char *s2, *p2; 53 const char *s2, *p2;
54 for( ; *str; str++) { 54 for( ; *str; str++) {
55 for(s2=str, p2=ptn; ; s2++,p2++) { 55 for(s2=str, p2=ptn; ; s2++,p2++) {
56 if (!*p2) return str; // check if we reached the end of ptn, if so, return str 56 if (!*p2) return str; // check if we reached the end of ptn, if so, return str
57 if (toupper(*s2) != toupper(*p2)) break; // check if the chars match(ignoring case) 57 if (toupper(*s2) != toupper(*p2)) break; // check if the chars match(ignoring case)
58 } 58 }
59 } 59 }
60 return 0; 60 return 0;
61} 61}
62#endif //HAVE_STRCASESTR 62#endif //HAVE_STRCASESTR
63 63
64const char *getFontSize(const char *pattern, int *size) { 64const char *getFontSize(const char *pattern, int *size) {
65 const char *p; 65 const char *p;
66 const char *p2=0; 66 const char *p2=0;
67 int n=0; 67 int n=0;
68 68
69 for (p=pattern; 1; p++) { 69 for (p=pattern; 1; p++) {
70 if (!*p) { 70 if (!*p) {
71 if (p2!=0 && n>1 && n<72) { 71 if (p2!=0 && n>1 && n<72) {
72 *size = n; return p2+1; 72 *size = n; return p2+1;
73 } else { 73 } else {
74 *size = 16; return 0; 74 *size = 16; return 0;
75 } 75 }
76 } else if (*p=='-') { 76 } else if (*p=='-') {
77 if (n>1 && n<72 && p2!=0) { 77 if (n>1 && n<72 && p2!=0) {
78 *size = n; 78 *size = n;
79 return p2+1; 79 return p2+1;
80 } 80 }
81 p2=p; n=0; 81 p2=p; n=0;
82 } else if (*p>='0' && *p<='9' && p2!=0) { 82 } else if (*p>='0' && *p<='9' && p2!=0) {
83 n *= 10; 83 n *= 10;
84 n += *p-'0'; 84 n += *p-'0';
85 } else { 85 } else {
86 p2=0; n=0; 86 p2=0; n=0;
87 } 87 }
88 } 88 }
89} 89}
90 90
91const char *getFontElement(const char *pattern, char *buf, int bufsiz, ...) { 91const char *getFontElement(const char *pattern, char *buf, int bufsiz, ...) {
92 const char *p, *v; 92 const char *p, *v;
93 char *p2; 93 char *p2;
94 va_list va; 94 va_list va;
95 95
96 va_start(va, bufsiz); 96 va_start(va, bufsiz);
97 buf[bufsiz-1] = 0; 97 buf[bufsiz-1] = 0;
98 buf[bufsiz-2] = '*'; 98 buf[bufsiz-2] = '*';
99 while((v = va_arg(va, char *)) != 0) { 99 while((v = va_arg(va, char *)) != 0) {
100 p = ::strcasestr(pattern, v); 100 p = ::strcasestr(pattern, v);
101 if (p) { 101 if (p) {
102 std::strncpy(buf, p+1, bufsiz-2); 102 std::strncpy(buf, p+1, bufsiz-2);
103 p2 = strchr(buf, '-'); 103 p2 = strchr(buf, '-');
104 if (p2) *p2=0; 104 if (p2) *p2=0;
105 va_end(va); 105 va_end(va);
106 return p; 106 return p;
107 } 107 }
108 } 108 }
109 va_end(va); 109 va_end(va);
110 std::strncpy(buf, "*", bufsiz); 110 std::strncpy(buf, "*", bufsiz);
111 return 0; 111 return 0;
112} 112}
113 113
114XFontSet createFontSet(const char *fontname) { 114XFontSet createFontSet(const char *fontname) {
115 Display *display = FbTk::App::instance()->display(); 115 Display *display = FbTk::App::instance()->display();
116 XFontSet fs; 116 XFontSet fs;
117 const int FONT_ELEMENT_SIZE=50; 117 const int FONT_ELEMENT_SIZE=50;
118 char **missing, *def = "-"; 118 char **missing, *def = "-";
119 int nmissing, pixel_size = 0, buf_size = 0; 119 int nmissing, pixel_size = 0, buf_size = 0;
120 char weight[FONT_ELEMENT_SIZE], slant[FONT_ELEMENT_SIZE]; 120 char weight[FONT_ELEMENT_SIZE], slant[FONT_ELEMENT_SIZE];
121 121
122 fs = XCreateFontSet(display, 122 fs = XCreateFontSet(display,
123 fontname, &missing, &nmissing, &def); 123 fontname, &missing, &nmissing, &def);
124 if (fs && (! nmissing)) return fs; 124 if (fs && (! nmissing)) return fs;
125 125
126#ifdef HAVE_SETLOCALE 126#ifdef HAVE_SETLOCALE
127 if (! fs) { 127 if (! fs) {
128 if (nmissing) XFreeStringList(missing); 128 if (nmissing) XFreeStringList(missing);
129 129
130 setlocale(LC_CTYPE, "C"); 130 setlocale(LC_CTYPE, "C");
131 fs = XCreateFontSet(display, fontname, 131 fs = XCreateFontSet(display, fontname,
132 &missing, &nmissing, &def); 132 &missing, &nmissing, &def);
133 setlocale(LC_CTYPE, ""); 133 setlocale(LC_CTYPE, "");
134 } 134 }
135#endif // HAVE_SETLOCALE 135#endif // HAVE_SETLOCALE
136 136
137 if (fs) { 137 if (fs) {
138 XFontStruct **fontstructs; 138 XFontStruct **fontstructs;
139 char **fontnames; 139 char **fontnames;
140 XFontsOfFontSet(fs, &fontstructs, &fontnames); 140 XFontsOfFontSet(fs, &fontstructs, &fontnames);
141 fontname = fontnames[0]; 141 fontname = fontnames[0];
142 } 142 }
143 143
144 getFontElement(fontname, weight, FONT_ELEMENT_SIZE, 144 getFontElement(fontname, weight, FONT_ELEMENT_SIZE,
145 "-medium-", "-bold-", "-demibold-", "-regular-", 0); 145 "-medium-", "-bold-", "-demibold-", "-regular-", 0);
146 getFontElement(fontname, slant, FONT_ELEMENT_SIZE, 146 getFontElement(fontname, slant, FONT_ELEMENT_SIZE,
147 "-r-", "-i-", "-o-", "-ri-", "-ro-", 0); 147 "-r-", "-i-", "-o-", "-ri-", "-ro-", 0);
148 getFontSize(fontname, &pixel_size); 148 getFontSize(fontname, &pixel_size);
149 149
150 if (! strcmp(weight, "*")) 150 if (! strcmp(weight, "*"))
151 std::strncpy(weight, "medium", FONT_ELEMENT_SIZE); 151 std::strncpy(weight, "medium", FONT_ELEMENT_SIZE);
152 if (! strcmp(slant, "*")) 152 if (! strcmp(slant, "*"))
153 std::strncpy(slant, "r", FONT_ELEMENT_SIZE); 153 std::strncpy(slant, "r", FONT_ELEMENT_SIZE);
154 if (pixel_size < 3) 154 if (pixel_size < 3)
155 pixel_size = 3; 155 pixel_size = 3;
156 else if (pixel_size > 97) 156 else if (pixel_size > 97)
157 pixel_size = 97; 157 pixel_size = 97;
158 158
159 buf_size = strlen(fontname) + (FONT_ELEMENT_SIZE * 2) + 64; 159 buf_size = strlen(fontname) + (FONT_ELEMENT_SIZE * 2) + 64;
160 char *pattern2 = new char[buf_size]; 160 char *pattern2 = new char[buf_size];
161 snprintf(pattern2, buf_size - 1, 161 snprintf(pattern2, buf_size - 1,
162 "%s," 162 "%s,"
163 "-*-*-%s-%s-*-*-%d-*-*-*-*-*-*-*," 163 "-*-*-%s-%s-*-*-%d-*-*-*-*-*-*-*,"
164 "-*-*-*-*-*-*-%d-*-*-*-*-*-*-*,*", 164 "-*-*-*-*-*-*-%d-*-*-*-*-*-*-*,*",
165 fontname, weight, slant, pixel_size, pixel_size); 165 fontname, weight, slant, pixel_size, pixel_size);
166 fontname = pattern2; 166 fontname = pattern2;
167 167
168 if (nmissing) 168 if (nmissing)
169 XFreeStringList(missing); 169 XFreeStringList(missing);
170 if (fs) 170 if (fs)
171 XFreeFontSet(display, fs); 171 XFreeFontSet(display, fs);
172 172
173 fs = XCreateFontSet(display, fontname, 173 fs = XCreateFontSet(display, fontname,
174 &missing, &nmissing, &def); 174 &missing, &nmissing, &def);
175 delete [] pattern2; 175 delete [] pattern2;
176 176
177 return fs; 177 return fs;
178} 178}
179 179
180}; 180};
@@ -183,75 +183,75 @@ namespace FbTk {
183XmbFontImp::XmbFontImp(const char *filename, bool utf8):m_fontset(0), m_utf8mode(utf8) { 183XmbFontImp::XmbFontImp(const char *filename, bool utf8):m_fontset(0), m_utf8mode(utf8) {
184#ifdef DEBUG 184#ifdef DEBUG
185#ifdef X_HAVE_UTF8_STRING 185#ifdef X_HAVE_UTF8_STRING
186 cerr<<"Using utf8 = "<<utf8<<endl; 186 cerr<<"Using utf8 = "<<utf8<<endl;
187#else // X_HAVE_UTF8_STRING 187#else // X_HAVE_UTF8_STRING
188 cerr<<"Using uft8 = false"<<endl; 188 cerr<<"Using uft8 = false"<<endl;
189#endif //X_HAVE_UTF8_STRING 189#endif //X_HAVE_UTF8_STRING
190#endif // DEBUG 190#endif // DEBUG
191 if (filename != 0) 191 if (filename != 0)
192 load(filename); 192 load(filename);
193} 193}
194 194
195XmbFontImp::~XmbFontImp() { 195XmbFontImp::~XmbFontImp() {
196 if (m_fontset != 0) 196 if (m_fontset != 0)
197 XFreeFontSet(App::instance()->display(), m_fontset); 197 XFreeFontSet(App::instance()->display(), m_fontset);
198} 198}
199 199
200bool XmbFontImp::load(const std::string &fontname) { 200bool XmbFontImp::load(const std::string &fontname) {
201 if (fontname.size() == 0) 201 if (fontname.size() == 0)
202 return false; 202 return false;
203 XFontSet set = createFontSet(fontname.c_str()); 203 XFontSet set = createFontSet(fontname.c_str());
204 if (set == 0) 204 if (set == 0)
205 return false; 205 return false;
206 if (m_fontset != 0) 206 if (m_fontset != 0)
207 XFreeFontSet(App::instance()->display(), m_fontset); 207 XFreeFontSet(App::instance()->display(), m_fontset);
208 m_fontset = set; 208 m_fontset = set;
209 m_setextents = XExtentsOfFontSet(m_fontset); 209 m_setextents = XExtentsOfFontSet(m_fontset);
210 210
211 return true; 211 return true;
212} 212}
213 213
214void XmbFontImp::drawText(Drawable w, int screen, GC gc, const char *text, 214void XmbFontImp::drawText(Drawable w, int screen, GC gc, const char *text,
215 size_t len, int x, int y) const { 215 size_t len, int x, int y) const {
216 216
217 if (text == 0 || len == 0 || w == 0 || m_fontset == 0) 217 if (text == 0 || len == 0 || w == 0 || m_fontset == 0)
218 return; 218 return;
219#ifdef X_HAVE_UTF8_STRING 219#ifdef X_HAVE_UTF8_STRING
220 if (m_utf8mode) { 220 if (m_utf8mode) {
221 Xutf8DrawString(App::instance()->display(), w, m_fontset, 221 Xutf8DrawString(App::instance()->display(), w, m_fontset,
222 gc, x, y, 222 gc, x, y,
223 text, len); 223 text, len);
224 } else 224 } else
225#endif //X_HAVE_UTF8_STRING 225#endif //X_HAVE_UTF8_STRING
226 { 226 {
227 XmbDrawString(App::instance()->display(), w, m_fontset, 227 XmbDrawString(App::instance()->display(), w, m_fontset,
228 gc, x, y, 228 gc, x, y,
229 text, len); 229 text, len);
230 } 230 }
231} 231}
232 232
233unsigned int XmbFontImp::textWidth(const char * const text, unsigned int len) const { 233unsigned int XmbFontImp::textWidth(const char * const text, unsigned int len) const {
234 if (m_fontset == 0) 234 if (m_fontset == 0)
235 return 0; 235 return 0;
236 XRectangle ink, logical; 236 XRectangle ink, logical;
237#ifdef X_HAVE_UTF8_STRING 237#ifdef X_HAVE_UTF8_STRING
238 if (m_utf8mode) { 238 if (m_utf8mode) {
239 Xutf8TextExtents(m_fontset, text, len, 239 Xutf8TextExtents(m_fontset, text, len,
240 &ink, &logical); 240 &ink, &logical);
241 } else 241 } else
242#endif // X_HAVE_UTF8_STRING 242#endif // X_HAVE_UTF8_STRING
243 { 243 {
244 XmbTextExtents(m_fontset, text, len, 244 XmbTextExtents(m_fontset, text, len,
245 &ink, &logical); 245 &ink, &logical);
246 } 246 }
247 247
248 return logical.width; 248 return logical.width;
249} 249}
250 250
251unsigned int XmbFontImp::height() const { 251unsigned int XmbFontImp::height() const {
252 if (m_fontset == 0) 252 if (m_fontset == 0)
253 return 0; 253 return 0;
254 return m_setextents->max_ink_extent.height; 254 return m_setextents->max_ink_extent.height;
255} 255}
256 256
257}; // end namespace FbTk 257}; // end namespace FbTk