Dispatch to vector-inline (SVG source) or base64 PNG (everything else).
(parent, defs, item)
| 231 | |
| 232 | |
| 233 | def _image_svg(parent, defs, item) -> None: |
| 234 | """Dispatch to vector-inline (SVG source) or base64 PNG (everything else).""" |
| 235 | from pathlib import Path as _Path |
| 236 | ext = _Path(item.file_path).suffix.lower() |
| 237 | pos = item.pos() |
| 238 | x, y = pos.x(), pos.y() |
| 239 | w, h = item.display_width, item.display_height |
| 240 | |
| 241 | if ext == ".svg": |
| 242 | try: |
| 243 | svg_bytes = _Path(item.file_path).read_bytes() |
| 244 | except OSError: |
| 245 | return |
| 246 | _inline_image_svg(parent, defs, svg_bytes, x, y, w, h) |
| 247 | return |
| 248 | |
| 249 | # Raster path (PNG, JPG, PDF rendered to pixmap, etc.) |
| 250 | px = item._pixmap |
| 251 | if px is None or px.isNull(): |
| 252 | return |
| 253 | buf = QBuffer() |
| 254 | buf.open(QIODevice.WriteOnly) |
| 255 | px.save(buf, "PNG") |
| 256 | data = base64.b64encode(bytes(buf.data())).decode("ascii") |
| 257 | buf.close() |
| 258 | el = ET.SubElement(parent, f"{{{_SVG_NS}}}image") |
| 259 | el.set("x", f"{x:.2f}") |
| 260 | el.set("y", f"{y:.2f}") |
| 261 | el.set("width", f"{px.width()}") |
| 262 | el.set("height", f"{px.height()}") |
| 263 | el.set("href", f"data:image/png;base64,{data}") |
| 264 | |
| 265 | |
| 266 | def _inline_image_svg(parent, defs, svg_bytes: bytes, |
no test coverage detected