diff options
author | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2017-04-11 20:31:18 +0300 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2017-04-14 12:33:13 -0700 |
commit | 98e2e59e5e963dbc5451a19233d942b429a74855 (patch) | |
tree | 2eb68e34823d07a7402e0c633e4c3e9510b76b4c /src/mbgl/text | |
parent | 2f4d162debd7e4accfc0b20360058304dce40801 (diff) | |
download | qtlocation-mapboxgl-98e2e59e5e963dbc5451a19233d942b429a74855.tar.gz |
[core] Safeguard PositionedIcon usage via optional
Diffstat (limited to 'src/mbgl/text')
-rw-r--r-- | src/mbgl/text/collision_feature.cpp | 4 | ||||
-rw-r--r-- | src/mbgl/text/collision_feature.hpp | 22 | ||||
-rw-r--r-- | src/mbgl/text/quads.cpp | 8 | ||||
-rw-r--r-- | src/mbgl/text/shaping.cpp | 8 | ||||
-rw-r--r-- | src/mbgl/text/shaping.hpp | 43 |
5 files changed, 52 insertions, 33 deletions
diff --git a/src/mbgl/text/collision_feature.cpp b/src/mbgl/text/collision_feature.cpp index 67f669ab61..885ba5c426 100644 --- a/src/mbgl/text/collision_feature.cpp +++ b/src/mbgl/text/collision_feature.cpp @@ -13,7 +13,7 @@ CollisionFeature::CollisionFeature(const GeometryCoordinates& line, const float padding, const style::SymbolPlacementType placement, IndexedSubfeature indexedFeature_, - const bool straight) + const AlignmentType alignment) : indexedFeature(std::move(indexedFeature_)) { if (top == 0 && bottom == 0 && left == 0 && right == 0) return; @@ -32,7 +32,7 @@ CollisionFeature::CollisionFeature(const GeometryCoordinates& line, GeometryCoordinate anchorPoint = convertPoint<int16_t>(anchor.point); - if (straight) { + if (alignment == AlignmentType::Straight) { // used for icon labels that are aligned with the line, but don't curve along it const GeometryCoordinate vector = convertPoint<int16_t>(util::unit(convertPoint<double>(line[anchor.segment + 1] - line[anchor.segment])) * length); const GeometryCoordinates newLine({ anchorPoint - vector, anchorPoint + vector }); diff --git a/src/mbgl/text/collision_feature.hpp b/src/mbgl/text/collision_feature.hpp index 1436e0815b..006a47eb74 100644 --- a/src/mbgl/text/collision_feature.hpp +++ b/src/mbgl/text/collision_feature.hpp @@ -33,6 +33,11 @@ public: class CollisionFeature { public: + enum class AlignmentType : bool { + Straight = 0, + Curved + }; + // for text CollisionFeature(const GeometryCoordinates& line, const Anchor& anchor, @@ -41,29 +46,34 @@ public: const float padding, const style::SymbolPlacementType placement, const IndexedSubfeature& indexedFeature_) - : CollisionFeature(line, anchor, shapedText.top, shapedText.bottom, shapedText.left, shapedText.right, boxScale, padding, placement, indexedFeature_, false) {} + : CollisionFeature(line, anchor, shapedText.top, shapedText.bottom, shapedText.left, shapedText.right, boxScale, padding, placement, indexedFeature_, AlignmentType::Curved) {} // for icons CollisionFeature(const GeometryCoordinates& line, const Anchor& anchor, - const PositionedIcon& shapedIcon, + optional<PositionedIcon> shapedIcon, const float boxScale, const float padding, const style::SymbolPlacementType placement, const IndexedSubfeature& indexedFeature_) - : CollisionFeature(line, anchor, shapedIcon.top, shapedIcon.bottom, shapedIcon.left, shapedIcon.right, boxScale, padding, placement, indexedFeature_, true) {} + : CollisionFeature(line, anchor, + (shapedIcon ? shapedIcon->top() : 0), + (shapedIcon ? shapedIcon->bottom() : 0), + (shapedIcon ? shapedIcon->left() : 0), + (shapedIcon ? shapedIcon->right() : 0), + boxScale, padding, placement, indexedFeature_, AlignmentType::Straight) {} CollisionFeature(const GeometryCoordinates& line, - const Anchor& anchor, + const Anchor&, const float top, const float bottom, const float left, const float right, const float boxScale, const float padding, - const style::SymbolPlacementType placement, + const style::SymbolPlacementType, IndexedSubfeature, - const bool straight); + const AlignmentType); std::vector<CollisionBox> boxes; IndexedSubfeature indexedFeature; diff --git a/src/mbgl/text/quads.cpp b/src/mbgl/text/quads.cpp index 188f88655d..bf7791581a 100644 --- a/src/mbgl/text/quads.cpp +++ b/src/mbgl/text/quads.cpp @@ -22,12 +22,12 @@ SymbolQuad getIconQuad(const Anchor& anchor, const float layoutTextSize, const style::SymbolPlacementType placement, const Shaping& shapedText) { - auto image = *(shapedIcon.image); + auto image = *shapedIcon.image(); const float border = 1.0; - auto left = shapedIcon.left - border; + auto left = shapedIcon.left() - border; auto right = left + image.pos.w / image.relativePixelRatio; - auto top = shapedIcon.top - border; + auto top = shapedIcon.top() - border; auto bottom = top + image.pos.h / image.relativePixelRatio; Point<float> tl; Point<float> tr; @@ -67,7 +67,7 @@ SymbolQuad getIconQuad(const Anchor& anchor, bl = {left, bottom}; } - float angle = shapedIcon.angle; + float angle = shapedIcon.angle(); if (placement == style::SymbolPlacementType::Line) { assert(static_cast<unsigned int>(anchor.segment) < line.size()); const GeometryCoordinate &prev= line[anchor.segment]; diff --git a/src/mbgl/text/shaping.cpp b/src/mbgl/text/shaping.cpp index aa760ea4fe..5fae03b4c7 100644 --- a/src/mbgl/text/shaping.cpp +++ b/src/mbgl/text/shaping.cpp @@ -10,7 +10,11 @@ namespace mbgl { -PositionedIcon shapeIcon(const SpriteAtlasElement& image, const std::array<float, 2>& iconOffset, const float iconRotation) { +optional<PositionedIcon> PositionedIcon::shapeIcon(const SpriteAtlasElement& image, const std::array<float, 2>& iconOffset, const float iconRotation) { + if (!image.pos.hasArea()) { + return {}; + } + float dx = iconOffset[0]; float dy = iconOffset[1]; float x1 = dx - image.width/ 2.0f; @@ -18,7 +22,7 @@ PositionedIcon shapeIcon(const SpriteAtlasElement& image, const std::array<float float y1 = dy - image.height / 2.0f; float y2 = y1 + image.height; - return PositionedIcon(image, y1, y2, x1, x2, iconRotation); + return { PositionedIcon { image, y1, y2, x1, x2, iconRotation } }; } void align(Shaping& shaping, diff --git a/src/mbgl/text/shaping.hpp b/src/mbgl/text/shaping.hpp index fc404e3a97..5e932e6df7 100644 --- a/src/mbgl/text/shaping.hpp +++ b/src/mbgl/text/shaping.hpp @@ -12,33 +12,38 @@ class SymbolFeature; class BiDi; class PositionedIcon { -public: - PositionedIcon() = default; +private: PositionedIcon(const SpriteAtlasElement& image_, float top_, float bottom_, float left_, float right_, float angle_) - : image(image_), - top(top_), - bottom(bottom_), - left(left_), - right(right_), - angle(angle_) {} - - optional<SpriteAtlasElement> image; - float top = 0; - float bottom = 0; - float left = 0; - float right = 0; - float angle = 0; - - explicit operator bool() const { return image && (*image).pos.hasArea(); } + : _image(image_), + _top(top_), + _bottom(bottom_), + _left(left_), + _right(right_), + _angle(angle_) {} + + optional<SpriteAtlasElement> _image; + float _top; + float _bottom; + float _left; + float _right; + float _angle; + +public: + static optional<PositionedIcon> shapeIcon(const class SpriteAtlasElement&, const std::array<float, 2>& iconOffset, const float iconRotation); + + optional<class SpriteAtlasElement> image() const { return _image; } + float top() const { return _top; } + float bottom() const { return _bottom; } + float left() const { return _left; } + float right() const { return _right; } + float angle() const { return _angle; } }; -PositionedIcon shapeIcon(const SpriteAtlasElement&, const std::array<float, 2>& iconOffset, const float iconRotation); - const Shaping getShaping(const std::u16string& string, float maxWidth, float lineHeight, |