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

Function _crop_svg

SLiCAP/SLiCAPsvgTools.py:143–178  ·  view source on GitHub ↗

Crop the SVG file by adjusting its viewBox and dimensions.

(input_path)

Source from the content-addressed store, hash-verified

141 return min_x, min_y, max_x, max_y
142
143def _crop_svg(input_path):
144 """Crop the SVG file by adjusting its viewBox and dimensions."""
145 tree = ET.parse(input_path)
146 root = tree.getroot()
147
148 # Calculate the bounding box
149 bbox = _calculate_svg_bbox(root)
150 if not bbox:
151 raise ValueError("No visible elements found to crop.")
152
153 min_x, min_y, max_x, max_y = bbox
154
155 # Add margin around the image
156
157 min_x -= ini.svg_margin
158 min_y -= ini.svg_margin
159 max_x += ini.svg_margin
160 max_y += ini.svg_margin
161
162 width, height = max(0, max_x - min_x), max(0, max_y - min_y)
163
164 # Update the viewBox and dimensions
165 root.set("viewBox", f"{min_x} {min_y} {width} {height}")
166 root.set("width", f"{width}mm")
167 root.set("height", f"{height}mm")
168
169 # Insert white background rectangle as first child
170 ns = root.tag.split("}")[0].lstrip("{") if "}" in root.tag else ""
171 tag = f"{{{ns}}}rect" if ns else "rect"
172 bg = ET.Element(tag, attrib={
173 "x": str(min_x), "y": str(min_y),
174 "width": str(width), "height": str(height),
175 "fill": "white"
176 })
177 root.insert(0, bg)
178 tree.write(input_path)
179
180if __name__ == "__main__":
181

Callers 2

KiCADsch2svgFunction · 0.90
SLiCAPsvgTools.pyFile · 0.85

Calls 2

_calculate_svg_bboxFunction · 0.85
writeMethod · 0.80

Tested by

no test coverage detected