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

font_bitmap.cpp

Go to the documentation of this file.
00001 /*
00002         $Id: font_bitmap.cpp,v 1.1 2001/03/06 15:09:20 mbn Exp $
00003 
00004         ------------------------------------------------------------------------
00005         ClanLib, the platform independent game SDK.
00006 
00007         This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
00008         version 2. See COPYING for details.
00009 
00010         For a total list of contributers see CREDITS.
00011 
00012         ------------------------------------------------------------------------
00013 
00014         File purpose:
00015                 Bitmap font class.
00016 
00017 */
00018 
00019 #include "Core/precomp.h"
00020 #include <API/Display/Font/font.h>
00021 #include <API/Display/Font/font_description.h>
00022 #include <API/Display/Display/surfaceprovider.h>
00023 #include <API/Display/Display/surface.h>
00024 
00025 #include "font_bitmap.h"
00026 
00027 CL_Font_Bitmap::CL_Font_Bitmap(CL_Font_Description *font_desc)
00028         : CL_Font_Generic()
00029 {
00030         space_len = font_desc->space_len;
00031         subtract_width = font_desc->subtract_width;
00032 
00033         int len = font_desc->letters.length();
00034         for (int j=0; j<256; j++)
00035                 chars[j] = NULL;
00036 
00037         font_desc->lock();
00038         for (int i=0; i<len; i++)
00039         {
00040                 if (i == 0)
00041                         height = font_desc->letter_providers[i]->get_height();
00042         
00043                 chars[(unsigned char) font_desc->letters[i]] = CL_Surface::create(
00044                         font_desc->letter_providers[i],
00045                         false);
00046         }
00047         font_desc->unlock();
00048 }
00049 
00050 CL_Font_Bitmap::~CL_Font_Bitmap()
00051 {
00052         for (int i=0; i<256; i++)
00053                 if (chars[i]!=NULL)
00054                         delete chars[i];
00055 }
00056 
00057 int CL_Font_Bitmap::get_height()
00058 {
00059         return height;
00060 }
00061 
00062 int CL_Font_Bitmap::get_text_width(const char *_text)
00063 {
00064         int len = 0;
00065 
00066         unsigned char *text = (unsigned char *) _text;
00067 
00068         while (*text != 0)
00069         {
00070                 if (chars[*text] != NULL)
00071                         len += chars[*text]->get_width()-subtract_width;
00072                 else
00073                         len += space_len;
00074 
00075                 text++;
00076         }
00077 
00078         return len;
00079 }
00080 
00081 int CL_Font_Bitmap::get_char_width(const char _character)
00082 {
00083         unsigned char character = (unsigned char) _character;
00084         
00085         if (chars[character] != NULL)
00086         {
00087                 return chars[character]->get_width()-subtract_width;
00088         }
00089         else
00090         {
00091                 return space_len;
00092         }
00093 }
00094 
00095 void CL_Font_Bitmap::print_left(int x, int y, const char *_text, int n_height)
00096 {
00097         int pos_x = x;
00098         int pos_y = y;
00099 
00100         unsigned char *text = (unsigned char *) _text;
00101 
00102         while (*text != 0)
00103         {
00104                 // new line on '\n'
00105                 if( *text == '\n' )
00106                 {
00107                         pos_x = x;
00108                         pos_y += (get_height() + n_height);
00109                         text++;
00110                 }
00111         
00112                 if (chars[*text] != NULL)
00113                 {
00114                         int width = chars[*text]->get_width()-subtract_width;
00115 
00116                         chars[*text]->put_screen(pos_x, pos_y);
00117 
00118                         pos_x += width;
00119                 }
00120                 else
00121                 {
00122                         pos_x += space_len;
00123                 }
00124                 text++;
00125         }
00126 }
00127 
00128 void CL_Font_Bitmap::print_left(int x, int y, float sx, float sy, const char *_text)
00129 {
00130         int pos_x = x;
00131 
00132         unsigned char *text = (unsigned char *) _text;
00133 
00134         while (*text != 0)
00135         {
00136                 if (chars[*text] != NULL)
00137                 {
00138                         int width = (int) ((chars[*text]->get_width()-subtract_width)*sx);
00139 
00140                         chars[*text]->put_screen(pos_x, y, sx, sy);
00141 
00142                         pos_x += width;
00143                 }
00144                 else
00145                 {
00146                         pos_x += (int) (space_len*sx);
00147                 }
00148 
00149                 text++;
00150         }
00151 }
00152 
00153 void CL_Font_Bitmap::print_center(int x, int y, const char *text)
00154 {
00155         print_left(x - get_text_width(text)/2, y, text);
00156 }
00157 
00158 void CL_Font_Bitmap::print_right(int x, int y, const char *text)
00159 {
00160         print_left(x - get_text_width(text), y, text);
00161 }
00162 
00163 void CL_Font_Bitmap::put_target(int x, int y, const char *_text, CL_Target *target, int alignment)
00164 {
00165         int pos_x;
00166         unsigned char *text = (unsigned char *) _text;
00167                 
00168         switch(alignment)
00169         {
00170                 case CL_Font::ALIGN_CENTER:
00171                         pos_x = x - get_text_width(_text)/2;
00172                         break;
00173                 case CL_Font::ALIGN_RIGHT:
00174                         pos_x = x - get_text_width(_text);
00175                         break;
00176                 default:
00177                         pos_x = x;
00178         }       
00179         
00180         while (*text != 0)
00181         {
00182                 if (chars[*text] != NULL)
00183                 {
00184                         int width = chars[*text]->get_width()-subtract_width;
00185 
00186                         chars[*text]->put_target(pos_x, y, 0, target);
00187 
00188                         pos_x += width;
00189                 }
00190                 else
00191                 {
00192                         pos_x += space_len;
00193                 }
00194 
00195                 text++;
00196         }
00197 }
00198 

Generated at Wed Apr 4 19:54:00 2001 for ClanLib by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001