summaryrefslogtreecommitdiff
path: root/pango/mini-fribidi/fribidi.c
blob: 902cde311cf77099397fdc8c0ffe9e105578e9fb (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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
/* FriBidi - Library of BiDi algorithm
 * Copyright (C) 1999,2000 Dov Grobgeld, and
 * Copyright (C) 2001 Behdad Esfahbod.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public  
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,  
 * but WITHOUT ANY WARRANTY; without even the implied warranty of   
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License  
 * along with this library, in a file named COPYING.LIB; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA
 * 
 * For licensing issues, contact <dov@imagic.weizmann.ac.il> and
 * <fwpg@sharif.edu>.
 */

#include <glib.h>
#include "pango/pango-utils.h"
#include "fribidi_types.h"

#undef DEBUG

#ifdef DEBUG
#include <stdio.h>
#endif

#ifdef DEBUG
#define DBG(s) if (fribidi_debug) { fprintf(stderr, s); }
#else
#define DBG(s)
#endif

#ifdef DEBUG
// for easier test with the reference code only.
#define MAX_LEVEL 15
#else
// default value.
#define MAX_LEVEL 61
#endif
/*======================================================================
// Typedef for the run-length list.
//----------------------------------------------------------------------*/
typedef struct _TypeLink TypeLink;

struct _TypeLink
{
  TypeLink *prev;
  TypeLink *next;

  FriBidiCharType type;
  gint pos;
  gint len;
  gint level;
};

#define FRIBIDI_LEVEL_START   -1
#define FRIBIDI_LEVEL_END     -1
#define FRIBIDI_LEVEL_REMOVED -2

typedef struct
{
  FriBidiChar key;
  FriBidiChar value;
}
key_value_t;

typedef struct
{
  FriBidiCharType override;	/* only L, R and N are valid */
  gint level;
}
level_info;

#ifdef DEBUG
static gboolean fribidi_debug = FALSE;
#endif

gboolean fribidi_set_debug (gboolean debug)
{
#ifdef DEBUG
  return fribidi_debug = debug;
#else
  return 0;
#endif
}

#ifdef DEBUG
static gint
bidi_string_strlen (FriBidiChar * str)
{
  gint len = 0;

  while (*str++)
    len++;

  return len;
}
#endif

#ifndef USE_SIMPLE_MALLOC
static TypeLink *free_type_links = NULL;
#endif

static TypeLink *
new_type_link (void)
{
  TypeLink *link;

#ifdef USE_SIMPLE_MALLOC
  link = g_malloc (sizeof (TypeLink));
#else
  if (free_type_links)
    {
      link = free_type_links;
      free_type_links = free_type_links->next;
    }
  else
    {
      static GMemChunk *mem_chunk = NULL;

      if (!mem_chunk)
	mem_chunk = g_mem_chunk_new ("TypeLinkList",
				     sizeof (TypeLink),
				     sizeof (TypeLink) * 128, G_ALLOC_ONLY);

      link = g_chunk_new (TypeLink, mem_chunk);
    }
#endif

  link->len = 0;
  link->pos = 0;
  link->level = 0;
  link->next = NULL;
  link->prev = NULL;
  return link;
}

static void
free_type_link (TypeLink * link)
{
#ifdef USE_SIMPLE_MALLOC
  g_free (link);
#else
  link->next = free_type_links;
  free_type_links = link;
#endif
}

static TypeLink *
run_length_encode_types (FriBidiCharType * char_type, gint type_len)
{
  TypeLink *list = NULL, *last, *link;
  TypeLink current;

  FriBidiCharType type;
  gint len, pos, i;

  /* Add the starting link */
  list = new_type_link ();
  list->type = FRIBIDI_TYPE_SOT;
  list->level = FRIBIDI_LEVEL_START;
  list->len = 0;
  list->pos = 0;
  last = list;

  /* Sweep over the string_types */
  current.type = FRIBIDI_LEVEL_START;
  current.len = 0;
  current.pos = -1;
  for (i = 0; i <= type_len; i++)
    {
      if (char_type[i] != current.type || i == type_len)
	{
	  if (current.pos >= 0)
	    {
	      link = new_type_link ();
	      link->type = current.type;
	      link->pos = current.pos;
	      link->len = current.len;
	      last->next = link;
	      link->prev = last;
	      last = last->next;
	    }
	  if (i == type_len)
	    break;
	  current.len = 0;
	  current.pos = i;
	}
      current.type = char_type[i];
      current.len++;
    }

  /* Add the ending link */
  link = new_type_link ();
  link->type = FRIBIDI_TYPE_EOT;
  link->level = FRIBIDI_LEVEL_END;
  link->len = 0;
  link->pos = type_len;
  last->next = link;
  link->prev = last;

  return list;
}

/* explicits_list is a list like type_rl_list, that holds the explicit
   codes that are removed from rl_list, to reinsert them later by calling
   the override_list.
*/
static void
init_list (TypeLink ** start, TypeLink ** end)
{
  TypeLink *list = NULL;
  TypeLink *link;

  /* Add the starting link */
  list = new_type_link ();
  list->type = FRIBIDI_TYPE_SOT;
  list->level = FRIBIDI_LEVEL_START;
  list->len = 0;
  list->pos = 0;

  /* Add the ending link */
  link = new_type_link ();
  link->type = FRIBIDI_TYPE_EOT;
  link->level = FRIBIDI_LEVEL_END;
  link->len = 0;
  link->pos = 0;
  list->next = link;
  link->prev = list;

  *start = list;
  *end = link;
}

/* move an element before another element in a list, the list must have a
   previous element, used to update explicits_list.
   assuming that p have both prev and next or none of them, also update
   the list that p is currently in, if any.
*/
void
move_element_before (TypeLink * p, TypeLink * list)
{
  if (p->prev)
    {
      p->prev->next = p->next;
      p->next->prev = p->prev;
    }
  p->prev = list->prev;
  list->prev->next = p;
  p->next = list;
  list->prev = p;
}

/* override the rl_list 'base', with the elements in the list 'over', to
   reinsert the previously-removed explicit codes (at X9) from
   'explicits_list' back into 'type_rl_list'. This is used at the end of I2
   to restore the explicit marks, and also to reset the character types of
   characters at L1.

   it is assumed that the 'pos' of the first element in 'base' list is not
   more than the 'pos' of the first element of the 'over' list, and the
   'pos' of the last element of the 'base' list is not less than the 'pos'
   of the last element of the 'over' list. these two conditions are always
   satisfied for the two usages mentioned above.

   TBD: use some explanatory names instead of p, q, ...
*/
void
override_list (TypeLink * base, TypeLink * over)
{
  TypeLink *p = base, *q, *r, *s, *t;
  gint pos = 0, pos2;

  if (!base)
    base = over;
  else if (over)
    {
      q = over;
      while (q)
	{
	  if (!q->len || q->pos < pos)
	    {
	      t = q;
	      q = q->next;
	      free_type_link (t);
	      continue;
	    }
	  pos = q->pos;
	  while (p->next && p->next->pos <= pos)
	    p = p->next;
	  /* now p is the element that q must be inserted 'in'. */
	  pos2 = pos + q->len;
	  r = p;
	  while (r->next && r->next->pos < pos2)
	    r = r->next;
	  /* now r is the last element that q affects. */
	  if (p == r)
	    {
	      /* split p into at most 3 interval, and insert q in the place of
	         the second interval, set r to be the third part. */
	      /* third part needed? */
	      if (p->next && p->next->pos == pos2)
		r = r->next;
	      else
		{
		  r = new_type_link ();
		  *r = *p;
		  if (r->next)
		    {
		      r->next->prev = r;
		      r->len = r->next->pos - pos2;
		    }
		  else
		    r->len -= pos - p->pos;
		  r->pos = pos2;
		}
	      /* first part needed? */
	      if (p->prev && p->pos == pos)
		{
		  t = p;
		  p = p->prev;
		  free_type_link (t);
		}
	      else
		p->len = pos - p->pos;
	    }
	  else
	    {
	      /* cut the end of p. */
	      p->len = pos - p->pos;
	      /* if all of p is cut, remove it. */
	      if (!p->len && p->prev)
		p = p->prev;

	      /* cut the begining of r. */
	      r->pos = pos2;
	      if (r->next)
		r->len = r->next->pos - pos2;
	      /* if all of r is cut, remove it. */
	      if (!r->len && r->next)
		r = r->next;

	      /* remove the elements between p and r. */
	      for (s = p->next; s != r;)
		{
		  t = s;
		  s = s->next;
		  free_type_link (t);
		}
	    }
	  /* before updating the next and prev links to point to the inserted q,
	     we must remember the next element of q in the 'over' list.
	   */
	  t = q;
	  q = q->next;
	  p->next = t;
	  t->prev = p;
	  t->next = r;
	  r->prev = t;
	}
    }
}

/* Some convenience macros */
#define RL_TYPE(list) (list)->type
#define RL_LEN(list) (list)->len
#define RL_POS(list) (list)->pos
#define RL_LEVEL(list) (list)->level

static void
compact_list (TypeLink * list)
{
  if (list->next)
    {
      list = list->next;
      while (list)
	{
	  if (RL_TYPE (list->prev) == RL_TYPE (list)
	      && RL_LEVEL (list->prev) == RL_LEVEL (list))
	    {
	      TypeLink *next = list->next;
	      list->prev->next = list->next;
	      list->next->prev = list->prev;
	      RL_LEN (list->prev) += RL_LEN (list);
	      free_type_link (list);
	      list = next;
	    }
	  else
	    list = list->next;
	}
    }
}

static void
compact_neutrals (TypeLink * list)
{
  if (list->next)
    {
      list = list->next;
      while (list)
	{
	  if (RL_LEVEL (list->prev) == RL_LEVEL (list)
	      &&
	      ((RL_TYPE
		(list->prev) == RL_TYPE (list)
		|| FRIBIDI_IS_NEUTRAL (RL_TYPE (list->prev))
		&& FRIBIDI_IS_NEUTRAL (RL_TYPE (list)))))
	    {
	      TypeLink *next = list->next;
	      list->prev->next = list->next;
	      list->next->prev = list->prev;
	      RL_LEN (list->prev) += RL_LEN (list);
	      free_type_link (list);
	      list = next;
	    }
	  else
	    list = list->next;
	}
    }
}

/*=======================================================
// define macros for push and pop the status in to / out of the stack
//-------------------------------------------------------*/

/* There's some little points in pushing and poping into the status stack:
   1. when the embedding level is not valid (more than MAX_LEVEL=61),
   you must reject it, and not to push into the stack, but when you see a
   PDF, you must find the matching code, and if it was pushed in the stack,
   pop it, it means you must pop if and only if you have pushed the
   matching code, the over_pushed var counts the number of rejected codes yet.
   2. there's a more confusing point too, when the embedding level is exactly
   MAX_LEVEL-1=60, an LRO or LRE must be rejected because the new level would
   be MAX_LEVEL+1=62, that is invalid, but an RLO or RLE must be accepted
   because the new level is MAX_LEVEL=61, that is valid, so the rejected
   codes may be not continuous in the logical order, in fact there is at
   most two continuous intervals of codes, with a RLO or RLE between them.
   to support the case, the first_interval var counts the number of rejected
   codes in the first interval, when it is 0, means that there is only one
   interval yet.
*/

/* a. If this new level would be valid, then this embedding code is valid.
   Remember (push) the current embedding level and override status.
   Reset current level to this new level, and reset the override status to
   new_override.
   b. If the new level would not be valid, then this code is invalid. Don't
   change the current level or override status.
*/
#define PUSH_STATUS \
    { \
      if (new_level <= MAX_LEVEL) \
        { \
          if (level == MAX_LEVEL - 1) \
            first_interval = over_pushed; \
          status_stack->level = level; \
          status_stack->override = override; \
          status_stack++; \
          stack_size++; \
          level = new_level; \
          override = new_override; \
        } else \
	  over_pushed++; \
    }

/* If there was a valid matching code, restore (pop) the last remembered
   (pushed) embedding level and directional override.
*/
#define POP_STATUS \
    { \
      if (over_pushed || stack_size) \
      { \
        if (over_pushed > first_interval) \
          over_pushed--; \
        else \
          { \
            if (over_pushed == first_interval) \
              first_interval = 0; \
            status_stack--; \
            stack_size--; \
            level = status_stack->level; \
            override = status_stack->override; \
          } \
      } \
    }

/*==========================================================================
// There was no support for sor and eor in the absence of Explicit Embedding
// Levels, so define macros, to support them, with as less change as needed.
//--------------------------------------------------------------------------*/

/* Return the type of previous char or the sor, if already at the start of
   a run level. */
#define PREV_TYPE_OR_SOR(pp) \
    (RL_LEVEL(pp->prev)==RL_LEVEL(pp) ? RL_TYPE(pp->prev) : FRIBIDI_LEVEL_TO_DIR( \
      (RL_LEVEL(pp->prev)>RL_LEVEL(pp) ? RL_LEVEL(pp->prev) : RL_LEVEL(pp)) \
    ))

/* Return the type of next char or the eor, if already at the end of
   a run level. */
#define NEXT_TYPE_OR_EOR(pp) \
    (!pp->next ? FRIBIDI_LEVEL_TO_DIR(RL_LEVEL(pp)) : \
    (RL_LEVEL(pp->next)==RL_LEVEL(pp) ? RL_TYPE(pp->next) : FRIBIDI_LEVEL_TO_DIR( \
      (RL_LEVEL(pp->next)>RL_LEVEL(pp) ? RL_LEVEL(pp->next) : RL_LEVEL(pp)) \
    )))


/* Return the embedding direction of a link. */
#define FRIBIDI_EMBEDDING_DIRECTION(list) \
    FRIBIDI_LEVEL_TO_DIR(RL_LEVEL(list))

#ifdef DEBUG
/*======================================================================
//  For debugging, define some functions for printing the types and the
//  levels.
//----------------------------------------------------------------------*/

static guchar char_from_level_array[] = {
  'e',				/* FRIBIDI_LEVEL_REMOVED, internal error, this level shouldn't be viewed.  */
  '_',				/* FRIBIDI_LEVEL_START or _END, indicating start of string and end of string. */
  /* 0-9,A-F are the only valid levels in debug mode and before resolving
     implicits. after that the levels X, Y, Z may be appear too. */
  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  'A', 'B', 'C', 'D', 'E', 'F',
  'X', 'Y', 'Z',		/* only must appear after resolving implicits. */
  'o', 'o', 'o'			/* overflows, this levels and higher levels show a bug!. */
};

#define fribidi_char_from_level(level) char_from_level_array[(level) + 2]

static void
print_types_re (TypeLink * pp)
{
  fprintf (stderr, "Run types  : ");
  while (pp)
    {
      fprintf (stderr, "%d:l%d(%s)[%d] ",
	       pp->pos, pp->len, fribidi_type_name (pp->type), pp->level);
      pp = pp->next;
    }
  fprintf (stderr, "\n");
}

static void
print_resolved_levels (TypeLink * pp)
{
  fprintf (stderr, "Res. levels: ");
  while (pp)
    {
      gint i;
      for (i = 0; i < RL_LEN (pp); i++)
	fprintf (stderr, "%c", fribidi_char_from_level (RL_LEVEL (pp)));
      pp = pp->next;
    }
  fprintf (stderr, "\n");
}

static void
print_resolved_types (TypeLink * pp)
{
  fprintf (stderr, "Res. types : ");
  while (pp)
    {
      gint i;
      for (i = 0; i < RL_LEN (pp); i++)
	fprintf (stderr, "%c", fribidi_char_from_type (pp->type));
      pp = pp->next;
    }
  fprintf (stderr, "\n");
}

static void
print_bidi_string (FriBidiChar * str)
{
  gint i;
  fprintf (stderr, "Org. types : ");
  for (i = 0; i < bidi_string_strlen (str); i++)
    fprintf (stderr, "%c",
	     fribidi_char_from_type (_pango_fribidi_get_type (str[i])));
  fprintf (stderr, "\n");
}
#endif

/*======================================================================
//  This function should follow the Unicode specification closely!
//----------------------------------------------------------------------*/
static void
fribidi_analyse_string (	/* input */
			 FriBidiChar * str,
			 gint len, FriBidiCharType * pbase_dir,
			 /* output */
			 TypeLink ** ptype_rl_list, gint * pmax_level)
{
  gint base_level, base_dir;
  gint max_level;
  gint i;
  gint prev_last_strong, last_strong;
  FriBidiCharType *char_type;
  TypeLink *type_rl_list, *explicits_list, *explicits_list_end, *pp;

  /* Determinate character types */
  char_type = g_new (FriBidiCharType, len);
  for (i = 0; i < len; i++)
    char_type[i] = _pango_fribidi_get_type (str[i]);

  /* Run length encode the character types */
  type_rl_list = run_length_encode_types (char_type, len);
  g_free (char_type);

  init_list (&explicits_list, &explicits_list_end);

  /* Find the base level */
  if (FRIBIDI_IS_STRONG (*pbase_dir))
    base_level = FRIBIDI_DIR_TO_LEVEL (*pbase_dir);
  /* P2. P3. Search for first strong character and use its direction as
     base direction */
  else
    {
      base_level = 0;		/* Default */
      base_dir = FRIBIDI_TYPE_ON;
      for (pp = type_rl_list; pp; pp = pp->next)
	if (FRIBIDI_IS_LETTER (RL_TYPE (pp)))
	  {
	    base_level = FRIBIDI_DIR_TO_LEVEL (RL_TYPE (pp));
	    base_dir = FRIBIDI_LEVEL_TO_DIR (base_level);
	    break;
	  }

      /* If no strong base_dir was found, resort to the weak direction
         that was passed on input. */
      if (FRIBIDI_IS_NEUTRAL (base_dir))
	base_level = FRIBIDI_DIR_TO_LEVEL (*pbase_dir);
    }
  base_dir = FRIBIDI_LEVEL_TO_DIR (base_level);

#ifdef DEBUG
  if (fribidi_debug)
    {
      fprintf (stderr, "Base level : %c\n",
	       fribidi_char_from_level (base_level));
      fprintf (stderr, "Base dir   : %c\n",
	       fribidi_char_from_type (base_dir));
      print_types_re (type_rl_list);
    }
#endif

  /* Explicit Levels and Directions */
  DBG ("Explicit Levels and Directions.\n");
  {
    /* X1. Begin by setting the current embedding level to the paragraph
       embedding level. Set the directional override status to neutral.
       Process each character iteratively, applying rules X2 through X9.
       Only embedding levels from 0 to 61 are valid in this phase. */
    gint level = base_level;
    gint override = FRIBIDI_TYPE_ON;
    gint new_level, new_override;
    /* stack */
    gint stack_size = 0;
    gint over_pushed = 0;
    gint first_interval = 0;
    level_info *status_stack = g_new (level_info, MAX_LEVEL + 2);
    TypeLink *temp_link = new_type_link ();

    gint i;

    for (pp = type_rl_list->next; pp->next; pp = pp->next)
      {
	gint this_type = RL_TYPE (pp);
	if (FRIBIDI_IS_EXPLICIT_OR_BN (this_type))
	  {
	    if (FRIBIDI_IS_STRONG (this_type))
	      {			/* LRE, RLE, LRO, RLO */
		/* 1. Explicit Embeddings */
		/*   X2. With each RLE, compute the least greater odd embedding level. */
		/*   X3. With each LRE, compute the least greater even embedding level. */
		/* 2. Explicit Overrides */
		/*   X4. With each RLO, compute the least greater odd embedding level. */
		/*   X5. With each LRO, compute the least greater even embedding level. */
		new_override = FRIBIDI_EXPLICIT_TO_OVERRIDE_DIR (this_type);
		for (i = 0; i < RL_LEN (pp); i++)
		  {
		    new_level =
		      ((level + FRIBIDI_DIR_TO_LEVEL (this_type) + 2) & ~1) -
		      FRIBIDI_DIR_TO_LEVEL (this_type);
		    PUSH_STATUS;
		  }
	      }
	    else if (this_type == FRIBIDI_TYPE_PDF)
	      {
		/* 3. Terminating Embeddings and overrides */
		/*   X7. With each PDF, determine the matching embedding or
		   override code. */
		for (i = 0; i < RL_LEN (pp); i++)
		  POP_STATUS;
	      }
	    /* X9. Remove all RLE, LRE, RLO, LRO, PDF, and BN codes. */
	    /* Remove element and add it to explicits_list */
	    temp_link->next = pp->next;
	    pp->level = FRIBIDI_LEVEL_REMOVED;
	    move_element_before (pp, explicits_list_end);
	    pp = temp_link;
	  }
	else
	  {
	    /* X6. For all typed besides RLE, LRE, RLO, LRO, and PDF:
	       a. Set the level of the current character to the current
	       embedding level.
	       b. Whenever the directional override status is not neutral,
	       reset the current character type to the directional override
	       status. */
	    RL_LEVEL (pp) = level;
	    if (!FRIBIDI_IS_NEUTRAL (override))
	      RL_TYPE (pp) = override;
	  }
	/* X8. All explicit directional embeddings and overrides are
	   completely terminated at the end of each paragraph. Paragraph
	   separators are not included in the embedding. */
	/* This function is running on a single paragraph, so we can do
	   X8 after all the input is processed. */
      }

    /* Implementing X8. It has no effect on a single paragraph! */
    level = base_level;
    override = FRIBIDI_TYPE_ON;
    status_stack -= stack_size;
    stack_size = 0;
    over_pushed = 0;

    free_type_link (temp_link);
    g_free (status_stack);
  }
  /* X10. The remaining rules are applied to each run of characters at the
     same level. For each run, determine the start-of-level-run (sor) and
     end-of-level-run (eor) type, either L or R. This depends on the
     higher of the two levels on either side of the boundary (at the start
     or end of the paragraph, the level of the 'other' run is the base
     embedding level). If the higher level is odd, the type is R, otherwise
     it is L. */
  /* Resolving Implicit Levels can be done out of X10 loop, so only change
     of Resolving Weak Types and Resolving Neutral Types is needed. */

  compact_list (type_rl_list);

#ifdef DEBUG
  if (fribidi_debug)
    {
      print_types_re (type_rl_list);
      print_bidi_string (str);
      print_resolved_levels (type_rl_list);
      print_resolved_types (type_rl_list);
    }
#endif

  /* 4. Resolving weak types */
  DBG ("Resolving weak types.\n");
  {
    gint last_strong, prev_type_org, w4;

    last_strong = base_dir;

    for (pp = type_rl_list->next; pp->next; pp = pp->next)
      {
	gint prev_type, this_type, next_type;

	prev_type = PREV_TYPE_OR_SOR (pp);
	this_type = RL_TYPE (pp);
	next_type = NEXT_TYPE_OR_EOR (pp);

	if (FRIBIDI_IS_STRONG (prev_type))
	  last_strong = prev_type;

	/* W1. NSM
	   Examine each non-spacing mark (NSM) in the level run, and change the
	   type of the NSM to the type of the previous character. If the NSM
	   is at the start of the level run, it will get the type of sor. */
	if (this_type == FRIBIDI_TYPE_NSM)
	  {
	    RL_TYPE (pp) = prev_type;
	    continue;
	  }

	/* W2: European numbers. */
	if (this_type == FRIBIDI_TYPE_EN && last_strong == FRIBIDI_TYPE_AL)
	  {
	    RL_TYPE (pp) = FRIBIDI_TYPE_AN;

	    /* Resolving dependency of loops for rules W1 and W2, so we
	       can merge them in one loop. */
	    if (next_type == FRIBIDI_TYPE_NSM)
	      RL_TYPE (pp->next) = FRIBIDI_TYPE_AN;
	  }
      }


    last_strong = base_dir;
    /* Resolving dependency of loops for rules W4 and W5, W5 may
       want to prevent W4 to take effect in the next turn, do this 
       through "w4". */
    w4 = 1;
    /* Resolving dependency of loops for rules W4 and W5 with W7,
       W7 may change an EN to L but it sets the prev_type_org if needed,
       so W4 and W5 in next turn can still do their works. */
    prev_type_org = FRIBIDI_TYPE_ON;

    for (pp = type_rl_list->next; pp->next; pp = pp->next)
      {
	gint prev_type, this_type, next_type;

	prev_type = PREV_TYPE_OR_SOR (pp);
	this_type = RL_TYPE (pp);
	next_type = NEXT_TYPE_OR_EOR (pp);

	if (FRIBIDI_IS_STRONG (prev_type))
	  last_strong = prev_type;

	/* W3: Change ALs to R. */
	if (this_type == FRIBIDI_TYPE_AL)
	  {
	    RL_TYPE (pp) = FRIBIDI_TYPE_RTL;
	    w4 = 1;
	    prev_type_org = FRIBIDI_TYPE_ON;
	    continue;
	  }

	/* W4. A single european separator changes to a european number.
	   A single common separator between two numbers of the same type
	   changes to that type. */
	if (w4
	    && RL_LEN (pp) == 1 && FRIBIDI_IS_ES_OR_CS (this_type)
	    && FRIBIDI_IS_NUMBER (prev_type_org) && prev_type_org == next_type
	    && (prev_type_org == FRIBIDI_TYPE_EN
		|| this_type == FRIBIDI_TYPE_CS))
	  {
	    RL_TYPE (pp) = prev_type;
	    this_type = RL_TYPE (pp);
	  }
	w4 = 1;

	/* W5. A sequence of European terminators adjacent to European
	   numbers changes to All European numbers. */
	if (this_type == FRIBIDI_TYPE_ET
	    && (prev_type_org == FRIBIDI_TYPE_EN
		|| next_type == FRIBIDI_TYPE_EN))
	  {
	    RL_TYPE (pp) = FRIBIDI_TYPE_EN;
	    w4 = 0;
	    this_type = RL_TYPE (pp);
	  }

	/* W6. Otherwise change separators and terminators to other neutral. */
	if (FRIBIDI_IS_NUMBER_SEPARATOR_OR_TERMINATOR (this_type))
	  RL_TYPE (pp) = FRIBIDI_TYPE_ON;

	/* W7. Change european numbers to L. */
	if (this_type == FRIBIDI_TYPE_EN && last_strong == FRIBIDI_TYPE_LTR)
	  {
	    RL_TYPE (pp) = FRIBIDI_TYPE_LTR;
	    prev_type_org = (RL_LEVEL (pp) == RL_LEVEL (pp->next) ?
			     FRIBIDI_TYPE_EN : FRIBIDI_TYPE_ON);
	  }
	else
	  prev_type_org = PREV_TYPE_OR_SOR (pp->next);
      }
  }

  compact_neutrals (type_rl_list);

#ifdef DEBUG
  if (fribidi_debug)
    {
      print_resolved_levels (type_rl_list);
      print_resolved_types (type_rl_list);
    }
#endif

  /* 5. Resolving Neutral Types */
  DBG ("Resolving neutral types.\n");
  {
    gint prev_type, next_type;

    /* N1. and N2.
       For each neutral, resolve it. */
    for (pp = type_rl_list->next; pp->next; pp = pp->next)
      {
	gint prev_type, this_type, next_type;

	/* "European and arabic numbers are treated as though they were R"
	   FRIBIDI_CHANGE_NUMBER_TO_RTL does this. */
	this_type = FRIBIDI_CHANGE_NUMBER_TO_RTL (RL_TYPE (pp));
	prev_type = FRIBIDI_CHANGE_NUMBER_TO_RTL (PREV_TYPE_OR_SOR (pp));
	next_type = FRIBIDI_CHANGE_NUMBER_TO_RTL (NEXT_TYPE_OR_EOR (pp));

	if (FRIBIDI_IS_NEUTRAL (this_type))
	  RL_TYPE (pp) = (prev_type == next_type) ?
	    /* N1. */ prev_type :
	    /* N2. */ FRIBIDI_EMBEDDING_DIRECTION (pp);
      }
  }

  compact_list (type_rl_list);

#ifdef DEBUG
  if (fribidi_debug)
    {
      print_resolved_levels (type_rl_list);
      print_resolved_types (type_rl_list);
    }
#endif

  /* 6. Resolving implicit levels */
  DBG ("Resolving implicit levels.\n");
  {
    max_level = base_level;

    for (pp = type_rl_list->next; pp->next; pp = pp->next)
      {
	gint this_type, level;

	this_type = RL_TYPE (pp);
	level = RL_LEVEL (pp);

	/* I1. Even */
	/* I2. Odd */
	if (FRIBIDI_IS_NUMBER (this_type))
	  RL_LEVEL (pp) = (RL_LEVEL (pp) + 2) & ~1;
	else
	  RL_LEVEL (pp) = (RL_LEVEL (pp) ^ FRIBIDI_DIR_TO_LEVEL (this_type)) +
	    (RL_LEVEL (pp) & 1);

	if (RL_LEVEL (pp) > max_level)
	  max_level = RL_LEVEL (pp);
      }
  }

  compact_list (type_rl_list);

#ifdef DEBUG
  if (fribidi_debug)
    {
      print_bidi_string (str);
      print_resolved_levels (type_rl_list);
      print_resolved_types (type_rl_list);
    }
#endif

/* Reinsert the explicit codes & bn's that already removed, from the
   explicits_list to type_rl_list. */
  DBG ("Reinserting explicit codes.\n");
  {
    TypeLink *p;

    override_list (type_rl_list, explicits_list);
    p = type_rl_list->next;
    if (p->level < 0)
      p->level = base_level;
    for (; p->next; p = p->next)
      if (p->level < 0)
	p->level = p->prev->level;
  }

#ifdef DEBUG
  if (fribidi_debug)
    {
      print_types_re (type_rl_list);
      print_resolved_levels (type_rl_list);
      print_resolved_types (type_rl_list);
    }
#endif

  DBG ("Reset the embedding levels.\n");
  {
    gint j, k, state, pos;
    TypeLink *p, *q, *list, *list_end;

    /* L1. Reset the embedding levels of some chars. */
    init_list (&list, &list_end);
    q = list_end;
    state = 1;
    pos = len - 1;
    for (j = len - 1; j >= 0; j--)
      {
	k = _pango_fribidi_get_type (str[j]);
	if (!state && FRIBIDI_IS_SEPARATOR (k))
	  {
	    state = 1;
	    pos = j;
	  }
	else
	  if (state
	      && !(j && FRIBIDI_IS_EXPLICIT_OR_SEPARATOR_OR_BN_OR_WS (k)))
	  {
	    /* if state is on at the very first of string, do this too. */
	    if (!j)
	      j--;
	    state = 0;
	    p = new_type_link ();
	    p->prev = p->next = 0;
	    p->pos = j + 1;
	    p->len = pos - j;
	    p->type = base_dir;
	    p->level = base_level;
	    move_element_before (p, q);
	    q = p;
	  }
      }
    override_list (type_rl_list, list);
  }

#ifdef DEBUG
  if (fribidi_debug)
    {
      print_types_re (type_rl_list);
      print_resolved_levels (type_rl_list);
      print_resolved_types (type_rl_list);
    }
#endif

  *ptype_rl_list = type_rl_list;
  *pmax_level = max_level;
  *pbase_dir = base_dir;
}

/*======================================================================
//  Frees up the rl_list, must be called after each call to
//  fribidi_analyse_string(), after the list is not needed anymore.
//----------------------------------------------------------------------*/
void
free_rl_list (TypeLink * type_rl_list)
{

  TypeLink *p, *pp;
  if (!type_rl_list)
    return;
#ifdef USE_SIMPLE_MALLOC
  for (pp = type_rl_list; pp; pp = pp->next)
    {
      p = pp->next;
      g_free (pp);
    };
#else
  for (pp = type_rl_list->next; pp->next; pp = pp->next)
    /*Nothing */ ;
  pp->next = free_type_links;
  free_type_links = type_rl_list;
  type_rl_list = NULL;
#endif
}

static gboolean mirroring = TRUE;

gboolean fribidi_mirroring_status (void)
{
  return mirroring;
}

void
fribidi_set_mirroring (gboolean mirror)
{
  mirroring = mirror;
}

/*======================================================================
//  Here starts the exposed front end functions.
//----------------------------------------------------------------------*/

/*======================================================================
//  fribidi_embedding_levels() is used in order to just get the
//  embedding levels.
//----------------------------------------------------------------------*/
void
pango_log2vis_get_embedding_levels (
				       /* input */
				       gunichar * str,
				       int len,
				       PangoDirection *pbase_dir,
				       /* output */
				       guint8 * embedding_level_list)
{
  TypeLink *type_rl_list, *pp;
  gint max_level;
  FriBidiCharType fribidi_base_dir;
 
  fribidi_base_dir = (*pbase_dir == PANGO_DIRECTION_LTR) ? FRIBIDI_TYPE_L : FRIBIDI_TYPE_R;
    
  if (len == 0)
    return;

  fribidi_analyse_string (str, len, &fribidi_base_dir,
			  /* output */
			  &type_rl_list, &max_level);

  for (pp = type_rl_list->next; pp->next; pp = pp->next)
    {
      gint i;
      gint pos = RL_POS (pp);
      gint len = RL_LEN (pp);
      gint level = RL_LEVEL (pp);
      for (i = 0; i < len; i++)
	embedding_level_list[pos + i] = level;
    }

  free_rl_list (type_rl_list);
   
  *pbase_dir = (fribidi_base_dir == FRIBIDI_TYPE_L) ?  PANGO_DIRECTION_LTR : PANGO_DIRECTION_RTL;
}