xref: /vim-8.2.3635/src/arabic.c (revision 12ee7ff0)
1 /* vi:set ts=8 sts=4 sw=4 noet:
2  *
3  * VIM - Vi IMproved    by Bram Moolenaar
4  *
5  * Do ":help uganda"  in Vim to read copying and usage conditions.
6  * Do ":help credits" in Vim to see a list of people who contributed.
7  * See README.txt for an overview of the Vim source code.
8  */
9 
10 /*
11  * arabic.c: functions for Arabic language
12  *
13  * Author: Nadim Shaikli & Isam Bayazidi
14  * Farsi support and restructuring to make adding new letters easier by Ali
15  * Gholami Rudi.  Further work by Ameretat Reith.
16  */
17 
18 /*
19  * Sorted list of unicode Arabic characters.  Each entry holds the
20  * presentation forms of a letter.
21  *
22  * Arabic characters are categorized into following types:
23  *
24  * Isolated	- iso-8859-6 form
25  * Initial	- unicode form-B start
26  * Medial	- unicode form-B middle
27  * Final	- unicode form-B final
28  * Stand-Alone	- unicode form-B isolated
29  */
30 
31 #include "vim.h"
32 
33 #if defined(FEAT_ARABIC) || defined(PROTO)
34 
35 // Unicode values for Arabic characters.
36 #define a_HAMZA				0x0621
37 #define a_ALEF_MADDA			0x0622
38 #define a_ALEF_HAMZA_ABOVE		0x0623
39 #define a_WAW_HAMZA			0x0624
40 #define a_ALEF_HAMZA_BELOW		0x0625
41 #define a_YEH_HAMZA			0x0626
42 #define a_ALEF				0x0627
43 #define a_BEH				0x0628
44 #define a_TEH_MARBUTA			0x0629
45 #define a_TEH				0x062a
46 #define a_THEH				0x062b
47 #define a_JEEM				0x062c
48 #define a_HAH				0x062d
49 #define a_KHAH				0x062e
50 #define a_DAL				0x062f
51 #define a_THAL				0x0630
52 #define a_REH				0x0631
53 #define a_ZAIN				0x0632
54 #define a_SEEN				0x0633
55 #define a_SHEEN				0x0634
56 #define a_SAD				0x0635
57 #define a_DAD				0x0636
58 #define a_TAH				0x0637
59 #define a_ZAH				0x0638
60 #define a_AIN				0x0639
61 #define a_GHAIN				0x063a
62 #define a_TATWEEL			0x0640
63 #define a_FEH				0x0641
64 #define a_QAF				0x0642
65 #define a_KAF				0x0643
66 #define a_LAM				0x0644
67 #define a_MEEM				0x0645
68 #define a_NOON				0x0646
69 #define a_HEH				0x0647
70 #define a_WAW				0x0648
71 #define a_ALEF_MAKSURA			0x0649
72 #define a_YEH				0x064a
73 #define a_FATHATAN			0x064b
74 #define a_DAMMATAN			0x064c
75 #define a_KASRATAN			0x064d
76 #define a_FATHA				0x064e
77 #define a_DAMMA				0x064f
78 #define a_KASRA				0x0650
79 #define a_SHADDA			0x0651
80 #define a_SUKUN				0x0652
81 #define a_MADDA_ABOVE			0x0653
82 #define a_HAMZA_ABOVE			0x0654
83 #define a_HAMZA_BELOW			0x0655
84 
85 #define a_PEH				0x067e
86 #define a_TCHEH				0x0686
87 #define a_JEH				0x0698
88 #define a_FKAF				0x06a9
89 #define a_GAF				0x06af
90 #define a_FYEH				0x06cc
91 
92 #define a_s_LAM_ALEF_MADDA_ABOVE	0xfef5
93 #define a_f_LAM_ALEF_MADDA_ABOVE	0xfef6
94 #define a_s_LAM_ALEF_HAMZA_ABOVE	0xfef7
95 #define a_f_LAM_ALEF_HAMZA_ABOVE	0xfef8
96 #define a_s_LAM_ALEF_HAMZA_BELOW	0xfef9
97 #define a_f_LAM_ALEF_HAMZA_BELOW	0xfefa
98 #define a_s_LAM_ALEF			0xfefb
99 #define a_f_LAM_ALEF			0xfefc
100 
101 static struct achar {
102     unsigned c;
103     unsigned isolated;
104     unsigned initial;
105     unsigned medial;
106     unsigned final;
107 } achars[] = {
108     {a_HAMZA, 0xfe80, 0, 0, 0},
109     {a_ALEF_MADDA, 0xfe81, 0, 0, 0xfe82},
110     {a_ALEF_HAMZA_ABOVE, 0xfe83, 0, 0, 0xfe84},
111     {a_WAW_HAMZA, 0xfe85, 0, 0, 0xfe86},
112     {a_ALEF_HAMZA_BELOW, 0xfe87, 0, 0, 0xfe88},
113     {a_YEH_HAMZA, 0xfe89, 0xfe8b, 0xfe8c, 0xfe8a},
114     {a_ALEF, 0xfe8d, 0, 0, 0xfe8e},
115     {a_BEH, 0xfe8f, 0xfe91, 0xfe92, 0xfe90},
116     {a_TEH_MARBUTA, 0xfe93, 0, 0, 0xfe94},
117     {a_TEH, 0xfe95, 0xfe97, 0xfe98, 0xfe96},
118     {a_THEH, 0xfe99, 0xfe9b, 0xfe9c, 0xfe9a},
119     {a_JEEM, 0xfe9d, 0xfe9f, 0xfea0, 0xfe9e},
120     {a_HAH, 0xfea1, 0xfea3, 0xfea4, 0xfea2},
121     {a_KHAH, 0xfea5, 0xfea7, 0xfea8, 0xfea6},
122     {a_DAL, 0xfea9, 0, 0, 0xfeaa},
123     {a_THAL, 0xfeab, 0, 0, 0xfeac},
124     {a_REH, 0xfead, 0, 0, 0xfeae},
125     {a_ZAIN, 0xfeaf, 0, 0, 0xfeb0},
126     {a_SEEN, 0xfeb1, 0xfeb3, 0xfeb4, 0xfeb2},
127     {a_SHEEN, 0xfeb5, 0xfeb7, 0xfeb8, 0xfeb6},
128     {a_SAD, 0xfeb9, 0xfebb, 0xfebc, 0xfeba},
129     {a_DAD, 0xfebd, 0xfebf, 0xfec0, 0xfebe},
130     {a_TAH, 0xfec1, 0xfec3, 0xfec4, 0xfec2},
131     {a_ZAH, 0xfec5, 0xfec7, 0xfec8, 0xfec6},
132     {a_AIN, 0xfec9, 0xfecb, 0xfecc, 0xfeca},
133     {a_GHAIN, 0xfecd, 0xfecf, 0xfed0, 0xfece},
134     {a_TATWEEL, 0, 0x0640, 0x0640, 0x0640},
135     {a_FEH, 0xfed1, 0xfed3, 0xfed4, 0xfed2},
136     {a_QAF, 0xfed5, 0xfed7, 0xfed8, 0xfed6},
137     {a_KAF, 0xfed9, 0xfedb, 0xfedc, 0xfeda},
138     {a_LAM, 0xfedd, 0xfedf, 0xfee0, 0xfede},
139     {a_MEEM, 0xfee1, 0xfee3, 0xfee4, 0xfee2},
140     {a_NOON, 0xfee5, 0xfee7, 0xfee8, 0xfee6},
141     {a_HEH, 0xfee9, 0xfeeb, 0xfeec, 0xfeea},
142     {a_WAW, 0xfeed, 0, 0, 0xfeee},
143     {a_ALEF_MAKSURA, 0xfeef, 0, 0, 0xfef0},
144     {a_YEH, 0xfef1, 0xfef3, 0xfef4, 0xfef2},
145     {a_FATHATAN, 0xfe70, 0, 0, 0},
146     {a_DAMMATAN, 0xfe72, 0, 0, 0},
147     {a_KASRATAN, 0xfe74, 0, 0, 0},
148     {a_FATHA, 0xfe76, 0, 0xfe77, 0},
149     {a_DAMMA, 0xfe78, 0, 0xfe79, 0},
150     {a_KASRA, 0xfe7a, 0, 0xfe7b, 0},
151     {a_SHADDA, 0xfe7c, 0, 0xfe7c, 0},
152     {a_SUKUN, 0xfe7e, 0, 0xfe7f, 0},
153     {a_MADDA_ABOVE, 0, 0, 0, 0},
154     {a_HAMZA_ABOVE, 0, 0, 0, 0},
155     {a_HAMZA_BELOW, 0, 0, 0, 0},
156     {a_PEH, 0xfb56, 0xfb58, 0xfb59, 0xfb57},
157     {a_TCHEH, 0xfb7a, 0xfb7c, 0xfb7d, 0xfb7b},
158     {a_JEH, 0xfb8a, 0, 0, 0xfb8b},
159     {a_FKAF, 0xfb8e, 0xfb90, 0xfb91, 0xfb8f},
160     {a_GAF, 0xfb92, 0xfb94, 0xfb95, 0xfb93},
161     {a_FYEH, 0xfbfc, 0xfbfe, 0xfbff, 0xfbfd},
162 };
163 
164 #define a_BYTE_ORDER_MARK		0xfeff
165 
166 #define ARRAY_SIZE(a)		(sizeof(a) / sizeof((a)[0]))
167 
168 /*
169  * Find the struct achar pointer to the given Arabic char.
170  * Returns NULL if not found.
171  */
172     static struct achar *
173 find_achar(int c)
174 {
175     int h, m, l;
176 
177     // using binary search to find c
178     h = ARRAY_SIZE(achars);
179     l = 0;
180     while (l < h)
181     {
182 	m = (h + l) / 2;
183 	if (achars[m].c == (unsigned)c)
184 	    return &achars[m];
185 	if ((unsigned)c < achars[m].c)
186 	    h = m;
187 	else
188 	    l = m + 1;
189     }
190     return NULL;
191 }
192 
193 /*
194  * Change shape - from Combination (2 char) to an Isolated
195  */
196     static int
197 chg_c_laa2i(int hid_c)
198 {
199     int tempc;
200 
201     switch (hid_c)
202     {
203 	case a_ALEF_MADDA:
204 	    tempc = a_s_LAM_ALEF_MADDA_ABOVE;
205 	    break;
206 	case a_ALEF_HAMZA_ABOVE:
207 	    tempc = a_s_LAM_ALEF_HAMZA_ABOVE;
208 	    break;
209 	case a_ALEF_HAMZA_BELOW:
210 	    tempc = a_s_LAM_ALEF_HAMZA_BELOW;
211 	    break;
212 	case a_ALEF:
213 	    tempc = a_s_LAM_ALEF;
214 	    break;
215 	default:
216 	    tempc = 0;
217     }
218 
219     return tempc;
220 }
221 
222 /*
223  * Change shape - from Combination-Isolated to Final
224  */
225     static int
226 chg_c_laa2f(int hid_c)
227 {
228     int tempc;
229 
230     switch (hid_c)
231     {
232 	case a_ALEF_MADDA:
233 	    tempc = a_f_LAM_ALEF_MADDA_ABOVE;
234 	    break;
235 	case a_ALEF_HAMZA_ABOVE:
236 	    tempc = a_f_LAM_ALEF_HAMZA_ABOVE;
237 	    break;
238 	case a_ALEF_HAMZA_BELOW:
239 	    tempc = a_f_LAM_ALEF_HAMZA_BELOW;
240 	    break;
241 	case a_ALEF:
242 	    tempc = a_f_LAM_ALEF;
243 	    break;
244 	default:
245 	    tempc = 0;
246     }
247 
248     return tempc;
249 }
250 
251 /*
252  * Returns whether it is possible to join the given letters
253  */
254     static int
255 can_join(int c1, int c2)
256 {
257     struct achar *a1 = find_achar(c1);
258     struct achar *a2 = find_achar(c2);
259 
260     return a1 && a2 && (a1->initial || a1->medial) && (a2->final || a2->medial);
261 }
262 
263 /*
264  * Check whether we are dealing with a character that could be regarded as an
265  * Arabic combining character, need to check the character before this.
266  */
267     int
268 arabic_maycombine(int two)
269 {
270     if (p_arshape && !p_tbidi)
271 	return (two == a_ALEF_MADDA
272 		    || two == a_ALEF_HAMZA_ABOVE
273 		    || two == a_ALEF_HAMZA_BELOW
274 		    || two == a_ALEF);
275     return FALSE;
276 }
277 
278 /*
279  * Check whether we are dealing with Arabic combining characters.
280  * Note: these are NOT really composing characters!
281  */
282     int
283 arabic_combine(
284     int		one,	    // first character
285     int		two)	    // character just after "one"
286 {
287     if (one == a_LAM)
288 	return arabic_maycombine(two);
289     return FALSE;
290 }
291 
292 /*
293  * A_is_iso returns true if 'c' is an Arabic ISO-8859-6 character
294  *		(alphabet/number/punctuation)
295  */
296     static int
297 A_is_iso(int c)
298 {
299     return find_achar(c) != NULL;
300 }
301 
302 /*
303  * A_is_ok returns true if 'c' is an Arabic 10646 (8859-6 or Form-B)
304  */
305     static int
306 A_is_ok(int c)
307 {
308     return (A_is_iso(c) || c == a_BYTE_ORDER_MARK);
309 }
310 
311 /*
312  * A_is_valid returns true if 'c' is an Arabic 10646 (8859-6 or Form-B)
313  *		with some exceptions/exclusions
314  */
315     static int
316 A_is_valid(int c)
317 {
318     return (A_is_ok(c) && c != a_HAMZA);
319 }
320 
321 /*
322  * Do Arabic shaping on character "c".  Returns the shaped character.
323  * out:    "ccp" points to the first byte of the character to be shaped.
324  * in/out: "c1p" points to the first composing char for "c".
325  * in:     "prev_c"  is the previous character (not shaped)
326  * in:     "prev_c1" is the first composing char for the previous char
327  *		     (not shaped)
328  * in:     "next_c"  is the next character (not shaped).
329  */
330     int
331 arabic_shape(
332     int		c,
333     int		*ccp,
334     int		*c1p,
335     int		prev_c,
336     int		prev_c1,
337     int		next_c)
338 {
339     int		curr_c;
340     int		curr_laa;
341     int		prev_laa;
342 
343     // Deal only with Arabic characters, pass back all others
344     if (!A_is_ok(c))
345 	return c;
346 
347     curr_laa = arabic_combine(c, *c1p);
348     prev_laa = arabic_combine(prev_c, prev_c1);
349 
350     if (curr_laa)
351     {
352 	if (A_is_valid(prev_c) && can_join(prev_c, a_LAM) && !prev_laa)
353 	    curr_c = chg_c_laa2f(*c1p);
354 	else
355 	    curr_c = chg_c_laa2i(*c1p);
356 
357 	// Remove the composing character
358 	*c1p = 0;
359     }
360     else
361     {
362 	struct achar *curr_a = find_achar(c);
363 	int backward_combine = !prev_laa && can_join(prev_c, c);
364 	int forward_combine = can_join(c, next_c);
365 
366 	if (backward_combine)
367 	{
368 	    if (forward_combine)
369 		curr_c = curr_a->medial;
370 	    else
371 		curr_c = curr_a->final;
372 	}
373 	else
374 	{
375 	    if (forward_combine)
376 		curr_c = curr_a->initial;
377 	    else
378 		curr_c = curr_a->isolated;
379 	}
380     }
381 
382     // Character missing from the table means using original character.
383     if (curr_c == NUL)
384 	curr_c = c;
385 
386     if (curr_c != c && ccp != NULL)
387     {
388 	char_u buf[MB_MAXBYTES + 1];
389 
390 	// Update the first byte of the character.
391 	(*mb_char2bytes)(curr_c, buf);
392 	*ccp = buf[0];
393     }
394 
395     // Return the shaped character
396     return curr_c;
397 }
398 #endif // FEAT_ARABIC
399