• 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 <texgine/dynamic_file_font_provider.h>
17 #include <texgine/system_font_provider.h>
18 #include <texgine/typography_builder.h>
19 
20 #include "feature_test_framework.h"
21 
22 using namespace OHOS::Rosen::TextEngine;
23 
24 namespace {
25 constexpr auto EXAMPLE_TEXT = "Hello 1234";
26 
27 struct FeatureInfo {
28     std::string path = "";
29     std::string fontFamily;
30     std::string title = "";
31     std::map<std::string, int> features;
32 } g_infos[] = {
33     {
34         .title = "default",
35     },
36     {
37         .path = RESOURCE_PATH_PREFIX "Roboto-Black.ttf",
38         .fontFamily = "Roboto",
39         .title = "Roboto, pnum",
40         .features = { std::pair<std::string, int>("pnum", 1), },
41     },
42     {
43         .path = RESOURCE_PATH_PREFIX "Roboto-Black.ttf",
44         .fontFamily = "Roboto",
45         .title = "Roboto, tnum",
46         .features = { std::pair<std::string, int>("tnum", 1), },
47     },
48     {
49         .path = RESOURCE_PATH_PREFIX "Roboto-Black.ttf",
50         .fontFamily = "Roboto",
51         .title = "Roboto, pnum 1, pnum 0, tnum 1",
52         .features = {
53             std::pair<std::string, int>("pnum", 1),
54             std::pair<std::string, int>("pnum", 0),
55             std::pair<std::string, int>("tnum", 1),
56         },
57     },
58 };
59 
60 class FeatureTest : public TestFeature {
61 public:
FeatureTest()62     FeatureTest() : TestFeature("FeatureTest")
63     {
64     }
65 
Layout()66     void Layout()
67     {
68         for (const auto &info : g_infos) {
69             auto dfProvider = DynamicFileFontProvider::Create();
70             dfProvider->LoadFont(info.fontFamily, info.path);
71             auto fps = FontProviders::Create();
72             fps->AppendFontProvider(dfProvider);
73             fps->AppendFontProvider(SystemFontProvider::GetInstance());
74             auto builder = TypographyBuilder::Create({}, std::move(fps));
75 
76             TextStyle style;
77             style.fontFamilies = {info.fontFamily};
78             style.fontSize = 64;    // 64 means the font size
79             for (const auto &[feature, value] : info.features) {
80                 style.fontFeature.SetFeature(feature, value);
81             }
82 
83             builder->PushStyle(style);
84             builder->AppendSpan(EXAMPLE_TEXT);
85             builder->PopStyle();
86             auto typography = builder->Build();
87             double widthLimit = 300.0;
88             typography->Layout(widthLimit);
89             typographies_.push_back({
90                 .typography = typography,
91                 .comment = info.title,
92             });
93         }
94     }
95 } g_test;
96 } // namespace
97