Imported Upstream version 1.0.2
[platform/upstream/fribidi.git] / lib / fribidi-deprecated.c
1 /* FriBidi
2  * fribidi-deprecated.c - deprecated interfaces.
3  *
4  * Authors:
5  *   Behdad Esfahbod, 2001, 2002, 2004
6  *   Dov Grobgeld, 1999, 2000
7  *
8  * Copyright (C) 2004 Sharif FarsiWeb, Inc
9  * Copyright (C) 2001,2002 Behdad Esfahbod
10  * Copyright (C) 1999,2000 Dov Grobgeld
11  * 
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2.1 of the License, or (at your option) any later version.
16  * 
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  * 
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this library, in a file named COPYING; if not, write to the
24  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25  * Boston, MA 02110-1301, USA
26  * 
27  * For licensing issues, contact <fribidi.license@gmail.com>.
28  */
29
30 #include "common.h"
31
32 #undef FRIBIDI_NO_DEPRECATED
33
34 #include <fribidi-deprecated.h>
35 #include <fribidi.h>
36
37 #ifdef FRIBIDI_NO_DEPRECATED
38 #else
39
40 static FriBidiFlags flags = FRIBIDI_FLAGS_DEFAULT | FRIBIDI_FLAGS_ARABIC;
41
42 FRIBIDI_ENTRY fribidi_boolean
43 fribidi_set_mirroring (
44   /* input */
45   fribidi_boolean state
46 )
47 {
48   return FRIBIDI_ADJUST_AND_TEST_BITS (flags, FRIBIDI_FLAG_SHAPE_MIRRORING, state);
49 }
50
51 FRIBIDI_ENTRY fribidi_boolean
52 fribidi_mirroring_status (
53   void
54 )
55 {
56   return FRIBIDI_TEST_BITS (flags, FRIBIDI_FLAG_SHAPE_MIRRORING);
57 }
58
59 FRIBIDI_ENTRY fribidi_boolean
60 fribidi_set_reorder_nsm (
61   /* input */
62   fribidi_boolean state
63 )
64 {
65   return FRIBIDI_ADJUST_AND_TEST_BITS (flags, FRIBIDI_FLAG_REORDER_NSM, state);
66 }
67
68 fribidi_boolean
69 fribidi_reorder_nsm_status (
70   void
71 )
72 {
73   return FRIBIDI_TEST_BITS (flags, FRIBIDI_FLAG_REORDER_NSM);
74 }
75
76
77
78
79 FRIBIDI_ENTRY FriBidiLevel
80 fribidi_log2vis_get_embedding_levels (
81   const FriBidiCharType *bidi_types,    /* input list of bidi types as returned by
82                                            fribidi_get_bidi_types() */
83   const FriBidiStrIndex len,    /* input string length of the paragraph */
84   FriBidiParType *pbase_dir,    /* requested and resolved paragraph
85                                  * base direction */
86   FriBidiLevel *embedding_levels        /* output list of embedding levels */
87 )
88 {
89   return fribidi_get_par_embedding_levels_ex (bidi_types, NULL, len, pbase_dir, embedding_levels);
90 }
91
92 FRIBIDI_ENTRY FriBidiCharType
93 fribidi_get_type (
94   FriBidiChar ch                /* input character */
95 )
96 {
97   return fribidi_get_bidi_type (ch);
98 }
99
100 FRIBIDI_ENTRY FriBidiCharType
101 fribidi_get_type_internal (
102   FriBidiChar ch                /* input character */
103 )
104 {
105   return fribidi_get_bidi_type (ch);
106 }
107
108
109
110 FRIBIDI_ENTRY FriBidiStrIndex
111 fribidi_remove_bidi_marks (
112   FriBidiChar *str,
113   const FriBidiStrIndex len,
114   FriBidiStrIndex *positions_to_this,
115   FriBidiStrIndex *position_from_this_list,
116   FriBidiLevel *embedding_levels
117 )
118 {
119   register FriBidiStrIndex i, j = 0;
120   fribidi_boolean private_from_this = false;
121   fribidi_boolean status = false;
122
123   if UNLIKELY
124     (len == 0)
125     {
126       status = true;
127       goto out;
128     }
129
130   DBG ("in fribidi_remove_bidi_marks");
131
132   fribidi_assert (str);
133
134   /* If to_this is not NULL, we must have from_this as well. If it is
135      not given by the caller, we have to make a private instance of it. */
136   if (positions_to_this && !position_from_this_list)
137     {
138       position_from_this_list = fribidi_malloc (sizeof
139                                                 (position_from_this_list[0]) *
140                                                 len);
141       if UNLIKELY
142         (!position_from_this_list) goto out;
143       private_from_this = true;
144       for (i = 0; i < len; i++)
145         position_from_this_list[positions_to_this[i]] = i;
146     }
147
148   for (i = 0; i < len; i++)
149     if (!FRIBIDI_IS_EXPLICIT_OR_BN (fribidi_get_bidi_type (str[i]))
150         && !FRIBIDI_IS_ISOLATE (fribidi_get_bidi_type (str[i]))
151         && str[i] != FRIBIDI_CHAR_LRM && str[i] != FRIBIDI_CHAR_RLM)
152       {
153         str[j] = str[i];
154         if (embedding_levels)
155           embedding_levels[j] = embedding_levels[i];
156         if (position_from_this_list)
157           position_from_this_list[j] = position_from_this_list[i];
158         j++;
159       }
160
161   /* Convert the from_this list to to_this */
162   if (positions_to_this)
163     {
164       for (i = 0; i < len; i++)
165         positions_to_this[i] = -1;
166       for (i = 0; i < len; i++)
167         positions_to_this[position_from_this_list[i]] = i;
168     }
169
170   status = true;
171
172 out:
173
174   if (private_from_this)
175     fribidi_free (position_from_this_list);
176
177   return status ? j : -1;
178 }
179
180
181
182 FRIBIDI_ENTRY FriBidiLevel
183 fribidi_log2vis (
184   /* input */
185   const FriBidiChar *str,
186   FriBidiStrIndex len,
187   /* input and output */
188   FriBidiParType *pbase_dir,
189   /* output */
190   FriBidiChar *visual_str,
191   FriBidiStrIndex *positions_L_to_V,
192   FriBidiStrIndex *positions_V_to_L,
193   FriBidiLevel *embedding_levels
194 )
195 {
196   register FriBidiStrIndex i;
197   FriBidiLevel max_level = 0;
198   fribidi_boolean private_V_to_L = false;
199   fribidi_boolean private_embedding_levels = false;
200   fribidi_boolean status = false;
201   FriBidiArabicProp *ar_props = NULL;
202   FriBidiCharType *bidi_types = NULL;
203   FriBidiBracketType *bracket_types = NULL;
204
205   if UNLIKELY
206     (len == 0)
207     {
208       status = true;
209       goto out;
210     }
211
212   DBG ("in fribidi_log2vis");
213
214   fribidi_assert (str);
215   fribidi_assert (pbase_dir);
216
217   bidi_types = fribidi_malloc (len * sizeof bidi_types[0]);
218   if (!bidi_types)
219     goto out;
220
221   fribidi_get_bidi_types (str, len, bidi_types);
222
223   bracket_types = fribidi_malloc (len * sizeof bracket_types[0]);
224   if (!bracket_types)
225     goto out;
226
227   fribidi_get_bracket_types (str, len, bidi_types,
228                              /* output */
229                              bracket_types);
230   if (!embedding_levels)
231     {
232       embedding_levels = fribidi_malloc (len * sizeof embedding_levels[0]);
233       if (!embedding_levels)
234         goto out;
235       private_embedding_levels = true;
236     }
237
238   max_level = fribidi_get_par_embedding_levels_ex (bidi_types,
239                                                    bracket_types,
240                                                    len,
241                                                    pbase_dir,
242                                                    embedding_levels) - 1;
243   if UNLIKELY
244     (max_level < 0) goto out;
245
246   /* If l2v is to be calculated we must have v2l as well. If it is not
247      given by the caller, we have to make a private instance of it. */
248   if (positions_L_to_V && !positions_V_to_L)
249     {
250       positions_V_to_L =
251         (FriBidiStrIndex *) fribidi_malloc (sizeof (FriBidiStrIndex) * len);
252       if (!positions_V_to_L)
253         goto out;
254       private_V_to_L = true;
255     }
256
257   /* Set up the ordering array to identity order */
258   if (positions_V_to_L)
259     {
260       for (i = 0; i < len; i++)
261         positions_V_to_L[i] = i;
262     }
263
264
265   if (visual_str)
266     {
267       /* Using memcpy instead
268       for (i = len - 1; i >= 0; i--)
269         visual_str[i] = str[i];
270       */
271       memcpy (visual_str, str, len * sizeof (*visual_str));
272
273       /* Arabic joining */
274       ar_props = fribidi_malloc (len * sizeof ar_props[0]);
275       fribidi_get_joining_types (str, len, ar_props);
276       fribidi_join_arabic (bidi_types, len, embedding_levels, ar_props);
277
278       fribidi_shape (flags, embedding_levels, len, ar_props, visual_str);
279     }
280
281   /* line breaking goes here, but we assume one line in this function */
282
283   /* and this should be called once per line, but again, we assume one
284    * line in this deprecated function */
285   status =
286     fribidi_reorder_line (flags, bidi_types, len, 0, *pbase_dir,
287                           embedding_levels, visual_str,
288                           positions_V_to_L);
289
290   /* Convert the v2l list to l2v */
291   if (positions_L_to_V)
292     {
293       for (i = 0; i < len; i++)
294         positions_L_to_V[i] = -1;
295       for (i = 0; i < len; i++)
296         positions_L_to_V[positions_V_to_L[i]] = i;
297     }
298
299 out:
300
301   if (private_V_to_L)
302     fribidi_free (positions_V_to_L);
303
304   if (private_embedding_levels)
305     fribidi_free (embedding_levels);
306
307   if (ar_props)
308     fribidi_free (ar_props);
309
310   if (bidi_types)
311     fribidi_free (bidi_types);
312
313   if (bracket_types)
314     fribidi_free (bracket_types);
315
316   return status ? max_level + 1 : 0;
317 }
318
319 FRIBIDI_ENTRY FriBidiLevel
320 fribidi_get_par_embedding_levels (
321   /* input */
322   const FriBidiCharType *bidi_types,
323   const FriBidiStrIndex len,
324   /* input and output */
325   FriBidiParType *pbase_dir,
326   /* output */
327   FriBidiLevel *embedding_levels
328 )
329 {
330   return fribidi_get_par_embedding_levels_ex (/* input */
331                                               bidi_types,
332                                               NULL, /* No bracket_types */
333                                               len,
334                                               /* input and output */
335                                               pbase_dir,
336                                               /* output */
337                                               embedding_levels);
338 }
339
340 #endif /* !FRIBIDI_NO_DEPRECATED */
341
342 /* Editor directions:
343  * vim:textwidth=78:tabstop=8:shiftwidth=2:autoindent:cindent
344  */