Font-aware unit sizing; half-height unit toggle; bump to 0.86

Paths-mode unit rendering now always goes through _markup_unit_svg,
which fits the font's actual ink bounds (width AND height) to the
unit box — correct for all fonts, not just ГОСТ тип А.

Added half-height unit toggle: restricts the unit label to the upper
half of the panel box (y1 = midpoint at 103.8 mm), keeping it clear
of the numeric labels when they visually crowd the unit area.
This commit is contained in:
2026-07-15 00:10:02 +02:00
parent 62d9f02a82
commit 6bf7c70231
3 changed files with 34 additions and 14 deletions
+1 -1
View File
@@ -15,7 +15,7 @@ spec:
spec: spec:
containers: containers:
- name: m1730-generator - name: m1730-generator
image: git.baumann.gr/adebaumann/m1730-generator:0.85 image: git.baumann.gr/adebaumann/m1730-generator:0.86
imagePullPolicy: Always imagePullPolicy: Always
ports: ports:
- containerPort: 5000 - containerPort: 5000
+15 -12
View File
@@ -9,7 +9,7 @@ from fontTools.pens.svgPathPen import SVGPathPen
app = Flask(__name__) app = Flask(__name__)
VERSION = "0.85" VERSION = "0.86"
_LOGO_PATH = pathlib.Path(__file__).parent / 'static' / 'logo.png' _LOGO_PATH = pathlib.Path(__file__).parent / 'static' / 'logo.png'
_LOGO_B64 = base64.b64encode(_LOGO_PATH.read_bytes()).decode() _LOGO_B64 = base64.b64encode(_LOGO_PATH.read_bytes()).decode()
@@ -74,6 +74,7 @@ _FRAC_SIDE = 0.12
_UNIT_BOX_X0, _UNIT_BOX_X1 = 39.6, 57.4 _UNIT_BOX_X0, _UNIT_BOX_X1 = 39.6, 57.4
_UNIT_BOX_Y0, _UNIT_BOX_Y1 = 92.8, 114.8 _UNIT_BOX_Y0, _UNIT_BOX_Y1 = 92.8, 114.8
_UNIT_BOX_HALF_Y1 = (_UNIT_BOX_Y0 + _UNIT_BOX_Y1) / 2 # 103.8 — upper half of panel
def _glyph_svg(ch: str, x: float, y: float, fs: float, fill: str, fd: _FontData): def _glyph_svg(ch: str, x: float, y: float, fs: float, fill: str, fd: _FontData):
@@ -244,9 +245,10 @@ def _has_markup(s: str) -> bool:
return any(c in s for c in '^_') or ('{' in s and '}' in s) return any(c in s for c in '^_') or ('{' in s and '}' in s)
def _markup_unit_svg(unit: str, fd: _FontData, fill='#1a1a1a') -> str: def _markup_unit_svg(unit: str, fd: _FontData, fill='#1a1a1a', box_y1=None) -> str:
nodes = _parse_markup(unit) nodes = _parse_markup(unit)
x0, x1, y0, y1 = _UNIT_BOX_X0, _UNIT_BOX_X1, _UNIT_BOX_Y0, _UNIT_BOX_Y1 x0, x1 = _UNIT_BOX_X0, _UNIT_BOX_X1
y0, y1 = _UNIT_BOX_Y0, (box_y1 if box_y1 is not None else _UNIT_BOX_Y1)
xc, yc = (x0 + x1) / 2, (y0 + y1) / 2 xc, yc = (x0 + x1) / 2, (y0 + y1) / 2
w1 = _measure(nodes, 1.0, fd) w1 = _measure(nodes, 1.0, fd)
@@ -316,8 +318,9 @@ def _fmt(v: float) -> str:
def generate_svg(unit, min_val, max_val, range_label, big_ticks, small_ticks, label_count, def generate_svg(unit, min_val, max_val, range_label, big_ticks, small_ticks, label_count,
use_paths=True, scale_type='linear', custom_labels=None, label_size=9, use_paths=True, scale_type='linear', custom_labels=None, label_size=9,
font_key=None): font_key=None, half_height_unit=False):
fd = _FONTS.get(font_key) or _FONTS.get(_DEFAULT_FONT) or next(iter(_FONTS.values())) fd = _FONTS.get(font_key) or _FONTS.get(_DEFAULT_FONT) or next(iter(_FONTS.values()))
unit_box_y1 = _UNIT_BOX_HALF_Y1 if half_height_unit else None
if scale_type == 'log': if scale_type == 'log':
if min_val <= 0 or max_val <= 0: if min_val <= 0 or max_val <= 0:
@@ -424,13 +427,12 @@ def generate_svg(unit, min_val, max_val, range_label, big_ticks, small_ticks, la
' M 58.443978,107.54692 H 183.64409 v 6.19963 H 58.443978 Z" />' ' M 58.443978,107.54692 H 183.64409 v 6.19963 H 58.443978 Z" />'
) )
if _has_markup(unit): # Paths mode and any unit needing special handling (markup, half-height):
unit_svg = ' ' + _markup_unit_svg(unit, fd) # use _markup_unit_svg which fits actual ink bounds to the box, giving
elif use_paths: # correct sizing for all fonts. Text mode for plain normal-height units
unit_svg = ' ' + _text_to_paths( # keeps live <text> nodes; half-height always uses paths for correct centering.
unit, 41.455711, 109.03796, _unit_font_size(unit, fd), fd, if use_paths or _has_markup(unit) or half_height_unit:
wrap_transform=f'scale({_UNIT_SCALE_X},1.0364155)' unit_svg = ' ' + _markup_unit_svg(unit, fd, box_y1=unit_box_y1)
)
else: else:
fs = _unit_font_size(unit, fd) fs = _unit_font_size(unit, fd)
unit_svg = ( unit_svg = (
@@ -504,6 +506,7 @@ def generate():
use_paths = request.form.get('output_mode', 'paths') == 'paths' use_paths = request.form.get('output_mode', 'paths') == 'paths'
scale_type = request.form.get('scale_type', 'linear') scale_type = request.form.get('scale_type', 'linear')
font_key = request.form.get('font', _DEFAULT_FONT) font_key = request.form.get('font', _DEFAULT_FONT)
half_height_unit = request.form.get('half_height_unit') == '1'
if request.form.get('max_is_inf') == '1': if request.form.get('max_is_inf') == '1':
max_val = math.inf max_val = math.inf
@@ -513,7 +516,7 @@ def generate():
try: try:
svg = generate_svg(unit, min_val, max_val, range_label, big_ticks, small_ticks, svg = generate_svg(unit, min_val, max_val, range_label, big_ticks, small_ticks,
label_count, use_paths, scale_type, custom_labels, label_size, label_count, use_paths, scale_type, custom_labels, label_size,
font_key) font_key, half_height_unit)
except ValueError as err: except ValueError as err:
return Response(str(err), status=400, mimetype='text/plain') return Response(str(err), status=400, mimetype='text/plain')
return Response(svg, mimetype='image/svg+xml') return Response(svg, mimetype='image/svg+xml')
+17
View File
@@ -302,6 +302,14 @@
<label>Unit</label> <label>Unit</label>
<input name="unit" type="text" value="%" required> <input name="unit" type="text" value="%" required>
<span style="font-family:var(--f-mono);font-size:0.65rem;color:var(--muted)">Markup: cm^2 · H_2O · {1/2} fraction · nests, e.g. {m/s^2}</span> <span style="font-family:var(--f-mono);font-size:0.65rem;color:var(--muted)">Markup: cm^2 · H_2O · {1/2} fraction · nests, e.g. {m/s^2}</span>
<div style="display:flex;align-items:center;gap:0.5rem;margin-top:0.2rem">
<div class="mode-toggle" role="group" aria-label="Unit height">
<button type="button" class="mode-btn active" data-unit-height="normal">Full height</button>
<button type="button" class="mode-btn" data-unit-height="half"
title="Restrict unit label to the upper half of the panel — use when numeric labels visually crowd the unit">Half height</button>
</div>
<input type="hidden" name="half_height_unit" id="half-height-unit" value="0">
</div>
</div> </div>
<div class="field"> <div class="field">
<label>Range label</label> <label>Range label</label>
@@ -511,6 +519,15 @@
document.getElementById('font-select').addEventListener('change', doGenerate); document.getElementById('font-select').addEventListener('change', doGenerate);
document.querySelectorAll('[data-unit-height]').forEach(btn => {
btn.addEventListener('click', () => {
document.querySelectorAll('[data-unit-height]').forEach(b => b.classList.remove('active'));
btn.classList.add('active');
document.getElementById('half-height-unit').value = btn.dataset.unitHeight === 'half' ? '1' : '0';
doGenerate();
});
});
doGenerate(); doGenerate();
</script> </script>
</body> </body>