Panda3D
Loading...
Searching...
No Matches
textEncoder.h
Go to the documentation of this file.
1/**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file textEncoder.h
10 * @author drose
11 * @date 2003-03-26
12 */
13
14#ifndef TEXTENCODER_H
15#define TEXTENCODER_H
16
17#include "dtoolbase.h"
18#include "unicodeLatinMap.h"
19
20#include <ctype.h>
21
22class StringDecoder;
23
24/**
25 * This class can be used to convert text between multiple representations,
26 * e.g. UTF-8 to UTF-16. You may use it as a static class object, passing
27 * the encoding each time, or you may create an instance and use that object,
28 * which will record the current encoding and retain the current string.
29 *
30 * This class is also a base class of TextNode, which inherits this
31 * functionality.
32 */
33class EXPCL_DTOOL_DTOOLUTIL TextEncoder {
34PUBLISHED:
35 enum Encoding {
36 E_iso8859,
37 E_utf8,
38 E_utf16be,
39
40 // Deprecated alias for E_utf16be
41 E_unicode = E_utf16be,
42 };
43
44 INLINE TextEncoder();
45 INLINE TextEncoder(const TextEncoder &copy);
46
47 virtual ~TextEncoder() = default;
48
49 INLINE void set_encoding(Encoding encoding);
50 INLINE Encoding get_encoding() const;
51
52 INLINE static void set_default_encoding(Encoding encoding);
53 INLINE static Encoding get_default_encoding();
54 MAKE_PROPERTY(default_encoding, get_default_encoding, set_default_encoding);
55
56#ifdef CPPPARSER
57 EXTEND void set_text(PyObject *text);
58 EXTEND void set_text(PyObject *text, Encoding encoding);
59#else
60 INLINE void set_text(const std::string &text);
61 INLINE void set_text(const std::string &text, Encoding encoding);
62#endif
63 INLINE void clear_text();
64 INLINE bool has_text() const;
65
66 void make_upper();
67 void make_lower();
68
69#ifdef CPPPARSER
70 EXTEND PyObject *get_text() const;
71 EXTEND PyObject *get_text(Encoding encoding) const;
72 EXTEND void append_text(PyObject *text);
73#else
74 INLINE std::string get_text() const;
75 INLINE std::string get_text(Encoding encoding) const;
76 INLINE void append_text(const std::string &text);
77#endif
78 INLINE void append_unicode_char(char32_t character);
79 INLINE size_t get_num_chars() const;
80 INLINE int get_unicode_char(size_t index) const;
81 INLINE void set_unicode_char(size_t index, char32_t character);
82 INLINE std::string get_encoded_char(size_t index) const;
83 INLINE std::string get_encoded_char(size_t index, Encoding encoding) const;
84 INLINE std::string get_text_as_ascii() const;
85
86 INLINE static std::string reencode_text(const std::string &text, Encoding from, Encoding to);
87
88 INLINE static bool unicode_isalpha(char32_t character);
89 INLINE static bool unicode_isdigit(char32_t character);
90 INLINE static bool unicode_ispunct(char32_t character);
91 INLINE static bool unicode_islower(char32_t character);
92 INLINE static bool unicode_isupper(char32_t character);
93 INLINE static bool unicode_isspace(char32_t character);
94 INLINE static int unicode_toupper(char32_t character);
95 INLINE static int unicode_tolower(char32_t character);
96
97 INLINE static std::string upper(const std::string &source);
98 INLINE static std::string upper(const std::string &source, Encoding encoding);
99 INLINE static std::string lower(const std::string &source);
100 INLINE static std::string lower(const std::string &source, Encoding encoding);
101
102 // Direct support for wide-character strings. Now publishable with the new
103 // wstring support in interrogate.
104 INLINE void set_wtext(const std::wstring &wtext);
105 INLINE const std::wstring &get_wtext() const;
106 INLINE void append_wtext(const std::wstring &text);
107 std::wstring get_wtext_as_ascii() const;
108 bool is_wtext() const;
109
110#ifdef CPPPARSER
111 EXTEND static PyObject *encode_wchar(char32_t ch, Encoding encoding);
112 EXTEND INLINE PyObject *encode_wtext(const std::wstring &wtext) const;
113 EXTEND static PyObject *encode_wtext(const std::wstring &wtext, Encoding encoding);
114 EXTEND INLINE PyObject *decode_text(PyObject *text) const;
115 EXTEND static PyObject *decode_text(PyObject *text, Encoding encoding);
116#else
117 static std::string encode_wchar(char32_t ch, Encoding encoding);
118 INLINE std::string encode_wtext(const std::wstring &wtext) const;
119 static std::string encode_wtext(const std::wstring &wtext, Encoding encoding);
120 INLINE std::wstring decode_text(const std::string &text) const;
121 static std::wstring decode_text(const std::string &text, Encoding encoding);
122#endif
123
124 MAKE_PROPERTY(text, get_text, set_text);
125
126protected:
127 virtual void text_changed();
128
129private:
130 enum Flags {
131 F_got_text = 0x0001,
132 F_got_wtext = 0x0002,
133 };
134 static std::wstring decode_text_impl(StringDecoder &decoder);
135
136 int _flags;
137 Encoding _encoding;
138 std::string _text;
139 std::wstring _wtext;
140
141 static Encoding _default_encoding;
142};
143
144EXPCL_DTOOL_DTOOLUTIL std::ostream &
145operator << (std::ostream &out, TextEncoder::Encoding encoding);
146EXPCL_DTOOL_DTOOLUTIL std::istream &
147operator >> (std::istream &in, TextEncoder::Encoding &encoding);
148
149// We'll define the output operator for wstring here, too. Presumably this
150// will not be automatically defined by any system libraries.
151
152// This function is declared inline to minimize the risk of link conflicts
153// should another third-party module also define the same output operator.
154INLINE EXPCL_DTOOL_DTOOLUTIL std::ostream &
155operator << (std::ostream &out, const std::wstring &str);
156
157#include "textEncoder.I"
158
159#endif
The base class to a family of classes that decode various kinds of encoded byte streams.
std::wstring decode_text(const std::string &text) const
Returns the given wstring decoded to a single-byte string, via the current encoding system.
void append_text(const std::string &text)
Appends the indicates string to the end of the stored text.
set_text
Changes the text that is stored in the encoder.
static std::string upper(const std::string &source)
Converts the string to uppercase, assuming the string is encoded in the default encoding.
void append_wtext(const std::wstring &text)
Appends the indicates string to the end of the stored wide-character text.
void set_unicode_char(size_t index, char32_t character)
Sets the Unicode value of the nth character in the stored text.
static std::string lower(const std::string &source)
Converts the string to lowercase, assuming the string is encoded in the default encoding.
std::string get_text_as_ascii() const
Returns the text associated with the node, converted as nearly as possible to a fully-ASCII represent...
static std::string reencode_text(const std::string &text, Encoding from, Encoding to)
Given the indicated text string, which is assumed to be encoded via the encoding "from",...
static std::string encode_wchar(char32_t ch, Encoding encoding)
Encodes a single Unicode character into a one-, two-, three-, or four-byte string,...
static bool unicode_ispunct(char32_t character)
Returns true if the indicated character is a punctuation mark, false otherwise.
static int unicode_tolower(char32_t character)
Returns the uppercase equivalent of the given Unicode character.
static bool unicode_isupper(char32_t character)
Returns true if the indicated character is an uppercase letter, false otherwise.
bool is_wtext() const
Returns true if any of the characters in the string returned by get_wtext() are out of the range of a...
get_default_encoding
Specifies the default encoding to be used for all subsequently created TextEncoder objects.
Definition textEncoder.h:54
static int unicode_toupper(char32_t character)
Returns the uppercase equivalent of the given Unicode character.
std::wstring get_wtext_as_ascii() const
Returns the text associated with the node, converted as nearly as possible to a fully-ASCII represent...
Encoding get_encoding() const
Returns the encoding by which the string set via set_text() is to be interpreted.
Definition textEncoder.I:60
static bool unicode_isdigit(char32_t character)
Returns true if the indicated character is a numeric digit, false otherwise.
void set_encoding(Encoding encoding)
Specifies how the string set via set_text() is to be interpreted.
Definition textEncoder.I:48
get_text
Returns the current text, as encoded via the current encoding system.
const std::wstring & get_wtext() const
Returns the text associated with the TextEncoder, as a wide-character string.
size_t get_num_chars() const
Returns the number of characters in the stored text.
void make_lower()
Adjusts the text stored within the encoder to all lowercase letters (preserving accent marks correctl...
void clear_text()
Removes the text from the TextEncoder.
void make_upper()
Adjusts the text stored within the encoder to all uppercase letters (preserving accent marks correctl...
std::string get_encoded_char(size_t index) const
Returns the nth char of the stored text, as a one-, two-, or three-byte encoded string.
static bool unicode_isspace(char32_t character)
Returns true if the indicated character is a whitespace letter, false otherwise.
int get_unicode_char(size_t index) const
Returns the Unicode value of the nth character in the stored text.
static bool unicode_islower(char32_t character)
Returns true if the indicated character is a lowercase letter, false otherwise.
set_default_encoding
Specifies the default encoding to be used for all subsequently created TextEncoder objects.
Definition textEncoder.h:54
std::string encode_wtext(const std::wstring &wtext) const
Encodes a wide-text string into a single-char string, according to the current encoding.
static bool unicode_isalpha(char32_t character)
Returns true if the indicated character is an alphabetic letter, false otherwise.
void append_unicode_char(char32_t character)
Appends a single character to the end of the stored text.
void set_wtext(const std::wstring &wtext)
Changes the text that is stored in the encoder.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.