• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "typography_create.h"
17 #include "utils/text_trace.h"
18 
19 #include "convert.h"
20 #include "line_typography.h"
21 #include "typography.h"
22 #include <unicode/utf8.h>
23 
24 namespace OHOS {
25 namespace Rosen {
Create(const TypographyStyle & style,std::shared_ptr<FontCollection> collection)26 std::unique_ptr<TypographyCreate> TypographyCreate::Create(const TypographyStyle& style,
27     std::shared_ptr<FontCollection> collection)
28 {
29     return std::make_unique<AdapterTxt::TypographyCreate>(style, collection);
30 }
31 
32 namespace AdapterTxt {
TypographyCreate(const TypographyStyle & style,std::shared_ptr<OHOS::Rosen::FontCollection> collection)33 TypographyCreate::TypographyCreate(const TypographyStyle& style,
34     std::shared_ptr<OHOS::Rosen::FontCollection> collection)
35 {
36     auto paragraphStyle = Convert(style);
37     auto txtFontCollection = Convert(collection)->Get();
38     builder_ = SPText::ParagraphBuilder::Create(paragraphStyle, txtFontCollection);
39 }
40 
PushStyle(const TextStyle & style)41 void TypographyCreate::PushStyle(const TextStyle& style)
42 {
43     TEXT_TRACE_FUNC();
44     auto txtTextStyle = Convert(style);
45     builder_->PushStyle(txtTextStyle);
46 }
47 
PopStyle()48 void TypographyCreate::PopStyle()
49 {
50     builder_->Pop();
51 }
52 
AppendText(const std::u16string & text)53 void TypographyCreate::AppendText(const std::u16string& text)
54 {
55     TEXT_TRACE_FUNC();
56     builder_->AddText(text);
57 }
58 
SymbolToUTF16(const std::vector<uint32_t> & utf32Text)59 std::vector<uint16_t> TypographyCreate::SymbolToUTF16(const std::vector<uint32_t> &utf32Text)
60 {
61     size_t utf32Index = 0;
62     size_t codePoint = 0;
63     int error = 0;
64     std::vector<uint16_t> utf16Text;
65     while (utf32Index < utf32Text.size()) {
66         UTF32_NEXT_CHAR_SAFE(utf32Text.data(), utf32Index, utf32Text.size(), codePoint, error);
67         utf16Text.push_back(U16_LEAD(codePoint));
68         utf16Text.push_back(U16_TRAIL(codePoint));
69     }
70     return utf16Text;
71 }
72 
AppendSymbol(const uint32_t & symbolId)73 void TypographyCreate::AppendSymbol(const uint32_t& symbolId)
74 {
75     TEXT_TRACE_FUNC();
76     std::vector<uint32_t> symbolUnicode = {symbolId};
77     std::vector<uint16_t> symbolUnicode16 = SymbolToUTF16(symbolUnicode);
78     std::u16string text;
79     std::copy(symbolUnicode16.begin(), symbolUnicode16.end(), std::back_inserter(text));
80     builder_->AddText(text);
81 }
82 
AppendPlaceholder(const PlaceholderSpan & span)83 void TypographyCreate::AppendPlaceholder(const PlaceholderSpan& span)
84 {
85     auto txtPlaceholderRun = Convert(span);
86     builder_->AddPlaceholder(txtPlaceholderRun);
87 }
88 
CreateTypography()89 std::unique_ptr<OHOS::Rosen::Typography> TypographyCreate::CreateTypography()
90 {
91     TEXT_TRACE_FUNC();
92     auto paragraph = builder_->Build();
93     return std::make_unique<Typography>(std::move(paragraph));
94 }
95 
CreateLineTypography()96 std::unique_ptr<OHOS::Rosen::LineTypography> TypographyCreate::CreateLineTypography()
97 {
98     TEXT_TRACE_FUNC();
99     auto lineFetcher = builder_->BuildLineFetcher();
100     if (lineFetcher == nullptr) {
101         return nullptr;
102     }
103     return std::make_unique<LineTypography>(std::move(lineFetcher));
104 }
105 } // namespace AdapterTxt
106 } // namespace Rosen
107 } // namespace OHOS
108