Panda3D
textNode.h
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 textNode.h
10  * @author drose
11  * @date 2002-03-13
12  */
13 
14 #ifndef TEXTNODE_H
15 #define TEXTNODE_H
16 
17 #include "pandabase.h"
18 
19 #include "config_text.h"
20 #include "textEncoder.h"
21 #include "textProperties.h"
22 #include "textFont.h"
23 #include "textAssembler.h"
24 #include "pandaNode.h"
25 #include "luse.h"
26 #include "geom.h"
27 #include "pmutex.h"
28 #include "mutexHolder.h"
29 
30 /**
31  * The primary interface to this module. This class does basic text assembly;
32  * given a string of text and a TextFont object, it creates a piece of
33  * geometry that may be placed in the 3-d or 2-d world to represent the
34  * indicated text.
35  *
36  * The TextNode may be used in one of two ways. Naively, it may simply be
37  * parented directly into the scene graph and rendered as if it were a
38  * GeomNode; in this mode, the actual polygon geometry that renders the text
39  * is not directly visible or accessible, but remains hidden within the
40  * TextNode.
41  *
42  * The second way TextNode may be used is as a text generator. To use it in
43  * this way, do not parent the TextNode to the scene graph; instead, set the
44  * properties of the text and call generate() to return an ordinary node,
45  * containing ordinary geometry, which you may use however you like. Each
46  * time you call generate() a new node is returned.
47  */
48 class EXPCL_PANDA_TEXT TextNode : public PandaNode, public TextEncoder, public TextProperties {
49 PUBLISHED:
50  explicit TextNode(const std::string &name);
51  explicit TextNode(const std::string &name, const TextProperties &copy);
52 protected:
53  TextNode(const TextNode &copy);
54  virtual PandaNode *make_copy() const;
55 
56 PUBLISHED:
57  ~TextNode();
58 
59  enum FlattenFlags {
60  FF_none = 0x0000,
61  FF_light = 0x0001,
62  FF_medium = 0x0002,
63  FF_strong = 0x0004,
64  FF_dynamic_merge = 0x0008,
65  };
66 
67  INLINE PN_stdfloat get_line_height() const;
68 
69  INLINE void set_max_rows(int max_rows);
70  INLINE void clear_max_rows();
71  INLINE bool has_max_rows() const;
72  INLINE int get_max_rows() const;
73  INLINE bool has_overflow() const;
74 
75  INLINE void set_frame_color(PN_stdfloat r, PN_stdfloat g, PN_stdfloat b, PN_stdfloat a);
76  INLINE void set_frame_color(const LColor &frame_color);
77  INLINE LColor get_frame_color() const;
78 
79  INLINE void set_card_border(PN_stdfloat size, PN_stdfloat uv_portion);
80  INLINE void clear_card_border();
81  INLINE PN_stdfloat get_card_border_size() const;
82  INLINE PN_stdfloat get_card_border_uv_portion() const;
83  INLINE bool has_card_border() const;
84 
85  INLINE void set_card_color(PN_stdfloat r, PN_stdfloat g, PN_stdfloat b, PN_stdfloat a);
86  INLINE void set_card_color(const LColor &card_color);
87  INLINE LColor get_card_color() const;
88 
89  INLINE void set_card_texture(Texture *card_texture);
90  INLINE void clear_card_texture();
91  INLINE bool has_card_texture() const;
92  INLINE Texture *get_card_texture() const;
93 
94  INLINE void set_frame_as_margin(PN_stdfloat left, PN_stdfloat right,
95  PN_stdfloat bottom, PN_stdfloat top);
96  INLINE void set_frame_actual(PN_stdfloat left, PN_stdfloat right,
97  PN_stdfloat bottom, PN_stdfloat top);
98  INLINE void clear_frame();
99  INLINE bool has_frame() const;
100  INLINE bool is_frame_as_margin() const;
101  INLINE LVecBase4 get_frame_as_set() const;
102  INLINE LVecBase4 get_frame_actual() const;
103 
104  INLINE void set_frame_line_width(PN_stdfloat line_width);
105  INLINE PN_stdfloat get_frame_line_width() const;
106  INLINE void set_frame_corners(bool corners);
107  INLINE bool get_frame_corners() const;
108 
109  INLINE void set_card_as_margin(PN_stdfloat left, PN_stdfloat right,
110  PN_stdfloat bottom, PN_stdfloat top);
111  INLINE void set_card_actual(PN_stdfloat left, PN_stdfloat right,
112  PN_stdfloat bottom, PN_stdfloat top);
113  INLINE void set_card_decal(bool card_decal);
114  INLINE void clear_card();
115  INLINE bool has_card() const;
116  INLINE bool get_card_decal() const;
117  INLINE bool is_card_as_margin() const;
118  INLINE LVecBase4 get_card_as_set() const;
119  INLINE LVecBase4 get_card_actual() const;
120  INLINE LVecBase4 get_card_transformed() const;
121 
122  INLINE void set_transform(const LMatrix4 &transform);
123  INLINE LMatrix4 get_transform() const;
124 
125  INLINE void set_coordinate_system(CoordinateSystem cs);
126  INLINE CoordinateSystem get_coordinate_system() const;
127 
128  INLINE void set_usage_hint(Geom::UsageHint usage_hint);
129  INLINE Geom::UsageHint get_usage_hint() const;
130 
131  INLINE void set_flatten_flags(int flatten_flags);
132  INLINE int get_flatten_flags() const;
133 
134  // These methods are inherited from TextProperties, but we override here so
135  // we can flag the TextNode as dirty when they have been changed.
136 
137  INLINE void set_font(TextFont *font);
138  INLINE void clear_font();
139 
140  INLINE void set_small_caps(bool small_caps);
141  INLINE void clear_small_caps();
142 
143  INLINE void set_small_caps_scale(PN_stdfloat small_caps_scale);
144  INLINE void clear_small_caps_scale();
145 
146  INLINE void set_slant(PN_stdfloat slant);
147  INLINE void clear_slant();
148 
149  INLINE void set_align(Alignment align_type);
150  INLINE void clear_align();
151 
152  INLINE void set_indent(PN_stdfloat indent);
153  INLINE void clear_indent();
154 
155  INLINE void set_wordwrap(PN_stdfloat wordwrap);
156  INLINE void clear_wordwrap();
157 
158  INLINE void set_text_color(PN_stdfloat r, PN_stdfloat g, PN_stdfloat b, PN_stdfloat a);
159  INLINE void set_text_color(const LColor &text_color);
160  INLINE void clear_text_color();
161 
162  INLINE void set_shadow_color(PN_stdfloat r, PN_stdfloat g, PN_stdfloat b, PN_stdfloat a);
163  INLINE void set_shadow_color(const LColor &shadow_color);
164  INLINE void clear_shadow_color();
165 
166  INLINE void set_shadow(PN_stdfloat xoffset, PN_stdfloat yoffset);
167  INLINE void set_shadow(const LVecBase2 &shadow_offset);
168  INLINE void clear_shadow();
169 
170  INLINE void set_bin(const std::string &bin);
171  INLINE void clear_bin();
172 
173  INLINE int set_draw_order(int draw_order);
174  INLINE void clear_draw_order();
175 
176  INLINE void set_tab_width(PN_stdfloat tab_width);
177  INLINE void clear_tab_width();
178 
179  INLINE void set_glyph_scale(PN_stdfloat glyph_scale);
180  INLINE void clear_glyph_scale();
181 
182  INLINE void set_glyph_shift(PN_stdfloat glyph_shift);
183  INLINE void clear_glyph_shift();
184 
185  // After the text has been set, you can query this to determine how it will
186  // be wordwrapped.
187  INLINE std::string get_wordwrapped_text() const;
188 
189  // These methods calculate the width of a single character or a line of text
190  // in the current font.
191  PN_stdfloat calc_width(wchar_t character) const;
192  INLINE PN_stdfloat calc_width(const std::string &line) const;
193 
194  bool has_exact_character(wchar_t character) const;
195  bool has_character(wchar_t character) const;
196  bool is_whitespace(wchar_t character) const;
197 
198  INLINE std::wstring get_wordwrapped_wtext() const;
199  PN_stdfloat calc_width(const std::wstring &line) const;
200 
201  virtual void output(std::ostream &out) const;
202  virtual void write(std::ostream &out, int indent_level = 0) const;
203 
204  // The following functions return information about the text that was last
205  // built (and is currently visible).
206  INLINE PN_stdfloat get_left() const;
207  INLINE PN_stdfloat get_right() const;
208  INLINE PN_stdfloat get_bottom() const;
209  INLINE PN_stdfloat get_top() const;
210  INLINE PN_stdfloat get_height() const;
211  INLINE PN_stdfloat get_width() const;
212 
213  INLINE LPoint3 get_upper_left_3d() const;
214  INLINE LPoint3 get_lower_right_3d() const;
215 
216  INLINE int get_num_rows() const;
217 
218  INLINE PT(PandaNode) generate();
219  INLINE void update();
220  INLINE void force_update();
221 
222  PT(PandaNode) get_internal_geom() const;
223 
224 PUBLISHED:
225  MAKE_PROPERTY(max_rows, get_max_rows, set_max_rows);
226  MAKE_PROPERTY(frame_color, get_frame_color, set_frame_color);
227  MAKE_PROPERTY(card_color, get_card_color, set_card_color);
228  MAKE_PROPERTY(card_texture, get_card_texture, set_card_texture);
229  MAKE_PROPERTY(frame_line_width, get_frame_line_width, set_frame_line_width);
230  MAKE_PROPERTY(frame_corners, get_frame_corners, set_frame_corners);
231  MAKE_PROPERTY(transform, get_transform, set_transform);
232  MAKE_PROPERTY(coordinate_system, get_coordinate_system, set_coordinate_system);
233  MAKE_PROPERTY(usage_hint, get_usage_hint, set_usage_hint);
234  MAKE_PROPERTY(flatten_flags, get_flatten_flags, set_flatten_flags);
235 
236  MAKE_PROPERTY2(font, has_font, get_font, set_font, clear_font);
237  MAKE_PROPERTY2(small_caps, has_small_caps, get_small_caps,
238  set_small_caps, clear_small_caps);
239  MAKE_PROPERTY2(small_caps_scale, has_small_caps_scale, get_small_caps_scale,
240  set_small_caps_scale, clear_small_caps_scale);
241  MAKE_PROPERTY2(slant, has_slant, get_slant, set_slant, clear_slant);
242  MAKE_PROPERTY2(underscore, has_underscore, get_underscore,
243  set_underscore, clear_underscore);
244  MAKE_PROPERTY2(underscore_height, has_underscore_height, get_underscore_height,
245  set_underscore_height, clear_underscore_height);
246  MAKE_PROPERTY2(align, has_align, get_align, set_align, clear_align);
247  MAKE_PROPERTY2(indent, has_indent, get_indent, set_indent, clear_indent);
248  MAKE_PROPERTY2(wordwrap, has_wordwrap, get_wordwrap, set_wordwrap, clear_wordwrap);
249  MAKE_PROPERTY2(preserve_trailing_whitespace,
250  has_preserve_trailing_whitespace, get_preserve_trailing_whitespace,
251  set_preserve_trailing_whitespace, clear_preserve_trailing_whitespace);
252  MAKE_PROPERTY2(text_color, has_text_color, get_text_color,
253  set_text_color, clear_text_color);
254  MAKE_PROPERTY2(shadow_color, has_shadow_color, get_shadow_color,
255  set_shadow_color, clear_shadow_color);
256  MAKE_PROPERTY2(shadow, has_shadow, get_shadow, set_shadow, clear_shadow);
257  MAKE_PROPERTY2(bin, has_bin, get_bin, set_bin, clear_bin);
258  MAKE_PROPERTY2(draw_order, has_draw_order, get_draw_order,
259  set_draw_order, clear_draw_order);
260  MAKE_PROPERTY2(tab_width, has_tab_width, get_tab_width,
261  set_tab_width, clear_tab_width);
262  MAKE_PROPERTY2(glyph_scale, has_glyph_scale, get_glyph_scale,
263  set_glyph_scale, clear_glyph_scale);
264  MAKE_PROPERTY2(glyph_shift, has_glyph_shift, get_glyph_shift,
265  set_glyph_shift, clear_glyph_shift);
266  MAKE_PROPERTY2(text_scale, has_text_scale, get_text_scale,
267  set_text_scale, clear_text_scale);
268 
269 public:
270  // From parent class TextEncoder;
271  virtual void text_changed() final;
272 
273  // From parent class PandaNode
274  virtual int get_unsafe_to_apply_attribs() const;
275  virtual void apply_attribs_to_vertices(const AccumulatedAttribs &attribs,
276  int attrib_types,
277  GeomTransformer &transformer);
278  virtual CPT(TransformState)
279  calc_tight_bounds(LPoint3 &min_point, LPoint3 &max_point,
280  bool &found_any,
281  const TransformState *transform,
282  Thread *current_thread) const;
283 
284  virtual bool cull_callback(CullTraverser *trav, CullTraverserData &data);
285  virtual bool is_renderable() const;
286 
287  virtual void compute_internal_bounds(CPT(BoundingVolume) &internal_bounds,
288  int &internal_vertices,
289  int pipeline_stage,
290  Thread *current_thread) const;
291 
292  virtual void r_prepare_scene(GraphicsStateGuardianBase *gsg,
293  const RenderState *node_state,
294  GeomTransformer &transformer,
295  Thread *current_thread);
296 
297 private:
298  INLINE void invalidate_no_measure();
299  INLINE void invalidate_with_measure();
300  INLINE void check_rebuild() const;
301  INLINE void check_measure() const;
302 
303  void do_rebuild();
304  void do_measure();
305 
306  PT(PandaNode) do_generate();
307  PT(PandaNode) do_get_internal_geom() const;
308 
309  PT(PandaNode) make_frame();
310  PT(PandaNode) make_card();
311  PT(PandaNode) make_card_with_border();
312 
313  static int count_geoms(PandaNode *node);
314 
315  Mutex _lock;
316  PT(PandaNode) _internal_geom;
317 
318  PT(Texture) _card_texture;
319  LColor _frame_color;
320  LColor _card_color;
321 
322  enum Flags {
323  F_has_frame = 0x0001,
324  F_frame_as_margin = 0x0002,
325  F_has_card = 0x0004,
326  F_card_as_margin = 0x0008,
327  F_has_card_texture = 0x0010,
328  F_frame_corners = 0x0020,
329  F_card_transp = 0x0040,
330  F_has_card_border = 0x0080,
331  F_needs_rebuild = 0x0100,
332  F_needs_measure = 0x0200,
333  F_has_overflow = 0x0400,
334  F_card_decal = 0x0800,
335  };
336 
337  int _flags;
338  int _max_rows;
339  GeomEnums::UsageHint _usage_hint;
340  int _flatten_flags;
341  PN_stdfloat _frame_width;
342  PN_stdfloat _card_border_size;
343  PN_stdfloat _card_border_uv_portion;
344 
345  LVector2 _frame_ul, _frame_lr;
346  LVector2 _card_ul, _card_lr;
347 
348  LMatrix4 _transform;
349  CoordinateSystem _coordinate_system;
350 
351  LPoint3 _ul3d, _lr3d;
352 
353  // Returned from TextAssembler:
354  LVector2 _text_ul, _text_lr;
355  int _num_rows;
356  std::wstring _wordwrapped_wtext;
357 
358  static PStatCollector _text_generate_pcollector;
359 
360 public:
361  static TypeHandle get_class_type() {
362  return _type_handle;
363  }
364  static void init_type() {
365  PandaNode::init_type();
366  TextProperties::init_type();
367  register_type(_type_handle, "TextNode",
368  PandaNode::get_class_type(),
369  TextProperties::get_class_type());
370  }
371  virtual TypeHandle get_type() const {
372  return get_class_type();
373  }
374  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
375 
376 private:
377  static TypeHandle _type_handle;
378 };
379 
380 #include "textNode.I"
381 
382 #endif
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
set_align
Specifies the alignment of the text within its margins.
A basic node of the scene graph or data graph.
Definition: pandaNode.h:64
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Indicates a coordinate-system transform on vertices.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This class can be used to convert text between multiple representations, e.g.
Definition: textEncoder.h:33
set_glyph_scale
Specifies the factor by which to scale each letter of the text as it is placed, in addition to any sc...
Represents a texture object, which is typically a single 2-d image but may also represent a 1-d or 3-...
Definition: texture.h:71
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
Definition: register_type.I:22
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This collects together the pieces of data that are accumulated for each node while walking the scene ...
A standard mutex, or mutual exclusion lock.
Definition: pmutex.h:38
An encapsulation of a font; i.e.
Definition: textFont.h:32
set_transform
Sets the transform that will be applied to this node and below.
Definition: pandaNode.h:183
set_tab_width
Sets the width of each tab stop, in screen units.
This is an abstract class for any volume in any sense which can be said to define the locality of ref...
A lightweight class that represents a single element that may be timed and/or counted via stats.
set_shadow
Specifies that the text should be drawn with a shadow, by creating a second copy of the text and offs...
This class is used by the SceneGraphReducer to maintain and accumulate the set of attributes we have ...
set_draw_order
Sets the drawing order of text created by the TextNode.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
set_small_caps
Sets the small_caps flag.
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20
set_wordwrap
Sets the text up to automatically wordwrap when it exceeds the indicated width.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual PandaNode * make_copy() const
Returns a newly-allocated PandaNode that is a shallow copy of this one.
Definition: pandaNode.cxx:487
This represents a unique collection of RenderAttrib objects that correspond to a particular renderabl...
Definition: renderState.h:47
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
set_glyph_shift
Specifies a vertical amount to shift each letter of the text as it is placed.
set_slant
Specifies the factor by which the text slants to the right.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is a base class for the GraphicsStateGuardian class, which is itself a base class for the variou...
set_indent
Specifies the amount of extra space that is inserted before the first character of each line.
A thread; that is, a lightweight process.
Definition: thread.h:46
set_bin
Names the CullBin that the text geometry should be assigned to.
The primary interface to this module.
Definition: textNode.h:48
This defines the set of visual properties that may be assigned to the individual characters of the te...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
set_small_caps_scale
Sets the scale factor applied to lowercase letters from their uppercase equivalents,...
set_font
Sets the font that will be used when making text.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
This object performs a depth-first traversal of the scene graph, with optional view-frustum culling,...
Definition: cullTraverser.h:45
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
An object specifically designed to transform the vertices of a Geom without disturbing indexing or af...