herdsman pushed a commit to branch master.

http://git.enlightenment.org/core/efl.git/commit/?id=cbea6e438698c27755754049d35158336ca6daeb

commit cbea6e438698c27755754049d35158336ca6daeb
Author: Youngbok Shin <[email protected]>
Date:   Tue Jun 13 13:15:12 2017 +0300

    evas textblock: add align=locale option to respect locale's direction
    
    Summary:
    There are many requests to add a new feature for handling horizontal align
    according to current locale. For example, in RTL locale setting,
    users want to see right aligned text for every list's item.
    Even if some of list's items only contain LTR characters!
    It is useful for the needs.
    @feature
    
    Test Plan: N/A
    
    Reviewers: herdsman, tasn, woohyun, raster, cedric
    
    Reviewed By: herdsman, raster
    
    Subscribers: z-wony, jpeg
    
    Differential Revision: https://phab.enlightenment.org/D4664
---
 src/lib/evas/Evas_Common.h                         |  8 +++++
 src/lib/evas/canvas/evas_main.c                    |  6 ++++
 src/lib/evas/canvas/evas_object_textblock.c        | 36 +++++++++++++++++++---
 src/lib/evas/common/language/evas_language_utils.c | 19 ++++++++++++
 src/lib/evas/common/language/evas_language_utils.h |  3 ++
 5 files changed, 67 insertions(+), 5 deletions(-)

diff --git a/src/lib/evas/Evas_Common.h b/src/lib/evas/Evas_Common.h
index 80e36587e5..4051f4f33a 100644
--- a/src/lib/evas/Evas_Common.h
+++ b/src/lib/evas/Evas_Common.h
@@ -3221,6 +3221,14 @@ EAPI int  evas_string_char_prev_get(const char *str, int 
pos, int *decoded) EINA
 EAPI int  evas_string_char_len_get(const char *str) EINA_WARN_UNUSED_RESULT 
EINA_ARG_NONNULL(1);
 
 /**
+ * Get language direction.
+ *
+ * @ingroup Evas_Utils
+ * @since 1.18
+ */
+EAPI Evas_BiDi_Direction     evas_language_direction_get(void);
+
+/**
  * Reinitialize language from the environment.
  *
  * The locale can change while a process is running. This call tells evas to
diff --git a/src/lib/evas/canvas/evas_main.c b/src/lib/evas/canvas/evas_main.c
index 964b8ccbb3..ea3e5daa4e 100644
--- a/src/lib/evas/canvas/evas_main.c
+++ b/src/lib/evas/canvas/evas_main.c
@@ -820,6 +820,12 @@ evas_ector_get(Evas_Public_Data *e, void *output)
    return NULL;
 }
 
+EAPI Evas_BiDi_Direction
+evas_language_direction_get(void)
+{
+   return evas_common_language_direction_get();
+}
+
 EAPI void
 evas_language_reinit(void)
 {
diff --git a/src/lib/evas/canvas/evas_object_textblock.c 
b/src/lib/evas/canvas/evas_object_textblock.c
index 6476c48b02..05ebba9e4c 100644
--- a/src/lib/evas/canvas/evas_object_textblock.c
+++ b/src/lib/evas/canvas/evas_object_textblock.c
@@ -421,6 +421,13 @@ typedef enum _Evas_Textblock_Item_Type
    EVAS_TEXTBLOCK_ITEM_FORMAT,
 } Evas_Textblock_Item_Type;
 
+typedef enum _Evas_Textblock_Align_Auto
+{
+   EVAS_TEXTBLOCK_ALIGN_AUTO_NONE,
+   EVAS_TEXTBLOCK_ALIGN_AUTO_NORMAL,
+   EVAS_TEXTBLOCK_ALIGN_AUTO_LOCALE
+} Evas_Textblock_Align_Auto;
+
 struct _Evas_Object_Textblock_Item
 {
    EINA_INLIST;
@@ -538,7 +545,7 @@ struct _Evas_Object_Textblock_Format
    Eina_Bool            strikethrough : 1;  /**< EINA_TRUE if text should be 
stricked off, else EINA_FALSE */
    Eina_Bool            backing : 1;  /**< EINA_TRUE if enable background 
color, else EINA_FALSE */
    Eina_Bool            password : 1;  /**< EINA_TRUE if the text is password, 
else EINA_FALSE */
-   Eina_Bool            halign_auto : 1;  /**< EINA_TRUE if auto horizontal 
align, else EINA_FALSE */
+   Evas_Textblock_Align_Auto halign_auto : 2;  /**< Auto horizontal align mode 
*/
 };
 
 struct _Efl_Canvas_Text_Style
@@ -1928,6 +1935,7 @@ _format_command(Evas_Object *eo_obj, 
Evas_Object_Textblock_Format *fmt, const ch
          * Sets the horizontal alignment of the text. The value can either be
          * a number, a percentage or one of several presets:
          * @li "auto" - Respects LTR/RTL settings
+         * @li "locale" - Respects locale(language) direction settings
          * @li "center" - Centers the text in the line
          * @li "middle" - Alias for "center"
          * @li "left" - Puts the text at the left of the line
@@ -1942,7 +1950,11 @@ _format_command(Evas_Object *eo_obj, 
Evas_Object_Textblock_Format *fmt, const ch
          */
         if (len == 4 && !strcmp(param, "auto"))
           {
-             fmt->halign_auto = EINA_TRUE;
+             fmt->halign_auto = EVAS_TEXTBLOCK_ALIGN_AUTO_NORMAL;
+          }
+        if (len == 6 && !strcmp(param, "locale"))
+          {
+             fmt->halign_auto = EVAS_TEXTBLOCK_ALIGN_AUTO_LOCALE;
           }
         else
           {
@@ -1982,7 +1994,7 @@ _format_command(Evas_Object *eo_obj, 
Evas_Object_Textblock_Format *fmt, const ch
                   if (fmt->halign < 0.0) fmt->halign = 0.0;
                   else if (fmt->halign > 1.0) fmt->halign = 1.0;
                }
-             fmt->halign_auto = EINA_FALSE;
+             fmt->halign_auto = EVAS_TEXTBLOCK_ALIGN_AUTO_NONE;
           }
      }
    else if (cmd == valignstr)
@@ -2859,7 +2871,7 @@ struct _Ctxt
    int have_underline, have_underline2;
    double align, valign;
    Textblock_Position position;
-   Eina_Bool align_auto : 1;
+   Evas_Textblock_Align_Auto align_auto : 2;
    Eina_Bool width_changed : 1;
 };
 
@@ -3503,7 +3515,7 @@ static inline double
 _layout_line_align_get(Ctxt *c)
 {
 #ifdef BIDI_SUPPORT
-   if (c->align_auto && c->ln)
+   if ((c->align_auto == EVAS_TEXTBLOCK_ALIGN_AUTO_NORMAL) && c->ln)
      {
         if (c->ln->items && c->ln->items->text_node &&
               (c->ln->par->direction == EVAS_BIDI_DIRECTION_RTL))
@@ -3517,6 +3529,20 @@ _layout_line_align_get(Ctxt *c)
              return 0.0;
           }
      }
+   else if (c->align_auto == EVAS_TEXTBLOCK_ALIGN_AUTO_LOCALE)
+     {
+        if (evas_common_language_direction_get() == EVAS_BIDI_DIRECTION_RTL)
+          {
+             /* Align right*/
+             return 1.0;
+          }
+        else
+          {
+             /* Align left */
+             return 0.0;
+          }
+     }
+
 #endif
    return c->align;
 }
diff --git a/src/lib/evas/common/language/evas_language_utils.c 
b/src/lib/evas/common/language/evas_language_utils.c
index ce075a1534..8eda465480 100644
--- a/src/lib/evas/common/language/evas_language_utils.c
+++ b/src/lib/evas/common/language/evas_language_utils.c
@@ -22,6 +22,7 @@
 
 #include <stdlib.h>
 #include <locale.h>
+#include <libintl.h>
 
 #ifdef HAVE_EVIL
 # include <Evil.h>
@@ -47,6 +48,7 @@
 
 static char lang[6]; /* FIXME: Maximum length I know about */
 static char lang_full[32];
+static Evas_BiDi_Direction lang_dir = EVAS_BIDI_DIRECTION_NEUTRAL;
 
 static Evas_Script_Type
 _evas_common_language_char_script_search(Eina_Unicode unicode)
@@ -194,10 +196,27 @@ evas_common_language_from_locale_full_get(void)
    return "";
 }
 
+Evas_BiDi_Direction
+evas_common_language_direction_get(void)
+{
+   if (lang_dir == EVAS_BIDI_DIRECTION_NEUTRAL)
+     {
+        const char *dir_str = dgettext(PACKAGE, "default:LTR");
+
+        if (dir_str && !strcmp(dir_str, "default:RTL"))
+          lang_dir = EVAS_BIDI_DIRECTION_RTL;
+        else
+          lang_dir = EVAS_BIDI_DIRECTION_LTR;
+     }
+
+   return lang_dir;
+}
+
 void
 evas_common_language_reinit(void)
 {
    *lang = *lang_full = '\0';
+   lang_dir = EVAS_BIDI_DIRECTION_NEUTRAL;
 }
 
 /*
diff --git a/src/lib/evas/common/language/evas_language_utils.h 
b/src/lib/evas/common/language/evas_language_utils.h
index 8c7529a572..1ecde84967 100644
--- a/src/lib/evas/common/language/evas_language_utils.h
+++ b/src/lib/evas/common/language/evas_language_utils.h
@@ -132,6 +132,9 @@ evas_common_language_from_locale_get(void);
 const char *
 evas_common_language_from_locale_full_get(void);
 
+Evas_BiDi_Direction
+evas_common_language_direction_get(void);
+
 void
 evas_common_language_reinit(void);
 #endif

-- 


Reply via email to