Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/text/textNode.h

Go to the documentation of this file.
00001 // Filename: textNode.h
00002 // Created by:  drose (13Mar02)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #ifndef TEXTNODE_H
00020 #define TEXTNODE_H
00021 
00022 #include "pandabase.h"
00023 
00024 #include "config_text.h"
00025 #include "textEncoder.h"
00026 #include "textFont.h"
00027 #include "unicodeLatinMap.h"
00028 #include "pandaNode.h"
00029 #include "luse.h"
00030 
00031 class StringDecoder;
00032 
00033 ////////////////////////////////////////////////////////////////////
00034 //       Class : TextNode
00035 // Description : The primary interface to this module.  This class
00036 //               does basic text assembly; given a string of text and
00037 //               a TextFont object, it creates a piece of geometry
00038 //               that may be placed in the 3-d or 2-d world to
00039 //               represent the indicated text.
00040 //
00041 //               The TextNode may be used in one of two ways.
00042 //               Naively, it may simply be parented directly into the
00043 //               scene graph and rendered as if it were a GeomNode; in
00044 //               this mode, the actual polygon geometry that renders
00045 //               the text is not directly visible or accessible, but
00046 //               remains hidden within the TextNode.
00047 //
00048 //               The second way TextNode may be used is as a text
00049 //               generator.  To use it in this way, do not parent the
00050 //               TextNode to the scene graph; instead, set the
00051 //               properties of the text and call generate() to return
00052 //               an ordinary node, containing ordinary geometry, which
00053 //               you may use however you like.  Each time you call
00054 //               generate() a new node is returned.
00055 ////////////////////////////////////////////////////////////////////
00056 class EXPCL_PANDA TextNode : public PandaNode, public TextEncoder {
00057 PUBLISHED:
00058   TextNode(const string &name);
00059   ~TextNode();
00060 
00061   enum Alignment {
00062     A_left,
00063     A_right,
00064     A_center,
00065   };
00066  
00067   INLINE int freeze();
00068   INLINE int thaw();
00069 
00070   INLINE void set_font(TextFont *font);
00071   INLINE TextFont *get_font() const;
00072 
00073   INLINE static void set_default_font(TextFont *);
00074   INLINE static TextFont *get_default_font();
00075 
00076   INLINE float get_line_height() const;
00077 
00078   INLINE void set_small_caps(bool small_caps);
00079   INLINE bool get_small_caps() const;
00080   INLINE void set_small_caps_scale(float small_caps_scale);
00081   INLINE float get_small_caps_scale() const;
00082 
00083   INLINE void set_slant(float slant);
00084   INLINE float get_slant() const;
00085 
00086   INLINE void set_align(Alignment align_type);
00087   INLINE Alignment get_align() const;
00088 
00089   INLINE void set_wordwrap(float width);
00090   INLINE void clear_wordwrap();
00091   INLINE bool has_wordwrap() const;
00092   INLINE float get_wordwrap() const;
00093 
00094   INLINE void set_text_color(float r, float g, float b, float a);
00095   INLINE void set_text_color(const Colorf &text_color);
00096   INLINE void clear_text_color();
00097   INLINE bool has_text_color() const;
00098   INLINE Colorf get_text_color() const;
00099 
00100   INLINE void set_frame_color(float r, float g, float b, float a);
00101   INLINE void set_frame_color(const Colorf &frame_color);
00102   INLINE Colorf get_frame_color() const;
00103 
00104   INLINE void set_card_border(float size, float uv_portion);
00105   INLINE void clear_card_border();
00106   INLINE float get_card_border_size() const;
00107   INLINE float get_card_border_uv_portion() const;
00108   INLINE bool has_card_border() const;
00109 
00110   INLINE void set_card_color(float r, float g, float b, float a);
00111   INLINE void set_card_color(const Colorf &card_color);
00112   INLINE Colorf get_card_color() const;
00113 
00114   INLINE void set_card_texture(Texture *card_texture);
00115   INLINE void clear_card_texture();
00116   INLINE bool has_card_texture() const;
00117   INLINE Texture *get_card_texture() const;
00118 
00119   INLINE void set_shadow_color(float r, float g, float b, float a);
00120   INLINE void set_shadow_color(const Colorf &shadow_color);
00121   INLINE Colorf get_shadow_color() const;
00122 
00123   INLINE void set_frame_as_margin(float left, float right,
00124                                   float bottom, float top);
00125   INLINE void set_frame_actual(float left, float right,
00126                                float bottom, float top);
00127   INLINE void clear_frame();
00128   INLINE bool has_frame() const;
00129   INLINE bool is_frame_as_margin() const;
00130   INLINE LVecBase4f get_frame_as_set() const;
00131   INLINE LVecBase4f get_frame_actual() const;
00132 
00133   INLINE void set_frame_line_width(float line_width);
00134   INLINE float get_frame_line_width() const;
00135   INLINE void set_frame_corners(bool corners);
00136   INLINE bool get_frame_corners() const;
00137 
00138   INLINE void set_card_as_margin(float left, float right,
00139                                  float bottom, float top);
00140   INLINE void set_card_actual(float left, float right,
00141                               float bottom, float top);
00142   INLINE void clear_card();
00143   INLINE bool has_card() const;
00144   INLINE bool is_card_as_margin() const;
00145   INLINE LVecBase4f get_card_as_set() const;
00146   INLINE LVecBase4f get_card_actual() const;
00147   INLINE LVecBase4f get_card_transformed() const;
00148 
00149   INLINE void set_shadow(float xoffset, float yoffset);
00150   INLINE void clear_shadow();
00151   INLINE bool has_shadow() const;
00152   INLINE LVecBase2f get_shadow() const;
00153 
00154   INLINE void set_bin(const string &bin);
00155   INLINE void clear_bin();
00156   INLINE bool has_bin() const;
00157   INLINE const string &get_bin() const;
00158 
00159   INLINE int set_draw_order(int draw_order);
00160   INLINE int get_draw_order() const;
00161 
00162   INLINE void set_transform(const LMatrix4f &transform);
00163   INLINE LMatrix4f get_transform() const;
00164 
00165   INLINE void set_coordinate_system(CoordinateSystem cs);
00166   INLINE CoordinateSystem get_coordinate_system() const;
00167 
00168   // These methods are inherited from TextEncoder, but we override
00169   // here so we can flag the TextNode as dirty when they have been
00170   // change.
00171   INLINE void set_text(const string &text);
00172   INLINE void set_text(const string &text, Encoding encoding);
00173   INLINE void clear_text();
00174   INLINE void append_text(const string &text);
00175   INLINE void append_unicode_char(int character);
00176 
00177   INLINE float calc_width(int character) const;
00178   INLINE float calc_width(const string &line) const;
00179   string wordwrap_to(const string &text, float wordwrap_width,
00180                      bool preserve_trailing_whitespace) const;
00181 
00182   virtual void write(ostream &out, int indent_level = 0) const;
00183 
00184   // The following functions return information about the text that
00185   // was last built (and is currently visible).
00186   INLINE float get_left() const;
00187   INLINE float get_right() const;
00188   INLINE float get_bottom() const;
00189   INLINE float get_top() const;
00190   INLINE float get_height() const;
00191   INLINE float get_width() const;
00192 
00193   INLINE LPoint3f get_upper_left_3d() const;
00194   INLINE LPoint3f get_lower_right_3d() const;
00195 
00196   INLINE int get_num_rows() const;
00197 
00198   PT(PandaNode) generate();
00199   INLINE void update();
00200   INLINE void force_update();
00201 
00202 public:
00203   // Direct support for wide-character strings.
00204   INLINE void set_wtext(const wstring &wtext);
00205   INLINE void append_wtext(const wstring &text);
00206 
00207   INLINE float calc_width(const wstring &line) const;
00208   INLINE wstring wordwrap_to(const wstring &wtext, float wordwrap_width,
00209                              bool preserve_trailing_whitespace) const;
00210 
00211   // From parent class PandaNode
00212   virtual int get_unsafe_to_apply_attribs() const;
00213   virtual void apply_attribs_to_vertices(const AccumulatedAttribs &attribs,
00214                                          int attrib_types,
00215                                          GeomTransformer &transformer);
00216   virtual CPT(TransformState)
00217     calc_tight_bounds(LPoint3f &min_point, LPoint3f &max_point,
00218                       bool &found_any,
00219                       const TransformState *transform) const;
00220 
00221   virtual bool has_cull_callback() const;
00222   virtual bool cull_callback(CullTraverser *trav, CullTraverserData &data);
00223 
00224   virtual BoundingVolume *recompute_internal_bound();
00225 
00226 private:
00227   INLINE void invalidate_no_measure();
00228   INLINE void invalidate_with_measure();
00229   INLINE void check_rebuild() const;
00230   INLINE void check_measure() const;
00231 
00232   void do_rebuild();
00233   void do_measure();
00234 
00235 #ifndef CPPPARSER  // interrogate has a bit of trouble with wstring.
00236   float assemble_row(wstring::iterator &si, const wstring::iterator &send, 
00237                      TextFont *font, GeomNode *dest, const LMatrix4f &mat);
00238   PT(PandaNode) assemble_text(wstring::iterator si, const wstring::iterator &send,
00239                               TextFont *font,
00240                               LVector2f &ul, LVector2f &lr, int &num_rows);
00241   float measure_row(wstring::iterator &si, const wstring::iterator &send,
00242                     TextFont *font);
00243   void measure_text(wstring::iterator si, const wstring::iterator &send,
00244                     TextFont *font,
00245                     LVector2f &ul, LVector2f &lr, int &num_rows);
00246 #endif  // CPPPARSER
00247 
00248   enum CheesyPlacement {
00249     CP_above,
00250     CP_below,
00251     CP_top,
00252     CP_bottom,
00253     CP_within,
00254   };
00255   enum CheesyTransform {
00256     CT_none,
00257     CT_mirror_x,
00258     CT_mirror_y,
00259     CT_rotate_90,
00260     CT_rotate_180,
00261     CT_rotate_270,
00262     CT_squash,
00263     CT_squash_mirror_y,
00264     CT_squash_mirror_diag,
00265     CT_small_squash,
00266     CT_small_squash_mirror_y,
00267     CT_small,
00268     CT_small_rotate_270,
00269     CT_tiny,
00270     CT_tiny_mirror_x,
00271     CT_tiny_rotate_270,
00272   };
00273 
00274   void get_character_glyphs(int character, TextFont *font,
00275                             bool &got_glyph, const TextGlyph *&glyph,
00276                             const TextGlyph *&second_glyph,
00277                             UnicodeLatinMap::AccentType &accent_type,
00278                             int &additional_flags,
00279                             float &glyph_scale, float &advance_scale);
00280 
00281   void tack_on_accent(UnicodeLatinMap::AccentType accent_type,
00282                       const LPoint3f &min_vert, const LPoint3f &max_vert,
00283                       const LPoint3f &centroid,
00284                       TextFont *font, GeomNode *dest, 
00285                       Geom *geom_array[], int &num_geoms);
00286   bool tack_on_accent(char accent_mark, CheesyPlacement placement,
00287                       CheesyTransform transform,
00288                       const LPoint3f &min_vert, const LPoint3f &max_vert,
00289                       const LPoint3f &centroid,
00290                       TextFont *font, GeomNode *dest, 
00291                       Geom *geom_array[], int &num_geoms);
00292 
00293   PT(PandaNode) make_frame();
00294   PT(PandaNode) make_card();
00295   PT(PandaNode) make_card_with_border();
00296 
00297   static void load_default_font();
00298 
00299   PT(TextFont) _font;
00300   PT(PandaNode) _internal_geom;
00301 
00302   float _slant;
00303 
00304   PT(Texture) _card_texture;
00305   Colorf _text_color;
00306   Colorf _shadow_color;
00307   Colorf _frame_color;
00308   Colorf _card_color;
00309 
00310   enum Flags {
00311     F_has_text_color   =  0x00000001,
00312     F_has_wordwrap     =  0x00000002,
00313     F_has_frame        =  0x00000004,
00314     F_frame_as_margin  =  0x00000008,
00315     F_has_card         =  0x00000010,
00316     F_card_as_margin   =  0x00000020,
00317     F_has_card_texture =  0x00000040,
00318     F_has_shadow       =  0x00000080,
00319     F_frame_corners    =  0x00000100,
00320     F_card_transp      =  0x00000200,
00321     F_has_card_border  =  0x00000400,
00322     F_needs_rebuild    =  0x00004000,
00323     F_needs_measure    =  0x00008000,
00324     F_small_caps       =  0x00010000,
00325   };
00326 
00327   int _flags;
00328   Alignment _align;
00329   float _wordwrap_width;
00330   float _frame_width;
00331   float _card_border_size;
00332   float _card_border_uv_portion;
00333   float _small_caps_scale;
00334 
00335   LVector2f _frame_ul, _frame_lr;
00336   LVector2f _card_ul, _card_lr;
00337   LVector2f _shadow_offset;
00338 
00339   string _bin;
00340   int _draw_order;
00341 
00342   LMatrix4f _transform;
00343   CoordinateSystem _coordinate_system;
00344 
00345   LPoint2f _ul2d, _lr2d;
00346   LPoint3f _ul3d, _lr3d;
00347   int _num_rows;
00348 
00349   static PT(TextFont) _default_font;
00350   static bool _loaded_default_font;
00351 
00352 public:
00353   static TypeHandle get_class_type() {
00354     return _type_handle;
00355   }
00356   static void init_type() {
00357     PandaNode::init_type();
00358     register_type(_type_handle, "TextNode",
00359                   PandaNode::get_class_type(),
00360                   TextEncoder::get_class_type());
00361   }
00362   virtual TypeHandle get_type() const {
00363     return get_class_type();
00364   }
00365   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00366 
00367 private:
00368   static TypeHandle _type_handle;
00369 };
00370 
00371 #include "textNode.I"
00372 
00373 #endif

Generated on Fri May 2 00:44:22 2003 for Panda by doxygen1.3