svg correction

This commit is contained in:
2026-05-12 21:43:27 +02:00
parent 0e635d124e
commit 367d72e200
83 changed files with 560 additions and 505 deletions

View File

@@ -18,6 +18,7 @@ FRACTION_GROUP_RE = re.compile(
)
TEXT_RE = re.compile(r"(<text\b[^>]*\by=\")[-0-9.]+(\"[^>]*>)")
LINE_Y_RE = re.compile(r"<line\b[^>]*\by1=\"(?P<y1>[-0-9.]+)\"[^>]*\by2=\"(?P<y2>[-0-9.]+)\"")
DATA_BOX_RE = re.compile(r'data-box="(?P<x>[-0-9.]+) (?P<y>[-0-9.]+) (?P<w>[-0-9.]+) (?P<h>[-0-9.]+)"')
def parse_class_font_sizes(svg: str) -> dict[str, float]:
@@ -50,7 +51,9 @@ def compact_group(match: re.Match[str], class_sizes: dict[str, float]) -> str:
bar_y = (float(line_match.group("y1")) + float(line_match.group("y2"))) / 2
size = group_font_size(body, class_sizes)
numerator_y = bar_y - min(max(8.0, size * 0.38), size * 0.50)
denominator_y = bar_y + min(max(10.0, size * 0.48), size * 0.55)
# SVG text y is a baseline, not the visible top of the digit. The denominator
# baseline must therefore sit lower so the glyph itself clears the bar.
denominator_y = bar_y + max(18.0, size * 1.05)
replacements = [f"{numerator_y:g}", f"{denominator_y:g}"]
@@ -60,7 +63,18 @@ def compact_group(match: re.Match[str], class_sizes: dict[str, float]) -> str:
return f"{text_match.group(1)}{replacements.pop(0)}{text_match.group(2)}"
body = TEXT_RE.sub(replace_text_y, body, count=2)
return f"{match.group('prefix')}{body}{match.group('suffix')}"
data_box_y = numerator_y - size * 0.85 - 2
data_box_height = denominator_y + size * 0.25 + 2 - data_box_y
prefix = DATA_BOX_RE.sub(
lambda data_match: (
f'data-box="{data_match.group("x")} {data_box_y:g} '
f'{data_match.group("w")} {data_box_height:g}"'
),
match.group("prefix"),
count=1,
)
return f"{prefix}{body}{match.group('suffix')}"
def compact_svg(path: Path) -> None: