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 #include <texgine/utils/memory_usage_scope.h> 20 21 #include "feature_test_framework.h" 22 #include "my_any_span.h" 23 24 using namespace OHOS::Rosen::TextEngine; 25 26 namespace { 27 struct Res { 28 std::string textContent = ""; 29 uint32_t color = 0x00000000; 30 }; 31 32 std::vector<Res> g_shortRes = { 33 { 34 .textContent = "提示:十翅+堡堡分享桶=?", 35 .color = 0xffffff00, 36 }, 37 }; 38 39 std::vector<Res> g_longRes = { 40 { 41 .textContent = "【损友阿乐的投喂】", 42 .color = 0xff00ff00, 43 }, 44 { 45 .textContent = "休息天也要和朋友们一起云享受生活!损友阿乐拎着美味出现了!来猜今天阿乐带来什么美味套餐?", 46 .color = 0xffffff00, 47 }, 48 { 49 .textContent = "提示:十翅+堡堡分享桶=?", 50 .color = 0xffff00ff, 51 }, 52 { 53 .textContent = "答案:十翅堡堡分享桶,疫情期间注意防护哦", 54 .color = 0xff0000ff, 55 }, 56 { 57 .textContent = "【损友阿乐的投喂】", 58 .color = 0xff00ff00, 59 }, 60 { 61 .textContent = "休息天也要和朋友们一起云享受生活!损友阿乐拎着美味出现了!来猜今天阿乐带来什么美味套餐?", 62 .color = 0xffffff00, 63 }, 64 { 65 .textContent = "提示:十翅+堡堡分享桶=?", 66 .color = 0xff0000ff, 67 }, 68 { 69 .textContent = "答案:十翅堡堡分享桶,疫情期间注意防护哦", 70 .color = 0xff0000ff, 71 } 72 }; 73 74 std::vector<Res> g_manyImageRes = { 75 { .textContent = "同行多个", }, 76 { .color = 0xffff0000, }, 77 { .color = 0xffffff00, }, 78 { .color = 0xff0000ff, }, 79 { .color = 0xff00ff00, }, 80 { .textContent = "同行多个", }, 81 { .color = 0xffff0000, }, 82 { .color = 0xffffff00, }, 83 { .color = 0xff0000ff, }, 84 { .color = 0xff00ff00, }, 85 }; 86 87 struct Info { 88 std::vector<Res> res; 89 TextStyle textStyle = {}; 90 double imageHeight = 20.0; 91 TypographyStyle tpStyle; 92 TextRectHeightStyle heightStyle = TextRectHeightStyle::TIGHT; 93 TextRectWidthStyle widthStyle = TextRectWidthStyle::TIGHT; 94 int widthLimit = 500; 95 std::string title = ""; 96 bool rect = false; 97 } g_infos[] = { 98 { 99 .res = g_shortRes, 100 .title = "default", 101 }, 102 { 103 .res = g_shortRes, 104 .textStyle = { .fontSize = 32, }, 105 .imageHeight = 32, 106 .title = "fontSize: 32, imageHeight: 32", 107 }, 108 { 109 .res = g_longRes, 110 .imageHeight = 16, 111 .title = "多行显示", 112 }, 113 { 114 .res = g_manyImageRes, 115 .imageHeight = 16, 116 .title = "同行多个", 117 }, 118 { 119 .res = g_longRes, 120 .imageHeight = 16, 121 .tpStyle = { .maxLines = 2 }, 122 .title = "maxLines: 2", 123 }, 124 { 125 .res = g_longRes, 126 .imageHeight = 16, 127 .tpStyle = { .maxLines = 2, .ellipsis = u"***" }, 128 .title = "maxLines: 2, ellipsis: '***'", 129 }, 130 { 131 .res = g_shortRes, 132 .tpStyle = { 133 .lineStyle = { .only = true }, 134 }, 135 .title = "RectHeightStyle: Tight, RectWidthStyle: Tight", 136 .rect = true, 137 }, 138 { 139 .res = g_shortRes, 140 .tpStyle = { 141 .lineStyle = { .only = true }, 142 }, 143 .heightStyle = TextRectHeightStyle::COVER_TOP_AND_BOTTOM, 144 .title = "RectHeightStyle: MaxHeight, RectWidthStyle: Tight", 145 .rect = true, 146 }, 147 { 148 .res = g_longRes, 149 .imageHeight = 16, 150 .tpStyle = { 151 .lineStyle = { .only = true }, 152 }, 153 .title = "RectHeightStyle: Tight, RectWidthStyle: Tight", 154 .rect = true, 155 }, 156 { 157 .res = g_longRes, 158 .imageHeight = 16, 159 .tpStyle = { 160 .lineStyle = { .only = true }, 161 }, 162 .widthStyle = TextRectWidthStyle::MAX_WIDTH, 163 .title = "RectHeightStyle: Tight, RectWidthStyle: MaxWidth", 164 .rect = true, 165 } 166 }; 167 168 class MixTest : public TestFeature { 169 public: MixTest()170 MixTest() : TestFeature("MixTest") 171 { 172 } 173 Layout()174 void Layout() override 175 { 176 for (auto &info : g_infos) { 177 auto builder = TypographyBuilder::Create(info.tpStyle); 178 179 for (auto &res : info.res) { 180 if (res.color != 0x00000000) { 181 double imageWidth = 20.0; 182 builder->AppendSpan(std::make_shared<MyAnySpan>( 183 imageWidth, info.imageHeight, 184 AnySpanAlignment::ABOVE_BASELINE, 185 TextBaseline::ALPHABETIC, 186 0, res.color)); 187 } 188 if (!res.textContent.empty()) { 189 builder->PushStyle(info.textStyle); 190 builder->AppendSpan(res.textContent); 191 } 192 } 193 194 auto typography = builder->Build(); 195 typography->Layout(info.widthLimit); 196 typographies_.push_back({ 197 .typography = typography, 198 .needRainbowChar = info.rect, 199 }); 200 } 201 } 202 } g_test; 203 } // namespace 204