encconv.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: encconv.h
  3. // Purpose: interface of wxEncodingConverter
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxEncodingConverter
  9. This class is capable of converting strings between two 8-bit encodings/charsets.
  10. It can also convert from/to Unicode.
  11. Only a limited subset of encodings is supported by wxEncodingConverter:
  12. @c wxFONTENCODING_ISO8859_1..15, @c wxFONTENCODING_CP1250..1257 and
  13. @c wxFONTENCODING_KOI8.
  14. @note
  15. Please use wxMBConv classes instead if possible. wxCSConv has much better
  16. support for various encodings than wxEncodingConverter.
  17. wxEncodingConverter is useful only if you rely on wxCONVERT_SUBSTITUTE mode
  18. of operation (see wxEncodingConverter::Init()).
  19. @library{wxbase}
  20. @category{conv}
  21. @see wxFontMapper, wxMBConv, @ref overview_nonenglish
  22. */
  23. class wxEncodingConverter : public wxObject
  24. {
  25. public:
  26. /**
  27. Constructor.
  28. */
  29. wxEncodingConverter();
  30. /**
  31. Return @true if (any text in) multibyte encoding @a encIn can be converted to
  32. another one (@a encOut) losslessly.
  33. Do not call this method with @c wxFONTENCODING_UNICODE as either parameter,
  34. it doesn't make sense (always works in one sense and always depends
  35. on the text to convert in the other).
  36. */
  37. static bool CanConvert(wxFontEncoding encIn,
  38. wxFontEncoding encOut);
  39. /**
  40. @name Conversion functions
  41. @{
  42. */
  43. /**
  44. Convert input string according to settings passed to Init() and writes
  45. the result to output.
  46. All the Convert() function overloads return @true if the conversion was
  47. lossless and @false if at least one of the characters couldn't be converted
  48. was and replaced with '?' in the output.
  49. Note that if @c wxCONVERT_SUBSTITUTE was passed to Init(), substitution is
  50. considered a lossless operation.
  51. @note You must call Init() before using this method!
  52. */
  53. bool Convert(const char* input, char* output) const;
  54. bool Convert(const wchar_t* input, wchar_t* output) const;
  55. bool Convert(const char* input, wchar_t* output) const;
  56. bool Convert(const wchar_t* input, char* output) const;
  57. /**
  58. Convert input string according to settings passed to Init() in-place.
  59. With this overload, the conversion result is written to the same memory
  60. area from which the input is read.
  61. See the Convert(const char*,char*) const overload for more info.
  62. */
  63. bool Convert(char* str) const;
  64. /**
  65. Convert input string according to settings passed to Init() in-place.
  66. With this overload, the conversion result is written to the same memory
  67. area from which the input is read.
  68. See the Convert(const wchar_t*,wchar_t*) const overload for more info.
  69. */
  70. bool Convert(wchar_t* str) const;
  71. /**
  72. Convert a wxString and return a new wxString object.
  73. See the Convert(const char*,char*) const overload for more info.
  74. */
  75. wxString Convert(const wxString& input) const;
  76. //@}
  77. /**
  78. Similar to GetPlatformEquivalents(), but this one will return ALL
  79. equivalent encodings, regardless of the platform, and including itself.
  80. This platform's encodings are before others in the array.
  81. And again, if @a enc is in the array, it is the very first item in it.
  82. */
  83. static wxFontEncodingArray GetAllEquivalents(wxFontEncoding enc);
  84. /**
  85. Return equivalents for given font that are used under given platform.
  86. Supported platforms:
  87. @li wxPLATFORM_UNIX
  88. @li wxPLATFORM_WINDOWS
  89. @li wxPLATFORM_OS2
  90. @li wxPLATFORM_MAC
  91. @li wxPLATFORM_CURRENT
  92. wxPLATFORM_CURRENT means the platform this binary was compiled for.
  93. Examples:
  94. @verbatim
  95. current platform enc returned value
  96. ----------------------------------------------
  97. unix CP1250 {ISO8859_2}
  98. unix ISO8859_2 {ISO8859_2}
  99. windows ISO8859_2 {CP1250}
  100. unix CP1252 {ISO8859_1,ISO8859_15}
  101. @endverbatim
  102. Equivalence is defined in terms of convertibility: two encodings are
  103. equivalent if you can convert text between then without losing
  104. information (it may - and will - happen that you lose special chars
  105. like quotation marks or em-dashes but you shouldn't lose any diacritics
  106. and language-specific characters when converting between equivalent encodings).
  107. Remember that this function does @b NOT check for presence of
  108. fonts in system. It only tells you what are most suitable
  109. encodings. (It usually returns only one encoding.)
  110. @note Note that argument enc itself may be present in the returned array,
  111. so that you can, as a side-effect, detect whether the encoding is
  112. native for this platform or not.
  113. @note Convert() is not limited to converting between equivalent encodings,
  114. it can convert between two arbitrary encodings.
  115. @note If @a enc is present in the returned array, then it is always the first
  116. item of it.
  117. @note Please note that the returned array may contain no items at all.
  118. */
  119. static wxFontEncodingArray GetPlatformEquivalents(wxFontEncoding enc,
  120. int platform = wxPLATFORM_CURRENT);
  121. /**
  122. Initialize the conversion.
  123. Both output or input encoding may be wxFONTENCODING_UNICODE, but only
  124. if wxUSE_ENCODING is set to 1.
  125. All subsequent calls to Convert() will interpret its argument
  126. as a string in @a input_enc encoding and will output string in
  127. @a output_enc encoding.
  128. You must call this method before calling Convert. You may call
  129. it more than once in order to switch to another conversion.
  130. @a method affects behaviour of Convert() in case input character
  131. cannot be converted because it does not exist in output encoding:
  132. @li @b wxCONVERT_STRICT: follow behaviour of GNU Recode - just copy
  133. unconvertible characters to output and don't change them
  134. (its integer value will stay the same)
  135. @li @b wxCONVERT_SUBSTITUTE: try some (lossy) substitutions - e.g.
  136. replace unconvertible latin capitals with acute by ordinary
  137. capitals, replace en-dash or em-dash by '-' etc.
  138. Both modes guarantee that output string will have same length
  139. as input string.
  140. @return @false if given conversion is impossible, @true otherwise
  141. (conversion may be impossible either if you try to convert
  142. to Unicode with non-Unicode build of wxWidgets or if input
  143. or output encoding is not supported).
  144. */
  145. bool Init(wxFontEncoding input_enc, wxFontEncoding output_enc,
  146. int method = wxCONVERT_STRICT);
  147. };