textEncoder.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. class StringDecoder;
  24. ////////////////////////////////////////////////////////////////////
  25. // Class : TextEncoder
  26. // Description : This class can be used to convert text between
  27. // multiple representations, e.g. utf-8 to Unicode. You
  28. // may use it as a static class object, passing the
  29. // encoding each time, or you may create an instance and
  30. // use that object, which will record the current
  31. // encoding and retain the current string.
  32. //
  33. // This class is also a base class of TextNode, which
  34. // inherits this functionality.
  35. ////////////////////////////////////////////////////////////////////
  36. class EXPCL_PANDAEXPRESS TextEncoder {
  37. PUBLISHED:
  38. enum Encoding {
  39. E_iso8859,
  40. E_utf8,
  41. E_unicode
  42. };
  43. INLINE TextEncoder();
  44. INLINE void set_encoding(Encoding encoding);
  45. INLINE Encoding get_encoding() const;
  46. INLINE static void set_default_encoding(Encoding encoding);
  47. INLINE static Encoding get_default_encoding();
  48. INLINE void set_text(const string &text);
  49. INLINE void set_text(const string &text, Encoding encoding);
  50. INLINE void clear_text();
  51. INLINE bool has_text() const;
  52. void make_upper();
  53. void make_lower();
  54. INLINE string get_text() const;
  55. INLINE string get_text(Encoding encoding) const;
  56. INLINE void append_text(const string &text);
  57. INLINE void append_unicode_char(int character);
  58. INLINE int get_num_chars() const;
  59. INLINE int get_unicode_char(int index) const;
  60. INLINE void set_unicode_char(int index, int character);
  61. INLINE string get_encoded_char(int index) const;
  62. INLINE string get_encoded_char(int index, Encoding encoding) const;
  63. INLINE string get_text_as_ascii() const;
  64. INLINE static string reencode_text(const string &text, Encoding from, Encoding to);
  65. INLINE static bool unicode_isalpha(int character);
  66. INLINE static bool unicode_isdigit(int character);
  67. INLINE static bool unicode_ispunct(int character);
  68. INLINE static bool unicode_islower(int character);
  69. INLINE static bool unicode_isupper(int character);
  70. INLINE static int unicode_toupper(int character);
  71. INLINE static int unicode_tolower(int character);
  72. INLINE static string upper(const string &source);
  73. INLINE static string upper(const string &source, Encoding encoding);
  74. INLINE static string lower(const string &source);
  75. INLINE static string lower(const string &source, Encoding encoding);
  76. public:
  77. // Direct support for wide-character strings. Not publishable for
  78. // now (until we add support for wstring to interrogate).
  79. INLINE void set_wtext(const wstring &wtext);
  80. INLINE const wstring &get_wtext() const;
  81. INLINE void append_wtext(const wstring &text);
  82. wstring get_wtext_as_ascii() const;
  83. static string encode_wchar(wchar_t ch, Encoding encoding);
  84. INLINE string encode_wtext(const wstring &wtext) const;
  85. static string encode_wtext(const wstring &wtext, Encoding encoding);
  86. INLINE wstring decode_text(const string &text) const;
  87. static wstring decode_text(const string &text, Encoding encoding);
  88. private:
  89. enum Flags {
  90. F_got_text = 0x0001,
  91. F_got_wtext = 0x0002,
  92. };
  93. static wstring decode_text_impl(StringDecoder &decoder);
  94. int _flags;
  95. Encoding _encoding;
  96. string _text;
  97. wstring _wtext;
  98. static ConfigVariableEnum<Encoding> _default_encoding;
  99. public:
  100. static TypeHandle get_class_type() {
  101. return _type_handle;
  102. }
  103. static void init_type() {
  104. register_type(_type_handle, "TextEncoder");
  105. }
  106. private:
  107. static TypeHandle _type_handle;
  108. };
  109. EXPCL_PANDAEXPRESS ostream &
  110. operator << (ostream &out, TextEncoder::Encoding encoding);
  111. EXPCL_PANDAEXPRESS istream &
  112. operator >> (istream &in, TextEncoder::Encoding &encoding);
  113. #include "textEncoder.I"
  114. #endif