Back to index

nux  3.0.0
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     KerningPair()
00122       : first(0), second(0), amount(0)
00123     {}
00124   };
00125 
00126   struct Charset
00127   {
00128     bool italic;
00129     bool bold;
00130     unsigned short LineHeight;
00131     unsigned short Base;
00132     unsigned short Width, Height;
00133     unsigned short Pages;
00134     unsigned short FontHeight;
00135     unsigned short Ascent;
00136     unsigned short Descent;
00137     int AvgCharWidth;
00138     int MaxCharWidth;
00139     int InternalLeading;
00140     int ExternalLeading;
00141     unsigned short NumChar;
00142     CharDescriptor Chars[256];
00143     unsigned short NumKerningPairs;
00144     KerningPair *Kerning;
00145 
00146     Charset()
00147     : italic(false), bold(false), LineHeight(0), Base(0), Width(0), Height(0)
00148         , Pages(0), FontHeight(0), Ascent(0), Descent(0), AvgCharWidth(0)
00149         , MaxCharWidth(0), InternalLeading(0), ExternalLeading(0)
00150         , NumChar(0), NumKerningPairs(0), Kerning(NULL)
00151     {}
00152 
00153     ~Charset()
00154     {
00155       delete [] Kerning;
00156     }
00157   };
00158 
00159 // Information about a glyph. Tex_y2 can be calculated from tex_y1
00160 // and _tex_line_height(see below). Advance is the width of the
00161 // glyph in screen space.
00162   struct Glyph
00163   {
00164     float tex_x1, tex_y1, tex_x2;
00165     int advance;
00166   };
00167 
00168   class FontRenderer;
00169 
00170 
00172 
00173 // This font system loads in a custom file containing a gray scale
00174 // texture(used as alpha texture) with all the letters on it, and
00175 // information about what glyph is where.
00176   class FontTexture: public Object
00177   {
00178   public:
00179     NUX_DECLARE_OBJECT_TYPE(FontTexture, Object);
00180 
00181     FontTexture(const char *FontFile, NUX_FILE_LINE_PROTO);
00182     FontTexture(INT width, INT height, BYTE *Texture);
00183     ~FontTexture();
00184 
00185     // The line height is a constant;
00186     int GetLineHeight() const
00187     {
00188       return m_Charset.FontHeight;
00189     }
00190     // Knowing the width of a character or a string can be useful if you
00191     // want your UI to look good at all.
00192     int GetCharWidth(const char &c) const;
00193     int GetStringWidth(const NString &str) const;
00194     int GetCharStringWidth(const char *str) const;
00195     int GetStringWidth(const NString &str, int num_char_to_compute) const;
00196     int GetCharStringWidth(const char *str, int num_char_to_compute) const;
00197     int GetFontHeight();
00198 
00199     //    CursorPosToX(similar to ScriptStringCPtoX from microsoft UniScript)
00200     //        The CursorPosToX function returns the x-coordinate for the leading or trailing edge of a character position.
00201 
00202     //        Parameters
00203     //        icp
00204     //          [in] Character position in the string.
00205     //        fTrailing
00206     //          [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.
00207     //        pX
00208     //          [out] Pointer to a variable that receives the corresponding x coordinate for the icp.
00209     //
00210     //        Return Values
00211     //          If the function succeeds, it returns S_OK.
00212     //          If the function fails, it returns an HRESULT.
00213     //          The return value can be tested with the SUCCEEDED and FAILED macros.
00214     bool CursorPosToX(const NString &Str,
00215                        int icp,
00216                        bool fTrailing,
00217                        int *pX);
00218 
00219     //    XToCursorPosition(similar to ScriptStringXtoCP from microsoft UniScript)
00220     //        The XToCursorPosition function converts an x-coordinate to a character position.
00221     //
00222     //    Parameters
00223     //        iX
00224     //          [in] Specifies the x coordinate.
00225     //        FirstVisibleCharIndex,
00226     //          [in] Index of the first visible character in the text box
00227     //        piCh
00228     //          [out] Pointer to a variable that receives the character position corresponding to iX.
00229     //        piTrailing
00230     //          [out] Pointer to a variable that receives an indicator whether the position is the leading or trailing edge of the character.
00231     //
00232     //        Return Values
00233     //          If the function is successful, it returns S_OK.
00234     //          If the function fails, it returns an HRESULT.
00235     //          The return value can be tested with the SUCCEEDED and FAILED macros.
00236     bool XToCursorPosition(const NString &Str,
00237                             int iX,
00238                             unsigned int FirstVisibleCharIndex,
00239                             int *piCh,
00240                             int *piTrailing);
00241 
00242     bool BMFontParseFNT( std::istream &Stream);
00243 
00244     const Charset &GetFontInfo() const;
00245 
00246     std::vector<BaseTexture*> TextureArray;
00247 
00248   private:
00249     INT _RefCount;
00250     INT _textureBMF;
00251     std::vector<unsigned int> m_gl_texture_id;
00252     Charset m_Charset;
00253 
00254     friend class FontRenderer;
00255   };
00256 
00257 }
00258 
00259 #endif //FONTTEXTURE_H