MercuryText.h

Go to the documentation of this file.
00001 #ifndef MERCURYTEXT_H
00002 #define MERCURYTEXT_H
00003 
00004 #include "MercuryObject.h"
00005 #include "MercurySprite.h"
00006 #include "MercuryTexture.h"
00007 #include "MercuryVector.h"
00008 
00009 #ifndef __FREETYPE_H__
00010 class FT_LibraryRec_;
00011 class FT_FaceRec_;
00012 #endif
00013 
00014 struct MercuryTextCharData
00015 {
00016     int top_px;
00017     int left_px;
00018     int advance_x;
00019     int advance_y;
00020 
00021     float top_v;
00022     float bottom_v;
00023     float left_u;
00024     float right_u;
00025 
00026     int width_px;
00027     int height_px;
00028 };
00029 
00030 struct MercuryTextGlyphData
00031 {
00032     MercuryTextGlyphData()
00033         :height_px(0), point_size(0), image_data(NULL) 
00034     {}
00035 
00036     RawImageData* image_data;
00037     int height_px;
00038     int point_size;
00039     MercuryTextCharData  charDatas[256];
00040     MercuryTexture textureData;
00041 };
00042 
00043 struct MercuryTextChar
00044 {
00045     MercuryPoint position;
00046     MercuryVertex vertices[4];
00047     int advance_x;
00048     int advance_y;
00049 };
00050 
00052 class MercuryText : public MercuryObject
00053 {
00054 public:
00055     MercuryText();
00056     virtual ~MercuryText();
00057 
00058     virtual void Init();
00059 
00060     void SetText(const MString& text);
00061     inline MString GetText() const { return m_text; }
00062     void SetFont(const MString& font);
00063     inline MString GetFont() const { return m_font; }
00064     void SetSize(unsigned int px);
00065     inline unsigned int GetSize() { return m_height; }
00066     void Clear();
00067 
00068     void BuildGlyphData();
00069 
00070     virtual void Message( int Message, PStack & data, const MString & name );
00071     virtual bool Command( PStack & ret, const char * command, PStack & args );
00072     virtual void EnumerateCommands( MVector< MString > & toAdd );
00073 
00074     CLASS_RTTI( MercuryText, MercuryObject );
00075 
00076     int GetCurWidth() { return m_width; }
00077     int GetCurHeight() { return m_height; }
00078     void SetMaxWidth( int maxwidth) { m_maxwidth = maxwidth; }
00079     void SetTabWidth( int pixels ) { m_tabwidth = pixels; }
00080 
00081     inline const MercuryTextGlyphData* GetGlyphData() const { return m_mtgd; }
00082     
00083     inline unsigned int NumCharacters() const { return m_characters.size(); }
00084     inline const MercuryTextChar* GetCharacter(unsigned int i) const { return m_characters[i]; }
00085 
00086     virtual void CustomRender();
00087 private:
00088     int m_maxwidth, m_tabwidth;
00089 
00090     bool New_Face(FT_LibraryRec_ * library, const MString& filepathname,
00091                     long face_index, FT_FaceRec_ **aface );
00092 
00093     void SetupSizing(const DisplayDimensions& d);
00094     unsigned int m_h, m_fontheight;
00095     MercuryTextChar* BuildCharSprite(const char ch, int& x, int& y);
00096 
00097     void BuildFontDisplay();
00098 
00099     MString m_text; 
00100     MString m_font;
00101     MVector< MAutoPtr<MercuryTextChar> > m_characters;
00102 
00103     MString m_id;
00104 
00105     struct LetterHolder
00106     {
00107         LetterHolder( MercuryTextChar* sprite, int width, int line, int pixcol, bool space, bool tab, bool enter ) :
00108             pSprite( sprite ), iWidth(width), iLine(line), iPixCol(pixcol), bSpace( space ), bTab( tab ), bEnter( enter ) { }
00109         MercuryTextChar* pSprite;
00110         int             iWidth;
00111         int             iLine;
00112         int             iPixCol;
00113         bool            bSpace;
00114         bool            bTab;
00115         bool            bEnter;
00116     };
00117 
00118     char* m_fontFileData;
00119     MercuryTextGlyphData* m_mtgd;
00120 };
00121 
00122 //this is needed to avoid problems with static time execution
00123 class GlobalFontHash
00124 {
00125 public:
00126     GlobalFontHash()
00127         :m_fonthash(NULL)
00128     {};
00129 
00130     ~GlobalFontHash()
00131     {
00132         SAFE_DELETE(m_fonthash);
00133     }
00134 
00135     void Destroy()
00136     {
00137         SAFE_DELETE(m_fonthash);
00138     }
00139 
00140     inline MHash< MercuryTextGlyphData >& operator*()
00141     {
00142         if (!m_fonthash)
00143             m_fonthash = new MHash< MercuryTextGlyphData >;
00144         return *m_fonthash;
00145     }
00146 private:
00147     MHash< MercuryTextGlyphData >* m_fonthash;
00148 };
00149 
00150 extern GlobalFontHash FONTHASH;
00151 
00152 #endif
00153 
00154 /* 
00155  * Copyright (c) 2005-2006, Joshua Allen
00156  * All rights reserved.
00157  *
00158  * Redistribution and use in source and binary forms, with or
00159  * without modification, are permitted provided that the following
00160  * conditions are met:
00161  *  -   Redistributions of source code must retain the above
00162  *      copyright notice, this list of conditions and the following disclaimer.
00163  *  -   Redistributions in binary form must reproduce the above copyright
00164  *      notice, this list of conditions and the following disclaimer in
00165  *      the documentation and/or other materials provided with the distribution.
00166  *  -   Neither the name of the Mercury Engine nor the names of its
00167  *      contributors may be used to endorse or promote products derived from
00168  *      this software without specific prior written permission.
00169  *
00170  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00171  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00172  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00173  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00174  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00175  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00176  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00177  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00178  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00179  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00180  */

Hosted by SourceForge.net Logo