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 测试类:FontStyleSet
27 测试接口:GetStyle
28 测试内容:对接口入参输入string出参接收styleName,构造typeface字体格式,并指定在font上,最终通过drawtextblob接口将styleName内容绘制在画布上
29 */
30
31 namespace OHOS {
32 namespace Rosen {
33
DrawFontstylesetGetstyle(TestPlaybackCanvas * playbackCanvas,std::shared_ptr<Drawing::FontMgr> font_mgr,std::string name,std::int8_t index)34 void DrawFontstylesetGetstyle(TestPlaybackCanvas* playbackCanvas,
35 std::shared_ptr<Drawing::FontMgr> font_mgr, std::string name, std::int8_t index)
36 {
37 std::shared_ptr<Drawing::FontStyleSet> fontStyleSet(
38 name.empty()
39 ? font_mgr->CreateStyleSet(0)
40 : font_mgr->MatchFamily(name.c_str())
41 );
42 std::string styleName = "";
43 Drawing::FontStyle fontStyle;
44 fontStyleSet->GetStyle(index, &fontStyle, &styleName);
45
46 auto typeface = std::shared_ptr<Drawing::Typeface>(fontStyleSet->CreateTypeface(0));
47 auto font = Drawing::Font(typeface, 50.f, 1.0f, 0.f);
48 std::string text1 = "DDGR ddgr 鸿蒙 !@#%¥^&*;:,。";
49 std::string text2 = "-_=+()123`.---~|{}【】,./?、?<>《》";
50 std::string text3 = "\xE2\x99\x88\xE2\x99\x8A\xE2\x99\x88\xE2\x99\x8C\xE2\x99\x8D\xE2\x99\x8D";
51 std::string text4 = "styleName = " + styleName;
52 std::string texts[] = {text1, text2, text3, text4};
53 int line = 200;
54 int baseLine = 200;
55 for (auto text :texts) {
56 std::shared_ptr<Drawing::TextBlob> textBlob = Drawing::TextBlob::MakeFromText(text.c_str(), text.size(), font);
57 Drawing::Brush brush;
58 playbackCanvas->AttachBrush(brush);
59 playbackCanvas->DrawTextBlob(textBlob.get(), baseLine, line);
60 line += baseLine;
61 playbackCanvas->DetachBrush();
62 Drawing::Pen pen;
63 playbackCanvas->AttachPen(pen);
64 playbackCanvas->DrawTextBlob(textBlob.get(), baseLine, line);
65 line += baseLine;
66 playbackCanvas->DetachPen();
67 }
68 }
69
70 //对应用例 FontStyleSet_GetStyle_3001
71 DEF_DTK(fontstyleset_getstyle, TestLevel::L1, 1)
72 {
73 DrawFontstylesetGetstyle(playbackCanvas_, Drawing::FontMgr::CreateDefaultFontMgr(), "", 1);
74 }
75
76 //对应用例 FontStyleSet_GetStyle_3002
77 DEF_DTK(fontstyleset_getstyle, TestLevel::L1, 2)
78 {
79 DrawFontstylesetGetstyle(playbackCanvas_, Drawing::FontMgr::CreateDefaultFontMgr(), "", -1);
80 }
81
82 //对应用例 FontStyleSet_GetStyle_3003
83 DEF_DTK(fontstyleset_getstyle, TestLevel::L1, 3)
84 {
85 DrawFontstylesetGetstyle(playbackCanvas_, Drawing::FontMgr::CreateDefaultFontMgr(), "HarmonyOS Sans", 0);
86 }
87
88 //对应用例 FontStyleSet_GetStyle_3004
89 DEF_DTK(fontstyleset_getstyle, TestLevel::L1, 4)
90 {
91 DrawFontstylesetGetstyle(playbackCanvas_, Drawing::FontMgr::CreateDefaultFontMgr(), "HarmonyOS Sans", -1);
92 }
93
94 //对应用例 FontStyleSet_GetStyle_3005
95 DEF_DTK(fontstyleset_getstyle, TestLevel::L1, 5)
96 {
97 DrawFontstylesetGetstyle(playbackCanvas_, Drawing::FontMgr::CreateDynamicFontMgr(), "", 50);
98 }
99
100 //对应用例 FontStyleSet_GetStyle_3006
101 DEF_DTK(fontstyleset_getstyle, TestLevel::L1, 6)
102 {
103 DrawFontstylesetGetstyle(playbackCanvas_, Drawing::FontMgr::CreateDynamicFontMgr(), "", 1);
104 }
105
106 //对应用例 FontStyleSet_GetStyle_3007
107 DEF_DTK(fontstyleset_getstyle, TestLevel::L1, 7)
108 {
109 DrawFontstylesetGetstyle(playbackCanvas_, Drawing::FontMgr::CreateDynamicFontMgr(), "HarmonyOS Sans", -1);
110 }
111
112 //对应用例 FontStyleSet_GetStyle_3008
113 DEF_DTK(fontstyleset_getstyle, TestLevel::L1, 8)
114 {
115 DrawFontstylesetGetstyle(playbackCanvas_, Drawing::FontMgr::CreateDynamicFontMgr(), "", 0);
116 }
117
118 //对应用例 FontStyleSet_GetStyle_3009
119 DEF_DTK(fontstyleset_getstyle, TestLevel::L1, 9)
120 {
121 DrawFontstylesetGetstyle(playbackCanvas_, Drawing::FontMgr::CreateDefaultFontMgr(), "HarmonyOS Sans", 1);
122 }
123
124 //对应用例 FontStyleSet_GetStyle_3010
125 DEF_DTK(fontstyleset_getstyle, TestLevel::L1, 10)
126 {
127 DrawFontstylesetGetstyle(playbackCanvas_, Drawing::FontMgr::CreateDefaultFontMgr(), "", 0);
128 }
129
130 //对应用例 FontStyleSet_GetStyle_3011
131 DEF_DTK(fontstyleset_getstyle, TestLevel::L1, 11)
132 {
133 DrawFontstylesetGetstyle(playbackCanvas_, Drawing::FontMgr::CreateDefaultFontMgr(), "HarmonyOS Sans", 50);
134 }
135
136 //对应用例 FontStyleSet_GetStyle_3012
137 DEF_DTK(fontstyleset_getstyle, TestLevel::L1, 12)
138 {
139 DrawFontstylesetGetstyle(playbackCanvas_, Drawing::FontMgr::CreateDynamicFontMgr(), "HarmonyOS Sans", 0);
140 }
141
142 //对应用例 FontStyleSet_GetStyle_3013
143 DEF_DTK(fontstyleset_getstyle, TestLevel::L1, 13)
144 {
145 DrawFontstylesetGetstyle(playbackCanvas_, Drawing::FontMgr::CreateDynamicFontMgr(), "HarmonyOS Sans", 50);
146 }
147
148 //对应用例 FontStyleSet_GetStyle_3014
149 DEF_DTK(fontstyleset_getstyle, TestLevel::L1, 14)
150 {
151 DrawFontstylesetGetstyle(playbackCanvas_, Drawing::FontMgr::CreateDefaultFontMgr(), "", 50);
152 }
153
154 //对应用例 FontStyleSet_GetStyle_3015
155 DEF_DTK(fontstyleset_getstyle, TestLevel::L1, 15)
156 {
157 DrawFontstylesetGetstyle(playbackCanvas_, Drawing::FontMgr::CreateDynamicFontMgr(), "", -1);
158 }
159
160 //对应用例 FontStyleSet_GetStyle_3016
161 DEF_DTK(fontstyleset_getstyle, TestLevel::L1, 16)
162 {
163 DrawFontstylesetGetstyle(playbackCanvas_, Drawing::FontMgr::CreateDynamicFontMgr(), "HarmonyOS Sans", 1);
164 }
165
166 }
167 }