-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcopyReferenceAtoB.py
142 lines (112 loc) · 3.71 KB
/
copyReferenceAtoB.py
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
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from io import open
import sys
import os
import collections
import fontforge
# ignore warning
# import warnings
# warnings.filterwarnings("ignore")
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QFileDialog, QDialog, QPushButton,
QLineEdit, QLabel,
QApplication, QVBoxLayout)
class askSetting(QDialog):
def __init__(self,
app=None,
parent=None,
items=None):
super(askSetting, self).__init__(parent)
self.app = app
self.items = items
layout = QVBoxLayout()
self.lineedits = {}
for key in items.keys():
layout.addWidget(QLabel(key))
self.lineedits[key] = QLineEdit()
self.lineedits[key].setText(items[key])
# enable ime input
self.lineedits[key].inputMethodQuery(Qt.ImEnabled)
layout.addWidget(self.lineedits[key])
self.btn = QPushButton('TTF File to Read', self)
self.btn.clicked.connect(lambda: (self.bye(items)))
self.btn.setFocusPolicy(Qt.StrongFocus)
layout.addWidget(self.btn)
self.setLayout(layout)
self.setWindowTitle(' Setting ')
def bye(self, items):
fileName = QFileDialog.getOpenFileName(
self, 'Dialog Title', '~/', initialFilter='*.ttf')
if fileName == (u'', u'*.ttf'):
print("Must Provide an input TTF file.")
sys.exit()
for key in self.lineedits.keys():
self.items[key] = self.lineedits[key].text()
self.items['getOpenFileName'] = fileName[0]
self.close()
self.app.exit(1)
inFilePrompt = "File to read"
defaultInFile = "copyReferenceAtoB"
# outFilePrompt = "TTF File to write"
# defaultOutFile = "out.ttf"
items = collections.OrderedDict()
items[inFilePrompt] = defaultInFile
# items[outFilePrompt] = defaultOutFile
app = QApplication(sys.argv)
ask = askSetting(app=app, items=items)
ask.show()
rtnCode = app.exec_()
# If press OK button rtnCode should be 1
if rtnCode != 1:
print('User abort by closing Setting dialog')
sys.exit
# print(items)
ttfFile = fontforge.open(items['getOpenFileName'])
f = open(items[inFilePrompt], 'r', encoding="utf-8")
ttfFile.selection.none()
###
# file contents
# ## start with "##" line will be ignore to read
# 問
# 问
# ie. \w
# ie. word
###
count = 0
for line in f:
if line.startswith("##"):
continue
words = line.encode("raw_unicode_escape").split()
# words = line.split()
# print(len(words))
if len(words) == 2:
sys.stdout.write(words[0].decode('unicode_escape'))
count += 1
if count % 25 == 0:
sys.stdout.write("\n")
sys.stdout.flush()
if words[0] == words[1]:
pass
elif words[0].startswith(b'\u') and words[1].startswith(b'\u'):
ttfFile.selection.select(words[0][1:])
ttfFile.copyReference()
if words[1].startswith(b'\u'):
ttfFile.selection.select(words[1][1:])
else:
ttfFile.selection.select(words[1])
ttfFile.paste()
else:
ttfFile.selection.select(words[0])
ttfFile.copyReference()
if words[1].startswith(b'\u'):
ttfFile.selection.select(words[1][1:])
else:
ttfFile.selection.select(words[1])
ttfFile.paste()
ttfFile.fontname = "Z-"+ttfFile.fontname
ttfFile.familyname = "Z-"+ttfFile.familyname
if not os.path.exists("out"):
os.makedirs("out")
ttfFile.generate("out/"+ttfFile.fontname+".ttf")
print(u'\nGenerated '+ttfFile.fontname+u" as out/"+ttfFile.fontname+u"\n")