]> git.lyx.org Git - lyx.git/blob - src/support/unicode.h
Implement native reading of mo files.
[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/strfwd.h"
17
18 #include <cstddef>
19 #include <vector>
20
21
22 namespace lyx {
23
24 class IconvProcessor
25 {
26 public:
27         IconvProcessor(char const * tocode = "", char const * fromcode = "");
28         /// copy constructor needed because of pimpl_
29         IconvProcessor(IconvProcessor const &);
30         /// assignment operator needed because of pimpl_
31         void operator=(IconvProcessor const &);
32         /// destructor
33         ~IconvProcessor();
34
35         /// convert any data from \c fromcode to \c tocode unicode format.
36         /// \return the number of bytes of the converted output buffer.
37         int convert(char const * in_buffer, size_t in_size,
38                 char * out_buffer, size_t max_out_size);
39
40         /// source encoding
41         std::string from() const;
42         /// target encoding
43         std::string to() const;
44
45 private:
46         /// open iconv.
47         /// \return true if the processor is ready to use.
48         bool init();
49         /// hide internals
50         struct Impl;
51         Impl * pimpl_;
52 };
53
54 // A single codepoint conversion for utf8_to_ucs4 does not make
55 // sense, so that function is left out.
56
57 std::vector<char_type> utf8_to_ucs4(std::vector<char> const & utf8str);
58
59 std::vector<char_type> utf8_to_ucs4(char const * utf8str, size_t ls);
60
61 // utf16_to_ucs4
62
63 std::vector<char_type> utf16_to_ucs4(unsigned short const * s, size_t ls);
64
65 // ucs4_to_utf16
66
67 std::vector<unsigned short> ucs4_to_utf16(char_type const * s, size_t ls);
68
69 // ucs4_to_utf8
70
71 std::vector<char> ucs4_to_utf8(char_type c);
72
73 std::vector<char> ucs4_to_utf8(std::vector<char_type> const & ucs4str);
74
75 std::vector<char> ucs4_to_utf8(char_type const * ucs4str, size_t ls);
76
77 /// convert \p s from encoding \p encoding to ucs4.
78 /// \p encoding must be a valid iconv 8bit encoding
79 std::vector<char_type>
80 eightbit_to_ucs4(char const * s, size_t ls, std::string const & encoding);
81
82 /// convert \p s from ucs4 to encoding \p encoding.
83 /// \p encoding must be a valid iconv 8bit encoding
84 std::vector<char> ucs4_to_eightbit(char_type const * ucs4str,
85         size_t ls, std::string const & encoding);
86
87 /// convert ucs4 character \p c to encoding \p encoding.
88 /// \p encoding must be a valid iconv 8bit encoding
89 char ucs4_to_eightbit(char_type c, std::string const & encoding);
90
91 ///
92 void ucs4_to_multibytes(char_type ucs4, std::vector<char> & out,
93         std::string const & encoding);
94
95 extern char const * ucs4_codeset;
96
97 /// How many bytes does one UCS4 code point use at most in encoding \p encoding?
98 int max_encoded_bytes(std::string const & encoding);
99
100 } // namespace lyx
101
102 #endif