common: revise the identifier() implementation
[platform/core/graphics/tizenvg.git] / src / lib / tvgFill.cpp
1 /*
2  * Copyright (c) 2020-2021 Samsung Electronics Co., Ltd. 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 #include "tvgFill.h"
23
24 /************************************************************************/
25 /* Internal Class Implementation                                        */
26 /************************************************************************/
27
28
29 /************************************************************************/
30 /* External Class Implementation                                        */
31 /************************************************************************/
32
33 Fill::Fill():pImpl(new Impl())
34 {
35 }
36
37
38 Fill::~Fill()
39 {
40     delete(pImpl);
41 }
42
43
44 Result Fill::colorStops(const ColorStop* colorStops, uint32_t cnt) noexcept
45 {
46     if ((!colorStops && cnt > 0) || (colorStops && cnt == 0)) return Result::InvalidArguments;
47
48     if (cnt == 0) {
49         if (pImpl->colorStops) {
50             free(pImpl->colorStops);
51             pImpl->colorStops = nullptr;
52             pImpl->cnt = 0;
53         }
54         return Result::Success;
55     }
56
57     if (pImpl->cnt != cnt) {
58         pImpl->colorStops = static_cast<ColorStop*>(realloc(pImpl->colorStops, cnt * sizeof(ColorStop)));
59     }
60
61     pImpl->cnt = cnt;
62     memcpy(pImpl->colorStops, colorStops, cnt * sizeof(ColorStop));
63
64     return Result::Success;
65 }
66
67
68 uint32_t Fill::colorStops(const ColorStop** colorStops) const noexcept
69 {
70     if (colorStops) *colorStops = pImpl->colorStops;
71
72     return pImpl->cnt;
73 }
74
75
76 Result Fill::spread(FillSpread s) noexcept
77 {
78     pImpl->spread = s;
79
80     return Result::Success;
81 }
82
83
84 FillSpread Fill::spread() const noexcept
85 {
86     return pImpl->spread;
87 }
88
89
90 Result Fill::transform(const Matrix& m) noexcept
91 {
92     if (!pImpl->transform) {
93         pImpl->transform = static_cast<Matrix*>(malloc(sizeof(Matrix)));
94     }
95     *pImpl->transform = m;
96     return Result::Success;
97 }
98
99
100 Matrix Fill::transform() const noexcept
101 {
102     if (pImpl->transform) return *pImpl->transform;
103     return {1, 0, 0, 0, 1, 0, 0, 0, 1};
104 }
105
106
107 Fill* Fill::duplicate() const noexcept
108 {
109     return pImpl->duplicate();
110 }
111
112 uint32_t Fill::identifier() const noexcept
113 {
114     return pImpl->id;
115 }