mozman / ezdxf

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

Font rendering depends on DXF viewer/application #733

Open shine-flower opened 2 years ago

shine-flower commented 2 years ago

code:height is 2.7. How to make the height of hollow characters consistent with the height of ordinary characters?

doc = ezdxf.readfile('211.dxf')
doc.styles.new('SimHei', dxfattribs={'font': 'SimHei.ttf'})
msp = doc.modelspace()
height = 2.7
first_path = text2path.make_paths_from_str("厂牌AAA123", fonts.FontFace(family="SimHei"), height)
final_paths = path.transform_paths(first_path, Matrix44.translate(1.810846223812833, 1.482741896976211, 0))
path.render_splines_and_polylines(msp, final_paths, dxfattribs={"color": 7})
text2 = msp.add_text(
    "厂牌AAA123",
    dxfattribs={
        "style": "SimHei",
        "height": height
    },
).set_pos((1.810846223812833, 1.482741896976211))
zoom.extents(msp)
doc.saveas("text2path.dxf")

result:

image

mozman commented 2 years ago

All text rendering in ezdxf is done by the Matplotlib package which does not render fonts the same way as AutoCAD or BricsCAD. That's a fact you have to live with. (see update at the end)

In the example script below I have to use the SimSun.ttc font and I noticed that it's not easy to get the same font for the text2path add-on, which is specified by the font-family, and the DXF text-style, which requires the exact true-type filename. So check if you really use the same font in both use cases.

image

This is the text2path.dxf file rendered by different DXF viewers:

BricsCAD, it does not match:

image

The ezdxf view command which uses the Qt framework to render fonts, and it also does not match:

image

And the ezdxf draw command which uses the Matplotlib package to render fonts, and no surprise this does match:

image

... and Autodesk DWG TrueView can't render .ttc fonts at all:

image

Example script:

from pathlib import Path
import ezdxf
from ezdxf.math import Matrix44
from ezdxf import path, fonts, zoom
from ezdxf.addons import text2path

CWD = Path("~/Desktop/Outbox").expanduser()
if not CWD.exists():
    CWD = Path(".")

doc = ezdxf.new()
msp = doc.modelspace()
doc.styles.new('SimSun', dxfattribs={'font': 'simsun.ttc'})
height = 2.7
first_path = text2path.make_paths_from_str("厂牌AAA123", fonts.FontFace(family="SimSun"), height)
final_paths = path.transform_paths(first_path, Matrix44.translate(1.810846223812833, 1.482741896976211, 0))

text2 = msp.add_text(
    "厂牌AAA123",
    dxfattribs={
        "style": "SimSun",
        "height": height
    },
).set_placement((1.810846223812833, 1.482741896976211))
path.render_splines_and_polylines(msp, final_paths, dxfattribs={"color": 1})
zoom.extents(msp)
doc.saveas(CWD / "text2path.dxf")

UPDATE: Text is rendered by fontTools since version v1.1.0, the result is now closer to AutoCAD than rendered by Matplotlib but will never be perfect.

mozman commented 2 years ago

This issue stays open for easy finding.

shine-flower commented 2 years ago

after testing:The ratio of hollowed-out fonts to the original text is 1.45.(font: KaiTi.ttf、SimHei.ttf、FangSong.ttf) There is only one exception:Microsoft YaHei.ttf.It is 1:1. 1、I can find the font file by using Microsoft YaHei.ttf, but the actual font file in the system is mysh.ttc. I can't find the font file by using mysh.ttc in code, nor can I change it to Microsoft YaHei.ttc

doc.styles.new('SimSun', dxfattribs={'font': 'Microsoft YaHei.ttf'})

image 2、code:At this point, the error is acceptable to me image image

chibai commented 2 years ago

@shine-flower if you just want to render the CAD file. You can refer this https://github.com/mozman/ezdxf/discussions/724

mozman commented 5 months ago

Update: Text is rendered by fontTools since version v1.1.0, the result is now closer to AutoCAD than rendered by Matplotlib but will never be perfect.