Imported Upstream version 1.0.5
[platform/upstream/fribidi.git] / lib / fribidi-arabic.c
1 /* fribidi-arabic.c - Arabic shaping
2  *
3  * Copyright (C) 2005  Behdad Esfahbod
4  *
5  * This file is part of GNU FriBidi.
6  * 
7  * GNU FriBidi is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  * 
12  * GNU FriBidi is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Lesser General Public License for more details.
16  * 
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with GNU FriBidi; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20  * 
21  * For licensing issues, contact <fribidi.license@gmail.com> or write to
22  * Sharif FarsiWeb, Inc., PO Box 13445-389, Tehran, Iran.
23  *
24  * Author(s):
25  *   Behdad Esfahbod, 2005
26  */
27
28 #include "common.h"
29
30 #ifdef HAVE_STDLIB_H
31 # include <stdlib.h>
32 #endif
33
34
35 #include <fribidi-arabic.h>
36 #include <fribidi-unicode.h>
37
38
39 typedef struct _PairMap {
40   FriBidiChar pair[2], to;
41 } PairMap;
42
43
44 #define FRIBIDI_ACCESS_SHAPE_TABLE(table,min,max,x,shape) (table), (min), (max)
45 # define FRIBIDI_ACCESS_SHAPE_TABLE_REAL(table,min,max,x,shape) \
46         (((x)<(min)||(x)>(max))?(x):(table)[(x)-(min)][(shape)])
47
48 #include "arabic-shaping.tab.i"
49 #include "arabic-misc.tab.i"
50
51
52 static void
53 fribidi_shape_arabic_joining (
54   /* input */
55   const FriBidiChar table[][4],
56   FriBidiChar min,
57   FriBidiChar max,
58   const FriBidiStrIndex len,
59   const FriBidiArabicProp *ar_props,
60   /* input and output */
61   FriBidiChar *str
62 )
63 {
64   register FriBidiStrIndex i;
65
66   for (i = 0; i < len; i++)
67     if (FRIBIDI_ARAB_SHAPES(ar_props[i]))
68       str[i] = FRIBIDI_ACCESS_SHAPE_TABLE_REAL (table, min, max, str[i], FRIBIDI_JOIN_SHAPE (ar_props[i]));
69 }
70
71
72
73 static int
74 comp_PairMap (const void *pa, const void *pb)
75 {
76   PairMap *a = (PairMap *)pa;
77   PairMap *b = (PairMap *)pb;
78
79   if (a->pair[0] != b->pair[0])
80     return a->pair[0] < b->pair[0] ? -1 : +1;
81   else
82     return a->pair[1] < b->pair[1] ? -1 :
83            a->pair[1] > b->pair[1] ? +1 :
84            0;
85 }
86
87
88 static FriBidiChar
89 find_pair_match (const PairMap *table, int size, FriBidiChar first, FriBidiChar second)
90 {
91   PairMap *match;
92   PairMap x;
93   x.pair[0] = first;
94   x.pair[1] = second;
95   x.to = 0;
96   match = bsearch (&x, table, size, sizeof (table[0]), comp_PairMap);
97   return match ? match->to : 0;
98 }
99
100 #define PAIR_MATCH(table,len,first,second) \
101         ((first)<(table[0].pair[0])||(first)>(table[len-1].pair[0])?0: \
102          find_pair_match(table, len, first, second))
103
104 static void
105 fribidi_shape_arabic_ligature (
106   /* input */
107   const PairMap *table,
108   int size,
109   const FriBidiLevel *embedding_levels,
110   const FriBidiStrIndex len,
111   /* input and output */
112   FriBidiArabicProp *ar_props,
113   FriBidiChar *str
114 )
115 {
116   /* TODO: This doesn't form ligatures for even-level Arabic text.
117    * no big problem though. */
118   register FriBidiStrIndex i;
119
120   for (i = 0; i < len - 1; i++) {
121     register FriBidiChar c;
122     if (FRIBIDI_LEVEL_IS_RTL(embedding_levels[i]) &&
123         embedding_levels[i] == embedding_levels[i+1] &&
124         (c = PAIR_MATCH(table, size, str[i], str[i+1])))
125       {
126         str[i] = FRIBIDI_CHAR_FILL;
127         FRIBIDI_SET_BITS(ar_props[i], FRIBIDI_MASK_LIGATURED);
128         str[i+1] = c;
129       }
130   }
131 }
132
133 #define DO_LIGATURING(table, levels, len, ar_props, str) \
134         fribidi_shape_arabic_ligature ((table), sizeof(table)/sizeof((table)[0]), levels, len, ar_props, str)
135
136 #define DO_SHAPING(tablemacro, len, ar_props, str) \
137         fribidi_shape_arabic_joining (tablemacro(,), len, ar_props, str);
138         
139
140
141
142 FRIBIDI_ENTRY void
143 fribidi_shape_arabic (
144   /* input */
145   FriBidiFlags flags,
146   const FriBidiLevel *embedding_levels,
147   const FriBidiStrIndex len,
148   /* input and output */
149   FriBidiArabicProp *ar_props,
150   FriBidiChar *str
151 )
152 {
153   DBG ("in fribidi_shape_arabic");
154
155   if UNLIKELY
156     (len == 0 || !str) return;
157
158   DBG ("in fribidi_shape");
159
160   fribidi_assert (ar_props);
161
162   if (FRIBIDI_TEST_BITS (flags, FRIBIDI_FLAG_SHAPE_ARAB_PRES))
163     {
164       DO_SHAPING (FRIBIDI_GET_ARABIC_SHAPE_PRES, len, ar_props, str);
165     }
166
167   if (FRIBIDI_TEST_BITS (flags, FRIBIDI_FLAG_SHAPE_ARAB_LIGA))
168     {
169       DO_LIGATURING (mandatory_liga_table, embedding_levels, len, ar_props, str);
170     }
171
172   if (FRIBIDI_TEST_BITS (flags, FRIBIDI_FLAG_SHAPE_ARAB_CONSOLE))
173     {
174       DO_LIGATURING (console_liga_table, embedding_levels, len, ar_props, str);
175       DO_SHAPING (FRIBIDI_GET_ARABIC_SHAPE_NSM, len, ar_props, str);
176     }
177 }
178
179 /* Editor directions:
180  * Local Variables:
181  *   mode: c
182  *   c-basic-offset: 2
183  *   indent-tabs-mode: t
184  *   tab-width: 8
185  * End:
186  * vim: textwidth=78: autoindent: cindent: shiftwidth=2: tabstop=8:
187  */