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.
This class can be used to convert text between multiple representations, e.g.
Definition textEncoder.h:33
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.