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 // 0xFFFF is the max value of uint16_t
77 if (symbolId <= 0xFFFF) {
78 return;
79 }
80 std::vector<uint32_t> symbolUnicode = {symbolId};
81 std::vector<uint16_t> symbolUnicode16 = SymbolToUTF16(symbolUnicode);
82 std::u16string text;
83 std::copy(symbolUnicode16.begin(), symbolUnicode16.end(), std::back_inserter(text));
84 builder_->AddText(text);
85 }
86
AppendPlaceholder(const PlaceholderSpan & span)87 void TypographyCreate::AppendPlaceholder(const PlaceholderSpan& span)
88 {
89 auto txtPlaceholderRun = Convert(span);
90 builder_->AddPlaceholder(txtPlaceholderRun);
91 }
92
CreateTypography()93 std::unique_ptr<OHOS::Rosen::Typography> TypographyCreate::CreateTypography()
94 {
95 TEXT_TRACE_FUNC();
96 auto paragraph = builder_->Build();
97 return std::make_unique<Typography>(std::move(paragraph));
98 }
99
CreateLineTypography()100 std::unique_ptr<OHOS::Rosen::LineTypography> TypographyCreate::CreateLineTypography()
101 {
102 TEXT_TRACE_FUNC();
103 auto lineFetcher = builder_->BuildLineFetcher();
104 if (lineFetcher == nullptr) {
105 return nullptr;
106 }
107 return std::make_unique<LineTypography>(std::move(lineFetcher));
108 }
109 } // namespace AdapterTxt
110 } // namespace Rosen
111 } // namespace OHOS
112