mirror of
https://github.com/SoftFever/OrcaSlicer.git
synced 2025-07-11 16:57:53 -06:00
Unicode handling:
Removed the Perl dependencies on Encode, Encode::Locale and Unicode::Normalize. Added dependency on boost::locale. Added encode_path, decode_path, normalize_utf8 functions to Slic3r.xs Slic3r.xs has been made mostly utf8 safe by using the boost::nowide library, thanks to @alexrj for the idea. Simplified the encode_path / decode_path stuff: wxWidgets are unicode already, so there is no need to decode_path() from it. Perl / win32 interfacing is non-unicode, so decode_path() is executed on ARGV just at the beginning of the perl scripts.
This commit is contained in:
parent
31085fb1d7
commit
1385018724
33 changed files with 236 additions and 186 deletions
|
@ -1,7 +1,21 @@
|
|||
#include <locale>
|
||||
|
||||
#include <boost/log/core.hpp>
|
||||
#include <boost/log/trivial.hpp>
|
||||
#include <boost/log/expressions.hpp>
|
||||
|
||||
#include <boost/locale.hpp>
|
||||
|
||||
#include <boost/nowide/integration/filesystem.hpp>
|
||||
#include <boost/nowide/convert.hpp>
|
||||
|
||||
#ifdef WIN32
|
||||
extern "C" {
|
||||
__declspec(dllimport) int WideCharToMultiByte(unsigned int, unsigned long, wchar_t const *, int, char *, int, char const *, int *);
|
||||
__declspec(dllimport) int MultiByteToWideChar(unsigned int, unsigned long, char const *, int, wchar_t *, int);
|
||||
}
|
||||
#endif /* WIN32 */
|
||||
|
||||
namespace Slic3r {
|
||||
|
||||
static boost::log::trivial::severity_level logSeverity = boost::log::trivial::error;
|
||||
|
@ -30,9 +44,13 @@ void set_logging_level(unsigned int level)
|
|||
}
|
||||
|
||||
// Force set_logging_level(<=error) after loading of the DLL.
|
||||
static struct SetLoggingLevelOnInit {
|
||||
SetLoggingLevelOnInit() { set_logging_level(1); }
|
||||
} g_SetLoggingLevelOnInit;
|
||||
// Switch boost::filesystem to utf8.
|
||||
static struct RunOnInit {
|
||||
RunOnInit() {
|
||||
boost::nowide::nowide_filesystem();
|
||||
set_logging_level(1);
|
||||
}
|
||||
} g_RunOnInit;
|
||||
|
||||
void trace(unsigned int level, const char *message)
|
||||
{
|
||||
|
@ -56,6 +74,46 @@ void trace(unsigned int level, const char *message)
|
|||
(::boost::log::keywords::severity = severity)) << message;
|
||||
}
|
||||
|
||||
std::string encode_path(const char *src)
|
||||
{
|
||||
#ifdef WIN32
|
||||
// Convert the source utf8 encoded string to a wide string.
|
||||
std::wstring wstr_src = boost::nowide::widen(src);
|
||||
if (wstr_src.length() == 0)
|
||||
return std::string();
|
||||
// Convert a wide string to a local code page.
|
||||
int size_needed = ::WideCharToMultiByte(0, 0, wstr_src.data(), (int)wstr_src.size(), nullptr, 0, nullptr, nullptr);
|
||||
std::string str_dst(size_needed, 0);
|
||||
::WideCharToMultiByte(0, 0, wstr_src.data(), (int)wstr_src.size(), const_cast<char*>(str_dst.data()), size_needed, nullptr, nullptr);
|
||||
return str_dst;
|
||||
#else /* WIN32 */
|
||||
return src;
|
||||
#endif /* WIN32 */
|
||||
}
|
||||
|
||||
std::string decode_path(const char *src)
|
||||
{
|
||||
#ifdef WIN32
|
||||
int len = strlen(src);
|
||||
if (len == 0)
|
||||
return std::string();
|
||||
// Convert the string encoded using the local code page to a wide string.
|
||||
int size_needed = ::MultiByteToWideChar(0, 0, src, len, nullptr, 0);
|
||||
std::wstring wstr_dst(size_needed, 0);
|
||||
::MultiByteToWideChar(0, 0, src, len, const_cast<wchar_t*>(wstr_dst.data()), size_needed);
|
||||
// Convert a wide string to utf8.
|
||||
return boost::nowide::narrow(wstr_dst.c_str());
|
||||
#else /* WIN32 */
|
||||
return src;
|
||||
#endif /* WIN32 */
|
||||
}
|
||||
|
||||
std::string normalize_utf8_nfc(const char *src)
|
||||
{
|
||||
static std::locale locale_utf8("en_US.UTF-8");
|
||||
return boost::locale::normalize(src, boost::locale::norm_nfc, locale_utf8);
|
||||
}
|
||||
|
||||
} // namespace Slic3r
|
||||
|
||||
#ifdef SLIC3R_HAS_BROKEN_CROAK
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue