Panda3D
geomTextGlyph.cxx
1 // Filename: geomTextGlyph.cxx
2 // Created by: drose (31Mar05)
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 "geomTextGlyph.h"
16 
17 #ifdef HAVE_FREETYPE
18 
19 #include "datagramIterator.h"
20 #include "bamReader.h"
21 #include "indent.h"
22 
23 TypeHandle GeomTextGlyph::_type_handle;
24 
25 
26 ////////////////////////////////////////////////////////////////////
27 // Function: GeomTextGlyph::Constructor
28 // Access: Public
29 // Description:
30 ////////////////////////////////////////////////////////////////////
31 GeomTextGlyph::
32 GeomTextGlyph(DynamicTextGlyph *glyph, const GeomVertexData *data) :
33  Geom(data)
34 {
35  // Initially, there is only one glyph in the Geom. There might be
36  // additional Glyphs later when we flatten the graph and call
37  // Geom::unify().
38  if (glyph != (DynamicTextGlyph *)NULL) {
39  _glyphs.reserve(1);
40  _glyphs.push_back(glyph);
41  glyph->_geom_count++;
42  }
43 }
44 
45 ////////////////////////////////////////////////////////////////////
46 // Function: GeomTextGlyph::Constructor
47 // Access: Public
48 // Description:
49 ////////////////////////////////////////////////////////////////////
50 GeomTextGlyph::
51 GeomTextGlyph(const GeomVertexData *data) :
52  Geom(data)
53 {
54  // With this constructor, there are no glyphs initially.
55 }
56 
57 ////////////////////////////////////////////////////////////////////
58 // Function: GeomTextGlyph::Copy Constructor
59 // Access: Public
60 // Description:
61 ////////////////////////////////////////////////////////////////////
62 GeomTextGlyph::
63 GeomTextGlyph(const GeomTextGlyph &copy) :
64  Geom(copy),
65  _glyphs(copy._glyphs)
66 {
67  Glyphs::iterator gi;
68  for (gi = _glyphs.begin(); gi != _glyphs.end(); ++gi) {
69  DynamicTextGlyph *glyph = (*gi);
70  nassertv(glyph != (DynamicTextGlyph *)NULL);
71  glyph->_geom_count++;
72  }
73 }
74 
75 ////////////////////////////////////////////////////////////////////
76 // Function: GeomTextGlyph::Copy Assignment Operator
77 // Access: Public
78 // Description:
79 ////////////////////////////////////////////////////////////////////
80 void GeomTextGlyph::
81 operator = (const GeomTextGlyph &copy) {
82  Geom::operator = (copy);
83 
84  Glyphs::iterator gi;
85  for (gi = _glyphs.begin(); gi != _glyphs.end(); ++gi) {
86  DynamicTextGlyph *glyph = (*gi);
87  nassertv(glyph != (DynamicTextGlyph *)NULL);
88  glyph->_geom_count--;
89  nassertv((*gi)->_geom_count >= 0);
90  }
91  _glyphs = copy._glyphs;
92  for (gi = _glyphs.begin(); gi != _glyphs.end(); ++gi) {
93  DynamicTextGlyph *glyph = (*gi);
94  nassertv(glyph != (DynamicTextGlyph *)NULL);
95  glyph->_geom_count++;
96  }
97 }
98 
99 ////////////////////////////////////////////////////////////////////
100 // Function: GeomTextGlyph::Destructor
101 // Access: Public, Virtual
102 // Description:
103 ////////////////////////////////////////////////////////////////////
104 GeomTextGlyph::
105 ~GeomTextGlyph() {
106  Glyphs::iterator gi;
107  for (gi = _glyphs.begin(); gi != _glyphs.end(); ++gi) {
108  DynamicTextGlyph *glyph = (*gi);
109  nassertv(glyph != (DynamicTextGlyph *)NULL);
110  glyph->_geom_count--;
111  nassertv(glyph->_geom_count >= 0);
112  }
113 }
114 
115 ////////////////////////////////////////////////////////////////////
116 // Function: GeomTextGlyph::make_copy
117 // Access: Public, Virtual
118 // Description: Returns a newly-allocated Geom that is a shallow copy
119 // of this one. It will be a different Geom pointer,
120 // but its internal data may or may not be shared with
121 // that of the original Geom.
122 ////////////////////////////////////////////////////////////////////
124 make_copy() const {
125  return new GeomTextGlyph(*this);
126 }
127 
128 ////////////////////////////////////////////////////////////////////
129 // Function: GeomTextGlyph::copy_primitives_from
130 // Access: Public, Virtual
131 // Description: Copies the primitives from the indicated Geom into
132 // this one. This does require that both Geoms contain
133 // the same fundamental type primitives, both have a
134 // compatible shade model, and both use the same
135 // GeomVertexData. Both Geoms must also be the same
136 // specific class type (i.e. if one is a GeomTextGlyph,
137 // they both must be.)
138 //
139 // Returns true if the copy is successful, or false
140 // otherwise (because the Geoms were mismatched).
141 ////////////////////////////////////////////////////////////////////
142 bool GeomTextGlyph::
143 copy_primitives_from(const Geom *other) {
144  if (!Geom::copy_primitives_from(other)) {
145  return false;
146  }
147 
148  const GeomTextGlyph *tother;
149  DCAST_INTO_R(tother, other, false);
150 
151  // Also copy the glyph pointers.
152  Glyphs::const_iterator gi;
153  for (gi = tother->_glyphs.begin(); gi != tother->_glyphs.end(); ++gi) {
154  _glyphs.push_back(*gi);
155  (*gi)->_geom_count++;
156  }
157 
158  return true;
159 }
160 
161 ////////////////////////////////////////////////////////////////////
162 // Function: GeomTextGlyph::count_geom
163 // Access: Public
164 // Description: Records the reference count of the other Geom within
165 // this Geom, as if the primitives were copied in via
166 // copy_primitives_from() (but does not actually copy
167 // any primitives). This is particularly necessary for
168 // GeomTextGlyph's reference counting mechanism.
169 //
170 // Does nothing if the other Geom is not a
171 // GeomTextGlyph.
172 ////////////////////////////////////////////////////////////////////
173 void GeomTextGlyph::
174 count_geom(const Geom *other) {
175  if (other->is_of_type(GeomTextGlyph::get_class_type())) {
176  const GeomTextGlyph *tother;
177  DCAST_INTO_V(tother, other);
178 
179  Glyphs::const_iterator gi;
180  for (gi = tother->_glyphs.begin(); gi != tother->_glyphs.end(); ++gi) {
181  _glyphs.push_back(*gi);
182  (*gi)->_geom_count++;
183  }
184  }
185 }
186 
187 ////////////////////////////////////////////////////////////////////
188 // Function: GeomTextGlyph::output
189 // Access: Public, Virtual
190 // Description:
191 ////////////////////////////////////////////////////////////////////
192 void GeomTextGlyph::
193 output(ostream &out) const {
194  Geom::output(out);
195  out << ", glyphs: [";
196  Glyphs::const_iterator gi;
197  for (gi = _glyphs.begin(); gi != _glyphs.end(); ++gi) {
198  DynamicTextGlyph *glyph = (*gi);
199  nassertv(glyph != (DynamicTextGlyph *)NULL);
200  out << " " << glyph->get_character();
201  }
202  out << " ]";
203 }
204 
205 ////////////////////////////////////////////////////////////////////
206 // Function: GeomTextGlyph::write
207 // Access: Public, Virtual
208 // Description:
209 ////////////////////////////////////////////////////////////////////
210 void GeomTextGlyph::
211 write(ostream &out, int indent_level) const {
212  Geom::write(out, indent_level);
213  indent(out, indent_level)
214  << "Glyphs: [";
215  Glyphs::const_iterator gi;
216  for (gi = _glyphs.begin(); gi != _glyphs.end(); ++gi) {
217  DynamicTextGlyph *glyph = (*gi);
218  nassertv(glyph != (DynamicTextGlyph *)NULL);
219  out << " " << glyph->get_character();
220  }
221  out << " ]\n";
222 }
223 
224 ////////////////////////////////////////////////////////////////////
225 // Function: GeomTextGlyph::register_with_factory
226 // Access: Public, Static
227 // Description: Factory method to generate a GeomTextGlyph object
228 ////////////////////////////////////////////////////////////////////
229 void GeomTextGlyph::
231  BamReader::get_factory()->register_factory(get_class_type(), make_GeomTextGlyph);
232 }
233 
234 ////////////////////////////////////////////////////////////////////
235 // Function: GeomTextGlyph::make_GeomTextGlyph
236 // Access: Public
237 // Description: Factory method to generate a GeomTextGlyph object
238 ////////////////////////////////////////////////////////////////////
239 TypedWritable* GeomTextGlyph::
240 make_GeomTextGlyph(const FactoryParams &params) {
241  GeomTextGlyph *me = new GeomTextGlyph((DynamicTextGlyph *)NULL,
242  (GeomVertexData *)NULL);
243  DatagramIterator scan;
244  BamReader *manager;
245 
246  parse_params(params, scan, manager);
247  me->fillin(scan, manager);
248  return me;
249 }
250 
251 #endif // HAVE_FREETYPE
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:122
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:37
virtual Geom * make_copy() const
Returns a newly-allocated Geom that is a shallow copy of this one.
Definition: geom.cxx:118
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
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:40
void register_factory(TypeHandle handle, CreateFunc *func)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:90
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:213
virtual bool copy_primitives_from(const Geom *other)
Copies the primitives from the indicated Geom into this one.
Definition: geom.cxx:882
bool is_of_type(TypeHandle handle) const
Returns true if the current object is or derives from the indicated type.
Definition: typedObject.I:63
A class to retrieve the individual data elements previously stored in a Datagram. ...
void operator=(const Geom &copy)
The copy assignment operator is not pipeline-safe.
Definition: geom.cxx:84
static void register_with_read_factory()
Tells the BamReader how to create objects of type Geom.
Definition: geom.cxx:1671
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85