Pascal code to lua for Arabic

Hi everyone ;
I need help to convert a pascal code(or some of its functions) to lua, the code help link Arabic glyphs,reverse them  and execlude numbers(as Arabic is bidirectional).

 

//FUNCTION TO REPLACE PATTERNS OCCURNCE Function WideStringReplace(const S, OldPattern, NewPattern: WideString; Flags: TReplaceFlags): WideString; var Srch,OldP,RemS: WideString; // Srch and Oldp can contain uppercase versions of S,OldPattern P : Integer; begin Srch:=S; OldP:=OldPattern; if rfIgnoreCase in Flags then begin Srch:=WideUpperCase(Srch); OldP:=WideUpperCase(OldP); end; RemS:=S; Result:=''; while (Length(Srch)\<\>0) do begin P:=Pos(OldP, Srch); if P=0 then begin Result:=Result+RemS; Srch:=''; end else begin Result:=Result+Copy(RemS,1,P-1)+NewPattern; P:=P+Length(OldP); RemS:=Copy(RemS,P,Length(RemS)-P+1); if not (rfReplaceAll in Flags) then begin Result:=Result+RemS; Srch:=''; end else Srch:=Copy(Srch,P,Length(Srch)-P+1); end; end; end; //REVERSE LETTERS AND EXECLUDE NUMBERS function reverse( atext:widestring):widestring; var i:integer; wdcr1,wdcr2,text:widestring; begin for I :=0 to Length(Atext)-1 do begin if I \< Length(Atext) then begin wdcr1 :=Atext[I+1]; if ((wdcr1 \>= #$0030) and (wdcr1 \<= #$0039 )) or ((wdcr1 \>= #$0660) and (wdcr1 \<= #$0669)) then begin text:=wdcr1+text; Insert( wdcr1,result,length(text)); wdcr1:=#0; end else begin Insert( wdcr1,result,1); wdcr1:=#0; text:=''; end; end ; end; end; /////////////////////////////////////////////// //A TABLE OF ARABIC GLYPHS ACCORDING TO UNICODE WITH DIFFRENT GLYPH SHAPE FOR EACH LETTER ACCORDING TO ITS POSITION type TGlyphRecord = record Isolated, Final, Initial, Medial: WideChar; end; const GlyphTable: array[#$0621..#$0651] of TGlyphRecord = ( (Isolated:#$fe80), (Isolated:#$fe81; Final:#$fe82), (Isolated:#$fe83; Final:#$fe84), (Isolated:#$fe85;final:#$fe86), (Isolated:#$FE87;Final:#$FE88), //hemza teht ya (isolated:#$FE89;Final:#$fe8a; Initial:#$fe8b; Medial:#$fe8c), (Isolated:#$fe8d; Final:#$fe8e), // alef (isolated:#$FE8F;Final:#$fe90; Initial:#$fe91; Medial:#$fe92), //ba repeated (Isolated:#$fe93;Final:#$fe94), //ta marbootah (Isolated:#$fe95; Final:#$fe96; Initial:#$fe97; Medial:#$fe98), //ta (Isolated:#$fe99;Final:#$fe9a; Initial:#$fe9b; Medial:#$fe9c), //tha (Isolated:#$fe9d;Final:#$fe9e; Initial:#$fe9f; Medial:#$FEA0), //jeem (Isolated:#$fea1; Final:#$fea2; Initial:#$fea3; Medial:#$FEA4), // ha (Isolated:#$fea5; Final:#$fea6; Initial:#$fea7; Medial:#$FEA8), //kha (Isolated:#$fea9;Final:#$feaa), //dal (Isolated:#$feab;Final:#$feac), //dhal (Isolated:#$fead;Final:#$feae;), // Initial:#$feae; Medial:#$feae //ra (Isolated:#$feaf;Final:#$feb0), //za (Isolated:#$feb1;Final:#$feb2; Initial:#$feb3; Medial:#$feb4 ), //seen (Isolated:#$feb5; Final:#$feb6; Initial:#$feb7; Medial:#$feb8), //sheen (Isolated:#$feb9; Final:#$feba; Initial:#$febb; Medial:#$febc), //ssad (Isolated:#$febd; Final:#$febe; Initial:#$febf; Medial:#$fec0), //dhad (Isolated:#$fec1;Final:#$fec2; Initial:#$fec3; Medial:#$fec4), //tda (Isolated:#$fec5;Final:#$fec6; Initial:#$fec7; Medial:#$fec8), //thda (Isolated:#$fec9; Final:#$feca; Initial:#$fecb; Medial:#$fecc), //ain (Isolated:#$fecd; Final:#$fece; Initial:#$fecf; Medial:#$fed0), //ghain (), (), (), (), (), (), (Isolated:#$fed1; Final:#$fed2; Initial:#$fed3; Medial:#$fed4), //fa (Isolated:#$fed5; Final:#$fed6; Initial:#$fed7; Medial:#$fed8), //gaf (Isolated:#$fed9; Final:#$feda; Initial:#$fedb; Medial:#$fedc), //kaf (Isolated:#$fedd;final:#$fede; Initial:#$fedf; Medial:#$fee0), //lam (Isolated:#$fee1;final:#$fee2; Initial:#$fee3;medial:#$Fee4), //meem (Isolated:#$fee5;final:#$fee6; Initial:#$fee7;medial:#$fee8), //noon //noon (Isolated:#$fee9;final:#$feea; Initial:#$feeb; Medial:#$feec), //heh (Isolated:#$feed;Final:#$feee), //wow (Isolated:#$feef; Final:#$fef0), //alef mongaleba ya (Isolated:#$fef1; Final:#$fef2; Initial:#$fef3;Medial:#$fef4), //ya (Isolated:#$FEFB;Final:#$FEFc;medial:#$FEFC), (Isolated:#$061F), (), (), (), (), (Medial:#$fe7d) ); //FUNCTION FOR CHOOSING FROM THE ABOVE UNICODE TABLE DUE TO LAST POSITION OCCURMENT function wrt(text:widestring): WideString; const Alflam: array[1..8] of widestring= (#$fedf#$FE8e, #$fee0#$FE8e, #$fedf#$FE84, #$fee0#$FE84,#$fedf#$FE88, #$fee0#$FE88, #$fedf#$FE82,#$fee0#$FE82) ; const Alf: array[1..8] of widechar= (#$fefb,#$fefc,#$fef7,#$fef8, #$fef9,#$fefa , #$fef5,#$fef6); var c,I: Integer; change, LinkBefore, LinkAfter: Boolean; Current, Next: WideChar; Glyph: TGlyphRecord; cn1:integer; BEGIN LinkBefore := False; //////////////////// for I :=1 to Length(Text) do begin Current := Text[I]; if I \< Length(Text) then Next := Text[I + 1] else Next := #0; change:=(current=#$0623)and(next=#$0644); LinkAfter := (Next \> #$0620) and (Next \<= #$0651) and ((GlyphTable[Next].Final \<\> #0) or (GlyphTable[Next].Medial \<\> #0)); if (Current \>= #$0621) and (Current \<= #$0651) then begin Glyph := GlyphTable[Current]; if LinkBefore and LinkAfter and (Glyph.Medial \<\> #0) then Text[I] := Glyph.Medial else if LinkAfter and (Glyph.Initial \<\> #0) then Text[I] := Glyph.Initial else if LinkBefore and (Glyph.Final \<\> #0) then Text[I] := Glyph.Final else if Glyph.Isolated \<\> #0 then Text[I] := Glyph.Isolated; LinkBefore := ((Glyph.Initial \<\> #0) or (Glyph.Medial \<\> #0)) and LinkAfter; end else LinkBefore := False; // cn2:=0; end; for cn1:=1 to high(alf) do begin text:= WideStringReplace( text,alflam[cn1],alf[cn1],[rfReplaceAll, rfIgnoreCase] ); end; result:=reverse(text); exit; end; Function Editarabic(text:string):string; Begin if lang=1 then result:=text; if lang=2 then result:=utf8encode(widestring(wrt(utf8decode(text))) ); exit; end; end.

Honestly I don’t think anyone will help. This can take a lot of time and knowledge. You can def pay someone. Prob won’t be cheap. I might be 100% wrong though.

thanx  InfiSnyp for your quick reply, I’ll try my best on that one, it is only I’m kind of newbie to Lua and need some guidelines, instructions or any kind of help

The way I started by just writing code in video tutorials.

http://www.youtube.com/playlist?list=PLfjNlDrVXRK0NVKsqHgbG9RdOTp5K3Pos

This might help as well

https://docs.coronalabs.com/guide/start/helloWorld/index.html

Ya Mark has some great tuts for begginers.

Taking it as a practice, have come up with the following lua module that enables Arabic to work with a bitmap font  :) 

 

local arabic = {} local utf8=require "utf8" local glyphTable= {} glyphTable= { {isolated=0XFE80}, {isolated=0XFE81, final=0XFE82}, {isolated=0XFE83, final=0XFE84}, {isolated=0XFE85,final=0XFE86}, {isolated=0XFE87,final=0XFE88}, --HEMZA TEHT YA {isolated=0XFE89,final=0XFE8A, initial=0XFE8B, medial=0XFE8C}, {isolated=0XFE8D, final=0XFE8E}, --ALF -- ALEF {isolated=0XFE8F,final=0XFE90, initial=0XFE91, medial=0XFE92}, --BA REPEATED {isolated=0XFE93,final=0XFE94}, --TA MARBOOTAH {isolated=0XFE95, final=0XFE96, initial=0XFE97, medial=0XFE98}, --TA {isolated=0XFE99,final=0XFE9A, initial=0XFE9B, medial=0XFE9C}, --THA {isolated=0XFE9D,final=0XFE9E, initial=0XFE9F, medial=0XFEA0}, --JEEM {isolated=0XFEA1, final=0XFEA2, initial=0XFEA3, medial=0XFEA4}, -- HA {isolated=0XFEA5, final=0XFEA6, initial=0XFEA7, medial=0XFEA8}, --KHA {isolated=0XFEA9,final=0XFEAA}, --DAL {isolated=0XFEAB,final=0XFEAC}, --DHAL {isolated=0XFEAD,final=0XFEAE}, -- initial=0XFEAE, medial=0XFEAE --RA {isolated=0XFEAF,final=0XFEB0}, --ZA {isolated=0XFEB1,final=0XFEB2, initial=0XFEB3, medial=0XFEB4 }, --SEEN {isolated=0XFEB5, final=0XFEB6, initial=0XFEB7, medial=0XFEB8}, --SHEEN {isolated=0XFEB9, final=0XFEBA, initial=0XFEBB, medial=0XFEBC}, --SSAD {isolated=0XFEBD, final=0XFEBE, initial=0XFEBF, medial=0XFEC0}, --DHAD {isolated=0XFEC1,final=0XFEC2, initial=0XFEC3, medial=0XFEC4}, --TDA {isolated=0XFEC5,final=0XFEC6, initial=0XFEC7, medial=0XFEC8}, --THDA {isolated=0XFEC9, final=0XFECA, initial=0XFECB, medial=0XFECC}, --AIN {isolated=0XFECD, final=0XFECE, initial=0XFECF, medial=0XFED0}, --GHAIN {}, {}, {}, {}, {}, {}, {isolated=0XFED1, final=0XFED2, initial=0XFED3, medial=0XFED4}, --FA {isolated=0XFED5, final=0XFED6, initial=0XFED7, medial=0XFED8}, --GAF {isolated=0XFED9, final=0XFEDA, initial=0XFEDB, medial=0XFEDC}, --KAF {isolated=0XFEDD,final=0XFEDE, initial=0XFEDF, medial=0XFEE0}, --LAM {isolated=0XFEE1,final=0XFEE2, initial=0XFEE3,medial=0XFEE4}, --MEEM {isolated=0XFEE5,final=0XFEE6, initial=0XFEE7,medial=0XFEE8}, --NOON --NOON {isolated=0XFEE9,final=0XFEEA, initial=0XFEEB, medial=0XFEEC}, --HEH {isolated=0XFEED,final=0XFEEE}, --WOW {isolated=0XFEEF, final=0XFEF0}, --ALEF MONGALEBA YA {isolated=0XFEF1, final=0XFEF2, initial=0XFEF3,medial=0XFEF4}, --YA {isolated=0XFEFB,final=0XFEFC}, {isolated=0X061F}, {}, {}, {}, {}, {medial=0XFE7D} } local n=1568 function renameKeys( ) -- body for k, v in ipairs(glyphTable) do if k\<=(#glyphTable+1) then local c = '' n=n+1 c=utf8.char( n ) glyphTable[c]=glyphTable[k] end end end function delOldKeys( ) for k, v in pairs(glyphTable) do if (type(k)) =="number" then glyphTable[k]=nil end end end function arabic.init() for k,v in pairs(glyphTable)do if glyphTable[k].isolated then glyphTable[k].isolated=utf8.char(glyphTable[k].isolated) end if glyphTable[k].initial then glyphTable[k].initial=utf8.char(glyphTable[k].initial) end if glyphTable[k].final then glyphTable[k].final=utf8.char(glyphTable[k].final) end if glyphTable[k].medial then glyphTable[k].medial=utf8.char(glyphTable[k].medial) end end renameKeys() delOldKeys() end --setmetatable(glyphTable,{\_\_index=charit()} ) local glyph={}--{ isolated='', final=' ', initial=' ', medial=' '} local first,last =utf8.char(0x0620),utf8.char(0x0651) local ts local st = '' local current, nxt='' local linkBefore,linkAfter=false,false function linkChars( s) local text={}--utf8.reverse(s) local s= s:gsub('%d+',s.reverse) local text = {} for i = 1, utf8.len(s) do text[i] = utf8.sub( s , i , i ) end linkBefore = false for i=1,utf8.len(s) do current = utf8.sub(s,i,i) if i \< utf8.len(s) then nxt = utf8.sub( s , i+1 , i+1 ) else nxt ='' end linkAfter = ( nxt \> first and nxt \< last ) and ( glyphTable[nxt].final ~= '' or glyphTable[nxt].medial~='' ) if ( current \>= first and current \<= last) then glyph = glyphTable[current] if ( linkBefore and linkAfter ) and ( glyph.medial ) then text[i] = glyph.medial elseif linkAfter and (glyph.initial) then text[i] = glyph.initial elseif linkBefore and( glyph.final ) then text[i]=glyph.final elseif glyph.isolated then text[i] = glyph.isolated end linkBefore = (glyph.initial or glyph.medial ) and linkAfter else linkBefore = false end end for \_, v in ipairs(text) do st=st..v end return st:gsub("([\194-\244][\128-\191]\*)", string.reverse):reverse() end

Honestly I don’t think anyone will help. This can take a lot of time and knowledge. You can def pay someone. Prob won’t be cheap. I might be 100% wrong though.

thanx  InfiSnyp for your quick reply, I’ll try my best on that one, it is only I’m kind of newbie to Lua and need some guidelines, instructions or any kind of help

The way I started by just writing code in video tutorials.

http://www.youtube.com/playlist?list=PLfjNlDrVXRK0NVKsqHgbG9RdOTp5K3Pos

This might help as well

https://docs.coronalabs.com/guide/start/helloWorld/index.html

Ya Mark has some great tuts for begginers.

Taking it as a practice, have come up with the following lua module that enables Arabic to work with a bitmap font  :) 

 

local arabic = {} local utf8=require "utf8" local glyphTable= {} glyphTable= { {isolated=0XFE80}, {isolated=0XFE81, final=0XFE82}, {isolated=0XFE83, final=0XFE84}, {isolated=0XFE85,final=0XFE86}, {isolated=0XFE87,final=0XFE88}, --HEMZA TEHT YA {isolated=0XFE89,final=0XFE8A, initial=0XFE8B, medial=0XFE8C}, {isolated=0XFE8D, final=0XFE8E}, --ALF -- ALEF {isolated=0XFE8F,final=0XFE90, initial=0XFE91, medial=0XFE92}, --BA REPEATED {isolated=0XFE93,final=0XFE94}, --TA MARBOOTAH {isolated=0XFE95, final=0XFE96, initial=0XFE97, medial=0XFE98}, --TA {isolated=0XFE99,final=0XFE9A, initial=0XFE9B, medial=0XFE9C}, --THA {isolated=0XFE9D,final=0XFE9E, initial=0XFE9F, medial=0XFEA0}, --JEEM {isolated=0XFEA1, final=0XFEA2, initial=0XFEA3, medial=0XFEA4}, -- HA {isolated=0XFEA5, final=0XFEA6, initial=0XFEA7, medial=0XFEA8}, --KHA {isolated=0XFEA9,final=0XFEAA}, --DAL {isolated=0XFEAB,final=0XFEAC}, --DHAL {isolated=0XFEAD,final=0XFEAE}, -- initial=0XFEAE, medial=0XFEAE --RA {isolated=0XFEAF,final=0XFEB0}, --ZA {isolated=0XFEB1,final=0XFEB2, initial=0XFEB3, medial=0XFEB4 }, --SEEN {isolated=0XFEB5, final=0XFEB6, initial=0XFEB7, medial=0XFEB8}, --SHEEN {isolated=0XFEB9, final=0XFEBA, initial=0XFEBB, medial=0XFEBC}, --SSAD {isolated=0XFEBD, final=0XFEBE, initial=0XFEBF, medial=0XFEC0}, --DHAD {isolated=0XFEC1,final=0XFEC2, initial=0XFEC3, medial=0XFEC4}, --TDA {isolated=0XFEC5,final=0XFEC6, initial=0XFEC7, medial=0XFEC8}, --THDA {isolated=0XFEC9, final=0XFECA, initial=0XFECB, medial=0XFECC}, --AIN {isolated=0XFECD, final=0XFECE, initial=0XFECF, medial=0XFED0}, --GHAIN {}, {}, {}, {}, {}, {}, {isolated=0XFED1, final=0XFED2, initial=0XFED3, medial=0XFED4}, --FA {isolated=0XFED5, final=0XFED6, initial=0XFED7, medial=0XFED8}, --GAF {isolated=0XFED9, final=0XFEDA, initial=0XFEDB, medial=0XFEDC}, --KAF {isolated=0XFEDD,final=0XFEDE, initial=0XFEDF, medial=0XFEE0}, --LAM {isolated=0XFEE1,final=0XFEE2, initial=0XFEE3,medial=0XFEE4}, --MEEM {isolated=0XFEE5,final=0XFEE6, initial=0XFEE7,medial=0XFEE8}, --NOON --NOON {isolated=0XFEE9,final=0XFEEA, initial=0XFEEB, medial=0XFEEC}, --HEH {isolated=0XFEED,final=0XFEEE}, --WOW {isolated=0XFEEF, final=0XFEF0}, --ALEF MONGALEBA YA {isolated=0XFEF1, final=0XFEF2, initial=0XFEF3,medial=0XFEF4}, --YA {isolated=0XFEFB,final=0XFEFC}, {isolated=0X061F}, {}, {}, {}, {}, {medial=0XFE7D} } local n=1568 function renameKeys( ) -- body for k, v in ipairs(glyphTable) do if k\<=(#glyphTable+1) then local c = '' n=n+1 c=utf8.char( n ) glyphTable[c]=glyphTable[k] end end end function delOldKeys( ) for k, v in pairs(glyphTable) do if (type(k)) =="number" then glyphTable[k]=nil end end end function arabic.init() for k,v in pairs(glyphTable)do if glyphTable[k].isolated then glyphTable[k].isolated=utf8.char(glyphTable[k].isolated) end if glyphTable[k].initial then glyphTable[k].initial=utf8.char(glyphTable[k].initial) end if glyphTable[k].final then glyphTable[k].final=utf8.char(glyphTable[k].final) end if glyphTable[k].medial then glyphTable[k].medial=utf8.char(glyphTable[k].medial) end end renameKeys() delOldKeys() end --setmetatable(glyphTable,{\_\_index=charit()} ) local glyph={}--{ isolated='', final=' ', initial=' ', medial=' '} local first,last =utf8.char(0x0620),utf8.char(0x0651) local ts local st = '' local current, nxt='' local linkBefore,linkAfter=false,false function linkChars( s) local text={}--utf8.reverse(s) local s= s:gsub('%d+',s.reverse) local text = {} for i = 1, utf8.len(s) do text[i] = utf8.sub( s , i , i ) end linkBefore = false for i=1,utf8.len(s) do current = utf8.sub(s,i,i) if i \< utf8.len(s) then nxt = utf8.sub( s , i+1 , i+1 ) else nxt ='' end linkAfter = ( nxt \> first and nxt \< last ) and ( glyphTable[nxt].final ~= '' or glyphTable[nxt].medial~='' ) if ( current \>= first and current \<= last) then glyph = glyphTable[current] if ( linkBefore and linkAfter ) and ( glyph.medial ) then text[i] = glyph.medial elseif linkAfter and (glyph.initial) then text[i] = glyph.initial elseif linkBefore and( glyph.final ) then text[i]=glyph.final elseif glyph.isolated then text[i] = glyph.isolated end linkBefore = (glyph.initial or glyph.medial ) and linkAfter else linkBefore = false end end for \_, v in ipairs(text) do st=st..v end return st:gsub("([\194-\244][\128-\191]\*)", string.reverse):reverse() end