1 /* 2 * Copyright (c) 2023 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 16 #include "texgine/any_span.h" 17 #include "texgine/system_font_provider.h" 18 #include "texgine/typography_builder.h" 19 20 #include "feature_test_framework.h" 21 #include "my_any_span.h" 22 23 using namespace OHOS::Rosen::TextEngine; 24 25 namespace { 26 constexpr auto EXAMPLE_TEXT = "hello world, hello openharmony!"; 27 constexpr auto EXAMPLE_TEXT2 = "你 好 世 界 你好鸿蒙!"; 28 29 struct IntrinsicTestData { 30 TypographyStyle ys; 31 std::string title; 32 double widthLimit = 150; 33 const char *text = EXAMPLE_TEXT; 34 std::shared_ptr<MyAnySpan> as = std::make_shared<MyAnySpan>(20, 80); 35 } g_datas[] = { 36 { 37 .ys = { 38 .ellipsis = u"", 39 }, 40 .title = "不换行的对照组", 41 .widthLimit = 700, 42 }, 43 { 44 .ys = { 45 .ellipsis = u"", 46 .wordBreakType = WordBreakType::BREAK_WORD, 47 }, 48 .title = "breakWord", 49 }, 50 { 51 .ys = { 52 .ellipsis = u"", 53 .wordBreakType = WordBreakType::BREAK_ALL, 54 }, 55 .title = "breakAll", 56 }, 57 { 58 .ys = { 59 .maxLines = 1, 60 }, 61 .title = "maxline = 1", 62 }, 63 { 64 .ys = { 65 .ellipsis = u"", 66 .wordBreakType = WordBreakType::BREAK_ALL, 67 }, 68 .title = "breakAll,中文", 69 .widthLimit = 120, 70 .text = EXAMPLE_TEXT2, 71 }, 72 }; 73 74 class IntrinsicWidthTest : public TestFeature { 75 public: IntrinsicWidthTest()76 IntrinsicWidthTest() : TestFeature("IntrinsicWidthTest") 77 { 78 } 79 Layout()80 void Layout() 81 { 82 for (auto &[ys, title, limit, text, as] : g_datas) { 83 auto builder = TypographyBuilder::Create(ys); 84 builder->PushStyle({}); 85 builder->AppendSpan(text); 86 builder->AppendSpan(as); 87 builder->PopStyle(); 88 89 auto typography = builder->Build(); 90 typography->Layout(limit); 91 auto onPaint = [&](const struct TypographyData &tyData, TexgineCanvas &canvas, double x, double y) { 92 // typography 93 tyData.typography->Paint(canvas, x, y); 94 95 TexginePaint paint; 96 paint.SetAntiAlias(true); 97 paint.SetStyle(TexginePaint::FILL); 98 99 // max 100 paint.SetColor(0x5500FF00); 101 auto rect1 = TexgineRect::MakeXYWH(x, y, tyData.typography->GetMaxIntrinsicWidth(), 10); 102 canvas.DrawRect(rect1, paint); 103 104 // min 105 paint.SetColor(0x5500FFCC); 106 auto rect2 = TexgineRect::MakeXYWH(x, y + 10, tyData.typography->GetMinIntrinsicWidth(), 10); 107 canvas.DrawRect(rect2, paint); 108 109 // placeholder 110 auto rects = tyData.typography->GetTextRectsOfPlaceholders(); 111 for (auto &[rect, _] : rects) { 112 rect.GetRect()->offset(x, y); 113 canvas.DrawRect(rect, paint); 114 } 115 }; 116 117 typographies_.push_back({ 118 .typography = typography, 119 .comment = title, 120 .atNewline = true, 121 .onPaint = onPaint, 122 }); 123 } 124 } 125 } g_test; 126 } // namespace 127