22702bbad128deafa71153fb76517635a257cc1d
[platform/core/graphics/tizenvg.git] / test / capi / capiPaint.cpp
1 /*
2  * Copyright (c) 2021 - 2022 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
23 #include <thorvg_capi.h>
24 #include "../catch.hpp"
25
26
27 TEST_CASE("Paint Transform", "[capiPaint]")
28 {
29     Tvg_Paint* paint = tvg_shape_new();
30     REQUIRE(paint);
31
32     Tvg_Matrix matrix_set = {1, 0, 0, 0, 1, 0, 0, 0, 1}, matrix_get;
33
34     REQUIRE(tvg_paint_set_transform(paint, &matrix_set) == TVG_RESULT_SUCCESS);
35     REQUIRE(tvg_paint_get_transform(paint, &matrix_get) == TVG_RESULT_SUCCESS);
36     REQUIRE(matrix_get.e11 == Approx(matrix_set.e11).margin(0.000001));
37     REQUIRE(matrix_get.e12 == Approx(matrix_set.e12).margin(0.000001));
38     REQUIRE(matrix_get.e13 == Approx(matrix_set.e13).margin(0.000001));
39     REQUIRE(matrix_get.e21 == Approx(matrix_set.e21).margin(0.000001));
40     REQUIRE(matrix_get.e22 == Approx(matrix_set.e22).margin(0.000001));
41     REQUIRE(matrix_get.e23 == Approx(matrix_set.e23).margin(0.000001));
42     REQUIRE(matrix_get.e31 == Approx(matrix_set.e31).margin(0.000001));
43     REQUIRE(matrix_get.e32 == Approx(matrix_set.e32).margin(0.000001));
44     REQUIRE(matrix_get.e33 == Approx(matrix_set.e33).margin(0.000001));
45
46     REQUIRE(tvg_paint_del(paint) == TVG_RESULT_SUCCESS);
47 }
48
49 TEST_CASE("Paint Translate", "[capiPaint]")
50 {
51     Tvg_Paint* paint = tvg_shape_new();
52     REQUIRE(paint);
53
54     Tvg_Matrix matrix;
55
56     REQUIRE(tvg_paint_translate(paint, 20, 30) == TVG_RESULT_SUCCESS);
57     REQUIRE(tvg_paint_get_transform(paint, &matrix) == TVG_RESULT_SUCCESS);
58     REQUIRE(matrix.e11 == Approx(1).margin(0.000001));
59     REQUIRE(matrix.e12 == Approx(0).margin(0.000001));
60     REQUIRE(matrix.e13 == Approx(20).margin(0.000001));
61     REQUIRE(matrix.e21 == Approx(0).margin(0.000001));
62     REQUIRE(matrix.e22 == Approx(1).margin(0.000001));
63     REQUIRE(matrix.e23 == Approx(30).margin(0.000001));
64     REQUIRE(matrix.e31 == Approx(0).margin(0.000001));
65     REQUIRE(matrix.e32 == Approx(0).margin(0.000001));
66     REQUIRE(matrix.e33 == Approx(1).margin(0.000001));
67
68     REQUIRE(tvg_paint_del(paint) == TVG_RESULT_SUCCESS);
69 }
70
71 TEST_CASE("Paint Scale", "[capiPaint]")
72 {
73     Tvg_Paint* paint = tvg_shape_new();
74     REQUIRE(paint);
75
76     Tvg_Matrix matrix;
77
78     REQUIRE(tvg_paint_scale(paint, 2.5f) == TVG_RESULT_SUCCESS);
79     REQUIRE(tvg_paint_get_transform(paint, &matrix) == TVG_RESULT_SUCCESS);
80     REQUIRE(matrix.e11 == Approx(2.5).margin(0.000001));
81     REQUIRE(matrix.e12 == Approx(0).margin(0.000001));
82     REQUIRE(matrix.e13 == Approx(0).margin(0.000001));
83     REQUIRE(matrix.e21 == Approx(0).margin(0.000001));
84     REQUIRE(matrix.e22 == Approx(2.5).margin(0.000001));
85     REQUIRE(matrix.e23 == Approx(0).margin(0.000001));
86     REQUIRE(matrix.e31 == Approx(0).margin(0.000001));
87     REQUIRE(matrix.e32 == Approx(0).margin(0.000001));
88     REQUIRE(matrix.e33 == Approx(1).margin(0.000001));
89
90     REQUIRE(tvg_paint_del(paint) == TVG_RESULT_SUCCESS);
91 }
92
93 TEST_CASE("Paint Rotate", "[capiPaint]")
94 {
95     Tvg_Paint* paint = tvg_shape_new();
96     REQUIRE(paint);
97
98     Tvg_Matrix matrix;
99
100     REQUIRE(tvg_paint_rotate(paint, 180.0f) == TVG_RESULT_SUCCESS);
101     REQUIRE(tvg_paint_get_transform(paint, &matrix) == TVG_RESULT_SUCCESS);
102     REQUIRE(matrix.e11 == Approx(-1).margin(0.000001));
103     REQUIRE(matrix.e12 == Approx(0).margin(0.000001));
104     REQUIRE(matrix.e13 == Approx(0).margin(0.000001));
105     REQUIRE(matrix.e21 == Approx(-0).margin(0.000001));
106     REQUIRE(matrix.e22 == Approx(-1).margin(0.000001));
107     REQUIRE(matrix.e23 == Approx(0).margin(0.000001));
108     REQUIRE(matrix.e31 == Approx(0).margin(0.000001));
109     REQUIRE(matrix.e32 == Approx(0).margin(0.000001));
110     REQUIRE(matrix.e33 == Approx(1).margin(0.000001));
111
112     REQUIRE(tvg_paint_del(paint) == TVG_RESULT_SUCCESS);
113 }
114
115 TEST_CASE("Paint Opacity", "[capiPaint]")
116 {
117     Tvg_Paint* paint = tvg_shape_new();
118     REQUIRE(paint);
119
120     uint8_t opacity;
121
122     REQUIRE(tvg_paint_set_opacity(paint, 0) == TVG_RESULT_SUCCESS);
123     REQUIRE(tvg_paint_get_opacity(paint, &opacity) == TVG_RESULT_SUCCESS);
124     REQUIRE(0 == opacity);
125
126     REQUIRE(tvg_paint_set_opacity(paint, 128) == TVG_RESULT_SUCCESS);
127     REQUIRE(tvg_paint_get_opacity(paint, &opacity) == TVG_RESULT_SUCCESS);
128     REQUIRE(128 == opacity);
129
130     REQUIRE(tvg_paint_set_opacity(paint, 255) == TVG_RESULT_SUCCESS);
131     REQUIRE(tvg_paint_get_opacity(paint, &opacity) == TVG_RESULT_SUCCESS);
132     REQUIRE(255 == opacity);
133
134     REQUIRE(tvg_paint_del(paint) == TVG_RESULT_SUCCESS);
135 }
136
137 TEST_CASE("Paint Bounds", "[capiPaint]")
138 {
139     Tvg_Paint* paint = tvg_shape_new();
140     REQUIRE(paint);
141
142     float x, y, w, h;
143
144     REQUIRE(tvg_shape_append_rect(paint, 0, 10, 20, 100, 0, 0) == TVG_RESULT_SUCCESS);
145     REQUIRE(tvg_paint_get_bounds(paint, &x, &y, &w, &h, true) == TVG_RESULT_SUCCESS);
146
147     REQUIRE(x == 0);
148     REQUIRE(y == 10);
149     REQUIRE(w == 20);
150     REQUIRE(h == 100);
151
152     REQUIRE(tvg_shape_reset(paint) == TVG_RESULT_SUCCESS);
153
154     REQUIRE(tvg_paint_translate(paint, 100, 100) == TVG_RESULT_SUCCESS);
155     REQUIRE(tvg_paint_scale(paint, 2) == TVG_RESULT_SUCCESS);
156
157     REQUIRE(tvg_shape_move_to(paint, 0, 10) == TVG_RESULT_SUCCESS);
158     REQUIRE(tvg_shape_line_to(paint, 20, 110) == TVG_RESULT_SUCCESS);
159     REQUIRE(tvg_paint_get_bounds(paint, &x, &y, &w, &h, false) == TVG_RESULT_SUCCESS);
160
161     REQUIRE(x == 0);
162     REQUIRE(y == 10);
163     REQUIRE(w == 20);
164     REQUIRE(h == 100);
165
166     REQUIRE(tvg_paint_get_bounds(paint, &x, &y, &w, &h, true) == TVG_RESULT_SUCCESS);
167
168     REQUIRE(x == Approx(100.0f).margin(0.000001));
169     REQUIRE(y == Approx(120.0f).margin(0.000001));
170     REQUIRE(w == Approx(40.0f).margin(0.000001));
171     REQUIRE(h == Approx(200.0f).margin(0.000001));
172
173     REQUIRE(tvg_paint_del(paint) == TVG_RESULT_SUCCESS);
174 }
175
176 TEST_CASE("Paint Dupliction", "[capiPaint]")
177 {
178     Tvg_Paint* paint = tvg_shape_new();
179     REQUIRE(paint);
180
181     REQUIRE(tvg_paint_set_opacity(paint, 0) == TVG_RESULT_SUCCESS);
182
183     REQUIRE(tvg_paint_translate(paint, 200, 100) == TVG_RESULT_SUCCESS);
184     REQUIRE(tvg_paint_scale(paint, 2.2f) == TVG_RESULT_SUCCESS);
185     REQUIRE(tvg_paint_rotate(paint, 90.0f) == TVG_RESULT_SUCCESS);
186
187     Tvg_Paint* paint_copy = tvg_paint_duplicate(paint);
188     REQUIRE(paint_copy);
189
190     uint8_t opacity;
191     REQUIRE(tvg_paint_get_opacity(paint_copy, &opacity) == TVG_RESULT_SUCCESS);
192     REQUIRE(0 == opacity);
193
194     Tvg_Matrix matrix;
195     REQUIRE(tvg_paint_get_transform(paint, &matrix) == TVG_RESULT_SUCCESS);
196     REQUIRE(matrix.e11 == Approx(0.0f).margin(0.000001));
197     REQUIRE(matrix.e12 == Approx(-2.2f).margin(0.000001));
198     REQUIRE(matrix.e13 == Approx(200.0f).margin(0.000001));
199     REQUIRE(matrix.e21 == Approx(2.2f).margin(0.000001));
200     REQUIRE(matrix.e22 == Approx(0.0f).margin(0.000001));
201     REQUIRE(matrix.e23 == Approx(100.0f).margin(0.000001));
202     REQUIRE(matrix.e31 == Approx(0.0f).margin(0.000001));
203     REQUIRE(matrix.e32 == Approx(0.0f).margin(0.000001));
204     REQUIRE(matrix.e33 == Approx(1.0f).margin(0.000001));
205
206     REQUIRE(tvg_paint_del(paint) == TVG_RESULT_SUCCESS);
207     REQUIRE(tvg_paint_del(paint_copy) == TVG_RESULT_SUCCESS);
208 }
209
210 TEST_CASE("Paint Clip Path Composite Method", "[capiPaint]")
211 {
212     Tvg_Paint* paint = tvg_shape_new();
213     REQUIRE(paint);
214
215     Tvg_Paint* target = tvg_shape_new();
216     REQUIRE(target);
217
218     REQUIRE(tvg_paint_set_composite_method(paint, NULL, TVG_COMPOSITE_METHOD_NONE) == TVG_RESULT_SUCCESS);
219     REQUIRE(tvg_paint_set_composite_method(paint, target, TVG_COMPOSITE_METHOD_NONE) == TVG_RESULT_INVALID_ARGUMENT);
220     REQUIRE(tvg_paint_set_composite_method(paint, NULL, TVG_COMPOSITE_METHOD_CLIP_PATH) == TVG_RESULT_INVALID_ARGUMENT);
221
222     Tvg_Paint* target2 = tvg_shape_new();
223     REQUIRE(target2);
224     REQUIRE(tvg_paint_set_composite_method(paint, target2, TVG_COMPOSITE_METHOD_CLIP_PATH) == TVG_RESULT_SUCCESS);
225
226     const Tvg_Paint* target3 = nullptr;
227     Tvg_Composite_Method method = TVG_COMPOSITE_METHOD_NONE;
228     REQUIRE(tvg_paint_get_composite_method(paint, NULL, &method) == TVG_RESULT_INVALID_ARGUMENT);
229     REQUIRE(tvg_paint_get_composite_method(paint, &target3, NULL) == TVG_RESULT_INVALID_ARGUMENT);
230     REQUIRE(tvg_paint_get_composite_method(paint, &target3, &method) == TVG_RESULT_SUCCESS);
231     REQUIRE(method == TVG_COMPOSITE_METHOD_CLIP_PATH);
232     REQUIRE(target2 == target3);
233
234     REQUIRE(tvg_paint_del(paint) == TVG_RESULT_SUCCESS);
235 }