1 | /* |
2 | * Copyright (c) 2020 - 2023 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 | #ifndef _TVG_FILL_H_ |
24 | #define _TVG_FILL_H_ |
25 | |
26 | #include <cstdlib> |
27 | #include <cstring> |
28 | #include "tvgCommon.h" |
29 | |
30 | template<typename T> |
31 | struct DuplicateMethod |
32 | { |
33 | virtual ~DuplicateMethod() {} |
34 | virtual T* duplicate() = 0; |
35 | }; |
36 | |
37 | template<class T> |
38 | struct FillDup : DuplicateMethod<Fill> |
39 | { |
40 | T* inst = nullptr; |
41 | |
42 | FillDup(T* _inst) : inst(_inst) {} |
43 | ~FillDup() {} |
44 | |
45 | Fill* duplicate() override |
46 | { |
47 | return inst->duplicate(); |
48 | } |
49 | }; |
50 | |
51 | struct Fill::Impl |
52 | { |
53 | ColorStop* colorStops = nullptr; |
54 | Matrix* transform = nullptr; |
55 | uint32_t cnt = 0; |
56 | FillSpread spread; |
57 | DuplicateMethod<Fill>* dup = nullptr; |
58 | uint8_t id; |
59 | |
60 | ~Impl() |
61 | { |
62 | delete(dup); |
63 | free(colorStops); |
64 | free(transform); |
65 | } |
66 | |
67 | void method(DuplicateMethod<Fill>* dup) |
68 | { |
69 | this->dup = dup; |
70 | } |
71 | |
72 | Fill* duplicate() |
73 | { |
74 | auto ret = dup->duplicate(); |
75 | if (!ret) return nullptr; |
76 | |
77 | ret->pImpl->cnt = cnt; |
78 | ret->pImpl->spread = spread; |
79 | ret->pImpl->colorStops = static_cast<ColorStop*>(malloc(sizeof(ColorStop) * cnt)); |
80 | memcpy(ret->pImpl->colorStops, colorStops, sizeof(ColorStop) * cnt); |
81 | if (transform) { |
82 | ret->pImpl->transform = static_cast<Matrix*>(malloc(sizeof(Matrix))); |
83 | *ret->pImpl->transform = *transform; |
84 | } |
85 | return ret; |
86 | } |
87 | }; |
88 | |
89 | #endif //_TVG_FILL_H_ |
90 | |