From 4585c9cafa87cad6b54397af7e9375cc31b72f89 Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Tue, 27 Jun 2023 03:38:57 +1000 Subject: Lazy-loading of ImageOperation inside Directives also fixed cases of drawables not staying centered after adding directives that scale --- source/core/StarDirectives.cpp | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) (limited to 'source/core/StarDirectives.cpp') diff --git a/source/core/StarDirectives.cpp b/source/core/StarDirectives.cpp index cac1eaf..a18951e 100644 --- a/source/core/StarDirectives.cpp +++ b/source/core/StarDirectives.cpp @@ -24,6 +24,14 @@ Directives::Entry::Entry(Entry const& other) { length = other.length; } +ImageOperation const& Directives::Entry::loadOperation(Shared const& parent) const { + if (operation.is()) { + try { operation = imageOperationFromString(string(parent)); } + catch (StarException const& e) { operation = ErrorImageOperation{ std::current_exception() }; } + } + return operation; +} + StringView Directives::Entry::string(Shared const& parent) const { StringView result = parent.string; result = result.utf8().substr(begin, length); @@ -54,6 +62,15 @@ Directives::Directives(const char* directives) { parse(directives); } +void Directives::loadOperations() const { + if (!shared) + return; + + MutexLocker lock(shared->mutex, true); + for (auto& entry : shared->entries) + entry.loadOperation(*shared); +} + void Directives::parse(String&& directives) { if (directives.empty()) return; @@ -63,15 +80,19 @@ void Directives::parse(String&& directives) { StringView prefix = ""; view.forEachSplitView("?", [&](StringView split, size_t beg, size_t end) { if (!split.empty()) { - try { - ImageOperation operation = imageOperationFromString(split); - newList.emplace_back(move(operation), beg, end); - } - catch (StarException const& e) { - if (beg == 0) + if (beg == 0) { + try { + ImageOperation operation = imageOperationFromString(split); + newList.emplace_back(move(operation), beg, end); + } + catch (StarException const& e) { prefix = split; - else - newList.emplace_back(ErrorImageOperation{ std::current_exception() }, beg, end); + return; + } + } + else { + ImageOperation operation = NullImageOperation(); + newList.emplace_back(move(operation), beg, end); } } }); @@ -259,10 +280,11 @@ inline Image DirectivesGroup::applyNewImage(Image const& image) const { void DirectivesGroup::applyExistingImage(Image& image) const { forEach([&](auto const& entry, Directives const& directives) { - if (auto error = entry.operation.ptr()) + ImageOperation const& operation = entry.loadOperation(*directives.shared); + if (auto error = operation.ptr()) std::rethrow_exception(error->exception); else - processImageOperation(entry.operation, image); + processImageOperation(operation, image); }); } -- cgit v1.2.3