Skip to content

Commit

Permalink
Merge pull request #49 from laurierloi/svg-fix-nested
Browse files Browse the repository at this point in the history
Fix #45 and support Images nodes
  • Loading branch information
hbmartin authored Jul 6, 2024
2 parents 60d4cab + ccf0163 commit be320fc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
26 changes: 20 additions & 6 deletions graphviz2drawio/models/SVG.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
from xml.etree.ElementTree import Element

def svg_tag(tag):
return "{http://www.w3.org/2000/svg}" + tag


def get_first(g: Element, tag: str) -> Element:
return g.findall("./{http://www.w3.org/2000/svg}" + tag)[0]
target = svg_tag(tag)
for i in g.iter():
if i.tag == target:
return i
raise RuntimeError(
f"Failed to find tag ({tag}) in {g}, contains {[i for i in g.iter()]}"
)


def get_title(g: Element) -> str:
return get_first(g, "title").text


def get_text(g: Element) -> str | None:
try:
text_el = get_first(g, "text")
Expand All @@ -18,9 +27,14 @@ def get_text(g: Element) -> str | None:
return text_el.text


def is_tag(g: Element, tag: str) -> bool:
return g.tag == "{http://www.w3.org/2000/svg}" + tag
def is_tag(g, tag):
return g.tag == svg_tag(tag)


def has(g: Element, tag: str) -> bool:
return len(g.findall("./{http://www.w3.org/2000/svg}" + tag)) > 0
def has(g, tag):
try:
get_first(g, tag)
except RuntimeError:
return False
else:
return True
10 changes: 10 additions & 0 deletions graphviz2drawio/mx/NodeFactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ def rect_from_svg_points(self, svg):
height = test_height
return Rect(x=min_x, y=min_y, width=width, height=height)

@staticmethod
def rect_from_image(attrib):
filtered = dict()
for k, v in attrib.items():
if k in ["x", "y", "width", "height"]:
filtered[k] = float(v.strip("px"))
return Rect(**filtered)

def rect_from_ellipse_svg(self, attrib):
cx = float(attrib["cx"])
cy = float(attrib["cy"])
Expand All @@ -47,6 +55,8 @@ def from_svg(self, g) -> Node:
SVG.get_first(g, "polygon").attrib["points"],
)
shape = Shape.RECT
elif SVG.has(g, "image"):
rect = self.rect_from_image(SVG.get_first(g, "image").attrib)
else:
rect = self.rect_from_ellipse_svg(SVG.get_first(g, "ellipse").attrib)
shape = Shape.ELLIPSE
Expand Down
5 changes: 5 additions & 0 deletions test/directed/tooltip.gv.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
digraph {
Hello
World [tooltip=PIZZA]
Hello -> { World }
}

0 comments on commit be320fc

Please sign in to comment.