Logo
  • Main Page
  • Related Pages
  • Modules
  • Classes
  • Files

mmsfbfont.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2007 Stefan Schwarzer, Jens Schneider,             *
00003  *                           Matthias Hardt, Guido Madaus                  *
00004  *                                                                         *
00005  *   Copyright (C) 2007-2008 BerLinux Solutions GbR                        *
00006  *                           Stefan Schwarzer & Guido Madaus               *
00007  *                                                                         *
00008  *   Copyright (C) 2009-2013 BerLinux Solutions GmbH                       *
00009  *                                                                         *
00010  *   Authors:                                                              *
00011  *      Stefan Schwarzer   <stefan.schwarzer@diskohq.org>,                 *
00012  *      Matthias Hardt     <matthias.hardt@diskohq.org>,                   *
00013  *      Jens Schneider     <jens.schneider@diskohq.org>,                   *
00014  *      Guido Madaus       <guido.madaus@diskohq.org>,                     *
00015  *      Patrick Helterhoff <patrick.helterhoff@diskohq.org>,               *
00016  *      René Bählkow       <rene.baehlkow@diskohq.org>                     *
00017  *                                                                         *
00018  *   This library is free software; you can redistribute it and/or         *
00019  *   modify it under the terms of the GNU Lesser General Public            *
00020  *   License version 2.1 as published by the Free Software Foundation.     *
00021  *                                                                         *
00022  *   This library is distributed in the hope that it will be useful,       *
00023  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00024  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00025  *   Lesser General Public License for more details.                       *
00026  *                                                                         *
00027  *   You should have received a copy of the GNU Lesser General Public      *
00028  *   License along with this library; if not, write to the                 *
00029  *   Free Software Foundation, Inc.,                                       *
00030  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
00031  **************************************************************************/
00032 
00033 #ifndef MMSFBFONT_H_
00034 #define MMSFBFONT_H_
00035 
00036 #include "mmsgui/fb/mmsfbbase.h"
00037 #include "mmsgui/fb/mmsfbbuffer.h"
00038 
00039 #ifdef __HAVE_GLU__
00040 #define MMSFBFONT_GLYPH_MAX_MESHES  512
00041 #endif
00042 
00043 //! descibes a loaded glyph
00044 typedef struct {
00045     //! character code
00046     unsigned int    character;
00047     //! pointer to bitmap data
00048     unsigned char   *buffer;
00049     //! pitch in byte of one row in the bitmap buffer
00050     int             pitch;
00051     //! x-offset
00052     int             left;
00053     //! y-offset
00054     int             top;
00055     //! width in pixel of the glyph
00056     int             width;
00057     //! height in pixel of the glyph
00058     int             height;
00059     //! width in pixel of the whole character
00060     int             advanceX;
00061 #ifdef __HAVE_OPENGL__
00062 #ifndef __HAVE_GLU__
00063     //! OpenGL texture for this glyph, we use bitmaps from freetype
00064     //! note: text rendering based on textures needs a lot of memory and is not the fastest way
00065     GLuint  texture;
00066 #else
00067     //! OpenGL primitives for this glyph, we convert outlines from freetype using GLU tesselator
00068     //! note: text rendering based on primitives can be more than two times faster
00069     MMSFBBuffer *meshes;
00070     MMSFBBuffer *outline;
00071 #endif
00072 #endif
00073 } MMSFBFont_Glyph;
00074 
00075 //! Font rendering class.
00076 /*!
00077 \author Jens Schneider
00078 */
00079 class MMSFBFont {
00080     private:
00081         //! true if initialized
00082         bool        initialized;
00083 
00084         //! to make it thread-safe
00085         MMSMutex    Lock;
00086 
00087 #ifdef __HAVE_DIRECTFB__
00088         //! pointer to the directfb font
00089         void        *dfbfont;
00090 #endif
00091 
00092         //! static pointer to the freetype library
00093         static void *ft_library;
00094 
00095         //! pointer to the loaded freetype face
00096         void    *ft_face;
00097 
00098         //! font file
00099         string  filename;
00100 
00101         //! defines mapping between filename and font id
00102         typedef std::map<std::string, unsigned int> MMSFBFONT_MAP;
00103 
00104         //! static index
00105         static MMSFBFONT_MAP index;
00106 
00107         //! static index position
00108         static unsigned int index_pos;
00109 
00110         //! id of font
00111         unsigned int font_id;
00112 
00113 #if (defined(__HAVE_OPENGL__) && defined(__HAVE_GLU__))
00114         //! scale coefficient
00115         float   scale_coeff;
00116 #endif
00117 
00118         //! ascender
00119         int     ascender;
00120 
00121         //! descender
00122         int     descender;
00123 
00124         //! real height of one line
00125         int     height;
00126 
00127         //! maps a character id to a already loaded glyph (see glyphpool)
00128         std::map<unsigned int, MMSFBFont_Glyph> charmap;
00129 
00130         //! reference counter for final release of freetype library
00131         static unsigned int numReferences;
00132 
00133         void lock();
00134         void unlock();
00135 
00136         void *loadFTGlyph(unsigned int character);
00137         bool setupFTGlyph(unsigned int character, void *ftg, MMSFBFont_Glyph *glyph);
00138 
00139     public:
00140         MMSFBFont(string filename, int w, int h);
00141         virtual ~MMSFBFont();
00142 
00143         bool isInitialized();
00144 
00145         bool getStringWidth(string text, int len, int *width);
00146         bool getHeight(int *height);
00147 
00148         bool getAscender(int *ascender);
00149         bool getDescender(int *descender);
00150 
00151         bool getScaleCoeff(float *scale_coeff);
00152 
00153         bool getGlyph(unsigned int character, MMSFBFont_Glyph *glyph);
00154 
00155         friend class MMSFBSurface;
00156         friend class MMSFBBackEndInterface;
00157 };
00158 
00159 #define MMSFBFONT_GET_UNICODE_CHAR(text, len) \
00160     for (int cnt = 0; cnt < len; cnt++) { \
00161         unsigned char c = text[cnt]; \
00162         unsigned int character; \
00163         if(c >= 0xf0) /* 11110000 -> 4 bytes */ { \
00164             if(len < (cnt + 3)) { DEBUGMSG("MMSFBFONT", "invalid unicode string"); break; } \
00165             character = (unsigned int)((c & 0x07 /* 00000111 */) << 18); \
00166             character |= ((text[++cnt] & 0x3f /* 00111111 */) << 12); \
00167             character |= ((text[++cnt] & 0x3f) << 6); \
00168             character |= (text[++cnt] & 0x3f); \
00169         } else if(c >= 0xe0)  /* 11100000 -> 3 bytes */ { \
00170             if(len < (cnt + 2)) { DEBUGMSG("MMSFBFONT", "invalid unicode string"); break; } \
00171             character = (unsigned int)((c & 0x0f /* 00001111 */) << 12); \
00172             character |= ((text[++cnt] & 0x3f) << 6); \
00173             character |= (text[++cnt] & 0x3f); \
00174         } else if(c >= 0xc0)  /* 11000000 -> 2 bytes */ { \
00175             if(len < (cnt + 1)) { DEBUGMSG("MMSFBFONT", "invalid unicode string"); break; } \
00176             character = (unsigned int)(((c & 0x1f /* 00011111 */) << 6) | (text[++cnt] & 0x3f)); \
00177         } else  /* 1 byte */ { \
00178             character = (unsigned int)c; \
00179         }
00180 
00181 #endif /*MMSFBFONT_H_*/

Generated by doxygen