GL Studio 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) 2015 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 <string>
44 #include <vector>
45 #include "gls_gl.h"
46 #include "vertex.h"
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 //===========================================================================
58 // BEGIN NAMESPACE
59 //===========================================================================
60 namespace disti
61 {
62 
63 //---------------------------------------------------------------------------
64 // FORWARD REFERENCES
65 //---------------------------------------------------------------------------
66 class VertexNoColor;
67 class glsColor;
68 class Image;
69 
70 typedef VertexNoColor Vector;
71 
72 //===========================================================================
73 /**
74 * The GlsFontBase class provides a specific font face for use within the GL
75 * Studio. The font face is determined at construction time by the
76 * parameters passed in by the creator of the GlsFontBase instance. Specific
77 * instances should be wrapped in a singelton object wrapper, as there is
78 * no reason to have more than one instance of the exact same face (meaning
79 * font family, style, and point size.)
80 *
81 * Once instanced, a GlsFontRenderer object can be used to render characters in
82 * Open GL at specific locations and with several different text effects.
83 *
84 * \sa GlsFontRenderer
85 */
86 //===========================================================================
88 {
89 public:
90 
91  // Define the character type to use.
92  typedef GLuint Char_t;
93 
94  //-----------------------------------------------------------------------
95  /// Attributes of the specific font
96  //-----------------------------------------------------------------------
97  struct FontAttr_t
98  {
99  std::string family; /** Font family name. Eg. Arial */
100  std::string style; /** Font style. Eg. Bold */
101  GLuint ptSize; /** Font point size */
102  GLuint descender; /** Font descender in positive pixels */
103  Char_t firstChar; /** First character code in the character set */
104  Char_t lastChar; /** Last character code in the character set */
105  GLuint maxCharHeight; /** Maximum character height in pixels */
106  GLuint maxCharWidth; /** Maximum character width in pixels */
107  GLint underlinePos; /** Position of the underline for underlined text */
108  GLuint underlineSize; /** thickness of the underline */
109 
110  /** Default constructor to initialize font data to known values */
112  ptSize (0),
113  descender (0),
114  firstChar (32),
115  lastChar (127),
116  maxCharHeight(0),
117  maxCharWidth (0),
118  underlinePos (0),
119  underlineSize(0)
120  {
121  }
122  };
123 
124  //-----------------------------------------------------------------------
125  /// Character attributes. One item for each character in the set.
126  //-----------------------------------------------------------------------
127  struct CharAttr_t
128  {
129  GLfloat width; /** Character width in pixels/points */
130  GLfloat height; /** Character height in pixels/points */
131  GLfloat varTexX1; /** Left most x position in the texture for variable width font spacing. (in texture coordinates) */
132  GLfloat varTexX2; /** Right most x position in the texture for variable width font spacing. (in texture coordinates) */
133  GLfloat fixedTexX1; /** Left most x position in the texture for fixed width font spacing. (in texture coordinates) */
134  GLfloat fixedTexX2; /** Right most x position in the texture for fixed width font spacing. (in texture coordinates) */
135  GLfloat varTexY1; /** Lowest y position in the texture for variable height font spacing. (in texture coordinates) */
136  GLfloat varTexY2; /** Upper most y position in the texture for variable height font spacing. (in texture coordinates) */
137  GLfloat fixedTexY1; /** Lowest y position in the texture for fixed height font spacing. (in texture coordinates) */
138  GLfloat fixedTexY2; /** Upper most y position in the texture for fixed height font spacing. (in texture coordinates) */
139 
140  // Glyph metrics for character placement
141  // Additional information on the meaning of these parameters can
142  // be found in the freetype library documentation.
143  GLfloat horiBearingX; /** Distance from pen position to the left edge of the character (varTexX1) for horizontal strings. */
144  GLfloat horiBearingY; /** Distance from pen position (ie the baseline) to the top edge of the character (varTexY2) for horizontal strings. */
145  GLfloat horiAdvance; /** Distance to advance the pen position after drawing this character for horizontal strings. */
146  GLfloat vertBearingX; /** Distance from pen position (ie the center line) to the left edge of the character (varTexX1) for vertical strings. */
147  GLfloat vertBearingY; /** Distance from pen position to the top edge of the character (varTexY2) for vertical strings. */
148  GLfloat vertAdvance; /** Distance to advance the pen position after drawing this character for vertical strings. */
149 
150  /// Default constructor to initialize character attribute data to known values
151  CharAttr_t() : width (0),
152  height (0),
153  varTexX1 (0),
154  varTexX2 (0),
155  fixedTexX1 (0),
156  fixedTexX2 (0),
157  varTexY1 (0),
158  varTexY2 (0),
159  fixedTexY1 (0),
160  fixedTexY2 (0),
161  horiBearingX (0),
162  horiBearingY (0),
163  horiAdvance (0),
164  vertBearingX (0),
165  vertBearingY (0),
166  vertAdvance (0)
167  {
168  }
169 
170  CharAttr_t(GLfloat w,
171  GLfloat h,
172  GLfloat vTexX1,
173  GLfloat vTexX2,
174  GLfloat fTexX1,
175  GLfloat fTexX2,
176  GLfloat vTexY1,
177  GLfloat vTexY2,
178  GLfloat fTexY1,
179  GLfloat fTexY2,
180  GLfloat horiBearingX_,
181  GLfloat horiBearingY_,
182  GLfloat horiAdvance_,
183  GLfloat vertBearingX_,
184  GLfloat vertBearingY_,
185  GLfloat vertAdvance_ ) :
186  width (w),
187  height (h),
188  varTexX1 (vTexX1),
189  varTexX2 (vTexX2),
190  fixedTexX1 (fTexX1),
191  fixedTexX2 (fTexX2),
192  varTexY1 (vTexY1),
193  varTexY2 (vTexY2),
194  fixedTexY1 (fTexY1),
195  fixedTexY2 (fTexY2),
196  horiBearingX (horiBearingX_),
197  horiBearingY (horiBearingY_),
198  horiAdvance (horiAdvance_),
199  vertBearingX (vertBearingX_),
200  vertBearingY (vertBearingY_),
201  vertAdvance (vertAdvance_)
202  {
203  }
204  }; // end struct CharAttr_t
205 
206  typedef std::vector<CharAttr_t> AttrCont_t;
207 
208  /** Class Constructor. */
209  GLS_EXPORT GlsFontBase(
210  const FontAttr_t& fontAttr,
211  const AttrCont_t& charAttributes,
212  Image* texture);
213 
214  /**
215  * Access the attributes of the font.
216  * \return all the attributes of the font.
217  */
218  const FontAttr_t& Attributes() const
219  {
220  return _attr;
221  }
222 
223  /**
224  * NOTE: This function will only return char attributes for pre-generated ASCII fonts.
225  * Users should call CharAttrUnicode for GlsUnicodeFontBase fonts or CharAttrRuntime for GlsRuntimeFontBase fonts.
226  * \param c The character to retrieve attributes for.
227  * \returns The attributes for the character.
228  */
229  const CharAttr_t& CharAttr(Char_t c) const
230  {
231  return _charAttr[CharAttrIndex(c)];
232  }
233 
234  /**
235  * Calculates an index for the specified character depending on the set of characters the texture map was generated for.
236  * NOTE: This function will only return char attributes for pre-generated ASCII fonts.
237  * Users should call CharAttrUnicode for GlsUnicodeFontBase fonts or CharAttrRuntime for GlsRuntimeFontBase fonts.
238  * \param c specifies for which character the index is calculated for
239  * \return the index of the specified character
240  */
241  GLuint CharAttrIndex(Char_t c) const
242  {
243  return (_attr.firstChar <= c && c <= _attr.lastChar)
244  ? GLuint(c - _attr.firstChar) : 0;
245  }
246 
247  /**
248  * Access the character attributes of the font.
249  * \return the attributes of all the characters in the font.
250  */
251  const AttrCont_t& CharAttributes() const
252  {
253  return _charAttr;
254  }
255 
256  /**
257  * \return The font family string
258  */
259  std::string Family() const
260  {
261  return _attr.family;
262  }
263 
264  /**
265  * \return The unique lookup key for this font.
266  */
267  std::string Key() const
268  {
269  return _key;
270  }
271 
272  /**
273  * Builds a unique lookup key for a specific font, style, and size.
274  * \param name Name of the font
275  * \param style Style of the font
276  * \param ptSize Point size of the font
277  */
278  static GLS_EXPORT std::string Key(
279  const std::string& name,
280  const std::string& style,
281  GLuint ptSize);
282 
283  /**
284  * \return maximum height in pixels of the characters in the set
285  */
286  GLfloat MaxCharHeight() const
287  {
288  return GLfloat(_attr.maxCharHeight);
289  }
290 
291  /**
292  * \return maximum width in pixels of the characters in the set
293  */
294  GLfloat MaxCharWidth() const
295  {
296  return GLfloat(_attr.maxCharWidth);
297  }
298 
299  /**
300  * \return maximum height in pixels of the characters in the set
301  */
302  GLuint PtSize() const
303  {
304  return _attr.ptSize;
305  }
306 
307  /**
308  * Return the style description
309  */
310  std::string Style() const
311  {
312  return _attr.style;
313  }
314 
315  /**
316  * \return a pointer to the font's texture image.
317  */
318  Image* Texture() const
319  {
320  return _texture;
321  }
322 
323 protected:
324 
325  /**
326  * Class Destructor. This is protected so that no one can delete a font
327  * except via the font manager GlsFontMan.
328  * It is not vitual because we want to avoid calling destructors on unloaded font code,
329  * and it is not needed.
330  */
331  GLS_EXPORT ~GlsFontBase();
332 
333  friend class GlsFontMan;
334 
335  /** Attributes of the font. */
337 
338  /** Proportional character attributes used when proportional character spacing is turned on. */
339  AttrCont_t _charAttr;
340 
341  /** Unique key used as a lookup key to find fonts within maps. */
342  std::string _key;
343 
344  /** Texture of all characters in the set. */
346 
347 private:
348 
349  //-----------------------------------------------------------------------
350  // Prevent copies
351  //-----------------------------------------------------------------------
352  GlsFontBase(const GlsFontBase&);
353  GlsFontBase& operator=(const GlsFontBase&);
354 
355 }; // end class GlsFontBase
356 
357 } // end namespace disti
358 
359 #endif
const AttrCont_t & CharAttributes() const
Definition: gls_font_base.h:251
Definition: image.h:160
GLuint ptSize
Definition: gls_font_base.h:101
GLuint maxCharHeight
Definition: gls_font_base.h:105
GLint underlinePos
Definition: gls_font_base.h:107
const FontAttr_t & Attributes() const
Definition: gls_font_base.h:218
Image * _texture
Definition: gls_font_base.h:345
GLuint PtSize() const
Definition: gls_font_base.h:302
const CharAttr_t & CharAttr(Char_t c) const
Definition: gls_font_base.h:229
Image * Texture() const
Definition: gls_font_base.h:318
GLfloat vertAdvance
Definition: gls_font_base.h:148
GLfloat vertBearingY
Definition: gls_font_base.h:147
GLuint CharAttrIndex(Char_t c) const
Definition: gls_font_base.h:241
GLfloat varTexX2
Definition: gls_font_base.h:132
GLuint underlineSize
Definition: gls_font_base.h:108
FontAttr_t _attr
Definition: gls_font_base.h:336
Char_t firstChar
Definition: gls_font_base.h:103
VertexNoColor Vector
Definition: gls_font_base.h:68
GLfloat horiBearingY
Definition: gls_font_base.h:144
GLuint descender
Definition: gls_font_base.h:102
GLfloat MaxCharWidth() const
Definition: gls_font_base.h:294
GLfloat varTexY1
Definition: gls_font_base.h:135
GLfloat MaxCharHeight() const
Definition: gls_font_base.h:286
The disti::Vertex class. A class for manipulating 3D vertices.
GLfloat fixedTexX2
Definition: gls_font_base.h:134
Attributes of the specific font.
Definition: gls_font_base.h:97
FontAttr_t()
Definition: gls_font_base.h:111
GLfloat horiBearingX
Definition: gls_font_base.h:143
GLfloat fixedTexX1
Definition: gls_font_base.h:133
GLfloat varTexX1
Definition: gls_font_base.h:131
GLfloat height
Definition: gls_font_base.h:130
CharAttr_t()
Default constructor to initialize character attribute data to known values.
Definition: gls_font_base.h:151
Definition: gls_font_man.h:63
Char_t lastChar
Definition: gls_font_base.h:104
std::string style
Definition: gls_font_base.h:100
Definition: vertex.h:84
std::string Key() const
Definition: gls_font_base.h:267
std::string _key
Definition: gls_font_base.h:342
GLfloat fixedTexY1
Definition: gls_font_base.h:137
AttrCont_t _charAttr
Definition: gls_font_base.h:339
GLfloat horiAdvance
Definition: gls_font_base.h:145
std::string Style() const
Definition: gls_font_base.h:310
Character attributes. One item for each character in the set.
Definition: gls_font_base.h:127
GLuint maxCharWidth
Definition: gls_font_base.h:106
std::string Family() const
Definition: gls_font_base.h:259
Definition: bmpimage.h:46
GLfloat varTexY2
Definition: gls_font_base.h:136
GLfloat vertBearingX
Definition: gls_font_base.h:146
GLfloat fixedTexY2
Definition: gls_font_base.h:138
GlsFontBase(const FontAttr_t &fontAttr, const AttrCont_t &charAttributes, Image *texture)
Definition: gls_font_base.h:87
The gls_gl.