diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2017-05-18 09:37:21 -0700 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2017-05-23 12:59:27 -0700 |
commit | 1759603e128ad0a08f4a25009b82695420ec2840 (patch) | |
tree | 77d74c683ca34efc25bd79cbb25774049dbfc695 /src/mbgl/text | |
parent | 9e1cbbeacb4007f9c7919185df0aae90fc8f7ad1 (diff) | |
download | qtlocation-mapboxgl-1759603e128ad0a08f4a25009b82695420ec2840.tar.gz |
[core] Simplify and fix sprite atlas coordinate calculations
* Always return image metrics exclusive of padding
* Work with integer coordinates whenever possible
* Eliminate redundant SpriteAtlasElement members
* Fix asymmetric re-padding in getIconQuad when pixelRatio != 1
* Add explanatory comments
Diffstat (limited to 'src/mbgl/text')
-rw-r--r-- | src/mbgl/text/quads.cpp | 22 | ||||
-rw-r--r-- | src/mbgl/text/shaping.cpp | 8 |
2 files changed, 21 insertions, 9 deletions
diff --git a/src/mbgl/text/quads.cpp b/src/mbgl/text/quads.cpp index dc48634fb0..33b94d7114 100644 --- a/src/mbgl/text/quads.cpp +++ b/src/mbgl/text/quads.cpp @@ -24,11 +24,15 @@ SymbolQuad getIconQuad(const Anchor& anchor, const Shaping& shapedText) { const SpriteAtlasElement& image = shapedIcon.image(); + // If you have a 10px icon that isn't perfectly aligned to the pixel grid it will cover 11 actual + // pixels. The quad needs to be padded to account for this, otherwise they'll look slightly clipped + // on one edge in some cases. const float border = 1.0; - auto left = shapedIcon.left() - border; - auto right = left + image.pos.w / image.relativePixelRatio; - auto top = shapedIcon.top() - border; - auto bottom = top + image.pos.h / image.relativePixelRatio; + + float top = shapedIcon.top() - border / image.pixelRatio; + float left = shapedIcon.left() - border / image.pixelRatio; + float bottom = shapedIcon.bottom() + border / image.pixelRatio; + float right = shapedIcon.right() + border / image.pixelRatio; Point<float> tl; Point<float> tr; Point<float> br; @@ -92,7 +96,15 @@ SymbolQuad getIconQuad(const Anchor& anchor, br = util::matrixMultiply(matrix, br); } - return SymbolQuad { tl, tr, bl, br, image.pos, 0, 0, anchor.point, globalMinScale, std::numeric_limits<float>::infinity(), shapedText.writingMode }; + // Icon quad is padded, so texture coordinates also need to be padded. + Rect<uint16_t> textureRect { + static_cast<uint16_t>(image.textureRect.x - border), + static_cast<uint16_t>(image.textureRect.y - border), + static_cast<uint16_t>(image.textureRect.w + border * 2), + static_cast<uint16_t>(image.textureRect.h + border * 2) + }; + + return SymbolQuad { tl, tr, bl, br, textureRect, 0, 0, anchor.point, globalMinScale, std::numeric_limits<float>::infinity(), shapedText.writingMode }; } struct GlyphInstance { diff --git a/src/mbgl/text/shaping.cpp b/src/mbgl/text/shaping.cpp index 6ee646be96..f9f90627d4 100644 --- a/src/mbgl/text/shaping.cpp +++ b/src/mbgl/text/shaping.cpp @@ -13,10 +13,10 @@ namespace mbgl { PositionedIcon PositionedIcon::shapeIcon(const SpriteAtlasElement& image, const std::array<float, 2>& iconOffset, const float iconRotation) { float dx = iconOffset[0]; float dy = iconOffset[1]; - float x1 = dx - image.size[0] / 2.0f; - float x2 = x1 + image.size[0]; - float y1 = dy - image.size[1] / 2.0f; - float y2 = y1 + image.size[1]; + float x1 = dx - image.displaySize()[0] / 2.0f; + float x2 = x1 + image.displaySize()[0]; + float y1 = dy - image.displaySize()[1] / 2.0f; + float y2 = y1 + image.displaySize()[1]; return PositionedIcon { image, y1, y2, x1, x2, iconRotation }; } |