123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972 |
- #region File Description
- //-----------------------------------------------------------------------------
- /// <summary>
- /// This is an Open Source File Created by: Abdullah Konash (http://abdullahkonash.com/) Twitter: @konash
- /// This File allow the users to use arabic text in XNA and Unity platform.
- /// It flips the characters and replace them with the appropriate ones to connect the letters in the correct way.
- /// </summary>
- //-----------------------------------------------------------------------------
- #endregion
- #region Using Statements
- using System;
- using System.Collections.Generic;
- #endregion
- namespace ArabicSupport
- {
-
- public class ArabicFixer
- {
- /// <summary>
- /// Fix the specified string.
- /// </summary>
- /// <param name='str'>
- /// String to be fixed.
- /// </param>
- public static string Fix(string str)
- {
- return Fix(str, false, true);
- }
-
- public static string Fix(string str, bool rtl)
- {
- if(rtl)
-
- {
- return Fix(str);
- }
- else
- {
- string[] words = str.Split(' ');
- string result = "";
- string arabicToIgnore = "";
- foreach(string word in words)
- {
- if(char.IsLower(word.ToLower()[word.Length/2]))
- {
- result += Fix(arabicToIgnore) + word + " ";
- arabicToIgnore = "";
- }
- else
- {
- arabicToIgnore += word + " ";
-
- }
- }
- if(arabicToIgnore != "")
- result += Fix(arabicToIgnore);
-
- return result;
- }
- }
-
- /// <summary>
- /// Fix the specified string with customization options.
- /// </summary>
- /// <param name='str'>
- /// String to be fixed.
- /// </param>
- /// <param name='showTashkeel'>
- /// Show tashkeel.
- /// </param>
- /// <param name='useHinduNumbers'>
- /// Use hindu numbers.
- /// </param>
- public static string Fix(string str, bool showTashkeel, bool useHinduNumbers)
- {
- ArabicFixerTool.showTashkeel = showTashkeel;
- ArabicFixerTool.useHinduNumbers =useHinduNumbers;
-
- if(str.Contains("\n"))
- str = str.Replace("\n", Environment.NewLine);
-
- if(str.Contains(Environment.NewLine))
- {
- string[] stringSeparators = new string[] {Environment.NewLine};
- string[] strSplit = str.Split(stringSeparators, StringSplitOptions.None);
-
- if(strSplit.Length == 0)
- return ArabicFixerTool.FixLine(str);
- else if(strSplit.Length == 1)
- return ArabicFixerTool.FixLine(str);
- else
- {
- string outputString = ArabicFixerTool.FixLine(strSplit[0]);
- int iteration = 1;
- if(strSplit.Length > 1)
- {
- while(iteration < strSplit.Length)
- {
- outputString += Environment.NewLine + ArabicFixerTool.FixLine(strSplit[iteration]);
- iteration++;
- }
- }
- return outputString;
- }
- }
- else
- {
- return ArabicFixerTool.FixLine(str);
- }
-
- }
-
- }
-
- }
- /// <summary>
- /// Arabic Contextual forms General - Unicode
- /// </summary>
- internal enum IsolatedArabicLetters
- {
- Hamza = 0xFE80,
- Alef = 0xFE8D,
- AlefHamza = 0xFE83,
- WawHamza = 0xFE85,
- AlefMaksoor = 0xFE87,
- AlefMaksora = 0xFBFC,
- HamzaNabera = 0xFE89,
- Ba = 0xFE8F,
- Ta = 0xFE95,
- Tha2 = 0xFE99,
- Jeem = 0xFE9D,
- H7aa = 0xFEA1,
- Khaa2 = 0xFEA5,
- Dal = 0xFEA9,
- Thal = 0xFEAB,
- Ra2 = 0xFEAD,
- Zeen = 0xFEAF,
- Seen = 0xFEB1,
- Sheen = 0xFEB5,
- S9a = 0xFEB9,
- Dha = 0xFEBD,
- T6a = 0xFEC1,
- T6ha = 0xFEC5,
- Ain = 0xFEC9,
- Gain = 0xFECD,
- Fa = 0xFED1,
- Gaf = 0xFED5,
- Kaf = 0xFED9,
- Lam = 0xFEDD,
- Meem = 0xFEE1,
- Noon = 0xFEE5,
- Ha = 0xFEE9,
- Waw = 0xFEED,
- Ya = 0xFEF1,
- AlefMad = 0xFE81,
- TaMarboota = 0xFE93,
- PersianPe = 0xFB56, // Persian Letters;
- PersianChe = 0xFB7A,
- PersianZe = 0xFB8A,
- PersianGaf = 0xFB92,
- PersianGaf2 = 0xFB8E
-
- }
- /// <summary>
- /// Arabic Contextual forms - Isolated
- /// </summary>
- internal enum GeneralArabicLetters
- {
- Hamza = 0x0621,
- Alef = 0x0627,
- AlefHamza = 0x0623,
- WawHamza = 0x0624,
- AlefMaksoor = 0x0625,
- AlefMagsora = 0x0649,
- HamzaNabera = 0x0626,
- Ba = 0x0628,
- Ta = 0x062A,
- Tha2 = 0x062B,
- Jeem = 0x062C,
- H7aa = 0x062D,
- Khaa2 = 0x062E,
- Dal = 0x062F,
- Thal = 0x0630,
- Ra2 = 0x0631,
- Zeen = 0x0632,
- Seen = 0x0633,
- Sheen = 0x0634,
- S9a = 0x0635,
- Dha = 0x0636,
- T6a = 0x0637,
- T6ha = 0x0638,
- Ain = 0x0639,
- Gain = 0x063A,
- Fa = 0x0641,
- Gaf = 0x0642,
- Kaf = 0x0643,
- Lam = 0x0644,
- Meem = 0x0645,
- Noon = 0x0646,
- Ha = 0x0647,
- Waw = 0x0648,
- Ya = 0x064A,
- AlefMad = 0x0622,
- TaMarboota = 0x0629,
- PersianPe = 0x067E, // Persian Letters;
- PersianChe = 0x0686,
- PersianZe = 0x0698,
- PersianGaf = 0x06AF,
- PersianGaf2 = 0x06A9
-
- }
- /// <summary>
- /// Data Structure for conversion
- /// </summary>
- internal class ArabicMapping
- {
- public int from;
- public int to;
- public ArabicMapping(int from, int to)
- {
- this.from = from;
- this.to = to;
- }
- }
- /// <summary>
- /// Sets up and creates the conversion table
- /// </summary>
- internal class ArabicTable
- {
-
- private static List<ArabicMapping> mapList;
- private static ArabicTable arabicMapper;
-
- /// <summary>
- /// Setting up the conversion table
- /// </summary>
- private ArabicTable()
- {
- mapList = new List<ArabicMapping>();
-
-
-
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.Hamza, (int)IsolatedArabicLetters.Hamza));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.Alef, (int)IsolatedArabicLetters.Alef));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.AlefHamza, (int)IsolatedArabicLetters.AlefHamza));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.WawHamza, (int)IsolatedArabicLetters.WawHamza));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.AlefMaksoor, (int)IsolatedArabicLetters.AlefMaksoor));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.AlefMagsora, (int)IsolatedArabicLetters.AlefMaksora));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.HamzaNabera, (int)IsolatedArabicLetters.HamzaNabera));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.Ba, (int)IsolatedArabicLetters.Ba));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.Ta, (int)IsolatedArabicLetters.Ta));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.Tha2, (int)IsolatedArabicLetters.Tha2));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.Jeem, (int)IsolatedArabicLetters.Jeem));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.H7aa, (int)IsolatedArabicLetters.H7aa));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.Khaa2, (int)IsolatedArabicLetters.Khaa2));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.Dal, (int)IsolatedArabicLetters.Dal));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.Thal, (int)IsolatedArabicLetters.Thal));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.Ra2, (int)IsolatedArabicLetters.Ra2));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.Zeen, (int)IsolatedArabicLetters.Zeen));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.Seen, (int)IsolatedArabicLetters.Seen));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.Sheen, (int)IsolatedArabicLetters.Sheen));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.S9a, (int)IsolatedArabicLetters.S9a));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.Dha, (int)IsolatedArabicLetters.Dha));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.T6a, (int)IsolatedArabicLetters.T6a));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.T6ha, (int)IsolatedArabicLetters.T6ha));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.Ain, (int)IsolatedArabicLetters.Ain));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.Gain, (int)IsolatedArabicLetters.Gain));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.Fa, (int)IsolatedArabicLetters.Fa));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.Gaf, (int)IsolatedArabicLetters.Gaf));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.Kaf, (int)IsolatedArabicLetters.Kaf));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.Lam, (int)IsolatedArabicLetters.Lam));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.Meem, (int)IsolatedArabicLetters.Meem));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.Noon, (int)IsolatedArabicLetters.Noon));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.Ha, (int)IsolatedArabicLetters.Ha));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.Waw, (int)IsolatedArabicLetters.Waw));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.Ya, (int)IsolatedArabicLetters.Ya));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.AlefMad, (int)IsolatedArabicLetters.AlefMad));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.TaMarboota, (int)IsolatedArabicLetters.TaMarboota));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.PersianPe, (int)IsolatedArabicLetters.PersianPe)); // Persian Letters;
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.PersianChe, (int)IsolatedArabicLetters.PersianChe));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.PersianZe, (int)IsolatedArabicLetters.PersianZe));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.PersianGaf, (int)IsolatedArabicLetters.PersianGaf));
- mapList.Add(new ArabicMapping((int)GeneralArabicLetters.PersianGaf2, (int)IsolatedArabicLetters.PersianGaf2));
-
-
-
-
- //for (int i = 0; i < generalArabic.Length; i++)
- // mapList.Add(new ArabicMapping((int)generalArabic.GetValue(i), (int)isolatedArabic.GetValue(i))); // I
-
-
- }
-
- /// <summary>
- /// Singleton design pattern, Get the mapper. If it was not created before, create it.
- /// </summary>
- internal static ArabicTable ArabicMapper
- {
- get
- {
- if (arabicMapper == null)
- arabicMapper = new ArabicTable();
- return arabicMapper;
- }
- }
-
- internal int Convert(int toBeConverted)
- {
-
- foreach (ArabicMapping arabicMap in mapList)
- if (arabicMap.from == toBeConverted)
- {
- return arabicMap.to;
- }
- return toBeConverted;
- }
-
-
- }
- internal class TashkeelLocation
- {
- public char tashkeel;
- public int position;
- public TashkeelLocation(char tashkeel, int position)
- {
- this.tashkeel = tashkeel;
- this.position = position;
- }
- }
- internal class ArabicFixerTool
- {
- internal static bool showTashkeel = true;
- internal static bool useHinduNumbers = false;
-
-
- internal static string RemoveTashkeel(string str, out List<TashkeelLocation> tashkeelLocation)
- {
- tashkeelLocation = new List<TashkeelLocation>();
- char[] letters = str.ToCharArray();
- int index = 0;
- for (int i = 0; i < letters.Length; i++) {
- if (letters [i] == (char)0x064B) { // Tanween Fatha
- tashkeelLocation.Add (new TashkeelLocation ((char)0x064B, i));
- index++;
- } else if (letters [i] == (char)0x064C) { // DAMMATAN
- tashkeelLocation.Add (new TashkeelLocation ((char)0x064C, i));
- index++;
- } else if (letters [i] == (char)0x064D){ // KASRATAN
- tashkeelLocation.Add (new TashkeelLocation ((char)0x064D, i));
- index++;
- }else if (letters [i] == (char)0x064E) { // FATHA
- if(index > 0)
- {
- if(tashkeelLocation[index-1].tashkeel == (char)0x0651 ) // SHADDA
- {
- tashkeelLocation [index - 1].tashkeel = (char)0xFC60; // Shadda With Fatha
- continue;
- }
- }
- tashkeelLocation.Add (new TashkeelLocation ((char)0x064E, i));
- index++;
- } else if (letters [i] == (char)0x064F) { // DAMMA
- if (index > 0) {
- if (tashkeelLocation [index - 1].tashkeel == (char)0x0651) { // SHADDA
- tashkeelLocation [index - 1].tashkeel = (char)0xFC61; // Shadda With DAMMA
- continue;
- }
- }
- tashkeelLocation.Add (new TashkeelLocation ((char)0x064F, i));
- index++;
- } else if (letters [i] == (char)0x0650) { // KASRA
- if (index > 0) {
- if (tashkeelLocation [index - 1].tashkeel == (char)0x0651) { // SHADDA
- tashkeelLocation [index - 1].tashkeel = (char)0xFC62; // Shadda With KASRA
- continue;
- }
- }
- tashkeelLocation.Add (new TashkeelLocation ((char)0x0650, i));
- index++;
- } else if (letters [i] == (char)0x0651) { // SHADDA
- if(index > 0)
- {
- if(tashkeelLocation[index-1].tashkeel == (char)0x064E ) // FATHA
- {
- tashkeelLocation [index - 1].tashkeel = (char)0xFC60; // Shadda With Fatha
- continue;
- }
- if(tashkeelLocation[index-1].tashkeel == (char)0x064F ) // DAMMA
- {
- tashkeelLocation [index - 1].tashkeel = (char)0xFC61; // Shadda With DAMMA
- continue;
- }
- if(tashkeelLocation[index-1].tashkeel == (char)0x0650 ) // KASRA
- {
- tashkeelLocation [index - 1].tashkeel = (char)0xFC62; // Shadda With KASRA
- continue;
- }
- }
- tashkeelLocation.Add (new TashkeelLocation ((char)0x0651, i));
- index++;
- } else if (letters [i] == (char)0x0652) { // SUKUN
- tashkeelLocation.Add (new TashkeelLocation ((char)0x0652, i));
- index++;
- } else if (letters [i] == (char)0x0653) { // MADDAH ABOVE
- tashkeelLocation.Add (new TashkeelLocation ((char)0x0653, i));
- index++;
- }
- }
-
- string[] split = str.Split(new char[]{(char)0x064B,(char)0x064C,(char)0x064D,
- (char)0x064E,(char)0x064F,(char)0x0650,
-
- (char)0x0651,(char)0x0652,(char)0x0653,(char)0xFC60,(char)0xFC61,(char)0xFC62});
- str = "";
-
- foreach(string s in split)
- {
- str += s;
- }
-
- return str;
- }
-
- internal static char[] ReturnTashkeel(char[] letters, List<TashkeelLocation> tashkeelLocation)
- {
- char[] lettersWithTashkeel = new char[letters.Length + tashkeelLocation.Count];
-
- int letterWithTashkeelTracker = 0;
- for(int i = 0; i<letters.Length; i++)
- {
- lettersWithTashkeel[letterWithTashkeelTracker] = letters[i];
- letterWithTashkeelTracker++;
- foreach(TashkeelLocation hLocation in tashkeelLocation)
- {
- if(hLocation.position == letterWithTashkeelTracker)
- {
- lettersWithTashkeel[letterWithTashkeelTracker] = hLocation.tashkeel;
- letterWithTashkeelTracker++;
- }
- }
- }
-
- return lettersWithTashkeel;
- }
-
- /// <summary>
- /// Converts a string to a form in which the sting will be displayed correctly for arabic text.
- /// </summary>
- /// <param name="str">String to be converted. Example: "Aaa"</param>
- /// <returns>Converted string. Example: "aa aaa A" without the spaces.</returns>
- internal static string FixLine(string str)
- {
- string test = "";
-
- List<TashkeelLocation> tashkeelLocation;
-
- string originString = RemoveTashkeel(str, out tashkeelLocation);
-
- char[] lettersOrigin = originString.ToCharArray();
- char[] lettersFinal = originString.ToCharArray();
-
-
- for (int i = 0; i < lettersOrigin.Length; i++)
- {
- lettersOrigin[i] = (char)ArabicTable.ArabicMapper.Convert(lettersOrigin[i]);
- }
-
- for (int i = 0; i < lettersOrigin.Length; i++)
- {
- bool skip = false;
-
- //lettersOrigin[i] = (char)ArabicTable.ArabicMapper.Convert(lettersOrigin[i]);
- // For special Lam Letter connections.
- if (lettersOrigin[i] == (char)IsolatedArabicLetters.Lam)
- {
-
- if (i < lettersOrigin.Length - 1)
- {
- //lettersOrigin[i + 1] = (char)ArabicTable.ArabicMapper.Convert(lettersOrigin[i + 1]);
- if ((lettersOrigin[i + 1] == (char)IsolatedArabicLetters.AlefMaksoor))
- {
- lettersOrigin[i] = (char)0xFEF7;
- lettersFinal[i + 1] = (char)0xFFFF;
- skip = true;
- }
- else if ((lettersOrigin[i + 1] == (char)IsolatedArabicLetters.Alef))
- {
- lettersOrigin[i] = (char)0xFEF9;
- lettersFinal[i + 1] = (char)0xFFFF;
- skip = true;
- }
- else if ((lettersOrigin[i + 1] == (char)IsolatedArabicLetters.AlefHamza))
- {
- lettersOrigin[i] = (char)0xFEF5;
- lettersFinal[i + 1] = (char)0xFFFF;
- skip = true;
- }
- else if ((lettersOrigin[i + 1] == (char)IsolatedArabicLetters.AlefMad))
- {
- lettersOrigin[i] = (char)0xFEF3;
- lettersFinal[i + 1] = (char)0xFFFF;
- skip = true;
- }
- }
-
- }
-
-
- if (!IsIgnoredCharacter(lettersOrigin[i]))
- {
- if (IsMiddleLetter(lettersOrigin, i))
- lettersFinal[i] = (char)(lettersOrigin[i] + 3);
- else if (IsFinishingLetter(lettersOrigin, i))
- lettersFinal[i] = (char)(lettersOrigin[i] + 1);
- else if (IsLeadingLetter(lettersOrigin, i))
- lettersFinal[i] = (char)(lettersOrigin[i] + 2);
- }
- //string strOut = String.Format(@"\x{0:x4}", (ushort)lettersOrigin[i]);
- //UnityEngine.Debug.Log(strOut);
- //strOut = String.Format(@"\x{0:x4}", (ushort)lettersFinal[i]);
- //UnityEngine.Debug.Log(strOut);
- test += Convert.ToString((int)lettersOrigin[i], 16) + " ";
- if (skip)
- i++;
-
-
- //chaning numbers to hindu
- if(useHinduNumbers){
- if(lettersOrigin[i] == (char)0x0030)
- lettersFinal[i] = (char)0x0660;
- else if(lettersOrigin[i] == (char)0x0031)
- lettersFinal[i] = (char)0x0661;
- else if(lettersOrigin[i] == (char)0x0032)
- lettersFinal[i] = (char)0x0662;
- else if(lettersOrigin[i] == (char)0x0033)
- lettersFinal[i] = (char)0x0663;
- else if(lettersOrigin[i] == (char)0x0034)
- lettersFinal[i] = (char)0x0664;
- else if(lettersOrigin[i] == (char)0x0035)
- lettersFinal[i] = (char)0x0665;
- else if(lettersOrigin[i] == (char)0x0036)
- lettersFinal[i] = (char)0x0666;
- else if(lettersOrigin[i] == (char)0x0037)
- lettersFinal[i] = (char)0x0667;
- else if(lettersOrigin[i] == (char)0x0038)
- lettersFinal[i] = (char)0x0668;
- else if(lettersOrigin[i] == (char)0x0039)
- lettersFinal[i] = (char)0x0669;
- }
-
- }
-
-
-
- //Return the Tashkeel to their places.
- if(showTashkeel)
- lettersFinal = ReturnTashkeel(lettersFinal, tashkeelLocation);
-
-
- List<char> list = new List<char>();
-
- List<char> numberList = new List<char>();
-
- for (int i = lettersFinal.Length - 1; i >= 0; i--)
- {
-
-
- // if (lettersFinal[i] == '(')
- // numberList.Add(')');
- // else if (lettersFinal[i] == ')')
- // numberList.Add('(');
- // else if (lettersFinal[i] == '<')
- // numberList.Add('>');
- // else if (lettersFinal[i] == '>')
- // numberList.Add('<');
- // else
- if (char.IsPunctuation(lettersFinal[i]) && i>0 && i < lettersFinal.Length-1 &&
- (char.IsPunctuation(lettersFinal[i-1]) || char.IsPunctuation(lettersFinal[i+1])))
- {
- if (lettersFinal[i] == '(')
- list.Add(')');
- else if (lettersFinal[i] == ')')
- list.Add('(');
- else if (lettersFinal[i] == '<')
- list.Add('>');
- else if (lettersFinal[i] == '>')
- list.Add('<');
- else if (lettersFinal[i] == '[')
- list.Add(']');
- else if (lettersFinal[i] == ']')
- list.Add('[');
- else if (lettersFinal[i] != 0xFFFF)
- list.Add(lettersFinal[i]);
- }
- // For cases where english words and arabic are mixed. This allows for using arabic, english and numbers in one sentence.
- else if(lettersFinal[i] == ' ' && i > 0 && i < lettersFinal.Length-1 &&
- (char.IsLower(lettersFinal[i-1]) || char.IsUpper(lettersFinal[i-1]) || char.IsNumber(lettersFinal[i-1])) &&
- (char.IsLower(lettersFinal[i+1]) || char.IsUpper(lettersFinal[i+1]) ||char.IsNumber(lettersFinal[i+1])))
-
- {
- numberList.Add(lettersFinal[i]);
- }
-
- else if (char.IsNumber(lettersFinal[i]) || char.IsLower(lettersFinal[i]) ||
- char.IsUpper(lettersFinal[i]) || char.IsSymbol(lettersFinal[i]) ||
- char.IsPunctuation(lettersFinal[i]))// || lettersFinal[i] == '^') //)
- {
-
- if (lettersFinal[i] == '(')
- numberList.Add(')');
- else if (lettersFinal[i] == ')')
- numberList.Add('(');
- else if (lettersFinal[i] == '<')
- numberList.Add('>');
- else if (lettersFinal[i] == '>')
- numberList.Add('<');
- else if (lettersFinal[i] == '[')
- list.Add(']');
- else if (lettersFinal[i] == ']')
- list.Add('[');
- else
- numberList.Add(lettersFinal[i]);
- }
- else if( (lettersFinal[i] >= (char)0xD800 && lettersFinal[i] <= (char)0xDBFF) ||
- (lettersFinal[i] >= (char)0xDC00 && lettersFinal[i] <= (char)0xDFFF))
- {
- numberList.Add(lettersFinal[i]);
- }
- else
- {
- if (numberList.Count > 0)
- {
- for (int j = 0; j < numberList.Count; j++)
- list.Add(numberList[numberList.Count - 1 - j]);
- numberList.Clear();
- }
- if (lettersFinal[i] != 0xFFFF)
- list.Add(lettersFinal[i]);
-
- }
- }
- if (numberList.Count > 0)
- {
- for (int j = 0; j < numberList.Count; j++)
- list.Add(numberList[numberList.Count - 1 - j]);
- numberList.Clear();
- }
-
- // Moving letters from a list to an array.
- lettersFinal = new char[list.Count];
- for (int i = 0; i < lettersFinal.Length; i++)
- lettersFinal[i] = list[i];
-
-
- str = new string(lettersFinal);
- return str;
- }
-
- /// <summary>
- /// English letters, numbers and punctuation characters are ignored. This checks if the ch is an ignored character.
- /// </summary>
- /// <param name="ch">The character to be checked for skipping</param>
- /// <returns>True if the character should be ignored, false if it should not be ignored.</returns>
- internal static bool IsIgnoredCharacter(char ch)
- {
- bool isPunctuation = char.IsPunctuation(ch);
- bool isNumber = char.IsNumber(ch);
- bool isLower = char.IsLower(ch);
- bool isUpper = char.IsUpper(ch);
- bool isSymbol = char.IsSymbol(ch);
- bool isPersianCharacter = ch == (char)0xFB56 || ch == (char)0xFB7A || ch == (char)0xFB8A || ch == (char)0xFB92 || ch == (char)0xFB8E;
- bool isPresentationFormB = (ch <= (char)0xFEFF && ch >= (char)0xFE70);
- bool isAcceptableCharacter = isPresentationFormB || isPersianCharacter || ch == (char)0xFBFC;
- return isPunctuation ||
- isNumber ||
- isLower ||
- isUpper ||
- isSymbol ||
- !isAcceptableCharacter ||
- ch == 'a' || ch == '>' || ch == '<' || ch == (char)0x061B;
-
- // return char.IsPunctuation(ch) || char.IsNumber(ch) || ch == 'a' || ch == '>' || ch == '<' ||
- // char.IsLower(ch) || char.IsUpper(ch) || ch == (char)0x061B || char.IsSymbol(ch)
- // || !(ch <= (char)0xFEFF && ch >= (char)0xFE70) // Presentation Form B
- // || ch == (char)0xFB56 || ch == (char)0xFB7A || ch == (char)0xFB8A || ch == (char)0xFB92; // Persian Characters
-
- // PersianPe = 0xFB56,
- // PersianChe = 0xFB7A,
- // PersianZe = 0xFB8A,
- // PersianGaf = 0xFB92
- //lettersOrigin[i] <= (char)0xFEFF && lettersOrigin[i] >= (char)0xFE70
- }
-
- /// <summary>
- /// Checks if the letter at index value is a leading character in Arabic or not.
- /// </summary>
- /// <param name="letters">The whole word that contains the character to be checked</param>
- /// <param name="index">The index of the character to be checked</param>
- /// <returns>True if the character at index is a leading character, else, returns false</returns>
- internal static bool IsLeadingLetter(char[] letters, int index)
- {
- bool lettersThatCannotBeBeforeALeadingLetter = index == 0
- || letters[index - 1] == ' '
- || letters[index - 1] == '*' // ??? Remove?
- || letters[index - 1] == 'A' // ??? Remove?
- || char.IsPunctuation(letters[index - 1])
- || letters[index - 1] == '>'
- || letters[index - 1] == '<'
- || letters[index - 1] == (int)IsolatedArabicLetters.Alef
- || letters[index - 1] == (int)IsolatedArabicLetters.Dal
- || letters[index - 1] == (int)IsolatedArabicLetters.Thal
- || letters[index - 1] == (int)IsolatedArabicLetters.Ra2
- || letters[index - 1] == (int)IsolatedArabicLetters.Zeen
- || letters[index - 1] == (int)IsolatedArabicLetters.PersianZe
- //|| letters[index - 1] == (int)IsolatedArabicLetters.AlefMaksora
- || letters[index - 1] == (int)IsolatedArabicLetters.Waw
- || letters[index - 1] == (int)IsolatedArabicLetters.AlefMad
- || letters[index - 1] == (int)IsolatedArabicLetters.AlefHamza
- || letters[index - 1] == (int)IsolatedArabicLetters.Hamza
- || letters[index - 1] == (int)IsolatedArabicLetters.AlefMaksoor
- || letters[index - 1] == (int)IsolatedArabicLetters.WawHamza;
- bool lettersThatCannotBeALeadingLetter = letters[index] != ' '
- && letters[index] != (int)IsolatedArabicLetters.Dal
- && letters[index] != (int)IsolatedArabicLetters.Thal
- && letters[index] != (int)IsolatedArabicLetters.Ra2
- && letters[index] != (int)IsolatedArabicLetters.Zeen
- && letters[index] != (int)IsolatedArabicLetters.PersianZe
- && letters[index] != (int)IsolatedArabicLetters.Alef
- && letters[index] != (int)IsolatedArabicLetters.AlefHamza
- && letters[index] != (int)IsolatedArabicLetters.AlefMaksoor
- && letters[index] != (int)IsolatedArabicLetters.AlefMad
- && letters[index] != (int)IsolatedArabicLetters.WawHamza
- && letters[index] != (int)IsolatedArabicLetters.Waw
- && letters[index] != (int)IsolatedArabicLetters.Hamza;
- bool lettersThatCannotBeAfterLeadingLetter = index < letters.Length - 1
- && letters[index + 1] != ' '
- && !char.IsPunctuation(letters[index + 1] )
- && !char.IsNumber(letters[index + 1])
- && !char.IsSymbol(letters[index + 1])
- && !char.IsLower(letters[index + 1])
- && !char.IsUpper(letters[index + 1])
- && letters[index + 1] != (int)IsolatedArabicLetters.Hamza;
- if(lettersThatCannotBeBeforeALeadingLetter && lettersThatCannotBeALeadingLetter && lettersThatCannotBeAfterLeadingLetter)
- // if ((index == 0 || letters[index - 1] == ' ' || letters[index - 1] == '*' || letters[index - 1] == 'A' || char.IsPunctuation(letters[index - 1])
- // || letters[index - 1] == '>' || letters[index - 1] == '<'
- // || letters[index - 1] == (int)IsolatedArabicLetters.Alef
- // || letters[index - 1] == (int)IsolatedArabicLetters.Dal || letters[index - 1] == (int)IsolatedArabicLetters.Thal
- // || letters[index - 1] == (int)IsolatedArabicLetters.Ra2
- // || letters[index - 1] == (int)IsolatedArabicLetters.Zeen || letters[index - 1] == (int)IsolatedArabicLetters.PersianZe
- // || letters[index - 1] == (int)IsolatedArabicLetters.AlefMaksora || letters[index - 1] == (int)IsolatedArabicLetters.Waw
- // || letters[index - 1] == (int)IsolatedArabicLetters.AlefMad || letters[index - 1] == (int)IsolatedArabicLetters.AlefHamza
- // || letters[index - 1] == (int)IsolatedArabicLetters.AlefMaksoor || letters[index - 1] == (int)IsolatedArabicLetters.WawHamza)
- // && letters[index] != ' ' && letters[index] != (int)IsolatedArabicLetters.Dal
- // && letters[index] != (int)IsolatedArabicLetters.Thal
- // && letters[index] != (int)IsolatedArabicLetters.Ra2
- // && letters[index] != (int)IsolatedArabicLetters.Zeen && letters[index] != (int)IsolatedArabicLetters.PersianZe
- // && letters[index] != (int)IsolatedArabicLetters.Alef && letters[index] != (int)IsolatedArabicLetters.AlefHamza
- // && letters[index] != (int)IsolatedArabicLetters.AlefMaksoor
- // && letters[index] != (int)IsolatedArabicLetters.AlefMad
- // && letters[index] != (int)IsolatedArabicLetters.WawHamza
- // && letters[index] != (int)IsolatedArabicLetters.Waw
- // && letters[index] != (int)IsolatedArabicLetters.Hamza
- // && index < letters.Length - 1 && letters[index + 1] != ' ' && !char.IsPunctuation(letters[index + 1] ) && !char.IsNumber(letters[index + 1])
- // && letters[index + 1] != (int)IsolatedArabicLetters.Hamza )
- {
- return true;
- }
- else
- return false;
- }
-
- /// <summary>
- /// Checks if the letter at index value is a finishing character in Arabic or not.
- /// </summary>
- /// <param name="letters">The whole word that contains the character to be checked</param>
- /// <param name="index">The index of the character to be checked</param>
- /// <returns>True if the character at index is a finishing character, else, returns false</returns>
- internal static bool IsFinishingLetter(char[] letters, int index)
- {
- bool indexZero = index != 0;
- bool lettersThatCannotBeBeforeAFinishingLetter = (index == 0) ? false :
- letters[index - 1] != ' '
- // && char.IsDigit(letters[index-1])
- // && char.IsLower(letters[index-1])
- // && char.IsUpper(letters[index-1])
- // && char.IsNumber(letters[index-1])
- // && char.IsWhiteSpace(letters[index-1])
- // && char.IsPunctuation(letters[index-1])
- // && char.IsSymbol(letters[index-1])
- && letters[index - 1] != (int)IsolatedArabicLetters.Dal
- && letters[index - 1] != (int)IsolatedArabicLetters.Thal
- && letters[index - 1] != (int)IsolatedArabicLetters.Ra2
- && letters[index - 1] != (int)IsolatedArabicLetters.Zeen
- && letters[index - 1] != (int)IsolatedArabicLetters.PersianZe
- //&& letters[index - 1] != (int)IsolatedArabicLetters.AlefMaksora
- && letters[index - 1] != (int)IsolatedArabicLetters.Waw
- && letters[index - 1] != (int)IsolatedArabicLetters.Alef
- && letters[index - 1] != (int)IsolatedArabicLetters.AlefMad
- && letters[index - 1] != (int)IsolatedArabicLetters.AlefHamza
- && letters[index - 1] != (int)IsolatedArabicLetters.AlefMaksoor
- && letters[index - 1] != (int)IsolatedArabicLetters.WawHamza
- && letters[index - 1] != (int)IsolatedArabicLetters.Hamza
- && !char.IsPunctuation(letters[index - 1])
- && letters[index - 1] != '>'
- && letters[index - 1] != '<';
-
- bool lettersThatCannotBeFinishingLetters = letters[index] != ' ' && letters[index] != (int)IsolatedArabicLetters.Hamza;
-
- if(lettersThatCannotBeBeforeAFinishingLetter && lettersThatCannotBeFinishingLetters)
- // if (index != 0 && letters[index - 1] != ' ' && letters[index - 1] != '*' && letters[index - 1] != 'A'
- // && letters[index - 1] != (int)IsolatedArabicLetters.Dal && letters[index - 1] != (int)IsolatedArabicLetters.Thal
- // && letters[index - 1] != (int)IsolatedArabicLetters.Ra2
- // && letters[index - 1] != (int)IsolatedArabicLetters.Zeen && letters[index - 1] != (int)IsolatedArabicLetters.PersianZe
- // && letters[index - 1] != (int)IsolatedArabicLetters.AlefMaksora && letters[index - 1] != (int)IsolatedArabicLetters.Waw
- // && letters[index - 1] != (int)IsolatedArabicLetters.Alef && letters[index - 1] != (int)IsolatedArabicLetters.AlefMad
- // && letters[index - 1] != (int)IsolatedArabicLetters.AlefHamza && letters[index - 1] != (int)IsolatedArabicLetters.AlefMaksoor
- // && letters[index - 1] != (int)IsolatedArabicLetters.WawHamza && letters[index - 1] != (int)IsolatedArabicLetters.Hamza
- // && !char.IsPunctuation(letters[index - 1]) && letters[index - 1] != '>' && letters[index - 1] != '<'
- // && letters[index] != ' ' && index < letters.Length
- // && letters[index] != (int)IsolatedArabicLetters.Hamza)
- {
- //try
- //{
- // if (char.IsPunctuation(letters[index + 1]))
- // return true;
- // else
- // return false;
- //}
- //catch (Exception e)
- //{
- // return false;
- //}
-
- return true;
- }
- //return true;
- else
- return false;
- }
-
- /// <summary>
- /// Checks if the letter at index value is a middle character in Arabic or not.
- /// </summary>
- /// <param name="letters">The whole word that contains the character to be checked</param>
- /// <param name="index">The index of the character to be checked</param>
- /// <returns>True if the character at index is a middle character, else, returns false</returns>
- internal static bool IsMiddleLetter(char[] letters, int index)
- {
- bool lettersThatCannotBeMiddleLetters = (index == 0) ? false :
- letters[index] != (int)IsolatedArabicLetters.Alef
- && letters[index] != (int)IsolatedArabicLetters.Dal
- && letters[index] != (int)IsolatedArabicLetters.Thal
- && letters[index] != (int)IsolatedArabicLetters.Ra2
- && letters[index] != (int)IsolatedArabicLetters.Zeen
- && letters[index] != (int)IsolatedArabicLetters.PersianZe
- //&& letters[index] != (int)IsolatedArabicLetters.AlefMaksora
- && letters[index] != (int)IsolatedArabicLetters.Waw
- && letters[index] != (int)IsolatedArabicLetters.AlefMad
- && letters[index] != (int)IsolatedArabicLetters.AlefHamza
- && letters[index] != (int)IsolatedArabicLetters.AlefMaksoor
- && letters[index] != (int)IsolatedArabicLetters.WawHamza
- && letters[index] != (int)IsolatedArabicLetters.Hamza;
- bool lettersThatCannotBeBeforeMiddleCharacters = (index == 0) ? false :
- letters[index - 1] != (int)IsolatedArabicLetters.Alef
- && letters[index - 1] != (int)IsolatedArabicLetters.Dal
- && letters[index - 1] != (int)IsolatedArabicLetters.Thal
- && letters[index - 1] != (int)IsolatedArabicLetters.Ra2
- && letters[index - 1] != (int)IsolatedArabicLetters.Zeen
- && letters[index - 1] != (int)IsolatedArabicLetters.PersianZe
- //&& letters[index - 1] != (int)IsolatedArabicLetters.AlefMaksora
- && letters[index - 1] != (int)IsolatedArabicLetters.Waw
- && letters[index - 1] != (int)IsolatedArabicLetters.AlefMad
- && letters[index - 1] != (int)IsolatedArabicLetters.AlefHamza
- && letters[index - 1] != (int)IsolatedArabicLetters.AlefMaksoor
- && letters[index - 1] != (int)IsolatedArabicLetters.WawHamza
- && letters[index - 1] != (int)IsolatedArabicLetters.Hamza
- && !char.IsPunctuation(letters[index - 1])
- && letters[index - 1] != '>'
- && letters[index - 1] != '<'
- && letters[index - 1] != ' '
- && letters[index - 1] != '*';
- bool lettersThatCannotBeAfterMiddleCharacters = (index >= letters.Length - 1) ? false :
- letters[index + 1] != ' '
- && letters[index + 1] != '\r'
- && letters[index + 1] != (int)IsolatedArabicLetters.Hamza
- && !char.IsNumber(letters[index + 1])
- && !char.IsSymbol(letters[index + 1])
- && !char.IsPunctuation(letters[index + 1]);
- if(lettersThatCannotBeAfterMiddleCharacters && lettersThatCannotBeBeforeMiddleCharacters && lettersThatCannotBeMiddleLetters)
- // if (index != 0 && letters[index] != ' '
- // && letters[index] != (int)IsolatedArabicLetters.Alef && letters[index] != (int)IsolatedArabicLetters.Dal
- // && letters[index] != (int)IsolatedArabicLetters.Thal && letters[index] != (int)IsolatedArabicLetters.Ra2
- // && letters[index] != (int)IsolatedArabicLetters.Zeen && letters[index] != (int)IsolatedArabicLetters.PersianZe
- // && letters[index] != (int)IsolatedArabicLetters.AlefMaksora
- // && letters[index] != (int)IsolatedArabicLetters.Waw && letters[index] != (int)IsolatedArabicLetters.AlefMad
- // && letters[index] != (int)IsolatedArabicLetters.AlefHamza && letters[index] != (int)IsolatedArabicLetters.AlefMaksoor
- // && letters[index] != (int)IsolatedArabicLetters.WawHamza && letters[index] != (int)IsolatedArabicLetters.Hamza
- // && letters[index - 1] != (int)IsolatedArabicLetters.Alef && letters[index - 1] != (int)IsolatedArabicLetters.Dal
- // && letters[index - 1] != (int)IsolatedArabicLetters.Thal && letters[index - 1] != (int)IsolatedArabicLetters.Ra2
- // && letters[index - 1] != (int)IsolatedArabicLetters.Zeen && letters[index - 1] != (int)IsolatedArabicLetters.PersianZe
- // && letters[index - 1] != (int)IsolatedArabicLetters.AlefMaksora
- // && letters[index - 1] != (int)IsolatedArabicLetters.Waw && letters[index - 1] != (int)IsolatedArabicLetters.AlefMad
- // && letters[index - 1] != (int)IsolatedArabicLetters.AlefHamza && letters[index - 1] != (int)IsolatedArabicLetters.AlefMaksoor
- // && letters[index - 1] != (int)IsolatedArabicLetters.WawHamza && letters[index - 1] != (int)IsolatedArabicLetters.Hamza
- // && letters[index - 1] != '>' && letters[index - 1] != '<'
- // && letters[index - 1] != ' ' && letters[index - 1] != '*' && !char.IsPunctuation(letters[index - 1])
- // && index < letters.Length - 1 && letters[index + 1] != ' ' && letters[index + 1] != '\r' && letters[index + 1] != 'A'
- // && letters[index + 1] != '>' && letters[index + 1] != '>' && letters[index + 1] != (int)IsolatedArabicLetters.Hamza
- // )
- {
- try
- {
- if (char.IsPunctuation(letters[index + 1]))
- return false;
- else
- return true;
- }
- catch
- {
- return false;
- }
- //return true;
- }
- else
- return false;
- }
-
-
- }
|