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 "draw_textblob_test.h"
16 
17 namespace OHOS {
18 namespace Rosen {
19 using namespace Drawing;
OnTestFunction(Drawing::Canvas * canvas)20 void DrawTextBlobTest::OnTestFunction(Drawing::Canvas* canvas)
21 {
22     Font font = Font();
23     font.SetSize(60); // text size 60
24     std::shared_ptr<TextBlob> textblob = TextBlob::MakeFromString("Hello",
25         font, TextEncoding::UTF8);
26     // 1. test pen + BlurDrawLooper
27     Pen pen;
28     pen.SetWidth(2.f);
29     pen.SetColor(0xFFFF0000); // color:red
30     canvas->AttachPen(pen);
31     canvas->DrawTextBlob(textblob.get(), 100, 100); // Screen coordinates(100, 100)
32     canvas->DetachPen();
33 
34     auto blurDrawLooper = BlurDrawLooper::CreateBlurDrawLooper(3.f,
35         -3.f, 3.f, Color{0xFF00FF00}); // radius:3.f, offset(-3.f, 3.f), color:green
36     pen.SetLooper(blurDrawLooper);
37     canvas->AttachPen(pen);
38     canvas->DrawTextBlob(textblob.get(), 100, 200); // Screen coordinates(100, 200)
39     canvas->DetachPen();
40 
41     // 2. test brush + BlurDrawLooper
42     Brush brush;
43     brush.SetColor(0xFFFF0000); // color:red
44     canvas->AttachBrush(brush);
45     canvas->DrawTextBlob(textblob.get(), 300, 100); // Screen coordinates(300, 100)
46     canvas->DetachBrush();
47 
48     brush.SetLooper(blurDrawLooper);
49     canvas->AttachBrush(brush);
50     canvas->DrawTextBlob(textblob.get(), 300, 200); // Screen coordinates(300, 200)
51     canvas->DetachBrush();
52 
53     // 3. test pen + brush + BlurDrawLooper
54     pen.SetLooper(nullptr);
55     brush.SetLooper(nullptr);
56     canvas->AttachPen(pen);
57     canvas->AttachBrush(brush);
58     canvas->DrawTextBlob(textblob.get(), 500, 100); // Screen coordinates(500, 100)
59     canvas->DetachBrush();
60     canvas->DetachPen();
61 
62     pen.SetLooper(blurDrawLooper);
63     brush.SetLooper(blurDrawLooper);
64     canvas->AttachPen(pen);
65     canvas->AttachBrush(brush);
66     canvas->DrawTextBlob(textblob.get(), 500, 200); // Screen coordinates(500, 200)
67     canvas->DetachBrush();
68     canvas->DetachPen();
69 
70     // 4. test pen + BlurDrawLooper, brush + BlurDrawLooper2, can not combine paint
71     auto blurDrawLooper2 = BlurDrawLooper::CreateBlurDrawLooper(3.f,
72         -3.f, 3.f, Color{0xFF00FF00}); // radius:3.f, offset(-3.f, 3.f), color:green
73     pen.SetLooper(blurDrawLooper);
74     brush.SetLooper(blurDrawLooper2);
75     canvas->AttachPen(pen);
76     canvas->AttachBrush(brush);
77     canvas->DrawTextBlob(textblob.get(), 500, 300); // Screen coordinates(500, 300)
78     canvas->DetachBrush();
79     canvas->DetachPen();
80 }
81 
OnTestPerformance(Drawing::Canvas * canvas)82 void DrawTextBlobTest::OnTestPerformance(Drawing::Canvas* canvas)
83 {
84     Font font = Font();
85     font.SetSize(60); // text size 60
86     std::shared_ptr<TextBlob> textblob = TextBlob::MakeFromString("Hello",
87         font, TextEncoding::UTF8);
88     // test drawtextblob performance with BlurDrawLooper
89     Brush brush;
90     brush.SetColor(0xFFFF0000); // color:red
91     auto blurDrawLooper = BlurDrawLooper::CreateBlurDrawLooper(3.f,
92         3.f, 3.f, Color{0xFF00FF00}); // radius:3.f, offset(3.f, 3.f), color:green
93     brush.SetLooper(blurDrawLooper);
94     canvas->AttachBrush(brush);
95     for (int i = 0; i < testCount_; i++) {
96         canvas->DrawTextBlob(textblob.get(), 300, 200); // Screen coordinates(300, 200)
97     }
98     canvas->DetachBrush();
99 }
100 } // namespace Rosen
101 } // namespace OHOS