nux-0.9.48

NuxGraphics/FontTexture.h

Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #ifndef FONTTEXTURE_H
00024 #define FONTTEXTURE_H
00025 
00026 class IOpenGLPixelShader;
00027 
00028 namespace nux
00029 {
00030 
00031   typedef enum _TextAlignment
00032   {
00033     eAlignTextNone       = 0,
00034     eAlignTextLeft       = 1,
00035     eAlignTextRight      = 2,
00036     eAlignTextCenter     = 3,
00037 
00038   } TextAlignment;
00039 
00040   class StringBBox
00041   {
00042   public:
00043     StringBBox()
00044     {
00045       x = 0;
00046       y = 0;
00047       width = 0;
00048       height = 0;
00049       ybearing = 0;
00050       downline = 0;
00051     };
00052     ~StringBBox() {};
00053 
00054     int x;
00055     int y;
00056     int width;
00057     int height;
00058     int ybearing; // max ybearing of the string
00059     int downline; // max downline of the string (max space below the baseline)
00060   };
00061 
00062   class PageBBox
00063   {
00064   public:
00065     PageBBox()
00066     {
00067       xmin = 0;
00068       ymin = 0;
00069       xmax = 0;
00070       ymax = 0;
00071       x_margin = 0;
00072       y_margin = 0;
00073     };
00074     ~PageBBox() {};
00075     INT xmin;
00076     INT ymin;
00077     INT xmax;
00078     INT ymax;
00079     INT x_margin;
00080     INT y_margin;
00081   };
00082 
00084   struct CharDescriptor
00085   {
00086     //clean 16 bytes
00087     unsigned short x;
00088     unsigned short y;
00089     unsigned short Width;
00090     unsigned short Height;
00091     short XOffset;
00092     short YOffset;
00093     unsigned short page;
00094     short XAdvance;
00095     short abcA;
00096     short abcB;
00097     short abcC;
00098 
00099 
00100     CharDescriptor()
00101       : x ( 0 )
00102       , y ( 0 )
00103       , Width ( 0 )
00104       , Height ( 0 )
00105       , XOffset ( 0 )
00106       , YOffset ( 0 )
00107       , page ( 0 )
00108       , XAdvance ( 0 )
00109       , abcA ( 0 )
00110       , abcB ( 0 )
00111       , abcC ( 0 )
00112     { }
00113   };
00114 
00115   struct KerningPair
00116   {
00117     unsigned short first;
00118     unsigned short second;
00119     short          amount;
00120   };
00121 
00122   struct Charset
00123   {
00124     bool italic;
00125     bool bold;
00126     unsigned short LineHeight;
00127     unsigned short Base;
00128     unsigned short Width, Height;
00129     unsigned short Pages;
00130     unsigned short FontHeight;
00131     unsigned short Ascent;
00132     unsigned short Descent;
00133     int AvgCharWidth;
00134     int MaxCharWidth;
00135     int InternalLeading;
00136     int ExternalLeading;
00137     unsigned short NumChar;
00138     CharDescriptor Chars[256];
00139     unsigned short NumKerningPairs;
00140     KerningPair *Kerning;
00141   };
00142 
00143 // Information about a glyph. Tex_y2 can be calculated from tex_y1
00144 // and _tex_line_height (see below). Advance is the width of the
00145 // glyph in screen space.
00146   struct Glyph
00147   {
00148     float tex_x1, tex_y1, tex_x2;
00149     int advance;
00150   };
00151 
00152   class FontRenderer;
00153 
00154 
00156 
00157 // This font system loads in a custom file containing a gray scale
00158 // texture (used as alpha texture) with all the letters on it, and
00159 // information about what glyph is where.
00160   class FontTexture: public Object
00161   {
00162   public:
00163     NUX_DECLARE_OBJECT_TYPE (FontTexture, Object);
00164 
00165     FontTexture (const TCHAR *FontFile, NUX_FILE_LINE_PROTO);
00166     FontTexture (INT width, INT height, BYTE *Texture);
00167     ~FontTexture();
00168 
00169     // The line height is a constant;
00170     int GetLineHeight() const
00171     {
00172       return m_Charset.FontHeight;
00173     }
00174     // Knowing the width of a character or a string can be useful if you
00175     // want your UI to look good at all.
00176     int GetCharWidth (const TCHAR &c) const;
00177     int GetStringWidth (const NString &str) const;
00178     int GetCharStringWidth (const TCHAR *str) const;
00179     int GetStringWidth (const NString &str, int num_char_to_compute) const;
00180     int GetCharStringWidth (const TCHAR *str, int num_char_to_compute) const;
00181     int GetFontHeight();
00182 
00183     //    CursorPosToX (similar to ScriptStringCPtoX from microsoft UniScript)
00184     //        The CursorPosToX function returns the x-coordinate for the leading or trailing edge of a character position.
00185 
00186     //        Parameters
00187     //        icp
00188     //          [in] Character position in the string.
00189     //        fTrailing
00190     //          [in] Indicates the edge of the icp that corresponds to the x coordinate. If TRUE, it indicates the trailing edge. If FALSE, it indicates the leading edge.
00191     //        pX
00192     //          [out] Pointer to a variable that receives the corresponding x coordinate for the icp.
00193     //
00194     //        Return Values
00195     //          If the function succeeds, it returns S_OK.
00196     //          If the function fails, it returns an HRESULT.
00197     //          The return value can be tested with the SUCCEEDED and FAILED macros.
00198     bool CursorPosToX (const NString &Str,
00199                        int icp,
00200                        bool fTrailing,
00201                        int *pX);
00202 
00203     //    XToCursorPosition (similar to ScriptStringXtoCP from microsoft UniScript)
00204     //        The XToCursorPosition function converts an x-coordinate to a character position.
00205     //
00206     //    Parameters
00207     //        iX
00208     //          [in] Specifies the x coordinate.
00209     //        FirstVisibleCharIndex,
00210     //          [in] Index of the first visible character in the text box
00211     //        piCh
00212     //          [out] Pointer to a variable that receives the character position corresponding to iX.
00213     //        piTrailing
00214     //          [out] Pointer to a variable that receives an indicator whether the position is the leading or trailing edge of the character.
00215     //
00216     //        Return Values
00217     //          If the function is successful, it returns S_OK.
00218     //          If the function fails, it returns an HRESULT.
00219     //          The return value can be tested with the SUCCEEDED and FAILED macros.
00220     bool XToCursorPosition (const NString &Str,
00221                             int iX,
00222                             t_u32 FirstVisibleCharIndex,
00223                             int *piCh,
00224                             int *piTrailing);
00225 
00226     bool BMFontParseFNT ( std::istream &Stream);
00227 
00228     const Charset &GetFontInfo() const;
00229 
00230     std::vector<BaseTexture*> TextureArray;
00231 
00232   private:
00233     INT _RefCount;
00234     INT _textureBMF;
00235     std::vector<t_u32> m_gl_texture_id;
00236     Charset m_Charset;
00237 
00238     friend class FontRenderer;
00239   };
00240 
00241 }
00242 
00243 #endif //FONTTEXTURE_H