2002-11-26 16:01:28 +00:00
|
|
|
// XftFontImp.cc Xft font implementation for FbTk
|
2006-02-16 06:53:05 +00:00
|
|
|
// Copyright (c) 2002 - 2006 Henrik Kinnunen (fluxgen at fluxbox dot org)
|
2004-09-10 16:12:49 +00:00
|
|
|
//
|
2002-11-26 16:01:28 +00:00
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2004-11-19 11:37:27 +00:00
|
|
|
//$Id$
|
2002-11-26 16:01:28 +00:00
|
|
|
|
|
|
|
#include "XftFontImp.hh"
|
|
|
|
#include "App.hh"
|
2004-09-11 23:01:34 +00:00
|
|
|
#include "FbDrawable.hh"
|
2002-11-26 16:01:28 +00:00
|
|
|
|
2006-03-26 04:02:30 +00:00
|
|
|
#include <math.h>
|
|
|
|
|
2002-11-26 16:01:28 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif //HAVE_CONFIG_H
|
2004-08-10 11:57:35 +00:00
|
|
|
|
2002-11-26 16:01:28 +00:00
|
|
|
namespace FbTk {
|
|
|
|
|
2006-03-26 04:02:30 +00:00
|
|
|
XftFontImp::XftFontImp(const char *name, bool utf8):
|
|
|
|
m_utf8mode(utf8), m_name("") {
|
|
|
|
|
|
|
|
for (int r = ROT0; r <= ROT270; r++)
|
|
|
|
m_xftfonts[r] = 0;
|
|
|
|
|
2002-12-01 13:42:15 +00:00
|
|
|
if (name != 0)
|
|
|
|
load(name);
|
2002-11-26 16:01:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
XftFontImp::~XftFontImp() {
|
2006-03-26 04:02:30 +00:00
|
|
|
for (int r = ROT0; r <= ROT270; r++)
|
|
|
|
if (m_xftfonts[r] != 0)
|
|
|
|
XftFontClose(App::instance()->display(), m_xftfonts[r]);
|
2002-11-26 16:01:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool XftFontImp::load(const std::string &name) {
|
2002-12-01 13:42:15 +00:00
|
|
|
//Note: assumes screen 0 for now, changes on draw if needed
|
2004-09-10 16:12:49 +00:00
|
|
|
|
2002-12-01 13:42:15 +00:00
|
|
|
Display *disp = App::instance()->display();
|
2004-09-10 16:12:49 +00:00
|
|
|
|
- Usage of xft-fonts is prefered, except a font-description starts with '-'
- Removed "antialias"-option completly, to enable/disable "antialias"
use either <fontname>:antialias=<bool> in the style or use
Xft.antialias: <bool> in your .Xdefaults
- Added new styleresources:
*.font.effect: <halo|shadow>
*.font.shadow.x : <int> - shadow x offset
*.font.shadow.y : <int> - shadow y offset
*.font.shadow.color : <color> - color of shadow
*.font.halo.color : <color> - color of halo
- Removed 'shadow' and 'halo' options from fontdefinitions:
!! Style authors have to update their styles !!
- Simplified XmbFontImp to not try all possible fonts to match locale
- Style authors may specify multiple fonts:
<font1>|<font2>|<font3>
if loading of font1 fails, fluxbox probes <font2>, etc. The last font is
"fixed". Hints for style authors:
- if xft tries to load a font it will _ALWAYS_ return a valid font,
it doesnt have to look like the one you want to have, read more
about it: http://fontconfig.org/fontconfig-user.html
- export XFT_DEBUG=1 before running fluxbox helps to see
which fonts are picked.
eg:
*.font: Verdana,Arial-12:antialias=true|-artwiz-snap-*-
if fluxbox is compiled with xft this will NEVER try to
load "-artwiz-snap-*-" since xft gives for sure a font,
most likely Verdana or Arial but noone knows. So, if
fluxbox has no xft support the first fontpattern fails
and fluxbox tries the next one, which might be successful.
if everything fails, it will use "fixed"
- Added caching of fonts, fonts are only loaded once.
- Fixed #1090902 (slow utf8 start)
2005-06-03 07:25:48 +00:00
|
|
|
XftFont *newxftfont = XftFontOpenXlfd(disp, 0, name.c_str());
|
|
|
|
if (newxftfont == 0) {
|
|
|
|
newxftfont = XftFontOpenName(disp, 0, name.c_str());
|
2002-12-01 13:42:15 +00:00
|
|
|
if (newxftfont == 0)
|
|
|
|
return false;
|
|
|
|
}
|
- Usage of xft-fonts is prefered, except a font-description starts with '-'
- Removed "antialias"-option completly, to enable/disable "antialias"
use either <fontname>:antialias=<bool> in the style or use
Xft.antialias: <bool> in your .Xdefaults
- Added new styleresources:
*.font.effect: <halo|shadow>
*.font.shadow.x : <int> - shadow x offset
*.font.shadow.y : <int> - shadow y offset
*.font.shadow.color : <color> - color of shadow
*.font.halo.color : <color> - color of halo
- Removed 'shadow' and 'halo' options from fontdefinitions:
!! Style authors have to update their styles !!
- Simplified XmbFontImp to not try all possible fonts to match locale
- Style authors may specify multiple fonts:
<font1>|<font2>|<font3>
if loading of font1 fails, fluxbox probes <font2>, etc. The last font is
"fixed". Hints for style authors:
- if xft tries to load a font it will _ALWAYS_ return a valid font,
it doesnt have to look like the one you want to have, read more
about it: http://fontconfig.org/fontconfig-user.html
- export XFT_DEBUG=1 before running fluxbox helps to see
which fonts are picked.
eg:
*.font: Verdana,Arial-12:antialias=true|-artwiz-snap-*-
if fluxbox is compiled with xft this will NEVER try to
load "-artwiz-snap-*-" since xft gives for sure a font,
most likely Verdana or Arial but noone knows. So, if
fluxbox has no xft support the first fontpattern fails
and fluxbox tries the next one, which might be successful.
if everything fails, it will use "fixed"
- Added caching of fonts, fonts are only loaded once.
- Fixed #1090902 (slow utf8 start)
2005-06-03 07:25:48 +00:00
|
|
|
|
2006-03-26 04:02:30 +00:00
|
|
|
// destroy all old fonts and set new
|
|
|
|
for (int r = ROT0; r <= ROT270; r++)
|
|
|
|
if (m_xftfonts[r] != 0)
|
|
|
|
XftFontClose(App::instance()->display(), m_xftfonts[r]);
|
2002-12-01 13:42:15 +00:00
|
|
|
|
2006-03-26 04:02:30 +00:00
|
|
|
m_xftfonts[ROT0] = newxftfont;
|
|
|
|
m_name = name;
|
2002-12-01 13:42:15 +00:00
|
|
|
|
|
|
|
return true;
|
2002-11-26 16:01:28 +00:00
|
|
|
}
|
|
|
|
|
2006-03-26 04:02:30 +00:00
|
|
|
void XftFontImp::drawText(const FbDrawable &w, int screen, GC gc, const char *text, size_t len, int x, int y, FbTk::Orientation orient) const {
|
|
|
|
if (m_xftfonts[orient] == 0)
|
2002-12-01 13:42:15 +00:00
|
|
|
return;
|
2006-03-26 04:02:30 +00:00
|
|
|
|
|
|
|
// we adjust y slightly so that the baseline is in the right spot
|
|
|
|
// (it is offset one by rotation >=180 degrees)
|
|
|
|
switch (orient) {
|
|
|
|
case ROT0:
|
|
|
|
break;
|
|
|
|
case ROT90:
|
|
|
|
break;
|
|
|
|
case ROT180:
|
|
|
|
x+=1;
|
|
|
|
y+=1;
|
|
|
|
break;
|
|
|
|
case ROT270:
|
|
|
|
y+=1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
XftFont *font = m_xftfonts[orient];
|
|
|
|
|
2004-09-11 23:01:34 +00:00
|
|
|
XftDraw *draw = XftDrawCreate(w.display(),
|
|
|
|
w.drawable(),
|
|
|
|
DefaultVisual(w.display(), screen),
|
|
|
|
DefaultColormap(w.display(), screen));
|
2002-12-01 13:42:15 +00:00
|
|
|
|
|
|
|
XGCValues gc_val;
|
|
|
|
|
|
|
|
// get foreground pixel value and convert it to XRenderColor value
|
|
|
|
// TODO: we should probably check return status
|
2004-09-11 23:01:34 +00:00
|
|
|
XGetGCValues(w.display(), gc, GCForeground, &gc_val);
|
2002-12-01 13:42:15 +00:00
|
|
|
|
|
|
|
// get red, green, blue values
|
|
|
|
XColor xcol;
|
|
|
|
xcol.pixel = gc_val.foreground;
|
2004-09-11 23:01:34 +00:00
|
|
|
XQueryColor(w.display(), DefaultColormap(w.display(), screen), &xcol);
|
2002-12-01 13:42:15 +00:00
|
|
|
|
|
|
|
// convert xcolor to XftColor
|
|
|
|
XRenderColor rendcol;
|
|
|
|
rendcol.red = xcol.red;
|
|
|
|
rendcol.green = xcol.green;
|
|
|
|
rendcol.blue = xcol.blue;
|
|
|
|
rendcol.alpha = 0xFFFF;
|
2004-09-10 16:12:49 +00:00
|
|
|
XftColor xftcolor;
|
2004-09-11 23:01:34 +00:00
|
|
|
XftColorAllocValue(w.display(),
|
|
|
|
DefaultVisual(w.display(), screen),
|
|
|
|
DefaultColormap(w.display(), screen),
|
2002-12-01 13:42:15 +00:00
|
|
|
&rendcol, &xftcolor);
|
|
|
|
|
|
|
|
// draw string
|
2002-11-26 16:01:28 +00:00
|
|
|
#ifdef HAVE_XFT_UTF8_STRING
|
2002-12-01 13:42:15 +00:00
|
|
|
if (m_utf8mode) {
|
2004-08-10 11:57:35 +00:00
|
|
|
// check the string size,
|
|
|
|
// if the size is zero we use the XftDrawString8 function instead.
|
|
|
|
XGlyphInfo ginfo;
|
2004-09-11 23:01:34 +00:00
|
|
|
XftTextExtentsUtf8(w.display(),
|
2006-03-26 04:02:30 +00:00
|
|
|
m_xftfonts[ROT0],
|
2004-08-10 11:57:35 +00:00
|
|
|
(XftChar8 *)text, len,
|
|
|
|
&ginfo);
|
|
|
|
if (ginfo.xOff != 0) {
|
|
|
|
XftDrawStringUtf8(draw,
|
|
|
|
&xftcolor,
|
2006-03-26 04:02:30 +00:00
|
|
|
font,
|
2004-08-10 11:57:35 +00:00
|
|
|
x, y,
|
|
|
|
(XftChar8 *)(text), len);
|
2004-09-11 23:01:34 +00:00
|
|
|
XftColorFree(w.display(),
|
|
|
|
DefaultVisual(w.display(), screen),
|
|
|
|
DefaultColormap(w.display(), screen), &xftcolor);
|
2004-08-10 11:57:35 +00:00
|
|
|
XftDrawDestroy(draw);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // HAVE_XFT_UTF8_STRING
|
2004-09-10 16:12:49 +00:00
|
|
|
|
2004-08-10 11:57:35 +00:00
|
|
|
XftDrawString8(draw,
|
|
|
|
&xftcolor,
|
2006-03-26 04:02:30 +00:00
|
|
|
font,
|
2004-08-10 11:57:35 +00:00
|
|
|
x, y,
|
|
|
|
(XftChar8 *)(text), len);
|
|
|
|
|
2002-11-26 16:01:28 +00:00
|
|
|
|
2004-09-11 23:01:34 +00:00
|
|
|
XftColorFree(w.display(),
|
|
|
|
DefaultVisual(w.display(), screen),
|
|
|
|
DefaultColormap(w.display(), screen), &xftcolor);
|
2002-12-01 13:42:15 +00:00
|
|
|
XftDrawDestroy(draw);
|
2002-11-26 16:01:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int XftFontImp::textWidth(const char * const text, unsigned int len) const {
|
2006-03-26 04:02:30 +00:00
|
|
|
if (m_xftfonts[ROT0] == 0)
|
2002-12-01 13:42:15 +00:00
|
|
|
return 0;
|
2004-08-10 11:57:35 +00:00
|
|
|
|
2002-12-01 13:42:15 +00:00
|
|
|
XGlyphInfo ginfo;
|
2004-09-10 16:12:49 +00:00
|
|
|
Display* disp = App::instance()->display();
|
2004-08-10 11:57:35 +00:00
|
|
|
|
2006-03-26 04:02:30 +00:00
|
|
|
XftFont *font = m_xftfonts[ROT0];
|
|
|
|
|
|
|
|
|
2002-11-26 16:01:28 +00:00
|
|
|
#ifdef HAVE_XFT_UTF8_STRING
|
2002-12-01 13:42:15 +00:00
|
|
|
if (m_utf8mode) {
|
2004-09-10 16:12:49 +00:00
|
|
|
XftTextExtentsUtf8(disp,
|
2006-03-26 04:02:30 +00:00
|
|
|
font,
|
2002-12-01 13:42:15 +00:00
|
|
|
(XftChar8 *)text, len,
|
|
|
|
&ginfo);
|
2004-08-10 11:57:35 +00:00
|
|
|
if (ginfo.xOff != 0)
|
|
|
|
return ginfo.xOff;
|
|
|
|
|
|
|
|
// the utf8 failed, try normal extents
|
|
|
|
}
|
2002-11-26 16:01:28 +00:00
|
|
|
#endif //HAVE_XFT_UTF8_STRING
|
2004-08-10 11:57:35 +00:00
|
|
|
|
2004-09-10 16:12:49 +00:00
|
|
|
XftTextExtents8(disp,
|
2006-03-26 04:02:30 +00:00
|
|
|
font,
|
2004-08-10 11:57:35 +00:00
|
|
|
(XftChar8 *)text, len,
|
|
|
|
&ginfo);
|
|
|
|
|
2002-12-01 13:42:15 +00:00
|
|
|
return ginfo.xOff;
|
2002-11-26 16:01:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int XftFontImp::height() const {
|
2006-03-26 04:02:30 +00:00
|
|
|
if (m_xftfonts[ROT0] == 0)
|
2002-12-01 13:42:15 +00:00
|
|
|
return 0;
|
2006-03-26 04:02:30 +00:00
|
|
|
else
|
|
|
|
return m_xftfonts[ROT0]->height;
|
2004-08-29 08:33:13 +00:00
|
|
|
//m_xftfont->ascent + m_xftfont->descent;
|
|
|
|
// curiously, fonts seem to have a smaller height, but the "height"
|
|
|
|
// is specified within the actual font, so it must be right, right?
|
2002-11-26 16:01:28 +00:00
|
|
|
}
|
|
|
|
|
2006-03-26 04:02:30 +00:00
|
|
|
bool XftFontImp::validOrientation(FbTk::Orientation orient) {
|
|
|
|
if (orient == ROT0 || m_xftfonts[orient])
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (m_xftfonts[ROT0] == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// otherwise, try to load that orientation
|
|
|
|
// radians is actually anti-clockwise, so we reverse it
|
|
|
|
double radians = -(orient) * 90 * M_PI / 180;
|
|
|
|
|
|
|
|
XftMatrix matrix;
|
|
|
|
XftMatrixInit(&matrix);
|
|
|
|
XftMatrixRotate(&matrix, cos(radians), sin(radians));
|
|
|
|
|
|
|
|
Display *disp = App::instance()->display();
|
|
|
|
|
|
|
|
XftPattern * pattern = XftNameParse(m_name.c_str());
|
|
|
|
XftPatternAddMatrix(pattern, XFT_MATRIX, &matrix);
|
|
|
|
XftResult result;
|
|
|
|
XftPattern * foundpat = XftFontMatch(disp, 0, pattern, &result);
|
|
|
|
XftPatternDestroy(pattern);
|
|
|
|
XftFont * new_font = XftFontOpenPattern(disp, foundpat);
|
|
|
|
|
|
|
|
if (new_font == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_xftfonts[orient] = new_font;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-11-26 16:01:28 +00:00
|
|
|
}; // end namespace FbTk
|