Skip to content

Commit

Permalink
refactor: Remove expansion of <use> elements
Browse files Browse the repository at this point in the history
  • Loading branch information
YishiMichael committed Sep 7, 2022
1 parent 53c43ee commit ae115d9
Showing 1 changed file with 5 additions and 47 deletions.
52 changes: 5 additions & 47 deletions manimlib/mobject/svg/svg_mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ def get_file_path(self) -> str:
return get_full_vector_image_path(self.file_name)

def modify_xml_tree(self, element_tree: ET.ElementTree) -> ET.ElementTree:
element_tree = self.expand_use_elements(element_tree)

config_style_attrs = self.generate_config_style_dict()
style_keys = (
"fill",
Expand All @@ -133,37 +131,6 @@ def modify_xml_tree(self, element_tree: ET.ElementTree) -> ET.ElementTree:
root_style_node.extend(root)
return ET.ElementTree(new_root)

@staticmethod
def expand_use_elements(element_tree: ET.ElementTree) -> ET.ElementTree:
# Replace `use` elements with copies of elements they refer to
while True:
element = element_tree.find(f".//{SVG_XMLNS}use[@{SVG_XLINK}href]")
if element is None:
break

element.tag = f"{SVG_XMLNS}g"
attrs = element.attrib
href_id = attrs.pop(f"{SVG_XLINK}href")[1:]
href_element = element_tree.find(f".//{SVG_XMLNS}*[@id='{href_id}']")
if href_element is None:
continue
attrs.pop("width", None)
attrs.pop("height", None)
x = attrs.pop("x", "0")
y = attrs.pop("y", "0")
if not x == y == "0":
translate_str = f"translate({x}, {y})"
if "transform" in attrs:
attrs["transform"] = translate_str + " " + attrs["transform"]
else:
attrs["transform"] = translate_str
shadow_node = ET.SubElement(element, href_element.tag, href_element.attrib)
shadow_node.extend(href_element)

for defs_element in element_tree.iterfind(f".//{SVG_XMLNS}defs"):
defs_element.clear()
return element_tree

def generate_config_style_dict(self) -> dict[str, str]:
keys_converting_dict = {
"fill": ("color", "fill_color"),
Expand All @@ -184,17 +151,15 @@ def generate_config_style_dict(self) -> dict[str, str]:
def get_mobjects_from(self, svg: se.SVG) -> list[VMobject]:
result = []
for shape in svg.elements():
if isinstance(shape, se.Group):
if isinstance(shape, (se.Group, se.Use)):
continue
elif isinstance(shape, se.Path):
mob = self.path_to_mobject(shape)
elif isinstance(shape, se.SimpleLine):
mob = self.line_to_mobject(shape)
elif isinstance(shape, se.Rect):
mob = self.rect_to_mobject(shape)
elif isinstance(shape, se.Circle):
mob = self.circle_to_mobject(shape)
elif isinstance(shape, se.Ellipse):
elif isinstance(shape, (se.Circle, se.Ellipse)):
mob = self.ellipse_to_mobject(shape)
elif isinstance(shape, se.Polygon):
mob = self.polygon_to_mobject(shape)
Expand All @@ -209,7 +174,8 @@ def get_mobjects_from(self, svg: se.SVG) -> list[VMobject]:
continue
if not mob.has_points():
continue
self.apply_style_to_mobject(mob, shape)
if isinstance(shape, se.GraphicObject):
self.apply_style_to_mobject(mob, shape)
if isinstance(shape, se.Transformable) and shape.apply:
self.handle_transform(mob, shape.transform)
result.append(mob)
Expand Down Expand Up @@ -268,15 +234,7 @@ def rect_to_mobject(self, rect: se.Rect) -> Rectangle:
))
return mob

def circle_to_mobject(self, circle: se.Circle) -> Circle:
# svgelements supports `rx` & `ry` but `r`
mob = Circle(radius=circle.rx)
mob.shift(_convert_point_to_3d(
circle.cx, circle.cy
))
return mob

def ellipse_to_mobject(self, ellipse: se.Ellipse) -> Circle:
def ellipse_to_mobject(self, ellipse: se.Circle | se.Ellipse) -> Circle:
mob = Circle(radius=ellipse.rx)
mob.stretch_to_fit_height(2 * ellipse.ry)
mob.shift(_convert_point_to_3d(
Expand Down

0 comments on commit ae115d9

Please sign in to comment.