2 * fribidi.c - Unicode bidirectional and Arabic joining/shaping algorithms
5 * Behdad Esfahbod, 2001, 2002, 2004
6 * Dov Grobgeld, 1999, 2000
8 * Copyright (C) 2004 Sharif FarsiWeb, Inc
9 * Copyright (C) 2001,2002 Behdad Esfahbod
10 * Copyright (C) 1999,2000 Dov Grobgeld
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.
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.
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
27 * For licensing issues, contact <fribidi.license@gmail.com>.
35 static int flag_debug = false;
38 FRIBIDI_ENTRY fribidi_boolean
39 fribidi_debug_status (
50 FRIBIDI_ENTRY fribidi_boolean
57 return flag_debug = state;
63 FRIBIDI_ENTRY FriBidiStrIndex
64 fribidi_remove_bidi_marks (
66 const FriBidiStrIndex len,
67 FriBidiStrIndex *positions_to_this,
68 FriBidiStrIndex *position_from_this_list,
69 FriBidiLevel *embedding_levels
72 register FriBidiStrIndex i, j = 0;
73 fribidi_boolean private_from_this = false;
74 fribidi_boolean status = false;
83 DBG ("in fribidi_remove_bidi_marks");
87 /* If to_this is not NULL, we must have from_this as well. If it is
88 not given by the caller, we have to make a private instance of it. */
89 if (positions_to_this && !position_from_this_list)
91 position_from_this_list = fribidi_malloc (sizeof
92 (position_from_this_list[0]) *
95 (!position_from_this_list) goto out;
96 private_from_this = true;
97 for (i = 0; i < len; i++)
98 position_from_this_list[positions_to_this[i]] = i;
101 for (i = 0; i < len; i++)
102 if (!FRIBIDI_IS_EXPLICIT_OR_BN (fribidi_get_bidi_type (str[i]))
103 && !FRIBIDI_IS_ISOLATE (fribidi_get_bidi_type (str[i]))
104 && str[i] != FRIBIDI_CHAR_LRM && str[i] != FRIBIDI_CHAR_RLM)
107 if (embedding_levels)
108 embedding_levels[j] = embedding_levels[i];
109 if (position_from_this_list)
110 position_from_this_list[j] = position_from_this_list[i];
114 /* Convert the from_this list to to_this */
115 if (positions_to_this)
117 for (i = 0; i < len; i++)
118 positions_to_this[i] = -1;
119 for (i = 0; i < len; i++)
120 positions_to_this[position_from_this_list[i]] = i;
127 if (private_from_this)
128 fribidi_free (position_from_this_list);
130 return status ? j : -1;
133 /* Local array size, used for stack-based local arrays */
134 #define LOCAL_LIST_SIZE 128
135 static FriBidiFlags flags = FRIBIDI_FLAGS_DEFAULT | FRIBIDI_FLAGS_ARABIC;
138 FRIBIDI_ENTRY FriBidiLevel
141 const FriBidiChar *str,
142 const FriBidiStrIndex len,
143 /* input and output */
144 FriBidiParType *pbase_dir,
146 FriBidiChar *visual_str,
147 FriBidiStrIndex *positions_L_to_V,
148 FriBidiStrIndex *positions_V_to_L,
149 FriBidiLevel *embedding_levels
152 register FriBidiStrIndex i;
153 FriBidiLevel max_level = 0;
154 fribidi_boolean private_V_to_L = false;
155 fribidi_boolean private_embedding_levels = false;
156 fribidi_boolean status = false;
157 FriBidiArabicProp local_ar_props[LOCAL_LIST_SIZE];
158 FriBidiArabicProp *ar_props = NULL;
159 FriBidiLevel local_embedding_levels[LOCAL_LIST_SIZE];
160 FriBidiCharType local_bidi_types[LOCAL_LIST_SIZE];
161 FriBidiCharType *bidi_types = NULL;
162 FriBidiBracketType local_bracket_types[LOCAL_LIST_SIZE];
163 FriBidiBracketType *bracket_types = NULL;
164 FriBidiStrIndex local_positions_V_to_L[LOCAL_LIST_SIZE];
173 DBG ("in fribidi_log2vis");
175 fribidi_assert (str);
176 fribidi_assert (pbase_dir);
178 if (len < LOCAL_LIST_SIZE)
179 bidi_types = local_bidi_types;
181 bidi_types = fribidi_malloc (len * sizeof bidi_types[0]);
185 fribidi_get_bidi_types (str, len, bidi_types);
187 if (len < LOCAL_LIST_SIZE)
188 bracket_types = local_bracket_types;
190 bracket_types = fribidi_malloc (len * sizeof bracket_types[0]);
195 fribidi_get_bracket_types (str, len, bidi_types,
198 if (!embedding_levels)
200 if (len < LOCAL_LIST_SIZE)
201 embedding_levels = local_embedding_levels;
203 embedding_levels = fribidi_malloc (len * sizeof embedding_levels[0]);
204 if (!embedding_levels)
206 private_embedding_levels = true;
209 max_level = fribidi_get_par_embedding_levels_ex (bidi_types,
213 embedding_levels) - 1;
215 (max_level < 0) goto out;
217 /* If l2v is to be calculated we must have v2l as well. If it is not
218 given by the caller, we have to make a private instance of it. */
219 if (positions_L_to_V && !positions_V_to_L)
221 if (len < LOCAL_LIST_SIZE)
222 positions_V_to_L = local_positions_V_to_L;
225 (FriBidiStrIndex *) fribidi_malloc (sizeof (FriBidiStrIndex) * len);
226 if (!positions_V_to_L)
228 private_V_to_L = true;
231 /* Set up the ordering array to identity order */
232 if (positions_V_to_L)
234 for (i = 0; i < len; i++)
235 positions_V_to_L[i] = i;
241 /* Using memcpy instead
242 for (i = len - 1; i >= 0; i--)
243 visual_str[i] = str[i];
245 memcpy (visual_str, str, len * sizeof (*visual_str));
248 if (len < LOCAL_LIST_SIZE)
249 ar_props = local_ar_props;
251 ar_props = fribidi_malloc (len * sizeof ar_props[0]);
252 fribidi_get_joining_types (str, len, ar_props);
253 fribidi_join_arabic (bidi_types, len, embedding_levels, ar_props);
255 fribidi_shape (flags, embedding_levels, len, ar_props, visual_str);
258 /* line breaking goes here, but we assume one line in this function */
260 /* and this should be called once per line, but again, we assume one
261 * line in this deprecated function */
263 fribidi_reorder_line (flags, bidi_types, len, 0, *pbase_dir,
264 embedding_levels, visual_str,
267 /* Convert the v2l list to l2v */
268 if (positions_L_to_V)
270 for (i = 0; i < len; i++)
271 positions_L_to_V[i] = -1;
272 for (i = 0; i < len; i++)
273 positions_L_to_V[positions_V_to_L[i]] = i;
278 if (private_V_to_L && positions_V_to_L != local_positions_V_to_L)
279 fribidi_free (positions_V_to_L);
281 if (private_embedding_levels && embedding_levels != local_embedding_levels)
282 fribidi_free (embedding_levels);
284 if (ar_props && ar_props != local_ar_props)
285 fribidi_free (ar_props);
287 if (bidi_types && bidi_types != local_bidi_types)
288 fribidi_free (bidi_types);
290 if (bracket_types && bracket_types != local_bracket_types)
291 fribidi_free (bracket_types);
293 return status ? max_level + 1 : 0;
296 const char *fribidi_unicode_version = FRIBIDI_UNICODE_VERSION;
298 const char *fribidi_version_info =
299 "(" FRIBIDI_NAME ") " FRIBIDI_VERSION "\n"
300 "interface version " FRIBIDI_INTERFACE_VERSION_STRING ",\n"
301 "Unicode Character Database version " FRIBIDI_UNICODE_VERSION ",\n"
307 "Copyright (C) 2004 Sharif FarsiWeb, Inc.\n"
308 "Copyright (C) 2001, 2002, 2004, 2005 Behdad Esfahbod\n"
309 "Copyright (C) 1999, 2000, 2017, 2018, 2019 Dov Grobgeld\n"
310 FRIBIDI_NAME " comes with NO WARRANTY, to the extent permitted by law.\n"
311 "You may redistribute copies of " FRIBIDI_NAME " under\n"
312 "the terms of the GNU Lesser General Public License.\n"
313 "For more information about these matters, see the file named COPYING.\n\n"
314 "Written by Behdad Esfahbod and Dov Grobgeld.\n";
316 /* Editor directions:
317 * vim:textwidth=78:tabstop=8:shiftwidth=2:autoindent:cindent