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 "../../dtk_test_ext.h"
16 #include "text/font.h"
17 #include "recording/mem_allocator.h"
18 #include "text/font_mgr.h"
19 #include "text/font_style_set.h"
20 #include "text/rs_xform.h"
21 #include "utils/point.h"
22 #include "utils/rect.h"
23 #include "text/typeface.h"
24
25 /*
26 测试类:FontMgr
27 测试接口:MatchFamily
28 测试内容:对接口入参familyName取string类型字符串,构造typeface字体格式,并指定在font上,最终通过drawtextblob接口将text内容绘制在画布上
29 */
30 namespace OHOS {
31 namespace Rosen {
32
CommonMatchFamily(TestPlaybackCanvas * playbackCanvas,std::shared_ptr<Drawing::FontMgr> fontMgr,std::string familyName)33 void CommonMatchFamily(TestPlaybackCanvas* playbackCanvas,
34 std::shared_ptr<Drawing::FontMgr> fontMgr,
35 std::string familyName)
36 {
37 if (familyName == "get") {
38 fontMgr->GetFamilyName(0, familyName);
39 }
40
41 std::shared_ptr<Drawing::FontStyleSet> fontStyleSet(fontMgr->MatchFamily(familyName.c_str()));
42 auto typeface = std::shared_ptr<Drawing::Typeface>(fontStyleSet->CreateTypeface(0));
43
44 auto font = Drawing::Font(typeface, 50.f, 1.0f, 0.f);
45 std::string text1 = "DDGR ddgr 鸿蒙 !@#%¥^&*;:,。";
46 std::string text2 = "-_=+()123`.---~|{}【】,./?、?<>《》";
47 std::string text3 = "\xE2\x99\x88\xE2\x99\x8A\xE2\x99\x88\xE2\x99\x8C\xE2\x99\x8D\xE2\x99\x8D";
48 std::string texts[] = {text1, text2, text3};
49 int line = 200;
50 int interval2 = 200;
51 for (auto text : texts) {
52 std::shared_ptr<Drawing::TextBlob> textBlob = Drawing::TextBlob::MakeFromText(text.c_str(), text.size(), font);
53 Drawing::Brush brush;
54 playbackCanvas->AttachBrush(brush);
55 playbackCanvas->DrawTextBlob(textBlob.get(), interval2, line);
56 line += interval2;
57 playbackCanvas->DetachBrush();
58 Drawing::Pen pen;
59 playbackCanvas->AttachPen(pen);
60 playbackCanvas->DrawTextBlob(textBlob.get(), interval2, line);
61 line += interval2;
62 playbackCanvas->DetachPen();
63 }
64 }
65 //对应用例 FontMgr_MatchFamily_3001
66 DEF_DTK(fontmgr_matchfamily, TestLevel::L1, 1)
67 {
68 std::shared_ptr<Drawing::FontMgr> fontMgr(Drawing::FontMgr::CreateDefaultFontMgr());
69 CommonMatchFamily(playbackCanvas_, fontMgr, "abcd");
70 }
71
72 //对应用例 FontMgr_MatchFamily_3002
73 DEF_DTK(fontmgr_matchfamily, TestLevel::L1, 2)
74 {
75 std::shared_ptr<Drawing::FontMgr> fontMgr(Drawing::FontMgr::CreateDynamicFontMgr());
76 CommonMatchFamily(playbackCanvas_, fontMgr, "");
77 }
78
79 //对应用例 FontMgr_MatchFamily_3003
80 DEF_DTK(fontmgr_matchfamily, TestLevel::L1, 3)
81 {
82 std::shared_ptr<Drawing::FontMgr> fontMgr(Drawing::FontMgr::CreateDefaultFontMgr());
83 CommonMatchFamily(playbackCanvas_, fontMgr, "");
84 }
85
86 //对应用例 FontMgr_MatchFamily_3004
87 DEF_DTK(fontmgr_matchfamily, TestLevel::L1, 4)
88 {
89 std::shared_ptr<Drawing::FontMgr> fontMgr(Drawing::FontMgr::CreateDefaultFontMgr());
90 CommonMatchFamily(playbackCanvas_, fontMgr, "HMOS Color Emoji");
91 }
92
93 //对应用例 FontMgr_MatchFamily_3005
94 DEF_DTK(fontmgr_matchfamily, TestLevel::L1, 5)
95 {
96 std::shared_ptr<Drawing::FontMgr> fontMgr(Drawing::FontMgr::CreateDynamicFontMgr());
97 CommonMatchFamily(playbackCanvas_, fontMgr, "abcd");
98 }
99
100 //对应用例 FontMgr_MatchFamily_3006
101 DEF_DTK(fontmgr_matchfamily, TestLevel::L1, 6)
102 {
103 std::shared_ptr<Drawing::FontMgr> fontMgr(Drawing::FontMgr::CreateDynamicFontMgr());
104 CommonMatchFamily(playbackCanvas_, fontMgr, "get");
105 }
106
107 //对应用例 FontMgr_MatchFamily_3007
108 DEF_DTK(fontmgr_matchfamily, TestLevel::L1, 7)
109 {
110 std::shared_ptr<Drawing::FontMgr> fontMgr(Drawing::FontMgr::CreateDynamicFontMgr());
111 CommonMatchFamily(playbackCanvas_, fontMgr, "HMOS Color Emoji");
112 }
113
114 //对应用例 FontMgr_MatchFamily_3008
115 DEF_DTK(fontmgr_matchfamily, TestLevel::L1, 8)
116 {
117 std::shared_ptr<Drawing::FontMgr> fontMgr(Drawing::FontMgr::CreateDefaultFontMgr());
118 CommonMatchFamily(playbackCanvas_, fontMgr, "get");
119 }
120
121 }
122 }
123