1 /*
2  * Copyright (c) 2024 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 #include "points.h"
16 
17 #include <native_drawing/drawing_brush.h>
18 #include <native_drawing/drawing_color.h>
19 #include <native_drawing/drawing_matrix.h>
20 #include <native_drawing/drawing_path.h>
21 #include <native_drawing/drawing_pen.h>
22 #include <native_drawing/drawing_point.h>
23 #include <native_drawing/drawing_round_rect.h>
24 #include <native_drawing/drawing_shader_effect.h>
25 
26 #include "test_common.h"
27 
28 #include "common/log_common.h"
29 
30 enum {
31     K_W = 640, // 640 是位图宽度
32     K_H = 490, // 490 是位图高度
33 };
Points()34 Points::Points()
35 {
36     bitmapWidth_ = K_W;
37     bitmapHeight_ = K_H;
38     fileName_ = "points";
39 }
40 
fill_pts(OH_Drawing_Point2D pts[],size_t n,TestRend * rand)41 void Points::fill_pts(OH_Drawing_Point2D pts[], size_t n, TestRend* rand)
42 {
43     for (size_t i = 0; i < n; i++) {
44         // Compute these independently and store in variables, rather
45         // than in the parameter-passing expression, to get consistent
46         // evaluation order across compilers.
47         float y = rand->nextUScalar1() * 480;
48         float x = rand->nextUScalar1() * 640;
49         pts[i].x = x;
50         pts[i].y = y;
51     }
52 }
53 
OnTestFunction(OH_Drawing_Canvas * canvas)54 void Points::OnTestFunction(OH_Drawing_Canvas* canvas)
55 {
56     OH_Drawing_CanvasTranslate(canvas, 1, 1); // 1,1 用于平移画布
57     TestRend rand;
58     const size_t n = 99; // 99 要绘制的点的数量
59     OH_Drawing_Pen* pen0 = OH_Drawing_PenCreate();
60     OH_Drawing_Brush* brush1 = OH_Drawing_BrushCreate();
61     OH_Drawing_Pen* pen2 = OH_Drawing_PenCreate();
62     OH_Drawing_Brush* brush3 = OH_Drawing_BrushCreate();
63 
64     OH_Drawing_PenSetColor(pen0, DRAW_COLORRED);
65     OH_Drawing_BrushSetColor(brush1, DRAW_COLORGREEN);
66     OH_Drawing_PenSetColor(pen2, DRAW_COLORBLUE);
67     OH_Drawing_BrushSetColor(brush3, DRAW_COLORWHITE);
68 
69     OH_Drawing_PenSetWidth(pen0, 4); // 4 画笔pen0的宽度
70     OH_Drawing_PenSetCap(pen2, LINE_ROUND_CAP);
71     OH_Drawing_PenSetWidth(pen2, 6); // 6 画笔pen2的宽度
72 
73     OH_Drawing_Point2D pts[n];
74     fill_pts(pts, n, &rand);
75 
76     OH_Drawing_CanvasAttachPen(canvas, pen0);
77     OH_Drawing_CanvasDrawPoints(canvas, POINT_MODE_POLYGON, n, pts);
78     OH_Drawing_CanvasDetachPen(canvas);
79 
80     OH_Drawing_CanvasAttachBrush(canvas, brush1);
81     OH_Drawing_CanvasDrawPoints(canvas, POINT_MODE_LINES, n, pts);
82     OH_Drawing_CanvasDetachBrush(canvas);
83 
84     OH_Drawing_CanvasAttachPen(canvas, pen2);
85     OH_Drawing_CanvasDrawPoints(canvas, POINT_MODE_POINTS, n, pts);
86     OH_Drawing_CanvasDetachPen(canvas);
87 
88     OH_Drawing_CanvasAttachBrush(canvas, brush3);
89     OH_Drawing_CanvasDrawPoints(canvas, POINT_MODE_POINTS, n, pts);
90     OH_Drawing_CanvasDetachBrush(canvas);
91 
92     OH_Drawing_PenDestroy(pen0);
93     OH_Drawing_PenDestroy(pen2);
94     OH_Drawing_BrushDestroy(brush1);
95     OH_Drawing_BrushDestroy(brush3);
96 }
97