2016-06-29 54 views
0

將以下代碼從Pascal翻譯爲Lua,其目的是將其作爲阿拉伯語腳本 - 形成阿拉伯語字符串,並將表中存儲的有序字形類型與位圖字體一起使用。 它適用於具有唯一字符的字符串,例如:字符串(「أحمدقطعالجزر」)可以正常工作,但是當重複字符時會出現問題,請考慮將三個字符串(「مهم」)形成爲鏈接字形像這樣(「مهم」)爲(م)s(而不是(مهم))返回第一個字形類型,循環總是爲重複字符提供第一個字形類型。
這裏是循環: 在Lua中循環顯示阿拉伯字符串和等效表格字形?

local current, nxt = '' 

local linkBefore , linkAfter = false , false 

function linkChars(text) 

    linkBefore = false 



    for i = 1 , utf8.len(text) do 

     current = utf8.sub(text , i , i) 

     if i < utf8.len(text) then 

      nxt = utf8.sub(text , 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) and current ~= nxt then 

      glyph= glyphTable[current] 

      if (linkBefore and linkAfter) and (glyph.medial) then 

       text =string.gsub(text,current,glyph.medial) 

      elseif linkAfter and (glyph.initial) then 

       text =string.gsub(text , current , glyph.initial) 

      elseif linkBefore and(glyph.final) then 

       text =string.gsub(text , current , glyph.final) 

      elseif glyph.isolated then 

       text =string.gsub(text , current , glyph.isolated) 

      end 

      linkBefore = (glyph.initial or glyph.medial) and linkAfter 

     else 

      linkBefore = false 

     end 

    end 

    text=text:gsub("([\194-\244][\128-\191]*)" , string.reverse):reverse() 

    return text 

end 

可能,我做錯了什麼是
的(對)的循環遍歷所有的字符爲每個條件時,我必須通過每個循環的條件炭?然而 ,如何使其正常工作?

這是全碼:

-- uses external utf8 file 

    local utf8=require "utf8" 

    --Arabic glyphs table 

    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}, 

-- alef 

    {isolated=0xfe8f,final=0xfe90, initial=0xfe91, medial=0xfe92}, 

    --ba 

    {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}, 


    --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   

    {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} 


    } 

--renaming nested tables, each table takes the name for 
--its equivlent Arabic char 

local n=1568 

function renameKeys() 


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 





    local glyph={} 

    local first , last =utf8.char(0x0620) , utf8.char(0x0651) 



    local current , nxt = '' 

    local linkBefore , linkAfter = false , false 

    -- the main Arabic script function 

    function linkChars(text) 

    linkBefore = false 

    for i= 1 , utf8.len(text) do 

    current = utf8.sub(text , i , i) 

    if i < utf8.len(text) then 


     nxt = utf8.sub(text , 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) and current ~= nxt then 

     glyph = glyphTable[current] 

    if (linkBefore and linkAfter) and (glyph.medial) then 

    text = string.gsub(text , current , glyph.medial) 


    elseif linkAfter and (glyph.initial) then 

    text = string.gsub(text , current , glyph.initial) 

    elseif linkBefore and(glyph.final) then 

    text = string.gsub(text , current , glyph.final) 

    elseif glyph.isolated then 

    text = string.gsub(text , current , glyph.isolated) 

    end 

    linkBefore = (glyph.initial or glyph.medial) and linkAfter 

    else 

    linkBefore = false 

    end 

    end 

    text= text:gsub("([\194-\244][\128-\191]*)" , string.reverse) 

    return text 

    end 
+1

你的代碼太凌亂,請正確格式化代碼。 –

+0

完成了,希望現在很清楚 –

+0

@hasanbaabad 我修好了你的想法。我希望你在你的編輯器上比在這裏做得更好:) – Piglet

回答

0

字符串表已經解決了這個問題,在這裏:

function linkChars(s) 


    local st='' 

    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~='') and nxt~ = '' 


    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 k, v in ipairs(text) do 
     st=st..v 
    end 

    return st:gsub("([\194-\244][\128-\191]*)", string.reverse):reverse() 

    end