forked from plepe/Leaflet.TextPath
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleaflet.textpath.js
More file actions
executable file
·330 lines (276 loc) · 10.8 KB
/
leaflet.textpath.js
File metadata and controls
executable file
·330 lines (276 loc) · 10.8 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
* Leaflet.TextPath - Shows text along a polyline
* Inspired by Tom Mac Wright article :
* http://mapbox.com/osmdev/2012/11/20/getting-serious-about-svg/
*/
(function () {
var __onAdd = L.Polyline.prototype.onAdd,
__onRemove = L.Polyline.prototype.onRemove,
__updatePath = L.Polyline.prototype._updatePath,
__bringToFront = L.Polyline.prototype.bringToFront;
var _getLengthCache = {}
var PolylineTextPath = {
onAdd: function (map) {
__onAdd.call(this, map);
this._textRedraw();
},
onRemove: function (map) {
map = map || this._map;
if (map && this._textNode && map._renderer._container)
map._renderer._container.removeChild(this._textNode);
__onRemove.call(this, map);
},
bringToFront: function () {
__bringToFront.call(this);
this._textRedraw();
},
_updatePath: function () {
__updatePath.call(this);
this._textRedraw();
},
_textRedraw: function () {
var text = this._text,
options = this._textOptions;
if (text) {
this.setText(null).setText(text, options);
}
},
_getLength: function (text, options) {
var cacheId = JSON.stringify(text) + '|' + JSON.stringify(options.attributes)
if (cacheId in _getLengthCache) {
return _getLengthCache[cacheId]
}
var svg = this._map._renderer._container;
var pattern = L.SVG.create('text');
for (var attr in options.attributes)
pattern.setAttribute(attr, options.attributes[attr]);
this._applyText(pattern, text);
svg.appendChild(pattern);
var length = pattern.getComputedTextLength();
svg.removeChild(pattern);
_getLengthCache[cacheId] = length
return length;
},
setText: function (text, options) {
this._text = text;
this._textOptions = options;
/* If not in SVG mode or Polyline not added to map yet return */
/* setText will be called by onAdd, using value stored in this._text */
if (!L.Browser.svg || typeof this._map === 'undefined') {
return this;
}
var defaults = {
repeat: false,
fillColor: 'black',
attributes: {},
below: false,
allowCrop: true
};
options = L.Util.extend(defaults, options);
/* If empty text, hide */
if (!text) {
if (this._textNode && this._textNode.parentNode) {
this._map._renderer._container.removeChild(this._textNode);
/* delete the node, so it will not be removed a 2nd time if the layer is later removed from the map */
delete this._textNode;
}
return this;
}
var id = 'pathdef-' + L.Util.stamp(this);
var svg = this._map._renderer._container;
this._path.setAttribute('id', id);
var textLength = null;
var pathLength = null;
var finalText = [];
var dx = 0
if (!options.allowCrop) {
if (textLength === null) {
textLength = this._getLength(text, options);
}
if (pathLength === null) {
pathLength = this._path.getTotalLength();
}
if (textLength > pathLength) {
return this;
}
}
if (typeof options.repeat === 'undefined' || options.repeat === false) {
/* Center text according to the path's bounding box */
if (options.center) {
if (textLength === null) {
textLength = this._getLength(text, options);
}
/* Set the position for the left side of the textNode */
dx = Math.max(0, (pathLength / 2) - (textLength / 2));
}
if (options.orientation === 'auto') {
var poiBegin = this._path.getPointAtLength(dx)
var poiEnd = this._path.getPointAtLength(dx + textLength)
var leftToRight = poiEnd.x >= poiBegin.x
if (leftToRight) {
finalText.push(text);
} else {
finalText.push({ text: turnText(text), rotate: 180 });
}
} else if (options.orientation === 'flip') {
finalText.push({ text: turnText(text), rotate: 180 });
} else {
finalText = [ text ];
}
} else {
/* Compute single pattern length */
if (textLength === null) {
textLength = this._getLength(text, options);
}
if (options.orientation === 'auto' || options.orientation === 'flip') {
var textTurned = turnText(options.turnedText || text)
var textLengthTurned = this._getLength(options.turnedText || text, options)
}
if (pathLength === null) {
pathLength = this._path.getTotalLength();
}
/* Compute length of a space */
var slength = this._getLength('\u00A0', options);
/* Create string as long as path */
var repeatDistance = parseFloat(options.repeat) || 0
var pos = 0
var spacingBalance = 0
var repeatCount = Math.floor((pathLength + repeatDistance) / (textLength + repeatDistance)) || 1;
var finalText = []
/* Calculate the position for the left side of the textNode */
if (options.center) {
dx = Math.max(0, (pathLength - textLength * repeatCount - repeatDistance * (repeatCount - 1)) / 2);
}
var i = 0;
do {
var spacesCount = 0
if (i > 0) {
spacesCount = Math.round((repeatDistance + spacingBalance) / slength);
spacingBalance = repeatDistance - (spacesCount * slength);
pos += spacesCount * slength
finalText.push('\u00A0'.repeat(spacesCount));
}
if (options.orientation === 'auto') {
var poiBegin = this._path.getPointAtLength(pos)
var poiEnd = this._path.getPointAtLength(pos + textLength)
var leftToRight = poiEnd.x >= poiBegin.x
if (leftToRight) {
finalText.push(text);
pos += textLength
} else {
finalText.push({ text: textTurned, rotate: 180 });
pos += textLengthTurned
}
} else if (options.orientation === 'flip') {
finalText.push({ text: textTurned, rotate: 180 });
pos += textLengthTurned
} else {
finalText.push(text);
pos += textLength
}
i++
} while (pos + repeatDistance + textLength < pathLength);
}
/* Put it along the path using textPath */
var textNode = L.SVG.create('text'),
textPath = L.SVG.create('textPath');
var dy = typeof options.offset === 'undefined' ? this._path.getAttribute('stroke-width') : options.offset;
textPath.setAttributeNS("http://www.w3.org/1999/xlink", "xlink:href", '#'+id);
textNode.setAttribute('dy', dy);
for (var attr in options.attributes)
textNode.setAttribute(attr, options.attributes[attr]);
this._applyText(textPath, finalText);
textNode.appendChild(textPath);
this._textNode = textNode;
if (dx !== 0) {
textNode.setAttribute('dx', dx);
}
if (options.below) {
svg.insertBefore(textNode, svg.firstChild);
}
else {
svg.appendChild(textNode);
}
/* Change label rotation (if required) */
if (options.orientation) {
var rotateAngle = 0;
switch (options.orientation) {
case 'perpendicular':
rotateAngle = 90;
break;
case 'auto':
case 'flip':
rotateAngle = 0;
break;
default:
rotateAngle = options.orientation;
}
var rotatecenterX = (textNode.getBBox().x + textNode.getBBox().width / 2);
var rotatecenterY = (textNode.getBBox().y + textNode.getBBox().height / 2);
textNode.setAttribute('transform','rotate(' + rotateAngle + ' ' + rotatecenterX + ' ' + rotatecenterY + ')');
}
/* Initialize mouse events for the additional nodes */
if (this.options.clickable) {
if (L.Browser.svg || !L.Browser.vml) {
textPath.setAttribute('class', 'leaflet-clickable');
}
L.DomEvent.on(textNode, 'click', this._onMouseClick, this);
var events = ['dblclick', 'mousedown', 'mouseover',
'mouseout', 'mousemove', 'contextmenu'];
for (var i = 0; i < events.length; i++) {
L.DomEvent.on(textNode, events[i], this._fireMouseEvent, this);
}
}
return this;
},
_applyText: function(parentNode, text) {
if (Array.isArray(text)) {
text.forEach(function (part) {
this._applyText(parentNode, part);
}.bind(this));
} else if (typeof text === 'object' && text !== null) {
var tspan = L.SVG.create('tspan');
parentNode.appendChild(tspan);
for (var attr in text) {
if (attr === 'text') {
this._applyText(tspan, text[attr]);
} else {
tspan.setAttribute(attr, text[attr]);
}
}
} else {
text = text.replace(/ /g, '\u00A0'); // Non breakable spaces
parentNode.appendChild(document.createTextNode(text));
}
}
};
L.Polyline.include(PolylineTextPath);
L.LayerGroup.include({
setText: function(text, options) {
for (var layer in this._layers) {
if (typeof this._layers[layer].setText === 'function') {
this._layers[layer].setText(text, options);
}
}
return this;
}
});
function turnText (text) {
if (Array.isArray(text)) {
return text
.slice().reverse()
.map(function (part) {
return turnText(part)
})
} else if (typeof text === 'object' && text !== null) {
var ret = {}
for (var attr in text) {
ret[attr] = text[attr]
}
ret.text = turnText(ret.text)
return ret
} else {
return text.split('').reverse().join('')
}
}
})();