| 1 |
// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*- |
| 2 |
// Font.hh for Blackbox - an X11 Window manager |
| 3 |
// Copyright (c) 2001 - 2005 Sean 'Shaleh' Perry <[email protected]> |
| 4 |
// Copyright (c) 1997 - 2000, 2002 - 2005 |
| 5 |
// Bradley T Hughes <bhughes at trolltech.com> |
| 6 |
// |
| 7 |
// Permission is hereby granted, free of charge, to any person obtaining a |
| 8 |
// copy of this software and associated documentation files (the "Software"), |
| 9 |
// to deal in the Software without restriction, including without limitation |
| 10 |
// the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 |
// and/or sell copies of the Software, and to permit persons to whom the |
| 12 |
// Software is furnished to do so, subject to the following conditions: |
| 13 |
// |
| 14 |
// The above copyright notice and this permission notice shall be included in |
| 15 |
// all copies or substantial portions of the Software. |
| 16 |
// |
| 17 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 |
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 |
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 23 |
// DEALINGS IN THE SOFTWARE. |
| 24 |
|
| 25 |
#ifndef __Font_hh |
| 26 |
#define __Font_hh |
| 27 |
|
| 28 |
#include "Unicode.hh" |
| 29 |
#include "Util.hh" |
| 30 |
|
| 31 |
namespace bt { |
| 32 |
|
| 33 |
// forward declarations |
| 34 |
class Display; |
| 35 |
class Font; |
| 36 |
class Pen; |
| 37 |
class Rect; |
| 38 |
class Resource; |
| 39 |
|
| 40 |
enum Alignment { |
| 41 |
AlignLeft, |
| 42 |
AlignCenter, |
| 43 |
AlignRight |
| 44 |
}; |
| 45 |
|
| 46 |
unsigned int textHeight(unsigned int screen, const Font &font); |
| 47 |
|
| 48 |
/* |
| 49 |
Returns the text indent (pad). Note that textRect() and |
| 50 |
drawText() use this function (so you do not have to). |
| 51 |
*/ |
| 52 |
unsigned int textIndent(unsigned int screen, const Font &font); |
| 53 |
|
| 54 |
Rect textRect(unsigned int screen, const Font &font, |
| 55 |
const bt::ustring &text); |
| 56 |
|
| 57 |
void drawText(const Font &font, const Pen &pen, |
| 58 |
Drawable drawable, const Rect &rect, |
| 59 |
Alignment alignment, const ustring &text); |
| 60 |
|
| 61 |
/* |
| 62 |
* Take a string and make it 'count' chars long by removing the |
| 63 |
* middle and replacing it with the string in 'ellide'. |
| 64 |
*/ |
| 65 |
ustring ellideText(const ustring &text, size_t count, |
| 66 |
const ustring &ellide); |
| 67 |
|
| 68 |
/* |
| 69 |
* Take a string and make no more than 'max_width' pixels wide by |
| 70 |
* removing the middle and replacing it with the string in 'ellide'. |
| 71 |
* The on-screen size of the string font is determined using the |
| 72 |
* specified font on the specified screen. |
| 73 |
*/ |
| 74 |
ustring ellideText(const ustring &text, |
| 75 |
unsigned int max_width, |
| 76 |
const ustring &ellide, |
| 77 |
unsigned int screen, |
| 78 |
const bt::Font &font); |
| 79 |
|
| 80 |
Alignment alignResource(const Resource &resource, |
| 81 |
const char* name, const char* classname, |
| 82 |
Alignment default_align = AlignLeft); |
| 83 |
|
| 84 |
class Font { |
| 85 |
public: |
| 86 |
static void clearCache(void); |
| 87 |
|
| 88 |
explicit inline Font(const std::string &name = std::string()) |
| 89 |
: _fontname(name), _fontset(0), _xftfont(0), _screen(~0u) |
| 90 |
{ } |
| 91 |
inline ~Font(void) |
| 92 |
{ unload(); } |
| 93 |
|
| 94 |
inline const std::string& fontName(void) const |
| 95 |
{ return _fontname; } |
| 96 |
inline void setFontName(const std::string &new_fontname) |
| 97 |
{ unload(); _fontname = new_fontname; } |
| 98 |
|
| 99 |
XFontSet fontSet(void) const; |
| 100 |
XftFont *xftFont(unsigned int screen) const; |
| 101 |
|
| 102 |
inline Font& operator=(const Font &f) |
| 103 |
{ setFontName(f.fontName()); return *this; } |
| 104 |
inline bool operator==(const Font &f) const |
| 105 |
{ return _fontname == f._fontname; } |
| 106 |
inline bool operator!=(const Font &f) const |
| 107 |
{ return (!operator==(f)); } |
| 108 |
|
| 109 |
private: |
| 110 |
void unload(void); |
| 111 |
|
| 112 |
std::string _fontname; |
| 113 |
mutable XFontSet _fontset; |
| 114 |
mutable XftFont *_xftfont; |
| 115 |
mutable unsigned int _screen; // only used for Xft |
| 116 |
}; |
| 117 |
|
| 118 |
} // namespace bt |
| 119 |
|
| 120 |
#endif // __Font_hh |