TextNode
from panda3d.core import TextNode
- class TextNode
Bases:
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 callgenerate()
a new node is returned.Inheritance diagram
-
enum FlattenFlags
-
enumerator FF_none = 0
-
enumerator FF_light = 1
-
enumerator FF_medium = 2
-
enumerator FF_strong = 4
-
enumerator FF_dynamic_merge = 8
-
enumerator FF_none = 0
- __init__(name: str, copy: TextProperties)
It’s sort of a copy constructor: it copies the indicated
TextProperties
, without copying a completeTextNode
.
- property align Alignment
- property bin string
Returns the drawing bin set with
set_bin()
, or empty string if no bin has been set.- Getter
Returns the drawing bin set with
set_bin()
, or empty string if no bin has been set.- Setter
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”.
- calc_width(line: str) float
Returns the width of a line of text of arbitrary characters. The line should not include the newline character.
- calc_width(line: str) float
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.
- calc_width(character: int) float
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).
- clear_align()
- clear_bin()
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.
- clear_card()
Specifies that a card will not be drawn behind the text.
- clear_card_border()
- clear_card_texture()
- clear_draw_order()
- clear_font()
Resets the font to the default font.
- clear_frame()
Specifies that a border will not be drawn around the text.
- clear_glyph_scale()
- clear_glyph_shift()
- clear_indent()
- clear_max_rows()
Resets the TextNode’s default behavior of not limiting the number of rows of text.
- clear_shadow()
Specifies that a shadow will not be drawn behind the text.
- clear_shadow_color()
- clear_slant()
- clear_small_caps()
- clear_small_caps_scale()
- clear_tab_width()
- clear_text_color()
Removes the text color specification; the text will be colored whatever it was in the source font file.
- property coordinate_system CoordinateSystem
Getter Setter
Specifies the coordinate system in which the text will be generated.
- property draw_order int
Returns the drawing order set with
set_draw_order()
.- Getter
Returns the drawing order set with
set_draw_order()
.- Setter
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.
- property flatten_flags int
- Getter
Returns the flatten flags. See
set_flatten_flags()
.- Setter
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 bygenerate()
) 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.
- property font TextFont
Returns the font currently in use, if any. If no font is in use, this returns the default font.
- Getter
Returns the font currently in use, if any. If no font is in use, this returns the default font.
- Setter
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()
.
- force_update()
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).
- property frame_corners bool
Getter Setter
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.
- property frame_line_width float
Returns/Specifies the thickness of the lines that will be used to draw the frame.
- generate() PandaNode
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.
- get_bottom() float
Returns the bottommost extent of the text in local 2-d coordinates, unmodified by the
set_transform()
matrix.
- get_card_actual() LVecBase4
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 viaset_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.
- get_card_as_set() LVecBase4
Returns the dimensions of the card as set by
set_card_as_margin()
orset_card_actual()
. Use is_card_actual() to determine how to interpret the values returned by this function. It is an error to call this ifhas_card()
is false.
- get_card_decal() bool
Returns the card_decal flag. See
set_card_decal()
.
- get_card_transformed() LVecBase4
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 seeget_upper_left_3d()
andget_lower_right_3d()
.
- static get_class_type() TypeHandle
- get_coordinate_system() CoordinateSystem
- get_flatten_flags() int
Returns the flatten flags. See
set_flatten_flags()
.
- get_frame_actual() LVecBase4
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 viaset_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.
- get_frame_as_set() LVecBase4
Returns the dimensions of the frame as set by
set_frame_as_margin()
orset_frame_actual()
. Use is_frame_actual() to determine how to interpret the values returned by this function. It is an error to call this ifhas_frame()
is false.
- get_frame_line_width() float
Returns the thickness of the lines that will be used to draw the frame.
- get_internal_geom() PandaNode
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.
- get_left() float
Returns the leftmost extent of the text in local 2-d coordinates, unmodified by the
set_transform()
matrix.
- get_line_height() float
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.
- get_lower_right_3d() LPoint3
Returns the lower-right extent of the text object, after it has been transformed into 3-d space by applying the
set_transform()
matrix.
- get_max_rows() int
Returns the limit on the height of the
TextNode
specified byset_max_rows()
.
- get_num_rows() int
Returns the number of rows of text that were generated. This counts word- wrapped rows as well as rows generated due to embedded newlines.
- get_right() float
Returns the rightmost extent of the text in local 2-d coordinates, unmodified by the
set_transform()
matrix.
- get_top() float
Returns the topmost extent of the text in local 2-d coordinates, unmodified by the
set_transform()
matrix.
- get_upper_left_3d() LPoint3
Returns the upper-left extent of the text object, after it has been transformed into 3-d space by applying the
set_transform()
matrix.
- get_usage_hint() UsageHint
Returns the UsageHint that will be applied to generated geometry. See
set_usage_hint()
.
- get_wordwrapped_text() str
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.
- get_wordwrapped_wtext() str
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.
- property glyph_scale float
Returns the scale factor of each letter as specified by
set_glyph_scale()
.- Getter
Returns the scale factor of each letter as specified by
set_glyph_scale()
.- Setter
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.
- property glyph_shift float
Returns the vertical shift of each letter as specified by
set_glyph_shift()
.- Getter
Returns the vertical shift of each letter as specified by
set_glyph_shift()
.- Setter
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.
- has_character(character: int) bool
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”.
- has_exact_character(character: int) bool
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()
.
- has_max_rows() bool
Returns true if a limit on the height of the
TextNode
has been set viaset_max_rows()
, false otherwise.
- has_overflow() bool
Returns true if the last text set on the text node exceeded the max_rows constraint, or false if it all fit.
- property indent float
Getter Setter
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.
- is_card_as_margin() bool
If this is true, the card was set via a call to
set_card_as_margin()
, and the dimension of the card as returned byget_card_as_set()
represent a margin all around the text. If false, then the card was set via a call toset_card_actual()
, and the dimensions of the card as returned byget_card_as_set()
are relative to the text’s origin.
- is_frame_as_margin() bool
If this is true, the frame was set via a call to
set_frame_as_margin()
, and the dimension of the frame as returned byget_frame_as_set()
represent a margin all around the text. If false, then the frame was set via a call toset_frame_actual()
, and the dimensions of the frame as returned byget_frame_as_set()
are relative to the text’s origin.
- is_whitespace(character: int) bool
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.
- property max_rows int
- Getter
Returns the limit on the height of the
TextNode
specified byset_max_rows()
.- Setter
Sets the maximum number of rows that may be formatted by the
TextNode
. If more text than this is attempted, it will be truncated andhas_overflow()
will return true.
- property preserve_trailing_whitespace bool
Returns the preserve_trailing_whitespace flag. See
set_preserve_trailing_whitespace()
.- Getter
Returns the preserve_trailing_whitespace flag. See
set_preserve_trailing_whitespace()
.- Setter
Sets the preserve_trailing_whitespace flag. When this is set, trailing whitespace at the end of the line is not stripped when the text is wordwrapped (it is stripped by default). Since the trailing whitespace is invisible, this is important primarily for determining the proper width of a frame or card behind the text.
- set_align(align_type: Alignment)
- set_bin(bin: str)
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”.
- set_card_actual(left: float, right: float, bottom: float, top: float)
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.
- set_card_as_margin(left: float, right: float, bottom: float, top: float)
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.
- set_card_decal(card_decal: bool)
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.
- set_coordinate_system(cs: CoordinateSystem)
Specifies the coordinate system in which the text will be generated.
- set_draw_order(draw_order: int) int
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.
- set_flatten_flags(flatten_flags: int)
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 bygenerate()
) 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.
- set_font(font: TextFont)
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()
.
- set_frame_actual(left: float, right: float, bottom: float, top: float)
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.
- set_frame_as_margin(left: float, right: float, bottom: float, top: float)
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.
- set_frame_corners(corners: bool)
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.
- set_frame_line_width(line_width: float)
Specifies the thickness of the lines that will be used to draw the frame.
- set_glyph_scale(glyph_scale: float)
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.
- set_glyph_shift(glyph_shift: float)
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.
- set_indent(indent: float)
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.
- set_max_rows(max_rows: int)
Sets the maximum number of rows that may be formatted by the
TextNode
. If more text than this is attempted, it will be truncated andhas_overflow()
will return true.
- set_shadow(shadow_offset: LVecBase2)
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.
- set_shadow(xoffset: float, yoffset: float)
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.
- set_small_caps(small_caps: bool)
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()
.
- set_small_caps_scale(small_caps_scale: float)
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.
- set_tab_width(tab_width: float)
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.
- set_transform(transform: LMatrix4)
Sets an additional transform that is applied to the entire text paragraph.
- set_usage_hint(usage_hint: UsageHint)
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.
- set_wordwrap(wordwrap: float)
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.
- property shadow LVector2
Returns the offset of the shadow as set by
set_shadow()
. It is an error to call this ifhas_shadow()
is false.- Getter
Returns the offset of the shadow as set by
set_shadow()
. It is an error to call this ifhas_shadow()
is false.- Setter
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.
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.
- property slant float
Returns the factor by which the text is specified to slant to the right.
- Getter
Returns the factor by which the text is specified to slant to the right.
Setter
- property small_caps bool
Returns the small_caps flag. See
set_small_caps()
.- Getter
Returns the small_caps flag. See
set_small_caps()
.- Setter
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()
.
- property small_caps_scale float
Returns the scale factor applied to lowercase letters from their uppercase equivalents, when the small_caps flag is in effect. See
set_small_caps()
andset_small_caps_scale()
.- Getter
Returns the scale factor applied to lowercase letters from their uppercase equivalents, when the small_caps flag is in effect. See
set_small_caps()
andset_small_caps_scale()
.- Setter
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.
- property tab_width float
Returns the width set via
set_tab_width()
.- Getter
Returns the width set via
set_tab_width()
.- Setter
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.
- property text_scale float
Returns the scale factor of the text as specified by
set_text_scale()
.- Getter
Returns the scale factor of the text as specified by
set_text_scale()
.- Setter
Specifies the factor by which to scale the text, in addition to any scalings imposed by the node, as well as in addition to the glyph scale.
The text scale is not cumulative when applied to nested
TextProperties
. See alsoset_glyph_scale()
, which is cumulative.
- property transform LMatrix4
Getter Setter
Sets an additional transform that is applied to the entire text paragraph.
- property underscore bool
Returns the underscore flag. See
set_underscore()
.- Getter
Returns the underscore flag. See
set_underscore()
.- Setter
Sets the underscore flag. When this is set, the text is underscored with a one-pixel line the same color as the text foreground, drawn at the baseline.
- property underscore_height float
Returns the vertical height of the underscore; see
set_underscore_height()
.- Getter
Returns the vertical height of the underscore; see
set_underscore_height()
.- Setter
Specifies the vertical height of the underscore, relative to the text baseline. This only has meaning if the underscore mode is enabled with
set_underscore()
.
- update()
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.
- property usage_hint UsageHint
- Getter
Returns the UsageHint that will be applied to generated geometry. See
set_usage_hint()
.- Setter
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.
-
enum FlattenFlags