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:
+16
-13
@@ -9,7 +9,7 @@ from fontTools.pens.svgPathPen import SVGPathPen
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
VERSION = "0.85"
|
||||
VERSION = "0.86"
|
||||
|
||||
_LOGO_PATH = pathlib.Path(__file__).parent / 'static' / 'logo.png'
|
||||
_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_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):
|
||||
@@ -244,9 +245,10 @@ def _has_markup(s: str) -> bool:
|
||||
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)
|
||||
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
|
||||
|
||||
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,
|
||||
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()))
|
||||
unit_box_y1 = _UNIT_BOX_HALF_Y1 if half_height_unit else None
|
||||
|
||||
if scale_type == 'log':
|
||||
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" />'
|
||||
)
|
||||
|
||||
if _has_markup(unit):
|
||||
unit_svg = ' ' + _markup_unit_svg(unit, fd)
|
||||
elif use_paths:
|
||||
unit_svg = ' ' + _text_to_paths(
|
||||
unit, 41.455711, 109.03796, _unit_font_size(unit, fd), fd,
|
||||
wrap_transform=f'scale({_UNIT_SCALE_X},1.0364155)'
|
||||
)
|
||||
# Paths mode and any unit needing special handling (markup, half-height):
|
||||
# use _markup_unit_svg which fits actual ink bounds to the box, giving
|
||||
# correct sizing for all fonts. Text mode for plain normal-height units
|
||||
# keeps live <text> nodes; half-height always uses paths for correct centering.
|
||||
if use_paths or _has_markup(unit) or half_height_unit:
|
||||
unit_svg = ' ' + _markup_unit_svg(unit, fd, box_y1=unit_box_y1)
|
||||
else:
|
||||
fs = _unit_font_size(unit, fd)
|
||||
unit_svg = (
|
||||
@@ -503,7 +505,8 @@ def generate():
|
||||
label_count = max(2, int(request.form.get('label_count', 6)))
|
||||
use_paths = request.form.get('output_mode', 'paths') == 'paths'
|
||||
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':
|
||||
max_val = math.inf
|
||||
|
||||
@@ -513,7 +516,7 @@ def generate():
|
||||
try:
|
||||
svg = generate_svg(unit, min_val, max_val, range_label, big_ticks, small_ticks,
|
||||
label_count, use_paths, scale_type, custom_labels, label_size,
|
||||
font_key)
|
||||
font_key, half_height_unit)
|
||||
except ValueError as err:
|
||||
return Response(str(err), status=400, mimetype='text/plain')
|
||||
return Response(svg, mimetype='image/svg+xml')
|
||||
|
||||
@@ -302,6 +302,14 @@
|
||||
<label>Unit</label>
|
||||
<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>
|
||||
<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 class="field">
|
||||
<label>Range label</label>
|
||||
@@ -511,6 +519,15 @@
|
||||
|
||||
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();
|
||||
</script>
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user