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 "../../lv_conf_internal.h"
24 #if LV_USE_THORVG_INTERNAL
25 
26 #include "tvgIteratorAccessor.h"
27 #include "tvgCompressor.h"
28 
29 /************************************************************************/
30 /* Internal Class Implementation                                        */
31 /************************************************************************/
32 
accessChildren(Iterator * it,function<bool (const Paint * paint,void * data)> func,void * data)33 static bool accessChildren(Iterator* it, function<bool(const Paint* paint, void* data)> func, void* data)
34 {
35     while (auto child = it->next()) {
36         //Access the child
37         if (!func(child, data)) return false;
38 
39         //Access the children of the child
40         if (auto it2 = IteratorAccessor::iterator(child)) {
41             if (!accessChildren(it2, func, data)) {
42                 delete(it2);
43                 return false;
44             }
45             delete(it2);
46         }
47     }
48     return true;
49 }
50 
51 
52 /************************************************************************/
53 /* External Class Implementation                                        */
54 /************************************************************************/
55 
set(unique_ptr<Picture> picture,function<bool (const Paint * paint)> func)56 TVG_DEPRECATED unique_ptr<Picture> Accessor::set(unique_ptr<Picture> picture, function<bool(const Paint* paint)> func) noexcept
57 {
58     auto backward = [](const tvg::Paint* paint, void* data) -> bool
59     {
60         auto func = reinterpret_cast<function<bool(const Paint* paint)>*>(data);
61         if (!(*func)(paint)) return false;
62         return true;
63     };
64 
65     set(picture.get(), backward, reinterpret_cast<void*>(&func));
66     return picture;
67 }
68 
69 
set(const Picture * picture,function<bool (const Paint * paint,void * data)> func,void * data)70 Result Accessor::set(const Picture* picture, function<bool(const Paint* paint, void* data)> func, void* data) noexcept
71 {
72     if (!picture || !func) return Result::InvalidArguments;
73 
74     //Use the Preorder Tree-Search
75 
76     //Root
77     if (!func(picture, data)) return Result::Success;
78 
79     //Children
80     if (auto it = IteratorAccessor::iterator(picture)) {
81         accessChildren(it, func, data);
82         delete(it);
83     }
84     return Result::Success;
85 }
86 
87 
id(const char * name)88 uint32_t Accessor::id(const char* name) noexcept
89 {
90     return djb2Encode(name);
91 }
92 
93 
~Accessor()94 Accessor::~Accessor()
95 {
96 
97 }
98 
99 
Accessor()100 Accessor::Accessor() : pImpl(nullptr)
101 {
102 
103 }
104 
105 
gen()106 unique_ptr<Accessor> Accessor::gen() noexcept
107 {
108     return unique_ptr<Accessor>(new Accessor);
109 }
110 
111 #endif /* LV_USE_THORVG_INTERNAL */
112 
113