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.1.1 $Rev: 1869 $
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 #ifndef DOXYGEN_SHOULD_SKIP_THIS
72 //------------------------------------------------------------------------------
73 
74 struct cFontCharDescriptor
75 {
76  int m_x, m_y;
77  int m_width, m_height;
78  int m_xOffset, m_yOffset;
79  int m_xAdvance;
80  int m_page;
81 
82  double m_tu0, m_tu1, m_tu2, m_tu3;
83  double m_tv0, m_tv1, m_tv2, m_tv3;
84  double m_px0, m_px1, m_px2, m_px3;
85  double m_py0, m_py1, m_py2, m_py3;
86 
87  cFontCharDescriptor()
88  {
89  initialize();
90  }
91 
92  void initialize()
93  {
94  m_x = 0;
95  m_y = 0;
96  m_width = 0;
97  m_height = 0;
98  m_xOffset = 0;
99  m_yOffset = 0;
100  m_xAdvance = 0;
101  m_page = 0;
102  }
103 };
104 
105 //------------------------------------------------------------------------------
106 
107 struct cFontCharset
108 {
109  unsigned int m_lineHeight;
110  unsigned int m_base;
111  unsigned int m_width, m_height;
112  unsigned int m_pages;
113  std::string m_fileName;
114 
115  cFontCharDescriptor m_chars[256];
116 
117  cFontCharset()
118  {
119  initialize();
120  }
121 
122  void initialize()
123  {
124  m_lineHeight = 0;
125  m_base = 0;
126  m_width = 0;
127  m_height = 0;
128  m_pages = 0;
129 
130  for (int i=0; i<256; i++)
131  m_chars[i].initialize();
132  }
133 
134  void preProcess();
135 };
136 
137 //------------------------------------------------------------------------------
138 #endif // DOXYGEN_SHOULD_SKIP_THIS
139 //------------------------------------------------------------------------------
140 
141 
142 //==============================================================================
158 //==============================================================================
159 class cFont
160 {
161  //--------------------------------------------------------------------------
162  // CONSTRUCTOR & DESTRUCTOR:
163  //--------------------------------------------------------------------------
164 
165 public:
166 
168  cFont();
169 
171  virtual ~cFont();
172 
173 
174  //--------------------------------------------------------------------------
175  // PUBLIC METHODS - FILES:
176  //--------------------------------------------------------------------------
177 
178 public:
179 
181  bool loadFromFile(const std::string& a_filename);
182 
184  std::string getFilename() const { return (m_filename); }
185 
186 
187  //--------------------------------------------------------------------------
188  // PUBLIC METHODS - FONT SETTINGS:
189  //--------------------------------------------------------------------------
190 
191 public:
192 
194  double getPointSize() const { return (m_charset.m_lineHeight); }
195 
197  double getTextWidth(const std::string& a_text);
198 
199 
200  //--------------------------------------------------------------------------
201  // PUBLIC METHODS - OPENGL RENDERING:
202  //--------------------------------------------------------------------------
203 
204 public:
205 
207  double renderText(const std::string& a_text,
208  const cColorf& a_color,
209  const double a_fontScale,
210  cRenderOptions& a_options);
211 
212 
213  //--------------------------------------------------------------------------
214  // PUBLIC MEMBERS:
215  //--------------------------------------------------------------------------
216 
217 public:
218 
221 
223  cFontCharset m_charset;
224 
225 
226  //-----------------------------------------------------------------------
227  // PROTECTED METHODS:
228  //--------------------------------------------------------------------------
229 
230 protected:
231 
233  void parseFont(std::istream& a_stream, cFontCharset& a_charsetDesc);
234 
236  void cleanup();
237 
238 
239  //-----------------------------------------------------------------------
240  // PROTECTED MEMBERS:
241  //--------------------------------------------------------------------------
242 
243 protected:
244 
246  std::string m_filename;
247 };
248 
249 //------------------------------------------------------------------------------
250 } // namespace chai3d
251 //------------------------------------------------------------------------------
252 
253 //------------------------------------------------------------------------------
254 #endif
255 //------------------------------------------------------------------------------
This structures provide a containers for storing rendering options that are passed through the sceneg...
Definition: CRenderOptions.h:82
double getPointSize() const
This method returns the current font size.
Definition: CFont.h:194
bool loadFromFile(const std::string &a_filename)
This method loads a font file by passing a path and file name as argument.
Definition: CFont.cpp:102
std::string m_filename
Current font filename loaded into memory.
Definition: CFont.h:246
This class implements support for 2D fonts.
Definition: CFont.h:159
cFont()
Constructor of cFont.
Definition: CFont.cpp:61
Implements option settings for CHAI3D.
std::string getFilename() const
This method returns the filename from which this font was last loaded or saved.
Definition: CFont.h:184
void cleanup()
This method cleans all memory.
Definition: CFont.cpp:87
virtual ~cFont()
Destructor of cFont.
Definition: CFont.cpp:76
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:223
double getTextWidth(const std::string &a_text)
This method returns the width of a particular string using this font.
Definition: CFont.cpp:358
Implements 2D textures.
This class implements a 2D texture map.
Definition: CTexture2d.h:84
double renderText(const std::string &a_text, const cColorf &a_color, const double a_fontScale, cRenderOptions &a_options)
This method render a single string of text.
Definition: CFont.cpp:267
void parseFont(std::istream &a_stream, cFontCharset &a_charsetDesc)
This method parses a font file.
Definition: CFont.cpp:175
cTexture2d * m_texture
Texture image containing the full bitmap representation of the font.
Definition: CFont.h:220