1 // Font.cc for FbTk
2 // Copyright (c) 2002 - 2006 Henrik Kinnunen (fluxgen at fluxbox dot 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: Font.hh 4199 2006-02-16 06:53:05Z mathias $
23 
24 #ifndef FBTK_FONT_HH
25 #define FBTK_FONT_HH
26 
27 #include <X11/Xlib.h>
28 #include <X11/Xresource.h>
29 
30 #include <string>
31 
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif // HAVE_CONFIG_H
35 
36 #ifdef HAVE_ICONV
37 #include <iconv.h>
38 #endif // HAVE_ICONV
39 
40 #include "Color.hh"
41 
42 namespace FbTk {
43 
44 class FontImp;
45 class FbDrawable;
46 
47 /**
48    Handles the client to fontimp bridge.
49 */
50 class Font {
51 public:
52 
53     /// called at FbTk::App creation time, initializes some stuff
54     static void init();
55 
56     /// called at FbTk::App destruction time, cleans up what was inited first
57     static void shutdown();
58 
59     /// @return true if multibyte is enabled, else false
multibyte()60     static bool multibyte() { return s_multibyte; }
61     /// @return true if utf-8 mode is enabled, else false
utf8()62     static bool utf8() { return s_utf8mode; }
63 
64 
65 
66     explicit Font(const char *name = "fixed");
67     virtual ~Font();
68     /**
69         Load a font
70         @return true on success, else false and it'll fall back on the last
71         loaded font
72     */
73     bool load(const std::string &name);
74 
setHalo(bool flag)75     void setHalo(bool flag)   { m_halo = flag; if (m_halo) setShadow(false); }
setHaloColor(const Color & color)76     void setHaloColor(const Color& color) { m_halo_color = color; }
77 
setShadow(bool flag)78     void setShadow(bool flag) { m_shadow = flag; if (m_shadow) setHalo(false); }
setShadowColor(const Color & color)79     void setShadowColor(const Color& color) { m_shadow_color = color; }
setShadowOffX(int offx)80     void setShadowOffX(int offx) { m_shadow_offx = offx; }
setShadowOffY(int offy)81     void setShadowOffY(int offy) { m_shadow_offy = offy; }
82 
83     /**
84        @param text text to check size
85        @param size length of text in bytes
86        @return size of text in pixels
87     */
88     unsigned int textWidth(const char * const text, unsigned int size) const;
89     unsigned int height() const;
90     int ascent() const;
91     int descent() const;
92     /**
93        Rotate font in any angle
94        (currently only 90 degrees supported and just XFont implementation)
95     */
96     void rotate(float angle);
97 
98     /**
99        Draws text to drawable
100        @param w the drawable
101        @param screen screen number
102        @param gc Graphic Context
103        @param text the text buffer
104        @param len size of text buffer
105        @param x position
106        @param y position
107        @param rotate if the text should be drawn rotated (if it's rotated before)
108     */
109     void drawText(const FbDrawable &w, int screen, GC gc,
110                   const char *text, size_t len,
111                   int x, int y, bool rotate=true) const;
112     /// @return true if the font is rotated, else false
isRotated() const113     bool isRotated() const { return m_rotated; }
114     /// @return rotated angle
angle() const115     float angle() const { return m_angle; }
hasShadow() const116     bool hasShadow() const { return m_shadow; }
hasHalo() const117     bool hasHalo() const { return m_halo; }
118 private:
119 
120     FbTk::FontImp* m_fontimp; ///< font implementation
121     std::string m_fontstr; ///< font name
122 
123     static bool s_multibyte; ///< if the fontimp should be a multibyte font
124     static bool s_utf8mode; ///< should the font use utf8 font imp
125 
126     bool m_rotated; ///< wheter we're rotated or not
127     float m_angle; ///< rotation angle
128     bool m_shadow; ///< shadow text
129     Color m_shadow_color; ///< shadow color
130     int m_shadow_offx; ///< offset y for shadow
131     int m_shadow_offy; ///< offset x for shadow
132     bool m_halo; ///< halo text
133     Color m_halo_color; ///< halo color
134 #ifdef HAVE_ICONV
135     iconv_t m_iconv;
136 #else
137     int m_iconv;
138 #endif // HAVE_ICONV
139 };
140 
141 } //end namespace FbTk
142 
143 #endif //FBTK_FONT_HH
144