Debian Codegraph @

thirdparty/thorvg/src/renderer/tvgFill.cpp

raw symbols SCIP
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 "tvgFill.h"
24
25 /************************************************************************/
26 /* Internal Class Implementation */
27 /************************************************************************/
28
29 Fill* RadialGradient::Impl::duplicate()
30 {
31 auto ret = RadialGradient::gen();
32 if (!ret) return nullptr;
33
34 ret->pImpl->cx = cx;
35 ret->pImpl->cy = cy;
36 ret->pImpl->r = r;
37 ret->pImpl->fx = fx;
38 ret->pImpl->fy = fy;
39 ret->pImpl->fr = fr;
40
41 return ret.release();
42 }
43
44
45 Result RadialGradient::Impl::radial(float cx, float cy, float r, float fx, float fy, float fr)
46 {
47 if (r < 0 || fr < 0) return Result::InvalidArguments;
48
49 this->cx = cx;
50 this->cy = cy;
51 this->r = r;
52 this->fx = fx;
53 this->fy = fy;
54 this->fr = fr;
55
56 return Result::Success;
57 };
58
59
60 Fill* LinearGradient::Impl::duplicate()
61 {
62 auto ret = LinearGradient::gen();
63 if (!ret) return nullptr;
64
65 ret->pImpl->x1 = x1;
66 ret->pImpl->y1 = y1;
67 ret->pImpl->x2 = x2;
68 ret->pImpl->y2 = y2;
69
70 return ret.release();
71 };
72
73
74 /************************************************************************/
75 /* External Class Implementation */
76 /************************************************************************/
77
78 Fill::Fill():pImpl(new Impl())
79 {
80 }
81
82
83 Fill::~Fill()
84 {
85 delete(pImpl);
86 }
87
88
89 Result Fill::colorStops(const ColorStop* colorStops, uint32_t cnt) noexcept
90 {
91 if ((!colorStops && cnt > 0) || (colorStops && cnt == 0)) return Result::InvalidArguments;
92
93 if (cnt == 0) {
94 if (pImpl->colorStops) {
95 free(pImpl->colorStops);
96 pImpl->colorStops = nullptr;
97 pImpl->cnt = 0;
98 }
99 return Result::Success;
100 }
101
102 if (pImpl->cnt != cnt) {
103 pImpl->colorStops = static_cast<ColorStop*>(realloc(pImpl->colorStops, cnt * sizeof(ColorStop)));
104 }
105
106 pImpl->cnt = cnt;
107 memcpy(pImpl->colorStops, colorStops, cnt * sizeof(ColorStop));
108
109 return Result::Success;
110 }
111
112
113 uint32_t Fill::colorStops(const ColorStop** colorStops) const noexcept
114 {
115 if (colorStops) *colorStops = pImpl->colorStops;
116
117 return pImpl->cnt;
118 }
119
120
121 Result Fill::spread(FillSpread s) noexcept
122 {
123 pImpl->spread = s;
124
125 return Result::Success;
126 }
127
128
129 FillSpread Fill::spread() const noexcept
130 {
131 return pImpl->spread;
132 }
133
134
135 Result Fill::transform(const Matrix& m) noexcept
136 {
137 if (!pImpl->transform) {
138 pImpl->transform = static_cast<Matrix*>(malloc(sizeof(Matrix)));
139 }
140 *pImpl->transform = m;
141 return Result::Success;
142 }
143
144
145 Matrix Fill::transform() const noexcept
146 {
147 if (pImpl->transform) return *pImpl->transform;
148 return {1, 0, 0, 0, 1, 0, 0, 0, 1};
149 }
150
151
152 Fill* Fill::duplicate() const noexcept
153 {
154 return pImpl->duplicate();
155 }
156
157
158 TVG_DEPRECATED uint32_t Fill::identifier() const noexcept
159 {
160 return (uint32_t) type();
161 }
162
163
164 RadialGradient::RadialGradient():pImpl(new Impl())
165 {
166 Fill::pImpl->method(new FillDup<RadialGradient::Impl>(pImpl));
167 }
168
169
170 RadialGradient::~RadialGradient()
171 {
172 delete(pImpl);
173 }
174
175
176 Result RadialGradient::radial(float cx, float cy, float r) noexcept
177 {
178 return pImpl->radial(cx, cy, r, cx, cy, 0.0f);
179 }
180
181
182 Result RadialGradient::radial(float* cx, float* cy, float* r) const noexcept
183 {
184 if (cx) *cx = pImpl->cx;
185 if (cy) *cy = pImpl->cy;
186 if (r) *r = pImpl->r;
187
188 return Result::Success;
189 }
190
191
192 unique_ptr<RadialGradient> RadialGradient::gen() noexcept
193 {
194 return unique_ptr<RadialGradient>(new RadialGradient);
195 }
196
197
198 TVG_DEPRECATED uint32_t RadialGradient::identifier() noexcept
199 {
200 return (uint32_t) Type::RadialGradient;
201 }
202
203
204 Type RadialGradient::type() const noexcept
205 {
206 return Type::RadialGradient;
207 }
208
209
210 LinearGradient::LinearGradient():pImpl(new Impl())
211 {
212 Fill::pImpl->method(new FillDup<LinearGradient::Impl>(pImpl));
213 }
214
215
216 LinearGradient::~LinearGradient()
217 {
218 delete(pImpl);
219 }
220
221
222 Result LinearGradient::linear(float x1, float y1, float x2, float y2) noexcept
223 {
224 pImpl->x1 = x1;
225 pImpl->y1 = y1;
226 pImpl->x2 = x2;
227 pImpl->y2 = y2;
228
229 return Result::Success;
230 }
231
232
233 Result LinearGradient::linear(float* x1, float* y1, float* x2, float* y2) const noexcept
234 {
235 if (x1) *x1 = pImpl->x1;
236 if (x2) *x2 = pImpl->x2;
237 if (y1) *y1 = pImpl->y1;
238 if (y2) *y2 = pImpl->y2;
239
240 return Result::Success;
241 }
242
243
244 unique_ptr<LinearGradient> LinearGradient::gen() noexcept
245 {
246 return unique_ptr<LinearGradient>(new LinearGradient);
247 }
248
249
250 TVG_DEPRECATED uint32_t LinearGradient::identifier() noexcept
251 {
252 return (uint32_t) Type::LinearGradient;
253 }
254
255
256 Type LinearGradient::type() const noexcept
257 {
258 return Type::LinearGradient;
259 }