You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法安装ttfquery,求TTF文件贝塞尔曲线信息提取替代方案

解决TTF字体贝塞尔曲线提取及日文汉字笔顺获取问题

问题背景

参考Stack Overflow帖子《Retrieving bounding box and bezier curve data for all glyphs in a .ttf font file in python》,想要从TTF文件提取贝塞尔曲线信息用于Matplotlib字符绘制动画,但Python 3.9无法安装老旧的TTFQuery包,安装时报错:

C:\Users\Chris>pip install TTFQuery
Collecting TTFQuery
  Downloading TTFQuery-1.0.5.tar.gz (17 kB)
  Preparing metadata (setup.py) ... error
  error: subprocess-exited-with-error

  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> [7 lines of output]
      Traceback (most recent call last):
        File "<string>", line 2, in <module>
        File "<pip-setuptools-caller>", line 34, in <module>
        File "C:\Users\Chris\AppData\Local\Temp\pip-install-djyaq0d_\ttfquery_678fb4fdd4004a81b1bc72c29a99b35f\setup.py", line 11
          except ImportError, err:
                            ^
      SyntaxError: invalid syntax
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.

现寻求:

  1. 无需降级Python的TTF贝塞尔曲线提取方法
  2. 免费日文汉字笔顺获取途径/工具/数据,用于制作类似Jisho的汉字绘制动画

一、替代TTFQuery提取TTF贝塞尔曲线的方法

无需降级Python,推荐两个维护活跃的开源库:

1. fontTools

fontTools是Python处理字体文件的核心工具,支持TTF等多种格式,可直接解析字形的贝塞尔曲线控制点数据。

安装命令:

pip install fonttools

示例代码(提取指定字符的贝塞尔曲线):

from fontTools.ttLib import TTFont
from fontTools.pens.recordingPen import RecordingPen

font_path = "your_font.ttf"
target_char = "漢"  # 目标日文汉字

with TTFont(font_path) as font:
    # 获取字符对应的字形名称
    glyph_name = font.getGlyphName(ord(target_char))
    glyph_set = font.getGlyphSet()
    glyph = glyph_set[glyph_name]
    
    # 用RecordingPen记录所有路径指令
    pen = RecordingPen()
    glyph.draw(pen)
    
    # 解析路径数据,区分不同类型的曲线
    for cmd in pen.value:
        if cmd[0] == 'qCurveTo':
            print(f"二次贝塞尔曲线控制点: {cmd[1:]}")
        elif cmd[0] == 'curveTo':
            print(f"三次贝塞尔曲线控制点: {cmd[1:]}")
        # 同时支持moveTo、lineTo等基础绘图指令

2. freetype-py

基于FreeType库的Python绑定,可直接读取TTF字形的矢量路径信息,适合底层解析需求。

安装命令:

pip install freetype-py

示例代码:

import freetype

font_path = "your_font.ttf"
target_char = "漢"

face = freetype.Face(font_path)
face.set_char_size(48 * 64)  # 设置字符大小(单位:1/64像素)

# 加载字符字形(不生成位图)
face.load_char(target_char, freetype.FT_LOAD_NO_BITMAP)
glyph = face.glyph

if glyph.format == freetype.FT_GLYPH_FORMAT_OUTLINE:
    outline = glyph.outline
    # 遍历每个轮廓的路径点
    for contour_idx in range(outline.n_contours):
        start = outline.contours[contour_idx]
        end = outline.contours[contour_idx+1] if contour_idx+1 < outline.n_contours else len(outline.points)
        points = outline.points[start:end]
        tags = outline.tags[start:end]
        
        current_point = None
        for idx, (point, tag) in enumerate(zip(points, tags)):
            if tag & freetype.FT_CURVE_TAG_ON:
                if current_point is None:
                    current_point = point
                else:
                    prev_tag = tags[idx-1]
                    if prev_tag & freetype.FT_CURVE_TAG_CONIC:
                        print(f"二次贝塞尔: {current_point} -> {points[idx-1]} -> {point}")
                    elif prev_tag & freetype.FT_CURVE_TAG_CUBIC:
                        print(f"三次贝塞尔: {current_point} -> {points[idx-2]} -> {points[idx-1]} -> {point}")
                    else:
                        print(f"直线: {current_point} -> {point}")
                    current_point = point

二、免费日文汉字笔顺获取途径

1. KanjiVG

开源SVG格式汉字数据集,包含每个汉字的笔顺路径数据,支持日文常用汉字。数据以XML/SVG存储,可直接解析路径指令用于动画绘制,无需额外处理字形数据。

2. 日本国語研究所「漢字筆順データ」

提供标准日文汉字的笔顺序列数据,包含每个汉字的笔画顺序、笔画数及对应编码,可搭配上述TTF字形数据,按笔顺顺序绘制贝塞尔曲线实现动画。

3. 开源工具:kanji-animator

部分开源项目基于KanjiVG等数据集实现了汉字笔顺动画逻辑,可直接参考其代码,或使用现有模块快速生成Matplotlib动画。


总结

无需降级Python,使用fontTools或freetype-py即可替代TTFQuery完成TTF贝塞尔曲线提取;获取日文汉字笔顺可利用KanjiVG等开源数据集,完全满足制作汉字绘制动画的需求。

内容的提问来源于stack exchange,提问作者J.Doe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 07:55:39