Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

/otk/font.cc

Go to the documentation of this file.
00001 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
00002 
00003 #ifdef    HAVE_CONFIG_H
00004 #  include "../config.h"
00005 #endif // HAVE_CONFIG_H
00006 
00007 extern "C" {
00008 #ifdef HAVE_STDLIB_H
00009 #  include <stdlib.h>
00010 #endif // HAVE_STDLIB_H
00011 }
00012 
00013 #include <iostream>
00014 #include <algorithm>
00015 
00016 #include "font.hh"
00017 #include "surface.hh"
00018 #include "util.hh"
00019 #include "display.hh"
00020 #include "screeninfo.hh"
00021 
00022 extern "C" {
00023 #ifdef    HAVE_STDIO_H
00024 #  include <stdio.h>
00025 #endif // HAVE_STDIO_H
00026 
00027 #include "../src/gettext.h"
00028 #define _(str) gettext(str)
00029 }
00030 
00031 namespace otk {
00032 
00033 std::string Font::_fallback_font = "fixed";
00034 bool        Font::_xft_init      = false;
00035 
00036 Font::Font(int screen_num, const std::string &fontstring,
00037              bool shadow, unsigned char offset, unsigned char tint)
00038   : _screen_num(screen_num),
00039     _fontstring(fontstring),
00040     _shadow(shadow),
00041     _offset(offset),
00042     _tint(tint),
00043     _xftfont(0)
00044 {
00045   assert(screen_num >= 0);
00046   assert(tint <= CHAR_MAX);
00047   
00048   if (!_xft_init) {
00049     if (!XftInit(0)) {
00050       printf(_("Couldn't initialize Xft.\n\n"));
00051       ::exit(3);
00052     }
00053     int version = XftGetVersion();
00054     printf(_("Using Xft %d.%d.%d (Built against %d.%d.%d).\n"),
00055            version / 10000 % 100, version / 100 % 100, version % 100,
00056            XFT_MAJOR, XFT_MINOR, XFT_REVISION);
00057     _xft_init = true;
00058   }
00059 
00060   if ((_xftfont = XftFontOpenName(**display, _screen_num,
00061                                   _fontstring.c_str())))
00062     return;
00063 
00064   printf(_("Unable to load font: %s\n"), _fontstring.c_str());
00065   printf(_("Trying fallback font: %s\n"), _fallback_font.c_str());
00066 
00067   if ((_xftfont = XftFontOpenName(**display, _screen_num,
00068                                   _fallback_font.c_str())))
00069     return;
00070 
00071   printf(_("Unable to load font: %s\n"), _fallback_font.c_str());
00072   printf(_("Aborting!.\n"));
00073 
00074   ::exit(3); // can't continue without a font
00075 }
00076 
00077 
00078 Font::~Font(void)
00079 {
00080   if (_xftfont)
00081     XftFontClose(**display, _xftfont);
00082 }
00083 
00084 
00085 int Font::measureString(const ustring &string) const
00086 {
00087   XGlyphInfo info;
00088 
00089   if (string.utf8())
00090     XftTextExtentsUtf8(**display, _xftfont,
00091                        (FcChar8*)string.c_str(), string.bytes(), &info);
00092   else
00093     XftTextExtents8(**display, _xftfont,
00094                     (FcChar8*)string.c_str(), string.bytes(), &info);
00095 
00096   return (signed) info.xOff + (_shadow ? _offset : 0);
00097 }
00098 
00099 
00100 int Font::height(void) const
00101 {
00102   return (signed) _xftfont->height + (_shadow ? _offset : 0);
00103 }
00104 
00105 
00106 int Font::maxCharWidth(void) const
00107 {
00108   return (signed) _xftfont->max_advance_width;
00109 }
00110 
00111 }

Generated on Tue Feb 4 22:58:56 2003 for Openbox by doxygen1.3-rc2