diff options
Diffstat (limited to 'src/mbgl/sprite')
-rw-r--r-- | src/mbgl/sprite/sprite_loader.cpp | 2 | ||||
-rw-r--r-- | src/mbgl/sprite/sprite_loader.hpp | 5 | ||||
-rw-r--r-- | src/mbgl/sprite/sprite_loader_observer.hpp | 7 | ||||
-rw-r--r-- | src/mbgl/sprite/sprite_parser.cpp | 72 | ||||
-rw-r--r-- | src/mbgl/sprite/sprite_parser.hpp | 2 |
5 files changed, 46 insertions, 42 deletions
diff --git a/src/mbgl/sprite/sprite_loader.cpp b/src/mbgl/sprite/sprite_loader.cpp index d4b1cade13..56f7abd50b 100644 --- a/src/mbgl/sprite/sprite_loader.cpp +++ b/src/mbgl/sprite/sprite_loader.cpp @@ -90,7 +90,7 @@ void SpriteLoader::emitSpriteLoadedIfComplete() { loader->worker.self().invoke(&SpriteLoaderWorker::parse, loader->image, loader->json); } -void SpriteLoader::onParsed(std::vector<std::unique_ptr<style::Image>>&& result) { +void SpriteLoader::onParsed(std::vector<Immutable<style::Image::Impl>> result) { observer->onSpriteLoaded(std::move(result)); } diff --git a/src/mbgl/sprite/sprite_loader.hpp b/src/mbgl/sprite/sprite_loader.hpp index 0b7d37fa14..d82c51e2da 100644 --- a/src/mbgl/sprite/sprite_loader.hpp +++ b/src/mbgl/sprite/sprite_loader.hpp @@ -1,6 +1,5 @@ #pragma once -#include <mbgl/util/noncopyable.hpp> #include <mbgl/style/image.hpp> #include <string> @@ -15,7 +14,7 @@ namespace mbgl { class FileSource; class SpriteLoaderObserver; -class SpriteLoader : public util::noncopyable { +class SpriteLoader { public: SpriteLoader(float pixelRatio); ~SpriteLoader(); @@ -29,7 +28,7 @@ private: // Invoked by SpriteAtlasWorker friend class SpriteLoaderWorker; - void onParsed(std::vector<std::unique_ptr<style::Image>>&&); + void onParsed(std::vector<Immutable<style::Image::Impl>>); void onError(std::exception_ptr); const float pixelRatio; diff --git a/src/mbgl/sprite/sprite_loader_observer.hpp b/src/mbgl/sprite/sprite_loader_observer.hpp index c730549c2c..3846d069f0 100644 --- a/src/mbgl/sprite/sprite_loader_observer.hpp +++ b/src/mbgl/sprite/sprite_loader_observer.hpp @@ -1,7 +1,9 @@ #pragma once +#include <mbgl/style/image.hpp> +#include <mbgl/util/immutable.hpp> + #include <exception> -#include <memory> #include <vector> namespace mbgl { @@ -13,8 +15,7 @@ class Image; class SpriteLoaderObserver { public: virtual ~SpriteLoaderObserver() = default; - - virtual void onSpriteLoaded(std::vector<std::unique_ptr<style::Image>>&&) {} + virtual void onSpriteLoaded(std::vector<Immutable<style::Image::Impl>>) {} virtual void onSpriteError(std::exception_ptr) {} }; diff --git a/src/mbgl/sprite/sprite_parser.cpp b/src/mbgl/sprite/sprite_parser.cpp index d39428a848..40e63d2d7d 100644 --- a/src/mbgl/sprite/sprite_parser.cpp +++ b/src/mbgl/sprite/sprite_parser.cpp @@ -149,50 +149,54 @@ optional<style::ImageContent> getContent(const JSValue& value, const char* prope } // namespace -std::vector<std::unique_ptr<style::Image>> parseSprite(const std::string& encodedImage, const std::string& json) { +std::vector<Immutable<style::Image::Impl>> parseSprite(const std::string& encodedImage, const std::string& json) { const PremultipliedImage raster = decodeImage(encodedImage); JSDocument doc; doc.Parse<0>(json.c_str()); if (doc.HasParseError()) { throw std::runtime_error("Failed to parse JSON: " + formatJSONParseError(doc)); - } else if (!doc.IsObject()) { + } + + if (!doc.IsObject()) { throw std::runtime_error("Sprite JSON root must be an object"); - } else { - std::vector<std::unique_ptr<style::Image>> images; - for (const auto& property : doc.GetObject()) { - const std::string name = { property.name.GetString(), property.name.GetStringLength() }; - const JSValue& value = property.value; - - if (value.IsObject()) { - const uint16_t x = getUInt16(value, "x", name.c_str(), 0); - const uint16_t y = getUInt16(value, "y", name.c_str(), 0); - const uint16_t width = getUInt16(value, "width", name.c_str(), 0); - const uint16_t height = getUInt16(value, "height", name.c_str(), 0); - const double pixelRatio = getDouble(value, "pixelRatio", name.c_str(), 1); - const bool sdf = getBoolean(value, "sdf", name.c_str(), false); - style::ImageStretches stretchX = getStretches(value, "stretchX", name.c_str()); - style::ImageStretches stretchY = getStretches(value, "stretchY", name.c_str()); - optional<style::ImageContent> content = getContent(value, "content", name.c_str()); - - auto image = createStyleImage(name, - raster, - x, - y, - width, - height, - pixelRatio, - sdf, - std::move(stretchX), - std::move(stretchY), - std::move(content)); - if (image) { - images.push_back(std::move(image)); - } + } + + const auto& properties = doc.GetObject(); + std::vector<Immutable<style::Image::Impl>> images; + images.reserve(properties.MemberCount()); + for (const auto& property : properties) { + const std::string name = {property.name.GetString(), property.name.GetStringLength()}; + const JSValue& value = property.value; + + if (value.IsObject()) { + const uint16_t x = getUInt16(value, "x", name.c_str(), 0); + const uint16_t y = getUInt16(value, "y", name.c_str(), 0); + const uint16_t width = getUInt16(value, "width", name.c_str(), 0); + const uint16_t height = getUInt16(value, "height", name.c_str(), 0); + const double pixelRatio = getDouble(value, "pixelRatio", name.c_str(), 1); + const bool sdf = getBoolean(value, "sdf", name.c_str(), false); + style::ImageStretches stretchX = getStretches(value, "stretchX", name.c_str()); + style::ImageStretches stretchY = getStretches(value, "stretchY", name.c_str()); + optional<style::ImageContent> content = getContent(value, "content", name.c_str()); + + auto image = createStyleImage(name, + raster, + x, + y, + width, + height, + pixelRatio, + sdf, + std::move(stretchX), + std::move(stretchY), + std::move(content)); + if (image) { + images.push_back(std::move(image->baseImpl)); } } - return images; } + return images; } } // namespace mbgl diff --git a/src/mbgl/sprite/sprite_parser.hpp b/src/mbgl/sprite/sprite_parser.hpp index 9656bcbf2a..f2e7358ac2 100644 --- a/src/mbgl/sprite/sprite_parser.hpp +++ b/src/mbgl/sprite/sprite_parser.hpp @@ -20,6 +20,6 @@ std::unique_ptr<style::Image> createStyleImage(const std::string& id, optional<style::ImageContent> content = nullopt); // Parses an image and an associated JSON file and returns the sprite objects. -std::vector<std::unique_ptr<style::Image>> parseSprite(const std::string& image, const std::string& json); +std::vector<Immutable<style::Image::Impl>> parseSprite(const std::string& image, const std::string& json); } // namespace mbgl |