MCPcopy Create free account
hub / github.com/SLiCAP/SLiCAP_python / _image_svg

Function _image_svg

SLiCAP/schematic/export.py:233–263  ·  view source on GitHub ↗

Dispatch to vector-inline (SVG source) or base64 PNG (everything else).

(parent, defs, item)

Source from the content-addressed store, hash-verified

231
232
233def _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
266def _inline_image_svg(parent, defs, svg_bytes: bytes,

Callers 1

_build_svgFunction · 0.85

Calls 2

_inline_image_svgFunction · 0.85
saveMethod · 0.45

Tested by

no test coverage detected