Panda3D
|
00001 // Filename: geomTextGlyph.cxx 00002 // Created by: drose (31Mar05) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "geomTextGlyph.h" 00016 00017 #ifdef HAVE_FREETYPE 00018 00019 #include "datagramIterator.h" 00020 #include "bamReader.h" 00021 #include "indent.h" 00022 00023 TypeHandle GeomTextGlyph::_type_handle; 00024 00025 00026 //////////////////////////////////////////////////////////////////// 00027 // Function: GeomTextGlyph::Constructor 00028 // Access: Public 00029 // Description: 00030 //////////////////////////////////////////////////////////////////// 00031 GeomTextGlyph:: 00032 GeomTextGlyph(DynamicTextGlyph *glyph, const GeomVertexData *data) : 00033 Geom(data) 00034 { 00035 // Initially, there is only one glyph in the Geom. There might be 00036 // additional Glyphs later when we flatten the graph and call 00037 // Geom::unify(). 00038 if (glyph != (DynamicTextGlyph *)NULL) { 00039 _glyphs.reserve(1); 00040 _glyphs.push_back(glyph); 00041 glyph->_geom_count++; 00042 } 00043 } 00044 00045 //////////////////////////////////////////////////////////////////// 00046 // Function: GeomTextGlyph::Constructor 00047 // Access: Public 00048 // Description: 00049 //////////////////////////////////////////////////////////////////// 00050 GeomTextGlyph:: 00051 GeomTextGlyph(const GeomVertexData *data) : 00052 Geom(data) 00053 { 00054 // With this constructor, there are no glyphs initially. 00055 } 00056 00057 //////////////////////////////////////////////////////////////////// 00058 // Function: GeomTextGlyph::Copy Constructor 00059 // Access: Public 00060 // Description: 00061 //////////////////////////////////////////////////////////////////// 00062 GeomTextGlyph:: 00063 GeomTextGlyph(const GeomTextGlyph ©) : 00064 Geom(copy), 00065 _glyphs(copy._glyphs) 00066 { 00067 Glyphs::iterator gi; 00068 for (gi = _glyphs.begin(); gi != _glyphs.end(); ++gi) { 00069 DynamicTextGlyph *glyph = (*gi); 00070 nassertv(glyph != (DynamicTextGlyph *)NULL); 00071 glyph->_geom_count++; 00072 } 00073 } 00074 00075 //////////////////////////////////////////////////////////////////// 00076 // Function: GeomTextGlyph::Copy Assignment Operator 00077 // Access: Public 00078 // Description: 00079 //////////////////////////////////////////////////////////////////// 00080 void GeomTextGlyph:: 00081 operator = (const GeomTextGlyph ©) { 00082 Geom::operator = (copy); 00083 00084 Glyphs::iterator gi; 00085 for (gi = _glyphs.begin(); gi != _glyphs.end(); ++gi) { 00086 DynamicTextGlyph *glyph = (*gi); 00087 nassertv(glyph != (DynamicTextGlyph *)NULL); 00088 glyph->_geom_count--; 00089 nassertv((*gi)->_geom_count >= 0); 00090 } 00091 _glyphs = copy._glyphs; 00092 for (gi = _glyphs.begin(); gi != _glyphs.end(); ++gi) { 00093 DynamicTextGlyph *glyph = (*gi); 00094 nassertv(glyph != (DynamicTextGlyph *)NULL); 00095 glyph->_geom_count++; 00096 } 00097 } 00098 00099 //////////////////////////////////////////////////////////////////// 00100 // Function: GeomTextGlyph::Destructor 00101 // Access: Public, Virtual 00102 // Description: 00103 //////////////////////////////////////////////////////////////////// 00104 GeomTextGlyph:: 00105 ~GeomTextGlyph() { 00106 Glyphs::iterator gi; 00107 for (gi = _glyphs.begin(); gi != _glyphs.end(); ++gi) { 00108 DynamicTextGlyph *glyph = (*gi); 00109 nassertv(glyph != (DynamicTextGlyph *)NULL); 00110 glyph->_geom_count--; 00111 nassertv(glyph->_geom_count >= 0); 00112 } 00113 } 00114 00115 //////////////////////////////////////////////////////////////////// 00116 // Function: GeomTextGlyph::make_copy 00117 // Access: Public, Virtual 00118 // Description: Returns a newly-allocated Geom that is a shallow copy 00119 // of this one. It will be a different Geom pointer, 00120 // but its internal data may or may not be shared with 00121 // that of the original Geom. 00122 //////////////////////////////////////////////////////////////////// 00123 Geom *GeomTextGlyph:: 00124 make_copy() const { 00125 return new GeomTextGlyph(*this); 00126 } 00127 00128 //////////////////////////////////////////////////////////////////// 00129 // Function: GeomTextGlyph::copy_primitives_from 00130 // Access: Public, Virtual 00131 // Description: Copies the primitives from the indicated Geom into 00132 // this one. This does require that both Geoms contain 00133 // the same fundamental type primitives, both have a 00134 // compatible shade model, and both use the same 00135 // GeomVertexData. Both Geoms must also be the same 00136 // specific class type (i.e. if one is a GeomTextGlyph, 00137 // they both must be.) 00138 // 00139 // Returns true if the copy is successful, or false 00140 // otherwise (because the Geoms were mismatched). 00141 //////////////////////////////////////////////////////////////////// 00142 bool GeomTextGlyph:: 00143 copy_primitives_from(const Geom *other) { 00144 if (!Geom::copy_primitives_from(other)) { 00145 return false; 00146 } 00147 00148 const GeomTextGlyph *tother; 00149 DCAST_INTO_R(tother, other, false); 00150 00151 // Also copy the glyph pointers. 00152 Glyphs::const_iterator gi; 00153 for (gi = tother->_glyphs.begin(); gi != tother->_glyphs.end(); ++gi) { 00154 _glyphs.push_back(*gi); 00155 (*gi)->_geom_count++; 00156 } 00157 00158 return true; 00159 } 00160 00161 //////////////////////////////////////////////////////////////////// 00162 // Function: GeomTextGlyph::count_geom 00163 // Access: Public 00164 // Description: Records the reference count of the other Geom within 00165 // this Geom, as if the primitives were copied in via 00166 // copy_primitives_from() (but does not actually copy 00167 // any primitives). This is particularly necessary for 00168 // GeomTextGlyph's reference counting mechanism. 00169 // 00170 // Does nothing if the other Geom is not a 00171 // GeomTextGlyph. 00172 //////////////////////////////////////////////////////////////////// 00173 void GeomTextGlyph:: 00174 count_geom(const Geom *other) { 00175 if (other->is_of_type(GeomTextGlyph::get_class_type())) { 00176 const GeomTextGlyph *tother; 00177 DCAST_INTO_V(tother, other); 00178 00179 Glyphs::const_iterator gi; 00180 for (gi = tother->_glyphs.begin(); gi != tother->_glyphs.end(); ++gi) { 00181 _glyphs.push_back(*gi); 00182 (*gi)->_geom_count++; 00183 } 00184 } 00185 } 00186 00187 //////////////////////////////////////////////////////////////////// 00188 // Function: GeomTextGlyph::output 00189 // Access: Public, Virtual 00190 // Description: 00191 //////////////////////////////////////////////////////////////////// 00192 void GeomTextGlyph:: 00193 output(ostream &out) const { 00194 Geom::output(out); 00195 out << ", glyphs: ["; 00196 Glyphs::const_iterator gi; 00197 for (gi = _glyphs.begin(); gi != _glyphs.end(); ++gi) { 00198 DynamicTextGlyph *glyph = (*gi); 00199 nassertv(glyph != (DynamicTextGlyph *)NULL); 00200 out << " " << glyph->get_character(); 00201 } 00202 out << " ]"; 00203 } 00204 00205 //////////////////////////////////////////////////////////////////// 00206 // Function: GeomTextGlyph::write 00207 // Access: Public, Virtual 00208 // Description: 00209 //////////////////////////////////////////////////////////////////// 00210 void GeomTextGlyph:: 00211 write(ostream &out, int indent_level) const { 00212 Geom::write(out, indent_level); 00213 indent(out, indent_level) 00214 << "Glyphs: ["; 00215 Glyphs::const_iterator gi; 00216 for (gi = _glyphs.begin(); gi != _glyphs.end(); ++gi) { 00217 DynamicTextGlyph *glyph = (*gi); 00218 nassertv(glyph != (DynamicTextGlyph *)NULL); 00219 out << " " << glyph->get_character(); 00220 } 00221 out << " ]\n"; 00222 } 00223 00224 //////////////////////////////////////////////////////////////////// 00225 // Function: GeomTextGlyph::register_with_factory 00226 // Access: Public, Static 00227 // Description: Factory method to generate a GeomTextGlyph object 00228 //////////////////////////////////////////////////////////////////// 00229 void GeomTextGlyph:: 00230 register_with_read_factory() { 00231 BamReader::get_factory()->register_factory(get_class_type(), make_GeomTextGlyph); 00232 } 00233 00234 //////////////////////////////////////////////////////////////////// 00235 // Function: GeomTextGlyph::make_GeomTextGlyph 00236 // Access: Public 00237 // Description: Factory method to generate a GeomTextGlyph object 00238 //////////////////////////////////////////////////////////////////// 00239 TypedWritable* GeomTextGlyph:: 00240 make_GeomTextGlyph(const FactoryParams ¶ms) { 00241 GeomTextGlyph *me = new GeomTextGlyph((DynamicTextGlyph *)NULL, 00242 (GeomVertexData *)NULL); 00243 DatagramIterator scan; 00244 BamReader *manager; 00245 00246 parse_params(params, scan, manager); 00247 me->fillin(scan, manager); 00248 return me; 00249 } 00250 00251 #endif // HAVE_FREETYPE