Rewrite generate_svg skeleton around arc geometry (body/rim/pivot only)
This commit is contained in:
+68
-170
@@ -52,18 +52,40 @@ def _load_fonts():
|
|||||||
_FONTS = _load_fonts()
|
_FONTS = _load_fonts()
|
||||||
_DEFAULT_FONT = 'alte-din-1451-mittelschrift.regular'
|
_DEFAULT_FONT = 'alte-din-1451-mittelschrift.regular'
|
||||||
|
|
||||||
# SVG absolute coordinate constants (1 user unit = 1 mm)
|
# SVG absolute coordinate constants (1 user unit = 1 mm), pre-layer-translate
|
||||||
ZERO_X = 60.844130
|
# local frame — the <g> wrapper applies translate(-60.717741,-107.14126).
|
||||||
MAX_X = 180.251230
|
PIVOT_X, PIVOT_Y = 107.42845, 162.62819
|
||||||
BAR_Y = 107.54670
|
R0 = 43.842751 # rim / inner-tick radius
|
||||||
SCALE_LEN = MAX_X - ZERO_X # 119.4071 mm
|
ANGLE_MIN, ANGLE_MAX = -45.667193, 45.667193 # degrees from vertical, clockwise-positive
|
||||||
|
|
||||||
BIG_H = 5.0895
|
MAJOR_LEN, MAJOR_W = 4.7839203, 0.79658329
|
||||||
SMALL_H = 2.6683
|
MEDIUM_LEN, MEDIUM_W = 3.1215858, 0.37985462
|
||||||
|
SMALL_LEN, SMALL_W = 1.8121802, 0.27478597
|
||||||
|
|
||||||
_UNIT_AREA_WIDTH = 18.445
|
LABEL_RADIUS = 49.862
|
||||||
_UNIT_SCALE_X = 0.96486402
|
|
||||||
_UNIT_MAX_FS = 18.7981
|
UNIT_X, UNIT_Y = 107.03512, 136.64594
|
||||||
|
|
||||||
|
LEFT_X, LEFT_Y0 = 73.432251, 145.16051
|
||||||
|
RIGHT_X, RIGHT_Y0 = 142.4485, 145.32773
|
||||||
|
CORNER_LINE_SPACING = 4.4586
|
||||||
|
CORNER_FONT_SIZE = 3.56685
|
||||||
|
|
||||||
|
PIVOT_CIRCLE_R = 1.7287594
|
||||||
|
|
||||||
|
BODY_D = (
|
||||||
|
"m 62.162097,107.14126 c -0.800087,0 -1.444356,0.64428 -1.444356,1.44436 v 45.44839 "
|
||||||
|
"c 0,0.80008 0.644269,1.44436 1.444356,1.44436 h 7.345288 c 0.902634,0 1.233517,1.12706 "
|
||||||
|
"1.233517,1.12706 l 0.09043,3.03909 21.903573,-0.0904 2.226221,-5.98775 c 8.352594,-2.8884 "
|
||||||
|
"16.624974,-2.8884 24.817094,0 l 2.22674,5.98775 21.90357,0.0904 0.0904,-3.03909 c 0,0 "
|
||||||
|
"0.33088,-1.12706 1.23352,-1.12706 h 7.86567 c 0.80009,0 1.44384,-0.64428 1.44384,-1.44436 "
|
||||||
|
"v -45.44839 c 0,-0.80008 -0.64375,-1.44436 -1.44384,-1.44436 z m 29.619898,45.85354 "
|
||||||
|
"a 1.3990685,1.3990685 0 0 1 1.398881,1.39939 1.3990685,1.3990685 0 0 1 -1.398881,1.39888 "
|
||||||
|
"1.3990685,1.3990685 0 0 1 -1.399398,-1.39888 1.3990685,1.3990685 0 0 1 1.399398,-1.39939 "
|
||||||
|
"z m 31.220835,0 a 1.3990685,1.3990685 0 0 1 1.39888,1.39939 1.3990685,1.3990685 0 0 1 "
|
||||||
|
"-1.39888,1.39888 1.3990685,1.3990685 0 0 1 -1.3994,-1.39888 1.3990685,1.3990685 0 0 1 "
|
||||||
|
"1.3994,-1.39939 z"
|
||||||
|
)
|
||||||
|
|
||||||
# --- Inline markup for the unit label -------------------------------------
|
# --- Inline markup for the unit label -------------------------------------
|
||||||
_SUP_SCALE = 0.62
|
_SUP_SCALE = 0.62
|
||||||
@@ -74,9 +96,15 @@ _FRAC_AXIS = 0.32
|
|||||||
_FRAC_GAP = 0.14
|
_FRAC_GAP = 0.14
|
||||||
_FRAC_SIDE = 0.12
|
_FRAC_SIDE = 0.12
|
||||||
|
|
||||||
_UNIT_BOX_X0, _UNIT_BOX_X1 = 39.6, 57.4
|
_UNIT_BOX_X0, _UNIT_BOX_X1 = UNIT_X - 15.0, UNIT_X + 15.0
|
||||||
_UNIT_BOX_Y0, _UNIT_BOX_Y1 = 92.8, 114.8
|
_UNIT_BOX_Y0, _UNIT_BOX_Y1 = UNIT_Y - 6.0, UNIT_Y + 6.0
|
||||||
_UNIT_BOX_HALF_Y0 = (_UNIT_BOX_Y0 + _UNIT_BOX_Y1) / 2 # 103.8 — bottom half starts here
|
_UNIT_MAX_FS = 9.46938021
|
||||||
|
_UNIT_SCALE_X = 1.0
|
||||||
|
|
||||||
|
|
||||||
|
def _point_at(radius: float, angle_deg: float):
|
||||||
|
th = math.radians(angle_deg)
|
||||||
|
return PIVOT_X + radius * math.sin(th), PIVOT_Y - radius * math.cos(th)
|
||||||
|
|
||||||
|
|
||||||
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):
|
||||||
@@ -247,11 +275,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', box_y0=None, box_y1=None) -> str:
|
def _markup_unit_svg(unit: str, fd: _FontData, fill='#1a1a1a') -> str:
|
||||||
nodes = _parse_markup(unit)
|
nodes = _parse_markup(unit)
|
||||||
x0, x1 = _UNIT_BOX_X0, _UNIT_BOX_X1
|
x0, x1 = _UNIT_BOX_X0, _UNIT_BOX_X1
|
||||||
y0 = box_y0 if box_y0 is not None else _UNIT_BOX_Y0
|
y0, y1 = _UNIT_BOX_Y0, _UNIT_BOX_Y1
|
||||||
y1 = 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)
|
||||||
@@ -270,13 +297,6 @@ def _markup_unit_svg(unit: str, fd: _FontData, fill='#1a1a1a', box_y0=None, box_
|
|||||||
return f'<g transform="{cond}">\n{chr(10).join(parts)}\n</g>'
|
return f'<g transform="{cond}">\n{chr(10).join(parts)}\n</g>'
|
||||||
|
|
||||||
|
|
||||||
def _unit_font_size(unit: str, fd: _FontData) -> float:
|
|
||||||
em_w = _measure(_parse_markup(unit), 1.0, fd)
|
|
||||||
if em_w <= 0:
|
|
||||||
return _UNIT_MAX_FS
|
|
||||||
return min(_UNIT_MAX_FS, _UNIT_AREA_WIDTH / (em_w * _UNIT_SCALE_X))
|
|
||||||
|
|
||||||
|
|
||||||
def _text_to_paths(text, x, y, font_size, fd: _FontData, anchor='start', fill='#1a1a1a', wrap_transform=None):
|
def _text_to_paths(text, x, y, font_size, fd: _FontData, anchor='start', fill='#1a1a1a', wrap_transform=None):
|
||||||
s = font_size / fd.upm
|
s = font_size / fd.upm
|
||||||
|
|
||||||
@@ -319,90 +339,10 @@ def _fmt(v: float) -> str:
|
|||||||
return s.rstrip('0').rstrip('.') if '.' in s else s
|
return s.rstrip('0').rstrip('.') if '.' in s else s
|
||||||
|
|
||||||
|
|
||||||
def generate_svg(unit, min_val, max_val, range_label, big_ticks, small_ticks, label_count,
|
def generate_svg(unit, min_val, max_val, left_label, right_label, big_ticks, small_ticks,
|
||||||
use_paths=True, scale_type='linear', custom_labels=None, label_size=9,
|
label_count, use_paths=True, scale_type='linear', custom_labels=None,
|
||||||
font_key=None, half_height_unit=False):
|
label_size=5.1126, font_key=None):
|
||||||
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_y0 = _UNIT_BOX_HALF_Y0 if half_height_unit else None
|
|
||||||
|
|
||||||
if scale_type == 'log':
|
|
||||||
if min_val <= 0 or max_val <= 0:
|
|
||||||
raise ValueError("Logarithmic scale requires min and max values greater than 0.")
|
|
||||||
lmin, lmax = math.log(min_val), math.log(max_val)
|
|
||||||
lspan = lmax - lmin
|
|
||||||
if lspan == 0:
|
|
||||||
raise ValueError("Logarithmic scale requires min and max values to differ.")
|
|
||||||
|
|
||||||
def value_to_x(v):
|
|
||||||
return ZERO_X + SCALE_LEN * (math.log(v) - lmin) / lspan
|
|
||||||
|
|
||||||
def major_value(i):
|
|
||||||
return min_val * (max_val / min_val) ** (i / big_ticks)
|
|
||||||
|
|
||||||
elif scale_type == 'sqrt':
|
|
||||||
if min_val < 0:
|
|
||||||
raise ValueError("Square-root scale requires min value ≥ 0.")
|
|
||||||
span = max_val - min_val
|
|
||||||
if span <= 0:
|
|
||||||
raise ValueError("Square-root scale requires max value greater than min.")
|
|
||||||
|
|
||||||
def value_to_x(v):
|
|
||||||
return ZERO_X + SCALE_LEN * math.sqrt((v - min_val) / span)
|
|
||||||
|
|
||||||
def major_value(i):
|
|
||||||
return min_val + span * (i / big_ticks) ** 2
|
|
||||||
|
|
||||||
elif scale_type == 'reciprocal':
|
|
||||||
if min_val <= 0:
|
|
||||||
raise ValueError("Reciprocal scale requires min and max values greater than 0.")
|
|
||||||
if not math.isinf(max_val) and max_val <= min_val:
|
|
||||||
raise ValueError("Reciprocal scale requires max value greater than min.")
|
|
||||||
if math.isinf(max_val):
|
|
||||||
def value_to_x(v):
|
|
||||||
if math.isinf(v):
|
|
||||||
return ZERO_X
|
|
||||||
return ZERO_X + SCALE_LEN * min_val / v
|
|
||||||
|
|
||||||
def major_value(i):
|
|
||||||
if i == 0:
|
|
||||||
return math.inf
|
|
||||||
return min_val * big_ticks / i
|
|
||||||
else:
|
|
||||||
if max_val <= 0:
|
|
||||||
raise ValueError("Reciprocal scale requires min and max values greater than 0.")
|
|
||||||
k = 1.0 / min_val - 1.0 / max_val
|
|
||||||
|
|
||||||
def value_to_x(v):
|
|
||||||
return ZERO_X + SCALE_LEN * (1.0 / v - 1.0 / max_val) / k
|
|
||||||
|
|
||||||
def major_value(i):
|
|
||||||
return 1.0 / (1.0 / max_val + i / big_ticks * k)
|
|
||||||
|
|
||||||
else:
|
|
||||||
span = max_val - min_val
|
|
||||||
|
|
||||||
def value_to_x(v):
|
|
||||||
if span == 0:
|
|
||||||
return ZERO_X
|
|
||||||
return ZERO_X + SCALE_LEN * (v - min_val) / span
|
|
||||||
|
|
||||||
def major_value(i):
|
|
||||||
return min_val + span * i / big_ticks
|
|
||||||
|
|
||||||
big_interval = SCALE_LEN / big_ticks
|
|
||||||
big_xs = [ZERO_X + i * big_interval for i in range(big_ticks + 1)]
|
|
||||||
big_vals = [major_value(i) for i in range(big_ticks + 1)]
|
|
||||||
|
|
||||||
if custom_labels:
|
|
||||||
label_count = max(1, min(len(custom_labels), big_ticks + 1))
|
|
||||||
custom_labels = custom_labels[:label_count]
|
|
||||||
else:
|
|
||||||
label_count = max(2, min(label_count, big_ticks + 1))
|
|
||||||
|
|
||||||
if label_count == 1:
|
|
||||||
label_indices = [0]
|
|
||||||
else:
|
|
||||||
label_indices = [round(k * big_ticks / (label_count - 1)) for k in range(label_count)]
|
|
||||||
|
|
||||||
font_face = (
|
font_face = (
|
||||||
'<defs><style>'
|
'<defs><style>'
|
||||||
@@ -413,77 +353,35 @@ def generate_svg(unit, min_val, max_val, range_label, big_ticks, small_ticks, la
|
|||||||
|
|
||||||
parts = [
|
parts = [
|
||||||
'<?xml version="1.0" encoding="UTF-8" standalone="no"?>',
|
'<?xml version="1.0" encoding="UTF-8" standalone="no"?>',
|
||||||
'<svg width="157.18373mm" height="26.683672mm"'
|
'<svg width="93.824249mm" height="57.215687mm"'
|
||||||
' viewBox="0 0 157.18373 26.683672" version="1.1"'
|
' viewBox="0 0 93.824249 57.215687" version="1.1"'
|
||||||
' xmlns="http://www.w3.org/2000/svg">',
|
' xmlns="http://www.w3.org/2000/svg">',
|
||||||
]
|
]
|
||||||
|
|
||||||
if use_paths:
|
if use_paths:
|
||||||
parts.append(' <g transform="translate(-37.352226,-90.454928)">')
|
parts.append(' <g transform="translate(-60.717741,-107.14126)">')
|
||||||
else:
|
else:
|
||||||
parts += [font_face, ' <g transform="translate(-37.352226,-90.454928)">']
|
parts += [font_face, ' <g transform="translate(-60.717741,-107.14126)">']
|
||||||
|
|
||||||
parts.append(
|
parts.append(
|
||||||
' <path style="fill:#ffffff;stroke:#1a1a1a;stroke-width:0.184;'
|
f' <path style="fill:#f9f9f9;stroke:#1a1a1a;stroke-width:0.184;'
|
||||||
'stroke-linecap:round;stroke-miterlimit:10"'
|
f'stroke-linecap:round;stroke-miterlimit:10" d="{BODY_D}" />'
|
||||||
' 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" />'
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Paths mode and any unit needing special handling (markup, half-height):
|
p0 = _point_at(R0, ANGLE_MIN)
|
||||||
# use _markup_unit_svg which fits actual ink bounds to the box, giving
|
pmid = _point_at(R0, 0.0)
|
||||||
# correct sizing for all fonts. Text mode for plain normal-height units
|
p1 = _point_at(R0, ANGLE_MAX)
|
||||||
# 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_y0=unit_box_y0)
|
|
||||||
else:
|
|
||||||
fs = _unit_font_size(unit, fd)
|
|
||||||
unit_svg = (
|
|
||||||
f' <text style="font-size:{fs:.4f}px;font-family:\'{fd.label}\',monospace;fill:#1a1a1a"'
|
|
||||||
f' x="41.455711" y="109.03796"'
|
|
||||||
f' transform="scale({_UNIT_SCALE_X},1.0364155)">{escape(unit)}</text>'
|
|
||||||
)
|
|
||||||
|
|
||||||
if use_paths:
|
|
||||||
parts += [unit_svg, ' ' + _text_to_paths(range_label, 184.79707, 113.6755, 3.60539, fd)]
|
|
||||||
else:
|
|
||||||
parts += [
|
|
||||||
unit_svg,
|
|
||||||
f' <text style="font-size:3.60539px;font-family:\'{fd.label}\',monospace;fill:#1a1a1a"'
|
|
||||||
f' x="184.79707" y="113.6755">{escape(range_label)}</text>',
|
|
||||||
]
|
|
||||||
|
|
||||||
for x in big_xs:
|
|
||||||
parts.append(
|
parts.append(
|
||||||
f' <line x1="{x:.5f}" y1="{BAR_Y}" x2="{x:.5f}" y2="{BAR_Y - BIG_H:.5f}"'
|
f' <path style="fill:none;stroke:#000000;stroke-width:0.484;'
|
||||||
f' style="stroke:#1a1a1a;stroke-width:0.555;stroke-linecap:butt" />'
|
f'stroke-linecap:round;stroke-miterlimit:10"'
|
||||||
|
f' d="M {p0[0]:.5f},{p0[1]:.5f} A {R0},{R0} 0 0 1 {pmid[0]:.5f},{pmid[1]:.5f}'
|
||||||
|
f' A {R0},{R0} 0 0 1 {p1[0]:.5f},{p1[1]:.5f}" />'
|
||||||
)
|
)
|
||||||
|
|
||||||
if small_ticks > 0:
|
|
||||||
sub = small_ticks + 1
|
|
||||||
for i in range(big_ticks):
|
|
||||||
for j in range(1, small_ticks + 1):
|
|
||||||
if scale_type == 'sqrt':
|
|
||||||
x = big_xs[i] + (j / sub) ** 2 * (big_xs[i + 1] - big_xs[i])
|
|
||||||
elif scale_type == 'reciprocal' or math.isinf(big_vals[i]):
|
|
||||||
x = big_xs[i] + (j / sub) * (big_xs[i + 1] - big_xs[i])
|
|
||||||
else:
|
|
||||||
v = big_vals[i] + j * (big_vals[i + 1] - big_vals[i]) / sub
|
|
||||||
x = value_to_x(v)
|
|
||||||
parts.append(
|
parts.append(
|
||||||
f' <line x1="{x:.5f}" y1="{BAR_Y}" x2="{x:.5f}" y2="{BAR_Y - SMALL_H:.5f}"'
|
f' <circle style="fill:none;stroke:#b3b3b3;stroke-width:0.184;'
|
||||||
f' style="stroke:#1a1a1a;stroke-width:0.302;stroke-linecap:butt" />'
|
f'stroke-linecap:round;stroke-miterlimit:10"'
|
||||||
)
|
f' cx="{PIVOT_X:.5f}" cy="{PIVOT_Y:.5f}" r="{PIVOT_CIRCLE_R}" />'
|
||||||
|
|
||||||
for k, idx in enumerate(label_indices):
|
|
||||||
x = big_xs[idx]
|
|
||||||
label = custom_labels[k] if custom_labels else _fmt(big_vals[idx])
|
|
||||||
if use_paths:
|
|
||||||
parts.append(' ' + _text_to_paths(label, x, 101.49598, label_size, fd, anchor='middle'))
|
|
||||||
else:
|
|
||||||
parts.append(
|
|
||||||
f' <text style="font-size:{label_size:.5f}px;font-family:\'{fd.label}\',monospace;fill:#1a1a1a"'
|
|
||||||
f' text-anchor="middle" x="{x:.5f}" y="101.49598">{escape(label)}</text>'
|
|
||||||
)
|
)
|
||||||
|
|
||||||
parts += [' </g>', '</svg>']
|
parts += [' </g>', '</svg>']
|
||||||
@@ -502,24 +400,24 @@ def generate():
|
|||||||
unit = request.form.get('unit', '%')
|
unit = request.form.get('unit', '%')
|
||||||
min_val = float(request.form.get('min', 0))
|
min_val = float(request.form.get('min', 0))
|
||||||
max_val = float(request.form.get('max', 100))
|
max_val = float(request.form.get('max', 100))
|
||||||
range_label = request.form.get('range_label', '5mA')
|
left_label = request.form.get('left_label', '')
|
||||||
|
right_label = request.form.get('right_label', '')
|
||||||
big_ticks = max(1, min(50, int(request.form.get('big_ticks', 10))))
|
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))))
|
small_ticks = max(0, min(20, int(request.form.get('small_ticks', 4))))
|
||||||
label_count = max(2, int(request.form.get('label_count', 6)))
|
label_count = max(2, int(request.form.get('label_count', 6)))
|
||||||
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
|
||||||
|
|
||||||
custom_labels = [s.strip() for s in request.form.get('labels', '').split(',') if s.strip()] or None
|
custom_labels = [s.strip() for s in request.form.get('labels', '').split(',') if s.strip()] or None
|
||||||
label_size = max(0.5, min(25.0, float(request.form.get('label_size', 9))))
|
label_size = max(0.5, min(25.0, float(request.form.get('label_size', 5.1126))))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
svg = generate_svg(unit, min_val, max_val, range_label, big_ticks, small_ticks,
|
svg = generate_svg(unit, min_val, max_val, left_label, right_label, big_ticks,
|
||||||
label_count, use_paths, scale_type, custom_labels, label_size,
|
small_ticks, label_count, use_paths, scale_type, custom_labels,
|
||||||
font_key, half_height_unit)
|
label_size, font_key)
|
||||||
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')
|
||||||
|
|||||||
Reference in New Issue
Block a user