1 /*
2  * Copyright (c) 2020 - 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 #ifndef _TVG_FILL_H_
27 #define _TVG_FILL_H_
28 
29 #include <cstdlib>
30 #include <cstring>
31 #include "tvgCommon.h"
32 
33 template<typename T>
34 struct DuplicateMethod
35 {
~DuplicateMethodDuplicateMethod36     virtual ~DuplicateMethod() {}
37     virtual T* duplicate() = 0;
38 };
39 
40 template<class T>
41 struct FillDup : DuplicateMethod<Fill>
42 {
43     T* inst = nullptr;
44 
FillDupFillDup45     FillDup(T* _inst) : inst(_inst) {}
~FillDupFillDup46     ~FillDup() {}
47 
duplicateFillDup48     Fill* duplicate() override
49     {
50         return inst->duplicate();
51     }
52 };
53 
54 struct Fill::Impl
55 {
56     ColorStop* colorStops = nullptr;
57     Matrix* transform = nullptr;
58     uint32_t cnt = 0;
59     FillSpread spread;
60     DuplicateMethod<Fill>* dup = nullptr;
61 
~ImplImpl62     ~Impl()
63     {
64         delete(dup);
65         free(colorStops);
66         free(transform);
67     }
68 
methodImpl69     void method(DuplicateMethod<Fill>* dup)
70     {
71         this->dup = dup;
72     }
73 
duplicateImpl74     Fill* duplicate()
75     {
76         auto ret = dup->duplicate();
77         if (!ret) return nullptr;
78 
79         ret->pImpl->cnt = cnt;
80         ret->pImpl->spread = spread;
81         ret->pImpl->colorStops = static_cast<ColorStop*>(malloc(sizeof(ColorStop) * cnt));
82         memcpy(ret->pImpl->colorStops, colorStops, sizeof(ColorStop) * cnt);
83         if (transform) {
84             ret->pImpl->transform = static_cast<Matrix*>(malloc(sizeof(Matrix)));
85             *ret->pImpl->transform = *transform;
86         }
87         return ret;
88     }
89 };
90 
91 
92 struct RadialGradient::Impl
93 {
94     float cx = 0.0f, cy = 0.0f;
95     float fx = 0.0f, fy = 0.0f;
96     float r = 0.0f, fr = 0.0f;
97 
98     Fill* duplicate();
99     Result radial(float cx, float cy, float r, float fx, float fy, float fr);
100 };
101 
102 
103 struct LinearGradient::Impl
104 {
105     float x1 = 0.0f;
106     float y1 = 0.0f;
107     float x2 = 0.0f;
108     float y2 = 0.0f;
109 
110     Fill* duplicate();
111 };
112 
113 
114 #endif  //_TVG_FILL_H_
115 
116 #endif /* LV_USE_THORVG_INTERNAL */
117 
118