encconv.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/encconv.h
  3. // Purpose: wxEncodingConverter class for converting between different
  4. // font encodings
  5. // Author: Vaclav Slavik
  6. // Copyright: (c) 1999 Vaclav Slavik
  7. // Licence: wxWindows licence
  8. /////////////////////////////////////////////////////////////////////////////
  9. #ifndef _WX_ENCCONV_H_
  10. #define _WX_ENCCONV_H_
  11. #include "wx/defs.h"
  12. #include "wx/object.h"
  13. #include "wx/fontenc.h"
  14. #include "wx/dynarray.h"
  15. // ----------------------------------------------------------------------------
  16. // constants
  17. // ----------------------------------------------------------------------------
  18. enum
  19. {
  20. wxCONVERT_STRICT,
  21. wxCONVERT_SUBSTITUTE
  22. };
  23. enum
  24. {
  25. wxPLATFORM_CURRENT = -1,
  26. wxPLATFORM_UNIX = 0,
  27. wxPLATFORM_WINDOWS,
  28. wxPLATFORM_OS2,
  29. wxPLATFORM_MAC
  30. };
  31. // ----------------------------------------------------------------------------
  32. // types
  33. // ----------------------------------------------------------------------------
  34. WX_DEFINE_ARRAY_INT(wxFontEncoding, wxFontEncodingArray);
  35. //--------------------------------------------------------------------------------
  36. // wxEncodingConverter
  37. // This class is capable of converting strings between any two
  38. // 8bit encodings/charsets. It can also convert from/to Unicode
  39. //--------------------------------------------------------------------------------
  40. class WXDLLIMPEXP_BASE wxEncodingConverter : public wxObject
  41. {
  42. public:
  43. wxEncodingConverter();
  44. virtual ~wxEncodingConverter() { if (m_Table) delete[] m_Table; }
  45. // Initialize conversion. Both output or input encoding may
  46. // be wxFONTENCODING_UNICODE.
  47. //
  48. // All subsequent calls to Convert() will interpret it's argument
  49. // as a string in input_enc encoding and will output string in
  50. // output_enc encoding.
  51. //
  52. // You must call this method before calling Convert. You may call
  53. // it more than once in order to switch to another conversion
  54. //
  55. // Method affects behaviour of Convert() in case input character
  56. // cannot be converted because it does not exist in output encoding:
  57. // wxCONVERT_STRICT --
  58. // follow behaviour of GNU Recode - just copy unconvertable
  59. // characters to output and don't change them (it's integer
  60. // value will stay the same)
  61. // wxCONVERT_SUBSTITUTE --
  62. // try some (lossy) substitutions - e.g. replace
  63. // unconvertable latin capitals with acute by ordinary
  64. // capitals, replace en-dash or em-dash by '-' etc.
  65. // both modes guarantee that output string will have same length
  66. // as input string
  67. //
  68. // Returns false if given conversion is impossible, true otherwise
  69. // (conversion may be impossible either if you try to convert
  70. // to Unicode with non-Unicode build of wxWidgets or if input
  71. // or output encoding is not supported.)
  72. bool Init(wxFontEncoding input_enc, wxFontEncoding output_enc, int method = wxCONVERT_STRICT);
  73. // Convert input string according to settings passed to Init.
  74. // Note that you must call Init before using Convert!
  75. bool Convert(const char* input, char* output) const;
  76. bool Convert(char* str) const { return Convert(str, str); }
  77. wxString Convert(const wxString& input) const;
  78. bool Convert(const char* input, wchar_t* output) const;
  79. bool Convert(const wchar_t* input, char* output) const;
  80. bool Convert(const wchar_t* input, wchar_t* output) const;
  81. bool Convert(wchar_t* str) const { return Convert(str, str); }
  82. // Return equivalent(s) for given font that are used
  83. // under given platform. wxPLATFORM_CURRENT means the plaform
  84. // this binary was compiled for
  85. //
  86. // Examples:
  87. // current platform enc returned value
  88. // -----------------------------------------------------
  89. // unix CP1250 {ISO8859_2}
  90. // unix ISO8859_2 {}
  91. // windows ISO8859_2 {CP1250}
  92. //
  93. // Equivalence is defined in terms of convertibility:
  94. // 2 encodings are equivalent if you can convert text between
  95. // then without losing information (it may - and will - happen
  96. // that you lose special chars like quotation marks or em-dashes
  97. // but you shouldn't lose any diacritics and language-specific
  98. // characters when converting between equivalent encodings).
  99. //
  100. // Convert() method is not limited to converting between
  101. // equivalent encodings, it can convert between arbitrary
  102. // two encodings!
  103. //
  104. // Remember that this function does _NOT_ check for presence of
  105. // fonts in system. It only tells you what are most suitable
  106. // encodings. (It usually returns only one encoding)
  107. //
  108. // Note that argument enc itself may be present in returned array!
  109. // (so that you can -- as a side effect -- detect whether the
  110. // encoding is native for this platform or not)
  111. static wxFontEncodingArray GetPlatformEquivalents(wxFontEncoding enc, int platform = wxPLATFORM_CURRENT);
  112. // Similar to GetPlatformEquivalent, but this one will return ALL
  113. // equivalent encodings, regardless the platform, including itself.
  114. static wxFontEncodingArray GetAllEquivalents(wxFontEncoding enc);
  115. // Return true if [any text in] one multibyte encoding can be
  116. // converted to another one losslessly.
  117. //
  118. // Do not call this with wxFONTENCODING_UNICODE, it doesn't make
  119. // sense (always works in one sense and always depends on the text
  120. // to convert in the other)
  121. static bool CanConvert(wxFontEncoding encIn, wxFontEncoding encOut)
  122. {
  123. return GetAllEquivalents(encIn).Index(encOut) != wxNOT_FOUND;
  124. }
  125. private:
  126. wchar_t *m_Table;
  127. bool m_UnicodeInput, m_UnicodeOutput;
  128. bool m_JustCopy;
  129. wxDECLARE_NO_COPY_CLASS(wxEncodingConverter);
  130. };
  131. #endif // _WX_ENCCONV_H_