]> git.lyx.org Git - lyx.git/blob - src/support/unicode.h
Fix bug 3904
[lyx.git] / src / support / unicode.h
1 /**
2  * \file unicode.h
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  *
10  * A collection of unicode conversion functions, using iconv.
11  */
12
13 #ifndef LYX_SUPPORT_UNICODE_H
14 #define LYX_SUPPORT_UNICODE_H
15
16 #include "support/types.h"
17
18 #include <boost/scoped_ptr.hpp>
19
20 #include <string>
21 #include <vector>
22
23
24 namespace lyx {
25
26 class IconvProcessor
27 {
28 public:
29         IconvProcessor(
30                 char const * tocode = "",
31                 char const * fromcode = "");
32         /// copy constructor needed because of pimpl_
33         IconvProcessor(IconvProcessor const &);
34         /// assignment operator needed because of pimpl_
35         IconvProcessor & operator=(IconvProcessor const &);
36         /// destructor (needs to be implemented in the .C file because the
37         /// boost::scoped_ptr destructor needs a fully defined type
38         ~IconvProcessor();
39
40         /// convert any data from \c fromcode to \c tocode unicode format.
41         /// \return the number of bytes of the converted output buffer.
42         int convert(
43                 char const * in_buffer,
44                 size_t in_size,
45                 char * out_buffer,
46                 size_t max_out_size);
47 private:
48         /// open iconv.
49         /// \return true if the processor is ready to use.
50         bool init();
51
52         std::string tocode_;
53         std::string fromcode_;
54
55         struct Private;
56         boost::scoped_ptr<Private> pimpl_;
57 };
58
59 // A single codepoint conversion for utf8_to_ucs4 does not make
60 // sense, so that function is left out.
61
62 std::vector<char_type> utf8_to_ucs4(std::vector<char> const & utf8str);
63
64 std::vector<char_type> utf8_to_ucs4(char const * utf8str, size_t ls);
65
66 // utf16_to_ucs4
67
68 std::vector<char_type> utf16_to_ucs4(unsigned short const * s, size_t ls);
69
70 // ucs4_to_utf16
71
72 std::vector<unsigned short> ucs4_to_utf16(char_type const * s, size_t ls);
73
74 // ucs4_to_utf8
75
76 std::vector<char> ucs4_to_utf8(char_type c);
77
78 std::vector<char> ucs4_to_utf8(std::vector<char_type> const & ucs4str);
79
80 std::vector<char> ucs4_to_utf8(char_type const * ucs4str, size_t ls);
81
82 /// convert \p s from encoding \p encoding to ucs4.
83 /// \p encoding must be a valid iconv 8bit encoding
84 std::vector<char_type>
85 eightbit_to_ucs4(char const * s, size_t ls, std::string const & encoding);
86
87 /// convert \p s from ucs4 to encoding \p encoding.
88 /// \p encoding must be a valid iconv 8bit encoding
89 std::vector<char>
90 ucs4_to_eightbit(char_type const * ucs4str, size_t ls, std::string const & encoding);
91
92 /// convert ucs4 character \p c to encoding \p encoding.
93 /// \p encoding must be a valid iconv 8bit encoding
94 char ucs4_to_eightbit(char_type c, std::string const & encoding);
95
96 ///
97 void ucs4_to_multibytes(char_type ucs4, std::vector<char> & out,
98         std::string const & encoding);
99
100 extern char const * ucs4_codeset;
101
102
103 } // namespace lyx
104
105 #endif