# panda3d.core.TextNode¶

from panda3d.core import TextNode

class TextNode

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

__init__(name: str) → None
__init__(name: str, copy: TextProperties) → None

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

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.

setMaxRows(max_rows: int) → None

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 hasOverflow() will return true.

clearMaxRows() → None

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

hasMaxRows() → bool

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

getMaxRows() → int

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

hasOverflow() → bool

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

setFrameColor(frame_color: LColor) → None
setFrameColor(r: float, g: float, b: float, a: float) → None
getFrameColor() → LColor
Return type

LColor

setCardBorder(size: float, uv_portion: float) → None
clearCardBorder() → None
getCardBorderSize() → float
getCardBorderUvPortion() → float
hasCardBorder() → bool
setCardColor(card_color: LColor) → None
setCardColor(r: float, g: float, b: float, a: float) → None
getCardColor() → LColor
Return type

LColor

setCardTexture(card_texture: Texture) → None
clearCardTexture() → None
hasCardTexture() → bool
getCardTexture() → Texture
Return type

Texture

setFrameAsMargin(left: float, right: float, bottom: float, top: float) → None

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.

setFrameActual(left: float, right: float, bottom: float, top: float) → None

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.

clearFrame() → None

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

hasFrame() → bool
isFrameAsMargin() → bool

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

getFrameAsSet() → LVecBase4

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

Return type

LVecBase4

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 via setFrameActual(), 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.

Return type

LVecBase4

setFrameLineWidth(line_width: float) → None

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

getFrameLineWidth() → float

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

setFrameCorners(corners: bool) → None

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.

getFrameCorners() → bool
setCardAsMargin(left: float, right: float, bottom: float, top: float) → None

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.

setCardActual(left: float, right: float, bottom: float, top: float) → None

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.

setCardDecal(card_decal: bool) → None

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.

clearCard() → None

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

hasCard() → bool
getCardDecal() → bool

Returns the card_decal flag. See setCardDecal().

isCardAsMargin() → bool

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

getCardAsSet() → LVecBase4

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

Return type

LVecBase4

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 via setCardActual(), 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.

Return type

LVecBase4

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 see getUpperLeft3d() and getLowerRight3d().

Return type

LVecBase4

setTransform(transform: LMatrix4) → None

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

getTransform() → LMatrix4
Return type

LMatrix4

setCoordinateSystem(cs: CoordinateSystem) → None

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

getCoordinateSystem() → CoordinateSystem
Return type

CoordinateSystem

setUsageHint(usage_hint: UsageHint) → None

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.

getUsageHint() → UsageHint

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

Return type

UsageHint

setFlattenFlags(flatten_flags: int) → None

Sets the flatten flags. This should be a union of the TextNodeFlattenFlags 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.

getFlattenFlags() → int

Returns the flatten flags. See setFlattenFlags().

setFont(font: TextFont) → None

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().

clearFont() → None

Resets the font to the default font.

setSmallCaps(small_caps: bool) → None

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().

clearSmallCaps() → None
setSmallCapsScale(small_caps_scale: float) → None

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.

clearSmallCapsScale() → None
setSlant(slant: float) → None
clearSlant() → None
setAlign(align_type: Alignment) → None
clearAlign() → None
setIndent(indent: float) → None

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.

clearIndent() → None
setWordwrap(wordwrap: float) → None

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.

clearWordwrap() → None

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

setTextColor(text_color: LColor) → None
setTextColor(r: float, g: float, b: float, a: float) → None
clearTextColor() → None

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

setShadowColor(shadow_color: LColor) → None
setShadowColor(r: float, g: float, b: float, a: float) → None
clearShadowColor() → None
setShadow(shadow_offset: LVecBase2) → None

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) → None

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.

clearShadow() → None

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

setBin(bin: str) → None

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”.

clearBin() → None

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.

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.

clearDrawOrder() → None
setTabWidth(tab_width: float) → None

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.

clearTabWidth() → None
setGlyphScale(glyph_scale: float) → None

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.

clearGlyphScale() → None
setGlyphShift(glyph_shift: float) → None

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.

clearGlyphShift() → None
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.

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).

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().

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”.

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.

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.

output(out: ostream) → None
write(out: ostream, indent_level: int) → None
getLeft() → float

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

getRight() → float

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

getBottom() → float

Returns the bottommost 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.

getHeight() → float

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

getWidth() → float

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

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.

Return type

LPoint3

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.

Return type

LPoint3

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.

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.

Return type

PandaNode

update() → None

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.

forceUpdate() → None

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).

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.

Return type

PandaNode

static getClassType() → TypeHandle
Return type

TypeHandle

property max_rows
Getter

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

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 and hasOverflow() will return true.

Return type

int

property frame_color

Getter Setter

Return type

LColor

property card_color

Getter Setter

Return type

LColor

property card_texture

Getter Setter

Return type

Texture

property frame_line_width
Getter

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

Setter

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

Return type

float

property frame_corners

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.

Return type

bool

property transform

Getter Setter

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

Return type

LMatrix4

property coordinate_system

Getter Setter

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

Return type

CoordinateSystem

property usage_hint
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.

Return type

UsageHint

property flatten_flags
Getter

Returns the flatten flags. See setFlattenFlags().

Setter

Sets the flatten flags. This should be a union of the TextNodeFlattenFlags 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.

Return type

int

property font

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().

Return type

TextFont

property small_caps

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().

Return type

bool

property small_caps_scale

Returns the scale factor applied to lowercase letters from their uppercase equivalents, when the small_caps flag is in effect. See setSmallCaps() and setSmallCapsScale().

Getter

Returns the scale factor applied to lowercase letters from their uppercase equivalents, when the small_caps flag is in effect. See setSmallCaps() and setSmallCapsScale().

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.

Return type

float

property slant

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

Return type

float

property underscore

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.

Return type

bool

property underscore_height

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().

Return type

float

property align

Getter Setter

Return type

Alignment

property indent

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.

Return type

float

property wordwrap

Getter Setter

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.

Return type

float

property preserve_trailing_whitespace

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.

Return type

bool

property text_color

Getter Setter

Return type

LColor

property shadow_color

Getter Setter

Return type

LColor

property shadow

Returns the offset of the shadow as set by setShadow(). It is an error to call this if hasShadow() is false.

Getter

Returns the offset of the shadow as set by setShadow(). It is an error to call this if hasShadow() 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.

Return type

LVector2

property bin

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”.

Return type

string

property draw_order

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.

Return type

int

property tab_width

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.

Return type

float

property glyph_scale

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.

Return type

float

property glyph_shift

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.

Return type

float

property text_scale

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 also setGlyphScale(), which is cumulative.

Return type

float

enum FlattenFlags
enumerator FF_none = 0
enumerator FF_light = 1
enumerator FF_medium = 2
enumerator FF_strong = 4
enumerator FF_dynamic_merge = 8