Back to index

extremetuxracer  0.5beta
FTCharToGlyphIndexMap.h
Go to the documentation of this file.
00001 #ifndef    __FTCharToGlyphIndexMap__
00002 #define    __FTCharToGlyphIndexMap__
00003 
00004 #include <stdlib.h>
00005 
00006 #include "FTGL.h"
00007 
00027 class FTGL_EXPORT FTCharToGlyphIndexMap
00028 {
00029     public:
00030   
00031         typedef unsigned long CharacterCode;
00032         typedef signed long GlyphIndex;
00033         
00034         enum 
00035         {
00036             NumberOfBuckets = 256,
00037             BucketSize = 256,
00038             IndexNotFound = -1
00039         };
00040 
00041         FTCharToGlyphIndexMap()
00042         {
00043             this->Indices = 0;
00044         }
00045 
00046         virtual ~FTCharToGlyphIndexMap()
00047         {
00048             if( this->Indices)
00049             {
00050                 // Free all buckets
00051                 this->clear();
00052         
00053                 // Free main structure
00054                 delete [] this->Indices;
00055                 this->Indices = 0;
00056             }
00057         }
00058   
00059         void clear()
00060         {
00061             if(this->Indices)
00062             {
00063                 for( int i = 0; i < FTCharToGlyphIndexMap::NumberOfBuckets; i++)
00064                 {
00065                     if( this->Indices[i])
00066                     {
00067                         delete [] this->Indices[i];
00068                         this->Indices[i] = 0;
00069                     }
00070                 }
00071             }
00072         }
00073 
00074         const GlyphIndex find( CharacterCode c)
00075         {
00076             if( !this->Indices)
00077             {
00078                 return 0;
00079             }
00080         
00081             // Find position of char code in buckets
00082             div_t pos = div( c, FTCharToGlyphIndexMap::BucketSize);
00083         
00084             if( !this->Indices[pos.quot])
00085             {
00086                 return 0;
00087             }
00088         
00089             const FTCharToGlyphIndexMap::GlyphIndex *ptr = &this->Indices[pos.quot][pos.rem];
00090             if( *ptr == FTCharToGlyphIndexMap::IndexNotFound)
00091             {
00092                 return 0;
00093             }
00094         
00095             return *ptr;
00096         }
00097 
00098         void insert( CharacterCode c, GlyphIndex g)
00099         {
00100             if( !this->Indices)
00101             {
00102                 this->Indices = new GlyphIndex* [FTCharToGlyphIndexMap::NumberOfBuckets];
00103                 for( int i = 0; i < FTCharToGlyphIndexMap::NumberOfBuckets; i++)
00104                 {
00105                     this->Indices[i] = 0;
00106                 }
00107             }
00108         
00109             // Find position of char code in buckets
00110             div_t pos = div(c, FTCharToGlyphIndexMap::BucketSize);
00111         
00112             // Allocate bucket if does not exist yet
00113             if( !this->Indices[pos.quot])
00114             {
00115                 this->Indices[pos.quot] = new GlyphIndex [FTCharToGlyphIndexMap::BucketSize];
00116                 for( int i = 0; i < FTCharToGlyphIndexMap::BucketSize; i++)
00117                 {
00118                     this->Indices[pos.quot][i] = FTCharToGlyphIndexMap::IndexNotFound;
00119                 }
00120             }
00121           
00122             this->Indices[pos.quot][pos.rem] = g;
00123         }
00124   
00125     private:
00126         GlyphIndex** Indices;
00127 };
00128 
00129 
00130 #endif  //  __FTCharToGlyphIndexMap__