#  Copyright (c) 2021, Manfred Moitzi
#  License: MIT License

from pathlib import Path
import ezdxf
from ezdxf import path, zoom
from ezdxf.math import Matrix44
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="Arial")
sx, sy = 4, 2
# create target box
msp.add_lwpolyline([(0, 0), (sx, 0), (sx, sy), (0, sy)], close=True)
text_as_paths = text2path.make_paths_from_str("Squeeze Me", ff)
final_paths = path.fit_paths_into_box(text_as_paths, size=(sx, sy, 0), uniform=False)
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))

path.render_lwpolylines(msp, final_paths, distance=0.01, dxfattribs=attr)

zoom.extents(msp)
doc.saveas(DIR / 'text2path.dxf')

@@ -385,28 +385,8 @@
  = %7B
-'layer': 'OUTLINE', 
 'col
@@ -390,17 +390,17 @@
 color': 
-1
+2
 %7D%0Aff = f
@@ -443,16 +443,17 @@
  = 4, 2%0A
+%0A
 # create
@@ -453,16 +453,20 @@
  create 
+the 
 target b
@@ -467,16 +467,17 @@
 rget box
+:
 %0Amsp.add
@@ -540,159 +540,504 @@
 True
-)%0Atext_as_paths = text2path.make_paths_from_str(%22Squeeze Me%22, ff)%0Afinal_paths = path.fit_paths_into_box(text_as_paths, size=(sx, sy, 0), uniform=False)
+,%0A                   dxfattribs=%7B'color': 1%7D)%0A%0A# convert text string into path objects:%0Atext_as_paths = text2path.make_paths_from_str(%22Squeeze Me%22, ff)%0A%0A# fit text paths into a given box size by scaling, does not move the path objects:%0A# uniform=True, keeps the text aspect ratio%0A# uniform=False, scales the text to touch all 4 sides of the box%0Afinal_paths = path.fit_paths_into_box(text_as_paths, size=(sx, sy, 0),%0A                                      uniform=False)%0A%0A# mirror text along x-axis
 %0Afin
@@ -1283,16 +1283,17 @@
 late(dx,
+ 
 dy, dz))
@@ -1354,24 +1354,56 @@
 .01,
- dxfattribs=attr
+%0A                        dxfattribs=%7B'color': 2%7D
 )%0A%0Az
@@ -1437,24 +1437,23 @@
 (DIR / '
-text2path
+SqeezeMe
 .dxf')%0A

document example