Mega Code Archive
Reshaping Arabic Sentences and Text Utilities to deal with Arabic
//package org.amr.arabic;
/*
* Date : 8th of June 2009
* the class is Arabic string reshaper Utilities, this class is targeting Android platform
*
* By : Amr Ismail Gawish
* E-Mail : amr.gawish@gmail.com
* Web : http://www.amr-gawish.com
*
* Updated : 8th of June 2009
* Adding comments and Announcing Open Source
*
* Updated: 6th of May 2010
* Enahancing Functionality by Amine : bakhtout@gmail.com
*
* */
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Typeface;
import android.view.Gravity;
import android.widget.TextView;
/**
* This class is the main class that is responsible for Reshaping Arabic Sentences and Text
* Utilities Class to make it easier to deal with Arabic Reshaper Class
* Wrapper for Arabic Reshaper Class
* @author Amr Gawish
*/
public class ArabicUtilities {
/**
* the path of teh fonts file must be under assets folder
*/
private static final String FONTS_LOCATION_PATH = "fonts/me_quran.ttf";
static Typeface face ;
/**
* Helper function is to check if the character passed, is Arabic
* @param target The Character to check Against
* @return true if the Character is Arabic letter, otherwise returns false
*/
private static boolean isArabicCharacter(char target){
//Iterate over the 36 Characters in ARABIC_GLPHIES Matrix
for(int i = 0; i < ArabicReshaper.ARABIC_GLPHIES.length;i++){
//Check if the target Character exist in ARABIC_GLPHIES Matrix
if(ArabicReshaper.ARABIC_GLPHIES[i][0]==target)
return true;
}
for(int i = 0; i < ArabicReshaper.HARAKATE.length;i++){
//Check if the target Character exist in ARABIC_GLPHIES Matrix
if(ArabicReshaper.HARAKATE[i]==target)
return true;
}
return false;
}
/**
* Helper function to split Sentence By Space
* @param sentence the Sentence to Split into Array of Words
* @return Array Of words
*/
private static String[] getWords(String sentence){
if (sentence != null) {
return sentence.split("\\s");
} else {
return new String[0];
}
}
/**
* Helper function to check if the word has Arabic Letters
* @param word The to check Against
* @return true if the word has Arabic letters, false otherwise
*/
public static boolean hasArabicLetters(String word){
//Iterate over the word to check all the word's letters
for(int i=0;i 0;
}
private char getHaraka(char target){
//Iterate over the 36 characters in the GLPHIES Matrix
for(int n = 0; n0){
return (char)getLamAlef(alef,lam,true)+" ";
}
}
//For the First Letter
reshapedLetters[0]=getReshapedGlphy(wordLetters[0], 2);
//The current Letter
char currentLetter=wordLetters[0];
/**
* The Main Iterator
*/
//Iterate over the word from the second character till the second to the last
for(int i=1;i0){
//Check if the Letter before the Lam is 2 Forms Letter, to Make the Lam Alef as its the end of the Word
if((i-2 < 0) || ((i-2 >= 0) && (getGlphyType(wordLetters[i-2])==2))){
//Mark the letter of Lam as Lam Indicator
reshapedLetters[i-1]=lamIndicator;
//Assign Lam Alef to the Letter of Alef
reshapedLetters[i]=(char)getLamAlef(wordLetters[i], currentLetter, true);
}else{ //The Letter before the Lam is more than 2 Forms Letter
//Mark the letter of Lam as Lam Indicator
reshapedLetters[i-1]=lamIndicator;
//Assign Lam Alef to the Letter of Alef
reshapedLetters[i]=(char)getLamAlef(wordLetters[i], currentLetter, false);
}
}else{ //The Word doesn't have LamAlef
int beforeLast=i-1;
//Check if the Letter Before Last has only 2 Forms, for the current Letter to be as a start for a new Word!
if(getGlphyType(wordLetters[beforeLast])==2){
//If the letter has only 2 shapes, then it doesnt matter which position it is, It'll be always the second form
reshapedLetters[i]=getReshapedGlphy(wordLetters[i], 2);
}else{
//Then it should be in the middle which should be placed in its right form [3]
reshapedLetters[i]=getReshapedGlphy(wordLetters[i], 3);
}
}
//Assign the CurrentLetter as the Word Letter
currentLetter=wordLetters[i];
}
/**
* The Last Letters Check
*/
//Check if the Letters are Lam Alef
if(getLamAlef(wordLetters[wordLength-1], wordLetters[wordLength-2], true)>0){
//Check if the Letter before the Lam is 2 Forms Letter, to Make the Lam Alef as its the end of the Word
if(getGlphyType(wordLetters[wordLength-3])==2){ //check for the last letter
//Mark the letter of Lam as Lam Indicator
reshapedLetters[wordLength-2]=lamIndicator;
//Assign Lam Alef to the Letter of Alef
reshapedLetters[wordLength-1]=(char)getLamAlef(wordLetters[wordLength-1], wordLetters[wordLength-2], true);
}else {
//Mark the letter of Lam as Lam Indicator
reshapedLetters[wordLength-2]=lamIndicator;
//Assign Lam Alef to the Letter of Alef
reshapedLetters[wordLength-1]=(char)getLamAlef(wordLetters[wordLength-1], wordLetters[wordLength-2], false);
}
}else {
//check for the last letter Before last has 2 forms, that means that the last Letter will be alone.
if(getGlphyType(wordLetters[wordLength-2])==2){
//If the letter has only 2 shapes, then it doesn't matter which position it is, It'll be always the second form
reshapedLetters[wordLength-1]=getReshapedGlphy(wordLetters[wordLength-1], 1);
}else {
//Put the right form of the character, 4 for the last letter in the word
reshapedLetters[wordLength-1]=getReshapedGlphy(wordLetters[wordLength-1], 4);
}
}
/**
* Assign the Final Results of Shaped Word
*/
//Iterate over the Reshaped Letters and remove the Lam Indicators
for(int i=0;i