news download themes documentation links










XftFontImp.cc

00001 // XftFontImp.cc  Xft font implementation for FbTk
00002 // Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org)
00003 // 
00004 // Permission is hereby granted, free of charge, to any person obtaining a
00005 // copy of this software and associated documentation files (the "Software"),
00006 // to deal in the Software without restriction, including without limitation
00007 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008 // and/or sell copies of the Software, and to permit persons to whom the
00009 // Software is furnished to do so, subject to the following conditions:
00010 //
00011 // The above copyright notice and this permission notice shall be included in
00012 // all copies or substantial portions of the Software.
00013 //
00014 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
00017 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00019 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00020 // DEALINGS IN THE SOFTWARE.
00021 
00022 //$Id: XftFontImp.cc,v 1.2 2002/12/01 13:42:15 rathnor Exp $
00023 
00024 #include "XftFontImp.hh"
00025 #include "App.hh"
00026 
00027 #ifdef HAVE_CONFIG_H
00028 #include "config.h"
00029 #endif //HAVE_CONFIG_H
00030 namespace FbTk {
00031 
00032 XftFontImp::XftFontImp(const char *name, bool utf8):m_xftfont(0),
00033                                                     m_utf8mode(utf8) {
00034     if (name != 0)
00035         load(name);
00036 }
00037 
00038 XftFontImp::~XftFontImp() {
00039     if (m_xftfont != 0)
00040         XftFontClose(App::instance()->display(), m_xftfont);
00041 }
00042 
00043 bool XftFontImp::load(const std::string &name) {
00044     //Note: assumes screen 0 for now, changes on draw if needed
00045     
00046     Display *disp = App::instance()->display();
00047     XftFont *newxftfont = XftFontOpenName(disp, 0, name.c_str());
00048         
00049     if (newxftfont == 0) { // failed to open font, lets test with XLFD
00050         newxftfont = XftFontOpenXlfd(disp, 0, name.c_str());
00051         if (newxftfont == 0)
00052             return false;
00053     }
00054     // destroy old font and set new
00055     if (m_xftfont != 0)
00056         XftFontClose(disp, m_xftfont);
00057 
00058     m_xftfont = newxftfont;
00059 
00060     return true;
00061 }
00062 
00063 void XftFontImp::drawText(Drawable w, int screen, GC gc, const char *text, size_t len, int x, int y) const {
00064     if (m_xftfont == 0)
00065         return;
00066     Display *disp = App::instance()->display();
00067     XftDraw *draw = XftDrawCreate(disp,
00068                                   w,
00069                                   DefaultVisual(disp, screen),
00070                                   DefaultColormap(disp, screen));
00071 
00072     XGCValues gc_val;
00073 
00074     // get foreground pixel value and convert it to XRenderColor value
00075     // TODO: we should probably check return status
00076     XGetGCValues(disp, gc, GCForeground, &gc_val);
00077 
00078     // get red, green, blue values
00079     XColor xcol;
00080     xcol.pixel = gc_val.foreground;
00081     XQueryColor(disp, DefaultColormap(disp, screen), &xcol);
00082 
00083     // convert xcolor to XftColor
00084     XRenderColor rendcol;
00085     rendcol.red = xcol.red;
00086     rendcol.green = xcol.green;
00087     rendcol.blue = xcol.blue;
00088     rendcol.alpha = 0xFFFF;
00089     XftColor xftcolor;  
00090     XftColorAllocValue(disp, DefaultVisual(disp, screen), DefaultColormap(disp, screen),
00091                        &rendcol, &xftcolor);
00092 
00093     // draw string
00094 #ifdef HAVE_XFT_UTF8_STRING
00095     if (m_utf8mode) {
00096         XftDrawStringUtf8(draw,
00097                           &xftcolor,
00098                           m_xftfont,
00099                           x, y,
00100                           (XftChar8 *)(text), len);
00101     } else 
00102 #endif // HAVE_XFT_UTF8_STRING
00103     {
00104             XftDrawString8(draw,
00105                            &xftcolor,
00106                            m_xftfont,
00107                            x, y,
00108                            (XftChar8 *)(text), len);
00109     }
00110 
00111     XftColorFree(disp, DefaultVisual(disp, screen), 
00112                  DefaultColormap(disp, screen), &xftcolor);
00113     XftDrawDestroy(draw);
00114 }
00115 
00116 unsigned int XftFontImp::textWidth(const char * const text, unsigned int len) const {
00117     if (m_xftfont == 0)
00118         return 0;
00119     XGlyphInfo ginfo;
00120 #ifdef HAVE_XFT_UTF8_STRING
00121     if (m_utf8mode) {
00122         XftTextExtentsUtf8(App::instance()->display(),
00123                            m_xftfont,
00124                            (XftChar8 *)text, len,
00125                            &ginfo);
00126     } else 
00127 #endif  //HAVE_XFT_UTF8_STRING
00128     {
00129             XftTextExtents8(App::instance()->display(),
00130                             m_xftfont,
00131                             (XftChar8 *)text, len,
00132                             &ginfo);
00133     }
00134     return ginfo.xOff;
00135 }
00136 
00137 unsigned int XftFontImp::height() const {
00138     if (m_xftfont == 0)
00139         return 0;
00140     return m_xftfont->height;
00141 }
00142 
00143 }; // end namespace FbTk

Fluxbox CVS-Jan-2003




      



Got comments about the page? Send them to webmaster.
If you have general Fluxbox related questions ask them on our irc channel or mailing lists.

Show Source








Designed by aLEczapKA