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

Ubuntu系统中ezdxf无法加载自定义字体的问题求助

问题排查与解决方案

1. 修正DXF文件写入方式

你手动以文本模式打开DXF文件写入,可能导致文件格式异常,建议改用ezdxf内置的saveas()方法,它会自动处理正确的文件模式:

import ezdxf

doc = ezdxf.new("AC1032")
doc.styles.add("IAmOnlineWithU", font="I am online with u")
msp = doc.modelspace()
t = msp.add_text("TEST TEXT", height=1, dxfattribs={"style": "IAmOnlineWithU"})
t.set_placement((0,0))
doc.saveas("out.dxf")

2. 正确指定字体名称

从fc-list输出和REPL结果来看,字体的家族名是"I am online with u",而非文件名。在doc.styles.add()中,font参数应使用字体家族名(若有特定样式可附带):

# 若字体有指定样式(如这里的"line"),可这样指定
doc.styles.add("IAmOnlineWithU", font="I am online with u,line")

3. 确保LibreCAD匹配字体

DXF文件不嵌入字体,LibreCAD需识别样式中指定的字体名称。确认LibreCAD的字体搜索目录包含你的字体文件,且名称与ezdxf中使用的完全一致(注意空格、大小写)。

4. 本地字体无需安装到系统的方法

无需将字体安装到系统,可通过ezdxf添加自定义字体搜索路径:

import ezdxf
from ezdxf.fonts import fonts

# 添加本地字体目录路径
fonts.add_search_path("/path/to/your/local/font/directory")
# 重建字体缓存
fonts.build_system_font_cache()

doc = ezdxf.new("AC1032")
# 可直接使用字体文件名或家族名
doc.styles.add("IAmOnlineWithU", font="IAmOnlineWithU-o96q.ttf")
msp = doc.modelspace()
t = msp.add_text("TEST TEXT", height=1, dxfattribs={"style": "IAmOnlineWithU"})
t.set_placement((0,0))
doc.saveas("out.dxf")

额外验证步骤

可通过以下代码确认字体是否被正确识别:

from ezdxf.fonts.fonts import find_font_face
f = find_font_face(family="I am online with u", style="line")
print(f.path)  # 输出字体文件路径,确认找到正确文件

内容的提问来源于stack exchange,提问作者mmachenry

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 23:22:47