• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <sstream>
17 
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 std::vector<size_t> g_positions = {0, 1, 2, 3, 4, 5, 6, 7, 11, 12, 13, 17, 18, 19};
28 
29 class WordBoundaryTest : public TestFeature {
30 public:
WordBoundaryTest()31     WordBoundaryTest() : TestFeature("WordBoundaryTest")
32     {
33     }
34 
Layout()35     void Layout()
36     {
37         auto builder = TypographyBuilder::Create();
38         builder->AppendSpan("你好");
39         builder->PushStyle({});
40         builder->PopStyle();
41         builder->AppendSpan("世界, ");
42         builder->AppendSpan(std::make_shared<MyAnySpan>(50, 50));   // 50 means span's width and height
43         builder->AppendSpan("He");
44         builder->PushStyle({});
45         builder->PopStyle();
46         builder->AppendSpan("llo World.");
47         auto typography = builder->Build();
48         typography->Layout(200);    // 200 means layout width
49 
50         for (const auto &index : g_positions) {
51             auto onPaint = [index](const struct TypographyData &tyData, TexgineCanvas &canvas, double x, double y) {
52                 const auto &ty = tyData.typography;
53                 ty->Paint(canvas, x, y);
54 
55                 TexginePaint paint;
56                 paint.SetAntiAlias(true);
57                 paint.SetColor(0x5500FF00);
58                 paint.SetStyle(TexginePaint::FILL);
59                 constexpr auto ws = TextRectWidthStyle::TIGHT;
60                 constexpr auto hs = TextRectHeightStyle::TIGHT;
61 
62                 const auto &[left, right] = ty->GetWordBoundaryByIndex(index);
63                 auto wordRects = ty->GetTextRectsByBoundary(Boundary{left, right}, hs, ws);
64                 for (auto &[rect, _] : wordRects) {
65                     rect.GetRect()->offset(x, y);
66                     canvas.DrawRect(rect, paint);
67                 }
68 
69                 paint.SetColor(0xFF000000);
70                 paint.SetStyle(TexginePaint::STROKE);
71                 auto indexRect = ty->GetTextRectsByBoundary(Boundary{index, index + 1}, hs, ws);
72                 for (auto &[rect, _] : indexRect) {
73                     rect.GetRect()->offset(x, y);
74                     canvas.DrawRect(rect, paint);
75                 }
76             };
77 
78             std::stringstream ss;
79             ss << "index: " << index;
80             typographies_.push_back({
81                 .typography = typography,
82                 .comment = ss.str(),
83                 .onPaint = onPaint,
84             });
85         }
86     }
87 } g_test;
88 } // namespace
89