From c03729d9bb9c0902b89a15643e67b620f7dd669b Mon Sep 17 00:00:00 2001 From: Viatrix Date: Fri, 13 Mar 2026 22:19:22 -0700 Subject: Alt attribute now set correctly (+ test). --- imagemap.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'imagemap.py') diff --git a/imagemap.py b/imagemap.py index a40116f..30fa0cd 100644 --- a/imagemap.py +++ b/imagemap.py @@ -20,7 +20,7 @@ def htmlval(val): AREA_ATTRS={ 'href':lambda a:a.get('href',a.get('{http://www.w3.org/1999/xlink}href')), - 'alt':lambda a:a.get('{http://www.w3.org/1999/xlink}title') + 'alt':lambda a:a.get('aria-label',a.get('{http://www.w3.org/1999/xlink}title',a.title)) } # TODO target SHAPE_MARKUP = { 'HTML': lambda shape, coords, href, alt: @@ -96,8 +96,8 @@ class ImageMap(inkex.OutputExtension): # (we re-set the existing style attribute in case it got unset on non-paths) newbytes=inkscape_command(self.svg,actions=command) self.svg=self.load(newbytes).getroot() + # preprocessing done, now for map generation - seen=set() shapes=[[] for i in range(len(links))] for el in self.svg.iterdescendants(): if not isinstance(el,inkex.ShapeElement): continue @@ -106,7 +106,6 @@ class ImageMap(inkex.OutputExtension): linkindex=int(linkindex[len(CSS_LINK_INDEX)+3:-2]) link=links[linkindex] href=link['href'] - alt=link['alt'] if int(linkindex) not in seen else None path=el.get_path().transform(el.composed_transform()).to_superpath() bezier.cspsubdiv(path,0.5) for subpath in path: @@ -115,12 +114,15 @@ class ImageMap(inkex.OutputExtension): while i=3: shapes[linkindex].insert(0,shapemarkup('poly',coords,href,alt)) + if rectifiable(coords): shapes[linkindex].insert(0,{'shape':'rect','coords':rectify(coords),'href':href}) + elif len(coords)>=3: shapes[linkindex].insert(0,{'shape':'poly','coords':coords,'href':href}) href=None # because subsequent subpaths must be enclaves - alt=None # TODO make it come first even though the order's reversed - seen.add(linkindex) - stream.write(bytes(''.join(sum(shapes,start=[])),'utf-8')) + for i in range(len(shapes)): + alt=links[i]['alt'] + if len(shapes[i])==0: inkex.errormsg(_("The hyperlink \"{}\" is not present in the output.").format(links[i]['href'])) + for j in shapes[i]: + stream.write(bytes(shapemarkup(j['shape'],j['coords'],j['href'],alt),'utf-8')) + alt=None if __name__ == "__main__": ImageMap().run() -- cgit