1 /* 2 * Copyright (c) 2025 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 "text/font_mgr.h" 18 #include "text/font_style_set.h" 19 #include "text/rs_xform.h" 20 #include "text/typeface.h" 21 #include "utils/point.h" 22 #include "utils/rect.h" 23 #include "recording/mem_allocator.h" 24 25 namespace OHOS { 26 namespace Rosen { 27 28 //演示用例 FontStyleSet_CreateTypeface_3001 29 DEF_DTK(fontstyleset_createtypeface, TestLevel::L1, 1) 30 { 31 //1、 调用CreateDefaultFontMgr 函数创建FontMgr实例 32 //2、调用FontMgr的CreateStyleSet 创建FontStyleSet实例 33 //3、通过FontStyleSet 创建字体测试用的typeface 34 //4、通过绘制DrawTextBlob 并设置Font的typeface绘制出文字 35 std::shared_ptr<Drawing::FontMgr> font_mgr(Drawing::FontMgr::CreateDefaultFontMgr()); 36 std::shared_ptr<Drawing::FontStyleSet> fontStyleSet(font_mgr->CreateStyleSet(0)); 37 auto typeface = std::shared_ptr<Drawing::Typeface>(fontStyleSet->CreateTypeface(1)); 38 39 auto font = Drawing::Font(typeface, 50.f, 1.0f, 0.f); 40 std::string text1 = "DDGR ddgr 鸿蒙 !@#%¥^&*;:,。"; 41 std::string text2 = "-_=+()123`.---~|{}【】,./?、?<>《》"; 42 std::string text3 = "\xE2\x99\x88\xE2\x99\x8A\xE2\x99\x88\xE2\x99\x8C\xE2\x99\x8D\xE2\x99\x8D"; 43 std::string texts[] = {text1, text2, text3}; 44 int line = 200; 45 for (auto text :texts) { 46 std::shared_ptr<Drawing::TextBlob> textBlob = Drawing::TextBlob::MakeFromText(text.c_str(), text.size(), font); 47 Drawing::Brush brush; 48 playbackCanvas_->AttachBrush(brush); 49 playbackCanvas_->DrawTextBlob(textBlob.get(), 200, line); 50 line += 200; 51 playbackCanvas_->DetachBrush(); 52 Drawing::Pen pen; 53 playbackCanvas_->AttachPen(pen); 54 playbackCanvas_->DrawTextBlob(textBlob.get(), 200, line); 55 line += 200; 56 playbackCanvas_->DetachPen(); 57 } 58 } 59 60 } 61 } 62