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