Add standard SVG object ID checking.

This commit is contained in:
Sergey Vartanov 2021-04-30 23:50:37 +03:00
parent 555285da85
commit ef921370fa
2 changed files with 32 additions and 15 deletions

View file

@ -148,8 +148,8 @@
showgrid="true"
inkscape:document-units="px"
inkscape:current-layer="layer1"
inkscape:cy="134.56631"
inkscape:cx="310.63103"
inkscape:cy="141.63738"
inkscape:cx="360.12851"
inkscape:zoom="11.313708"
inkscape:pageshadow="2"
inkscape:pageopacity="0.0"
@ -543,6 +543,16 @@
inkscape:label="main"
id="layer1"
style="display:inline">
<path
style="opacity:1;fill:none;fill-opacity:1;stroke:#cccccc;stroke-width:1;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
d="m 289.5,451.5 6,-2 6,2 z"
id="path6939"
inkscape:connector-curvature="0" />
<path
inkscape:connector-curvature="0"
id="path6941"
d="m 305.5,451.5 6,-2 6,2 z"
style="opacity:1;fill:none;fill-opacity:1;stroke:#cccccc;stroke-width:1;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" />
<path
style="opacity:1;fill:#000000;fill-opacity:1;stroke:#cccccc;stroke-width:1;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
d="m 227.5,425.5 1,5"
@ -14888,16 +14898,6 @@
<title
id="title6917">pergola</title>
</path>
<path
style="opacity:1;fill:none;fill-opacity:1;stroke:#000000;stroke-width:1;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
d="m 289.5,451.5 6,-2 6,2 z"
id="path6939"
inkscape:connector-curvature="0" />
<path
inkscape:connector-curvature="0"
id="path6941"
d="m 305.5,451.5 6,-2 6,2 z"
style="opacity:1;fill:none;fill-opacity:1;stroke:#000000;stroke-width:1;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1" />
<path
style="opacity:1;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.1;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
d="m 311.51172,465 c -0.0577,-0.001 -0.11517,0.007 -0.16992,0.0254 l -6,2 c -0.20422,0.0681 -0.34191,0.25932 -0.3418,0.47461 -2.1e-4,0.008 -2.1e-4,0.0169 0,0.0254 l 0,1.47461 1,0 0,-1 11,0 0,1 1,0 0,-1.46484 c 0.016,-0.22808 -0.12472,-0.43797 -0.3418,-0.50977 l -6,-2 c -0.0473,-0.0157 -0.0967,-0.0243 -0.14648,-0.0254 z"

Before

Width:  |  Height:  |  Size: 823 KiB

After

Width:  |  Height:  |  Size: 823 KiB

Before After
Before After

View file

@ -82,6 +82,17 @@ class Icon:
svg.add(path)
def is_standard(id_: str):
""" Check whether SVG object ID is standard Inkscape ID. """
if id_ == "base":
return True
for prefix in ["path", "circle", "rect", "defs", "use", "metadata"]:
matcher = re.match(prefix + "\\d+", id_)
if matcher:
return True
return False
class IconExtractor:
"""
Extract icons from SVG file.
@ -116,9 +127,14 @@ class IconExtractor:
self.parse(sub_node)
return
if (node.hasAttribute("id") and node.hasAttribute("d") and
node.getAttribute("id")):
if not node.hasAttribute("id") or not node.getAttribute("id"):
return
id_: str = node.getAttribute("id")
if is_standard(id_):
return
if node.hasAttribute("d"):
path: str = node.getAttribute("d")
matcher = re.match("[Mm] ([0-9.e-]*)[, ]([0-9.e-]*)", path)
if not matcher:
@ -139,10 +155,11 @@ class IconExtractor:
name = child_node.childNodes[0].nodeValue
break
id_: str = node.getAttribute("id")
matcher = re.match(STANDARD_INKSCAPE_ID, id_)
if not matcher:
self.icons[id_] = Icon(path, point, id_, name)
else:
ui.error(f"not standard ID {id_}")
def get_path(self, id_: str) -> (Icon, bool):
"""