เพื่อให้ครบกระบวน ต่อด้วยโค๊ดย้อนกลับอีกที
ต่อจาก
python: เขียนโค๊ดคลี่แสต็กฟอนต์ ที่ผ่านมานะครับ
$ vi dfont.py
...
def stack_format(stacklist):
def flush(newlist, templist, ind, cur_ind):
if ind == 0:
newlist.append('NPUSHB')
else:
newlist.append('NPUSHW')
newlist.append(' %s' % len(templist))
newlist.extend(templist)
return newlist, [], cur_ind
if len(stacklist)==0: return []
n = 0
newlist = []
templist = []
if int(stacklist[0]) < 256:
ind = 0
else:
ind = 1
while n < len(stacklist):
if int(stacklist[n]) < 256:
cur_ind = 0
else:
cur_ind = 1
if ind == cur_ind:
templist.insert(0,' '+stacklist[n])
else:
newlist, templist, ind = flush(newlist, templist, ind, cur_ind) #FLUSH
n += 1
newlist, templist, ind = flush(newlist, templist, ind, cur_ind) #FLUSH
return newlist
def ins_encode(txt):
"""Encode simple code into TrueType Instruction code"""
comment_list = ['#',';'] #COMMENT CHARACTER
flush_list = ['SVTCA'] #FLUSH STACK
stacklist = []
newlist = []
templist = []
txtlist = txt.split('\n')
for line in txtlist:
line = line.replace("\t"," ")
if " " in line:
l = line.split(" ")
else:
l = [line]
l = [ j for j in l if j != "" ]
if l == []:
continue
if l[0][0] in comment_list: #BYPASS COMMENT
continue
for f in flush_list: #?FLUSH STACK
if f == l[0][:len(f)]:
newlist.extend(stack_format(stacklist))
stacklist = []
newlist.extend(templist)
templist = []
break
if '[' in l[0]: #TO UPPERCASE
ltemp = l[0].split('[',1)
temp = ltemp[0].upper()+'['+ltemp[1]
else:
temp = l[0].upper()
templist.append(temp) #KEEP COMMAND
l.pop(0) #PROCESS REST STACK NUM
for element in l:
if element[0] in comment_list:
break
else:
stacklist.append(element)
if stacklist != []:
newlist.extend(stack_format(stacklist))
newlist.extend(templist)
print '===Encode==============================='
print '\n'.join(newlist)
if __name__ == "__main__":
txt = sys.argv[1]
#ins_decode(txt)
ins_encode(txt)
ทดสอบด้วยตัวอย่าง n เมื่อกี้นี้
$ dfont.py "
แปะด้วยโค๊ดเทียมที่ถูกคลี่แล้ว จากตัวอย่าง n
"
===Encode===============================
NPUSHB
10
2
8
0
78
13
9
8
11
70
20
SRP0
MIRP[rp0,min,rnd,grey]
MIRP[min,rnd,grey]
SHP[rp2]
MIRP[rp0,rnd,grey]
MIRP[min,rnd,grey]
IUP[x]
NPUSHB
15
3
9
0
3
14
1
6
135
14
17
184
12
188
10
1
SVTCA[y-axis]
MDAP[rnd]
ALIGNRP
MIRP[rnd,grey]
MIRP[rp0,rnd,grey]
MDRP[rnd,grey]
MIRP[min,rnd,grey]
SRP1
SRP2
SLOOP
IP
IUP[y]
NPUSHB
5
96
21
207
21
2
SVTCA[x-axis]
DELTAP1
ไม่เหมือนเป๊ะ แต่น่าจะทำงานได้เหมือนกัน