Add web SVG generator, container, and Kubernetes manifests

- Flask app (web/) generates M1730 scale SVGs from a form; embeds
  GOST typ A font as base64 so output is self-contained
- Dockerfile builds the app into git.baumann.gr/adebaumann/m1730-generator
- k8s/ deploys as a ClusterIP service for NPM to proxy at
  works.adebaumann.com/M1730-generator/

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-07-12 00:29:55 +02:00
parent 10d25462be
commit 9e7223a1f5
13 changed files with 1193 additions and 0 deletions
+117
View File
@@ -0,0 +1,117 @@
import base64
import pathlib
from flask import Flask, render_template, request, Response
from html import escape
app = Flask(__name__)
_FONT_PATH = pathlib.Path('/usr/local/share/fonts/г/ГОСТ_тип_А.ttf')
_FONT_B64 = base64.b64encode(_FONT_PATH.read_bytes()).decode()
_FONT_FACE = (
'<defs><style>'
'@font-face {'
'font-family:"ГОСТ тип А";'
f'src:url("data:font/truetype;base64,{_FONT_B64}") format("truetype");'
'}'
'</style></defs>'
)
# SVG absolute coordinate constants (1 user unit = 1 mm)
ZERO_X = 60.844130 # x of the zero/left tick
MAX_X = 180.251230 # x of the full-scale/right tick
BAR_Y = 107.54670 # y at the top of the scale bar (tick baseline)
SCALE_LEN = MAX_X - ZERO_X # 119.4071 mm
BIG_H = 5.0895 # major tick height
SMALL_H = 2.6683 # minor tick height
def _fmt(v: float) -> str:
if v == int(v):
return str(int(v))
return f"{v:.4g}"
def generate_svg(unit, min_val, max_val, range_label, big_ticks, small_ticks, label_count):
big_interval = SCALE_LEN / big_ticks
big_xs = [ZERO_X + i * big_interval for i in range(big_ticks + 1)]
label_count = max(2, min(label_count, big_ticks + 1))
label_indices = [round(k * big_ticks / (label_count - 1)) for k in range(label_count)]
parts = [
'<?xml version="1.0" encoding="UTF-8" standalone="no"?>',
'<svg width="157.18373mm" height="26.683672mm"'
' viewBox="0 0 157.18373 26.683672" version="1.1"'
' xmlns="http://www.w3.org/2000/svg">',
_FONT_FACE,
' <g transform="translate(-37.352226,-90.454928)">',
# Background rect with scale-bar cutout
' <path style="fill:#f9f9f9;stroke:#1a1a1a;stroke-width:0.184;'
'stroke-linecap:round;stroke-miterlimit:10"'
' d="M 37.444226,90.546928 V 117.0466 H 194.44395 V 90.546928 Z'
' M 58.443978,107.54692 H 183.64409 v 6.19963 H 58.443978 Z" />',
# Unit label (large letter in left panel)
f' <text style="font-size:18.7981px;font-family:\'ГОСТ тип А\',monospace;fill:#1a1a1a"'
f' x="41.455711" y="109.03796"'
f' transform="scale(0.96486402,1.0364155)">{escape(unit)}</text>',
# Range label inside the strip on the right
f' <text style="font-size:3.60539px;font-family:\'ГОСТ тип А\',monospace;fill:#1a1a1a"'
f' x="184.79707" y="113.6755">{escape(range_label)}</text>',
]
# Major ticks
for x in big_xs:
parts.append(
f' <line x1="{x:.5f}" y1="{BAR_Y}" x2="{x:.5f}" y2="{BAR_Y - BIG_H:.5f}"'
f' style="stroke:#1a1a1a;stroke-width:0.555;stroke-linecap:butt" />'
)
# Minor ticks
if small_ticks > 0:
sub = small_ticks + 1
for i in range(big_ticks):
for j in range(1, small_ticks + 1):
x = big_xs[i] + j * big_interval / sub
parts.append(
f' <line x1="{x:.5f}" y1="{BAR_Y}" x2="{x:.5f}" y2="{BAR_Y - SMALL_H:.5f}"'
f' style="stroke:#1a1a1a;stroke-width:0.302;stroke-linecap:butt" />'
)
# Numeric labels (centered on their tick, above the tick area)
for idx in label_indices:
x = big_xs[idx]
val = min_val + (max_val - min_val) * idx / big_ticks
parts.append(
f' <text style="font-size:8.89194px;font-family:\'ГОСТ тип А\',monospace;fill:#1a1a1a"'
f' text-anchor="middle" x="{x:.5f}" y="101.49598">{escape(_fmt(val))}</text>'
)
parts += [' </g>', '</svg>']
return '\n'.join(parts)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/generate', methods=['POST'])
def generate():
unit = request.form.get('unit', '%')
min_val = float(request.form.get('min', 0))
max_val = float(request.form.get('max', 100))
range_label = request.form.get('range_label', '5mA')
big_ticks = max(1, min(50, int(request.form.get('big_ticks', 10))))
small_ticks = max(0, min(20, int(request.form.get('small_ticks', 4))))
label_count = max(2, int(request.form.get('label_count', 6)))
svg = generate_svg(unit, min_val, max_val, range_label, big_ticks, small_ticks, label_count)
return Response(svg, mimetype='image/svg+xml')
if __name__ == '__main__':
app.run(debug=True)
+2
View File
@@ -0,0 +1,2 @@
flask
gunicorn
+213
View File
@@ -0,0 +1,213 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>M1730 Scale Generator</title>
<style>
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: system-ui, sans-serif;
background: #f4f4f5;
color: #222;
min-height: 100vh;
padding: 2rem 1.5rem;
}
.container { max-width: 840px; margin: 0 auto; }
h1 { font-size: 1.3rem; font-weight: 600; margin-bottom: 1.5rem; }
.card {
background: #fff;
border: 1px solid #ddd;
border-radius: 8px;
padding: 1.5rem;
margin-bottom: 1.5rem;
}
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 1rem 2rem;
}
@media (max-width: 560px) { .grid { grid-template-columns: 1fr; } }
label {
display: flex;
flex-direction: column;
gap: 0.3rem;
font-size: 0.88rem;
font-weight: 600;
}
label small { font-weight: normal; color: #666; font-size: 0.8rem; }
input[type="text"],
input[type="number"] {
padding: 0.4rem 0.65rem;
border: 1px solid #bbb;
border-radius: 4px;
font-size: 0.95rem;
width: 100%;
transition: border-color 0.15s;
}
input:focus { outline: 2px solid #0070f3; outline-offset: 1px; border-color: transparent; }
.actions {
grid-column: 1 / -1;
display: flex;
gap: 0.75rem;
align-items: center;
margin-top: 0.5rem;
}
button {
padding: 0.45rem 1.2rem;
font-size: 0.9rem;
border: none;
border-radius: 4px;
cursor: pointer;
font-weight: 500;
}
#btn-generate { background: #0070f3; color: #fff; }
#btn-generate:hover { background: #005fd4; }
#btn-download { background: #e5e5e5; color: #222; }
#btn-download:hover:not(:disabled) { background: #d4d4d4; }
#btn-download:disabled { opacity: 0.45; cursor: not-allowed; }
#status { font-size: 0.82rem; color: #888; }
.preview-card {
background: #fff;
border: 1px solid #ddd;
border-radius: 8px;
padding: 1.5rem;
}
.preview-label {
font-size: 0.8rem;
font-weight: 600;
color: #888;
text-transform: uppercase;
letter-spacing: 0.05em;
margin-bottom: 1rem;
}
#preview svg { width: 100%; height: auto; display: block; }
#preview-placeholder { font-size: 0.9rem; color: #aaa; }
</style>
</head>
<body>
<div class="container">
<h1>M1730 Scale Generator</h1>
<div class="card">
<form id="form" class="grid">
<label>
Unit
<small>Large letter left of scale</small>
<input name="unit" type="text" value="%" required>
</label>
<label>
Range label
<small>Small text in the strip (e.g. 5mA, 10V)</small>
<input name="range_label" type="text" value="5mA" required>
</label>
<label>
Min value
<small>Value at the left (zero) end</small>
<input name="min" type="number" step="any" value="0" required>
</label>
<label>
Max value
<small>Value at the right end</small>
<input name="max" type="number" step="any" value="100" required>
</label>
<label>
Major tick intervals
<small>Number of divisions (e.g. 10 → 0, 10, 20 … 100)</small>
<input name="big_ticks" type="number" min="1" max="50" value="10" required>
</label>
<label>
Minor ticks per interval
<small>Small ticks between each major pair</small>
<input name="small_ticks" type="number" min="0" max="20" value="4" required>
</label>
<label>
Number of labels
<small>Numeric values to print; best when (count1) divides intervals</small>
<input name="label_count" type="number" min="2" value="6" required>
</label>
<div class="actions">
<button type="submit" id="btn-generate">Generate</button>
<button type="button" id="btn-download" disabled>Download SVG</button>
<span id="status"></span>
</div>
</form>
</div>
<div class="preview-card">
<div class="preview-label">Preview</div>
<div id="preview">
<p id="preview-placeholder">Fill in the form and click Generate.</p>
</div>
</div>
</div>
<script>
let lastSvg = '';
async function doGenerate() {
const status = document.getElementById('status');
status.textContent = 'Generating…';
try {
const resp = await fetch('generate', {
method: 'POST',
body: new FormData(document.getElementById('form')),
});
if (!resp.ok) throw new Error(await resp.text());
lastSvg = await resp.text();
document.getElementById('preview').innerHTML = lastSvg;
document.getElementById('btn-download').disabled = false;
status.textContent = '';
} catch (err) {
status.textContent = 'Error: ' + err.message;
}
}
document.getElementById('form').addEventListener('submit', e => {
e.preventDefault();
doGenerate();
});
document.getElementById('btn-download').addEventListener('click', () => {
if (!lastSvg) return;
const a = Object.assign(document.createElement('a'), {
href: URL.createObjectURL(new Blob([lastSvg], { type: 'image/svg+xml' })),
download: 'M1730-Scale.svg',
});
a.click();
URL.revokeObjectURL(a.href);
});
// Show default scale on load
doGenerate();
</script>
</body>
</html>