aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/evas/src/lib/engines/common/language/evas_language_utils.c
blob: b362f10b18ea5973f85773f03fbb41ed5b55f763 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
 * @internal
 * @addtogroup Evas_Utils
 *
 * @{
 */
/**
 * @internal
 * @defgroup Evas_Script Evas Script (language) utility functions
 *
 * This set of functions and types helps evas handle scripts correctly.
 * @todo Document types, structures and macros.
 *
 * @{
 */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdlib.h>

#include <Eina.h>

#include "evas_language_utils.h"
#include "evas_bidi_utils.h" /* Used for splitting according to bidi */
#include "../evas_font_ot.h" /* Used for harfbuzz info */

#ifdef USE_HARFBUZZ
# include <hb.h>
#endif

#include "evas_script_table.h"

static Evas_Script_Type
_evas_common_language_char_script_search(Eina_Unicode unicode)
{
   int min = 0;
   int max  = (sizeof(_evas_script_slow_table) /
      sizeof(_evas_script_slow_table[0])) - 1;
   int mid;

   do
     {
        mid = (min + max) / 2;

        if (unicode < _evas_script_slow_table[mid].start)
           max = mid - 1;
        else if (unicode >= _evas_script_slow_table[mid].start +
              _evas_script_slow_table[mid].len)
           min = mid + 1;
        else
           return _evas_script_slow_table[mid].script;
     }
   while (min <= max);

   return EVAS_SCRIPT_UNKNOWN;
}

Evas_Script_Type
evas_common_language_char_script_get(Eina_Unicode unicode)
{
   if ((unicode >= 0) && (unicode < EVAS_SCRIPT_DIRECT_TABLE_LIMIT))
      return _evas_script_fast_table[unicode];
   else
      return _evas_common_language_char_script_search(unicode);
}

int
evas_common_language_script_end_of_run_get(const Eina_Unicode *str,
      const Evas_BiDi_Paragraph_Props *bidi_props, size_t start, int len)
{
   /* FIXME: Use the standard segmentation instead */
   Evas_Script_Type first = EVAS_SCRIPT_UNKNOWN;
   int i;
   for (i = 0 ; i < len ; i++, str++)
     {
        Evas_Script_Type tmp;
        tmp = evas_common_language_char_script_get(*str);
        /* Arabic is the first script in the array that's not
         * common/inherited. */
        if ((first == EVAS_SCRIPT_UNKNOWN) && (tmp >= EVAS_SCRIPT_ARABIC))
          {
             first = tmp;
             continue;
          }
        if ((first != tmp) && (tmp >= EVAS_SCRIPT_ARABIC))
          {
             break;
          }
     }
#ifdef BIDI_SUPPORT
     {
        int bidi_end;
        bidi_end = evas_bidi_end_of_run_get(bidi_props, start, len);
        if (bidi_end > 0)
          {
             i = (i < bidi_end) ? i : bidi_end;
          }
     }
#else
   (void) bidi_props;
   (void) start;
#endif
   return (i < len) ? i : 0;
}

Evas_Script_Type
evas_common_language_script_type_get(const Eina_Unicode *str, size_t len)
{
   Evas_Script_Type script = EVAS_SCRIPT_COMMON;
   const Eina_Unicode *end = str + len;
   /* Arabic is the first script in the array that's not a common/inherited */
   for ( ; str < end && ((script = evas_common_language_char_script_get(*str)) < EVAS_SCRIPT_ARABIC) ; str++)
     ;
   return script;
}

const char *
evas_common_language_from_locale_get(void)
{
   static char lang[6]; /* FIXME: Maximum length I know about */
   if (*lang) return lang;

   const char *locale;
   locale = getenv("LANG");
   if (locale && *locale)
     {
        char *itr;
        strncpy(lang, locale, 5);
        lang[5] = '\0';
        itr = lang;
        while (*itr)
          {
             if (*itr == '_')
               {
                  *itr = '\0';
               }
             itr++;
          }
        return lang;
     }

   return "";
}

/*
 * @}
 */
/*
 * @}
 */