0

(English is not my native languate)

I want to use ezdxf to convert 'DXF' file to picture.

as the document's example:


fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1])
ctx = RenderContext(dxf_doc)
out = MatplotlibBackend(ax)
frontend = Frontend(ctx, out)
frontend.ctx.set_current_layout(msp)
# frontend.draw_layout(msp, filter_func=lambda entity_f: entity_f in entity_use, finalize=True)
frontend.draw_entities(entity_use)
frontend.out.set_background(
frontend.ctx.current_layout_properties.background_color)
frontend.out.finalize()
fig.savefig(f'./datas/your_{i}.png', dpi=500)
plt.close(fig)

But,warning comes:


findfont: Font family ["Tssdeng"] not found,Falling back to DejaVu Sans.
findfont: Font family ["bzxw"] not found,Falling back to DejaVu Sans.
findfont: Font family ["simfang"] not found,Falling back to DejaVu Sans.
findfont: Font family [''] not found,Falling back to DejaVu Sans.
findfont: Font family ["tssdeng"] not found,Falling back to DejaVu Sans.

It makes: the picture created,will use rectangle to represent words(Chinese word,such as ,'工程',"图纸")

I guess,it may be any problem ,when the matplotlib load font.So,I tried the code below at the main.py 's head :


from matplotlib.font_manager import fontManager
fontManager.addfont(r"./mpl_ttf/TssdEng.ttf")
fontManager.addfont(r"./mpl_ttf/simfang.ttf")

But other warnings comes out:

Matplotlib RuntimeError: In affine_transform: Invalid vertices array.

So I just write a mini code,to show,how should use custom font:


import numpy as np

import matplotlib as mpl

from pathlib import Path
from matplotlib import font_manager
from matplotlib.font_manager import fontManager

import matplotlib.pyplot as plt


myfont = mpl.font_manager.FontProperties(fname=r"./mpl_ttf/TssdEng.ttf")
t = np.arange(-5*np.pi, 5*np.pi, 0.001)
y = np.sin(t)/t
my_post = plt.plot(t, y)
plt.title('matplotlib 中文显示测试——test chinese',fontproperties=myfont)
plt.xlabel('中文显示测试X',fontproperties=myfont)
plt.ylabel('中文显示测试Y',fontproperties=myfont)

plt.show()

warnings come:


RuntimeWarning: Glyph 101 missing from current font.

Also,I tried other codes(to find what char the font support):

from itertools import chain

from fontTools.ttLib import TTFont
from fontTools.unicode import Unicode

font_path = r"TssdEng.ttf"


def font_validation(input_string, font_path):
    font = TTFont(font_path)
    chars = chain.from_iterable([y + (Unicode[y[0]],) for y in x.cmap.items()] for x in font["cmap"].tables)
    for char in chain.from_iterable([y + (Unicode[y[0]],) for y in x.cmap.items()] for x in font["cmap"].tables):
        print(f" {char} ")
    for i in input_string:
        char = ord(i)
        print(i, char, hex(char), Unicode[char], char in (x[0] for x in chars))
    font.close()


font_validation("C", font_path)

It showed: the font not support all the char.

So, what could I do to convert a part of "DXF" file to picture by ezdxf with custom font?

lanhao945
  • 427
  • 5
  • 21
  • 1
    If the summary of your question is: How to render a glyph which is not included in a TTF file? I guess the answer is: Its not possible. If the replacement of the font in the DXF style table is a solution to you (like "TssdEng.ttf" -> "simsun.ttf"), so that at least as any text is rendered, this is possible and I can show you that. – mozman Dec 29 '21 at 09:35
  • @mozman Thanks very much. Finally, I get the message: the font,such as `Tssdeng.ttf`, is a custom font,that can noly be use by itself(a software like autodesk cad).So I finally tried the way that you saied. By setting `matplotlib`'s default font. – lanhao945 Dec 30 '21 at 00:40

0 Answers0