-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
130 lines (111 loc) · 3.68 KB
/
main.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
io.stdout:setvbuf("no")
local bit = require("bit")
local utf8 = require("utf8")
--local tohex = bit.tohex
--require("mobdebug").start()
--local font = love.graphics.newFont("Vazir-FD-WOL.ttf",44)
local font = love.graphics.newFont("tahoma.ttf",22)
local persianText = "حرف a اولین حرف در زبان انگلیسی است."
persianText = "حروف گ و چ و پ و ژ در زبان عربی وجود ندارند" .. "\n" .. persianText
local varientsCount = {1,2,2,2,2, 4,2,4,2,4, 4,4,4,4,2, 2,2,2,4,4, 4,4,4,4,4,4, 4,4,4,4,4, 4,4,2,2,4, 4,4,2,4,4, 4}
local persianLettersHex = {[0x067E] = 0xFB56, [0x0686] = 0xFB7A, [0x0698] = 0xFB8A, [0x06A9] = 0xFB8E, [0x06AF] = 0xFB92, [0x06CC] = 0xFBFC}
local varientPos = 1
local nextVarient = 0xFE80
local connectableCharacters = {}
for i=0x0621, 0x064A do -- 1569 1610
if i <= 0x063A or i >= 0x0641 then
local char = utf8.char(i)
local varients = {}
for n=1,varientsCount[varientPos] do
varients[n] = utf8.char(nextVarient)
nextVarient = nextVarient + 1
end
varientPos = varientPos + 1
connectableCharacters[char] = varients
end
end
function pairsByKeys (t, f)
local a = {}
for n in pairs(t) do table.insert(a, n) end
table.sort(a, f)
local i = 0 -- iterator variable
local iter = function () -- iterator function
i = i + 1
if a[i] == nil then return nil
else return a[i], t[a[i]]
end
end
return iter
end
for i,nextVarient in pairsByKeys(persianLettersHex) do
char = utf8.char(i)
varients = {}
for n=1,varientsCount[varientPos] do
varients[n] = utf8.char(nextVarient)
nextVarient = nextVarient + 1
end
varientPos = varientPos + 1
connectableCharacters[char] = varients
end
local function processPersian(text)
local length = utf8.len(text)
local proc1 = {}
local iter1 = string.gmatch(text,utf8.charpattern)
local iter2 = string.gmatch(text,utf8.charpattern)
local prevChar, nextChar = " ", " "
iter2()
for char in iter1 do
local codepoint = utf8.codepoint(char)
if codepoint <= 0x064A or codepoint >= 0x065E then
while true do
nextChar = iter2() or " "
local nextCodepoint = utf8.codepoint(nextChar)
if nextCodepoint <= 0x064A or nextCodepoint >= 0x065E then
break
end
end
end
local prevVars = connectableCharacters[prevChar] or {}
local curVars = connectableCharacters[char] or {}
local nextVars = connectableCharacters[nextChar] or {}
local backC = (#prevVars == 4)
local nextC = (#nextVars >= 2)
local prevCan = (#curVars == 4)
local result = char
if #curVars > 1 then
if backC and nextC and prevCan then
result = curVars[4]
elseif nextC and prevCan then
result = curVars[3]
elseif backC then
result = curVars[2]
else
result = curVars[1]
end
end
if codepoint <= 0x064A or codepoint >= 0x065E then
prevChar = char
end
proc1[#proc1 + 1] = result
end
text = table.concat(proc1)
local procrev = {}
local revpos = length
for char in string.gmatch(text,utf8.charpattern) do
procrev[revpos] = char
revpos = revpos-1
end
return table.concat(procrev)
end
function love.load()
love.window.setMode(800, 600, {resizable=true, vsync=false, minwidth=400, minheight=300})
love.graphics.setFont(font)
persianText = processPersian(persianText)
end
function love.draw()
love.graphics.setColor(1,1,1,1)
local gwidth = love.graphics.getWidth()
local gheight = love.graphics.getHeight()
local strwidth = font:getWidth(persianText)
love.graphics.print(persianText, gwidth/2, gheight/2, 0, 1, 1, math.floor(strwidth/2), math.floor(font:getHeight()/2))
end