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.
31 iconv_convert(std::string const & tocode, std::string const & fromcode,
32 std::vector<char> const & buf)
34 iconv_t cd = iconv_open(tocode.c_str(), fromcode.c_str());
35 if (cd == (iconv_t)(-1)) {
36 lyxerr << "Error returned from iconv_open" << endl;
39 lyxerr << "EINVAL The conversion from " << fromcode
41 << " is not supported by the implementation."
45 lyxerr << "\tSome other error: " << errno << endl;
50 char ICONV_CONST * inbuf = const_cast<char ICONV_CONST *>(&buf[0]);
51 size_t inbytesleft = buf.size();
52 char out[1000] = { 0 };
54 size_t outbytesleft = 1000;
56 size_t res = iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
58 if (res == (size_t)(-1)) {
59 lyxerr << "Error returned from iconv" << endl;
62 lyxerr << "E2BIG There is not sufficient room at *outbuf." << endl;
65 lyxerr << "EILSEQ An invalid multibyte sequence"
66 << " has been encountered in the input.\n"
67 << "When converting from " << fromcode
68 << " to " << tocode << ".\n";
69 lyxerr << "Input: " << std::hex;
70 for (size_t i = 0; i < buf.size(); ++i) {
71 unsigned char const b = buf[i];
72 lyxerr << "0x" << int(b) << " ";
77 lyxerr << "EINVAL An incomplete multibyte sequence"
78 << " has been encountered in the input.\n"
79 << "When converting from " << fromcode
80 << " to " << tocode << ".\n";
81 lyxerr << "Input: " << std::hex;
82 for (size_t i = 0; i < buf.size(); ++i) {
83 unsigned char const b = buf[i];
84 lyxerr << "0x" << int(b) << " ";
89 lyxerr << "\tSome other error: " << errno << endl;
94 if (iconv_close(cd) == -1) {
95 lyxerr << "Error returned from iconv_close("
96 << errno << ")" << endl;
100 //lyxerr << "Inbytesleft: " << inbytesleft << endl;
101 //lyxerr << "Outbytesleft: " << outbytesleft << endl;
102 int bytes = 1000 - outbytesleft;
104 std::vector<char> outvec(out, out + bytes);
109 std::vector<boost::uint32_t> bytes_to_ucs4(std::vector<char> const & bytes)
111 //lyxerr << "Outbuf =" << std::hex;
113 std::vector<boost::uint32_t> ucs4;
114 for (size_t i = 0; i < bytes.size(); i += 4) {
115 unsigned char const b1 = bytes[i ];
116 unsigned char const b2 = bytes[i + 1];
117 unsigned char const b3 = bytes[i + 2];
118 unsigned char const b4 = bytes[i + 3];
121 char * cc = reinterpret_cast<char *>(&c);
128 lyxerr << "Strange ucs4 value encountered\n";
130 << std::setw(2) << std::setfill('0') << int(b1)
131 << std::setw(2) << std::setfill('0') << int(b2)
132 << std::setw(2) << std::setfill('0') << int(b3)
133 << std::setw(2) << std::setfill('0') << int(b4)
147 std::vector<unsigned short> bytes_to_ucs2(std::vector<char> const & bytes)
149 //lyxerr << "Outbuf =" << std::hex;
151 std::vector<unsigned short> ucs2;
152 for (size_t i = 0; i < bytes.size(); i += 2) {
153 unsigned char const b1 = bytes[i ];
154 unsigned char const b2 = bytes[i + 1];
157 char * cc = reinterpret_cast<char *>(&c);
162 // << std::setw(2) << std::setfill('0') << int(b2)
163 // << std::setw(2) << std::setfill('0') << int(b1)
178 std::vector<boost::uint32_t> utf8_to_ucs4(std::vector<char> const & utf8str)
180 //lyxerr << "Buff = " << string(utf8str.begin(), utf8str.end())
181 // << " (" << utf8str.size() << ")" << endl;
182 //lyxerr << "Res = " << string(res.begin(), res.end())
183 // << " (" << res.size() << ")" << endl;
185 std::vector<char> res = iconv_convert("UCS-4", "UTF-8", utf8str);
186 return bytes_to_ucs4(res);
190 std::vector<boost::uint32_t>
191 ucs2_to_ucs4(std::vector<unsigned short> const & ucs2str)
193 // TODO: Simplify and speed up.
194 std::vector<char> in;
195 std::vector<unsigned short>::const_iterator cit = ucs2str.begin();
196 std::vector<unsigned short>::const_iterator end = ucs2str.end();
197 //lyxerr << std::hex;
198 for (; cit != end; ++cit) {
199 unsigned short s = *cit;
200 in.push_back(static_cast<char>(s & 0x00ff));
201 in.push_back(static_cast<char>((s & 0xff00) >> 8));
202 lyxerr << std::setw(2) << std::setfill('0') << (s & 0x00ff) << endl;
203 lyxerr << std::setw(2) << std::setfill('0') << ((s & 0xff00) >> 8) << endl;
206 std::vector<char> res = iconv_convert("UCS-4", "UCS-2", in);
207 return bytes_to_ucs4(res);
211 std::vector<unsigned short>
212 ucs4_to_ucs2(std::vector<boost::uint32_t> const & ucs4str)
214 std::vector<char> in;
215 std::vector<boost::uint32_t>::const_iterator cit = ucs4str.begin();
216 std::vector<boost::uint32_t>::const_iterator end = ucs4str.end();
217 for (; cit != end; ++cit) {
218 boost::uint32_t s = *cit;
219 in.push_back(static_cast<char>((s & 0xff000000) >> 24));
220 in.push_back(static_cast<char>((s & 0x00ff0000) >> 16));
221 in.push_back(static_cast<char>((s & 0x0000ff00) >> 8));
222 in.push_back(static_cast<char>(s & 0x000000ff));
224 std::vector<char> res = iconv_convert("UCS-2", "UCS-4", in);
225 return bytes_to_ucs2(res);
229 std::vector<unsigned short>
230 ucs4_to_ucs2(boost::uint32_t const * s, size_t ls)
232 std::vector<char> in;
233 for (size_t i = 0; i < ls; ++i) {
234 in.push_back(static_cast<char>((s[i] & 0xff000000) >> 24));
235 in.push_back(static_cast<char>((s[i] & 0x00ff0000) >> 16));
236 in.push_back(static_cast<char>((s[i] & 0x0000ff00) >> 8));
237 in.push_back(static_cast<char>(s[i] & 0x000000ff));
239 std::vector<char> res = iconv_convert("UCS-2", "UCS-4", in);
240 return bytes_to_ucs2(res);
245 ucs4_to_ucs2(boost::uint32_t c)
247 std::vector<char> in;
248 in.push_back(static_cast<char>((c & 0xff000000) >> 24));
249 in.push_back(static_cast<char>((c & 0x00ff0000) >> 16));
250 in.push_back(static_cast<char>((c & 0x0000ff00) >> 8));
251 in.push_back(static_cast<char>(c & 0x000000ff));
252 std::vector<char> res = iconv_convert("UCS-2", "UCS-4", in);
253 std::vector<unsigned short> us = bytes_to_ucs2(res);
257 return 0xfffd; // unknown character
261 std::vector<char> ucs4_to_utf8(std::vector<boost::uint32_t> const & ucs4str)
263 std::vector<char> in;
264 std::vector<boost::uint32_t>::const_iterator cit = ucs4str.begin();
265 std::vector<boost::uint32_t>::const_iterator end = ucs4str.end();
266 for (; cit != end; ++cit) {
267 boost::uint32_t s = *cit;
268 in.push_back(static_cast<char>((s & 0xff000000) >> 24));
269 in.push_back(static_cast<char>((s & 0x00ff0000) >> 16));
270 in.push_back(static_cast<char>((s & 0x0000ff00) >> 8));
271 in.push_back(static_cast<char>(s & 0x000000ff));
273 std::vector<char> res = iconv_convert("UTF-8", "UCS-4", in);
278 std::vector<char> ucs4_to_utf8(boost::uint32_t c)
280 std::vector<char> in;
281 in.push_back(static_cast<char>((c & 0xff000000) >> 24));
282 in.push_back(static_cast<char>((c & 0x00ff0000) >> 16));
283 in.push_back(static_cast<char>((c & 0x0000ff00) >> 8));
284 in.push_back(static_cast<char>(c & 0x000000ff));
285 std::vector<char> res = iconv_convert("UTF-8", "UCS-4", in);