evas textblock: add align=locale option to respect locale's direction 76/136276/2
authorYoungbok Shin <youngb.shin@samsung.com>
Tue, 13 Jun 2017 10:15:12 +0000 (13:15 +0300)
committerGerrit Code Review <gerrit@review.ap-northeast-2.compute.internal>
Mon, 3 Jul 2017 04:04:05 +0000 (04:04 +0000)
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

Change-Id: I23cff606706d421e1f5388875b4062ce112d2e2e

src/lib/evas/Evas_Common.h
src/lib/evas/canvas/evas_main.c
src/lib/evas/canvas/evas_object_textblock.c
src/lib/evas/common/language/evas_language_utils.c
src/lib/evas/common/language/evas_language_utils.h

index d1f456f..cacf21a 100755 (executable)
@@ -5939,6 +5939,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
index 9122d0c..5df672b 100644 (file)
@@ -717,6 +717,12 @@ evas_ector_get(Evas_Public_Data *e)
    return e->engine.ector;
 }
 
+EAPI Evas_BiDi_Direction
+evas_language_direction_get(void)
+{
+   return evas_common_language_direction_get();
+}
+
 EAPI void
 evas_language_reinit(void)
 {
index 021c0f7..26c9a10 100644 (file)
@@ -419,6 +419,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;
@@ -498,7 +505,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 _Evas_Textblock_Style
@@ -1780,6 +1787,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
@@ -1794,7 +1802,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
           {
@@ -1834,7 +1846,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)
@@ -2675,7 +2687,7 @@ struct _Ctxt
    /* END */
    double align, valign;
    Textblock_Position position;
-   Eina_Bool align_auto : 1;
+   Evas_Textblock_Align_Auto align_auto : 2;
    Eina_Bool width_changed : 1;
 };
 
@@ -3310,7 +3322,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)
      {
         /* TIZEN_ONLY(20170216): Apply align according to paragraph_direction */
         if (c->o->paragraph_direction == EVAS_BIDI_DIRECTION_LTR)
@@ -3332,6 +3344,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;
 }
index 8670006..e52c249 100644 (file)
@@ -22,6 +22,7 @@
 
 #include <stdlib.h>
 #include <locale.h>
+#include <libintl.h>
 
 #include <Eina.h>
 
@@ -43,6 +44,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)
@@ -190,10 +192,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;
 }
 
 /*
index 8c7529a..1ecde84 100644 (file)
@@ -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