00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00045
00046 Display *disp = App::instance()->display();
00047 XftFont *newxftfont = XftFontOpenName(disp, 0, name.c_str());
00048
00049 if (newxftfont == 0) {
00050 newxftfont = XftFontOpenXlfd(disp, 0, name.c_str());
00051 if (newxftfont == 0)
00052 return false;
00053 }
00054
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
00075
00076 XGetGCValues(disp, gc, GCForeground, &gc_val);
00077
00078
00079 XColor xcol;
00080 xcol.pixel = gc_val.foreground;
00081 XQueryColor(disp, DefaultColormap(disp, screen), &xcol);
00082
00083
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
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 };