aboutsummaryrefslogtreecommitdiff
path: root/imagemap.py
diff options
context:
space:
mode:
authorViatrix2026-03-13 22:19:22 -0700
committerViatrix2026-03-13 22:19:22 -0700
commitc03729d9bb9c0902b89a15643e67b620f7dd669b (patch)
tree2e108dbfd49e8476dba06b167d7d88a5e4af5add /imagemap.py
parenta6d891a0ddbcca22ee6aa946e90f7f8d42e1fec5 (diff)
Alt attribute now set correctly (+ test).
Diffstat (limited to 'imagemap.py')
-rw-r--r--imagemap.py18
1 files changed, 10 insertions, 8 deletions
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<len(coords):
if coords[i]==coords[(i+1)%len(coords)]: coords.pop(i)
else: i+=1
- if rectifiable(coords): shapes[linkindex].insert(0,shapemarkup('rect',rectify(coords),href,alt))
- elif len(coords)>=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()