summaryrefslogtreecommitdiff
path: root/src/lib/evas/canvas/evas_textblock_hyphenation.x
blob: bdcf37f92c903c7b1d6a1276922dc7874d5388d3 (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
153
154
#ifndef EVAS_TEXTBLOCK_HYPHENATION_H
#define EVAS_TEXTBLOCK_HYPHENATION_H
#ifdef HAVE_HYPHEN
#include <hyphen.h>

typedef struct
{
   const char *lang;
   HyphenDict *dict;
} Dict_Hyphen;

/* Hyphenation dictionaries */
static Dict_Hyphen _dicts_hyphen[64];
static size_t     _hyphens_num = 0;
static size_t     _hyphen_clients = 0;

static void
_dicts_hyphen_init(Eo *eo_obj)
{
   Efl_Canvas_Text_Data *o = eo_data_scope_get(eo_obj, MY_CLASS);

   if (!o->hyphenating)
     {
        _hyphen_clients++;
        o->hyphenating = EINA_TRUE;
     }
}

static void *
_dict_hyphen_load(const char *lang)
{
   Eina_Iterator *it;
   Eina_File_Direct_Info *dir;
   void *dict = NULL;

   it = eina_file_direct_ls(EVAS_DICTS_HYPHEN_DIR);
   if (!it)
     {
        ERR("Couldn't list files in hyphens path: %s\n", EVAS_DICTS_HYPHEN_DIR);
        return NULL;
     }

   /* The following is based on how files are installed in arch linux:
    * the files are in the pattern of "hyph_xx_XX.dic" (e.g. hyph_en_US.dic).
    * We are actually trying a bit more in case these are installed in another
    * name. We assume that they probably end in "xx_XX.dic" anyway. */
   EINA_ITERATOR_FOREACH(it, dir)
     {
        const char *file = dir->path + dir->name_start;
        char *prefix_off; /* 'hyph_' prefix (may be in some distros) */
        char *dic_off; /* '.dic' file extension offset */

        /* Check a few assumptions and reject if aren't met. */
        prefix_off = strstr(file, "hyph_");
        dic_off = strrchr(file, '.');
        if (!dic_off || ((size_t) (dic_off - file) + 4 != dir->name_length) ||
            (dic_off - file < 5)  ||
            ((dic_off - file > 0) && !prefix_off) ||
            strncmp(dic_off, ".dic", 4) || strncmp((dic_off - 5), lang, strlen(lang)))
          {
             continue;
          }

        dict = hnj_hyphen_load(dir->path);
        if (!dict)
          {
             ERR("Couldn't load hyphen dictionary: %s\n", dic_off - 5);
             continue;
          }
        _dicts_hyphen[_hyphens_num].lang = strndup(dic_off - 5, 5);
        _dicts_hyphen[_hyphens_num++].dict = dict;
        break;
     }

   if (it) eina_iterator_free(it);

   return dict;
}

static void
_dicts_hyphen_free(void)
{
   if (_hyphens_num == 0) return;

   for (size_t i = 0; i < _hyphens_num; i++)
     {
        hnj_hyphen_free(_dicts_hyphen[i].dict);
     }

   _hyphens_num = 0;
}

static inline void
_dicts_hyphen_detach(Eo *eo_obj)
{
   Efl_Canvas_Text_Data *o = eo_data_scope_get(eo_obj, MY_CLASS);

   if (!o->hyphenating) return;
   o->hyphenating = EINA_FALSE;
   _hyphen_clients--;
   if (_hyphen_clients == 0) _dicts_hyphen_free();
}

/* Returns the hyphen dictionary that matches the given language
 * string. The string should be in the format xx_XX e.g. en_US */
static inline void *
_hyphen_dict_get_from_lang(const char *lang)
{
   if (!lang || !(*lang))
     {
        if (!lang) lang = evas_common_language_from_locale_full_get();
        if (!lang || !(*lang)) return NULL;
     }

   for (size_t i = 0; i < _hyphens_num; i++)
     {
        if (!strcmp(_dicts_hyphen[i].lang, lang))
          {
             return _dicts_hyphen[i].dict;
          }
     }

   return _dict_hyphen_load(lang);
}

static char *
_layout_wrap_hyphens_get(const Eina_Unicode *text, const char *lang,
      int word_start, int word_len)
{
   char *utf8;
   int utf8_len; /* length of word */
   char *hyphens;
   char **rep = NULL;
   int *pos = NULL;
   int *cut = NULL;
   void *dict;

   dict = _hyphen_dict_get_from_lang(lang);
   if (!dict)
     {
        ERR("Couldn't find matching dictionary and couldn't fallback to locale %s\n", lang);
        return NULL;
     }

   utf8 = eina_unicode_unicode_to_utf8_range(
         text + word_start, word_len, &utf8_len);
   hyphens = malloc(sizeof(char) * (word_len + 5));
   hnj_hyphen_hyphenate2(dict, utf8, word_len, hyphens, NULL, &rep, &pos, &cut);
   free(utf8);
   return hyphens;
}

#endif //HAVE_HYPHEN
#endif //EVAS_TEXTBLOCK_HYPHENATION_H_