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
setBin()
, or empty string if no bin has been set.- Getter
Returns the drawing bin set with
setBin()
, 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”.
- calcWidth(line: str) float
Returns the width of a line of text of arbitrary characters. The line should not include the newline character.
- calcWidth(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.
- calcWidth(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).
- clearAlign()
- clearBin()
Removes the effect of a previous call to
setBin()
. Text will be drawn in whatever bin it would like to be drawn in, with no explicit ordering.
- clearCard()
Specifies that a card will not be drawn behind the text.
- clearCardBorder()
- clearCardTexture()
- clearDrawOrder()
- clearFont()
Resets the font to the default font.
- clearFrame()
Specifies that a border will not be drawn around the text.
- clearGlyphScale()
- clearGlyphShift()
- clearIndent()
- clearMaxRows()
Resets the TextNode’s default behavior of not limiting the number of rows of text.
- clearShadow()
Specifies that a shadow will not be drawn behind the text.
- clearShadowColor()
- clearSlant()
- clearSmallCaps()
- clearSmallCapsScale()
- clearTabWidth()
- clearTextColor()
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
setDrawOrder()
.- Getter
Returns the drawing order set with
setDrawOrder()
.- 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
setBin()
.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
setFlattenFlags()
.- 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
setDefaultFont()
.
- forceUpdate()
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.
- getBottom() float
Returns the bottommost extent of the text in local 2-d coordinates, unmodified by the
setTransform()
matrix.
- getCardActual() LVecBase4
Returns the actual dimensions of the card around the text. If the card was set via
setCardAsMargin()
, the result returned by this function reflects the size of the current text; if the card was set viasetCardActual()
, 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.
- getCardAsSet() LVecBase4
Returns the dimensions of the card as set by
setCardAsMargin()
orsetCardActual()
. Use is_card_actual() to determine how to interpret the values returned by this function. It is an error to call this ifhasCard()
is false.
- getCardDecal() bool
Returns the card_decal flag. See
setCardDecal()
.
- getCardTransformed() LVecBase4
Returns the actual card dimensions, transformed by the matrix set by
setTransform()
. This returns the card dimensions in actual coordinates as seen by the rest of the world. Also seegetUpperLeft3d()
andgetLowerRight3d()
.
- static getClassType() TypeHandle
- getCoordinateSystem() CoordinateSystem
- getFlattenFlags() int
Returns the flatten flags. See
setFlattenFlags()
.
- getFrameActual() LVecBase4
Returns the actual dimensions of the frame around the text. If the frame was set via
setFrameAsMargin()
, the result returned by this function reflects the size of the current text; if the frame was set viasetFrameActual()
, 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.
- getFrameAsSet() LVecBase4
Returns the dimensions of the frame as set by
setFrameAsMargin()
orsetFrameActual()
. Use is_frame_actual() to determine how to interpret the values returned by this function. It is an error to call this ifhasFrame()
is false.
- getInternalGeom() 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.
- getLeft() float
Returns the leftmost extent of the text in local 2-d coordinates, unmodified by the
setTransform()
matrix.
- getLineHeight() 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.
- getLowerRight3d() LPoint3
Returns the lower-right extent of the text object, after it has been transformed into 3-d space by applying the
setTransform()
matrix.
- getMaxRows() int
Returns the limit on the height of the
TextNode
specified bysetMaxRows()
.
- getNumRows() 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.
- getRight() float
Returns the rightmost extent of the text in local 2-d coordinates, unmodified by the
setTransform()
matrix.
- getTop() float
Returns the topmost extent of the text in local 2-d coordinates, unmodified by the
setTransform()
matrix.
- getUpperLeft3d() LPoint3
Returns the upper-left extent of the text object, after it has been transformed into 3-d space by applying the
setTransform()
matrix.
- getUsageHint() UsageHint
Returns the UsageHint that will be applied to generated geometry. See
setUsageHint()
.
- getWordwrappedText() 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.
- getWordwrappedWtext() 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
setGlyphScale()
.- Getter
Returns the scale factor of each letter as specified by
setGlyphScale()
.- 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
setGlyphShift()
) to implement superscripting or subscripting.
- property glyph_shift float
Returns the vertical shift of each letter as specified by
setGlyphShift()
.- Getter
Returns the vertical shift of each letter as specified by
setGlyphShift()
.- Setter
Specifies a vertical amount to shift each letter of the text as it is placed. This can be used (possibly in conjunction with
setGlyphScale()
) to implement superscripting or subscripting.
- hasCharacter(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”.
- hasExactCharacter(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
hasCharacter()
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
hasCharacter()
.
- hasMaxRows() bool
Returns true if a limit on the height of the
TextNode
has been set viasetMaxRows()
, false otherwise.
- hasOverflow() 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.
- isCardAsMargin() bool
If this is true, the card was set via a call to
setCardAsMargin()
, and the dimension of the card as returned bygetCardAsSet()
represent a margin all around the text. If false, then the card was set via a call tosetCardActual()
, and the dimensions of the card as returned bygetCardAsSet()
are relative to the text’s origin.
- isFrameAsMargin() bool
If this is true, the frame was set via a call to
setFrameAsMargin()
, and the dimension of the frame as returned bygetFrameAsSet()
represent a margin all around the text. If false, then the frame was set via a call tosetFrameActual()
, and the dimensions of the frame as returned bygetFrameAsSet()
are relative to the text’s origin.
- isWhitespace(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 bysetMaxRows()
.- 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 andhasOverflow()
will return true.
- property preserve_trailing_whitespace bool
Returns the preserve_trailing_whitespace flag. See
setPreserveTrailingWhitespace()
.- Getter
Returns the preserve_trailing_whitespace flag. See
setPreserveTrailingWhitespace()
.- 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.
- setAlign(align_type: Alignment)
- setBin(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”.
- setCardActual(left: float, right: float, bottom: float, top: float)
Similar to
setCardAsMargin()
, 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.
- setCardAsMargin(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
setFrameAsMargin()
, the parameters are the amount of additional padding to insert around the text in each dimension, and all should generally be positive.
- setCardDecal(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.
- setCoordinateSystem(cs: CoordinateSystem)
Specifies the coordinate system in which the text will be generated.
- setDrawOrder(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
setBin()
.The return value is the first unused draw_order number, e.g. _draw_order + 3.
- setFlattenFlags(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.
- setFont(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
setDefaultFont()
.
- setFrameActual(left: float, right: float, bottom: float, top: float)
Similar to
setFrameAsMargin()
, 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.
- setFrameAsMargin(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.
- setFrameCorners(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.
- setFrameLineWidth(line_width: float)
Specifies the thickness of the lines that will be used to draw the frame.
- setGlyphScale(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
setGlyphShift()
) to implement superscripting or subscripting.
- setGlyphShift(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
setGlyphScale()
) to implement superscripting or subscripting.
- setIndent(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.
- setMaxRows(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 andhasOverflow()
will return true.
- setShadow(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.
- setShadow(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.
- setSmallCaps(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
setSmallCapsScale()
.
- setSmallCapsScale(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
setSmallCaps()
. Normally, this will be a number less than one.
- setTabWidth(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.
- setTextColor(text_color: LColor)
Sets the color of the text. Note that this will modulate the color of all components of the text, including the shadow and outline. If you wish to only set the foreground color, see
DynamicTextFont.setFg()
.
- setTextColor(r: float, g: float, b: float, a: float)
Sets the color of the text. Note that this will modulate the color of all components of the text, including the shadow and outline. If you wish to only set the foreground color, see
DynamicTextFont.setFg()
.
- setTransform(transform: LMatrix4)
Sets an additional transform that is applied to the entire text paragraph.
- setUsageHint(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.
- setWordwrap(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
setShadow()
. It is an error to call this ifhasShadow()
is false.- Getter
Returns the offset of the shadow as set by
setShadow()
. It is an error to call this ifhasShadow()
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
setSmallCaps()
.- Getter
Returns the small_caps flag. See
setSmallCaps()
.- 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
setSmallCapsScale()
.
- 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
setSmallCaps()
andsetSmallCapsScale()
.- Getter
Returns the scale factor applied to lowercase letters from their uppercase equivalents, when the small_caps flag is in effect. See
setSmallCaps()
andsetSmallCapsScale()
.- Setter
Sets the scale factor applied to lowercase letters from their uppercase equivalents, when the small_caps flag is in effect. See
setSmallCaps()
. Normally, this will be a number less than one.
- property tab_width float
Returns the width set via
setTabWidth()
.- Getter
Returns the width set via
setTabWidth()
.- 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_color LColor
Getter Setter
Sets the color of the text. Note that this will modulate the color of all components of the text, including the shadow and outline. If you wish to only set the foreground color, see
DynamicTextFont.setFg()
.Sets the color of the text. Note that this will modulate the color of all components of the text, including the shadow and outline. If you wish to only set the foreground color, see
DynamicTextFont.setFg()
.
- property text_scale float
Returns the scale factor of the text as specified by
setTextScale()
.- Getter
Returns the scale factor of the text as specified by
setTextScale()
.- 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 alsosetGlyphScale()
, 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
setUnderscore()
.- Getter
Returns the underscore flag. See
setUnderscore()
.- 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
setUnderscoreHeight()
.- Getter
Returns the vertical height of the underscore; see
setUnderscoreHeight()
.- Setter
Specifies the vertical height of the underscore, relative to the text baseline. This only has meaning if the underscore mode is enabled with
setUnderscore()
.
- 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
setUsageHint()
.- 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