Add locale api to text abstraction 56/319856/2
authorBowon Ryu <bowon.ryu@samsung.com>
Wed, 19 Feb 2025 02:28:46 +0000 (11:28 +0900)
committerBowon Ryu <bowon.ryu@samsung.com>
Wed, 19 Feb 2025 04:18:58 +0000 (13:18 +0900)
Prevents the setlocale system call from being called repeatedly.

Change-Id: Ibd00d7e0f8145a4a6aa305a3ba4997c351acbd18
Signed-off-by: Bowon Ryu <bowon.ryu@samsung.com>
dali/devel-api/text-abstraction/font-client.cpp
dali/devel-api/text-abstraction/font-client.h
dali/internal/adaptor/common/adaptor-impl.cpp
dali/internal/adaptor/tizen-wayland/adaptor-impl-tizen.cpp
dali/internal/text/text-abstraction/font-client-impl.cpp
dali/internal/text/text-abstraction/font-client-impl.h
dali/internal/text/text-abstraction/shaping-impl.cpp

index 95845cd969115b01e8b3dbb6e4a36e8039981e1d..115a0108cbd8fd66b8d9799a2cfc695ae612d424 100644 (file)
@@ -368,6 +368,26 @@ void FontClientJoinFontThreads()
   Internal::FontClient::JoinFontThreads();
 }
 
+void EnsureLocale()
+{
+  Internal::FontClient::EnsureLocale();
+}
+
+const std::string& GetLocale()
+{
+  return Internal::FontClient::GetLocale();
+}
+
+const std::string& GetLocaleFull()
+{
+  return Internal::FontClient::GetLocaleFull();
+}
+
+void SetLocale(const std::string& locale)
+{
+  Internal::FontClient::SetLocale(locale);
+}
+
 } // namespace TextAbstraction
 
 } // namespace Dali
index 4ce027e2308ad15e7bf2910f52e25058991b4146..4a2b21ba0446233d11a3d160cef998a211d39a02 100644 (file)
@@ -635,10 +635,40 @@ DALI_ADAPTOR_API void FontClientPreCache(const FontFamilyList& fallbackFamilyLis
 DALI_ADAPTOR_API void FontClientFontPreLoad(const FontPathList& fontPathList, const FontPathList& memoryFontPathList, bool useThread, bool syncCreation);
 
 /**
 * @brief Joins font threads, waiting for their execution to complete.
 */
+ * @brief Joins font threads, waiting for their execution to complete.
+ */
 DALI_ADAPTOR_API void FontClientJoinFontThreads();
 
+/**
+ * @brief Ensure the locale of the font client.
+ * @note If there is no locale information, update it using setlocale().
+ */
+DALI_ADAPTOR_API void EnsureLocale();
+
+/**
+ * @brief Gets the current language.
+ *
+ * @note Returns the language code. (e.g., "en")
+ * @return The current language.
+ */
+DALI_ADAPTOR_API const std::string& GetLocale();
+
+/**
+ * @brief Gets the current locale identifier.
+ *
+ * @note Returns the locale identifier. (e.g., "en_US")
+ * @return The current locale identifier.
+ */
+DALI_ADAPTOR_API const std::string& GetLocaleFull();
+
+/**
+ * @brief Sets the current locale.
+ *
+ * @note Update language and locale identifier.
+ * @param[in] locale The current locale.
+ */
+DALI_ADAPTOR_API void SetLocale(const std::string& locale);
+
 } // namespace TextAbstraction
 
 } // namespace Dali
index 66c05ccd587f1d63a152bdd470f42319fa353ac4..b198c97cc7ca025f5238f4e7195096cd27137684 100644 (file)
@@ -379,6 +379,7 @@ void Adaptor::Start()
   mCore->Initialize();
 
   SetupSystemInformation();
+  TextAbstraction::EnsureLocale();
 
   // Start the callback manager
   mCallbackManager->Start();
index bef11991182c5280b5ef82b4560e034135b52979..30306981ca1d3c88765a48a4809314020cf201db 100644 (file)
@@ -55,6 +55,7 @@ static void OnSystemLanguageChanged(system_settings_key_e key, void* data)
   Adaptor* adaptor = static_cast<Adaptor*>(data);
   if(adaptor != NULL)
   {
+    TextAbstraction::SetLocale(locale);
     TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
     fontClient.ClearCacheOnLocaleChanged();
     fontClient.InitDefaultFontDescription();
index 886db026f172e4b8552f80d3c43720818843a833..668a83491df9183dbab7d1e2f82409ed10fa983b 100644 (file)
@@ -79,6 +79,9 @@ public:
   std::thread mThread;
 };
 
+std::string gLocale;     // The current language. (e.g., "en")
+std::string gLocaleFull; // The current locale identifier. (e.g., "en_US")
+
 Dali::TextAbstraction::FontClient FontClient::gPreCreatedFontClient(NULL);
 
 FontThread                        gPreCacheThread;
@@ -318,6 +321,40 @@ void FontClient::JoinFontThreads()
   }
 }
 
+void FontClient::EnsureLocale()
+{
+  if(gLocale.empty() || gLocaleFull.empty())
+  {
+    const char *locale;
+    locale = setlocale(LC_MESSAGES, NULL);
+    if(locale)
+    {
+      SetLocale(locale);
+    }
+  }
+}
+
+const std::string& FontClient::GetLocale()
+{
+  return gLocale;
+}
+
+const std::string& FontClient::GetLocaleFull()
+{
+  return gLocaleFull;
+}
+
+void FontClient::SetLocale(const std::string& locale)
+{
+  std::scoped_lock lock(gMutex);
+
+  // To unify the tizen system locale and format, remove the string after the dot.
+  std::istringstream stringStreamFull(locale);
+  std::getline(stringStreamFull, gLocaleFull, '.');
+  std::istringstream stringStream(locale);
+  std::getline(stringStream, gLocale, '_');
+}
+
 void FontClient::ClearCache()
 {
   if(mPlugin)
index 19db73dd2ea9e0945d149427c9553a70e7378c1a..4652d01184091fa739032c160ab7b82e7e947e7d 100644 (file)
@@ -92,6 +92,27 @@ public: // API for Dali::TextAbstraction::FontClient used.
    */
   static void JoinFontThreads();
 
+  /**
+   * @brief Ensure the locale of the font client.
+   * @note If there is no locale information, update it using setlocale().
+   */
+  static void EnsureLocale();
+
+  /**
+   * @brief Gets the current language. (e.g., "en")
+   */
+  static const std::string& GetLocale();
+
+  /**
+   * @brief Gets the current locale identifier. (e.g., "en_US")
+   */
+  static const std::string& GetLocaleFull();
+
+  /**
+   * @brief Update language and locale identifier.
+   */
+  static void SetLocale(const std::string& locale);
+
   /**
    * @copydoc Dali::TextAbstraction::FontClient::ClearCache()
    */
index 36bc4998c7524e4999f87727097fe6ce7c48950e..b7c921c4e627cb5b79695e09e23ef800c4183ad7 100644 (file)
@@ -176,11 +176,7 @@ struct Shaping::Plugin
         hb_buffer_set_script(harfBuzzBuffer,
                              SCRIPT_TO_HARFBUZZ[script]); /* see hb-unicode.h */
 
-        char* currentLocale = setlocale(LC_MESSAGES, NULL);
-
-        std::istringstream stringStream(currentLocale);
-        std::string        localeString;
-        std::getline(stringStream, localeString, '_');
+        const std::string& localeString = TextAbstraction::GetLocale();
         hb_buffer_set_language(harfBuzzBuffer, hb_language_from_string(localeString.c_str(), localeString.size()));
 
         /* Layout the text */