-
Notifications
You must be signed in to change notification settings - Fork 2
/
zhfonts.py
174 lines (154 loc) · 5.73 KB
/
zhfonts.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This script provide you with the Chinese fonts found by fc-list,
# let you choose which to use with thuthesis.
# The script prefers the font name in ASCII to the one containing Unicode
# character, thus trying to avoid the problem with 'Adobe 宋体 Std L'.
# The script should work with Python 2.6, 2.7 and Python 3.2, providing
# that your locale is utf-8. Please report issues if you find.
from __future__ import print_function
from __future__ import unicode_literals
try:
from subprocess import check_output
except ImportError: # python 2.6 has no check_output
from subprocess import Popen, PIPE
def check_output(cmdline):
return Popen(cmdline, stdout=PIPE).communicate()[0]
# Py3K renames raw_input to input.
try:
input = raw_input
except:
pass
import re
# The return value is of type byte string (in py3k).
fontliststr = check_output(["fc-list", "-f", "%{family}\n", ":lang=zh"])
allfontliststr = check_output(["fc-list", "-f", "%{family}\n"])
if not fontliststr:
print("No Chinese font exists! Leaving...")
exit(1)
# strip out ':style=BLABLA' stuff
fontnamelist = sorted([x.split(b":")[0] for x in fontliststr.splitlines()])
allfontnamelist = sorted([x.split(b":")[0] for x in allfontliststr.splitlines()])
# Convert to unicode string, assuming utf-8 encoding of byte string.
# Thus following variables are all unicode string.
fontnamelist = [x.decode("utf-8") for x in fontnamelist]
allfontnamelist = [x.decode("utf-8") for x in allfontnamelist]
songtilist = []
kaitilist = []
heitilist = []
fangsonglist = []
lishulist = []
youyuanlist = []
relist = ["仿宋|Fang", "宋|Ming", "黑|Hei|Sans|Gothic",
"楷|Kai", "隶|Li", "圆|Yuan"]
familylist = [fangsonglist, songtilist, heitilist,
kaitilist, lishulist, youyuanlist]
tuplelist = zip(relist, familylist)
for x in fontnamelist:
for t in tuplelist:
if re.search(t[0], x, re.IGNORECASE):
t[1].append(x)
break
def selectfont(fontlist):
if not fontlist:
if not fontnamelist:
if not allfontnamelist:
return ''
else:
return selectfont(allfontnamelist)
else:
return selectfont(fontnamelist)
for i, v in enumerate(fontlist):
print('{0:d} {1}'.format(i, v))
while True:
# Note input/raw_input in Python 2.x do not accept unicode string.
print("选择一个:(输入数字[0-{0:d}],默认0。按z在所有中文字体中选择,按a在所有字体中选择)".format(len(fontlist)-1), end='')
n_str = input()
if not n_str:
n = 0
else:
if n_str == "z" or n_str =="Z":
return selectfont(fontnamelist)
elif n_str == "a" or n_str == "A":
return selectfont(allfontnamelist)
else:
try:
n = int(n_str)
except ValueError:
continue
if 0 <= n < len(fontlist):
break
asciifontname = ''
for x in fontlist[n].split(","):
try:
x.encode('ascii')
except UnicodeEncodeError:
pass
else:
asciifontname = x
break
if asciifontname:
return asciifontname
else:
print("ASCII font name not found!")
print("You might encounter error using this font with XeLaTeX...")
return fontlist[n].split(",")[-1]
print("宋体:")
songti = selectfont(songtilist)
print("黑体:")
heiti = selectfont(heitilist)
print("楷体:")
kaiti = selectfont(kaitilist)
print("仿宋:")
fangsong = selectfont(fangsonglist)
print("隶书:")
lishu = selectfont(lishulist)
print("幼圆:")
youyuan = selectfont(youyuanlist)
if not songti or not heiti or not kaiti:
print("错误:缺少宋体、黑体或楷体字体")
exit(2)
print("生成字体文件fontname.def")
with open('fontname.def', 'w') as f:
f.write("% vim: set ft=tex:\n")
f.write("% This file is auto-generated by zhfonts.py script\n\n")
f.write("\\ProvidesFile{fontname.def}\n")
f.write("\\setCJKmainfont[BoldFont={" + heiti + "},ItalicFont={" + kaiti + "}]{" + songti + "}\n")
f.write("\\setCJKsansfont{" + heiti + "}\n")
if fangsong:
f.write("\\setCJKmonofont{" + fangsong + "}\n")
else:
print("缺少仿宋,宋体代替")
f.write("\\setCJKmonofont{" + songti + "}\n")
f.write("\\setCJKfamilyfont{zhsong}{" + songti + "}\n")
f.write("\\setCJKfamilyfont{zhhei}{" + heiti + "}\n")
f.write("\\setCJKfamilyfont{zhkai}{" + kaiti + "}\n")
if fangsong:
f.write("\\setCJKfamilyfont{zhfs}{" + fangsong + "}\n")
else:
f.write("\\setCJKfamilyfont{zhfs}{" + songti + "}\n")
if lishu:
f.write("\\setCJKfamilyfont{zhli}{" + lishu + "}\n")
else:
print("缺少隶书,宋体代替")
f.write("\\setCJKfamilyfont{zhli}{" + songti + "}\n")
if youyuan:
f.write("\\setCJKfamilyfont{zhyou}{" + youyuan + "}\n")
else:
print("缺少幼圆,宋体代替")
f.write("\\setCJKfamilyfont{zhyou}{" + songti + "}\n")
f.write('''
\\newcommand*{\\songti}{\\CJKfamily{zhsong}}
\\newcommand*{\\heiti}{\\CJKfamily{zhhei}}
\\newcommand*{\\kaishu}{\\CJKfamily{zhkai}}
\\newcommand*{\\fangsong}{\\CJKfamily{zhfs}}
\\newcommand*{\\lishu}{\\CJKfamily{zhli}}
\\newcommand*{\\youyuan}{\\CJKfamily{zhyou}}
''')
print("替换shuji.tex中的仿宋字体")
import fileinput
for line in fileinput.input("shuji.tex", inplace=True):
line = line.decode('utf-8')
if line.startswith(" \setCJKfamilyfont{zhfs}[RawFeature={vertical:}]"):
line = " \setCJKfamilyfont{zhfs}[RawFeature={vertical:}]{" + fangsong + "}\n"
print(line.encode('utf-8'), end = '')