TextNode

class TextNode

Bases: PandaNode, TextEncoder, TextProperties

The primary interface to this module. This class does basic text assembly; given a string of text and a TextFont object, it creates a piece of geometry that may be placed in the 3-d or 2-d world to represent the indicated text.

The TextNode may be used in one of two ways. Naively, it may simply be parented directly into the scene graph and rendered as if it were a GeomNode; in this mode, the actual polygon geometry that renders the text is not directly visible or accessible, but remains hidden within the TextNode.

The second way TextNode may be used is as a text generator. To use it in this way, do not parent the TextNode to the scene graph; instead, set the properties of the text and call generate() to return an ordinary node, containing ordinary geometry, which you may use however you like. Each time you call generate() a new node is returned.

Inheritance diagram

Inheritance diagram of TextNode

enum FlattenFlags
enumerator FF_none = 0
enumerator FF_light = 1
enumerator FF_medium = 2
enumerator FF_strong = 4
enumerator FF_dynamic_merge = 8
explicit TextNode(std::string const &name)
explicit TextNode(std::string const &name, TextProperties const &copy)

It’s sort of a copy constructor: it copies the indicated TextProperties, without copying a complete TextNode.

OK, this is a true copy constructor.

PN_stdfloat calc_width(wchar_t character) const
PN_stdfloat calc_width(std::string const &line) const
PN_stdfloat calc_width(std::wstring const &line) const

These methods calculate the width of a single character or a line of text in the current font.

Returns the width of a line of text of arbitrary characters. The line should not include the newline character.

Returns the width of a single character of the font, or 0.0 if the character is not known. This may be a wide character (greater than 255).

Returns the width of a line of text of arbitrary characters. The line should not include the newline character or any embedded control characters like 1 or 3.

void clear_align(void)
void clear_bin(void)

Removes the effect of a previous call to set_bin(). Text will be drawn in whatever bin it would like to be drawn in, with no explicit ordering.

void clear_card(void)

Specifies that a card will not be drawn behind the text.

void clear_card_border(void)
void clear_card_texture(void)
void clear_draw_order(void)
void clear_font(void)

Resets the font to the default font.

void clear_frame(void)

Specifies that a border will not be drawn around the text.

void clear_glyph_scale(void)
void clear_glyph_shift(void)
void clear_indent(void)
void clear_max_rows(void)

Resets the TextNode’s default behavior of not limiting the number of rows of text.

void clear_shadow(void)

Specifies that a shadow will not be drawn behind the text.

void clear_shadow_color(void)
void clear_slant(void)
void clear_small_caps(void)
void clear_small_caps_scale(void)
void clear_tab_width(void)
void clear_text_color(void)

Removes the text color specification; the text will be colored whatever it was in the source font file.

void clear_wordwrap(void)

Removes the wordwrap setting from the TextNode. Text will be as wide as it is.

void force_update(void)

Forces the TextNode to recompute itself now, even if it believes nothing has changed. Normally, this should not need to be called, but it may be useful if some properties change outside of the TextNode’s knowledge (for instance, within the font).

PointerTo<PandaNode> generate(void)

Generates the text, according to the parameters indicated within the TextNode, and returns a Node that may be parented within the tree to represent it.

PN_stdfloat get_bottom(void) const

Returns the bottommost extent of the text in local 2-d coordinates, unmodified by the set_transform() matrix.

LVecBase4 get_card_actual(void) const

Returns the actual dimensions of the card around the text. If the card was set via set_card_as_margin(), the result returned by this function reflects the size of the current text; if the card was set via set_card_actual(), this returns the values actually set.

If the text has no card at all, this returns the dimensions of the text itself, as if the card were set with a margin of 0, 0, 0, 0.

LVecBase4 get_card_as_set(void) const

Returns the dimensions of the card as set by set_card_as_margin() or set_card_actual(). Use is_card_actual() to determine how to interpret the values returned by this function. It is an error to call this if has_card() is false.

PN_stdfloat get_card_border_size(void) const
PN_stdfloat get_card_border_uv_portion(void) const
LColor get_card_color(void) const
bool get_card_decal(void) const

Returns the card_decal flag. See set_card_decal().

Texture *get_card_texture(void) const
LVecBase4 get_card_transformed(void) const

Returns the actual card dimensions, transformed by the matrix set by set_transform(). This returns the card dimensions in actual coordinates as seen by the rest of the world. Also see get_upper_left_3d() and get_lower_right_3d().

static TypeHandle get_class_type(void)
CoordinateSystem get_coordinate_system(void) const
int get_flatten_flags(void) const

Returns the flatten flags. See set_flatten_flags().

LVecBase4 get_frame_actual(void) const

Returns the actual dimensions of the frame around the text. If the frame was set via set_frame_as_margin(), the result returned by this function reflects the size of the current text; if the frame was set via set_frame_actual(), this returns the values actually set.

If the text has no frame at all, this returns the dimensions of the text itself, as if the frame were set with a margin of 0, 0, 0, 0.

LVecBase4 get_frame_as_set(void) const

Returns the dimensions of the frame as set by set_frame_as_margin() or set_frame_actual(). Use is_frame_actual() to determine how to interpret the values returned by this function. It is an error to call this if has_frame() is false.

LColor get_frame_color(void) const
bool get_frame_corners(void) const
PN_stdfloat get_frame_line_width(void) const

Returns the thickness of the lines that will be used to draw the frame.

PN_stdfloat get_height(void) const

Returns the net height of the text in local 2-d coordinates.

PointerTo<PandaNode> get_internal_geom(void) const

Returns the actual node that is used internally to render the text, if the TextNode is parented within the scene graph.

In general, you should not call this method. Call generate() instead if you want to get a handle to geometry that represents the text. This method is provided as a debugging aid only.

PN_stdfloat get_left(void) const

The following functions return information about the text that was last built (and is currently visible).

Returns the leftmost extent of the text in local 2-d coordinates, unmodified by the set_transform() matrix.

PN_stdfloat get_line_height(void) const

Returns the number of units high each line of text is. This is based on the font. Note that it is possible for the text to include nested font change commands, in which case the value of this method is questionable.

LPoint3 get_lower_right_3d(void) const

Returns the lower-right extent of the text object, after it has been transformed into 3-d space by applying the set_transform() matrix.

int get_max_rows(void) const

Returns the limit on the height of the TextNode specified by set_max_rows().

int get_num_rows(void) const

Returns the number of rows of text that were generated. This counts word- wrapped rows as well as rows generated due to embedded newlines.

PN_stdfloat get_right(void) const

Returns the rightmost extent of the text in local 2-d coordinates, unmodified by the set_transform() matrix.

PN_stdfloat get_top(void) const

Returns the topmost extent of the text in local 2-d coordinates, unmodified by the set_transform() matrix.

LMatrix4 get_transform(void) const
LPoint3 get_upper_left_3d(void) const

Returns the upper-left extent of the text object, after it has been transformed into 3-d space by applying the set_transform() matrix.

GeomEnums::UsageHint get_usage_hint(void) const

Returns the UsageHint that will be applied to generated geometry. See set_usage_hint().

PN_stdfloat get_width(void) const

Returns the net width of the text in local 2-d coordinates.

std::string get_wordwrapped_text(void) const

After the text has been set, you can query this to determine how it will be wordwrapped.

Returns a string that represents the contents of the text, as it has been formatted by wordwrap rules.

In earlier versions, this did not contain any embedded special characters like 1 or 3; now it does.

std::wstring get_wordwrapped_wtext(void) const

Returns a wstring that represents the contents of the text, as it has been formatted by wordwrap rules.

In earlier versions, this did not contain any embedded special characters like 1 or 3; now it does.

bool has_card(void) const
bool has_card_border(void) const
bool has_card_texture(void) const
bool has_character(wchar_t character) const

Returns true if the named character exists in the font or can be synthesized by Panda, false otherwise. (Panda can synthesize some accented characters by combining similar-looking glyphs from the font.)

This returns true for whitespace and Unicode whitespace characters (if they exist in the font), but returns false for characters that would render with the “invalid glyph”.

bool has_exact_character(wchar_t character) const

Returns true if the named character exists in the font exactly as named, false otherwise. Note that because Panda can assemble glyphs together automatically using cheesy accent marks, this is not a reliable indicator of whether a suitable glyph can be rendered for the character. For that, use has_character() instead.

This returns true for whitespace and Unicode whitespace characters (if they exist in the font), but returns false for characters that would render with the “invalid glyph”. It also returns false for characters that would be synthesized within Panda, but see has_character().

bool has_frame(void) const
bool has_max_rows(void) const

Returns true if a limit on the height of the TextNode has been set via set_max_rows(), false otherwise.

bool has_overflow(void) const

Returns true if the last text set on the text node exceeded the max_rows constraint, or false if it all fit.

bool is_card_as_margin(void) const

If this is true, the card was set via a call to set_card_as_margin(), and the dimension of the card as returned by get_card_as_set() represent a margin all around the text. If false, then the card was set via a call to set_card_actual(), and the dimensions of the card as returned by get_card_as_set() are relative to the text’s origin.

bool is_frame_as_margin(void) const

If this is true, the frame was set via a call to set_frame_as_margin(), and the dimension of the frame as returned by get_frame_as_set() represent a margin all around the text. If false, then the frame was set via a call to set_frame_actual(), and the dimensions of the frame as returned by get_frame_as_set() are relative to the text’s origin.

bool is_whitespace(wchar_t character) const

Returns true if the indicated character represents whitespace in the font, or false if anything visible will be rendered for it.

This returns true for whitespace and Unicode whitespace characters (if they exist in the font), and returns false for any other characters, including characters that do not exist in the font (these would be rendered with the “invalid glyph”, which is visible).

Note that this function can be reliably used to identify Unicode whitespace characters only if the font has all of the whitespace characters defined. It will return false for any character not in the font, even if it is an official Unicode whitespace character.

virtual void output(std::ostream &out) const
void set_align(TextProperties::Alignment align_type)
void set_bin(std::string const &bin)

Names the GeomBin that the TextNode geometry should be assigned to. If this is set, then a GeomBinTransition will be created to explicitly place each component in the named bin.

The draw_order value will also be passed to each GeomBinTransition as appropriate; this is particularly useful if this names a GeomBinFixed, e.g. “fixed”.

void set_card_actual(PN_stdfloat left, PN_stdfloat right, PN_stdfloat bottom, PN_stdfloat top)

Similar to set_card_as_margin, except the card is specified in actual coordinate units (relative to the text’s origin), irrespective of the size of the text. The left and bottom coordinates should generally be negative, while the right and top coordinates should generally be positive.

void set_card_as_margin(PN_stdfloat left, PN_stdfloat right, PN_stdfloat bottom, PN_stdfloat top)

Specifies that a (possibly opaque or semitransparent) card will be held behind the text when it is next created. Like set_frame_as_margin, the parameters are the amount of additional padding to insert around the text in each dimension, and all should generally be positive.

void set_card_border(PN_stdfloat size, PN_stdfloat uv_portion)
void set_card_color(PN_stdfloat r, PN_stdfloat g, PN_stdfloat b, PN_stdfloat a)
void set_card_color(LColor const &card_color)
void set_card_decal(bool card_decal)

Sets the card_decal flag. When this is true, the text is decalled onto the card, which is necessary if the TextNode is to be rendered in the 3-d world without putting it in a bin.

void set_card_texture(Texture *card_texture)
void set_coordinate_system(CoordinateSystem cs)

Specifies the coordinate system in which the text will be generated.

int set_draw_order(int draw_order)

Sets the drawing order of text created by the TextMaker. This is actually the draw order of the card and frame. The shadow is drawn at _draw_order+1, and the text at _draw_order+2.

This affects the sorting order assigned to the arcs as they are created, and also is passed to whatever bin may be assigned via set_bin().

The return value is the first unused draw_order number, e.g. _draw_order + 3.

void set_flatten_flags(int flatten_flags)

Sets the flatten flags. This should be a union of the TextNode::FlattenFlags options. This controls the degree of flattening performed on the TextNode’s internal geometry (i.e. the scene graph returned by generate()) each time the text is changed. In general, more flattening means a more optimal result, but it will take more time to generate.

The choice may be any of these three:

FF_none - No flatten operation is called. The letters are left as independent Geoms.

FF_light - A flatten_light() operation is called. The attributes are applied to the vertices, but no nodes are removed.

FF_medium - A flatten_medium() operation is called. The attributes are applied to the vertices, and a few trivial nodes are removed.

FF_strong - A flatten_strong() operation is called. The attributes are applied to the vertices, and the resulting nodes are aggressively combined into as few nodes as possible.

In addition to the above choices, you may optionally include the following flag:

FF_dynamic_merge - Copy the geoms into a single GeomVertexData as we go, instead of relying on the flatten operation at the end. This pre-flattens the text considerably, and may obviate the need for flatten altogether; it also tends to improve performance considerably even if you do call flatten. However, it is not as fast as not calling flatten at all.

The default is taken from the text-flatten and text-dynamic-merge config variables.

void set_font(TextFont *font)

Sets the font that will be used when making text. If this is set to NULL, the default font will be used, which can be set via set_default_font().

void set_frame_actual(PN_stdfloat left, PN_stdfloat right, PN_stdfloat bottom, PN_stdfloat top)

Similar to set_frame_as_margin, except the frame is specified in actual coordinate units (relative to the text’s origin), irrespective of the size of the text. The left and bottom coordinates should generally be negative, while the right and top coordinates should generally be positive.

void set_frame_as_margin(PN_stdfloat left, PN_stdfloat right, PN_stdfloat bottom, PN_stdfloat top)

Specifies that a border will be drawn around the text when it is next created. The parameters are the amount of additional padding to insert between the frame and the text in each dimension, and all should generally be positive.

void set_frame_color(PN_stdfloat r, PN_stdfloat g, PN_stdfloat b, PN_stdfloat a)
void set_frame_color(LColor const &frame_color)
void set_frame_corners(bool corners)

Enables or disables the drawing of corners for the frame. These are extra points drawn at each of the four corners, to soften the ugly edges generated when the line width is greater than one.

void set_frame_line_width(PN_stdfloat line_width)

Specifies the thickness of the lines that will be used to draw the frame.

void set_glyph_scale(PN_stdfloat glyph_scale)

Specifies the factor by which to scale each letter of the text as it is placed. This can be used (possibly in conjunction with set_glyph_shift()) to implement superscripting or subscripting.

void set_glyph_shift(PN_stdfloat glyph_shift)

Specifies a vertical amount to shift each letter of the text as it is placed. This can be used (possibly in conjunction with set_glyph_scale()) to implement superscripting or subscripting.

void set_indent(PN_stdfloat indent)

Specifies the amount of extra space that is inserted before the first character of each line. This can be thought of as a left margin.

void set_max_rows(int max_rows)

Sets the maximum number of rows that may be formatted by the TextNode. If more text than this is attempted, it will be truncated and has_overflow() will return true.

void set_shadow(PN_stdfloat xoffset, PN_stdfloat yoffset)
void set_shadow(LVecBase2 const &shadow_offset)

Specifies that the text should be drawn with a shadow, by creating a second copy of the text and offsetting it slightly behind the first.

void set_shadow_color(PN_stdfloat r, PN_stdfloat g, PN_stdfloat b, PN_stdfloat a)
void set_shadow_color(LColor const &shadow_color)
void set_slant(PN_stdfloat slant)
void set_small_caps(bool small_caps)

Sets the small_caps flag. When this is set, lowercase letters are generated as scaled-down versions of their uppercase equivalents. This is particularly useful to set for fonts that do not have lowercase letters.

It is also a good idea to set this for a (dynamic) font that has already implemented lowercase letters as scaled-down versions of their uppercase equivalents, since without this flag the texture memory may needlessly duplicate equivalent glyphs for upper and lowercase letters. Setting this flag causes the texture memory to share the mixed-case letters.

The amount by which the lowercase letters are scaled is specified by set_small_caps_scale().

void set_small_caps_scale(PN_stdfloat small_caps_scale)

Sets the scale factor applied to lowercase letters from their uppercase equivalents, when the small_caps flag is in effect. See set_small_caps(). Normally, this will be a number less than one.

void set_tab_width(PN_stdfloat tab_width)

Sets the width of each tab stop, in screen units. A tab character embedded in the text will advance the horizontal position to the next tab stop.

void set_text_color(PN_stdfloat r, PN_stdfloat g, PN_stdfloat b, PN_stdfloat a)
void set_text_color(LColor const &text_color)
void set_transform(LMatrix4 const &transform)

Sets an additional transform that is applied to the entire text paragraph.

void set_usage_hint(GeomEnums::UsageHint usage_hint)

Specifies the UsageHint that will be applied to generated geometry. The default is UH_static, which is probably the right setting, but if you know the TextNode’s geometry will have a short lifespan, it may be better to set it to UH_stream. See geomEnums.h.

void set_wordwrap(PN_stdfloat wordwrap)

Sets the text up to automatically wordwrap when it exceeds the indicated width. This can be thought of as a right margin or margin width.

void update(void)

Can be called after the TextNode has been fully configured, to force the node to recompute its text immediately, rather than waiting for it to be drawn. This call is optional.

virtual void write(std::ostream &out, int indent_level = 0) const