00001 #ifndef __FTFont__ 00002 #define __FTFont__ 00003 00004 #include <ft2build.h> 00005 #include FT_FREETYPE_H 00006 00007 #include "FTFace.h" 00008 #include "FTGL.h" 00009 00010 class FTGlyphContainer; 00011 class FTGlyph; 00012 00013 00014 /** 00015 * FTFont is the public interface for the FTGL library. 00016 * 00017 * Specific font classes are derived from this class. It uses the helper 00018 * classes FTFace and FTSize to access the Freetype library. This class 00019 * is abstract and deriving classes must implement the protected 00020 * <code>MakeGlyph</code> function to create glyphs of the 00021 * appropriate type. 00022 * 00023 * It is good practice after using these functions to test the error 00024 * code returned. <code>FT_Error Error()</code>. Check the freetype file fterrdef.h 00025 * for error definitions. 00026 * 00027 * @see FTFace 00028 * @see FTSize 00029 * @see FTGlyphContainer 00030 * @see FTGlyph 00031 */ 00032 class FTGL_EXPORT FTFont 00033 { 00034 public: 00035 /** 00036 * Open and read a font file. Sets Error flag. 00037 * 00038 * @param fontFilePath font file path. 00039 */ 00040 FTFont( const char* fontFilePath); 00041 00042 /** 00043 * Open and read a font from a buffer in memory. Sets Error flag. 00044 * The buffer is owned by the client and is NOT copied by FTGL. The 00045 * pointer must be valid while using FTGL. 00046 * 00047 * @param pBufferBytes the in-memory buffer 00048 * @param bufferSizeInBytes the length of the buffer in bytes 00049 */ 00050 FTFont( const unsigned char *pBufferBytes, size_t bufferSizeInBytes); 00051 00052 /** 00053 * Destructor 00054 */ 00055 virtual ~FTFont(); 00056 00057 /** 00058 * Attach auxilliary file to font e.g font metrics. 00059 * 00060 * Note: not all font formats implement this function. 00061 * 00062 * @param fontFilePath auxilliary font file path. 00063 * @return <code>true</code> if file has been attached 00064 * successfully. 00065 */ 00066 bool Attach( const char* fontFilePath); 00067 00068 /** 00069 * Attach auxilliary data to font e.g font metrics, from memory 00070 * 00071 * Note: not all font formats implement this function. 00072 * 00073 * @param pBufferBytes the in-memory buffer 00074 * @param bufferSizeInBytes the length of the buffer in bytes 00075 * @return <code>true</code> if file has been attached 00076 * successfully. 00077 */ 00078 bool Attach( const unsigned char *pBufferBytes, size_t bufferSizeInBytes); 00079 00080 /** 00081 * Set the character map for the face. 00082 * 00083 * @param encoding Freetype enumerate for char map code. 00084 * @return <code>true</code> if charmap was valid and 00085 * set correctly 00086 */ 00087 bool CharMap( FT_Encoding encoding ); 00088 00089 /** 00090 * Get the number of character maps in this face. 00091 * 00092 * @return character map count. 00093 */ 00094 unsigned int CharMapCount(); 00095 00096 /** 00097 * Get a list of character maps in this face. 00098 * 00099 * @return pointer to the first encoding. 00100 */ 00101 FT_Encoding* CharMapList(); 00102 00103 /** 00104 * Set the char size for the current face. 00105 * 00106 * @param size the face size in points (1/72 inch) 00107 * @param res the resolution of the target device. 00108 * @return <code>true</code> if size was set correctly 00109 */ 00110 virtual bool FaceSize( const unsigned int size, const unsigned int res = 72); 00111 00112 /** 00113 * Get the current face size in points. 00114 * 00115 * @return face size 00116 */ 00117 unsigned int FaceSize() const; 00118 00119 /** 00120 * Set the extrusion distance for the font. Only implemented by 00121 * FTGLExtrdFont 00122 * 00123 * @param depth The extrusion distance. 00124 */ 00125 virtual void Depth( float /*depth*/){} 00126 00127 /** 00128 * Enable or disable the use of Display Lists inside FTGL 00129 * 00130 * @param useList <code>true</code> turns ON display lists. 00131 * <code>false</code> turns OFF display lists. 00132 */ 00133 void UseDisplayList( bool useList); 00134 00135 /** 00136 * Get the global ascender height for the face. 00137 * 00138 * @return Ascender height 00139 */ 00140 float Ascender() const; 00141 00142 /** 00143 * Gets the global descender height for the face. 00144 * 00145 * @return Descender height 00146 */ 00147 float Descender() const; 00148 00149 /** 00150 * Gets the line spacing for the font. 00151 * 00152 * @return Line height 00153 */ 00154 float LineHeight() const; 00155 00156 /** 00157 * Get the bounding box for a string. 00158 * 00159 * @param string a char string 00160 * @param llx lower left near x coord 00161 * @param lly lower left near y coord 00162 * @param llz lower left near z coord 00163 * @param urx upper right far x coord 00164 * @param ury upper right far y coord 00165 * @param urz upper right far z coord 00166 */ 00167 void BBox( const char* string, float& llx, float& lly, float& llz, float& urx, float& ury, float& urz); 00168 00169 /** 00170 * Get the bounding box for a string. 00171 * 00172 * @param string a wchar_t string 00173 * @param llx lower left near x coord 00174 * @param lly lower left near y coord 00175 * @param llz lower left near z coord 00176 * @param urx upper right far x coord 00177 * @param ury upper right far y coord 00178 * @param urz upper right far z coord 00179 */ 00180 void BBox( const wchar_t* string, float& llx, float& lly, float& llz, float& urx, float& ury, float& urz); 00181 00182 /** 00183 * Get the advance width for a string. 00184 * 00185 * @param string a wchar_t string 00186 * @return advance width 00187 */ 00188 float Advance( const wchar_t* string); 00189 00190 /** 00191 * Get the advance width for a string. 00192 * 00193 * @param string a char string 00194 * @return advance width 00195 */ 00196 float Advance( const char* string); 00197 00198 /** 00199 * Prepare for rendering 00200 */ 00201 virtual void PreRender() { preRenderCalled = true; } 00202 00203 /** 00204 * Cleanup after rendering 00205 */ 00206 virtual void PostRender() { preRenderCalled = false; } 00207 00208 /** 00209 * Render a string of characters 00210 * 00211 * @param string 'C' style string to be output. 00212 */ 00213 virtual void Render( const char* string ); 00214 00215 /** 00216 * Render a string of characters 00217 * 00218 * @param string 'C' style string to be output. 00219 * @param w_max maximum width of text 00220 * @param w_fade width at which text starts to fade 00221 */ 00222 virtual void Render( const char* string, float w_max, float w_fade ); 00223 00224 /** 00225 * Render a string of characters 00226 * 00227 * @param string wchar_t string to be output. 00228 */ 00229 virtual void Render( const wchar_t* string ); 00230 00231 /** 00232 * Queries the Font for errors. 00233 * 00234 * @return The current error code. 00235 */ 00236 FT_Error Error() const { return err;} 00237 00238 protected: 00239 /** 00240 * Construct a glyph of the correct type. 00241 * 00242 * Clients must overide the function and return their specialised 00243 * FTGlyph. 00244 * 00245 * @param g The glyph index NOT the char code. 00246 * @return An FT****Glyph or <code>null</code> on failure. 00247 */ 00248 virtual FTGlyph* MakeGlyph( unsigned int g) = 0; 00249 00250 /** 00251 * Current face object 00252 */ 00253 FTFace face; 00254 00255 /** 00256 * Current size object 00257 */ 00258 FTSize charSize; 00259 00260 /** 00261 * Flag to enable or disable the use of Display Lists inside FTGL 00262 * <code>true</code> turns ON display lists. 00263 * <code>false</code> turns OFF display lists. 00264 */ 00265 bool useDisplayLists; 00266 00267 /** 00268 * Current error code. Zero means no error. 00269 */ 00270 FT_Error err; 00271 00272 private: 00273 /** 00274 * Check that the glyph at <code>chr</code> exist. If not load it. 00275 * 00276 * @param chr character index 00277 * @return <code>true</code> if the glyph can be created. 00278 */ 00279 inline bool CheckGlyph( const unsigned int chr); 00280 00281 /** 00282 * PreRender() was called from outside. Do not call it again 00283 * from Render(), nor call PostRender(). 00284 * User has to call it himself. 00285 */ 00286 bool preRenderCalled; 00287 00288 /** 00289 * An object that holds a list of glyphs 00290 */ 00291 FTGlyphContainer* glyphList; 00292 00293 /** 00294 * Current pen or cursor position; 00295 */ 00296 FTPoint pen; 00297 }; 00298 00299 00300 #endif // __FTFont__ 00301