Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Font rendering depends on DXF viewer/application #733

Open
shine-flower opened this issue Aug 16, 2022 · 5 comments
Open

Font rendering depends on DXF viewer/application #733

shine-flower opened this issue Aug 16, 2022 · 5 comments

Comments

@shine-flower
Copy link

shine-flower commented Aug 16, 2022

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
Copy link
Owner

mozman commented Aug 16, 2022

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 mozman changed the title The same height setting, writing text and writing hollowed-out font finally reflected the inconsistency. Font rendering depends on DXF viewer/application Aug 16, 2022
@mozman
Copy link
Owner

mozman commented Aug 16, 2022

This issue stays open for easy finding.

@shine-flower
Copy link
Author

shine-flower commented Aug 17, 2022

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
Copy link

chibai commented Aug 23, 2022

@shine-flower if you just want to render the CAD file. You can refer this #724

@mozman
Copy link
Owner

mozman commented May 9, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants