src/FbTk/XmbFontImp.cc (raw)
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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
// XmbFontImp.cc for FbTk fluxbox toolkit // Copyright (c) 2002 Henrik Kinnunen (fluxgen at linuxmail.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. // $Id: XmbFontImp.cc,v 1.4 2002/12/02 19:31:05 fluxgen Exp $ #include "XmbFontImp.hh" #include "App.hh" #ifdef HAVE_CONFIG_H #include "config.h" #endif //HAVE_CONFIG_H #ifdef HAVE_SETLOCALE #include <locale.h> #endif // HAVE_SETLOCALE #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif // _GNU_SOURCE #include <cstdio> #include <cstdarg> #include <iostream> #include <cstring> using namespace std; namespace { #ifndef HAVE_STRCASESTR // // Tries to find a string in another and // ignoring the case of the characters // Returns 0 on success else pointer to str. const char *strcasestr(const char *str, const char *ptn) { const char *s2, *p2; for( ; *str; str++) { for(s2=str, p2=ptn; ; s2++,p2++) { // check if we reached the end of ptn, if so, return str if (!*p2) return str; // check if the chars match(ignoring case) if (toupper(*s2) != toupper(*p2)) break; } } return 0; } #endif //HAVE_STRCASESTR const char *getFontSize(const char *pattern, int *size) { const char *p; const char *p2=0; int n=0; for (p=pattern; 1; p++) { if (!*p) { if (p2!=0 && n>1 && n<72) { *size = n; return p2+1; } else { *size = 16; return 0; } } else if (*p=='-') { if (n>1 && n<72 && p2!=0) { *size = n; return p2+1; } p2=p; n=0; } else if (*p>='0' && *p<='9' && p2!=0) { n *= 10; n += *p-'0'; } else { p2=0; n=0; } } } const char *getFontElement(const char *pattern, char *buf, int bufsiz, ...) { const char *p, *v; char *p2; va_list va; va_start(va, bufsiz); buf[bufsiz-1] = 0; buf[bufsiz-2] = '*'; while((v = va_arg(va, char *)) != 0) { p = ::strcasestr(pattern, v); if (p) { std::strncpy(buf, p+1, bufsiz-2); p2 = strchr(buf, '-'); if (p2) *p2=0; va_end(va); return p; } } va_end(va); std::strncpy(buf, "*", bufsiz); return 0; } XFontSet createFontSet(const char *fontname) { Display *display = FbTk::App::instance()->display(); XFontSet fs; const int FONT_ELEMENT_SIZE=50; char **missing, *def = "-"; int nmissing, pixel_size = 0, buf_size = 0; char weight[FONT_ELEMENT_SIZE], slant[FONT_ELEMENT_SIZE]; fs = XCreateFontSet(display, fontname, &missing, &nmissing, &def); if (fs && (! nmissing)) 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, ""); } #endif // HAVE_SETLOCALE if (fs) { XFontStruct **fontstructs; char **fontnames; XFontsOfFontSet(fs, &fontstructs, &fontnames); fontname = fontnames[0]; } getFontElement(fontname, weight, FONT_ELEMENT_SIZE, "-medium-", "-bold-", "-demibold-", "-regular-", 0); getFontElement(fontname, slant, FONT_ELEMENT_SIZE, "-r-", "-i-", "-o-", "-ri-", "-ro-", 0); getFontSize(fontname, &pixel_size); if (! strcmp(weight, "*")) std::strncpy(weight, "medium", FONT_ELEMENT_SIZE); if (! strcmp(slant, "*")) std::strncpy(slant, "r", FONT_ELEMENT_SIZE); if (pixel_size < 3) pixel_size = 3; else if (pixel_size > 97) pixel_size = 97; buf_size = strlen(fontname) + (FONT_ELEMENT_SIZE * 2) + 64; char *pattern2 = new char[buf_size]; snprintf(pattern2, buf_size - 1, "%s," "-*-*-%s-%s-*-*-%d-*-*-*-*-*-*-*," "-*-*-*-*-*-*-%d-*-*-*-*-*-*-*,*", fontname, weight, slant, pixel_size, pixel_size); fontname = pattern2; if (nmissing) XFreeStringList(missing); if (fs) XFreeFontSet(display, fs); fs = XCreateFontSet(display, fontname, &missing, &nmissing, &def); delete [] pattern2; return fs; } }; namespace FbTk { XmbFontImp::XmbFontImp(const char *filename, bool utf8):m_fontset(0), m_utf8mode(utf8) { #ifdef DEBUG #ifdef X_HAVE_UTF8_STRING cerr<<"Using utf8 = "<<utf8<<endl; #else // X_HAVE_UTF8_STRING cerr<<"Using uft8 = false"<<endl; #endif //X_HAVE_UTF8_STRING #endif // DEBUG if (filename != 0) load(filename); } XmbFontImp::~XmbFontImp() { if (m_fontset != 0) XFreeFontSet(App::instance()->display(), m_fontset); } bool XmbFontImp::load(const std::string &fontname) { if (fontname.size() == 0) return false; XFontSet set = createFontSet(fontname.c_str()); 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(Drawable w, int screen, GC gc, const char *text, size_t len, int x, int y) const { if (text == 0 || len == 0 || w == 0 || m_fontset == 0) return; #ifdef X_HAVE_UTF8_STRING if (m_utf8mode) { Xutf8DrawString(App::instance()->display(), w, m_fontset, gc, x, y, text, len); } else #endif //X_HAVE_UTF8_STRING { XmbDrawString(App::instance()->display(), w, m_fontset, gc, x, y, text, len); } } unsigned int XmbFontImp::textWidth(const char * const 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); } else #endif // X_HAVE_UTF8_STRING { XmbTextExtents(m_fontset, text, len, &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 |