Files
mimaki/text_to_hpgl.py
2026-02-05 22:50:36 +01:00

118 lines
4.2 KiB
Python

"""
Convert text to HPGL using a font file (.otf, .ttf).
Uses matplotlib TextPath; flattens curves to line segments.
"""
from __future__ import division
import os
# Path codes from matplotlib.path.Path
MOVETO = 1
LINETO = 2
CURVE3 = 3
CURVE4 = 4
CLOSEPOLY = 79
def _flatten_quadratic(p0, p1, p2, n=10):
"""Quadratic Bezier: B(t) = (1-t)^2 P0 + 2(1-t)t P1 + t^2 P2."""
for i in range(1, n + 1):
t = i / n
u = 1 - t
x = u * u * p0[0] + 2 * u * t * p1[0] + t * t * p2[0]
y = u * u * p0[1] + 2 * u * t * p1[1] + t * t * p2[1]
yield (x, y)
def _flatten_cubic(p0, p1, p2, p3, n=10):
"""Cubic Bezier: B(t) = (1-t)^3 P0 + 3(1-t)^2 t P1 + 3(1-t)t^2 P2 + t^3 P3."""
for i in range(1, n + 1):
t = i / n
u = 1 - t
x = u * u * u * p0[0] + 3 * u * u * t * p1[0] + 3 * u * t * t * p2[0] + t * t * t * p3[0]
y = u * u * u * p0[1] + 3 * u * u * t * p1[1] + 3 * u * t * t * p2[1] + t * t * t * p3[1]
yield (x, y)
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)
segments = []
i = 0
contour_start = None
while i < len(codes):
code = codes[i]
if code == MOVETO:
x, y = vertices[i]
segments.append(('PU', int(round(x * scale)), int(round(y * scale))))
contour_start = (int(round(x * scale)), int(round(y * scale)))
i += 1
elif code == LINETO:
x, y = vertices[i]
segments.append(('PD', int(round(x * scale)), int(round(y * scale))))
i += 1
elif code == CLOSEPOLY:
if contour_start:
segments.append(('PD', contour_start[0], contour_start[1]))
contour_start = None
i += 1
elif code == CURVE3 and i + 1 < len(vertices):
for (x, y) in _flatten_quadratic(
(vertices[i - 1][0], vertices[i - 1][1]),
(vertices[i][0], vertices[i][1]),
(vertices[i + 1][0], vertices[i + 1][1]),
):
segments.append(('PD', int(round(x * scale)), int(round(y * scale))))
i += 2
elif code == CURVE4 and i + 2 < len(vertices):
p0, p1, p2, p3 = vertices[i - 1], vertices[i], vertices[i + 1], vertices[i + 2]
for (x, y) in _flatten_cubic(p0, p1, p2, p3):
segments.append(('PD', int(round(x * scale)), int(round(y * scale))))
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 all_segments:
if seg[0] == 'PU':
out.append('PU{},{}'.format(seg[1], seg[2]))
else:
out.append('PD{},{}'.format(seg[1], seg[2]))
out.append('PU')
return ';'.join(out) + ';'