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 "typography_builder_impl.h"
17
18 #include <cassert>
19 #include <iostream>
20 #include <memory>
21
22 #include "typography_impl.h"
23
24 namespace OHOS {
25 namespace Rosen {
26 namespace TextEngine {
Create(const TypographyStyle & ys,std::shared_ptr<FontProviders> fontProviders)27 std::unique_ptr<TypographyBuilder> TypographyBuilder::Create(const TypographyStyle& ys,
28 std::shared_ptr<FontProviders> fontProviders)
29 {
30 return std::make_unique<TypographyBuilderImpl>(ys, fontProviders);
31 }
32
TypographyBuilderImpl(const TypographyStyle & ys,std::shared_ptr<FontProviders> fontProviders)33 TypographyBuilderImpl::TypographyBuilderImpl(const TypographyStyle& ys,
34 std::shared_ptr<FontProviders> fontProviders): ys_(std::move(ys)), fontProviders_(fontProviders)
35 {
36 }
37
PushStyle(const TextStyle & style)38 void TypographyBuilderImpl::PushStyle(const TextStyle& style)
39 {
40 styleStack_.push(style);
41 lastTextSpan_ = nullptr;
42 }
43
PopStyle()44 void TypographyBuilderImpl::PopStyle()
45 {
46 assert(styleStack_.size() > 0);
47 styleStack_.pop();
48 }
49
AppendSpan(const std::shared_ptr<AnySpan> & as)50 void TypographyBuilderImpl::AppendSpan(const std::shared_ptr<AnySpan>& as)
51 {
52 if (as == nullptr) {
53 return;
54 }
55
56 VariantSpan vs(as);
57 if (styleStack_.empty()) {
58 vs.SetTextStyle(ys_.ConvertToTextStyle());
59 } else {
60 vs.SetTextStyle(styleStack_.top());
61 }
62
63 spans_.push_back(vs);
64 lastTextSpan_ = nullptr;
65 }
66
AppendSpan(const std::shared_ptr<TextSpan> & ts)67 void TypographyBuilderImpl::AppendSpan(const std::shared_ptr<TextSpan>& ts)
68 {
69 if (ts == nullptr) {
70 return;
71 }
72
73 if (lastTextSpan_ != nullptr) {
74 *lastTextSpan_ += *ts;
75 return;
76 }
77
78 VariantSpan vs(ts);
79 if (styleStack_.empty()) {
80 vs.SetTextStyle(ys_.ConvertToTextStyle());
81 } else {
82 vs.SetTextStyle(styleStack_.top());
83 }
84
85 spans_.push_back(vs);
86 lastTextSpan_ = ts;
87 }
88
AppendSpan(const std::string & text)89 void TypographyBuilderImpl::AppendSpan(const std::string& text)
90 {
91 AppendSpan(TextSpan::MakeFromText(text));
92 }
93
AppendSpan(const std::u16string & text)94 void TypographyBuilderImpl::AppendSpan(const std::u16string& text)
95 {
96 AppendSpan(TextSpan::MakeFromText(text));
97 }
98
AppendSpan(const std::u32string & text)99 void TypographyBuilderImpl::AppendSpan(const std::u32string& text)
100 {
101 AppendSpan(TextSpan::MakeFromText(text));
102 }
103
AppendSpan(const std::vector<uint16_t> & text)104 void TypographyBuilderImpl::AppendSpan(const std::vector<uint16_t>& text)
105 {
106 AppendSpan(TextSpan::MakeFromText(text));
107 }
108
AppendSpan(const std::vector<uint32_t> & text)109 void TypographyBuilderImpl::AppendSpan(const std::vector<uint32_t>& text)
110 {
111 AppendSpan(TextSpan::MakeFromText(text));
112 }
113
Build()114 std::shared_ptr<Typography> TypographyBuilderImpl::Build()
115 {
116 return std::make_shared<TypographyImpl>(ys_, spans_, fontProviders_);
117 }
118 } // namespace TextEngine
119 } // namespace Rosen
120 } // namespace OHOS
121