加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ttf字体生成.py 1.28 KB
一键复制 编辑 原始数据 按行查看 历史
qfcy_ 提交于 2024-11-08 10:15 . Initial commit
# 需要用fontforge安装目录的bin\ffpython.exe运行
import fontforge,json
def contour_to_glyph(glyph, contour):
pen = glyph.glyphPen()
for points in contour:
#points=points[::4] # 缩小字体大小
size=700
points=[(x,-y+size) for x,y in points]
# 将找到的轮廓点转化为字形轮廓
pen.moveTo(*points[0])
for point in points[1:]:
pen.lineTo(*point)
pen.closePath()
def set_fontinfo(font):
# 设置字体信息
font.fontname = "ShuFaTi"
font.fullname = "ShuFaTi Regular"
font.familyname = "书法体"
font.weight = "Regular"
font.version = "1.0"
def create_font(chars, contours, output_path):
font = fontforge.font()
font.encoding = "UnicodeFull"
for char, contour in zip(chars, contours):
print("Processing:",char)
glyph = font.createChar(ord(char))
contour_to_glyph(glyph, contour)
set_fontinfo(font)
# 保存字体
font.generate(output_path)
def main():
with open("glyph_data.json",encoding="utf-8") as f:
chars, contours=json.load(f)
output_path = "书法.ttf" # 替换为输出 TTF 文件路径
create_font(chars, contours, output_path)
print("TTF font file created successfully.")
if __name__=="__main__":main()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化