~karl-qdh/nux/nux.gtkentry-wrapper

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 * Copyright 2010 Inalogic Inc.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3, as
 * published by the  Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the applicable version of the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of both the GNU Lesser General Public
 * License version 3 along with this program.  If not, see
 * <http://www.gnu.org/licenses/>
 *
 * Authored by: Jay Taoko <jaytaoko@inalogic.com>
 *
 */


#ifndef FONTTEXTURE_H
#define FONTTEXTURE_H

class IOpenGLPixelShader;

namespace nux
{

  typedef enum _TextAlignment
  {
    eAlignTextNone       = 0,
    eAlignTextLeft       = 1,
    eAlignTextRight      = 2,
    eAlignTextCenter     = 3,

  } TextAlignment;

  class StringBBox
  {
  public:
    StringBBox()
    {
      x = 0;
      y = 0;
      width = 0;
      height = 0;
      ybearing = 0;
      downline = 0;
    };
    ~StringBBox() {};

    int x;
    int y;
    int width;
    int height;
    int ybearing; // max ybearing of the string
    int downline; // max downline of the string (max space below the baseline)
  };

  class PageBBox
  {
  public:
    PageBBox()
    {
      xmin = 0;
      ymin = 0;
      xmax = 0;
      ymax = 0;
      x_margin = 0;
      y_margin = 0;
    };
    ~PageBBox() {};
    INT xmin;
    INT ymin;
    INT xmax;
    INT ymax;
    INT x_margin;
    INT y_margin;
  };

///////////////////////////////////////////////////////////////////////////////////////////////////////
  struct CharDescriptor
  {
    //clean 16 bytes
    unsigned short x;
    unsigned short y;
    unsigned short Width;
    unsigned short Height;
    short XOffset;
    short YOffset;
    unsigned short page;
    short XAdvance;
    short abcA;
    short abcB;
    short abcC;


    CharDescriptor()
      : x ( 0 )
      , y ( 0 )
      , Width ( 0 )
      , Height ( 0 )
      , XOffset ( 0 )
      , YOffset ( 0 )
      , page ( 0 )
      , XAdvance ( 0 )
      , abcA ( 0 )
      , abcB ( 0 )
      , abcC ( 0 )
    { }
  };

  struct KerningPair
  {
    unsigned short first;
    unsigned short second;
    short          amount;
  };

  struct Charset
  {
    bool italic;
    bool bold;
    unsigned short LineHeight;
    unsigned short Base;
    unsigned short Width, Height;
    unsigned short Pages;
    unsigned short FontHeight;
    unsigned short Ascent;
    unsigned short Descent;
    int AvgCharWidth;
    int MaxCharWidth;
    int InternalLeading;
    int ExternalLeading;
    unsigned short NumChar;
    CharDescriptor Chars[256];
    unsigned short NumKerningPairs;
    KerningPair *Kerning;
  };

// Information about a glyph. Tex_y2 can be calculated from tex_y1
// and _tex_line_height (see below). Advance is the width of the
// glyph in screen space.
  struct Glyph
  {
    float tex_x1, tex_y1, tex_x2;
    int advance;
  };

  class FontRenderer;


////////////////////////////////////////////////////////////////////////////////////////////////////

// This font system loads in a custom file containing a gray scale
// texture (used as alpha texture) with all the letters on it, and
// information about what glyph is where.
  class FontTexture: public Object
  {
  public:
    NUX_DECLARE_OBJECT_TYPE (FontTexture, Object);

    FontTexture (const TCHAR *FontFile, NUX_FILE_LINE_PROTO);
    FontTexture (INT width, INT height, BYTE *Texture);
    ~FontTexture();

    // The line height is a constant;
    int GetLineHeight() const
    {
      return m_Charset.FontHeight;
    }
    // Knowing the width of a character or a string can be useful if you
    // want your UI to look good at all.
    int GetCharWidth (const TCHAR &c) const;
    int GetStringWidth (const NString &str) const;
    int GetCharStringWidth (const TCHAR *str) const;
    int GetStringWidth (const NString &str, int num_char_to_compute) const;
    int GetCharStringWidth (const TCHAR *str, int num_char_to_compute) const;
    int GetFontHeight();

    //    CursorPosToX (similar to ScriptStringCPtoX from microsoft UniScript)
    //        The CursorPosToX function returns the x-coordinate for the leading or trailing edge of a character position.

    //        Parameters
    //        icp
    //          [in] Character position in the string.
    //        fTrailing
    //          [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.
    //        pX
    //          [out] Pointer to a variable that receives the corresponding x coordinate for the icp.
    //
    //        Return Values
    //          If the function succeeds, it returns S_OK.
    //          If the function fails, it returns an HRESULT.
    //          The return value can be tested with the SUCCEEDED and FAILED macros.
    bool CursorPosToX (const NString &Str,
                       int icp,
                       bool fTrailing,
                       int *pX);

    //    XToCursorPosition (similar to ScriptStringXtoCP from microsoft UniScript)
    //        The XToCursorPosition function converts an x-coordinate to a character position.
    //
    //    Parameters
    //        iX
    //          [in] Specifies the x coordinate.
    //        FirstVisibleCharIndex,
    //          [in] Index of the first visible character in the text box
    //        piCh
    //          [out] Pointer to a variable that receives the character position corresponding to iX.
    //        piTrailing
    //          [out] Pointer to a variable that receives an indicator whether the position is the leading or trailing edge of the character.
    //
    //        Return Values
    //          If the function is successful, it returns S_OK.
    //          If the function fails, it returns an HRESULT.
    //          The return value can be tested with the SUCCEEDED and FAILED macros.
    bool XToCursorPosition (const NString &Str,
                            int iX,
                            t_u32 FirstVisibleCharIndex,
                            int *piCh,
                            int *piTrailing);

    bool BMFontParseFNT ( std::istream &Stream);

    const Charset &GetFontInfo() const;

    std::vector<BaseTexture*> TextureArray;

  private:
    INT _RefCount;
    INT _textureBMF;
    std::vector<t_u32> m_gl_texture_id;
    Charset m_Charset;

    friend class FontRenderer;
  };

}

#endif //FONTTEXTURE_H