1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "pen_test.h"
17
18 #include "draw/brush.h"
19 #include "draw/color.h"
20 #include "draw/path.h"
21 #include "draw/pen.h"
22 #include "utils/log.h"
23
24 namespace OHOS {
25 namespace Rosen {
26 namespace Drawing {
27 constexpr static float MARGINE_SCALE_SIZE = 10.0f;
TestPenColor(Canvas & canvas,uint32_t width,uint32_t height)28 void PenTest::TestPenColor(Canvas& canvas, uint32_t width, uint32_t height)
29 {
30 LOGI("+++++++ TestPenColor");
31 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
32
33 Pen pen;
34 pen.SetAntiAlias(true);
35 pen.SetWidth(10); // the thickness of the pen is 10
36
37 Rect rect(margin, margin, width - margin, height - margin);
38 pen.SetColor(Drawing::Color::COLOR_RED);
39 canvas.AttachPen(pen).DrawRect(rect);
40
41 Rect rect1(rect.GetLeft() + margin, rect.GetTop() + margin,
42 rect.GetRight() - margin, rect.GetBottom() - margin);
43 // r = 80, g = 227, b = 194, a = 255 max < 255,
44 pen.SetARGB(80, 227, 194, 255);
45 canvas.AttachPen(pen).DrawRect(rect1);
46
47 Rect rect2(rect1.GetLeft() + margin, rect1.GetTop() + margin,
48 rect1.GetRight() - margin, rect1.GetBottom() - margin);
49 // r = 0.5, g = 0.5, b = 0.5, a = 1.0 max < 1.0
50 pen.SetColor({0.5f, 0.5f, 0.5f, 1.0f}, ColorSpace::CreateSRGB());
51 canvas.AttachPen(pen).DrawRect(rect2);
52
53 Rect rect3(rect2.GetLeft() + margin, rect2.GetTop() + margin,
54 rect2.GetRight() - margin, rect2.GetBottom() - margin);
55 Color c(Drawing::Color::COLOR_YELLOW);
56 pen.SetColor(c);
57 canvas.AttachPen(pen).DrawRect(rect3);
58 }
59
TestPenAlpha(Canvas & canvas,uint32_t width,uint32_t height)60 void PenTest::TestPenAlpha(Canvas& canvas, uint32_t width, uint32_t height)
61 {
62 LOGI("+++++++ TestPenAlpha");
63 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
64
65 Pen pen;
66 pen.SetAntiAlias(true);
67 pen.SetWidth(10); // the thickness of the pen is 10
68
69 Rect rect(margin, margin, width - margin, height - margin);
70 pen.SetColor(Drawing::Color::COLOR_RED);
71 canvas.AttachPen(pen).DrawRect(rect);
72
73 Rect rect1(rect.GetLeft() + margin, rect.GetTop() + margin,
74 rect.GetRight() - margin, rect.GetBottom() - margin);
75 // alpha value is 0.5
76 pen.SetAlphaF(0.5f);
77 canvas.AttachPen(pen).DrawRect(rect1);
78
79 Rect rect2(rect1.GetLeft() + margin, rect1.GetTop() + margin,
80 rect1.GetRight() - margin, rect1.GetBottom() - margin);
81 // alpha value is 0x10
82 pen.SetAlpha(0x10);
83 canvas.AttachPen(pen).DrawRect(rect2);
84 }
85
TestPenWidth(Canvas & canvas,uint32_t width,uint32_t height)86 void PenTest::TestPenWidth(Canvas& canvas, uint32_t width, uint32_t height)
87 {
88 LOGI("+++++++ TestPenWidth");
89 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
90
91 Pen pen;
92 pen.SetAntiAlias(true);
93 pen.SetColor(Drawing::Color::COLOR_RED);
94
95 Rect rect(margin, margin, width - margin, height - margin);
96 pen.SetWidth(10); // the thickness of the pen is 10
97 canvas.AttachPen(pen).DrawRect(rect);
98
99 Rect rect1(rect.GetLeft() + margin, rect.GetTop() + margin,
100 rect.GetRight() - margin, rect.GetBottom() - margin);
101 pen.SetWidth(pen.GetWidth() * 2); // the thickness of the pen is 20
102 canvas.AttachPen(pen).DrawRect(rect1);
103 }
104
TestPenMiterLimit(Canvas & canvas,uint32_t width,uint32_t height)105 void PenTest::TestPenMiterLimit(Canvas& canvas, uint32_t width, uint32_t height)
106 {
107 LOGI("+++++++ TestPenMiterLimit");
108 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
109 Rect rect(margin, margin, width - margin, height - margin);
110
111 Pen pen;
112 pen.SetAntiAlias(true);
113 pen.SetColor(Drawing::Color::COLOR_RED);
114 pen.SetWidth(20); // the thickness of the pen is 20
115
116 // 0.2 of height
117 Point a(rect.GetLeft(), rect.GetTop() + rect.GetHeight() * 2.0f / MARGINE_SCALE_SIZE);
118 // 0.3 of height
119 Point b(rect.GetLeft(), rect.GetTop() + rect.GetHeight() * 3.0f / MARGINE_SCALE_SIZE);
120 // half of width and 0.25 of height
121 Point c(rect.GetLeft() + rect.GetWidth() / 2.0f,
122 rect.GetTop() + rect.GetHeight() * 2.5f / MARGINE_SCALE_SIZE);
123 Path path;
124 path.MoveTo(a.GetX(), a.GetY());
125 path.LineTo(c.GetX(), c.GetY());
126 path.LineTo(b.GetX(), b.GetY());
127 canvas.AttachPen(pen).DrawPath(path);
128
129 // 0.3 of height
130 canvas.Translate(0, rect.GetHeight() * 3.0f / MARGINE_SCALE_SIZE);
131 // miter limit is 10.0
132 pen.SetMiterLimit(10.0f);
133 canvas.AttachPen(pen).DrawPath(path);
134
135 // 0.3 of height
136 canvas.Translate(0, rect.GetHeight() * 3.0f / MARGINE_SCALE_SIZE);
137 // miter limit is 5.0
138 pen.SetMiterLimit(5.0f);
139 canvas.AttachPen(pen).DrawPath(path);
140 }
141
TestPenCapStyle(Canvas & canvas,uint32_t width,uint32_t height)142 void PenTest::TestPenCapStyle(Canvas& canvas, uint32_t width, uint32_t height)
143 {
144 LOGI("+++++++ TestPenCapStyle");
145 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
146 Rect rect(margin, margin, width - margin, height - margin);
147
148 Pen pen;
149 pen.SetAntiAlias(true);
150 pen.SetColor(Drawing::Color::COLOR_RED);
151 pen.SetWidth(30); // the thickness of the pen is 30
152
153 // a quarter of width and height
154 Point a(rect.GetLeft() + rect.GetWidth() / 4.0f, rect.GetTop() + rect.GetHeight() / 4.0f);
155 // a quarter of width and height
156 Point b(rect.GetRight() - rect.GetWidth() / 4.0f, rect.GetTop() + rect.GetHeight() / 4.0f);
157 pen.SetCapStyle(Pen::CapStyle::FLAT_CAP);
158 canvas.AttachPen(pen).DrawLine(a, b);
159
160 // a quarter of width and half height
161 Point c(rect.GetLeft() + rect.GetWidth() / 4.0f, rect.GetTop() + rect.GetHeight() / 2.0f);
162 // a quarter of width and half height
163 Point d(rect.GetRight() - rect.GetWidth() / 4.0f, rect.GetTop() + rect.GetHeight() / 2.0f);
164 pen.SetCapStyle(Pen::CapStyle::SQUARE_CAP);
165 canvas.AttachPen(pen).DrawLine(c, d);
166
167 // a quarter of width and height
168 Point e(rect.GetLeft() + rect.GetWidth() / 4.0f, rect.GetBottom() - rect.GetHeight() / 4.0f);
169 // a quarter of width and height
170 Point f(rect.GetRight() - rect.GetWidth() / 4.0f, rect.GetBottom() - rect.GetHeight() / 4.0f);
171 pen.SetCapStyle(Pen::CapStyle::ROUND_CAP);
172 canvas.AttachPen(pen).DrawLine(e, f);
173 }
174
TestPenJoinStyle(Canvas & canvas,uint32_t width,uint32_t height)175 void PenTest::TestPenJoinStyle(Canvas& canvas, uint32_t width, uint32_t height)
176 {
177 LOGI("+++++++ TestPenJoinStyle");
178 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
179
180 Pen pen;
181 pen.SetAntiAlias(true);
182 pen.SetColor(Drawing::Color::COLOR_RED);
183 pen.SetWidth(20); // the thickness of the pen is 20
184
185 Rect rect(margin, margin, width - margin, height - margin);
186 pen.SetJoinStyle(Pen::JoinStyle::MITER_JOIN);
187 canvas.AttachPen(pen).DrawRect(rect);
188
189 Rect rect1(rect.GetLeft() + margin, rect.GetTop() + margin,
190 rect.GetRight() - margin, rect.GetBottom() - margin);
191 pen.SetJoinStyle(Pen::JoinStyle::ROUND_JOIN);
192 canvas.AttachPen(pen).DrawRect(rect1);
193
194 Rect rect2(rect1.GetLeft() + margin, rect1.GetTop() + margin,
195 rect1.GetRight() - margin, rect1.GetBottom() - margin);
196 pen.SetJoinStyle(Pen::JoinStyle::BEVEL_JOIN);
197 canvas.AttachPen(pen).DrawRect(rect2);
198 }
199
TestPenBlendMode(Canvas & canvas,uint32_t width,uint32_t height)200 void PenTest::TestPenBlendMode(Canvas& canvas, uint32_t width, uint32_t height)
201 {
202 LOGI("+++++++ TestPenBlendMode");
203 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
204 Rect rect(margin, margin, width - margin, height - margin);
205
206 Pen pen;
207 pen.SetAntiAlias(true);
208 pen.SetWidth(30); // the thickness of the pen is 30
209
210 // a quarter of width and height
211 Rect rect1(rect.GetLeft(), rect.GetTop() + rect.GetHeight() / 4.0f,
212 rect.GetRight() - rect.GetWidth() / 4.0f, rect.GetBottom() - rect.GetHeight() / 4.0f);
213 pen.SetColor(Drawing::Color::COLOR_RED);
214 pen.SetBlendMode(Drawing::BlendMode::SRC_OVER);
215 canvas.AttachPen(pen).DrawRect(rect1);
216
217 Pen blenderPen;
218 blenderPen.SetAntiAlias(true);
219 blenderPen.SetWidth(30); // the thickness of the pen is 30
220 // a quarter of width and height
221 Rect rect2(rect.GetLeft() + rect.GetWidth() / 4.0f, rect.GetTop() + rect.GetHeight() / 4.0f,
222 rect.GetRight(), rect.GetBottom() - rect.GetHeight() / 4.0f);
223 blenderPen.SetColor(Drawing::Color::COLOR_BLUE);
224 blenderPen.SetBlendMode(Drawing::BlendMode::SRC_OUT);
225 canvas.AttachPen(blenderPen).DrawRect(rect2);
226 }
227
TestPenAntiAlias(Canvas & canvas,uint32_t width,uint32_t height)228 void PenTest::TestPenAntiAlias(Canvas& canvas, uint32_t width, uint32_t height)
229 {
230 LOGI("+++++++ TestPenAntiAlias");
231 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
232 Rect rect(margin, margin, width - margin, height - margin);
233
234 Pen pen;
235 pen.SetColor(Drawing::Color::COLOR_RED);
236 pen.SetWidth(30); // the thickness of the pen is 30
237
238 // a quarter of width and height
239 Point a(rect.GetLeft() + rect.GetWidth() / 4.0f, rect.GetTop() + rect.GetHeight() / 4.0f);
240 Point b(rect.GetRight() - rect.GetWidth() / 4.0f, rect.GetTop() + rect.GetHeight() / 4.0f);
241 Point e(rect.GetLeft() + rect.GetWidth() / 4.0f, rect.GetBottom() - rect.GetHeight() / 4.0f);
242 Point f(rect.GetRight() - rect.GetWidth() / 4.0f, rect.GetBottom() - rect.GetHeight() / 4.0f);
243
244 pen.SetAntiAlias(true);
245 canvas.AttachPen(pen).DrawLine(a, f);
246
247 pen.SetColor(Drawing::Color::COLOR_BLUE);
248 pen.SetAntiAlias(false);
249 canvas.AttachPen(pen).DrawLine(b, e);
250 }
251
TestPenPathEffect(Canvas & canvas,uint32_t width,uint32_t height)252 void PenTest::TestPenPathEffect(Canvas& canvas, uint32_t width, uint32_t height)
253 {
254 LOGI("+++++++ TestPenPathEffect");
255 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
256 Pen pen;
257 pen.SetAntiAlias(true);
258 pen.SetColor(Drawing::Color::COLOR_RED);
259 pen.SetWidth(20); // The thickness of the pen is 20
260
261 Rect rect(margin, margin, width - margin, height - margin);
262 canvas.AttachPen(pen).DrawRect(rect);
263
264 Rect rect1(rect.GetLeft() + margin, rect.GetTop() + margin,
265 rect.GetRight() - margin, rect.GetBottom() - margin);
266 scalar vals[] = { 10.0, 20.0 };
267 // number of elements in the intervals array is 2; offset into the intervals array is 25.
268 pen.SetPathEffect(PathEffect::CreateDashPathEffect(vals, 2, 25));
269 canvas.AttachPen(pen).DrawRect(rect1);
270
271 Rect rect2(rect1.GetLeft() + margin, rect1.GetTop() + margin,
272 rect1.GetRight() - margin, rect1.GetBottom() - margin);
273 Path shapePath;
274 shapePath.MoveTo(0, 0);
275 shapePath.LineTo(0, 20);
276 shapePath.LineTo(20, 0);
277 shapePath.Close();
278 // advance is 20 and phase is 0
279 pen.SetPathEffect(PathEffect::CreatePathDashEffect(shapePath, 20, 0, PathDashStyle::MORPH));
280 canvas.AttachPen(pen).DrawRect(rect2);
281 }
282
TestPenFilter(Canvas & canvas,uint32_t width,uint32_t height)283 void PenTest::TestPenFilter(Canvas& canvas, uint32_t width, uint32_t height)
284 {
285 LOGI("+++++++ TestPenFilter");
286 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
287 Pen pen;
288 pen.SetAntiAlias(true);
289 pen.SetColor(Drawing::Color::COLOR_RED);
290 pen.SetWidth(20); // the thickness of the pen is 20
291
292 Rect rect(margin, margin, width - margin, height - margin);
293
294 Rect rect1(rect.GetLeft() + margin, rect.GetTop() + margin,
295 rect.GetRight() - margin, rect.GetBottom() - margin);
296 Filter filter;
297 // radius of the gaussian blur to apply is 10
298 filter.SetMaskFilter(MaskFilter::CreateBlurMaskFilter(BlurType::NORMAL, 10));
299 pen.SetFilter(filter);
300 canvas.AttachPen(pen).DrawRect(rect1);
301
302 Rect rect2(rect1.GetLeft() + margin, rect1.GetTop() + margin,
303 rect1.GetRight() - margin, rect1.GetBottom() - margin);
304 filter.SetMaskFilter(nullptr);
305 pen.SetFilter(filter);
306 canvas.AttachPen(pen).DrawRect(rect2);
307 }
308
TestPenReset(Canvas & canvas,uint32_t width,uint32_t height)309 void PenTest::TestPenReset(Canvas& canvas, uint32_t width, uint32_t height)
310 {
311 LOGI("+++++++ TestPenReset");
312 const uint32_t margin = static_cast<uint32_t>(width / MARGINE_SCALE_SIZE);
313 Pen pen;
314 pen.SetAntiAlias(true);
315 pen.SetColor(Drawing::Color::COLOR_RED);
316 pen.SetWidth(20); // The thickness of the pen is 20
317
318 Rect rect(margin, margin, width - margin, height - margin);
319
320 Rect rect1(rect.GetLeft() + margin, rect.GetTop() + margin,
321 rect.GetRight() - margin, rect.GetBottom() - margin);
322 Filter filter;
323 // radius of the gaussian blur to apply is 10.
324 filter.SetMaskFilter(MaskFilter::CreateBlurMaskFilter(BlurType::NORMAL, 10));
325 pen.SetFilter(filter);
326 canvas.AttachPen(pen).DrawRect(rect1);
327
328 Rect rect2(rect1.GetLeft() + margin, rect1.GetTop() + margin,
329 rect1.GetRight() - margin, rect1.GetBottom() - margin);
330 pen.Reset();
331 canvas.AttachPen(pen).DrawRect(rect2);
332 }
333
PenTestCase()334 std::vector<PenTest::TestFunc> PenTest::PenTestCase()
335 {
336 std::vector<TestFunc> testFuncVec;
337 testFuncVec.push_back(TestPenColor);
338 testFuncVec.push_back(TestPenAlpha);
339 testFuncVec.push_back(TestPenWidth);
340 testFuncVec.push_back(TestPenMiterLimit);
341 testFuncVec.push_back(TestPenCapStyle);
342 testFuncVec.push_back(TestPenJoinStyle);
343 testFuncVec.push_back(TestPenBlendMode);
344 testFuncVec.push_back(TestPenAntiAlias);
345 testFuncVec.push_back(TestPenPathEffect);
346 testFuncVec.push_back(TestPenFilter);
347 testFuncVec.push_back(TestPenReset);
348 return testFuncVec;
349 }
350 } // namespace Drawing
351 } // namespace Rosen
352 } // namespace OHOS