robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
38986188ba
master
1/// @file arabic.c 2/// 3/// Functions for Arabic language. 4/// 5/// Author: Nadim Shaikli & Isam Bayazidi 6/// Farsi support and restructuring to make adding new letters easier by Ali 7/// Gholami Rudi. Further work by Ameretat Reith. 8 9/// Sorted list of unicode Arabic characters. Each entry holds the 10/// presentation forms of a letter. 11/// 12/// Arabic characters are categorized into following types: 13/// 14/// Isolated - iso-8859-6 form char denoted with a_* 15/// Initial - unicode form-B start char denoted with a_i_* 16/// Medial - unicode form-B middle char denoted with a_m_* 17/// Final - unicode form-B final char denoted with a_f_* 18/// Stand-Alone - unicode form-B isolated char denoted with a_s_* (NOT USED) 19 20#include <stdbool.h> 21#include <stddef.h> 22 23#include "nvim/arabic.h" 24#include "nvim/ascii_defs.h" 25#include "nvim/macros_defs.h" 26#include "nvim/option_vars.h" 27 28// Unicode values for Arabic characters. 29enum { 30a_HAMZA = 0x0621 , 31a_ALEF_MADDA = 0x0622 , 32a_ALEF_HAMZA_ABOVE = 0x0623 , 33a_WAW_HAMZA = 0x0624 , 34a_ALEF_HAMZA_BELOW = 0x0625 , 35a_YEH_HAMZA = 0x0626 , 36a_ALEF = 0x0627 , 37a_BEH = 0x0628 , 38a_TEH_MARBUTA = 0x0629 , 39a_TEH = 0x062a , 40a_THEH = 0x062b , 41a_JEEM = 0x062c , 42a_HAH = 0x062d , 43a_KHAH = 0x062e , 44a_DAL = 0x062f , 45a_THAL = 0x0630 , 46a_REH = 0x0631 , 47a_ZAIN = 0x0632 , 48a_SEEN = 0x0633 , 49a_SHEEN = 0x0634 , 50a_SAD = 0x0635 , 51a_DAD = 0x0636 , 52a_TAH = 0x0637 , 53a_ZAH = 0x0638 , 54a_AIN = 0x0639 , 55a_GHAIN = 0x063a , 56a_TATWEEL = 0x0640 , 57a_FEH = 0x0641 , 58a_QAF = 0x0642 , 59a_KAF = 0x0643 , 60a_LAM = 0x0644 , 61a_MEEM = 0x0645 , 62a_NOON = 0x0646 , 63a_HEH = 0x0647 , 64a_WAW = 0x0648 , 65a_ALEF_MAKSURA = 0x0649 , 66a_YEH = 0x064a , 67a_FATHATAN = 0x064b , 68a_DAMMATAN = 0x064c , 69a_KASRATAN = 0x064d , 70a_FATHA = 0x064e , 71a_DAMMA = 0x064f , 72a_KASRA = 0x0650 , 73a_SHADDA = 0x0651 , 74a_SUKUN = 0x0652 , 75a_MADDA_ABOVE = 0x0653 , 76a_HAMZA_ABOVE = 0x0654 , 77a_HAMZA_BELOW = 0x0655 , 78 79a_PEH = 0x067e , 80a_TCHEH = 0x0686 , 81a_JEH = 0x0698 , 82a_FKAF = 0x06a9 , 83a_GAF = 0x06af , 84a_FYEH = 0x06cc , 85 86a_s_LAM_ALEF_MADDA_ABOVE = 0xfef5 , 87a_f_LAM_ALEF_MADDA_ABOVE = 0xfef6 , 88a_s_LAM_ALEF_HAMZA_ABOVE = 0xfef7 , 89a_f_LAM_ALEF_HAMZA_ABOVE = 0xfef8 , 90a_s_LAM_ALEF_HAMZA_BELOW = 0xfef9 , 91a_f_LAM_ALEF_HAMZA_BELOW = 0xfefa , 92a_s_LAM_ALEF = 0xfefb , 93a_f_LAM_ALEF = 0xfefc , 94}; 95 96static struct achar { 97unsigned c ; 98unsigned isolated ; 99unsigned initial ; 100unsigned medial ; 101unsigned final ; 102}achars []= { 103 {a_HAMZA ,0xfe80 ,0 ,0 ,0 }, 104 {a_ALEF_MADDA ,0xfe81 ,0 ,0 ,0xfe82 }, 105 {a_ALEF_HAMZA_ABOVE ,0xfe83 ,0 ,0 ,0xfe84 }, 106 {a_WAW_HAMZA ,0xfe85 ,0 ,0 ,0xfe86 }, 107 {a_ALEF_HAMZA_BELOW ,0xfe87 ,0 ,0 ,0xfe88 }, 108 {a_YEH_HAMZA ,0xfe89 ,0xfe8b ,0xfe8c ,0xfe8a }, 109 {a_ALEF ,0xfe8d ,0 ,0 ,0xfe8e }, 110 {a_BEH ,0xfe8f ,0xfe91 ,0xfe92 ,0xfe90 }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,0xfee6 }, 136 {a_HEH ,0xfee9 ,0xfeeb ,0xfeec ,0xfeea }, 137 {a_WAW ,0xfeed ,0 ,0 ,0xfeee }, 138 {a_ALEF_MAKSURA ,0xfeef ,0 ,0 ,0xfef0 }, 139 {a_YEH ,0xfef1 ,0xfef3 ,0xfef4 ,0xfef2 }, 140 {a_FATHATAN ,0xfe70 ,0 ,0 ,0 }, 141 {a_DAMMATAN ,0xfe72 ,0 ,0 ,0 }, 142 {a_KASRATAN ,0xfe74 ,0 ,0 ,0 }, 143 {a_FATHA ,0xfe76 ,0 ,0xfe77 ,0 }, 144 {a_DAMMA ,0xfe78 ,0 ,0xfe79 ,0 }, 145 {a_KASRA ,0xfe7a ,0 ,0xfe7b ,0 }, 146 {a_SHADDA ,0xfe7c ,0 ,0xfe7c ,0 }, 147 {a_SUKUN ,0xfe7e ,0 ,0xfe7f ,0 }, 148 {a_MADDA_ABOVE ,0 ,0 ,0 ,0 }, 149 {a_HAMZA_ABOVE ,0 ,0 ,0 ,0 }, 150 {a_HAMZA_BELOW ,0 ,0 ,0 ,0 }, 151 {a_PEH ,0xfb56 ,0xfb58 ,0xfb59 ,0xfb57 }, 152 {a_TCHEH ,0xfb7a ,0xfb7c ,0xfb7d ,0xfb7b }, 153 {a_JEH ,0xfb8a ,0 ,0 ,0xfb8b }, 154 {a_FKAF ,0xfb8e ,0xfb90 ,0xfb91 ,0xfb8f }, 155 {a_GAF ,0xfb92 ,0xfb94 ,0xfb95 ,0xfb93 }, 156 {a_FYEH ,0xfbfc ,0xfbfe ,0xfbff ,0xfbfd }, 157}; 158 159#define a_BYTE_ORDER_MARK 0xfeff 160 161#include "arabic.c.generated.h" 162 163/// Find the struct achar pointer to the given Arabic char. 164/// Returns NULL if not found. 165static struct achar * find_achar (int c ) 166{ 167// using binary search to find c 168int h = ARRAY_SIZE (achars ); 169int l = 0 ; 170while (l < h ) { 171int m = (h + l ) /2 ; 172if (achars [m ].c == (unsigned )c ) { 173return & achars [m ]; 174 } 175if ((unsigned )c < achars [m ].c ) { 176h = m ; 177 }else { 178l = m + 1 ; 179 } 180 } 181return NULL ; 182} 183 184/// Change shape - from Combination (2 char) to an Isolated 185static int chg_c_laa2i (int hid_c ) 186{ 187int tempc ; 188 189switch (hid_c ) { 190case a_ALEF_MADDA : 191tempc = a_s_LAM_ALEF_MADDA_ABOVE ; 192break ; 193case a_ALEF_HAMZA_ABOVE : 194tempc = a_s_LAM_ALEF_HAMZA_ABOVE ; 195break ; 196case a_ALEF_HAMZA_BELOW : 197tempc = a_s_LAM_ALEF_HAMZA_BELOW ; 198break ; 199case a_ALEF : 200tempc = a_s_LAM_ALEF ; 201break ; 202default : 203tempc = 0 ; 204 } 205 206return tempc ; 207} 208 209/// Change shape - from Combination-Isolated to Final 210static int chg_c_laa2f (int hid_c ) 211{ 212int tempc ; 213 214switch (hid_c ) { 215case a_ALEF_MADDA : 216tempc = a_f_LAM_ALEF_MADDA_ABOVE ; 217break ; 218case a_ALEF_HAMZA_ABOVE : 219tempc = a_f_LAM_ALEF_HAMZA_ABOVE ; 220break ; 221case a_ALEF_HAMZA_BELOW : 222tempc = a_f_LAM_ALEF_HAMZA_BELOW ; 223break ; 224case a_ALEF : 225tempc = a_f_LAM_ALEF ; 226break ; 227default : 228tempc = 0 ; 229 } 230 231return tempc ; 232} 233 234/// Returns whether it is possible to join the given letters 235static int can_join (int c1 ,int c2 ) 236{ 237struct achar * a1 = find_achar (c1 ); 238struct achar * a2 = find_achar (c2 ); 239 240return a1 && a2 && (a1 -> initial || a1 -> medial )&& (a2 -> final || a2 -> medial ); 241} 242 243/// Check whether we are dealing with a character that could be regarded as an 244/// Arabic combining character, need to check the character before this. 245bool arabic_maycombine (int two ) 246FUNC_ATTR_PURE 247{ 248if (p_arshape && !p_tbidi ) { 249return two == a_ALEF_MADDA 250|| two == a_ALEF_HAMZA_ABOVE 251|| two == a_ALEF_HAMZA_BELOW 252|| two == a_ALEF ; 253 } 254return false; 255} 256 257/// Check whether we are dealing with Arabic combining characters. 258/// Returns false for negative values. 259/// Note: these are NOT really composing characters! 260/// 261/// @param one First character. 262/// @param two Character just after "one". 263bool arabic_combine (int one ,int two ) 264FUNC_ATTR_PURE 265{ 266if (one == a_LAM ) { 267return arabic_maycombine (two ); 268 } 269return false; 270} 271 272/// @return true if 'c' is an Arabic ISO-8859-6 character 273/// (alphabet/number/punctuation) 274static bool A_is_iso (int c ) 275{ 276return find_achar (c )!= NULL ; 277} 278 279/// @return true if 'c' is an Arabic 10646 (8859-6 or Form-B) 280static bool A_is_ok (int c ) 281{ 282return (A_is_iso (c )|| c == a_BYTE_ORDER_MARK ); 283} 284 285/// @return true if 'c' is an Arabic 10646 (8859-6 or Form-B) 286/// with some exceptions/exclusions 287static bool A_is_valid (int c ) 288{ 289return (A_is_ok (c )&& c != a_HAMZA ); 290} 291 292// Do Arabic shaping on character "c". Returns the shaped character. 293// in/out: "c1p" points to the first composing char for "c". 294// in: "prev_c" is the previous character (not shaped) 295// in: "prev_c1" is the first composing char for the previous char 296// (not shaped) 297// in: "next_c" is the next character (not shaped). 298int arabic_shape (int c ,int * c1p ,int prev_c ,int prev_c1 ,int next_c ) 299{ 300// Deal only with Arabic character, pass back all others 301if (!A_is_ok (c )) { 302return c ; 303 } 304 305int curr_c ; 306bool curr_laa = arabic_combine (c ,* c1p ); 307bool prev_laa = arabic_combine (prev_c ,prev_c1 ); 308 309if (curr_laa ) { 310if (A_is_valid (prev_c )&& can_join (prev_c ,a_LAM )&& !prev_laa ) { 311curr_c = chg_c_laa2f (* c1p ); 312 }else { 313curr_c = chg_c_laa2i (* c1p ); 314 } 315// Remove the composing character 316* c1p = 0 ; 317 }else { 318struct achar * curr_a = find_achar (c ); 319int backward_combine = !prev_laa && can_join (prev_c ,c ); 320int forward_combine = can_join (c ,next_c ); 321 322if (backward_combine ) { 323if (forward_combine ) { 324curr_c = (int )curr_a -> medial ; 325 }else { 326curr_c = (int )curr_a -> final ; 327 } 328 }else { 329if (forward_combine ) { 330curr_c = (int )curr_a -> initial ; 331 }else { 332curr_c = (int )curr_a -> isolated ; 333 } 334 } 335 } 336 337// Character missing from the table means using original character. 338if (curr_c == NUL ) { 339curr_c = c ; 340 } 341 342// Return the shaped character 343return curr_c ; 344}