From 5c1f562452e201a968063e176b55fc14d912aea1 Mon Sep 17 00:00:00 2001 From: Jaromir Wysoglad Date: Thu, 18 Jul 2019 13:34:22 +0200 Subject: TTS: Implement conversion to UTF-8 in say on linux --- .../text-to-speech/linux/linux-text-to-speech.cpp | 31 +++++++++++++++++++--- .../text-to-speech/linux/linux-text-to-speech.h | 3 ++- .../windows/windows-text-to-speech.cpp | 10 ++++++- .../windows/windows-text-to-speech.h | 2 +- 4 files changed, 39 insertions(+), 7 deletions(-) (limited to 'backends/text-to-speech') diff --git a/backends/text-to-speech/linux/linux-text-to-speech.cpp b/backends/text-to-speech/linux/linux-text-to-speech.cpp index db036e053a..4241b76c7b 100644 --- a/backends/text-to-speech/linux/linux-text-to-speech.cpp +++ b/backends/text-to-speech/linux/linux-text-to-speech.cpp @@ -27,6 +27,7 @@ #if defined(USE_LINUX_TTS) #include +#include "backends/platform/sdl/sdl-sys.h" #include "common/translation.h" #include "common/debug.h" @@ -107,13 +108,35 @@ void LinuxTextToSpeechManager::updateState(LinuxTextToSpeechManager::SpeechState _speechState = state; } -bool LinuxTextToSpeechManager::say(Common::String str) { +Common::String LinuxTextToSpeechManager::strToUtf8(Common::String str, Common::String charset) { +#if SDL_VERSION_ATLEAST(2, 0, 0) + + char *conv_text = SDL_iconv_string("UTF-8", charset.c_str(), str.c_str(), str.size() + 1); + Common::String result; + if (conv_text) { + result = conv_text; + SDL_free(conv_text); + } + + return result; +#else + return Common::String(); +#endif +} + +bool LinuxTextToSpeechManager::say(Common::String str, Common::String charset) { if (_speechState == BROKEN) return true; - //Convert string, that might have foreign characters to UTF-8 - if (ConfMan.get("gui_language") != "C") { - str = Common::convertUtf32ToUtf8(Common::convertToU32String(str.c_str(), Common::kWindows1250)).c_str(); + + if (charset == "") { +#ifdef USE_TRANSLATION + charset = TransMan.getCurrentCharset(); +#else + charset = "ASCII"; +#endif } + str = strToUtf8(str, charset); + if (isSpeaking()) stop(); debug("say: %s", str.c_str()); diff --git a/backends/text-to-speech/linux/linux-text-to-speech.h b/backends/text-to-speech/linux/linux-text-to-speech.h index 57b9aeea60..1bb3ad5ce2 100644 --- a/backends/text-to-speech/linux/linux-text-to-speech.h +++ b/backends/text-to-speech/linux/linux-text-to-speech.h @@ -42,7 +42,7 @@ public: LinuxTextToSpeechManager(); virtual ~LinuxTextToSpeechManager(); - virtual bool say(Common::String str); + virtual bool say(Common::String str, Common::String charset = ""); virtual bool stop(); virtual bool pause(); @@ -74,6 +74,7 @@ private: virtual void updateVoices(); void createVoice(int typeNumber, Common::TTSVoice::Gender, Common::TTSVoice::Age, char *description); SpeechState _speechState; + Common::String strToUtf8(Common::String str, Common::String charset); }; #endif diff --git a/backends/text-to-speech/windows/windows-text-to-speech.cpp b/backends/text-to-speech/windows/windows-text-to-speech.cpp index e9f28d966b..32087f5ccb 100644 --- a/backends/text-to-speech/windows/windows-text-to-speech.cpp +++ b/backends/text-to-speech/windows/windows-text-to-speech.cpp @@ -91,11 +91,19 @@ WindowsTextToSpeechManager::~WindowsTextToSpeechManager() { ::CoUninitialize(); } -bool WindowsTextToSpeechManager::say(Common::String str) { +bool WindowsTextToSpeechManager::say(Common::String str, Common::String charset) { if(_speechState == BROKEN || _speechState == NO_VOICE) { warning("The tts cannot speak in this state"); return true; } + + if (charset == "") { +#ifdef USE_TRANSLATION + charset = TransMan.getCurrentCharset(); +#else + charset = "ASCII"; +#endif + } if (isPaused()) { resume(); } diff --git a/backends/text-to-speech/windows/windows-text-to-speech.h b/backends/text-to-speech/windows/windows-text-to-speech.h index 2ae062ce92..848ae854eb 100644 --- a/backends/text-to-speech/windows/windows-text-to-speech.h +++ b/backends/text-to-speech/windows/windows-text-to-speech.h @@ -43,7 +43,7 @@ public: WindowsTextToSpeechManager(); virtual ~WindowsTextToSpeechManager(); - virtual bool say(Common::String str); + virtual bool say(Common::String str, Common::String charset = ""); virtual bool stop(); virtual bool pause(); -- cgit v1.2.3