mozman / ezdxf

Python interface to DXF
https://ezdxf.mozman.at
MIT License
906 stars 189 forks source link

Text2path is the same height, some fonts are actually bigger. #705

Closed shine-flower closed 2 years ago

shine-flower commented 2 years ago

Question 1

Text2path is the same height, some fonts are actually bigger. such as: Microsoft YaHei (I'm using height 1).

image

Question 2

The same code generated out of DXF files, different software to open, the smoothness of the line is very different, and autocad performance is not too friendly.

image

import ezdxf
from ezdxf.math import Matrix44
from ezdxf import path
from ezdxf.tools import fonts
from ezdxf.addons import text2path
fonts.load()
doc = ezdxf.new()
msp = doc.modelspace()
height = 1
# KaiTi
first_path = text2path.make_paths_from_str("Arial Narrow 产品编号1", fonts.FontFace(family="KaiTi"), height)
final_paths = path.transform_paths(first_path, Matrix44.translate(8, 6, 0))
path.render_splines_and_polylines(msp, final_paths, dxfattribs={"color": 7})
# FangSong
first_path = text2path.make_paths_from_str("Arial Narrow 产品编号2", fonts.FontFace(family="FangSong"), height)
final_paths = path.transform_paths(first_path, Matrix44.translate(8, 8, 0))
path.render_splines_and_polylines(msp, final_paths, dxfattribs={"color": 7})
# Microsoft YaHei
first_path = text2path.make_paths_from_str("Arial Narrow 产品编号3", fonts.FontFace(family="Microsoft YaHei"), height)
final_paths = path.transform_paths(first_path, Matrix44.translate(8, 10, 0))
path.render_splines_and_polylines(msp, final_paths, dxfattribs={"color": 7})
# SimHei
first_path = text2path.make_paths_from_str("Arial Narrow 产品编号4", fonts.FontFace(family="SimHei"), height)
final_paths = path.transform_paths(first_path, Matrix44.translate(8, 12, 0))
path.render_splines_and_polylines(msp, final_paths, dxfattribs={"color": 7})
doc.saveas("text2path.dxf")
mozman commented 2 years ago

1st Q: this depends on the font design and on the rendering by the Matplotlib package, but you can scale the paths to a uniform height. Detect the bounding box of the paths by the function bbox = ezdxf.path.bbox() and add a Matrix44.scale(heigth/bbox.size.y) operation to the transform_paths call, this scales all paths to the same final absolute height, but this is only useful for the same text.

2nd Q: this depends on the CAD application/DXF viewer - in AutoCAD/BricsCAD enter the command REGEN to get smoother curves

shine-flower commented 2 years ago

第一个问题:这取决于字体设计和 Matplotlib包的渲染,但您可以将路径缩放到统一的高度。通过函数检测路径的边界框bbox = ezdxf.path.bbox()并在调用中添加一个Matrix44.scale(heigth/bbox.size.y)操作transform_paths,这会将所有路径缩放到相同的最终绝对高度,但这仅对相同的文本有用。

第二个问题:这取决于 CAD 应用程序/DXF 查看器 - 在 AutoCAD/BricsCAD 中输入命令REGEN以获得更平滑的曲线

Q2: I added the following code. It is OK!

zoom.extents(msp)

image