Merge branch 'feat-weather' of https://github.com/jpirnay/crosspoint-reader into mybuild
This commit is contained in:
+3
-55
@@ -3,63 +3,11 @@ import os
|
||||
from PIL import Image
|
||||
import cairosvg
|
||||
import io
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
from svg_utils import fit_inside_canvas, parse_svg_intrinsic_size
|
||||
|
||||
threshold = 128
|
||||
|
||||
|
||||
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, int(round(fit_w / src_ratio)))
|
||||
else:
|
||||
fit_h = dst_h
|
||||
fit_w = max(1, int(round(fit_h * src_ratio)))
|
||||
|
||||
return fit_w, fit_h
|
||||
|
||||
|
||||
def svg_to_png_bytes(svg_path, width, height):
|
||||
with open(svg_path, "rb") as f:
|
||||
svg_data = f.read()
|
||||
@@ -124,7 +72,7 @@ def image_to_c_array(img, array_name):
|
||||
byte |= bit << (7 - b)
|
||||
packed.append(byte)
|
||||
# Format as C array
|
||||
c = f"#pragma once\n#include <cstdint>\n\n"
|
||||
c = "#pragma once\n#include <cstdint>\n\n"
|
||||
c += f"// size: {width}x{height}\n"
|
||||
c += f"static const uint8_t {array_name}[] = {{\n "
|
||||
for i, v in enumerate(packed):
|
||||
|
||||
@@ -14,14 +14,15 @@ import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import urllib.request
|
||||
import xml.etree.ElementTree as ET
|
||||
import zipfile
|
||||
|
||||
from PIL import Image
|
||||
|
||||
from svg_utils import fit_inside_canvas, parse_svg_intrinsic_size
|
||||
|
||||
try:
|
||||
import cairosvg # type: ignore
|
||||
except Exception:
|
||||
except ImportError:
|
||||
cairosvg = None
|
||||
|
||||
|
||||
@@ -52,55 +53,6 @@ ICON_SOURCES = {
|
||||
"WI48_THUNDERSTORM": "wi-thunderstorm.svg",
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
|
||||
return parse_len(root.get("width")), parse_len(root.get("height"))
|
||||
|
||||
|
||||
def fit_inside_canvas(src_w, src_h, dst_w, dst_h):
|
||||
if src_w <= 0 or src_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, int(round(fit_w / src_ratio)))
|
||||
else:
|
||||
fit_h = dst_h
|
||||
fit_w = max(1, int(round(fit_h * src_ratio)))
|
||||
return fit_w, fit_h
|
||||
|
||||
|
||||
def ensure_resvg_binary():
|
||||
if shutil.which("resvg"):
|
||||
return Path(shutil.which("resvg"))
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user