Panda3D
textFont.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file textFont.cxx
10  * @author drose
11  * @date 2002-02-08
12  */
13 
14 #include "textFont.h"
15 #include "config_text.h"
16 #include "string_utils.h"
17 #include "geomVertexData.h"
18 #include "geomVertexFormat.h"
19 #include "geomVertexWriter.h"
20 #include "geomLinestrips.h"
21 #include "geom.h"
22 #include <ctype.h>
23 
24 using std::istream;
25 using std::ostream;
26 using std::string;
27 
28 TypeHandle TextFont::_type_handle;
29 
30 /**
31  *
32  */
33 TextFont::
34 TextFont() {
35  _is_valid = false;
36  _line_height = 1.0f;
37  _space_advance = 0.25f;
38  _total_poly_margin = 0.0f;
39 }
40 
41 /**
42  *
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  _total_poly_margin(copy._total_poly_margin)
51 {
52 }
53 
54 /**
55  *
56  */
57 TextFont::
58 ~TextFont() {
59 }
60 
61 /**
62  * Returns the amount by which to offset the second glyph when it directly
63  * follows the first glyph. This is an additional offset that is added on top
64  * of the advance.
65  */
66 PN_stdfloat TextFont::
67 get_kerning(int first, int second) const {
68  return 0;
69 }
70 
71 /**
72  *
73  */
74 void TextFont::
75 write(ostream &out, int indent_level) const {
76  indent(out, indent_level)
77  << "TextFont " << get_name() << "\n";
78 }
79 
80 /**
81  * Returns a special glyph that can be used as a placeholder for any character
82  * not in the font. Note that it is not guaranteed that a font will return
83  * this particular glyph for a missing character (it may return a glyph of its
84  * own devising instead).
85  *
86  * Also note that even if a particular accented letter is missing from the
87  * font, Panda may still be able to render a suitable replacement by composing
88  * different glyphs together to simulate accent marks; this happens
89  * automatically behind the scenes.
90  */
93  if (_invalid_glyph == nullptr) {
94  make_invalid_glyph();
95  }
96  return _invalid_glyph;
97 }
98 
99 /**
100  * Returns the RenderMode value associated with the given string
101  * representation, or RM_invalid if the string does not match any known
102  * RenderMode value.
103  */
104 TextFont::RenderMode TextFont::
105 string_render_mode(const string &string) {
106  if (cmp_nocase_uh(string, "texture") == 0) {
107  return RM_texture;
108  } else if (cmp_nocase_uh(string, "wireframe") == 0) {
109  return RM_wireframe;
110  } else if (cmp_nocase_uh(string, "polygon") == 0) {
111  return RM_polygon;
112  } else if (cmp_nocase_uh(string, "extruded") == 0) {
113  return RM_extruded;
114  } else if (cmp_nocase_uh(string, "solid") == 0) {
115  return RM_solid;
116  } else if (cmp_nocase_uh(string, "distance_field") == 0) {
117  return RM_distance_field;
118  } else {
119  return RM_invalid;
120  }
121 }
122 
123 /**
124  * Constructs the special glyph used to represent a character not in the font.
125  */
126 void TextFont::
127 make_invalid_glyph() {
129  PT(GeomVertexData) vdata =
130  new GeomVertexData("invalid_glyph", vformat, GeomEnums::UH_static);
131 
132  GeomVertexWriter vertex(vdata, InternalName::get_vertex());
133  vertex.add_data3(_line_height * 0.2, 0.0f, _line_height * 0.1f);
134  vertex.add_data3(_line_height * 0.5f, 0.0f, _line_height * 0.1f);
135  vertex.add_data3(_line_height * 0.5f, 0.0f, _line_height * 0.7f);
136  vertex.add_data3(_line_height * 0.2, 0.0f, _line_height * 0.7f);
137 
138  PT(GeomPrimitive) prim = new GeomLinestrips(GeomEnums::UH_static);
139  prim->add_consecutive_vertices(0, 4);
140  prim->add_vertex(0);
141  prim->close_primitive();
142 
143  PT(Geom) geom = new Geom(vdata);
144  geom->add_primitive(prim);
145 
146  _invalid_glyph = new TextGlyph(0, geom, RenderState::make_empty(),
147  _line_height * 0.7f);
148 }
149 
150 /**
151  *
152  */
153 ostream &
154 operator << (ostream &out, TextFont::RenderMode rm) {
155  switch (rm) {
156  case TextFont::RM_texture:
157  return out << "texture";
158  case TextFont::RM_wireframe:
159  return out << "wireframe";
160  case TextFont::RM_polygon:
161  return out << "polygon";
162  case TextFont::RM_extruded:
163  return out << "extruded";
164  case TextFont::RM_solid:
165  return out << "solid";
166  case TextFont::RM_distance_field:
167  return out << "distance-field";
168 
169  case TextFont::RM_invalid:
170  return out << "invalid";
171  }
172 
173  return out << "(**invalid TextFont::RenderMode(" << (int)rm << ")**)";
174 }
175 
176 /**
177  *
178  */
179 istream &
180 operator >> (istream &in, TextFont::RenderMode &rm) {
181  string word;
182  in >> word;
183 
184  rm = TextFont::string_render_mode(word);
185  return in;
186 }
Defines a series of line strips.
This is an abstract base class for a family of classes that represent the fundamental geometry primit...
Definition: geomPrimitive.h:56
This defines the actual numeric vertex data stored in a Geom, in the structure defined by a particula...
This class defines the physical layout of the vertex data stored within a Geom.
static const GeomVertexFormat * get_v3()
Returns a standard vertex format with just a 3-component vertex position.
This object provides a high-level interface for quickly writing a sequence of numeric values from a v...
A container for geometry primitives.
Definition: geom.h:54
A base class for all things which can have a name.
Definition: namable.h:26
An encapsulation of a font; i.e.
Definition: textFont.h:32
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:92
virtual PN_stdfloat get_kerning(int first, int second) const
Returns the amount by which to offset the second glyph when it directly follows the first glyph.
Definition: textFont.cxx:67
static RenderMode string_render_mode(const std::string &string)
Returns the RenderMode value associated with the given string representation, or RM_invalid if the st...
Definition: textFont.cxx:105
A representation of a single glyph (character) from a font.
Definition: textGlyph.h:28
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.