diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2019-12-09 15:09:42 +0100 |
---|---|---|
committer | Alexander Shalamov <alexander.shalamov@mapbox.com> | 2020-01-15 15:02:11 +0200 |
commit | 62b0f4cde289e5918c41d5b69b0a03baa6821862 (patch) | |
tree | 2042319da9abd756564dcbba7812d4868a177800 /src | |
parent | 4b171cccf1c4012f8962b022f86c4ac8d73f09df (diff) | |
download | qtlocation-mapboxgl-62b0f4cde289e5918c41d5b69b0a03baa6821862.tar.gz |
[core] Add stretches and content to style::Image
Diffstat (limited to 'src')
-rw-r--r-- | src/mbgl/sprite/sprite_parser.cpp | 2 | ||||
-rw-r--r-- | src/mbgl/style/image.cpp | 23 | ||||
-rw-r--r-- | src/mbgl/style/image_impl.cpp | 57 | ||||
-rw-r--r-- | src/mbgl/style/image_impl.hpp | 15 |
4 files changed, 83 insertions, 14 deletions
diff --git a/src/mbgl/sprite/sprite_parser.cpp b/src/mbgl/sprite/sprite_parser.cpp index 99e2b0c8ca..f141037bc7 100644 --- a/src/mbgl/sprite/sprite_parser.cpp +++ b/src/mbgl/sprite/sprite_parser.cpp @@ -25,7 +25,7 @@ std::unique_ptr<style::Image> createStyleImage(const std::string& id, ratio <= 0 || ratio > 10 || srcX >= image.size.width || srcY >= image.size.height || srcX + width > image.size.width || srcY + height > image.size.height) { - Log::Error(Event::Sprite, "Can't create sprite with invalid metrics: %ux%u@%u,%u in %ux%u@%sx sprite", + Log::Error(Event::Sprite, "Can't create image with invalid metrics: %ux%u@%u,%u in %ux%u@%sx sprite", width, height, srcX, srcY, image.size.width, image.size.height, util::toString(ratio).c_str()); diff --git a/src/mbgl/style/image.cpp b/src/mbgl/style/image.cpp index 1747de5fcc..269f5f9d1a 100644 --- a/src/mbgl/style/image.cpp +++ b/src/mbgl/style/image.cpp @@ -6,11 +6,14 @@ namespace mbgl { namespace style { Image::Image(std::string id, - PremultipliedImage &&image, + PremultipliedImage&& image, const float pixelRatio, - bool sdf) - : baseImpl(makeMutable<Impl>(std::move(id), std::move(image), pixelRatio, sdf)) { -} + bool sdf, + ImageStretches stretchX, + ImageStretches stretchY, + optional<ImageContent> content) + : baseImpl(makeMutable<Impl>( + std::move(id), std::move(image), pixelRatio, sdf, std::move(stretchX), std::move(stretchY), content)) {} std::string Image::getID() const { return baseImpl->id; @@ -30,5 +33,17 @@ float Image::getPixelRatio() const { return baseImpl->pixelRatio; } +const ImageStretches& Image::getStretchX() const { + return baseImpl->stretchX; +} + +const ImageStretches& Image::getStretchY() const { + return baseImpl->stretchY; +} + +const ImageContent& Image::getContent() const { + return baseImpl->content; +} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/image_impl.cpp b/src/mbgl/style/image_impl.cpp index ce327262e8..c43bb552ee 100644 --- a/src/mbgl/style/image_impl.cpp +++ b/src/mbgl/style/image_impl.cpp @@ -4,19 +4,60 @@ namespace mbgl { namespace style { +namespace { + +bool validateStretch(const ImageStretches& stretches, const float size) { + if (stretches.empty()) { + return true; + } + float last = 0; + for (auto& part : stretches) { + if (part.first < last || part.second < part.first || size < part.second) { + return false; + } + last = part.second; + } + return true; +} + +bool validateContent(const ImageContent& content, const Size& size) { + if (content.left < 0 || size.width < content.left) return false; + if (content.top < 0 || size.height < content.top) return false; + if (content.right < 0 || size.width < content.right) return false; + if (content.bottom < 0 || size.height < content.bottom) return false; + if (content.right < content.left) return false; + if (content.bottom < content.top) return false; + return true; +} + +} // namespace + Image::Impl::Impl(std::string id_, PremultipliedImage&& image_, const float pixelRatio_, - bool sdf_) - : id(std::move(id_)), - image(std::move(image_)), - pixelRatio(pixelRatio_), - sdf(sdf_) { - + bool sdf_, + ImageStretches stretchX_, + ImageStretches stretchY_, + optional<ImageContent> content_) + : id(std::move(id_)), + image(std::move(image_)), + pixelRatio(pixelRatio_), + sdf(sdf_), + stretchX(std::move(stretchX_)), + stretchY(std::move(stretchY_)), + content(content_ + ? std::move(*content_) + : ImageContent{0, 0, static_cast<float>(image.size.width), static_cast<float>(image.size.height)}) { if (!image.valid()) { - throw util::SpriteImageException("Sprite image dimensions may not be zero"); + throw util::StyleImageException("dimensions may not be zero"); } else if (pixelRatio <= 0) { - throw util::SpriteImageException("Sprite pixelRatio may not be <= 0"); + throw util::StyleImageException("pixelRatio may not be <= 0"); + } else if (!validateStretch(stretchX, image.size.width)) { + throw util::StyleImageException("stretchX is out of bounds or overlapping"); + } else if (!validateStretch(stretchY, image.size.height)) { + throw util::StyleImageException("stretchY is out of bounds or overlapping"); + } else if (!validateContent(content, image.size)) { + throw util::StyleImageException("content area is invalid"); } } diff --git a/src/mbgl/style/image_impl.hpp b/src/mbgl/style/image_impl.hpp index b2decbf781..683cd3347b 100644 --- a/src/mbgl/style/image_impl.hpp +++ b/src/mbgl/style/image_impl.hpp @@ -11,7 +11,13 @@ namespace style { class Image::Impl { public: - Impl(std::string id, PremultipliedImage&&, float pixelRatio, bool sdf = false); + Impl(std::string id, + PremultipliedImage&&, + float pixelRatio, + bool sdf = false, + ImageStretches stretchX = {}, + ImageStretches stretchY = {}, + optional<ImageContent> content = nullopt); const std::string id; @@ -22,6 +28,13 @@ public: // Whether this image should be interpreted as a signed distance field icon. const bool sdf; + + // Stretch areas of this image. + const ImageStretches stretchX; + const ImageStretches stretchY; + + // The space where text can be fit into this image. + const ImageContent content; }; } // namespace style |