Panda3D
textProperties.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 textProperties.I
10  * @author drose
11  * @date 2004-04-06
12  */
13 
14 /**
15  *
16  */
17 INLINE bool TextProperties::
18 operator != (const TextProperties &other) const {
19  return !operator == (other);
20 }
21 
22 /**
23  * Returns true if any properties have been specified, false otherwise.
24  */
25 INLINE bool TextProperties::
27  return (_specified != 0);
28 }
29 
30 /**
31  * Specifies the default font to be used for any TextNode whose font is
32  * uninitialized or NULL. See set_font().
33  */
34 INLINE void TextProperties::
36  // If the user overrides the default, we don't need to try to load whatever
37  // it would have been.
38  _loaded_default_font = true;
39  _default_font = font;
40 }
41 
42 /**
43  * Specifies the default font to be used for any TextNode whose font is
44  * uninitialized or NULL. See set_font().
45  */
48  if (!_loaded_default_font) {
49  load_default_font();
50  }
51  return _default_font;
52 }
53 
54 /**
55  * Sets the font that will be used when making text. If this is set to NULL,
56  * the default font will be used, which can be set via set_default_font().
57  */
58 INLINE void TextProperties::
59 set_font(TextFont *font) {
60  _font = font;
61  _specified |= F_has_font;
62 }
63 
64 /**
65  * Restores the default font to the text.
66  */
67 INLINE void TextProperties::
68 clear_font() {
69  _font.clear();
70  _specified &= ~F_has_font;
71 }
72 
73 /**
74  *
75  */
76 INLINE bool TextProperties::
77 has_font() const {
78  return (_specified & F_has_font) != 0;
79 }
80 
81 /**
82  * Returns the font currently in use, if any. If no font is in use, this
83  * returns the default font.
84  */
85 INLINE TextFont *TextProperties::
86 get_font() const {
87  return has_font() ? _font.p() : get_default_font();
88 }
89 
90 /**
91  * Sets the small_caps flag. When this is set, lowercase letters are
92  * generated as scaled-down versions of their uppercase equivalents. This is
93  * particularly useful to set for fonts that do not have lowercase letters.
94  *
95  * It is also a good idea to set this for a (dynamic) font that has already
96  * implemented lowercase letters as scaled-down versions of their uppercase
97  * equivalents, since without this flag the texture memory may needlessly
98  * duplicate equivalent glyphs for upper and lowercase letters. Setting this
99  * flag causes the texture memory to share the mixed-case letters.
100  *
101  * The amount by which the lowercase letters are scaled is specified by
102  * set_small_caps_scale().
103  */
104 INLINE void TextProperties::
105 set_small_caps(bool small_caps) {
106  _small_caps = small_caps;
107  _specified |= F_has_small_caps;
108 }
109 
110 /**
111  *
112  */
113 INLINE void TextProperties::
114 clear_small_caps() {
115  _small_caps = false;
116  _specified &= ~F_has_small_caps;
117 }
118 
119 /**
120  *
121  */
122 INLINE bool TextProperties::
123 has_small_caps() const {
124  return (_specified & F_has_small_caps) != 0;
125 }
126 
127 /**
128  * Returns the small_caps flag. See set_small_caps().
129  */
130 INLINE bool TextProperties::
131 get_small_caps() const {
132  return _small_caps;
133 }
134 
135 /**
136  * Sets the scale factor applied to lowercase letters from their uppercase
137  * equivalents, when the small_caps flag is in effect. See set_small_caps().
138  * Normally, this will be a number less than one.
139  */
140 INLINE void TextProperties::
141 set_small_caps_scale(PN_stdfloat small_caps_scale) {
142  _small_caps_scale = small_caps_scale;
143 }
144 
145 /**
146  *
147  */
148 INLINE void TextProperties::
149 clear_small_caps_scale() {
150  _small_caps_scale = text_small_caps_scale;
151  _specified &= ~F_has_small_caps_scale;
152 }
153 
154 /**
155  *
156  */
157 INLINE bool TextProperties::
158 has_small_caps_scale() const {
159  return (_specified & F_has_small_caps_scale) != 0;
160 }
161 
162 /**
163  * Returns the scale factor applied to lowercase letters from their uppercase
164  * equivalents, when the small_caps flag is in effect. See set_small_caps()
165  * and set_small_caps_scale().
166  */
167 INLINE PN_stdfloat TextProperties::
168 get_small_caps_scale() const {
169  return _small_caps_scale;
170 }
171 
172 /**
173  * Specifies the factor by which the text slants to the right.
174  */
175 INLINE void TextProperties::
176 set_slant(PN_stdfloat slant) {
177  _slant = slant;
178  _specified |= F_has_slant;
179 }
180 
181 /**
182  *
183  */
184 INLINE void TextProperties::
185 clear_slant() {
186  _slant = 0.0f;
187  _specified &= ~F_has_slant;
188 }
189 
190 /**
191  *
192  */
193 INLINE bool TextProperties::
194 has_slant() const {
195  return (_specified & F_has_slant) != 0;
196 }
197 
198 /**
199  * Returns the factor by which the text is specified to slant to the right.
200  */
201 INLINE PN_stdfloat TextProperties::
202 get_slant() const {
203  return _slant;
204 }
205 
206 /**
207  * Sets the underscore flag. When this is set, the text is underscored with a
208  * one-pixel line the same color as the text foreground, drawn at the
209  * baseline.
210  */
211 INLINE void TextProperties::
212 set_underscore(bool underscore) {
213  _underscore = underscore;
214  _specified |= F_has_underscore;
215 }
216 
217 /**
218  *
219  */
220 INLINE void TextProperties::
221 clear_underscore() {
222  _underscore = false;
223  _specified &= ~F_has_underscore;
224 }
225 
226 /**
227  *
228  */
229 INLINE bool TextProperties::
230 has_underscore() const {
231  return (_specified & F_has_underscore) != 0;
232 }
233 
234 /**
235  * Returns the underscore flag. See set_underscore().
236  */
237 INLINE bool TextProperties::
238 get_underscore() const {
239  return _underscore;
240 }
241 
242 /**
243  * Specifies the vertical height of the underscore, relative to the text
244  * baseline. This only has meaning if the underscore mode is enabled with
245  * set_underscore().
246  */
247 INLINE void TextProperties::
248 set_underscore_height(PN_stdfloat underscore_height) {
249  _underscore_height = underscore_height;
250  _specified |= F_has_underscore_height;
251 }
252 
253 /**
254  *
255  */
256 INLINE void TextProperties::
257 clear_underscore_height() {
258  _underscore_height = 0.0f;
259  _specified &= ~F_has_underscore_height;
260 }
261 
262 /**
263  *
264  */
265 INLINE bool TextProperties::
266 has_underscore_height() const {
267  return (_specified & F_has_underscore_height) != 0;
268 }
269 
270 /**
271  * Returns the vertical height of the underscore; see set_underscore_height().
272  */
273 INLINE PN_stdfloat TextProperties::
274 get_underscore_height() const {
275  return has_underscore_height() ? _underscore_height : text_default_underscore_height;
276 }
277 
278 /**
279  * Specifies the alignment of the text within its margins.
280  */
281 INLINE void TextProperties::
282 set_align(TextProperties::Alignment align_type) {
283  _align = align_type;
284  _specified |= F_has_align;
285 }
286 
287 /**
288  * Restores the default alignment of the text.
289  */
290 INLINE void TextProperties::
291 clear_align() {
292  _align = A_left;
293  _specified &= ~F_has_align;
294 }
295 
296 /**
297  *
298  */
299 INLINE bool TextProperties::
300 has_align() const {
301  return (_specified & F_has_align) != 0;
302 }
303 
304 /**
305  *
306  */
307 INLINE TextProperties::Alignment TextProperties::
308 get_align() const {
309  return _align;
310 }
311 
312 /**
313  * Specifies the amount of extra space that is inserted before the first
314  * character of each line. This can be thought of as a left margin.
315  */
316 INLINE void TextProperties::
317 set_indent(PN_stdfloat indent) {
318  _indent_width = indent;
319  _specified |= F_has_indent;
320 }
321 
322 /**
323  * Removes the indent setting from the text. Text will be as wide as it is.
324  */
325 INLINE void TextProperties::
326 clear_indent() {
327  _indent_width = 0.0f;
328  _specified &= ~F_has_indent;
329 }
330 
331 /**
332  *
333  */
334 INLINE bool TextProperties::
335 has_indent() const {
336  return (_specified & F_has_indent) != 0;
337 }
338 
339 /**
340  *
341  */
342 INLINE PN_stdfloat TextProperties::
343 get_indent() const {
344  return _indent_width;
345 }
346 
347 /**
348  * Sets the text up to automatically wordwrap when it exceeds the indicated
349  * width. This can be thought of as a right margin or margin width.
350  */
351 INLINE void TextProperties::
352 set_wordwrap(PN_stdfloat wordwrap) {
353  _wordwrap_width = wordwrap;
354  _specified |= F_has_wordwrap;
355 }
356 
357 /**
358  * Removes the wordwrap setting from the text. Text will be as wide as it is.
359  */
360 INLINE void TextProperties::
361 clear_wordwrap() {
362  _wordwrap_width = 0.0f;
363  _specified &= ~F_has_wordwrap;
364 }
365 
366 /**
367  *
368  */
369 INLINE bool TextProperties::
370 has_wordwrap() const {
371  return (_specified & F_has_wordwrap) != 0;
372 }
373 
374 /**
375  *
376  */
377 INLINE PN_stdfloat TextProperties::
378 get_wordwrap() const {
379  return _wordwrap_width;
380 }
381 
382 /**
383  * Sets the preserve_trailing_whitespace flag. When this is set, trailing
384  * whitespace at the end of the line is not stripped when the text is
385  * wordwrapped (it is stripped by default). Since the trailing whitespace is
386  * invisible, this is important primarily for determining the proper width of
387  * a frame or card behind the text.
388  */
389 INLINE void TextProperties::
390 set_preserve_trailing_whitespace(bool preserve_trailing_whitespace) {
391  _preserve_trailing_whitespace = preserve_trailing_whitespace;
392  _specified |= F_has_preserve_trailing_whitespace;
393 }
394 
395 /**
396  *
397  */
398 INLINE void TextProperties::
399 clear_preserve_trailing_whitespace() {
400  _preserve_trailing_whitespace = false;
401  _specified &= ~F_has_preserve_trailing_whitespace;
402 }
403 
404 /**
405  *
406  */
407 INLINE bool TextProperties::
408 has_preserve_trailing_whitespace() const {
409  return (_specified & F_has_preserve_trailing_whitespace) != 0;
410 }
411 
412 /**
413  * Returns the preserve_trailing_whitespace flag. See
414  * set_preserve_trailing_whitespace().
415  */
416 INLINE bool TextProperties::
417 get_preserve_trailing_whitespace() const {
418  return _preserve_trailing_whitespace;
419 }
420 
421 /**
422  *
423  */
424 INLINE void TextProperties::
425 set_text_color(PN_stdfloat r, PN_stdfloat g, PN_stdfloat b, PN_stdfloat a) {
426  set_text_color(LColor(r, g, b, a));
427 }
428 
429 /**
430  *
431  */
432 INLINE void TextProperties::
433 set_text_color(const LColor &text_color) {
434  _text_color = text_color;
435  _specified |= F_has_text_color;
436  _text_state.clear();
437 }
438 
439 /**
440  * Removes the text color specification; the text will be colored whatever it
441  * was in the source font file.
442  */
443 INLINE void TextProperties::
444 clear_text_color() {
445  _text_color.set(1.0f, 1.0f, 1.0f, 1.0f);
446  _specified &= ~F_has_text_color;
447  _text_state.clear();
448 }
449 
450 /**
451  *
452  */
453 INLINE bool TextProperties::
454 has_text_color() const {
455  return (_specified & F_has_text_color) != 0;
456 }
457 
458 /**
459  *
460  */
461 INLINE LColor TextProperties::
462 get_text_color() const {
463  return _text_color;
464 }
465 
466 /**
467  *
468  */
469 INLINE void TextProperties::
470 set_shadow_color(PN_stdfloat r, PN_stdfloat g, PN_stdfloat b, PN_stdfloat a) {
471  set_shadow_color(LColor(r, g, b, a));
472 }
473 
474 /**
475  *
476  */
477 INLINE void TextProperties::
478 set_shadow_color(const LColor &shadow_color) {
479  _shadow_color = shadow_color;
480  _specified |= F_has_shadow_color;
481  _shadow_state.clear();
482 }
483 
484 /**
485  * Removes the shadow color specification.
486  */
487 INLINE void TextProperties::
488 clear_shadow_color() {
489  _shadow_color.set(0.0f, 0.0f, 0.0f, 1.0f);
490  _specified &= ~F_has_shadow_color;
491  _shadow_state.clear();
492 }
493 
494 /**
495  *
496  */
497 INLINE bool TextProperties::
498 has_shadow_color() const {
499  return (_specified & F_has_shadow_color) != 0;
500 }
501 
502 /**
503  *
504  */
505 INLINE LColor TextProperties::
506 get_shadow_color() const {
507  return _shadow_color;
508 }
509 
510 /**
511  * Specifies that the text should be drawn with a shadow, by creating a second
512  * copy of the text and offsetting it slightly behind the first.
513  */
514 INLINE void TextProperties::
515 set_shadow(PN_stdfloat xoffset, PN_stdfloat yoffset) {
516  set_shadow(LVecBase2(xoffset, yoffset));
517 }
518 
519 /**
520  * Specifies that the text should be drawn with a shadow, by creating a second
521  * copy of the text and offsetting it slightly behind the first.
522  */
523 INLINE void TextProperties::
524 set_shadow(const LVecBase2 &shadow_offset) {
525  _shadow_offset = shadow_offset;
526  _specified |= F_has_shadow;
527 }
528 
529 /**
530  * Specifies that a shadow will not be drawn behind the text.
531  */
532 INLINE void TextProperties::
533 clear_shadow() {
534  _specified &= ~F_has_shadow;
535  _shadow_offset.set(0.0f, 0.0f);
536 }
537 
538 /**
539  *
540  */
541 INLINE bool TextProperties::
542 has_shadow() const {
543  return (_specified & F_has_shadow) != 0;
544 }
545 
546 /**
547  * Returns the offset of the shadow as set by set_shadow(). It is an error to
548  * call this if has_shadow() is false.
549  */
550 INLINE LVector2 TextProperties::
551 get_shadow() const {
552  return _shadow_offset;
553 }
554 
555 /**
556  * Names the CullBin that the text geometry should be assigned to. If this is
557  * set, then a CullBinAttrib will be created to explicitly place each
558  * component in the named bin.
559  *
560  * The draw_order value will also be passed to each CullBinAttrib as
561  * appropriate; this is particularly useful if this names a CullBinFixed, e.g.
562  * "fixed".
563  */
564 INLINE void TextProperties::
565 set_bin(const std::string &bin) {
566  _bin = bin;
567  _specified |= F_has_bin;
568  _text_state.clear();
569  _shadow_state.clear();
570 }
571 
572 /**
573  * Removes the effect of a previous call to set_bin(). Text will be drawn in
574  * whatever bin it would like to be drawn in, with no explicit ordering.
575  */
576 INLINE void TextProperties::
577 clear_bin() {
578  _bin = std::string();
579  _specified &= ~F_has_bin;
580  _text_state.clear();
581  _shadow_state.clear();
582 }
583 
584 /**
585  * Returns true if an explicit drawing bin has been set via set_bin(), false
586  * otherwise.
587  */
588 INLINE bool TextProperties::
589 has_bin() const {
590  return (_specified & F_has_bin) != 0;
591 }
592 
593 /**
594  * Returns the drawing bin set with set_bin(), or empty string if no bin has
595  * been set.
596  */
597 INLINE const std::string &TextProperties::
598 get_bin() const {
599  return _bin;
600 }
601 
602 /**
603  * Sets the drawing order of text created by the TextNode. This is actually
604  * the draw order of the card and frame. The shadow is drawn at
605  * _draw_order+1, and the text at _draw_order+2.
606  *
607  * This affects the sorting order assigned to the nodes as they are created,
608  * and also is passed to whatever bin may be assigned via set_bin().
609  *
610  * The return value is the first unused draw_order number, e.g. _draw_order +
611  * 3.
612  */
613 INLINE int TextProperties::
614 set_draw_order(int draw_order) {
615  _draw_order = draw_order;
616  _specified |= F_has_draw_order;
617  _text_state.clear();
618  _shadow_state.clear();
619  return _draw_order + 3;
620 }
621 
622 /**
623  *
624  */
625 INLINE void TextProperties::
626 clear_draw_order() {
627  _draw_order = 1;
628  _specified &= ~F_has_draw_order;
629  _text_state.clear();
630  _shadow_state.clear();
631 }
632 
633 /**
634  *
635  */
636 INLINE bool TextProperties::
637 has_draw_order() const {
638  return (_specified & F_has_draw_order) != 0;
639 }
640 
641 /**
642  * Returns the drawing order set with set_draw_order().
643  */
644 INLINE int TextProperties::
645 get_draw_order() const {
646  return _draw_order;
647 }
648 
649 /**
650  * Sets the width of each tab stop, in screen units. A tab character embedded
651  * in the text will advance the horizontal position to the next tab stop.
652  */
653 INLINE void TextProperties::
654 set_tab_width(PN_stdfloat tab_width) {
655  _tab_width = tab_width;
656  _specified |= F_has_tab_width;
657 }
658 
659 /**
660  *
661  */
662 INLINE void TextProperties::
663 clear_tab_width() {
664  _tab_width = text_tab_width;
665  _specified &= ~F_has_tab_width;
666 }
667 
668 /**
669  *
670  */
671 INLINE bool TextProperties::
672 has_tab_width() const {
673  return (_specified & F_has_tab_width) != 0;
674 }
675 
676 /**
677  * Returns the width set via set_tab_width().
678  */
679 INLINE PN_stdfloat TextProperties::
680 get_tab_width() const {
681  return _tab_width;
682 }
683 
684 /**
685  * Specifies the factor by which to scale each letter of the text as it is
686  * placed, in addition to any scales inherited from the node or from
687  * set_text_scale(). This can be used (possibly in conjunction with
688  * set_glyph_shift()) to implement superscripting or subscripting.
689  *
690  * The glyph scale is cumulative when applied to nested TextProperties. It is
691  * intended primarily for implementing superscripts, not for scaling the text
692  * in general. See also set_text_scale(), which is intended primarily for
693  * scaling the text in general, and is not cumulative.
694  */
695 INLINE void TextProperties::
696 set_glyph_scale(PN_stdfloat glyph_scale) {
697  _glyph_scale = glyph_scale;
698  _specified |= F_has_glyph_scale;
699 }
700 
701 /**
702  *
703  */
704 INLINE void TextProperties::
705 clear_glyph_scale() {
706  _specified &= ~F_has_glyph_scale;
707  _glyph_scale = 0.0f;
708 }
709 
710 /**
711  *
712  */
713 INLINE bool TextProperties::
714 has_glyph_scale() const {
715  return (_specified & F_has_glyph_scale) != 0;
716 }
717 
718 /**
719  * Returns the scale factor of each letter as specified by set_glyph_scale().
720  */
721 INLINE PN_stdfloat TextProperties::
722 get_glyph_scale() const {
723  return _glyph_scale;
724 }
725 
726 /**
727  * Specifies a vertical amount to shift each letter of the text as it is
728  * placed. This can be used (possibly in conjunction with set_glyph_scale())
729  * to implement superscripting or subscripting.
730  */
731 INLINE void TextProperties::
732 set_glyph_shift(PN_stdfloat glyph_shift) {
733  _glyph_shift = glyph_shift;
734  _specified |= F_has_glyph_shift;
735 }
736 
737 /**
738  *
739  */
740 INLINE void TextProperties::
741 clear_glyph_shift() {
742  _specified &= ~F_has_glyph_shift;
743  _glyph_shift = 0.0f;
744 }
745 
746 /**
747  *
748  */
749 INLINE bool TextProperties::
750 has_glyph_shift() const {
751  return (_specified & F_has_glyph_shift) != 0;
752 }
753 
754 /**
755  * Returns the vertical shift of each letter as specified by
756  * set_glyph_shift().
757  */
758 INLINE PN_stdfloat TextProperties::
759 get_glyph_shift() const {
760  return _glyph_shift;
761 }
762 
763 /**
764  * Specifies the factor by which to scale the text, in addition to any
765  * scalings imposed by the node, as well as in addition to the glyph scale.
766  *
767  * The text scale is not cumulative when applied to nested TextProperties.
768  * See also set_glyph_scale(), which is cumulative.
769  */
770 INLINE void TextProperties::
771 set_text_scale(PN_stdfloat text_scale) {
772  _text_scale = text_scale;
773  _specified |= F_has_text_scale;
774 }
775 
776 /**
777  *
778  */
779 INLINE void TextProperties::
780 clear_text_scale() {
781  _specified &= ~F_has_text_scale;
782  _text_scale = 0.0f;
783 }
784 
785 /**
786  *
787  */
788 INLINE bool TextProperties::
789 has_text_scale() const {
790  return (_specified & F_has_text_scale) != 0;
791 }
792 
793 /**
794  * Returns the scale factor of the text as specified by set_text_scale().
795  */
796 INLINE PN_stdfloat TextProperties::
797 get_text_scale() const {
798  return _text_scale;
799 }
800 
801 /**
802  * Specifies the text direction. If none is specified, it will be guessed
803  * based on the contents of the string.
804  *
805  * @since 1.10.0
806  */
807 INLINE void TextProperties::
808 set_direction(Direction direction) {
809  _direction = direction;
810  _specified |= F_has_direction;
811 }
812 
813 /**
814  * Clears the text direction setting. If no text direction is specified, it
815  * will be guessed based on the contents of the string.
816  *
817  * @since 1.10.0
818  */
819 INLINE void TextProperties::
820 clear_direction() {
821  _specified &= ~F_has_direction;
822  _direction = D_ltr;
823 }
824 
825 /**
826  * @since 1.10.0
827  */
828 INLINE bool TextProperties::
829 has_direction() const {
830  return (_specified & F_has_direction) != 0;
831 }
832 
833 /**
834  * Returns the direction of the text as specified by set_direction().
835  *
836  * @since 1.10.0
837  */
838 INLINE TextProperties::Direction TextProperties::
839 get_direction() const {
840  return _direction;
841 }
bool is_any_specified() const
Returns true if any properties have been specified, false otherwise.
set_align
Specifies the alignment of the text within its margins.
set_direction
Specifies the text direction.
set_text_scale
Specifies the factor by which to scale the text, in addition to any scalings imposed by the node,...
set_glyph_scale
Specifies the factor by which to scale each letter of the text as it is placed, in addition to any sc...
static TextFont * get_default_font()
Specifies the default font to be used for any TextNode whose font is uninitialized or NULL.
set_preserve_trailing_whitespace
Sets the preserve_trailing_whitespace flag.
set_underscore_height
Specifies the vertical height of the underscore, relative to the text baseline.
An encapsulation of a font; i.e.
Definition: textFont.h:32
set_tab_width
Sets the width of each tab stop, in screen units.
set_shadow
Specifies that the text should be drawn with a shadow, by creating a second copy of the text and offs...
set_draw_order
Sets the drawing order of text created by the TextNode.
static void set_default_font(TextFont *)
Specifies the default font to be used for any TextNode whose font is uninitialized or NULL.
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.
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.
set_indent
Specifies the amount of extra space that is inserted before the first character of each line.
set_bin
Names the CullBin that the text geometry should be assigned to.
This defines the set of visual properties that may be assigned to the individual characters of the te...
set_underscore
Sets the underscore flag.
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.