Add to_upper() method to util.{cc,h}
[dcpomatic.git] / src / lib / util.cc
index 67b6599ea44459dd78d32ecd0dbdd7fe427fb415..d3511e8c7e34e0b1c5f5744147b18d83fb944837 100644 (file)
@@ -62,6 +62,9 @@ extern "C" {
 #include <curl/curl.h>
 #include <glib.h>
 #include <pangomm/init.h>
+#include <unicode/utypes.h>
+#include <unicode/unistr.h>
+#include <unicode/translit.h>
 #include <boost/algorithm/string.hpp>
 #include <boost/range/algorithm/replace_if.hpp>
 #include <boost/thread.hpp>
@@ -613,7 +616,7 @@ valid_image_file (boost::filesystem::path f)
                ext == ".tif" || ext == ".tiff" || ext == ".jpg" || ext == ".jpeg" ||
                ext == ".png" || ext == ".bmp" || ext == ".tga" || ext == ".dpx" ||
                ext == ".j2c" || ext == ".j2k" || ext == ".jp2" || ext == ".exr" ||
-               ext == ".jpf"
+               ext == ".jpf" || ext == ".psd"
                );
 }
 
@@ -771,28 +774,20 @@ careful_string_filter (string s)
           Safety first and all that.
        */
 
-       wstring ws = boost::locale::conv::utf_to_utf<wchar_t>(s);
+       /* First transliterate using libicu to try to remove accents in a "nice" way */
+       auto icu_utf16 = icu::UnicodeString::fromUTF8(icu::StringPiece(s));
+       auto status = U_ZERO_ERROR;
+       auto transliterator = icu::Transliterator::createInstance("NFD; [:M:] Remove; NFC", UTRANS_FORWARD, status);
+       transliterator->transliterate(icu_utf16);
+       s.clear ();
+       icu_utf16.toUTF8String(s);
 
+       /* Then remove anything that's not in a very limited character set */
+       wstring ws = boost::locale::conv::utf_to_utf<wchar_t>(s);
        string out;
        string const allowed = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_%.+";
        for (size_t i = 0; i < ws.size(); ++i) {
-
                wchar_t c = ws[i];
-
-               /* Remove some accents */
-               if (wstring(L"áàâ").find(c) != string::npos) {
-                       c = 'a';
-               }
-               if (wstring(L"éèêë").find(c) != string::npos) {
-                       c = 'e';
-               }
-               if (wstring(L"ö").find(c) != string::npos) {
-                       c = 'o';
-               }
-               if (wstring(L"ü").find(c) != string::npos) {
-                       c = 'u';
-               }
-
                if (allowed.find(c) != string::npos) {
                        out += c;
                }
@@ -870,11 +865,11 @@ remap (shared_ptr<const AudioBuffers> input, int output_channels, AudioMapping m
 Eyes
 increment_eyes (Eyes e)
 {
-       if (e == EYES_LEFT) {
-               return EYES_RIGHT;
+       if (e == Eyes::LEFT) {
+               return Eyes::RIGHT;
        }
 
-       return EYES_LEFT;
+       return Eyes::LEFT;
 }
 
 void
@@ -1172,3 +1167,12 @@ default_font_file ()
 
        return liberation_normal;
 }
+
+
+string
+to_upper (string s)
+{
+       transform (s.begin(), s.end(), s.begin(), ::toupper);
+       return s;
+}
+