Panda3D
textAssembler.I
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 textAssembler.I
10  * @author drose
11  * @date 2004-04-06
12  */
13 
14 /**
15  * Specifies the UsageHint that will be applied to generated geometry. The
16  * default is UH_static, which is probably the right setting, but if you know
17  * the TextNode's geometry will have a short lifespan, it may be better to set
18  * it to UH_stream. See geomEnums.h.
19  */
20 INLINE void TextAssembler::
21 set_usage_hint(Geom::UsageHint usage_hint) {
22  _usage_hint = usage_hint;
23 }
24 
25 /**
26  * Returns the UsageHint that will be applied to generated geometry. See
27  * set_usage_hint().
28  */
29 INLINE Geom::UsageHint TextAssembler::
30 get_usage_hint() const {
31  return _usage_hint;
32 }
33 
34 /**
35  * If max_rows is greater than zero, no more than max_rows will be accepted.
36  * Text beyond that will be truncated.
37  *
38  * Setting this will not truncate text immediately. You must follow this up
39  * with a call to set_wtext() to truncate the existing text.
40  */
41 INLINE void TextAssembler::
42 set_max_rows(int max_rows) {
43  _max_rows = max_rows;
44 }
45 
46 /**
47  * If max_rows is greater than zero, no more than max_rows will be accepted.
48  * Text beyond that will be truncated.
49  */
50 INLINE int TextAssembler::
51 get_max_rows() const {
52  return _max_rows;
53 }
54 
55 /**
56  * Sets the dynamic_merge flag. See TextNode::set_flatten_flags().
57  */
58 INLINE void TextAssembler::
59 set_dynamic_merge(bool dynamic_merge) {
60  _dynamic_merge = dynamic_merge;
61 }
62 
63 /**
64  * Returns the dynamic_merge flag. See TextNode::set_flatten_flags().
65  */
66 INLINE bool TextAssembler::
67 get_dynamic_merge() const {
68  return _dynamic_merge;
69 }
70 
71 /**
72  * Sets the multiline mode flag. Set the multiline mode to allow text to
73  * wrap. It defaults to true.
74  */
75 INLINE void TextAssembler::
76 set_multiline_mode(bool flag) {
77  _multiline_mode = flag;
78 }
79 
80 /**
81  * Returns the multline_mode flag. See TextNode::set_multiline_mode().
82  */
83 INLINE bool TextAssembler::
84 get_multiline_mode() const {
85  return _multiline_mode;
86 }
87 
88 /**
89  * Specifies the default TextProperties that are applied to the text in the
90  * absence of any nested property change sequences.
91  */
92 INLINE void TextAssembler::
93 set_properties(const TextProperties &properties) {
94  _initial_cprops = new ComputedProperties(properties);
95 }
96 
97 /**
98  * Returns the default TextProperties that are applied to the text in the
99  * absence of any nested property change sequences.
100  */
101 INLINE const TextProperties &TextAssembler::
102 get_properties() const {
103  return _initial_cprops->_properties;
104 }
105 
106 /**
107  * Returns the upper-left corner of the assembled text, in 2-d text
108  * coordinates.
109  */
110 INLINE const LVector2 &TextAssembler::
111 get_ul() const {
112  return _ul;
113 }
114 
115 /**
116  * Returns the lower-right corner of the assembled text, in 2-d text
117  * coordinates.
118  */
119 INLINE const LVector2 &TextAssembler::
120 get_lr() const {
121  return _lr;
122 }
123 
124 /**
125  * Computes the row index of the nth character or graphic object in the text
126  * and returns it.
127  *
128  * If the nth character is not a normal printable character with a position in
129  * the wordwrapped string, returns -1 (for instance, a soft-hyphen character,
130  * or a newline character, may not have a corresponding position).
131  */
132 int TextAssembler::
133 calc_r(int n) const {
134  int r, c;
135  if (calc_r_c(r, c, n)) {
136  return r;
137  }
138  return -1;
139 }
140 
141 /**
142  * Computes the column index of the nth character or graphic object in the
143  * text and returns it.
144  *
145  * If the nth character is not a normal printable character with a position in
146  * the wordwrapped string, returns -1 (for instance, a soft-hyphen character,
147  * or a newline character, may not have a corresponding position).
148  */
149 int TextAssembler::
150 calc_c(int n) const {
151  int r, c;
152  if (calc_r_c(r, c, n)) {
153  return c;
154  }
155  return -1;
156 }
157 
158 /**
159  * Returns the number of characters of text, before wordwrapping.
160  */
161 INLINE int TextAssembler::
163  return _text_string.size();
164 }
165 
166 /**
167  * Returns the character at the indicated position in the pre-wordwrapped
168  * string. If the object at this position is a graphic object instead of a
169  * character, returns 0.
170  */
171 INLINE wchar_t TextAssembler::
172 get_character(int n) const {
173  nassertr(n >= 0 && n < (int)_text_string.size(), 0);
174  return _text_string[n]._character;
175 }
176 
177 /**
178  * Returns the graphic object at the indicated position in the pre-wordwrapped
179  * string. If the object at this position is a character instead of a graphic
180  * object, returns NULL.
181  */
182 INLINE const TextGraphic *TextAssembler::
183 get_graphic(int n) const {
184  nassertr(n >= 0 && n < (int)_text_string.size(), 0);
185  return _text_string[n]._graphic;
186 }
187 
188 /**
189  * Returns the TextProperties in effect for the object at the indicated
190  * position in the pre-wordwrapped string.
191  */
192 INLINE const TextProperties &TextAssembler::
193 get_properties(int n) const {
194  nassertr(n >= 0 && n < (int)_text_string.size(), *(new TextProperties()));
195  return _text_string[n]._cprops->_properties;
196 }
197 
198 /**
199  * Returns the width of the character or object at the indicated position in
200  * the pre-wordwrapped string.
201  */
202 INLINE PN_stdfloat TextAssembler::
203 get_width(int n) const {
204  nassertr(n >= 0 && n < (int)_text_string.size(), 0.0f);
205 
206  return calc_width(_text_string[n]);
207 }
208 
209 /**
210  * Returns the number of rows of text after it has all been wordwrapped and
211  * assembled.
212  */
213 INLINE int TextAssembler::
214 get_num_rows() const {
215  return _text_block.size();
216 }
217 
218 /**
219  * Returns the number of characters and/or graphic objects in the nth row.
220  */
221 INLINE int TextAssembler::
222 get_num_cols(int r) const {
223  nassertr(r >= 0 && r <= (int)_text_block.size(), 0);
224  if (r == (int)_text_block.size()) {
225  return 0;
226  }
227  return _text_block[r]._string.size();
228 }
229 
230 /**
231  * Returns the character at the indicated position in the indicated row. If
232  * the object at this position is a graphic object instead of a character,
233  * returns 0.
234  */
235 INLINE wchar_t TextAssembler::
236 get_character(int r, int c) const {
237  nassertr(r >= 0 && r < (int)_text_block.size(), 0);
238  nassertr(c >= 0 && c < (int)_text_block[r]._string.size(), 0);
239  return _text_block[r]._string[c]._character;
240 }
241 
242 /**
243  * Returns the graphic object at the indicated position in the indicated row.
244  * If the object at this position is a character instead of a graphic object,
245  * returns NULL.
246  */
247 INLINE const TextGraphic *TextAssembler::
248 get_graphic(int r, int c) const {
249  nassertr(r >= 0 && r < (int)_text_block.size(), 0);
250  nassertr(c >= 0 && c < (int)_text_block[r]._string.size(), 0);
251  return _text_block[r]._string[c]._graphic;
252 }
253 
254 /**
255  * Returns the TextProperties in effect for the object at the indicated
256  * position in the indicated row.
257  */
258 INLINE const TextProperties &TextAssembler::
259 get_properties(int r, int c) const {
260  nassertr(r >= 0 && r < (int)_text_block.size(), *(new TextProperties()));
261  nassertr(c >= 0 && c < (int)_text_block[r]._string.size(), *(new TextProperties()));
262  return _text_block[r]._string[c]._cprops->_properties;
263 }
264 
265 /**
266  * Returns the width of the character or object at the indicated position in
267  * the indicated row.
268  */
269 INLINE PN_stdfloat TextAssembler::
270 get_width(int r, int c) const {
271  nassertr(r >= 0 && r < (int)_text_block.size(), 0.0f);
272  nassertr(c >= 0 && c < (int)_text_block[r]._string.size(), 0.0f);
273 
274  return calc_width(_text_block[r]._string[c]);
275 }
276 
277 /**
278  * Returns the y position of the origin of all of the characters or graphic
279  * objects in the indicated row.
280  *
281  * It is legal for r to exceed the index number of the last row by 1. The
282  * value of c is presently ignored.
283  */
284 INLINE PN_stdfloat TextAssembler::
285 get_ypos(int r, int) const {
286  nassertr(r >= 0 && r <= (int)_text_block.size(), 0.0f);
287  if (r == (int)_text_block.size()) {
288  return _next_row_ypos;
289  } else {
290  return _text_block[r]._ypos;
291  }
292 }
293 
294 /**
295  * Returns the width of a single character, according to its associated font.
296  */
297 INLINE PN_stdfloat TextAssembler::
298 calc_width(const TextCharacter &tch) {
299  if (tch._graphic != nullptr) {
300  return calc_width(tch._graphic, tch._cprops->_properties);
301  } else {
302  return calc_width(tch._character, tch._cprops->_properties);
303  }
304 }
305 
306 /**
307  *
308  */
309 INLINE TextAssembler::TextCharacter::
310 TextCharacter(wchar_t character,
311  TextAssembler::ComputedProperties *cprops) :
312  _character(character),
313  _graphic(nullptr),
314  _cprops(cprops)
315 {
316 }
317 
318 /**
319  *
320  */
321 INLINE TextAssembler::TextCharacter::
322 TextCharacter(const TextGraphic *graphic, const std::wstring &graphic_wname,
323  TextAssembler::ComputedProperties *cprops) :
324  _character(0),
325  _graphic(graphic),
326  _graphic_wname(graphic_wname),
327  _cprops(cprops)
328 {
329 }
330 
331 /**
332  *
333  */
334 INLINE TextAssembler::TextCharacter::
335 TextCharacter(const TextAssembler::TextCharacter &copy) :
336  _character(copy._character),
337  _graphic(copy._graphic),
338  _graphic_wname(copy._graphic_wname),
339  _cprops(copy._cprops)
340 {
341 }
342 
343 /**
344  *
345  */
346 INLINE void TextAssembler::TextCharacter::
347 operator = (const TextAssembler::TextCharacter &copy) {
348  _character = copy._character;
349  _graphic = copy._graphic;
350  _graphic_wname = copy._graphic_wname;
351  _cprops = copy._cprops;
352 }
353 
354 /**
355  *
356  */
357 INLINE TextAssembler::TextRow::
358 TextRow(int row_start) :
359  _row_start(row_start),
360  _got_soft_hyphens(false),
361  _xpos(0.0f),
362  _ypos(0.0f)
363 {
364 }
365 
366 /**
367  *
368  */
369 INLINE TextAssembler::TextRow::
370 TextRow(const TextAssembler::TextRow &copy) :
371  _string(copy._string),
372  _row_start(copy._row_start),
373  _got_soft_hyphens(copy._got_soft_hyphens),
374  _xpos(copy._xpos),
375  _ypos(copy._ypos),
376  _eol_cprops(copy._eol_cprops)
377 {
378 }
379 
380 /**
381  *
382  */
383 INLINE void TextAssembler::TextRow::
384 operator = (const TextAssembler::TextRow &copy) {
385  _string = copy._string;
386  _row_start = copy._row_start;
387  _got_soft_hyphens = copy._got_soft_hyphens;
388  _xpos = copy._xpos;
389  _ypos = copy._ypos;
390  _eol_cprops = copy._eol_cprops;
391 }
392 
393 /**
394  *
395  */
396 INLINE TextAssembler::ComputedProperties::
397 ComputedProperties(const TextProperties &orig_properties) :
398  _based_on(nullptr),
399  _depth(0),
400  _properties(orig_properties)
401 {
402 }
403 
404 /**
405  *
406  */
407 INLINE TextAssembler::ComputedProperties::
408 ComputedProperties(ComputedProperties *based_on, const std::wstring &wname,
409  TextEncoder *encoder) :
410  _based_on(based_on),
411  _depth(_based_on->_depth + 1),
412  _wname(wname),
413  _properties(based_on->_properties)
414 {
415  TextPropertiesManager *manager =
417 
418  // Now we have to encode the wstring into a string, for lookup in the
419  // TextPropertiesManager.
420  std::string name = encoder->encode_wtext(wname);
421 
422  const TextProperties *named_props = manager->get_properties_ptr(name);
423  if (named_props != nullptr) {
424  _properties.add_properties(*named_props);
425  } else {
426  text_cat.warning()
427  << "Unknown TextProperties: " << name << "\n";
428  }
429 }
430 
431 /**
432  *
433  */
434 INLINE TextAssembler::GeomCollectorKey::
435 GeomCollectorKey(const RenderState *state, const GeomVertexFormat *format) :
436  _state(state),
437  _format(format)
438 {
439 }
440 
441 /**
442  *
443  */
444 INLINE bool TextAssembler::GeomCollectorKey::
445 operator < (const TextAssembler::GeomCollectorKey &other) const {
446  if (_state != other._state) {
447  return _state < other._state;
448  }
449  return _format < other._format;
450 }
451 
452 /**
453  * If the indicated Geom is a GeomTextGlyph, increments its reference count
454  * and adds it into this geom. This is necessary to keep references to
455  * outstanding glyphs, so we know when it's safe to recycle no-longer-used
456  * glyphs.
457  *
458  * If the indicated Geom is an ordinary Geom, does nothing.
459  */
460 INLINE void TextAssembler::GeomCollector::
461 count_geom(const Geom *geom) {
462 #ifdef HAVE_FREETYPE
463  _geom->count_geom(geom);
464 #endif
465 }
set_max_rows
If max_rows is greater than zero, no more than max_rows will be accepted.
PN_stdfloat get_width(int n) const
Returns the width of the character or object at the indicated position in the pre-wordwrapped string.
int get_num_cols(int r) const
Returns the number of characters and/or graphic objects in the nth row.
const TextGraphic * get_graphic(int n) const
Returns the graphic object at the indicated position in the pre-wordwrapped string.
set_usage_hint
Specifies the UsageHint that will be applied to generated geometry.
int get_num_characters() const
Returns the number of characters of text, before wordwrapping.
set_multiline_mode
Sets the multiline mode flag.
void add_properties(const TextProperties &other)
Sets any properties that are explicitly specified in other on this object.
This class can be used to convert text between multiple representations, e.g.
Definition: textEncoder.h:33
const LVector2 & get_ul() const
Returns the upper-left corner of the assembled text, in 2-d text coordinates.
wchar_t get_character(int n) const
Returns the character at the indicated position in the pre-wordwrapped string.
PN_stdfloat get_ypos(int r, int c) const
Returns the y position of the origin of all of the characters or graphic objects in the indicated row...
int get_num_rows() const
Returns the number of rows of text after it has all been wordwrapped and assembled.
static TextPropertiesManager * get_global_ptr()
Returns the pointer to the global TextPropertiesManager object.
set_dynamic_merge
Sets the dynamic_merge flag.
static PN_stdfloat calc_width(wchar_t character, const TextProperties &properties)
Returns the width of a single character, according to its associated font.
A container for geometry primitives.
Definition: geom.h:54
set_properties
Specifies the default TextProperties that are applied to the text in the absence of any nested proper...
const LVector2 & get_lr() const
Returns the lower-right corner of the assembled text, in 2-d text coordinates.
This represents a unique collection of RenderAttrib objects that correspond to a particular renderabl...
Definition: renderState.h:47
int calc_c(int n) const
Computes the column index of the nth character or graphic object in the text and returns it.
std::string encode_wtext(const std::wstring &wtext) const
Encodes a wide-text string into a single-char string, according to the current encoding.
Definition: textEncoder.I:481
This class defines the physical layout of the vertex data stored within a Geom.
bool calc_r_c(int &r, int &c, int n) const
Computes the row and column index of the nth character or graphic object in the text.
const TextProperties * get_properties_ptr(const std::string &name)
Returns a pointer to the TextProperties with the indicated name, or NULL if there is no properties wi...
This defines the set of visual properties that may be assigned to the individual characters of the te...
int calc_r(int n) const
Computes the row index of the nth character or graphic object in the text and returns it.
This defines all of the TextProperties structures that might be referenced by name from an embedded t...
This defines a special model that has been constructed for the purposes of embedding an arbitrary gra...
Definition: textGraphic.h:37