Review changes

This commit is contained in:
jpirnay
2026-04-02 20:19:19 +02:00
parent 3f0e272b44
commit b3a80c162a
16 changed files with 176 additions and 446 deletions
+53
View File
@@ -0,0 +1,53 @@
import xml.etree.ElementTree as ET
def parse_svg_intrinsic_size(svg_data):
try:
root = ET.fromstring(svg_data)
except ET.ParseError:
return None, None
viewbox = root.get("viewBox") or root.get("viewbox")
if viewbox:
parts = viewbox.replace(",", " ").split()
if len(parts) == 4:
try:
vb_w = float(parts[2])
vb_h = float(parts[3])
if vb_w > 0 and vb_h > 0:
return vb_w, vb_h
except ValueError:
pass
def parse_len(value):
if not value:
return None
cleaned = "".join(ch for ch in value if ch.isdigit() or ch in ".-")
if not cleaned:
return None
try:
parsed = float(cleaned)
return parsed if parsed > 0 else None
except ValueError:
return None
w = parse_len(root.get("width"))
h = parse_len(root.get("height"))
return w, h
def fit_inside_canvas(src_w, src_h, dst_w, dst_h):
if src_w <= 0 or src_h <= 0 or dst_w <= 0 or dst_h <= 0:
return dst_w, dst_h
src_ratio = src_w / src_h
dst_ratio = dst_w / dst_h
if src_ratio >= dst_ratio:
fit_w = dst_w
fit_h = max(1, round(fit_w / src_ratio))
else:
fit_h = dst_h
fit_w = max(1, round(fit_h * src_ratio))
return fit_w, fit_h