summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/XftFontImp.cc112
-rw-r--r--src/XftFontImp.hh42
2 files changed, 154 insertions, 0 deletions
diff --git a/src/XftFontImp.cc b/src/XftFontImp.cc
new file mode 100644
index 0000000..d31a74e
--- /dev/null
+++ b/src/XftFontImp.cc
@@ -0,0 +1,112 @@
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.1 2002/10/14 18:14:20 fluxgen Exp $
23
24#include "XftFontImp.hh"
25#include "BaseDisplay.hh"
26
27XftFontImp::XftFontImp(const char *name):m_xftfont(0) {
28 if (name != 0)
29 load(name);
30}
31
32XftFontImp::~XftFontImp() {
33 if (m_xftfont != 0)
34 XftFontClose(BaseDisplay::getXDisplay(), m_xftfont);
35}
36
37bool XftFontImp::load(const std::string &name) {
38 //Note: assumes screen 0 for now, changes on draw if needed
39
40 Display *disp = BaseDisplay::getXDisplay();
41 XftFont *newxftfont = XftFontOpenName(disp, 0, name.c_str());
42
43 if (newxftfont == 0) // failed to open font, use old fon
44 return false;
45
46 // destroy old font and set new
47 if (m_xftfont != 0)
48 XftFontClose(disp, m_xftfont);
49
50 m_xftfont = newxftfont;
51
52 return true;
53}
54
55void XftFontImp::drawText(Drawable w, int screen, GC gc, const char *text, size_t len, int x, int y) const {
56 if (m_xftfont == 0)
57 return;
58 Display *disp = BaseDisplay::getXDisplay();
59 XftDraw *draw = XftDrawCreate(disp,
60 w,
61 DefaultVisual(disp, screen),
62 DefaultColormap(disp, screen));
63
64 XGCValues gc_val;
65
66 // get foreground pixel value and convert it to XRenderColor value
67 // TODO: we should probably check return status
68 XGetGCValues(disp, gc, GCForeground, &gc_val);
69
70 // get red, green, blue values
71 XColor xcol;
72 xcol.pixel = gc_val.foreground;
73 XQueryColor(disp, DefaultColormap(disp, screen), &xcol);
74
75 // convert xcolor to XftColor
76 XRenderColor rendcol;
77 rendcol.red = xcol.red;
78 rendcol.green = xcol.green;
79 rendcol.blue = xcol.blue;
80 rendcol.alpha = 0xFFFF;
81 XftColor xftcolor;
82 XftColorAllocValue(disp, DefaultVisual(disp, screen), DefaultColormap(disp, screen),
83 &rendcol, &xftcolor);
84
85 // draw string
86 XftDrawString8 (draw,
87 &xftcolor,
88 m_xftfont,
89 x, y,
90 (XftChar8 *)(text), len);
91
92 XftColorFree(disp, DefaultVisual(disp, screen),
93 DefaultColormap(disp, screen), &xftcolor);
94 XftDrawDestroy(draw);
95}
96
97unsigned int XftFontImp::textWidth(const char *text, unsigned int len) const {
98 if (m_xftfont == 0)
99 return 0;
100 XGlyphInfo ginfo;
101 XftTextExtents8(BaseDisplay::getXDisplay(),
102 m_xftfont,
103 (XftChar8 *)text, len,
104 &ginfo);
105 return ginfo.xOff;
106}
107
108unsigned int XftFontImp::height() const {
109 if (m_xftfont == 0)
110 return 0;
111 return m_xftfont->ascent + m_xftfont->descent;
112}
diff --git a/src/XftFontImp.hh b/src/XftFontImp.hh
new file mode 100644
index 0000000..de3a1ed
--- /dev/null
+++ b/src/XftFontImp.hh
@@ -0,0 +1,42 @@
1// XftFontImp.hh 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.hh,v 1.1 2002/10/14 18:14:13 fluxgen Exp $
23
24#ifndef XFTFONTIMP_HH
25#define XFTFONTIMP_HH
26
27#include "FontImp.hh"
28#include <X11/Xft/Xft.h>
29
30class XftFontImp:public FbTk::FontImp {
31public:
32 explicit XftFontImp(const char *fontname=0);
33 ~XftFontImp();
34 bool load(const std::string &name);
35 void drawText(Drawable w, int screen, GC gc, const char *text, size_t len, int x, int y) const;
36 unsigned int textWidth(const char *text, unsigned int len) const;
37 unsigned int height() const;
38private:
39 XftFont *m_xftfont;
40};
41
42#endif // XFTFONTIMP_HH