Panda3D
 All Classes Functions Variables Enumerations
textFont.cxx
1 // Filename: textFont.cxx
2 // Created by: drose (08Feb02)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "textFont.h"
16 #include "config_text.h"
17 #include "string_utils.h"
18 #include "geomVertexData.h"
19 #include "geomVertexFormat.h"
20 #include "geomVertexWriter.h"
21 #include "geomLinestrips.h"
22 #include "geom.h"
23 #include <ctype.h>
24 
25 TypeHandle TextFont::_type_handle;
26 
27 ////////////////////////////////////////////////////////////////////
28 // Function: TextFont::Constructor
29 // Access: Public
30 // Description:
31 ////////////////////////////////////////////////////////////////////
32 TextFont::
33 TextFont() {
34  _is_valid = false;
35  _line_height = 1.0f;
36  _space_advance = 0.25f;
37 }
38 
39 ////////////////////////////////////////////////////////////////////
40 // Function: TextFont::Copy Constructor
41 // Access: Public
42 // Description:
43 ////////////////////////////////////////////////////////////////////
44 TextFont::
45 TextFont(const TextFont &copy) :
46  Namable(copy),
47  _is_valid(copy._is_valid),
48  _line_height(copy._line_height),
49  _space_advance(copy._space_advance)
50 {
51 }
52 
53 ////////////////////////////////////////////////////////////////////
54 // Function: TextFont::Destructor
55 // Access: Published, Virtual
56 // Description:
57 ////////////////////////////////////////////////////////////////////
58 TextFont::
59 ~TextFont() {
60 }
61 
62 ////////////////////////////////////////////////////////////////////
63 // Function: TextFont::write
64 // Access: Published, Virtual
65 // Description:
66 ////////////////////////////////////////////////////////////////////
67 void TextFont::
68 write(ostream &out, int indent_level) const {
69  indent(out, indent_level)
70  << "TextFont " << get_name() << "\n";
71 }
72 
73 ////////////////////////////////////////////////////////////////////
74 // Function: TextFont::get_invalid_glyph
75 // Access: Public
76 // Description: Returns a special glyph that can be used as a
77 // placeholder for any character not in the font. Note
78 // that it is not guaranteed that a font will return
79 // this particular glyph for a missing character (it may
80 // return a glyph of its own devising instead).
81 //
82 // Also note that even if a particular accented letter
83 // is missing from the font, Panda may still be able to
84 // render a suitable replacement by composing different
85 // glyphs together to simulate accent marks; this
86 // happens automatically behind the scenes.
87 ////////////////////////////////////////////////////////////////////
90  if (_invalid_glyph == (TextGlyph *)NULL) {
91  make_invalid_glyph();
92  }
93  return _invalid_glyph;
94 }
95 
96 ////////////////////////////////////////////////////////////////////
97 // Function: TextFont::string_render_mode
98 // Access: Public
99 // Description: Returns the RenderMode value associated with the given
100 // string representation, or RM_invalid if the string
101 // does not match any known RenderMode value.
102 ////////////////////////////////////////////////////////////////////
103 TextFont::RenderMode TextFont::
104 string_render_mode(const string &string) {
105  if (cmp_nocase_uh(string, "texture") == 0) {
106  return RM_texture;
107  } else if (cmp_nocase_uh(string, "wireframe") == 0) {
108  return RM_wireframe;
109  } else if (cmp_nocase_uh(string, "polygon") == 0) {
110  return RM_polygon;
111  } else if (cmp_nocase_uh(string, "extruded") == 0) {
112  return RM_extruded;
113  } else if (cmp_nocase_uh(string, "solid") == 0) {
114  return RM_solid;
115  } else {
116  return RM_invalid;
117  }
118 }
119 
120 ////////////////////////////////////////////////////////////////////
121 // Function: TextFont::string_winding_order
122 // Access: Public
123 // Description: Returns the WindingOrder value associated with the given
124 // string representation, or WO_invalid if the string
125 // does not match any known WindingOrder value.
126 ////////////////////////////////////////////////////////////////////
127 TextFont::WindingOrder TextFont::
128 string_winding_order(const string &string) {
129  if (cmp_nocase_uh(string, "default") == 0) {
130  return WO_default;
131  } else if (cmp_nocase_uh(string, "left") == 0) {
132  return WO_left;
133  } else if (cmp_nocase_uh(string, "right") == 0) {
134  return WO_right;
135  } else {
136  return WO_invalid;
137  }
138 }
139 
140 ////////////////////////////////////////////////////////////////////
141 // Function: TextFont::make_invalid_glyph
142 // Access: Private
143 // Description: Constructs the special glyph used to represent a
144 // character not in the font.
145 ////////////////////////////////////////////////////////////////////
146 void TextFont::
147 make_invalid_glyph() {
148  CPT(GeomVertexFormat) vformat = GeomVertexFormat::get_v3();
149  PT(GeomVertexData) vdata =
150  new GeomVertexData("invalid_glyph", vformat, GeomEnums::UH_static);
151 
152  GeomVertexWriter vertex(vdata, InternalName::get_vertex());
153  vertex.add_data3(_line_height * 0.2, 0.0f, _line_height * 0.1f);
154  vertex.add_data3(_line_height * 0.5f, 0.0f, _line_height * 0.1f);
155  vertex.add_data3(_line_height * 0.5f, 0.0f, _line_height * 0.7f);
156  vertex.add_data3(_line_height * 0.2, 0.0f, _line_height * 0.7f);
157 
158  PT(GeomPrimitive) prim = new GeomLinestrips(GeomEnums::UH_static);
159  prim->add_consecutive_vertices(0, 4);
160  prim->add_vertex(0);
161  prim->close_primitive();
162 
163  PT(Geom) geom = new Geom(vdata);
164  geom->add_primitive(prim);
165 
166  _invalid_glyph = new TextGlyph(0, geom, RenderState::make_empty(),
167  _line_height * 0.7f);
168 }
169 
170 ////////////////////////////////////////////////////////////////////
171 // Function: TextFont::RenderMode output operator
172 // Description:
173 ////////////////////////////////////////////////////////////////////
174 ostream &
175 operator << (ostream &out, TextFont::RenderMode rm) {
176  switch (rm) {
177  case TextFont::RM_texture:
178  return out << "texture";
179  case TextFont::RM_wireframe:
180  return out << "wireframe";
181  case TextFont::RM_polygon:
182  return out << "polygon";
183  case TextFont::RM_extruded:
184  return out << "extruded";
185  case TextFont::RM_solid:
186  return out << "solid";
187 
188  case TextFont::RM_invalid:
189  return out << "invalid";
190  }
191 
192  return out << "(**invalid TextFont::RenderMode(" << (int)rm << ")**)";
193 }
194 
195 ////////////////////////////////////////////////////////////////////
196 // Function: TextFont::RenderMode input operator
197 // Description:
198 ////////////////////////////////////////////////////////////////////
199 istream &
200 operator >> (istream &in, TextFont::RenderMode &rm) {
201  string word;
202  in >> word;
203 
204  rm = TextFont::string_render_mode(word);
205  return in;
206 }
207 
208 ////////////////////////////////////////////////////////////////////
209 // Function: TextFont::WindingOrder output operator
210 // Description:
211 ////////////////////////////////////////////////////////////////////
212 ostream &
213 operator << (ostream &out, TextFont::WindingOrder wo) {
214  switch (wo) {
215  case TextFont::WO_default:
216  return out << "default";
217  case TextFont::WO_left:
218  return out << "left";
219  case TextFont::WO_right:
220  return out << "right";
221 
222  case TextFont::WO_invalid:
223  return out << "invalid";
224  }
225 
226  return out << "(**invalid TextFont::WindingOrder(" << (int)wo << ")**)";
227 }
228 
229 ////////////////////////////////////////////////////////////////////
230 // Function: TextFont::WindingOrder input operator
231 // Description:
232 ////////////////////////////////////////////////////////////////////
233 istream &
234 operator >> (istream &in, TextFont::WindingOrder &wo) {
235  string word;
236  in >> word;
237 
239  return in;
240 }
This object provides a high-level interface for quickly writing a sequence of numeric values from a v...
static WindingOrder string_winding_order(const string &string)
Returns the WindingOrder value associated with the given string representation, or WO_invalid if the ...
Definition: textFont.cxx:128
This class exists just to provide scoping for the various enumerated types used by Geom...
Definition: geomEnums.h:27
This is an abstract base class for a family of classes that represent the fundamental geometry primit...
Definition: geomPrimitive.h:63
An encapsulation of a font; i.e.
Definition: textFont.h:36
A base class for all things which can have a name.
Definition: namable.h:29
This defines the actual numeric vertex data stored in a Geom, in the structure defined by a particula...
A container for geometry primitives.
Definition: geom.h:58
A representation of a single glyph (character) from a font.
Definition: textGlyph.h:31
TextGlyph * get_invalid_glyph()
Returns a special glyph that can be used as a placeholder for any character not in the font...
Definition: textFont.cxx:89
This represents a unique collection of RenderAttrib objects that correspond to a particular renderabl...
Definition: renderState.h:53
Defines a series of line strips.
static RenderMode string_render_mode(const string &string)
Returns the RenderMode value associated with the given string representation, or RM_invalid if the st...
Definition: textFont.cxx:104
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85