svg-processor/processing/position_utils.py
2025-05-16 18:15:31 +04:00

33 lines
1.3 KiB
Python

import logging
from typing import Tuple, Optional
logger = logging.getLogger(__name__)
def extract_position_from_attributes(element, svg_type) -> Tuple[Optional[float], Optional[float]]:
"""Extract position directly from element attributes as a fallback."""
try:
if svg_type == 'rect':
x = float(element.getAttribute('x') or 0)
y = float(element.getAttribute('y') or 0)
return x, y
elif svg_type == 'circle' or svg_type == 'ellipse':
cx = float(element.getAttribute('cx') or 0)
cy = float(element.getAttribute('cy') or 0)
if svg_type == 'circle':
r = float(element.getAttribute('r') or 0)
return cx - r, cy - r
else: # ellipse
rx = float(element.getAttribute('rx') or 0)
ry = float(element.getAttribute('ry') or 0)
return cx - rx, cy - ry
elif svg_type == 'line':
x1 = float(element.getAttribute('x1') or 0)
y1 = float(element.getAttribute('y1') or 0)
return x1, y1
elif svg_type == 'path':
return None, None
except (ValueError, TypeError) as e:
logger.error(f"Error extracting position from attributes: {e}")
return None, None