summaryrefslogtreecommitdiff
path: root/src/lib/evas/common/language/evas_bidi_utils.c
blob: ab1afeb1f7d9e16aa32a082d7ff4a8c6b95c69b2 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <string.h>
#include <stdlib.h>

#include "evas_common_private.h"
#include "evas_bidi_utils.h"

#include "evas_font_private.h"

#ifdef BIDI_SUPPORT
#include <fribidi.h>
/**
 * @internal
 * @addtogroup Evas_Utils
 *
 * @{
 */
/**
 * @internal
 * @addtogroup Evas_BiDi
 *
 * @{
 */

/**
 * @internal
 * @def _SAFE_FREE(x)
 * checks if x is not NULL, if it's not, it's freed and set to NULL.
 */
#define _SAFE_FREE(x) \
   do {               \
      if (x)          \
        {             \
           free(x);   \
           x = NULL;  \
        }             \
     } while(0)

#if SIZEOF_FRIBIDICHAR != SIZEOF_EINA_UNICODE
# define EVAS_FRIBIDI_EINA_UNICODE_UNEQUAL
#endif

#ifdef EVAS_FRIBIDI_EINA_UNICODE_UNEQUAL
/* Convert bidichar to eina_unicode assume both are valid pointers */
static Eina_Unicode *
_evas_bidi_fribidichar_to_unicode(Eina_Unicode *dest, const FriBidiChar *src)
{
   Eina_Unicode *ret = dest;

   if ((!src) || (!dest)) return NULL;
   while (*src) *dest++ = *src++;
   *dest = 0;
   return ret;
}

/* Convert eina_unicode to bidi_char assume both are valid pointers */
static FriBidiChar *
_evas_bidi_unicode_to_fribidichar(FriBidiChar *dest, const Eina_Unicode *src)
{
   FriBidiChar *ret = dest;

   if ((!src) || (!dest)) return NULL;
   while (*src) *dest++ = *src++;
   *dest = 0;
   return ret;
}
#endif

/**
 * @internal
 * Checks if the string has RTL characters.
 *
 * @param str The string to be checked
 * @return #EINA_TRUE if true, #EINA_FALSE otherwise.
 */
Eina_Bool
evas_bidi_is_rtl_str(const Eina_Unicode *str)
{
   EvasBiDiCharType type;

   if (!str)
      return EINA_FALSE;

   for ( ; *str ; str++)
     {
        type = fribidi_get_bidi_type((FriBidiChar) *str);
        if (FRIBIDI_IS_RTL(type))
          {
             return EINA_TRUE;
          }
     }
   return EINA_FALSE;
}

/**
 * @internal
 * Shapes the string ustr according to the bidi properties.
 *
 * @param str The string to shape
 * @param bidi_props the bidi props to shaped according.
 * @param start the start of the string to shape (offset in bidi_props)
 * @param len the length of th string.
 * @return #EINA_TRUE on success, #EINA_FALSE otherwise.
 */
EAPI Eina_Bool
evas_bidi_shape_string(Eina_Unicode *eina_ustr, const Evas_BiDi_Paragraph_Props *bidi_props, size_t start, size_t len)
{
   FriBidiChar *ustr, *base_ustr = NULL;

   if (!bidi_props)
     return EINA_FALSE;

   /* The size of fribidichar is different than eina_unicode, convert */
#ifdef EVAS_FRIBIDI_EINA_UNICODE_UNEQUAL
   base_ustr = ustr = calloc(len + 1, sizeof(FriBidiChar));
   ustr = _evas_bidi_unicode_to_fribidichar(ustr, eina_ustr);
#else
   (void) base_ustr;
   ustr = (FriBidiChar *) eina_ustr;
#endif


   EvasBiDiJoiningType *join_types = NULL;
   join_types = (EvasBiDiJoiningType *) malloc(sizeof(EvasBiDiJoiningType) * len);
   if (!join_types)
     {
#ifdef EVAS_FRIBIDI_EINA_UNICODE_UNEQUAL
        if (base_ustr) free(base_ustr);
#endif
        return EINA_FALSE;
     }
   fribidi_get_joining_types(ustr, len, join_types);

   fribidi_join_arabic(bidi_props->char_types + start, len,
         bidi_props->embedding_levels + start, join_types);


   /* Actually modify the string */
   fribidi_shape(FRIBIDI_FLAGS_DEFAULT | FRIBIDI_FLAGS_ARABIC,
               bidi_props->embedding_levels + start, len, join_types, ustr);

   if (join_types) free(join_types);

   /* Convert back */
#ifdef EVAS_FRIBIDI_EINA_UNICODE_UNEQUAL
   _evas_bidi_fribidichar_to_unicode(eina_ustr, ustr);
   if (base_ustr) free(base_ustr);
#endif
   return EINA_TRUE;
}

/**
 * @internal
 * Return a -1 terminated array of the indexes of the delimiters (passed in
 * delim) found in the string. This result should be used with par_props_get.
 *
 * @param str The string to parse
 * @param delim a list of delimiters to work with.
 * @return returns a -1 terminated array of indexes according to positions of the delimiters found. NULL if there were none.
 */
int *
evas_bidi_segment_idxs_get(const Eina_Unicode *str, const char *delim)
{
   Eina_Unicode *udelim;
   const Eina_Unicode *str_base = str;
   int *ret, *tmp_ret;
   int ret_idx = 0, ret_len = 10; /* arbitrary choice */
   udelim = eina_unicode_utf8_to_unicode(delim, NULL);
   ret = malloc(ret_len * sizeof(int));
   for ( ; *str ; str++)
     {
        const Eina_Unicode *del;
        for (del = udelim ; *del ; del++)
          {
             if (*str == *del)
               {
                  if (ret_idx >= ret_len)
                    {
                       /* arbitrary choice */
                       ret_len += 20;
                       tmp_ret = realloc(ret, ret_len * sizeof(int));
                       if (!tmp_ret)
                         {
                            free(ret);
                            free(udelim);
                            return NULL;
                         }
                       ret = tmp_ret;
                    }
                  ret[ret_idx++] = str - str_base;
                  break;
               }
          }
     }
   free(udelim);

   /* If no indexes were found return NULL */
   if (ret_idx == 0)
     {
        free(ret);
        return NULL;
     }

   ret[ret_idx] = -1;
   tmp_ret = realloc(ret, (ret_idx + 1) * sizeof(int));

   return (tmp_ret) ? tmp_ret : ret;
}

/**
 * @internal
 * Allocates bidi properties according to ustr. First checks to see if the
 * passed has rtl chars, if not, it returns NULL.
 *
 * Assumes all the segment_idxs are either -1 or legal, and > 0 indexes.
 * Also assumes that the characters at the override points are of weak/neutral
 * bidi type, otherwise unexpected results may occur.
 *
 * @param ustr The string to update according to.
 * @param len The length of the string
 * @param segment_idxs A -1 terminated array of points to start a new bidi analysis at (used for section high level bidi overrides). - NULL means none.
 * @param base_bidi The base BiDi direction of paragraph.
 * @return returns allocated paragraph props on success, NULL otherwise.
 */
Evas_BiDi_Paragraph_Props *
evas_bidi_paragraph_props_get(const Eina_Unicode *eina_ustr, size_t len,
      int *segment_idxs, EvasBiDiParType base_bidi)
{
   Evas_BiDi_Paragraph_Props *bidi_props = NULL;
   EvasBiDiCharType *char_types = NULL;
   EvasBiDiLevel *embedding_levels = NULL;
   const FriBidiChar *ustr;
#ifdef EVAS_FRIBIDI_EINA_UNICODE_UNEQUAL
   FriBidiChar *base_ustr = NULL;
#endif
   EvasBiDiLevel ret_level = 0;
#if FRIBIDI_MAJOR_VERSION >= 1
   EvasBiDiBracketType *bracket_types = NULL;
#endif

   if (!eina_ustr)
      return NULL;

   /* No need to handle bidi */
   if (!evas_bidi_is_rtl_str(eina_ustr) &&
       (base_bidi != EVAS_BIDI_PARAGRAPH_RTL))
     {
        goto cleanup;
     }

   len = eina_unicode_strlen(eina_ustr);
   /* The size of fribidichar s different than eina_unicode, convert */
#ifdef EVAS_FRIBIDI_EINA_UNICODE_UNEQUAL
   base_ustr = calloc(len + 1, sizeof(FriBidiChar));
   base_ustr = _evas_bidi_unicode_to_fribidichar(base_ustr, eina_ustr);
   ustr = base_ustr;
#else
   ustr = (const FriBidiChar *) eina_ustr;
#endif

   bidi_props = evas_bidi_paragraph_props_new();
   bidi_props->direction = base_bidi;

   /* Prep work for reordering */
   char_types = (EvasBiDiCharType *) malloc(sizeof(EvasBiDiCharType) * len);
   if (!char_types)
      {
         goto cleanup;
      }
   fribidi_get_bidi_types(ustr, len, char_types);

#if FRIBIDI_MAJOR_VERSION >= 1
   bracket_types = (EvasBiDiBracketType *) malloc(sizeof(EvasBiDiBracketType) * len);
   if (!bracket_types)
      {
         goto cleanup;
      }
   fribidi_get_bracket_types(ustr, len, char_types, bracket_types);
#endif

   embedding_levels = (EvasBiDiLevel *)malloc(sizeof(EvasBiDiLevel) * len);
   if (!embedding_levels)
     {
        goto cleanup;
     }

   if (segment_idxs)
     {
        size_t pos = 0;
        int *itr;
        EvasBiDiLevel base_level = 0;
        EvasBiDiParType direction;

        for (itr = segment_idxs ; *itr > 0 ; itr++)
          {
             direction = base_bidi;
#if FRIBIDI_MAJOR_VERSION >= 1
             ret_level = fribidi_get_par_embedding_levels_ex(char_types + pos,
                                                             bracket_types,
                                                             *itr - pos,
                                                             &direction,
                                                             embedding_levels + pos);
#else
             ret_level = fribidi_get_par_embedding_levels(char_types + pos,
                                                          *itr - pos,
                                                          &direction,
                                                          embedding_levels + pos);
#endif
             if (!ret_level)
               {
                  goto cleanup;
               }

             /* Only on the first run */
             if (itr == segment_idxs)
               {
                  bidi_props->direction = direction;
                  /* adjust base_level to be 1 for rtl paragraphs, and 0 for
                   * ltr paragraphs. */
                  base_level =
                     EVAS_BIDI_PARAGRAPH_DIRECTION_IS_RTL(bidi_props) ? 1 : 0;
               }

             /* We want those chars at the override points to be on the base
              * level and we also remove -2 cause we later increment them,
              * just for simpler code paths */
             embedding_levels[*itr] = base_level - 2;
             pos = *itr + 1;
          }

        direction = base_bidi;
#if FRIBIDI_MAJOR_VERSION >= 1
        ret_level = fribidi_get_par_embedding_levels_ex(char_types + pos,
                                                        bracket_types,
                                                        len - pos,
                                                        &direction,
                                                        embedding_levels + pos);
#else
        ret_level = fribidi_get_par_embedding_levels(char_types + pos,
                                                     len - pos,
                                                     &direction,
                                                     embedding_levels + pos);
#endif
        if (!ret_level)
          {
             goto cleanup;
          }

        /* Increment all levels by 2 to emulate embedding. */
          {
             EvasBiDiLevel *bitr = embedding_levels, *end;
             end = bitr + len;
             for ( ; bitr < end ; bitr++)
               {
                  *bitr += 2;
               }
          }
     }
   else
     {
#if FRIBIDI_MAJOR_VERSION >= 1
        ret_level = fribidi_get_par_embedding_levels_ex(char_types,
                                                        bracket_types,
                                                        len,
                                                        &bidi_props->direction,
                                                        embedding_levels);
#else
        ret_level = fribidi_get_par_embedding_levels(char_types,
                                                     len,
                                                     &bidi_props->direction,
                                                     embedding_levels);
#endif
        if (!ret_level)
          {
             goto cleanup;
          }
     }


   /* clean up */
   if (bidi_props->embedding_levels)
     {
        free(bidi_props->embedding_levels);
     }
   bidi_props->embedding_levels = embedding_levels;

   /* clean up */

   if (bidi_props->char_types)
     {
        free(bidi_props->char_types);
     }
   bidi_props->char_types = char_types;
#ifdef EVAS_FRIBIDI_EINA_UNICODE_UNEQUAL
   if (base_ustr) free(base_ustr);
#endif
#if FRIBIDI_MAJOR_VERSION >= 1
   /* Currently, bracket_types is not reused in other places. */
   if (bracket_types) free(bracket_types);
#endif

   return bidi_props;

/* Cleanup */
cleanup:
   if (char_types) free(char_types);
#if FRIBIDI_MAJOR_VERSION >= 1
   if (bracket_types) free(bracket_types);
#endif
   if (embedding_levels) free(embedding_levels);
#ifdef EVAS_FRIBIDI_EINA_UNICODE_UNEQUAL
   if (base_ustr) free(base_ustr);
#endif
   if (bidi_props) evas_bidi_paragraph_props_unref(bidi_props); /* Clean up the bidi props */
   return NULL;
}

/**
 * @internal
 * Copies dst to src and refs (doesn't copy) the paragraph props.
 *
 * @param src the props to copy
 * @param dst the props to copy to.
 */
void
evas_bidi_props_copy_and_ref(const Evas_BiDi_Props *src, Evas_BiDi_Props *dst)
{
   dst->dir = src->dir;
}

/**
 * @internal
 * Reorders ustr according to the bidi props.
 *
 * @param ustr the string to reorder. - Null is ok, will just populate the map.
 * @param start the start of the line
 * @param len the length of the line
 * @param props the paragraph props to reorder according to
 * @param _v_to_l The visual to logical map to populate - if NULL it won't populate it.
 * @return #EINA_FALSE on success, #EINA_TRUE on error.
 */
Eina_Bool
evas_bidi_props_reorder_line(Eina_Unicode *eina_ustr, size_t start, size_t len, const Evas_BiDi_Paragraph_Props *props, EvasBiDiStrIndex **_v_to_l)
{
   EvasBiDiStrIndex *v_to_l = NULL;
   FriBidiChar *ustr = NULL;
#ifdef EVAS_FRIBIDI_EINA_UNICODE_UNEQUAL
   FriBidiChar *base_ustr = NULL;
#endif

   if (!props)
     return EINA_FALSE;

   if (eina_ustr)
     {
        /* The size of fribidichar is different than eina_unicode, convert */
#ifdef EVAS_FRIBIDI_EINA_UNICODE_UNEQUAL
        base_ustr = ustr = calloc(len + 1, sizeof(FriBidiChar));
        ustr = _evas_bidi_unicode_to_fribidichar(ustr, eina_ustr);
#else
        ustr = (FriBidiChar *) eina_ustr;
#endif
     }


   if (_v_to_l) {
      size_t i;
      v_to_l = *_v_to_l = calloc(len, sizeof(EvasBiDiStrIndex));
      if (!v_to_l)
        {
           goto error;
        }
      /* init the array for fribidi */
      for (i = 0 ; i < len ; i++)
        {
           v_to_l[i] = i;
        }
   }

     {
        EvasBiDiLevel *emb_lvl;
        emb_lvl = malloc((start + len) * sizeof(EvasBiDiLevel));
        memcpy(emb_lvl, props->embedding_levels,
              (start + len) * sizeof(EvasBiDiLevel));
        /* We pass v_to_l - start, because fribidi assumes start is the offset
         * from the start of v_to_l as well, not just the props. */
        if (!fribidi_reorder_line (FRIBIDI_FLAGS_DEFAULT, props->char_types,
                 len, start, props->direction, emb_lvl, ustr, v_to_l - start))
          {
             free(emb_lvl);
             goto error;
          }
        free(emb_lvl);
     }


   /* The size of fribidichar is different than eina_unicode, convert */
#ifdef EVAS_FRIBIDI_EINA_UNICODE_UNEQUAL
   _evas_bidi_fribidichar_to_unicode(eina_ustr, base_ustr);
   free(base_ustr);
#endif
   return EINA_FALSE;
/* ERROR HANDLING */
error:
#ifdef EVAS_FRIBIDI_EINA_UNICODE_UNEQUAL
   if (base_ustr) free(base_ustr);
#endif
   _SAFE_FREE(v_to_l);
   return EINA_TRUE;
}

/**
 * @internal
 * Returns the end of the current run of text
 *
 * @param bidi_props the paragraph properties
 * @param start where to start looking from
 * @param len the length of the string
 * @return the position of the end of the run (offset from
 * bidi_props->props->start), 0 when there is no end (i.e all the text)
 */
int
evas_bidi_end_of_run_get(const Evas_BiDi_Paragraph_Props *bidi_props,
      size_t start, int len)
{
   EvasBiDiLevel *i;
   EvasBiDiLevel base;

   if (!bidi_props || (len <= 0))
     return 0;

   i = bidi_props->embedding_levels + start;
   base = *i;
   for ( ; (len > 0) && (base == *i) ; len--, i++)
     ;

   if (len == 0)
     {
        return 0;
     }
   return i - (bidi_props->embedding_levels + start);
}

/**
 * @internal
 * Returns the visual string index from the logical string index.
 *
 * @param v_to_l the visual to logical map
 * @param len the length of the map.
 * @param position the position to convert.
 * @return on success the visual position, on failure the same position.
 */
EvasBiDiStrIndex
evas_bidi_position_logical_to_visual(EvasBiDiStrIndex *v_to_l, int len, EvasBiDiStrIndex position)
{
   int i;
   EvasBiDiStrIndex *ind;
   if (position >= len || !v_to_l)
      return position;

   for (i = 0, ind = v_to_l ; i < len ; i++, ind++)
     {
        if (*ind == position)
          {
             return i;
          }
     }
   return position;
}

/**
 * @internal
 * Returns the reversed pos of the index.
 *
 * @param dir the direction of the string
 * @param len the length of the map.
 * @param position the position to convert.
 * @return on success the visual position, on failure the same position.
 */
EvasBiDiStrIndex
evas_bidi_position_reverse(const Evas_BiDi_Props *props, int len, EvasBiDiStrIndex position)
{
   if (!props || position >= len)
      return position;

   return (props->dir == EVAS_BIDI_DIRECTION_RTL) ? (len - 1) - position : position;
}

/**
 * @internal
 * Checks if the char is rtl oriented. I.e even a neutral char can become rtl
 * if surrounded by rtl chars.
 *
 * @param bidi_props The bidi paragraph properties
 * @param start the base position
 * @param index the offset from the base position.
 * @return #EINA_TRUE if true, #EINA_FALSE otherwise.
 */
Eina_Bool
evas_bidi_is_rtl_char(const Evas_BiDi_Paragraph_Props *bidi_props, size_t start, EvasBiDiStrIndex ind)
{
   if(!bidi_props || ind < 0)
      return EINA_FALSE;
   return (FRIBIDI_IS_RTL(
            bidi_props->embedding_levels[ind + start]))
      ? EINA_TRUE : EINA_FALSE;
}

Evas_BiDi_Paragraph_Props *
evas_bidi_paragraph_props_new(void)
{
   Evas_BiDi_Paragraph_Props *ret;
   ret = calloc(1, sizeof(Evas_BiDi_Paragraph_Props));
   ret->direction = EVAS_BIDI_PARAGRAPH_NEUTRAL;
   ret->refcount = 1;

   return ret;
}

/**
 * @internal
 * Refs the bidi props.
 *
 * @param bidi_props the props to ref.
 */
Evas_BiDi_Paragraph_Props *
evas_bidi_paragraph_props_ref(Evas_BiDi_Paragraph_Props *bidi_props)
{
   if (!bidi_props) return NULL;
   BIDILOCK();

   bidi_props->refcount++;
   BIDIUNLOCK();
   return bidi_props;
}

/**
 * @internal
 * Unrefs and potentially frees the props.
 *
 * @param bidi_props the properties to unref
 */
void
evas_bidi_paragraph_props_unref(Evas_BiDi_Paragraph_Props *bidi_props)
{
   if (!bidi_props) return;
   BIDILOCK();

   if (--bidi_props->refcount == 0)
     {
        evas_bidi_paragraph_props_clean(bidi_props);
        free(bidi_props);
     }
   BIDIUNLOCK();
}


/**
 * @internal
 * Cleans the paragraph properties.
 *
 * @param bidi_props the properties to clean.
 */
void
evas_bidi_paragraph_props_clean(Evas_BiDi_Paragraph_Props *bidi_props)
{
   _SAFE_FREE(bidi_props->embedding_levels);
   _SAFE_FREE(bidi_props->char_types);
}

/**
 * @internal
 * Cleans the bidi properties.
 *
 * @param bidi_props the properties to clean.
 */
void
evas_bidi_props_clean(Evas_BiDi_Props *bidi_props)
{
   if (!bidi_props) return;
   bidi_props->dir = EVAS_BIDI_DIRECTION_NEUTRAL;
}
/**
 * @}
 */
/**
 * @}
 */
#endif

#if 0
/* Good for debugging */
static void
dump_levels(Eina_Unicode *ustr, EvasBiDiLevel *emb)
{
   for ( ; *ustr ; ustr++, emb++)
     {
        printf("%lc %d\n", *ustr, *emb);
     }
}
#endif