diff options
Diffstat (limited to 'src/FbTk/XftFontImp.cc')
-rw-r--r-- | src/FbTk/XftFontImp.cc | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/src/FbTk/XftFontImp.cc b/src/FbTk/XftFontImp.cc new file mode 100644 index 0000000..59f8440 --- /dev/null +++ b/src/FbTk/XftFontImp.cc | |||
@@ -0,0 +1,143 @@ | |||
1 | // XftFontImp.cc Xft font implementation for FbTk | ||
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: XftFontImp.cc,v 1.2 2002/12/01 13:42:15 rathnor Exp $ | ||
23 | |||
24 | #include "XftFontImp.hh" | ||
25 | #include "App.hh" | ||
26 | |||
27 | #ifdef HAVE_CONFIG_H | ||
28 | #include "config.h" | ||
29 | #endif //HAVE_CONFIG_H | ||
30 | namespace FbTk { | ||
31 | |||
32 | XftFontImp::XftFontImp(const char *name, bool utf8):m_xftfont(0), | ||
33 | m_utf8mode(utf8) { | ||
34 | if (name != 0) | ||
35 | load(name); | ||
36 | } | ||
37 | |||
38 | XftFontImp::~XftFontImp() { | ||
39 | if (m_xftfont != 0) | ||
40 | XftFontClose(App::instance()->display(), m_xftfont); | ||
41 | } | ||
42 | |||
43 | bool XftFontImp::load(const std::string &name) { | ||
44 | //Note: assumes screen 0 for now, changes on draw if needed | ||
45 | |||
46 | Display *disp = App::instance()->display(); | ||
47 | XftFont *newxftfont = XftFontOpenName(disp, 0, name.c_str()); | ||
48 | |||
49 | if (newxftfont == 0) { // failed to open font, lets test with XLFD | ||
50 | newxftfont = XftFontOpenXlfd(disp, 0, name.c_str()); | ||
51 | if (newxftfont == 0) | ||
52 | return false; | ||
53 | } | ||
54 | // destroy old font and set new | ||
55 | if (m_xftfont != 0) | ||
56 | XftFontClose(disp, m_xftfont); | ||
57 | |||
58 | m_xftfont = newxftfont; | ||
59 | |||
60 | return true; | ||
61 | } | ||
62 | |||
63 | void XftFontImp::drawText(Drawable w, int screen, GC gc, const char *text, size_t len, int x, int y) const { | ||
64 | if (m_xftfont == 0) | ||
65 | return; | ||
66 | Display *disp = App::instance()->display(); | ||
67 | XftDraw *draw = XftDrawCreate(disp, | ||
68 | w, | ||
69 | DefaultVisual(disp, screen), | ||
70 | DefaultColormap(disp, screen)); | ||
71 | |||
72 | XGCValues gc_val; | ||
73 | |||
74 | // get foreground pixel value and convert it to XRenderColor value | ||
75 | // TODO: we should probably check return status | ||
76 | XGetGCValues(disp, gc, GCForeground, &gc_val); | ||
77 | |||
78 | // get red, green, blue values | ||
79 | XColor xcol; | ||
80 | xcol.pixel = gc_val.foreground; | ||
81 | XQueryColor(disp, DefaultColormap(disp, screen), &xcol); | ||
82 | |||
83 | // convert xcolor to XftColor | ||
84 | XRenderColor rendcol; | ||
85 | rendcol.red = xcol.red; | ||
86 | rendcol.green = xcol.green; | ||
87 | rendcol.blue = xcol.blue; | ||
88 | rendcol.alpha = 0xFFFF; | ||
89 | XftColor xftcolor; | ||
90 | XftColorAllocValue(disp, DefaultVisual(disp, screen), DefaultColormap(disp, screen), | ||
91 | &rendcol, &xftcolor); | ||
92 | |||
93 | // draw string | ||
94 | #ifdef HAVE_XFT_UTF8_STRING | ||
95 | if (m_utf8mode) { | ||
96 | XftDrawStringUtf8(draw, | ||
97 | &xftcolor, | ||
98 | m_xftfont, | ||
99 | x, y, | ||
100 | (XftChar8 *)(text), len); | ||
101 | } else | ||
102 | #endif // HAVE_XFT_UTF8_STRING | ||
103 | { | ||
104 | XftDrawString8(draw, | ||
105 | &xftcolor, | ||
106 | m_xftfont, | ||
107 | x, y, | ||
108 | (XftChar8 *)(text), len); | ||
109 | } | ||
110 | |||
111 | XftColorFree(disp, DefaultVisual(disp, screen), | ||
112 | DefaultColormap(disp, screen), &xftcolor); | ||
113 | XftDrawDestroy(draw); | ||
114 | } | ||
115 | |||
116 | unsigned int XftFontImp::textWidth(const char * const text, unsigned int len) const { | ||
117 | if (m_xftfont == 0) | ||
118 | return 0; | ||
119 | XGlyphInfo ginfo; | ||
120 | #ifdef HAVE_XFT_UTF8_STRING | ||
121 | if (m_utf8mode) { | ||
122 | XftTextExtentsUtf8(App::instance()->display(), | ||
123 | m_xftfont, | ||
124 | (XftChar8 *)text, len, | ||
125 | &ginfo); | ||
126 | } else | ||
127 | #endif //HAVE_XFT_UTF8_STRING | ||
128 | { | ||
129 | XftTextExtents8(App::instance()->display(), | ||
130 | m_xftfont, | ||
131 | (XftChar8 *)text, len, | ||
132 | &ginfo); | ||
133 | } | ||
134 | return ginfo.xOff; | ||
135 | } | ||
136 | |||
137 | unsigned int XftFontImp::height() const { | ||
138 | if (m_xftfont == 0) | ||
139 | return 0; | ||
140 | return m_xftfont->height; | ||
141 | } | ||
142 | |||
143 | }; // end namespace FbTk | ||