panda3d.core.TextEncoder¶

class TextEncoder

This class can be used to convert text between multiple representations, e.g. UTF-8 to UTF-16. You may use it as a static class object, passing the encoding each time, or you may create an instance and use that object, which will record the current encoding and retain the current string.

This class is also a base class of TextNode, which inherits this functionality.

Inheritance diagram

enum Encoding
enumerator E_iso8859 = 0
enumerator E_utf8 = 1
enumerator E_utf16be = 2
enumerator E_unicode = 2

Deprecated alias for E_utf16be

__init__() → None
__init__(copy: TextEncoder) → None
appendText(text: object) → None
appendUnicodeChar(character: int) → None

Appends a single character to the end of the stored text. This may be a wide character, up to 16 bits in Unicode.

appendWtext(text: str) → None

Appends the indicates string to the end of the stored wide-character text.

clearText() → None

Removes the text from the TextEncoder.

decodeText(text: object) → object
decodeText(text: object, encoding: Encoding) → object
property default_encoding
Getter

Specifies the default encoding to be used for all subsequently created TextEncoder objects. See setEncoding().

Setter

Specifies the default encoding to be used for all subsequently created TextEncoder objects. See setEncoding().

Return type

Encoding

static encodeWchar(ch: int, encoding: Encoding) → object

Encodes a single Unicode character into a one-, two-, three-, or four-byte string, according to the given encoding system.

encodeWtext(wtext: str) → object

Encodes a wide-text string into a single-char string, according to the current encoding.

encodeWtext(wtext: str, encoding: Encoding) → object

Encodes a wide-text string into a single-char string, according to the given encoding.

static getDefaultEncoding() → Encoding

Specifies the default encoding to be used for all subsequently created TextEncoder objects. See setEncoding().

Return type

Encoding

getEncodedChar(index: size_t) → str

Returns the nth char of the stored text, as a one-, two-, or three-byte encoded string.

getEncodedChar(index: size_t, encoding: Encoding) → str

Returns the nth char of the stored text, as a one-, two-, or three-byte encoded string.

getEncoding() → Encoding

Returns the encoding by which the string set via setText() is to be interpreted. See setEncoding().

Return type

Encoding

getNumChars() → size_t

Returns the number of characters in the stored text. This is a count of wide characters, after the string has been decoded according to setEncoding().

Return type

size_t

getText() → object

Returns the current text, as encoded via the current encoding system.

getText(encoding: Encoding) → object

Returns the current text, as encoded via the indicated encoding system.

getTextAsAscii() → str

Returns the text associated with the node, converted as nearly as possible to a fully-ASCII representation. This means replacing accented letters with their unaccented ASCII equivalents.

It is possible that some characters in the string cannot be converted to ASCII. (The string may involve symbols like the copyright symbol, for instance, or it might involve letters in some other alphabet such as Greek or Cyrillic, or even Latin letters like thorn or eth that are not part of the ASCII character set.) In this case, as much of the string as possible will be converted to ASCII, and the nonconvertible characters will remain encoded in the encoding specified by setEncoding().

getUnicodeChar(index: size_t) → int

Returns the Unicode value of the nth character in the stored text. This may be a wide character (greater than 255), after the string has been decoded according to setEncoding().

getWtext() → str

Returns the text associated with the TextEncoder, as a wide-character string.

getWtextAsAscii() → str

Returns the text associated with the node, converted as nearly as possible to a fully-ASCII representation. This means replacing accented letters with their unaccented ASCII equivalents.

It is possible that some characters in the string cannot be converted to ASCII. (The string may involve symbols like the copyright symbol, for instance, or it might involve letters in some other alphabet such as Greek or Cyrillic, or even Latin letters like thorn or eth that are not part of the ASCII character set.) In this case, as much of the string as possible will be converted to ASCII, and the nonconvertible characters will remain in their original form.

hasText() → bool
isWtext() → bool

Returns true if any of the characters in the string returned by getWtext() are out of the range of an ASCII character (and, therefore, getWtext() should be called in preference to getText()).

static lower(source: str) → str

Converts the string to lowercase, assuming the string is encoded in the default encoding.

static lower(source: str, encoding: Encoding) → str

Converts the string to lowercase, assuming the string is encoded in the indicated encoding.

makeLower() → None

Adjusts the text stored within the encoder to all lowercase letters (preserving accent marks correctly).

makeUpper() → None

Adjusts the text stored within the encoder to all uppercase letters (preserving accent marks correctly).

static reencodeText(text: str, from: Encoding, to: Encoding) → str

Given the indicated text string, which is assumed to be encoded via the encoding “from”, decodes it and then reencodes it into the encoding “to”, and returns the newly encoded string. This does not change or affect any properties on the TextEncoder itself.

static setDefaultEncoding(encoding: Encoding) → None

Specifies the default encoding to be used for all subsequently created TextEncoder objects. See setEncoding().

setEncoding(encoding: Encoding) → None

Specifies how the string set via setText() is to be interpreted. The default, E_iso8859, means a standard string with one-byte characters (i.e. ASCII). Other encodings are possible to take advantage of character sets with more than 256 characters.

This affects only future calls to setText(); it does not change text that was set previously.

setText(text: object) → None
setText(text: object, encoding: Encoding) → None
setUnicodeChar(index: size_t, character: int) → None

Sets the Unicode value of the nth character in the stored text. This may be a wide character (greater than 255), after the string has been decoded according to setEncoding().

setWtext(wtext: str) → None

Changes the text that is stored in the encoder. Subsequent calls to getWtext() will return this same string, while getText() will return the encoded version of the string.

property text
Getter

Returns the current text, as encoded via the current encoding system.

Returns the current text, as encoded via the indicated encoding system.

Setter

Changes the text that is stored in the encoder. The text should be encoded according to the method indicated by setEncoding(). Subsequent calls to getText() will return this same string, while getWtext() will return the decoded version of the string.

The two-parameter version of setText() accepts an explicit encoding; the text is immediately decoded and stored as a wide-character string. Subsequent calls to getText() will return the same text re-encoded using whichever encoding is specified by setEncoding().

static unicodeIsalpha(character: int) → bool

Returns true if the indicated character is an alphabetic letter, false otherwise. This is akin to ctype’s isalpha(), extended to Unicode.

static unicodeIsdigit(character: int) → bool

Returns true if the indicated character is a numeric digit, false otherwise. This is akin to ctype’s isdigit(), extended to Unicode.

static unicodeIslower(character: int) → bool

Returns true if the indicated character is a lowercase letter, false otherwise. This is akin to ctype’s islower(), extended to Unicode.

static unicodeIspunct(character: int) → bool

Returns true if the indicated character is a punctuation mark, false otherwise. This is akin to ctype’s ispunct(), extended to Unicode.

static unicodeIsspace(character: int) → bool

Returns true if the indicated character is a whitespace letter, false otherwise. This is akin to ctype’s isspace(), extended to Unicode.

static unicodeIsupper(character: int) → bool

Returns true if the indicated character is an uppercase letter, false otherwise. This is akin to ctype’s isupper(), extended to Unicode.

static unicodeTolower(character: int) → int

Returns the uppercase equivalent of the given Unicode character. This is akin to ctype’s tolower(), extended to Unicode.

static unicodeToupper(character: int) → int

Returns the uppercase equivalent of the given Unicode character. This is akin to ctype’s toupper(), extended to Unicode.

static upper(source: str) → str

Converts the string to uppercase, assuming the string is encoded in the default encoding.

static upper(source: str, encoding: Encoding) → str

Converts the string to uppercase, assuming the string is encoded in the indicated encoding.