Shape text using Pango and HarfBuzz; gives us nice ligatures and exotic scripts.
[ccbs] / bigscreen / widestring.cpp
1 #include <stdlib.h>
2 #include <cstring>
3 #include <iconv.h>
4 #include <endian.h>
5 #include <exception>
6 #include <pqxx/util>
7 #include "widestring.h"
8
9 static iconv_t ucs4_iconv, ucs4_reverse_iconv;
10 static bool iconv_initialized = false;
11
12 void ensure_iconv_initialized()
13 {
14         if (iconv_initialized) {
15                 return;
16         }
17 #if __BYTE_ORDER == __LITTLE_ENDIAN
18         ucs4_iconv = iconv_open("ucs-4le", "utf-8");
19         ucs4_reverse_iconv = iconv_open("utf-8", "ucs-4le");
20 #else
21         ucs4_iconv = iconv_open("ucs-4be", "utf-8");
22         ucs4_reverse_iconv = iconv_open("utf-8", "ucs-4be");
23 #endif
24
25         iconv_initialized = true;
26 }
27
28 widestring::widestring()
29 {
30 }
31
32 widestring::widestring(const char *from)
33 {
34         *this = from;
35 }
36
37 widestring::widestring(const std::string &from)
38 {
39         *this = from.c_str();
40 }
41
42 widestring::widestring(const std::wstring &from)
43         : std::wstring(from)
44 {
45 }
46
47 void widestring::operator= (const char *from)
48 {
49         ensure_iconv_initialized();
50         
51         unsigned bytes = std::strlen(from);
52         char *from_buf = strdup(from);
53         wchar_t *to_buf = new wchar_t[bytes + 1];
54
55         char *inptr = from_buf, *outptr = reinterpret_cast<char *> (to_buf);
56
57         size_t in_left = bytes;
58         size_t out_left = bytes * sizeof(wchar_t);
59
60         size_t ret = iconv(ucs4_iconv, NULL, NULL, &outptr, &out_left);
61         if (ret == (size_t)(-1)) {
62                 throw std::runtime_error("Error in iconv during initialization");
63         }
64
65         ret = iconv(ucs4_iconv, &inptr, &in_left, &outptr, &out_left);
66         if (ret == (size_t)(-1)) {
67                 perror("iconv");
68                 throw std::runtime_error("Error in iconv during conversion");
69         }
70
71         erase(begin(), end());
72         std::copy(to_buf, reinterpret_cast<wchar_t *> (outptr), std::back_inserter(*this));
73
74         free(from_buf);
75         delete[] to_buf;
76 }
77
78 std::string widestring::to_utf8() const
79 {
80         ensure_iconv_initialized();
81
82         wchar_t *from_buf = wcsdup(c_str());
83         size_t out_max_size = size() * 6;
84         char *to_buf = new char[out_max_size];
85
86         char *inptr = reinterpret_cast<char *>(from_buf), *outptr = to_buf;
87
88         size_t in_left = size() * sizeof(wchar_t);
89         size_t out_left = out_max_size;
90
91         size_t ret = iconv(ucs4_reverse_iconv, NULL, NULL, &outptr, &out_left);
92         if (ret == (size_t)(-1)) {
93                 throw std::runtime_error("Error in iconv during initialization");
94         }
95
96         ret = iconv(ucs4_reverse_iconv, &inptr, &in_left, &outptr, &out_left);
97         if (ret == (size_t)(-1)) {
98                 perror("iconv");
99                 throw std::runtime_error("Error in iconv during conversion");
100         }
101
102         std::string utf8(to_buf, outptr);
103
104         free(from_buf);
105         delete[] to_buf;
106
107         return utf8;
108 }