CFont.h
Go to the documentation of this file.
1 //==============================================================================
2 /*
3  Software License Agreement (BSD License)
4  Copyright (c) 2003-2016, CHAI3D.
5  (www.chai3d.org)
6 
7  All rights reserved.
8 
9  Redistribution and use in source and binary forms, with or without
10  modification, are permitted provided that the following conditions
11  are met:
12 
13  * Redistributions of source code must retain the above copyright
14  notice, this list of conditions and the following disclaimer.
15 
16  * Redistributions in binary form must reproduce the above
17  copyright notice, this list of conditions and the following
18  disclaimer in the documentation and/or other materials provided
19  with the distribution.
20 
21  * Neither the name of CHAI3D nor the names of its contributors may
22  be used to endorse or promote products derived from this software
23  without specific prior written permission.
24 
25  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  POSSIBILITY OF SUCH DAMAGE.
37 
38  \author <http://www.chai3d.org>
39  \author Francois Conti
40  \version 3.2.0 $Rev: 2173 $
41 */
42 //==============================================================================
43 
44 //------------------------------------------------------------------------------
45 #ifndef CFontH
46 #define CFontH
47 //------------------------------------------------------------------------------
48 #include "system/CGlobals.h"
49 #include "materials/CTexture2d.h"
50 //------------------------------------------------------------------------------
51 #include <stdlib.h>
52 #include <string.h>
53 #include <iostream>
54 #include <sstream>
55 //------------------------------------------------------------------------------
56 
57 //------------------------------------------------------------------------------
58 namespace chai3d {
59 //------------------------------------------------------------------------------
60 
61 //==============================================================================
68 //==============================================================================
69 
70 //------------------------------------------------------------------------------
71 class cFont;
72 typedef std::shared_ptr<cFont> cFontPtr;
73 //------------------------------------------------------------------------------
74 
75 //------------------------------------------------------------------------------
76 #ifndef DOXYGEN_SHOULD_SKIP_THIS
77 //------------------------------------------------------------------------------
78 
79 struct cFontCharDescriptor
80 {
81  int m_x, m_y;
82  int m_width, m_height;
83  int m_xOffset, m_yOffset;
84  int m_xAdvance;
85  int m_page;
86 
87  double m_tu0, m_tu1, m_tu2, m_tu3;
88  double m_tv0, m_tv1, m_tv2, m_tv3;
89  double m_px0, m_px1, m_px2, m_px3;
90  double m_py0, m_py1, m_py2, m_py3;
91 
92  cFontCharDescriptor()
93  {
94  initialize();
95  }
96 
97  void initialize()
98  {
99  m_x = 0;
100  m_y = 0;
101  m_width = 0;
102  m_height = 0;
103  m_xOffset = 0;
104  m_yOffset = 0;
105  m_xAdvance = 0;
106  m_page = 0;
107  }
108 };
109 
110 //------------------------------------------------------------------------------
111 
112 struct cFontCharset
113 {
114  unsigned int m_lineHeight;
115  unsigned int m_base;
116  unsigned int m_width, m_height;
117  unsigned int m_pages;
118  std::string m_fileName;
119 
120  cFontCharDescriptor m_chars[256];
121 
122  cFontCharset()
123  {
124  initialize();
125  }
126 
127  void initialize()
128  {
129  m_lineHeight = 0;
130  m_base = 0;
131  m_width = 0;
132  m_height = 0;
133  m_pages = 0;
134 
135  for (int i=0; i<256; i++)
136  m_chars[i].initialize();
137  }
138 
139  void preProcess();
140 };
141 
142 //------------------------------------------------------------------------------
143 #endif // DOXYGEN_SHOULD_SKIP_THIS
144 //------------------------------------------------------------------------------
145 
146 
147 //==============================================================================
163 //==============================================================================
164 class cFont
165 {
166  //--------------------------------------------------------------------------
167  // CONSTRUCTOR & DESTRUCTOR:
168  //--------------------------------------------------------------------------
169 
170 public:
171 
173  cFont();
174 
176  virtual ~cFont();
177 
179  static cFontPtr create() { return (std::make_shared<cFont>()); }
180 
181 
182  //--------------------------------------------------------------------------
183  // PUBLIC METHODS - FILES:
184  //--------------------------------------------------------------------------
185 
186 public:
187 
189  bool loadFromFile(const std::string& a_filename);
190 
192  std::string getFilename() const { return (m_filename); }
193 
194 
195  //--------------------------------------------------------------------------
196  // PUBLIC METHODS - FONT SETTINGS:
197  //--------------------------------------------------------------------------
198 
199 public:
200 
202  double getPointSize() const { return (m_charset.m_lineHeight); }
203 
205  double getTextWidth(const std::string& a_text, const double a_letterSpacing);
206 
208  double getTextHeight(const std::string& a_text, const double a_lineSpacing);
209 
210 
211  //--------------------------------------------------------------------------
212  // PUBLIC METHODS - OPENGL RENDERING:
213  //--------------------------------------------------------------------------
214 
215 public:
216 
218  double renderText(const std::string& a_text,
219  const cColorf& a_color,
220  const double a_fontScale,
221  const double a_letterSpacing,
222  const double a_lineSpacing,
223  cRenderOptions& a_options);
224 
225 
226  //--------------------------------------------------------------------------
227  // PUBLIC MEMBERS:
228  //--------------------------------------------------------------------------
229 
230 public:
231 
234 
236  cFontCharset m_charset;
237 
238 
239  //-----------------------------------------------------------------------
240  // PROTECTED METHODS:
241  //--------------------------------------------------------------------------
242 
243 protected:
244 
246  void parseFont(std::istream& a_stream, cFontCharset& a_charsetDesc);
247 
249  void cleanup();
250 
251 
252  //-----------------------------------------------------------------------
253  // PROTECTED MEMBERS:
254  //--------------------------------------------------------------------------
255 
256 protected:
257 
259  std::string m_filename;
260 };
261 
262 //------------------------------------------------------------------------------
263 } // namespace chai3d
264 //------------------------------------------------------------------------------
265 
266 //------------------------------------------------------------------------------
267 #endif
268 //------------------------------------------------------------------------------
This structures provide a containers for storing rendering options that are passed through the sceneg...
Definition: CRenderOptions.h:82
std::shared_ptr< cFont > cFontPtr
Definition: CFont.h:71
double getPointSize() const
This method returns the current font size.
Definition: CFont.h:202
std::string m_filename
Current font filename loaded into memory.
Definition: CFont.h:259
This class implements support for 2D fonts.
Definition: CFont.h:164
Implements option settings for CHAI3D.
static cFontPtr create()
Shared cFont allocator.
Definition: CFont.h:179
std::string getFilename() const
This method returns the filename from which this font was last loaded or saved.
Definition: CFont.h:192
This class defines a color using a GLfloat representation for each component.
Definition: CColor.h:138
Definition: CAudioBuffer.cpp:56
cFontCharset m_charset
Information about each character in the set.
Definition: CFont.h:236
Implements 2D textures.
This class implements a 2D texture map.
Definition: CTexture2d.h:84
cTexture2d * m_texture
Texture image containing the full bitmap representation of the font.
Definition: CFont.h:233