def test_scaled_height_and_length_for_aligned_text(self, size, ff): length = 3 paths = text2path.make_paths_from_str("XXX", font=ff, size=size, align="LEFT") default = path.bbox(paths) paths = text2path.make_paths_from_str( "XXX", font=ff, size=size, align="ALIGNED", length=length) bbox = path.bbox(paths) scale = bbox.size.x / default.size.x assert bbox.size.x == pytest.approx(length), "expect exact length" assert bbox.size.y == pytest.approx(size * scale), \ "text height should be scaled"
def test_length_for_fit_alignment(self, size, ff): length = 3 paths = text2path.make_paths_from_str( "XXX", font=ff, size=size, align="FIT", length=length) bbox = path.bbox(paths) assert bbox.size.x == pytest.approx(length), "expect exact length" assert bbox.size.y == pytest.approx(size), \ "text height should be unscaled"
doc = ezdxf.new() doc.layers.new('OUTLINE') doc.layers.new('FILLING') msp = doc.modelspace() attr = {'color': 2} ff = fonts.FontFace(family="Arial") sx, sy = 4, 2 # create the target box: msp.add_lwpolyline([(0, 0), (sx, 0), (sx, sy), (0, sy)], close=True, dxfattribs={'color': 1}) # convert text string into path objects: text_as_paths = text2path.make_paths_from_str("Squeeze Me", ff) # fit text paths into a given box size by scaling, does not move the path objects: # uniform=True, keeps the text aspect ratio # uniform=False, scales the text to touch all 4 sides of the box final_paths = path.fit_paths_into_box(text_as_paths, size=(sx, sy, 0), uniform=False) # mirror text along x-axis final_paths = path.transform_paths(final_paths, Matrix44.scale(-1, 1, 1)) # move bottom/left corner to (0, 0) if required: bbox = path.bbox(final_paths) dx, dy, dz = -bbox.extmin final_paths = path.transform_paths(final_paths, Matrix44.translate(dx, dy, dz))
def _to_paths(s, f='Arial'): return text2path.make_paths_from_str(s, font=FontFace(family=f))
def test_paths_from_empty_string(self, ff): paths = text2path.make_paths_from_str("", font=ff) assert len(paths) == 0
def test_path_coordinates_for_negative_size(self, size, ff): # Negative text height mirrors text about the x-axis! paths = text2path.make_paths_from_str("X", font=ff, size=size) bbox = path.bbox(paths) assert bbox.extmax.y == pytest.approx(0) assert bbox.extmin.y == pytest.approx(size)
def test_path_coordinates_for_positive_size(self, size, ff): paths = text2path.make_paths_from_str("X", font=ff, size=size) bbox = path.bbox(paths) assert bbox.extmax.y == pytest.approx(size) assert bbox.extmin.y == pytest.approx(0)
def test_text_path_height_for_exact_drawing_units(self, size, ff): paths = text2path.make_paths_from_str("X", font=ff, size=size) bbox = path.bbox(paths) assert bbox.size.y == pytest.approx(abs(size))
# Requires: matplotlib from pathlib import Path import ezdxf from ezdxf import path, zoom from ezdxf.tools import fonts from ezdxf.addons import text2path DIR = Path('~/Desktop/Outbox').expanduser() fonts.load() doc = ezdxf.new() doc.layers.new('OUTLINE') doc.layers.new('FILLING') msp = doc.modelspace() attr = {'layer': 'OUTLINE', 'color': 1} ff = fonts.FontFace(family="Noto Sans SC") s = "Noto Sans SC 0123456789 %@ 中国文字" align = "LEFT" path.render_splines_and_polylines( msp, text2path.make_paths_from_str(s, ff, align=align), dxfattribs=attr) attr['layer'] = 'FILLING' attr['color'] = 2 for hatch in text2path.make_hatches_from_str(s, ff, align=align, dxfattribs=attr): msp.add_entity(hatch) zoom.extents(msp) doc.saveas(DIR / 'text2path.dxf')
from ezdxf.enums import TextEntityAlignment DIR = Path("~/Desktop/Outbox").expanduser() fonts.load() doc = ezdxf.new() doc.layers.new("OUTLINE") doc.layers.new("FILLING") msp = doc.modelspace() attr = {"layer": "OUTLINE", "color": 1} ff = fonts.FontFace(family="Noto Sans SC") s = "Noto Sans SC 0123456789 %@ 中国文字" align = TextEntityAlignment.LEFT path.render_splines_and_polylines(msp, text2path.make_paths_from_str(s, ff, align=align), dxfattribs=attr) attr["layer"] = "FILLING" attr["color"] = 2 for hatch in text2path.make_hatches_from_str(s, ff, align=align, dxfattribs=attr): msp.add_entity(hatch) zoom.extents(msp) doc.saveas(DIR / "text2path.dxf")