export hpgl load from folder
This commit is contained in:
@@ -33,36 +33,13 @@ def _flatten_cubic(p0, p1, p2, p3, n=10):
|
||||
yield (x, y)
|
||||
|
||||
|
||||
def text_to_hpgl(text, font_path, size_pt=72, units_per_pt=14):
|
||||
"""
|
||||
Convert text to HPGL string using the given font file.
|
||||
size_pt: font size in points.
|
||||
units_per_pt: HPGL units per point (default ~0.35 mm per pt).
|
||||
Returns HPGL string (IN;SP1;PU x,y;PD;PA x,y;...).
|
||||
"""
|
||||
try:
|
||||
from matplotlib.textpath import TextPath
|
||||
from matplotlib.font_manager import FontProperties
|
||||
except ImportError:
|
||||
raise RuntimeError('matplotlib is required for text-to-HPGL. Install with: pip install matplotlib')
|
||||
|
||||
if not text or not font_path or not os.path.isfile(font_path):
|
||||
raise ValueError('Need non-empty text and a valid font file path.')
|
||||
|
||||
prop = FontProperties(fname=font_path)
|
||||
path = TextPath((0, 0), text, size=size_pt, prop=prop)
|
||||
vertices = path.vertices
|
||||
codes = path.codes
|
||||
|
||||
def _path_to_segments(vertices, codes, scale):
|
||||
"""Convert a single path (vertices, codes) to list of (PU/PD, x, y) in HPGL units."""
|
||||
if codes is None:
|
||||
codes = [MOVETO] + [LINETO] * (len(vertices) - 1)
|
||||
|
||||
# Scale to HPGL units (y flip: matplotlib y up, HPGL typically y up too; we keep same)
|
||||
scale = units_per_pt
|
||||
segments = []
|
||||
i = 0
|
||||
contour_start = None
|
||||
|
||||
while i < len(codes):
|
||||
code = codes[i]
|
||||
if code == MOVETO:
|
||||
@@ -94,9 +71,44 @@ def text_to_hpgl(text, font_path, size_pt=72, units_per_pt=14):
|
||||
i += 3
|
||||
else:
|
||||
i += 1
|
||||
return segments
|
||||
|
||||
|
||||
def text_to_hpgl(text, font_path, size_pt=72, units_per_pt=14):
|
||||
"""
|
||||
Convert text to HPGL string using the given font file.
|
||||
Newlines in text start a new line (stacked vertically).
|
||||
size_pt: font size in points.
|
||||
units_per_pt: HPGL units per point (default ~0.35 mm per pt).
|
||||
Returns HPGL string (IN;SP1;PU x,y;PD;PA x,y;...).
|
||||
"""
|
||||
try:
|
||||
from matplotlib.textpath import TextPath
|
||||
from matplotlib.font_manager import FontProperties
|
||||
except ImportError:
|
||||
raise RuntimeError('matplotlib is required for text-to-HPGL. Install with: pip install matplotlib')
|
||||
|
||||
if not text or not font_path or not os.path.isfile(font_path):
|
||||
raise ValueError('Need non-empty text and a valid font file path.')
|
||||
|
||||
# Normalize line endings and split into lines
|
||||
lines = text.replace('\r\n', '\n').replace('\r', '\n').split('\n')
|
||||
scale = units_per_pt
|
||||
line_height_pt = size_pt * 1.2 # line spacing
|
||||
prop = FontProperties(fname=font_path)
|
||||
all_segments = []
|
||||
|
||||
for line_index, line in enumerate(lines):
|
||||
if not line.strip():
|
||||
continue
|
||||
# Each line at its own Y: first line at 0, next at -line_height_pt, etc. (y down)
|
||||
path = TextPath((0, -line_index * line_height_pt), line, size=size_pt, prop=prop)
|
||||
vertices = path.vertices
|
||||
codes = path.codes
|
||||
all_segments.extend(_path_to_segments(vertices, codes, scale))
|
||||
|
||||
out = ['IN', 'SP1']
|
||||
for seg in segments:
|
||||
for seg in all_segments:
|
||||
if seg[0] == 'PU':
|
||||
out.append('PU{},{}'.format(seg[1], seg[2]))
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user