Parse SVG length and convert to CSS pixels (96 dpi). SVGs with physical units (mm, cm, in, pt) but no viewBox use pixel coordinates internally; the width/height attributes only specify the physical rendered size. Converting to px gives the correct viewport size to use as the scale
(s, fallback: float)
| 46 | |
| 47 | |
| 48 | def _svg_length_to_px(s, fallback: float) -> float: |
| 49 | """Parse SVG length and convert to CSS pixels (96 dpi). |
| 50 | |
| 51 | SVGs with physical units (mm, cm, in, pt) but no viewBox use pixel |
| 52 | coordinates internally; the width/height attributes only specify the |
| 53 | physical rendered size. Converting to px gives the correct viewport |
| 54 | size to use as the scale denominator when inlining the SVG. |
| 55 | """ |
| 56 | m = _SVG_LENGTH_RE.match(str(s)) if s else None |
| 57 | if m: |
| 58 | try: |
| 59 | val = float(m.group(1)) |
| 60 | unit = (m.group(2) or 'px').lower() |
| 61 | return val * _UNIT_TO_PX.get(unit, 1.0) |
| 62 | except ValueError: |
| 63 | pass |
| 64 | return fallback |
| 65 | |
| 66 | |
| 67 | def export_bounds(scene) -> QRectF: |
no test coverage detected