add text to hpgl tab
This commit is contained in:
65
webui/app.py
65
webui/app.py
@@ -16,6 +16,7 @@ from Command import Command
|
||||
from Program import Program
|
||||
from Plotter import Plotter
|
||||
from hpgl_svg import program_to_svg
|
||||
from text_to_hpgl import text_to_hpgl
|
||||
|
||||
try:
|
||||
from config import webui_secret_key, webui_max_upload_mb
|
||||
@@ -28,6 +29,36 @@ app.secret_key = webui_secret_key()
|
||||
app.config['MAX_CONTENT_LENGTH'] = webui_max_upload_mb() * 1024 * 1024
|
||||
UPLOAD_DIR = os.path.join(ROOT, 'webui', 'uploads')
|
||||
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
||||
FONT_DIRS = [
|
||||
os.path.join(ROOT, 'font'),
|
||||
os.path.join(ROOT, '.git', 'font'),
|
||||
]
|
||||
|
||||
|
||||
def _resolve_font(filename):
|
||||
"""Return full path to font file if found in FONT_DIRS, else None."""
|
||||
for d in FONT_DIRS:
|
||||
if not os.path.isdir(d):
|
||||
continue
|
||||
path = os.path.join(d, filename)
|
||||
if os.path.isfile(path):
|
||||
return path
|
||||
return None
|
||||
|
||||
|
||||
def _list_fonts():
|
||||
"""Return list of font filenames (.otf, .ttf) from FONT_DIRS."""
|
||||
seen = set()
|
||||
out = []
|
||||
for d in FONT_DIRS:
|
||||
if not os.path.isdir(d):
|
||||
continue
|
||||
for name in sorted(os.listdir(d)):
|
||||
low = name.lower()
|
||||
if (low.endswith('.otf') or low.endswith('.ttf')) and name not in seen:
|
||||
seen.add(name)
|
||||
out.append(name)
|
||||
return out
|
||||
|
||||
|
||||
def _get_program():
|
||||
@@ -54,6 +85,40 @@ def index():
|
||||
return render_template('index.html')
|
||||
|
||||
|
||||
@app.route('/api/fonts')
|
||||
def list_fonts():
|
||||
"""List font filenames in font/ and .git/font/."""
|
||||
return jsonify({'fonts': _list_fonts()})
|
||||
|
||||
|
||||
@app.route('/api/text_to_hpgl', methods=['POST'])
|
||||
def api_text_to_hpgl():
|
||||
"""Generate HPGL from text and font; save as current program."""
|
||||
data = request.get_json() or {}
|
||||
text = (data.get('text') or '').strip()
|
||||
font_name = data.get('font') or ''
|
||||
size_pt = data.get('size_pt', 72)
|
||||
if not text:
|
||||
return jsonify({'error': 'Enter some text.'}), 400
|
||||
if not font_name:
|
||||
return jsonify({'error': 'Select a font.'}), 400
|
||||
font_path = _resolve_font(font_name)
|
||||
if not font_path:
|
||||
return jsonify({'error': 'Font not found: {}'.format(font_name)}), 400
|
||||
try:
|
||||
hpgl = text_to_hpgl(text, font_path, size_pt=size_pt)
|
||||
Program.parse(hpgl) # validate
|
||||
except Exception as e:
|
||||
return jsonify({'error': 'Text to HPGL failed: {}'.format(str(e))}), 400
|
||||
name = str(uuid.uuid4()) + '.hpgl'
|
||||
path = os.path.join(UPLOAD_DIR, name)
|
||||
with open(path, 'w') as out:
|
||||
out.write(hpgl)
|
||||
session['upload_path'] = path
|
||||
session['upload_filename'] = 'text_{}.hpgl'.format(font_name)
|
||||
return jsonify({'ok': True, 'filename': session['upload_filename'], 'preview_url': '/api/svg'})
|
||||
|
||||
|
||||
@app.route('/api/upload', methods=['POST'])
|
||||
def upload():
|
||||
if 'file' not in request.files:
|
||||
|
||||
Reference in New Issue
Block a user