CENTRE_OF_SCREEN -> VERTICAL_CENTRE_OF_SCREEN.
[libsub.git] / src / iso6937.cc
1 /*
2     Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "iso6937_tables.h"
21 #include "iso6937.h"
22 #include <boost/optional.hpp>
23 #include <boost/locale.hpp>
24 #include <string>
25 #include <iostream>
26
27 using std::string;
28 using std::cout;
29 using std::wcout;
30 using std::wstring;
31 using std::map;
32 using boost::optional;
33 using boost::locale::conv::utf_to_utf;
34 using namespace sub;
35
36 wstring
37 sub::iso6937_to_utf16 (string s)
38 {
39         if (iso6937::diacriticals.empty ()) {
40                 make_iso6937_tables ();
41         }
42
43         wstring o;
44
45         boost::optional<unsigned char> diacritical;
46
47         int i = 0;
48         while (s[i] != '\0') {
49                 unsigned char const u = static_cast<unsigned char> (s[i]);
50                 if (u >= 0xc1 && u <= 0xcf) {
51                         diacritical = u;
52                 } else if (diacritical) {
53                         o += (*iso6937::diacriticals[diacritical.get()])[u];
54                         diacritical.reset ();
55                 } else {
56                         o += iso6937::main[u];
57                 }
58
59                 ++i;
60         }
61
62         return o;
63 }
64
65 static optional<char>
66 find (map<char, wchar_t> const & m, wchar_t c)
67 {
68         for (map<char, wchar_t>::const_iterator i = m.begin(); i != m.end(); ++i) {
69                 if (i->second == c) {
70                         return i->first;
71                 }
72         }
73
74         return optional<char> ();
75 }
76
77 string
78 sub::utf16_to_iso6937 (wstring s)
79 {
80         if (iso6937::diacriticals.empty ()) {
81                 make_iso6937_tables ();
82         }
83
84         /* XXX: slow */
85
86         string o;
87         for (size_t i = 0; i < s.size(); ++i) {
88                 optional<char> c = find (iso6937::main, s[i]);
89                 if (c) {
90                         o += c.get ();
91                 } else {
92                         for (map<char, map<char, wchar_t> *>::const_iterator j = iso6937::diacriticals.begin(); j != iso6937::diacriticals.end(); ++j) {
93                                 c = find (*(j->second), s[i]);
94                                 if (c) {
95                                         o += j->first;
96                                         o += c.get ();
97                                         break;
98                                 }
99                         }
100                 }
101
102                 if (s[i] == 0x201e) {
103                         /* ISO6397 does not support German (lower) quotation mark (UTF 0x201e) so use
104                            a normal opening one (0x201c, which is 170 in ISO6937).
105                         */
106                         o += (char) 170;
107                 } else if (s[i] == 0x2013 || s[i] == 0x2014) {
108                         /* ISO6397 does not support en- or em-dashes, so use a horizontal bar (0x2015,
109                            which is 208 in ISO6937).
110                         */
111                         o += (char) 208;
112                 } else if (s[i] == 0x2010 || s[i] == 0x2011 || s[i] == 0x2012) {
113                         /* Similar story with hyphen, non-breaking hyphen, figure dash */
114                         o += '-';
115                 }
116
117         }
118
119         return o;
120 }