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 测试接口:GetFamilyName
28 测试内容:对接口index和familyName取组合,构造typeface字体格式,并指定在font上,最终通过drawtextblob接口将text值绘制在画布上
29 */
30 namespace OHOS {
31 namespace Rosen {
32
CommonGetFamilyName(TestPlaybackCanvas * playbackCanvas,std::shared_ptr<Drawing::FontMgr> & fontMgr,int index)33 void CommonGetFamilyName(TestPlaybackCanvas* playbackCanvas, std::shared_ptr<Drawing::FontMgr>& fontMgr, int index)
34 {
35 std::shared_ptr<Drawing::FontStyleSet> fontStyleSet(fontMgr->CreateStyleSet(0));
36 std::string familyName = "";
37 fontMgr->GetFamilyName(index, familyName);
38 auto typeface = std::shared_ptr<Drawing::Typeface>(fontStyleSet->CreateTypeface(0));
39
40 auto font = Drawing::Font(typeface, 50.f, 1.0f, 0.f);
41 std::string text1 = "DDGR ddgr 鸿蒙 !@#%¥^&*;:,。";
42 std::string text2 = "-_=+()123`.---~|{}【】,./?、?<>《》";
43 std::string text3 = "\xE2\x99\x88\xE2\x99\x8A\xE2\x99\x88\xE2\x99\x8C\xE2\x99\x8D\xE2\x99\x8D";
44 std::string text4 = "GetFamilyName = " + familyName;
45 std::string texts[] = {text1, text2, text3, text4};
46 int line = 200;
47 int interval2 = 200;
48 for (auto text : texts) {
49 std::shared_ptr<Drawing::TextBlob> textBlob = Drawing::TextBlob::MakeFromText(text.c_str(), text.size(), font);
50 Drawing::Brush brush;
51 playbackCanvas->AttachBrush(brush);
52 playbackCanvas->DrawTextBlob(textBlob.get(), interval2, line);
53 line += interval2;
54 playbackCanvas->DetachBrush();
55 Drawing::Pen pen;
56 playbackCanvas->AttachPen(pen);
57 playbackCanvas->DrawTextBlob(textBlob.get(), interval2, line);
58 line += interval2;
59 playbackCanvas->DetachPen();
60 }
61 }
62
63 //对应用例 FontMgr_GetFamilyName_3001
64 DEF_DTK(fontmgr_getfamilyname, TestLevel::L1, 1)
65 {
66 std::shared_ptr<Drawing::FontMgr> fontMgr(Drawing::FontMgr::CreateDefaultFontMgr());
67 CommonGetFamilyName(playbackCanvas_, fontMgr, -1);
68 }
69
70 //对应用例 FontMgr_GetFamilyName_3002
71 DEF_DTK(fontmgr_getfamilyname, TestLevel::L1, 2)
72 {
73 std::shared_ptr<Drawing::FontMgr> fontMgr(Drawing::FontMgr::CreateDynamicFontMgr());
74 CommonGetFamilyName(playbackCanvas_, fontMgr, 50);
75 }
76
77 //对应用例 FontMgr_GetFamilyName_3003
78 DEF_DTK(fontmgr_getfamilyname, TestLevel::L1, 3)
79 {
80 std::shared_ptr<Drawing::FontMgr> fontMgr(Drawing::FontMgr::CreateDynamicFontMgr());
81 CommonGetFamilyName(playbackCanvas_, fontMgr, -1);
82 }
83
84 //对应用例 FontMgr_GetFamilyName_3004
85 DEF_DTK(fontmgr_getfamilyname, TestLevel::L1, 4)
86 {
87 std::shared_ptr<Drawing::FontMgr> fontMgr(Drawing::FontMgr::CreateDynamicFontMgr());
88 CommonGetFamilyName(playbackCanvas_, fontMgr, 0);
89 }
90
91 //对应用例 FontMgr_GetFamilyName_3005
92 DEF_DTK(fontmgr_getfamilyname, TestLevel::L1, 5)
93 {
94 std::shared_ptr<Drawing::FontMgr> fontMgr(Drawing::FontMgr::CreateDefaultFontMgr());
95 CommonGetFamilyName(playbackCanvas_, fontMgr, 50);
96 }
97
98 //对应用例 FontMgr_GetFamilyName_3006
99 DEF_DTK(fontmgr_getfamilyname, TestLevel::L1, 6)
100 {
101 std::shared_ptr<Drawing::FontMgr> fontMgr(Drawing::FontMgr::CreateDefaultFontMgr());
102 CommonGetFamilyName(playbackCanvas_, fontMgr, 0);
103 }
104
105 }
106 }
107