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
16 #include "strokes.h"
17
18 #include <native_drawing/drawing_brush.h>
19 #include <native_drawing/drawing_color.h>
20 #include <native_drawing/drawing_matrix.h>
21 #include <native_drawing/drawing_path.h>
22 #include <native_drawing/drawing_pen.h>
23 #include <native_drawing/drawing_point.h>
24 #include <native_drawing/drawing_round_rect.h>
25 #include <native_drawing/drawing_shader_effect.h>
26
27 #include "test_common.h"
28
29 #include "common/log_common.h"
30
31 enum {
32 K_W = 400,
33 K_H = 400,
34 N = 50,
35 };
36
37 namespace {
38
rnd_rect(DrawRect & r,OH_Drawing_Pen * pen,TestRend & rand)39 void rnd_rect(DrawRect& r, OH_Drawing_Pen* pen, TestRend& rand)
40 {
41 float x = rand.nextUScalar1() * K_W;
42 float y = rand.nextUScalar1() * K_H;
43 float w = rand.nextUScalar1() * (K_W >> 2);
44 float h = rand.nextUScalar1() * (K_H >> 2);
45 float hoffset = rand.nextUScalar1();
46 float woffset = rand.nextUScalar1();
47
48 r.SetXYWH(x, y, w, h);
49 r.Offset(-w / 2 + woffset, -h / 2 + hoffset); // 2 表示去中点进行偏移
50
51 OH_Drawing_PenSetColor(pen, rand.nextU() | 0xFF000000);
52 }
53 } // namespace
54
Strokes2()55 Strokes2::Strokes2()
56 {
57 bitmapWidth_ = K_W;
58 bitmapHeight_ = K_H * 2; // 2 画2倍的高度
59 fileName_ = "strokes_poly"; // 对标 strokes.cpp
60 }
61
OnTestFunction(OH_Drawing_Canvas * canvas)62 void Strokes2::OnTestFunction(OH_Drawing_Canvas* canvas)
63 {
64 TestRend rand;
65 fPath = OH_Drawing_PathCreate();
66 OH_Drawing_PathMoveTo(fPath, 0, 0);
67
68 for (int i = 0; i < 13; i++) { // 13 for times
69 float x = rand.nextUScalar1() * (K_W >> 1);
70 float y = rand.nextUScalar1() * (K_H >> 1);
71 OH_Drawing_PathLineTo(fPath, x, y);
72 }
73
74 OH_Drawing_CanvasClear(canvas, DRAW_COLORWHITE);
75 OH_Drawing_Pen* pen = OH_Drawing_PenCreate();
76 OH_Drawing_PenSetWidth(pen, 4.5f); // 4.5f pen width
77
78 for (int y = 0; y < 2; y++) { // 2 for times
79 OH_Drawing_PenSetAntiAlias(pen, !!y);
80 OH_Drawing_CanvasSave(canvas);
81 OH_Drawing_CanvasTranslate(canvas, 0, y * K_H);
82 OH_Drawing_Rect* rect = OH_Drawing_RectCreate(2.f, 2.f, K_W - 2.f, K_H - 2.f); // 2.f 矩形参数
83 OH_Drawing_CanvasClipRect(canvas, rect, OH_Drawing_CanvasClipOp::INTERSECT, false);
84 TestRend randEx;
85 for (int i = 0; i < N / 2; i++) { // 2 for times
86 DrawRect r;
87 rnd_rect(r, pen, randEx);
88 OH_Drawing_CanvasAttachPen(canvas, pen);
89 OH_Drawing_CanvasRotate(canvas, 15.f, K_W / 2, K_H / 2); // 15.f 旋转角度, 2 被除数
90 OH_Drawing_CanvasDrawPath(canvas, fPath);
91 OH_Drawing_CanvasDetachPen(canvas);
92 }
93 OH_Drawing_RectDestroy(rect);
94 OH_Drawing_CanvasRestore(canvas);
95 }
96 OH_Drawing_PenDestroy(pen);
97 OH_Drawing_PathDestroy(fPath);
98 }
99