From d9244cc973cacb454960838287c8c47482b20e34 Mon Sep 17 00:00:00 2001 From: Ansis Brammanis Date: Fri, 5 Feb 2016 16:26:08 -0800 Subject: [PATCH] [core] make sure icons fit before the ends of the line This skips anchors if there is not enough room before the beginning or end of the line for the icon to fit. -js: https://github.com/mapbox/mapbox-gl-js/pull/2077/files --- package.json | 2 +- src/mbgl/text/get_anchors.cpp | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index d97d3f56e..3c4978c06 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ ], "devDependencies": { "aws-sdk": "^2.2.21", - "mapbox-gl-test-suite": "mapbox/mapbox-gl-test-suite#2b4ed672dc7e7e60f98115e3eb2dc55125b96e66", + "mapbox-gl-test-suite": "mapbox/mapbox-gl-test-suite#47f62a86a568bbf67dd5b2f4b42b46ef4769f356", "node-gyp": "^3.2.1", "request": "^2.67.0", "tape": "^4.2.2" diff --git a/src/mbgl/text/get_anchors.cpp b/src/mbgl/text/get_anchors.cpp index 1a2142bfb..7a129d692 100644 --- a/src/mbgl/text/get_anchors.cpp +++ b/src/mbgl/text/get_anchors.cpp @@ -10,14 +10,19 @@ namespace mbgl { Anchors resample(const std::vector &line, const float offset, const float spacing, const float angleWindowSize, const float maxAngle, const float labelLength, const bool continuedLine, const bool placeAtMiddle) { + const float halfLabelLength = labelLength / 2.0f; + float lineLength = 0; + for (auto it = line.begin(), end = line.end() - 1; it != end; it++) { + lineLength += util::dist(*(it), *(it + 1)); + } + float distance = 0; float markedDistance = offset - spacing; Anchors anchors; - auto end = line.end() - 1; int i = 0; - for (auto it = line.begin(); it != end; it++, i++) { + for (auto it = line.begin(), end = line.end() - 1; it != end; it++, i++) { const Coordinate &a = *(it); const Coordinate &b = *(it + 1); @@ -31,7 +36,12 @@ Anchors resample(const std::vector &line, const float offset, const x = util::interpolate(float(a.x), float(b.x), t), y = util::interpolate(float(a.y), float(b.y), t); - if (x >= 0 && x < util::EXTENT && y >= 0 && y < util::EXTENT) { + // Check that the point is within the tile boundaries and that + // the label would fit before the beginning and end of the line + // if placed at this point. + if (x >= 0 && x < util::EXTENT && y >= 0 && y < util::EXTENT && + markedDistance - halfLabelLength >= 0.0f && + markedDistance + halfLabelLength <= lineLength) { Anchor anchor(::round(x), ::round(y), angle, 0.5f, i); if (!angleWindowSize || checkMaxAngle(line, anchor, labelLength, angleWindowSize, maxAngle)) {