| 1 | /* |
| 2 | * Copyright (c) 2021 - 2024 the ThorVG project. All rights reserved. |
| 3 | |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | * of this software and associated documentation files (the "Software"), to deal |
| 6 | * in the Software without restriction, including without limitation the rights |
| 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | * copies of the Software, and to permit persons to whom the Software is |
| 9 | * furnished to do so, subject to the following conditions: |
| 10 | |
| 11 | * The above copyright notice and this permission notice shall be included in all |
| 12 | * copies or substantial portions of the Software. |
| 13 | |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | * SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #include "tvgIteratorAccessor.h" |
| 24 | #include "tvgCompressor.h" |
| 25 | |
| 26 | /************************************************************************/ |
| 27 | /* Internal Class Implementation */ |
| 28 | /************************************************************************/ |
| 29 | |
| 30 | static bool accessChildren(Iterator* it, function<bool(const Paint* paint, void* data)> func, void* data) |
| 31 | { |
| 32 | while (auto child = it->next()) { |
| 33 | //Access the child |
| 34 | if (!func(child, data)) return false; |
| 35 | |
| 36 | //Access the children of the child |
| 37 | if (auto it2 = IteratorAccessor::iterator(child)) { |
| 38 | if (!accessChildren(it2, func, data)) { |
| 39 | delete(it2); |
| 40 | return false; |
| 41 | } |
| 42 | delete(it2); |
| 43 | } |
| 44 | } |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | |
| 49 | /************************************************************************/ |
| 50 | /* External Class Implementation */ |
| 51 | /************************************************************************/ |
| 52 | |
| 53 | TVG_DEPRECATED unique_ptr<Picture> Accessor::set(unique_ptr<Picture> picture, function<bool(const Paint* paint)> func) noexcept |
| 54 | { |
| 55 | auto backward = [](const tvg::Paint* paint, void* data) -> bool |
| 56 | { |
| 57 | auto func = reinterpret_cast<function<bool(const Paint* paint)>*>(data); |
| 58 | if (!(*func)(paint)) return false; |
| 59 | return true; |
| 60 | }; |
| 61 | |
| 62 | set(picture.get(), backward, reinterpret_cast<void*>(&func)); |
| 63 | return picture; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | Result Accessor::set(Paint* paint, function<bool(const Paint* paint, void* data)> func, void* data) noexcept |
| 68 | { |
| 69 | if (!paint || !func) return Result::InvalidArguments; |
| 70 | |
| 71 | //Use the Preorder Tree-Searc |
| 72 | |
| 73 | //Root |
| 74 | if (!func(paint, data)) return Result::Success; |
| 75 | |
| 76 | //Children |
| 77 | if (auto it = IteratorAccessor::iterator(paint)) { |
| 78 | accessChildren(it, func, data); |
| 79 | delete(it); |
| 80 | } |
| 81 | return Result::Success; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | uint32_t Accessor::id(const char* name) noexcept |
| 86 | { |
| 87 | return djb2Encode(name); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | Accessor::~Accessor() |
| 92 | { |
| 93 | |
| 94 | } |
| 95 | |
| 96 | |
| 97 | Accessor::Accessor() : pImpl(nullptr) |
| 98 | { |
| 99 | |
| 100 | } |
| 101 | |
| 102 | |
| 103 | unique_ptr<Accessor> Accessor::gen() noexcept |
| 104 | { |
| 105 | return unique_ptr<Accessor>(new Accessor); |
| 106 | } |