From 7494e3d7f27d0dc78f9e381916fc07b6f0b4c9a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20B=C3=B6sch-Plepelits?= Date: Sun, 23 Dec 2018 23:18:45 +0100 Subject: [PATCH] Option 'turnedText': alternative text for east-west lines --- README.md | 1 + index.html | 11 ++++++++--- leaflet.textpath.js | 21 ++++++++++++++------- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 9ed4f5b..7574138 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,7 @@ The `text` parameter of `setText()` can either be: - {orientation: perpendicular} - places text at right angles to the line. - {orientation: auto} - flips the text on (part of) ways running west to east, so that they are readable upside down. * `allowCrop` If the line is too short to display the whole text, crop the text. If false, don't show the text at all. (Default: true). +* `turnedText` When orientation=auto is used, use this text for east -> west lines. * `attributes` Object containing the attributes applied to the `text` tag. Check valid attributes [here](https://developer.mozilla.org/en-US/docs/Web/SVG/Element/text#Attributes) (Default: `{}`) diff --git a/index.html b/index.html index 2361b8a..690d487 100644 --- a/index.html +++ b/index.html @@ -411,13 +411,18 @@ L.geoJson(road, { onEachFeature: function (feature, layer) { layer.setText( - 'Road', + [ 'Road', { text: ' ⯈', fill: 'red' } ], { repeat: 20, center: true, - offset: 4, + offset: 0, orientation: 'auto', - attributes: { 'font-size': '8pt', 'font-weight': 'bold' } + attributes: { + 'font-size': '8pt', + 'font-weight': 'bold', + 'dominant-baseline': 'middle', + }, + turnedText: [ { text: '⯇ ', fill: 'blue' }, 'Road' ], } ); }, diff --git a/leaflet.textpath.js b/leaflet.textpath.js index 3469d2e..ff79d7a 100755 --- a/leaflet.textpath.js +++ b/leaflet.textpath.js @@ -146,14 +146,16 @@ var PolylineTextPath = { finalText = [ text ]; } } else { - if (options.orientation === 'auto' || options.orientation === 'flip') { - var textTurned = turnText(text) - } - /* 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(); } @@ -173,7 +175,8 @@ var PolylineTextPath = { dx = Math.max(0, (pathLength - textLength * repeatCount - repeatDistance * (repeatCount - 1)) / 2); } - for (var i = 0; i < repeatCount; i++) { + var i = 0; + do { var spacesCount = 0 if (i > 0) { spacesCount = Math.round((repeatDistance + spacingBalance) / slength); @@ -189,17 +192,21 @@ var PolylineTextPath = { 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 } - pos += textLength - } + i++ + } while (pos + repeatDistance + textLength < pathLength); } /* Put it along the path using textPath */