GL Studio C++ Runtime API
gls_font_base.h
Go to the documentation of this file.
1 /*! \file
2  \brief The disti::GlsFontBase class and related classes.
3 
4  \par Copyright Information
5 
6  Copyright (c) 2017 by The DiSTI Corporation.<br>
7  11301 Corporate Blvd; Suite 100<br>
8  Orlando, Florida 32817<br>
9  USA<br>
10  <br>
11  All rights reserved.<br>
12 
13  This Software contains proprietary trade secrets of DiSTI and may not be
14 reproduced, in whole or part, in any form, or by any means of electronic,
15 mechanical, or otherwise, without the written permission of DiSTI. Said
16 permission may be derived through the purchase of applicable DiSTI product
17 licenses which detail the distribution rights of this content and any
18 Derivative Works based on this or other copyrighted DiSTI Software.
19 
20  NO WARRANTY. THE SOFTWARE IS PROVIDED "AS-IS," WITHOUT WARRANTY OF ANY KIND,
21 AND ANY USE OF THIS SOFTWARE PRODUCT IS AT YOUR OWN RISK. TO THE MAXIMUM EXTENT
22 PERMITTED BY APPLICABLE LAW, DISTI AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES
23 AND CONDITIONS, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
24 IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY AND/OR FITNESS FOR A
25 PARTICULAR PURPOSE, TITLE, AND NON-INFRINGEMENT, WITH REGARD TO THE SOFTWARE.
26 
27  LIMITATION OF LIABILITY. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW,
28 IN NO EVENT SHALL DISTI OR ITS SUPPLIERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
29 INDIRECT, OR CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION,
30 DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS
31 INFORMATION, OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR
32 INABILITY TO USE THE SOFTWARE, EVEN IF DISTI HAS BEEN ADVISED OF THE POSSIBILITY
33 OF SUCH DAMAGES. DISTI'S ENTIRE LIABILITY AND YOUR EXCLUSIVE REMEDY SHALL NOT
34 EXCEED FIVE DOLLARS (US$5.00).
35 
36  The aforementioned terms and restrictions are governed by the laws of the
37 State of Florida and the United States of America.
38 
39 */
40 #ifndef _GLS_FONT_H
41 #define _GLS_FONT_H
42 
43 #include "gls_gl.h"
44 #include "vertex.h"
45 #include <string>
46 #include <vector>
47 
48 //---------------------------------------------------------------------------
49 // MACROS FOR USE IN GENERATED SOURCE CODE
50 //---------------------------------------------------------------------------
51 #define BEGIN_CHAR_ATTR AttrCont_t attr
52 #define ADD_CHAR_ATTR( a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16 ) \
53  attr.push_back( CharAttr_t( a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16 ) )
54 #define END_CHAR_ATTR return attr
55 
56 //===========================================================================
57 // BEGIN NAMESPACE
58 //===========================================================================
59 namespace disti
60 {
61 //---------------------------------------------------------------------------
62 // FORWARD REFERENCES
63 //---------------------------------------------------------------------------
64 class VertexNoColor;
65 class GlsColor;
66 class Image;
67 
68 typedef VertexNoColor Vector;
69 
70 //===========================================================================
71 /**
72 * The GlsFontBase class provides a specific font face for use within the GL
73 * Studio. The font face is determined at construction time by the
74 * parameters passed in by the creator of the GlsFontBase instance. Specific
75 * instances should be wrapped in a singelton object wrapper, as there is
76 * no reason to have more than one instance of the exact same face (meaning
77 * font family, style, and point size.)
78 *
79 * Once instanced, a GlsFontRenderer object can be used to render characters in
80 * Open GL at specific locations and with several different text effects.
81 *
82 * \sa GlsFontRenderer
83 */
84 //===========================================================================
86 {
87 public:
88  // Define the character type to use.
89  typedef GLuint Char_t;
90 
91  //-----------------------------------------------------------------------
92  /// Attributes of the specific font
93  //-----------------------------------------------------------------------
94  struct FontAttr_t
95  {
96  std::string family; /** Font family name. Eg. Arial */
97  std::string style; /** Font style. Eg. Bold */
98  GLuint ptSize; /** Font point size */
99  GLuint descender; /** Font descender in positive pixels */
100  Char_t firstChar; /** First character code in the character set */
101  Char_t lastChar; /** Last character code in the character set */
102  GLuint maxCharHeight; /** Maximum character height in pixels */
103  GLuint maxCharWidth; /** Maximum character width in pixels */
104  GLint underlinePos; /** Position of the underline for underlined text */
105  GLuint underlineSize; /** thickness of the underline */
106 
107  /** Default constructor to initialize font data to known values */
109  : ptSize( 0 )
110  , descender( 0 )
111  , firstChar( 32 )
112  , lastChar( 127 )
113  , maxCharHeight( 0 )
114  , maxCharWidth( 0 )
115  , underlinePos( 0 )
116  , underlineSize( 0 )
117  {
118  }
119  };
120 
121  //-----------------------------------------------------------------------
122  /// Character attributes. One item for each character in the set.
123  //-----------------------------------------------------------------------
124  struct CharAttr_t
125  {
126  GLfloat width; /** Character width in pixels/points */
127  GLfloat height; /** Character height in pixels/points */
128  GLfloat varTexX1; /** Left most x position in the texture for variable width font spacing. (in texture coordinates) */
129  GLfloat varTexX2; /** Right most x position in the texture for variable width font spacing. (in texture coordinates) */
130  GLfloat fixedTexX1; /** Left most x position in the texture for fixed width font spacing. (in texture coordinates) */
131  GLfloat fixedTexX2; /** Right most x position in the texture for fixed width font spacing. (in texture coordinates) */
132  GLfloat varTexY1; /** Lowest y position in the texture for variable height font spacing. (in texture coordinates) */
133  GLfloat varTexY2; /** Upper most y position in the texture for variable height font spacing. (in texture coordinates) */
134  GLfloat fixedTexY1; /** Lowest y position in the texture for fixed height font spacing. (in texture coordinates) */
135  GLfloat fixedTexY2; /** Upper most y position in the texture for fixed height font spacing. (in texture coordinates) */
136 
137  // Glyph metrics for character placement
138  // Additional information on the meaning of these parameters can
139  // be found in the freetype library documentation.
140  GLfloat horiBearingX; /** Distance from pen position to the left edge of the character (varTexX1) for horizontal strings. */
141  GLfloat horiBearingY; /** Distance from pen position (ie the baseline) to the top edge of the character (varTexY2) for horizontal strings. */
142  GLfloat horiAdvance; /** Distance to advance the pen position after drawing this character for horizontal strings. */
143  GLfloat vertBearingX; /** Distance from pen position (ie the center line) to the left edge of the character (varTexX1) for vertical strings. */
144  GLfloat vertBearingY; /** Distance from pen position to the top edge of the character (varTexY2) for vertical strings. */
145  GLfloat vertAdvance; /** Distance to advance the pen position after drawing this character for vertical strings. */
146 
147  /// Default constructor to initialize character attribute data to known values
149  : width( 0 )
150  , height( 0 )
151  , varTexX1( 0 )
152  , varTexX2( 0 )
153  , fixedTexX1( 0 )
154  , fixedTexX2( 0 )
155  , varTexY1( 0 )
156  , varTexY2( 0 )
157  , fixedTexY1( 0 )
158  , fixedTexY2( 0 )
159  , horiBearingX( 0 )
160  , horiBearingY( 0 )
161  , horiAdvance( 0 )
162  , vertBearingX( 0 )
163  , vertBearingY( 0 )
164  , vertAdvance( 0 )
165  {
166  }
167 
168  CharAttr_t( GLfloat w,
169  GLfloat h,
170  GLfloat vTexX1,
171  GLfloat vTexX2,
172  GLfloat fTexX1,
173  GLfloat fTexX2,
174  GLfloat vTexY1,
175  GLfloat vTexY2,
176  GLfloat fTexY1,
177  GLfloat fTexY2,
178  GLfloat horiBearingX_,
179  GLfloat horiBearingY_,
180  GLfloat horiAdvance_,
181  GLfloat vertBearingX_,
182  GLfloat vertBearingY_,
183  GLfloat vertAdvance_ )
184  : width( w )
185  , height( h )
186  , varTexX1( vTexX1 )
187  , varTexX2( vTexX2 )
188  , fixedTexX1( fTexX1 )
189  , fixedTexX2( fTexX2 )
190  , varTexY1( vTexY1 )
191  , varTexY2( vTexY2 )
192  , fixedTexY1( fTexY1 )
193  , fixedTexY2( fTexY2 )
194  , horiBearingX( horiBearingX_ )
195  , horiBearingY( horiBearingY_ )
196  , horiAdvance( horiAdvance_ )
197  , vertBearingX( vertBearingX_ )
198  , vertBearingY( vertBearingY_ )
199  , vertAdvance( vertAdvance_ )
200  {
201  }
202  }; // end struct CharAttr_t
203 
204  typedef std::vector<CharAttr_t> AttrCont_t;
205 
206  /** Class Constructor. */
207  GLS_EXPORT GlsFontBase(
208  const FontAttr_t& fontAttr,
209  const AttrCont_t& charAttributes,
210  Image* texture );
211 
212  /**
213  * Access the attributes of the font.
214  * \return all the attributes of the font.
215  */
216  const FontAttr_t& Attributes() const
217  {
218  return _attr;
219  }
220 
221  /**
222  * NOTE: This function will only return char attributes for pre-generated ASCII fonts.
223  * Users should call CharAttrUnicode for GlsUnicodeFontBase fonts or CharAttrRuntime for GlsRuntimeFontBase fonts.
224  * \param c The character to retrieve attributes for.
225  * \returns The attributes for the character.
226  */
227  const CharAttr_t& CharAttr( Char_t c ) const
228  {
229  return _charAttr[ CharAttrIndex( c ) ];
230  }
231 
232  /**
233  * Calculates an index for the specified character depending on the set of characters the texture map was generated for.
234  * NOTE: This function will only return char attributes for pre-generated ASCII fonts.
235  * Users should call CharAttrUnicode for GlsUnicodeFontBase fonts or CharAttrRuntime for GlsRuntimeFontBase fonts.
236  * \param c specifies for which character the index is calculated for
237  * \return the index of the specified character
238  */
239  GLuint CharAttrIndex( Char_t c ) const
240  {
241  return ( _attr.firstChar <= c && c <= _attr.lastChar )
242  ? GLuint( c - _attr.firstChar )
243  : 0;
244  }
245 
246  /**
247  * Access the character attributes of the font.
248  * \return the attributes of all the characters in the font.
249  */
250  const AttrCont_t& CharAttributes() const
251  {
252  return _charAttr;
253  }
254 
255  /**
256  * \return The font family string
257  */
258  std::string Family() const
259  {
260  return _attr.family;
261  }
262 
263  /**
264  * \return The unique lookup key for this font.
265  */
266  std::string Key() const
267  {
268  return _key;
269  }
270 
271  /**
272  * Builds a unique lookup key for a specific font, style, and size.
273  * \param name Name of the font
274  * \param style Style of the font
275  * \param ptSize Point size of the font
276  */
277  static GLS_EXPORT std::string Key(
278  const std::string& name,
279  const std::string& style,
280  GLuint ptSize );
281 
282  /**
283  * \return maximum height in pixels of the characters in the set
284  */
285  GLfloat MaxCharHeight() const
286  {
287  return GLfloat( _attr.maxCharHeight );
288  }
289 
290  /**
291  * \return maximum width in pixels of the characters in the set
292  */
293  GLfloat MaxCharWidth() const
294  {
295  return GLfloat( _attr.maxCharWidth );
296  }
297 
298  /**
299  * \return maximum height in pixels of the characters in the set
300  */
301  GLuint PtSize() const
302  {
303  return _attr.ptSize;
304  }
305 
306  /**
307  * Return the style description
308  */
309  std::string Style() const
310  {
311  return _attr.style;
312  }
313 
314  /**
315  * \return a pointer to the font's texture image.
316  */
317  Image* Texture() const
318  {
319  return _texture;
320  }
321 
322 protected:
323  /**
324  * Class Destructor. This is protected so that no one can delete a font
325  * except via the font manager GlsFontMan.
326  * It is not vitual because we want to avoid calling destructors on unloaded font code,
327  * and it is not needed.
328  */
329  GLS_EXPORT ~GlsFontBase();
330 
331  friend class GlsFontMan;
332 
333  /** Attributes of the font. */
335 
336  /** Proportional character attributes used when proportional character spacing is turned on. */
337  AttrCont_t _charAttr;
338 
339  /** Unique key used as a lookup key to find fonts within maps. */
340  std::string _key;
341 
342  /** Texture of all characters in the set. */
344 
345 private:
346  //-----------------------------------------------------------------------
347  // Prevent copies
348  //-----------------------------------------------------------------------
349  GlsFontBase( const GlsFontBase& );
350  GlsFontBase& operator=( const GlsFontBase& );
351 
352 }; // end class GlsFontBase
353 
354 } // end namespace disti
355 
356 #endif
const AttrCont_t & CharAttributes() const
Definition: gls_font_base.h:250
Definition: image.h:162
GLuint ptSize
Definition: gls_font_base.h:98
GLuint maxCharHeight
Definition: gls_font_base.h:102
GLint underlinePos
Definition: gls_font_base.h:104
const FontAttr_t & Attributes() const
Definition: gls_font_base.h:216
Image * _texture
Definition: gls_font_base.h:343
GLuint PtSize() const
Definition: gls_font_base.h:301
const CharAttr_t & CharAttr(Char_t c) const
Definition: gls_font_base.h:227
Image * Texture() const
Definition: gls_font_base.h:317
GLfloat vertAdvance
Definition: gls_font_base.h:145
GLfloat vertBearingY
Definition: gls_font_base.h:144
GLuint CharAttrIndex(Char_t c) const
Definition: gls_font_base.h:239
GLfloat varTexX2
Definition: gls_font_base.h:129
GLuint underlineSize
Definition: gls_font_base.h:105
FontAttr_t _attr
Definition: gls_font_base.h:334
Char_t firstChar
Definition: gls_font_base.h:100
VertexNoColor Vector
Definition: gls_font_base.h:66
GLfloat horiBearingY
Definition: gls_font_base.h:141
GLuint descender
Definition: gls_font_base.h:99
GLfloat MaxCharWidth() const
Definition: gls_font_base.h:293
GLfloat varTexY1
Definition: gls_font_base.h:132
GLfloat MaxCharHeight() const
Definition: gls_font_base.h:285
The disti::Vertex class. A class for manipulating 3D vertices.
GLfloat fixedTexX2
Definition: gls_font_base.h:131
Attributes of the specific font.
Definition: gls_font_base.h:94
FontAttr_t()
Definition: gls_font_base.h:108
GLfloat horiBearingX
Definition: gls_font_base.h:140
GLfloat fixedTexX1
Definition: gls_font_base.h:130
GLfloat varTexX1
Definition: gls_font_base.h:128
GLfloat height
Definition: gls_font_base.h:127
CharAttr_t()
Default constructor to initialize character attribute data to known values.
Definition: gls_font_base.h:148
Definition: gls_font_man.h:62
Char_t lastChar
Definition: gls_font_base.h:101
std::string style
Definition: gls_font_base.h:97
Definition: vertex.h:83
std::string Key() const
Definition: gls_font_base.h:266
std::string _key
Definition: gls_font_base.h:340
GLfloat fixedTexY1
Definition: gls_font_base.h:134
AttrCont_t _charAttr
Definition: gls_font_base.h:337
GLfloat horiAdvance
Definition: gls_font_base.h:142
std::string Style() const
Definition: gls_font_base.h:309
Character attributes. One item for each character in the set.
Definition: gls_font_base.h:124
GLuint maxCharWidth
Definition: gls_font_base.h:103
std::string Family() const
Definition: gls_font_base.h:258
Definition: bmpimage.h:46
GLfloat varTexY2
Definition: gls_font_base.h:133
GLfloat vertBearingX
Definition: gls_font_base.h:143
GLfloat fixedTexY2
Definition: gls_font_base.h:135
GlsFontBase(const FontAttr_t &fontAttr, const AttrCont_t &charAttributes, Image *texture)
Definition: gls_font_base.h:85
The gls_gl.