fluxbox/src/FbTk/Font.cc

235 lines
6.8 KiB
C++
Raw Normal View History

2002-11-26 16:01:28 +00:00
// Font.cc
// Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org)
//
// 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.
2003-11-28 23:27:29 +00:00
//$Id: Font.cc,v 1.5 2003/11/28 23:27:29 fluxgen Exp $
2002-11-26 16:01:28 +00:00
#include "Font.hh"
#include "FontImp.hh"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif // HAVE_CONFIG_H
// for antialias
#ifdef USE_XFT
#include "XftFontImp.hh"
#endif // USE_XFT
// for multibyte support
#ifdef USE_XMB
#include "XmbFontImp.hh"
#endif //USE_XMB
// standard font system
#include "XFontImp.hh"
2003-11-28 23:27:29 +00:00
#include "GContext.hh"
2002-11-26 16:01:28 +00:00
//use gnu extensions
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif //_GNU_SOURCE
#ifndef __USE_GNU
#define __USE_GNU
#endif //__USE_GNU
#include <iostream>
#include <cstring>
#include <cstdlib>
2003-01-05 23:00:19 +00:00
#include <typeinfo>
2002-11-26 16:01:28 +00:00
using namespace std;
#ifdef HAVE_SETLOCALE
#include <locale.h>
#endif //HAVE_SETLOCALE
namespace FbTk {
bool Font::m_multibyte = false;
bool Font::m_utf8mode = false;
Font::Font(const char *name, bool antialias):
2002-12-01 13:42:15 +00:00
m_fontimp(0),
2003-11-28 23:27:29 +00:00
m_antialias(false), m_rotated(false), m_shadow(false) {
2002-11-26 16:01:28 +00:00
2002-12-01 13:42:15 +00:00
// MB_CUR_MAX returns the size of a char in the current locale
if (MB_CUR_MAX > 1) // more than one byte, then we're multibyte
m_multibyte = true;
char *s; // temporary string for enviroment variable
// check for utf-8 mode
if (((s = getenv("LC_ALL")) && *s) ||
((s = getenv("LC_CTYPE")) && *s) ||
((s = getenv("LANG")) && *s)) {
if (strstr(s, "UTF-8"))
m_utf8mode = true;
}
// create the right font implementation
// antialias is prio 1
2002-11-26 16:01:28 +00:00
#ifdef USE_XFT
2002-12-01 13:42:15 +00:00
if (antialias) {
m_fontimp.reset(new XftFontImp(0, m_utf8mode));
m_antialias = true;
}
2002-11-26 16:01:28 +00:00
#endif //USE_XFT
2002-12-01 13:42:15 +00:00
// if we didn't create a Xft font then create basic font
if (m_fontimp.get() == 0) {
2002-11-26 16:01:28 +00:00
#ifdef USE_XMB
2002-12-01 13:42:15 +00:00
if (m_multibyte || m_utf8mode)
m_fontimp.reset(new XmbFontImp(0, m_utf8mode));
else // basic font implementation
2002-11-26 16:01:28 +00:00
#endif // USE_XMB
2002-12-01 13:42:15 +00:00
m_fontimp.reset(new XFontImp());
}
2002-11-26 16:01:28 +00:00
2002-12-01 13:42:15 +00:00
if (name != 0) {
load(name);
}
2002-11-26 16:01:28 +00:00
}
Font::~Font() {
}
void Font::setAntialias(bool flag) {
2002-12-01 13:42:15 +00:00
bool loaded = m_fontimp->loaded();
2002-11-26 16:01:28 +00:00
#ifdef USE_XFT
2002-12-01 13:42:15 +00:00
if (flag && !isAntialias() && !m_rotated) {
m_fontimp.reset(new XftFontImp(m_fontstr.c_str(), m_utf8mode));
} else if (!flag && isAntialias())
2002-11-26 16:01:28 +00:00
#endif // USE_XFT
{
#ifdef USE_XMB
2002-12-01 13:42:15 +00:00
if (m_multibyte || m_utf8mode)
m_fontimp.reset(new XmbFontImp(m_fontstr.c_str(), m_utf8mode));
else
2002-11-26 16:01:28 +00:00
#endif // USE_XMB
2002-12-01 13:42:15 +00:00
m_fontimp.reset(new XFontImp(m_fontstr.c_str()));
2002-11-26 16:01:28 +00:00
}
2002-12-01 13:42:15 +00:00
if (m_fontimp->loaded() != loaded) { // if the new font failed to load, fall back to 'fixed'
if (!m_fontimp->load("fixed")) // if that failes too, output warning
cerr<<"Warning: can't load fallback font 'fixed'."<<endl;
}
2002-11-26 16:01:28 +00:00
2002-12-01 13:42:15 +00:00
m_antialias = flag;
2002-11-26 16:01:28 +00:00
}
2003-11-28 23:27:29 +00:00
bool Font::load(const std::string &name) {
if (name.size() == 0)
2002-12-01 13:42:15 +00:00
return false;
2003-11-28 23:27:29 +00:00
// find font option "shadow"
m_shadow = false;
size_t start_pos = name.find_first_of(":");
if (start_pos != std::string::npos) {
if (name.find_first_of("shadow", start_pos) != std::string::npos)
m_shadow = true;
}
2002-12-01 13:42:15 +00:00
m_fontstr = name;
2003-11-28 23:27:29 +00:00
return m_fontimp->load(name.c_str());
2002-11-26 16:01:28 +00:00
}
unsigned int Font::textWidth(const char * const text, unsigned int size) const {
2002-12-01 13:42:15 +00:00
return m_fontimp->textWidth(text, size);
2002-11-26 16:01:28 +00:00
}
unsigned int Font::height() const {
2002-12-01 13:42:15 +00:00
return m_fontimp->height();
2002-11-26 16:01:28 +00:00
}
int Font::ascent() const {
2002-12-01 13:42:15 +00:00
return m_fontimp->ascent();
2002-11-26 16:01:28 +00:00
}
int Font::descent() const {
2002-12-01 13:42:15 +00:00
return m_fontimp->descent();
2002-11-26 16:01:28 +00:00
}
2003-11-28 23:27:29 +00:00
void Font::drawText(Drawable w, int screen, GC gc,
const char *text, size_t len, int x, int y,
2002-12-08 19:12:07 +00:00
bool rotate) const {
2002-12-01 13:42:15 +00:00
if (text == 0 || len == 0)
return;
2003-11-28 23:27:29 +00:00
// so we don't end up in a loop with m_shadow
static bool first_run = true;
// draw shadow first
if (first_run && m_shadow) {
FbTk::GContext shadow_gc(w);
shadow_gc.setForeground(FbTk::Color("black", screen));
first_run = false; // so we don't end up in a loop
drawText(w, screen, shadow_gc.gc(), text, len, x + 1, y + 1);
first_run = true;
}
2002-12-08 19:12:07 +00:00
if (!rotate && isRotated()) {
// if this was called with request to not rotated the text
// we just forward it to the implementation that handles rotation
// currently just XFontImp
// Using dynamic_cast just temporarly until there's a better solution
// to put in FontImp
try {
XFontImp *font = dynamic_cast<XFontImp *>(m_fontimp.get());
font->setRotate(false); // disable rotation temporarly
2003-11-28 23:27:29 +00:00
2002-12-08 19:12:07 +00:00
font->drawText(w, screen, gc, text, len, x, y);
font->setRotate(true); // enable rotation
} catch (std::bad_cast &bc) {
// draw normal...
m_fontimp->drawText(w, screen, gc, text, len, x, y);
}
} else
m_fontimp->drawText(w, screen, gc, text, len, x, y);
2003-11-28 23:27:29 +00:00
2002-11-26 16:01:28 +00:00
}
void Font::rotate(float angle) {
#ifdef USE_XFT
2002-12-01 13:42:15 +00:00
// if we are rotated and we are changing to horiz text
// and we were antialiased before we rotated then change to XftFontImp
if (isRotated() && angle == 0 && isAntialias())
m_fontimp.reset(new XftFontImp(m_fontstr.c_str(), m_utf8mode));
2002-11-26 16:01:28 +00:00
#endif // USE_XFT
2002-12-01 13:42:15 +00:00
// change to a font imp that handles rotated fonts (i.e just XFontImp at the moment)
// if we're going to rotate this font
if (angle != 0 && isAntialias() && !isRotated()) {
m_fontimp.reset(new XFontImp(m_fontstr.c_str()));
2002-12-08 19:12:07 +00:00
if (!m_fontimp->loaded()) // if it failed to load font, try default font fixed
m_fontimp->load("fixed");
2002-12-01 13:42:15 +00:00
}
2002-11-26 16:01:28 +00:00
2002-12-01 13:42:15 +00:00
//Note: only XFontImp implements FontImp::rotate
m_fontimp->rotate(angle);
2002-11-26 16:01:28 +00:00
2002-12-01 13:42:15 +00:00
m_rotated = (angle == 0 ? false : true);
2002-12-08 19:12:07 +00:00
m_angle = angle;
2002-11-26 16:01:28 +00:00
}
};