Panda3D
Loading...
Searching...
No Matches
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 */
20INLINE void TextAssembler::
21set_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 */
29INLINE Geom::UsageHint TextAssembler::
30get_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 */
41INLINE void TextAssembler::
42set_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 */
50INLINE int TextAssembler::
51get_max_rows() const {
52 return _max_rows;
53}
54
55/**
56 * Sets the dynamic_merge flag. See TextNode::set_flatten_flags().
57 */
58INLINE void TextAssembler::
59set_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 */
66INLINE bool TextAssembler::
67get_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 */
75INLINE void TextAssembler::
76set_multiline_mode(bool flag) {
77 _multiline_mode = flag;
78}
79
80/**
81 * Returns the multline_mode flag. See TextNode::set_multiline_mode().
82 */
83INLINE bool TextAssembler::
84get_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 */
92INLINE void TextAssembler::
93set_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 */
102get_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 */
110INLINE const LVector2 &TextAssembler::
111get_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 */
119INLINE const LVector2 &TextAssembler::
120get_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 */
133calc_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 */
150calc_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 */
162get_num_characters() const {
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 */
171INLINE char32_t TextAssembler::
172get_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 */
183get_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 */
193get_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 */
202INLINE PN_stdfloat TextAssembler::
203get_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 */
214get_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 */
222get_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 */
235INLINE char32_t TextAssembler::
236get_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 */
248get_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 */
259get_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 */
269INLINE PN_stdfloat TextAssembler::
270get_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 */
284INLINE PN_stdfloat TextAssembler::
285get_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 */
297INLINE PN_stdfloat TextAssembler::
298calc_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 */
309INLINE TextAssembler::TextCharacter::
310TextCharacter(wchar_t character,
311 TextAssembler::ComputedProperties *cprops) :
312 _character(character),
313 _graphic(nullptr),
314 _cprops(cprops)
315{
316}
317
318/**
319 *
320 */
321INLINE TextAssembler::TextCharacter::
322TextCharacter(char32_t character,
323 TextAssembler::ComputedProperties *cprops) :
324 _character(character),
325 _graphic(nullptr),
326 _cprops(cprops)
327{
328}
329
330/**
331 *
332 */
333INLINE TextAssembler::TextCharacter::
334TextCharacter(const TextGraphic *graphic, const std::wstring &graphic_wname,
335 TextAssembler::ComputedProperties *cprops) :
336 _character(0),
337 _graphic(graphic),
338 _graphic_wname(graphic_wname),
339 _cprops(cprops)
340{
341}
342
343/**
344 *
345 */
346INLINE TextAssembler::TextCharacter::
347TextCharacter(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 */
358INLINE void TextAssembler::TextCharacter::
359operator = (const TextAssembler::TextCharacter &copy) {
360 _character = copy._character;
361 _graphic = copy._graphic;
362 _graphic_wname = copy._graphic_wname;
363 _cprops = copy._cprops;
364}
365
366/**
367 *
368 */
369INLINE TextAssembler::TextRow::
370TextRow(int row_start) :
371 _row_start(row_start),
372 _got_soft_hyphens(false),
373 _xpos(0.0f),
374 _ypos(0.0f)
375{
376}
377
378/**
379 *
380 */
381INLINE TextAssembler::TextRow::
382TextRow(const TextAssembler::TextRow &copy) :
383 _string(copy._string),
384 _row_start(copy._row_start),
385 _got_soft_hyphens(copy._got_soft_hyphens),
386 _xpos(copy._xpos),
387 _ypos(copy._ypos),
388 _eol_cprops(copy._eol_cprops)
389{
390}
391
392/**
393 *
394 */
395INLINE void TextAssembler::TextRow::
396operator = (const TextAssembler::TextRow &copy) {
397 _string = copy._string;
398 _row_start = copy._row_start;
399 _got_soft_hyphens = copy._got_soft_hyphens;
400 _xpos = copy._xpos;
401 _ypos = copy._ypos;
402 _eol_cprops = copy._eol_cprops;
403}
404
405/**
406 *
407 */
408INLINE TextAssembler::ComputedProperties::
409ComputedProperties(const TextProperties &orig_properties) :
410 _based_on(nullptr),
411 _depth(0),
412 _properties(orig_properties)
413{
414}
415
416/**
417 *
418 */
419INLINE TextAssembler::ComputedProperties::
420ComputedProperties(ComputedProperties *based_on, const std::wstring &wname,
421 TextEncoder *encoder) :
422 _based_on(based_on),
423 _depth(_based_on->_depth + 1),
424 _wname(wname),
425 _properties(based_on->_properties)
426{
427 TextPropertiesManager *manager =
429
430 // Now we have to encode the wstring into a string, for lookup in the
431 // TextPropertiesManager.
432 std::string name = encoder->encode_wtext(wname);
433
434 const TextProperties *named_props = manager->get_properties_ptr(name);
435 if (named_props != nullptr) {
436 _properties.add_properties(*named_props);
437 } else {
438 text_cat.warning()
439 << "Unknown TextProperties: " << name << "\n";
440 }
441}
442
443/**
444 *
445 */
446INLINE TextAssembler::GeomCollectorKey::
447GeomCollectorKey(const RenderState *state, const GeomVertexFormat *format) :
448 _state(state),
449 _format(format)
450{
451}
452
453/**
454 *
455 */
456INLINE bool TextAssembler::GeomCollectorKey::
457operator < (const TextAssembler::GeomCollectorKey &other) const {
458 if (_state != other._state) {
459 return _state < other._state;
460 }
461 return _format < other._format;
462}
463
464/**
465 * If the indicated Geom is a GeomTextGlyph, increments its reference count
466 * and adds it into this geom. This is necessary to keep references to
467 * outstanding glyphs, so we know when it's safe to recycle no-longer-used
468 * glyphs.
469 *
470 * If the indicated Geom is an ordinary Geom, does nothing.
471 */
472INLINE void TextAssembler::GeomCollector::
473count_geom(const Geom *geom) {
474#ifdef HAVE_FREETYPE
475 _geom->count_geom(geom);
476#endif
477}
This class defines the physical layout of the vertex data stored within a Geom.
A container for geometry primitives.
Definition geom.h:54
This represents a unique collection of RenderAttrib objects that correspond to a particular renderabl...
Definition renderState.h:47
set_usage_hint
Specifies the UsageHint that will be applied to generated geometry.
set_multiline_mode
Sets the multiline mode flag.
const LVector2 & get_ul() const
Returns the upper-left corner of the assembled text, in 2-d text coordinates.
int get_num_rows() const
Returns the number of rows of text after it has all been wordwrapped and assembled.
get_multiline_mode
Returns the multline_mode flag.
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.
set_max_rows
If max_rows is greater than zero, no more than max_rows will be accepted.
int calc_c(int n) const
Computes the column index of the nth character or graphic object in the text and returns it.
int get_num_cols(int r) const
Returns the number of characters and/or graphic objects in the nth row.
get_properties
Returns the default TextProperties that are applied to the text in the absence of any nested property...
int get_num_characters() const
Returns the number of characters of text, before wordwrapping.
get_usage_hint
Returns the UsageHint that will be applied to generated geometry.
get_max_rows
If max_rows is greater than zero, no more than max_rows will be accepted.
get_dynamic_merge
Returns the dynamic_merge flag.
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...
static PN_stdfloat calc_width(wchar_t character, const TextProperties &properties)
Returns the width of a single character, according to its associated font.
const TextGraphic * get_graphic(int n) const
Returns the graphic object at the indicated position in the pre-wordwrapped string.
int calc_r(int n) const
Computes the row index of the nth character or graphic object in the text and returns it.
PN_stdfloat get_width(int n) const
Returns the width of the character or object at the indicated position in the pre-wordwrapped string.
const LVector2 & get_lr() const
Returns the lower-right corner of the assembled text, in 2-d text coordinates.
set_dynamic_merge
Sets the dynamic_merge flag.
char32_t get_character(int n) const
Returns the character at the indicated position in the pre-wordwrapped string.
set_properties
Specifies the default TextProperties that are applied to the text in the absence of any nested proper...
This class can be used to convert text between multiple representations, e.g.
Definition textEncoder.h:33
std::string encode_wtext(const std::wstring &wtext) const
Encodes a wide-text string into a single-char string, according to the current encoding.
This defines a special model that has been constructed for the purposes of embedding an arbitrary gra...
Definition textGraphic.h:37
This defines all of the TextProperties structures that might be referenced by name from an embedded t...
static TextPropertiesManager * get_global_ptr()
Returns the pointer to the global TextPropertiesManager object.
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...
void add_properties(const TextProperties &other)
Sets any properties that are explicitly specified in other on this object.