3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS.
10 * A collection of unicode conversion functions, using iconv.
19 #include <boost/cstdint.hpp>
33 iconv_convert(std::string const & tocode, std::string const & fromcode,
34 std::vector<char> const & buf)
36 iconv_t cd = iconv_open(tocode.c_str(), fromcode.c_str());
37 if (cd == (iconv_t)(-1)) {
38 lyxerr << "Error returned from iconv_open" << endl;
41 lyxerr << "EINVAL The conversion from " << fromcode
43 << " is not supported by the implementation."
47 lyxerr << "\tSome other error: " << errno << endl;
52 char ICONV_CONST * inbuf = const_cast<char ICONV_CONST *>(&buf[0]);
53 size_t inbytesleft = buf.size();
54 char out[1000] = { 0 };
56 size_t outbytesleft = 1000;
58 size_t res = iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
60 if (res == (size_t)(-1)) {
61 lyxerr << "Error returned from iconv" << endl;
64 lyxerr << "E2BIG There is not sufficient room at *outbuf." << endl;
67 lyxerr << "EILSEQ An invalid multibyte sequence"
68 << " has been encountered in the input.\n"
69 << "When converting from " << fromcode
70 << " to " << tocode << ".\n";
71 lyxerr << "Input: " << std::hex;
72 for (size_t i = 0; i < buf.size(); ++i) {
73 unsigned char const b = buf[i];
74 lyxerr << "0x" << int(b) << " ";
79 lyxerr << "EINVAL An incomplete multibyte sequence"
80 << " has been encountered in the input.\n"
81 << "When converting from " << fromcode
82 << " to " << tocode << ".\n";
83 lyxerr << "Input: " << std::hex;
84 for (size_t i = 0; i < buf.size(); ++i) {
85 unsigned char const b = buf[i];
86 lyxerr << "0x" << int(b) << " ";
91 lyxerr << "\tSome other error: " << errno << endl;
96 if (iconv_close(cd) == -1) {
97 lyxerr << "Error returned from iconv_close("
98 << errno << ")" << endl;
101 //lyxerr << std::dec;
102 //lyxerr << "Inbytesleft: " << inbytesleft << endl;
103 //lyxerr << "Outbytesleft: " << outbytesleft << endl;
104 int bytes = 1000 - outbytesleft;
106 std::vector<char> outvec(out, out + bytes);
111 std::vector<boost::uint32_t> bytes_to_ucs4(std::vector<char> const & bytes)
113 //lyxerr << "Outbuf =" << std::hex;
115 std::vector<boost::uint32_t> ucs4;
116 for (size_t i = 0; i < bytes.size(); i += 4) {
117 unsigned char const b1 = bytes[i ];
118 unsigned char const b2 = bytes[i + 1];
119 unsigned char const b3 = bytes[i + 2];
120 unsigned char const b4 = bytes[i + 3];
123 char * cc = reinterpret_cast<char *>(&c);
130 lyxerr << "Strange ucs4 value encountered\n";
132 << std::setw(2) << std::setfill('0') << int(b1)
133 << std::setw(2) << std::setfill('0') << int(b2)
134 << std::setw(2) << std::setfill('0') << int(b3)
135 << std::setw(2) << std::setfill('0') << int(b4)
149 std::vector<unsigned short> bytes_to_ucs2(std::vector<char> const & bytes)
151 //lyxerr << "Outbuf =" << std::hex;
153 std::vector<unsigned short> ucs2;
154 for (size_t i = 0; i < bytes.size(); i += 2) {
155 unsigned char const b1 = bytes[i ];
156 unsigned char const b2 = bytes[i + 1];
159 char * cc = reinterpret_cast<char *>(&c);
164 // << std::setw(2) << std::setfill('0') << int(b2)
165 // << std::setw(2) << std::setfill('0') << int(b1)
180 std::vector<boost::uint32_t> utf8_to_ucs4(std::vector<char> const & utf8str)
182 //lyxerr << "Buff = " << string(utf8str.begin(), utf8str.end())
183 // << " (" << utf8str.size() << ")" << endl;
184 //lyxerr << "Res = " << string(res.begin(), res.end())
185 // << " (" << res.size() << ")" << endl;
187 std::vector<char> res = iconv_convert("UCS-4", "UTF-8", utf8str);
188 return bytes_to_ucs4(res);
192 std::vector<boost::uint32_t>
193 ucs2_to_ucs4(std::vector<unsigned short> const & ucs2str)
195 // TODO: Simplify and speed up.
196 std::vector<char> in;
197 std::vector<unsigned short>::const_iterator cit = ucs2str.begin();
198 std::vector<unsigned short>::const_iterator end = ucs2str.end();
199 //lyxerr << std::hex;
200 for (; cit != end; ++cit) {
201 unsigned short s = *cit;
202 in.push_back(static_cast<char>(s & 0x00ff));
203 in.push_back(static_cast<char>((s & 0xff00) >> 8));
204 lyxerr << std::setw(2) << std::setfill('0') << (s & 0x00ff) << endl;
205 lyxerr << std::setw(2) << std::setfill('0') << ((s & 0xff00) >> 8) << endl;
208 std::vector<char> res = iconv_convert("UCS-4", "UCS-2", in);
209 return bytes_to_ucs4(res);
213 std::vector<unsigned short>
214 ucs4_to_ucs2(std::vector<boost::uint32_t> const & ucs4str)
216 std::vector<char> in;
217 std::vector<boost::uint32_t>::const_iterator cit = ucs4str.begin();
218 std::vector<boost::uint32_t>::const_iterator end = ucs4str.end();
219 for (; cit != end; ++cit) {
220 boost::uint32_t s = *cit;
221 in.push_back(static_cast<char>((s & 0xff000000) >> 24));
222 in.push_back(static_cast<char>((s & 0x00ff0000) >> 16));
223 in.push_back(static_cast<char>((s & 0x0000ff00) >> 8));
224 in.push_back(static_cast<char>(s & 0x000000ff));
226 std::vector<char> res = iconv_convert("UCS-2", "UCS-4", in);
227 return bytes_to_ucs2(res);
231 std::vector<unsigned short>
232 ucs4_to_ucs2(boost::uint32_t const * s, size_t ls)
234 std::vector<char> in;
235 for (size_t i = 0; i < ls; ++i) {
236 in.push_back(static_cast<char>((s[i] & 0xff000000) >> 24));
237 in.push_back(static_cast<char>((s[i] & 0x00ff0000) >> 16));
238 in.push_back(static_cast<char>((s[i] & 0x0000ff00) >> 8));
239 in.push_back(static_cast<char>(s[i] & 0x000000ff));
241 std::vector<char> res = iconv_convert("UCS-2", "UCS-4", in);
242 return bytes_to_ucs2(res);
247 ucs4_to_ucs2(boost::uint32_t c)
249 std::vector<char> in;
250 in.push_back(static_cast<char>((c & 0xff000000) >> 24));
251 in.push_back(static_cast<char>((c & 0x00ff0000) >> 16));
252 in.push_back(static_cast<char>((c & 0x0000ff00) >> 8));
253 in.push_back(static_cast<char>(c & 0x000000ff));
254 std::vector<char> res = iconv_convert("UCS-2", "UCS-4", in);
255 std::vector<unsigned short> us = bytes_to_ucs2(res);
259 return 0xfffd; // unknown character
263 std::vector<char> ucs4_to_utf8(std::vector<boost::uint32_t> const & ucs4str)
265 std::vector<char> in;
266 std::vector<boost::uint32_t>::const_iterator cit = ucs4str.begin();
267 std::vector<boost::uint32_t>::const_iterator end = ucs4str.end();
268 for (; cit != end; ++cit) {
269 boost::uint32_t s = *cit;
270 in.push_back(static_cast<char>((s & 0xff000000) >> 24));
271 in.push_back(static_cast<char>((s & 0x00ff0000) >> 16));
272 in.push_back(static_cast<char>((s & 0x0000ff00) >> 8));
273 in.push_back(static_cast<char>(s & 0x000000ff));
275 std::vector<char> res = iconv_convert("UTF-8", "UCS-4", in);
280 std::vector<char> ucs4_to_utf8(boost::uint32_t c)
282 std::vector<char> in;
283 in.push_back(static_cast<char>((c & 0xff000000) >> 24));
284 in.push_back(static_cast<char>((c & 0x00ff0000) >> 16));
285 in.push_back(static_cast<char>((c & 0x0000ff00) >> 8));
286 in.push_back(static_cast<char>(c & 0x000000ff));
287 std::vector<char> res = iconv_convert("UTF-8", "UCS-4", in);