Files
svg2NameTag-Ren/test_parse.py
2026-07-25 16:34:35 -04:00

112 lines
3.8 KiB
Python

"""
Analyze the structural differences between old and new SVG formats
to determine impact on the 3D generation pipeline.
"""
import os
from xml.dom import minidom
from svg.path import parse_path
CLASS_WHITE = 'st2'
CLASS_CYAN = 'st1'
def analyze_svg(filepath, label):
filename = os.path.basename(filepath)
print(f"\n{'='*70}")
print(f"[{label}] {filename}")
print(f"{'='*70}")
doc = minidom.parse(filepath)
# Check SVG root attributes
svg_el = doc.getElementsByTagName('svg')[0]
print(f" viewBox: {svg_el.getAttribute('viewBox')}")
print(f" xmlns: {svg_el.getAttribute('xmlns')}")
print(f" version: {svg_el.getAttribute('version')}")
# Check for <style> block
style_els = doc.getElementsByTagName('style')
print(f" <style> blocks: {len(style_els)}")
# Check for <g> grouping
g_els = doc.getElementsByTagName('g')
print(f" <g> groups: {len(g_els)}")
# Check for non-path elements
for tag in ['text', 'rect', 'circle', 'ellipse', 'line', 'polyline', 'polygon']:
els = doc.getElementsByTagName(tag)
if els:
print(f" <{tag}> elements: {len(els)}")
# Analyze paths
paths_by_category = {'background': [], 'white': [], 'cyan': []}
for path_node in doc.getElementsByTagName('path'):
d_str = path_node.getAttribute('d')
class_str = path_node.getAttribute('class')
fill_str = path_node.getAttribute('fill')
if not d_str:
continue
fill_color = fill_str.lower().strip() if fill_str else ""
# Categorize
if class_str == CLASS_WHITE or fill_color in ['#ffffff', '#fff', 'white']:
cat = 'white'
elif class_str == CLASS_CYAN or fill_color in ['#00aeef', 'cyan']:
cat = 'cyan'
else:
cat = 'background'
# Parse the path and analyze its structure
try:
path_obj = parse_path(d_str)
segments = list(path_obj)
seg_types = [s.__class__.__name__ for s in segments]
# Count sub-paths (loops) via discontinuities
loop_count = 1
for i in range(1, len(segments)):
if abs(segments[i].start - segments[i-1].end) > 1e-4:
loop_count += 1
# Count M commands in raw d string
m_count = d_str.upper().count('M')
paths_by_category[cat].append({
'seg_count': len(segments),
'loop_count': loop_count,
'm_count': m_count,
'seg_types': set(seg_types),
'd_length': len(d_str),
'class': class_str,
'fill': fill_str,
})
except Exception as e:
print(f" ERROR parsing path: {e}")
for cat_name, cat_paths in paths_by_category.items():
print(f"\n --- {cat_name.upper()} ({len(cat_paths)} path elements) ---")
for i, p in enumerate(cat_paths):
print(f" path[{i}]: {p['seg_count']} segments, {p['loop_count']} loops, "
f"{p['m_count']} M-commands, types={p['seg_types']}, "
f"d-len={p['d_length']}, class='{p['class']}', fill='{p['fill']}'")
doc.unlink()
# Analyze old format files
print("\n" + "#"*70)
print("# OLD FORMAT FILES (nametags/)")
print("#"*70)
for f in sorted(os.listdir(r"S:\svg2nametagCLI\nametags")):
if f.endswith('.svg'):
analyze_svg(os.path.join(r"S:\svg2nametagCLI\nametags", f), "OLD")
# Analyze new format files
print("\n" + "#"*70)
print("# NEW FORMAT FILES (input/)")
print("#"*70)
for f in sorted(os.listdir(r"S:\svg2nametagCLI\input")):
if f.endswith('.svg'):
analyze_svg(os.path.join(r"S:\svg2nametagCLI\input", f), "NEW")