1begin scalebar scale, units, x, y
2
3	local worldBarLength
4	local power10, lastPower10
5	local digit
6	local mmBarLength, mmScaleSize
7	local unitDivider, unitLabel
8	local toggle, counter, x2
9
10	# 10mm long bars in a scale look good.  What distance does this
11	# represent in world coordinates?
12	#
13	let worldBarLength = (10 / 1000) * scale
14
15	# Find the largest power of ten that is smaller than this distance.
16	#
17	let power10=1
18	let lastPower10=1
19	while power10 < worldBarLength
20	do
21		let lastPower10 = power10
22		let power10 = power10 * 10
23	done
24
25	# Adjust this world coordinate bar length so it is a nice round
26	# multiple of some number.  For example, bars of length 233142
27	# are changed to 200000, 81123 is changed to 100000.
28	#
29	let digit=substr(worldBarLength, 1, 1)
30	if digit eq '1'
31	then
32		let worldBarLength = lastPower10
33	elif digit eq '2'
34	then
35		let worldBarLength = lastPower10 * 2
36	elif digit lt '8'
37	then
38		let worldBarLength = lastPower10 * 5
39	else
40		let worldBarLength = lastPower10 * 10
41	endif
42
43	let mmBarLength = (worldBarLength * 1000) / scale
44
45	# Overall length of scale bar on page in mm.
46	#
47	let mmScaleSize = 45
48
49	# Blank out background behind scale bar.
50	#
51	clearpath
52	color "white"
53	box x, y, x + mmScaleSize, y + 10
54	fill
55	clearpath
56
57	color "black"
58	linestyle 0.1
59	font 'Helvetica', 2.5
60	justify 'centre'
61
62	# If measurements are in thousands, or larger units then we can
63	# label bars with values divided by 1000 and show units as
64	# 'thousands of ...'.  Less zeros in labels makes scale easier to read.
65	#
66	if worldBarLength >= 1000
67	then
68		if worldBarLength >= 1000000
69		then
70			let unitDivider = 1000000
71			if units eq 'meters' or units eq 'metres' or units eq 'm'
72			then
73				let unitLabel = "thousands of kilometers"
74			else
75				let unitLabel = "millions of " . units
76			endif
77		else
78			let unitDivider = 1000
79			if units eq 'meters' or units eq 'metres' or units eq 'm'
80			then
81				let unitLabel = "kilometers"
82			else
83				let unitLabel = "thousands of " . units
84			endif
85		endif
86	else
87		let unitDivider = 1
88		let unitLabel = units
89	endif
90
91	let toggle = "off"
92	let counter = 0
93	let x2 = x + 2
94	while x2 + mmBarLength < x + mmScaleSize - 5
95	do
96		clearpath
97		move x2, y + 4
98
99		label (worldBarLength / unitDivider) * counter
100		let counter = counter + 1
101
102		# Display bars alternately as black and empty rectangles.
103		#
104		clearpath
105		box x2, y + 7, x2 + mmBarLength, y + 9
106		if toggle eq "off"
107		then
108			stroke
109			let toggle = "on"
110		else
111			fill
112			stroke
113			let toggle = "off"
114		endif
115		let x2 = x2 + mmBarLength
116
117		if x2 + mmBarLength >= x + mmScaleSize - 5
118		then
119			clearpath
120			move x2, y + 4
121			label (worldBarLength / unitDivider) * counter
122		endif
123	done
124
125	# Show units under scalebar.
126	#
127	clearpath
128	move x + mmScaleSize / 2, y + 1
129	label unitLabel
130end
131