textEncoder.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Filename: textEncoder.h
  2. // Created by: drose (26Mar03)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://etc.cmu.edu/panda3d/docs/license/ .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. #ifndef TEXTENCODER_H
  19. #define TEXTENCODER_H
  20. #include "pandabase.h"
  21. #include "unicodeLatinMap.h"
  22. #include "configVariableEnum.h"
  23. #include "typedObject.h"
  24. class StringDecoder;
  25. ////////////////////////////////////////////////////////////////////
  26. // Class : TextEncoder
  27. // Description : This class can be used to convert text between
  28. // multiple representations, e.g. utf-8 to Unicode. You
  29. // may use it as a static class object, passing the
  30. // encoding each time, or you may create an instance and
  31. // use that object, which will record the current
  32. // encoding and retain the current string.
  33. //
  34. // This class is also a base class of TextNode, which
  35. // inherits this functionality.
  36. ////////////////////////////////////////////////////////////////////
  37. class EXPCL_PANDAEXPRESS TextEncoder {
  38. PUBLISHED:
  39. enum Encoding {
  40. E_iso8859,
  41. E_utf8,
  42. E_unicode
  43. };
  44. INLINE TextEncoder();
  45. INLINE TextEncoder(const TextEncoder &copy);
  46. INLINE void set_encoding(Encoding encoding);
  47. INLINE Encoding get_encoding() const;
  48. INLINE static void set_default_encoding(Encoding encoding);
  49. INLINE static Encoding get_default_encoding();
  50. INLINE void set_text(const string &text);
  51. INLINE void set_text(const string &text, Encoding encoding);
  52. INLINE void clear_text();
  53. INLINE bool has_text() const;
  54. void make_upper();
  55. void make_lower();
  56. INLINE string get_text() const;
  57. INLINE string get_text(Encoding encoding) const;
  58. INLINE void append_text(const string &text);
  59. INLINE void append_unicode_char(int character);
  60. INLINE int get_num_chars() const;
  61. INLINE int get_unicode_char(int index) const;
  62. INLINE void set_unicode_char(int index, int character);
  63. INLINE string get_encoded_char(int index) const;
  64. INLINE string get_encoded_char(int index, Encoding encoding) const;
  65. INLINE string get_text_as_ascii() const;
  66. INLINE static string reencode_text(const string &text, Encoding from, Encoding to);
  67. INLINE static bool unicode_isalpha(int character);
  68. INLINE static bool unicode_isdigit(int character);
  69. INLINE static bool unicode_ispunct(int character);
  70. INLINE static bool unicode_islower(int character);
  71. INLINE static bool unicode_isupper(int character);
  72. INLINE static bool unicode_isspace(int character);
  73. INLINE static int unicode_toupper(int character);
  74. INLINE static int unicode_tolower(int character);
  75. INLINE static string upper(const string &source);
  76. INLINE static string upper(const string &source, Encoding encoding);
  77. INLINE static string lower(const string &source);
  78. INLINE static string lower(const string &source, Encoding encoding);
  79. // Direct support for wide-character strings. Now publishable with
  80. // the new wstring support in interrogate.
  81. INLINE void set_wtext(const wstring &wtext);
  82. INLINE const wstring &get_wtext() const;
  83. INLINE void append_wtext(const wstring &text);
  84. wstring get_wtext_as_ascii() const;
  85. bool is_wtext() const;
  86. static string encode_wchar(wchar_t ch, Encoding encoding);
  87. INLINE string encode_wtext(const wstring &wtext) const;
  88. static string encode_wtext(const wstring &wtext, Encoding encoding);
  89. INLINE wstring decode_text(const string &text) const;
  90. static wstring decode_text(const string &text, Encoding encoding);
  91. private:
  92. enum Flags {
  93. F_got_text = 0x0001,
  94. F_got_wtext = 0x0002,
  95. };
  96. static wstring decode_text_impl(StringDecoder &decoder);
  97. int _flags;
  98. Encoding _encoding;
  99. string _text;
  100. wstring _wtext;
  101. static ConfigVariableEnum<Encoding> _default_encoding;
  102. public:
  103. static TypeHandle get_class_type() {
  104. return _type_handle;
  105. }
  106. static void init_type() {
  107. register_type(_type_handle, "TextEncoder");
  108. }
  109. private:
  110. static TypeHandle _type_handle;
  111. };
  112. EXPCL_PANDAEXPRESS ostream &
  113. operator << (ostream &out, TextEncoder::Encoding encoding);
  114. EXPCL_PANDAEXPRESS istream &
  115. operator >> (istream &in, TextEncoder::Encoding &encoding);
  116. #include "textEncoder.I"
  117. #endif