File: imgmap.py

package info (click to toggle)
dia 0.98%2Bgit20250126-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 52,072 kB
  • sloc: ansic: 155,381; xml: 14,056; python: 6,250; cpp: 3,598; sh: 439; perl: 137; makefile: 25
file content (80 lines) | stat: -rw-r--r-- 2,807 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#    PyDia imgmap.py : produce an html image map
#    Copyright (c) 2007  Hans Breuer  <hans@breuer.org>

#    This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

import dia, sys, os.path

import gettext
_ = gettext.gettext

class ObjRenderer :
	def __init__ (self) :
		self.f = None
		# the bintmap exporters calculate their ow margins from the bunding box
		self.xofs = 0
		self.yofs = 0
	def begin_render (self, data, filename) :
		self.f = open(filename, "w")
		bMapLayer = 0 # if there is a special layer containing the links
		for layer in data.layers :
			if layer.name == "map" :
				bMapLayer = 1
				break
		scale = 20.0 * data.paper.scaling
		r = data.extents
		width = int((r.right - r.left) * scale)
		height = int((r.bottom - r.top) * scale)
		name = os.path.split (filename)[1]
		fname = name[:name.find(".")] + ".png" # guessing
		self.f.write ('<image src="%s" width="%d", height="%d" usemap="#%s">\n' % (fname, width, height, name))
		self.f.write ('<map name="%s">\n' % (name,))

		self.xofs = - (r.left * scale)
		self.yofs = - (r.top * scale)
		if bMapLayer :
			self.WriteAreas (layer, scale)
		else :
			for layer in data.layers :
				self.WriteAreas (layer, scale)
		self.f.write ('</map>\n')

	def WriteAreas (self, layer, scale) :
		for o in layer.objects :
			r = o.bounding_box
			if "name" in o.properties :
				url = o.properties["name"].value
			elif "text" in o.properties :
				url = o.properties["text"].value.text
			else :
				continue
			if len(url) == 0 or url.find (" ") >= 0 :
				continue

			alt = url
			if "comment" in o.properties :
				alt = o.properties["comment"].value
			# need to sale the original coords to the bitmap size
			x1 = int(r.left * scale) + self.xofs
			y1 = int(r.top * scale) + self.yofs
			x2 = int(r.right * scale) + self.xofs
			y2 = int(r.bottom * scale) + self.yofs
			self.f.write ('  <area shape="rect" href="%s" title="%s" alt="%s" coords="%d,%d,%d,%d">\n' % \
				 ("#" + url, url, alt, x1, y1, x2, y2))
	def end_render (self) :
		self.f.close()

# dia-python keeps a reference to the renderer class and uses it on demand
dia.register_export (_("Imagemap"), "cmap", ObjRenderer())