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 #include <sstream> 16 17 #include <texgine/any_span.h> 18 #include <texgine/system_font_provider.h> 19 #include <texgine/typography_builder.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 constexpr const char *TEXT = "Hi 世界"; 28 29 struct AlignmentTestData { 30 AnySpanAlignment alignment; 31 double offset = 0; 32 TextBaseline baseline = TextBaseline::ALPHABETIC; 33 } g_testDatas[] = { 34 { .alignment = AnySpanAlignment::ABOVE_BASELINE }, 35 { .alignment = AnySpanAlignment::ABOVE_BASELINE, .baseline = TextBaseline::IDEOGRAPHIC }, 36 { .alignment = AnySpanAlignment::OFFSET_AT_BASELINE }, 37 { .alignment = AnySpanAlignment::OFFSET_AT_BASELINE, .offset = 40 }, 38 { .alignment = AnySpanAlignment::BELOW_BASELINE }, 39 { .alignment = AnySpanAlignment::TOP_OF_ROW_BOX }, 40 { .alignment = AnySpanAlignment::CENTER_OF_ROW_BOX }, 41 { .alignment = AnySpanAlignment::BOTTOM_OF_ROW_BOX }, 42 }; 43 44 AnySpanAlignment g_onelineAlignments[] = { 45 AnySpanAlignment::TOP_OF_ROW_BOX, 46 AnySpanAlignment::TOP_OF_ROW_BOX, 47 AnySpanAlignment::CENTER_OF_ROW_BOX, 48 AnySpanAlignment::BOTTOM_OF_ROW_BOX, 49 AnySpanAlignment::OFFSET_AT_BASELINE, 50 AnySpanAlignment::ABOVE_BASELINE, 51 AnySpanAlignment::BELOW_BASELINE, 52 }; 53 54 std::map<AnySpanAlignment, std::string> g_alignmentToString = { 55 {AnySpanAlignment::ABOVE_BASELINE, "AB"}, 56 {AnySpanAlignment::BELOW_BASELINE, "BB"}, 57 {AnySpanAlignment::OFFSET_AT_BASELINE, "BL"}, 58 {AnySpanAlignment::TOP_OF_ROW_BOX, "TO"}, 59 {AnySpanAlignment::BOTTOM_OF_ROW_BOX, "BO"}, 60 {AnySpanAlignment::CENTER_OF_ROW_BOX, "MI"}, 61 }; 62 63 std::map<TextBaseline, std::string> g_baselineToString = { 64 {TextBaseline::ALPHABETIC, "Alpha"}, 65 {TextBaseline::IDEOGRAPHIC, "Ideog"}, 66 }; 67 68 class AlignmentTest : public TestFeature { 69 public: AlignmentTest()70 AlignmentTest() : TestFeature("AlignmentTest") {} 71 Layout()72 void Layout() 73 { 74 option_.needRainbowChar = true; 75 TextStyle textStyle = { 76 .fontSize = 20, 77 .decoration = TextDecoration::BASELINE, 78 }; 79 80 for (const auto &data : g_testDatas) { 81 std::vector<double> heights = {20, 40, 60}; 82 double width = 20.0; 83 for (const auto &height : heights) { 84 auto builder = TypographyBuilder::Create(); 85 builder->PushStyle(textStyle); 86 builder->AppendSpan(TEXT); 87 88 auto anySpan = std::make_shared<MyAnySpan>(width, 89 height, data.alignment, data.baseline, data.offset); 90 builder->AppendSpan(anySpan); 91 92 auto typography = builder->Build(); 93 double widthLimit = 150.0; 94 typography->Layout(widthLimit); 95 96 std::stringstream ss; 97 ss << g_alignmentToString[data.alignment]; 98 if (data.alignment == AnySpanAlignment::OFFSET_AT_BASELINE) { 99 ss << "(" << data.offset << ")"; 100 } 101 ss << " " << g_baselineToString[data.baseline]; 102 ss << " " << height << "px"; 103 104 typographies_.push_back({ 105 .typography = typography, 106 .comment = ss.str(), 107 .atNewline = (height == *heights.begin()), 108 }); 109 } 110 } 111 112 LayoutOneLine(textStyle); 113 } 114 LayoutOneLine(const TextStyle & textStyle)115 void LayoutOneLine(const TextStyle &textStyle) 116 { 117 auto builder = TypographyBuilder::Create(); 118 std::string title = "单行多个不同对齐方式"; 119 double anySpanWidth = 20.0; 120 double anySpanHeight = 30.0; 121 for (const auto &alignment : g_onelineAlignments) { 122 auto anySpan = std::make_shared<MyAnySpan>(anySpanWidth, anySpanHeight, alignment); 123 builder->AppendSpan(anySpan); 124 builder->PushStyle(textStyle); 125 builder->AppendSpan(TEXT); 126 title += g_alignmentToString[alignment] + " "; 127 } 128 129 auto typography = builder->Build(); 130 double widthLimit = 800.0; 131 typography->Layout(widthLimit); 132 typographies_.push_back({ 133 .typography = typography, 134 .comment = title, 135 }); 136 } 137 } g_test; 138 } // namespace 139