• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_SPAN_NODE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_SPAN_NODE_H
18 
19 #include <list>
20 #include <memory>
21 #include <string>
22 
23 #include "base/memory/referenced.h"
24 #include "core/components_ng/base/ui_node.h"
25 #include "core/components_ng/pattern/text/text_styles.h"
26 #include "core/components_v2/inspector/inspector_constants.h"
27 #include "core/gestures/gesture_info.h"
28 
29 #define DEFINE_SPAN_FONT_STYLE_ITEM(name, type)                              \
30 public:                                                                      \
31     std::optional<type> Get##name() const                                    \
32     {                                                                        \
33         if (spanItem_->fontStyle) {                                          \
34             return spanItem_->fontStyle->Get##name();                        \
35         }                                                                    \
36         return std::nullopt;                                                 \
37     }                                                                        \
38     bool Has##name() const                                                   \
39     {                                                                        \
40         if (spanItem_->fontStyle) {                                          \
41             return spanItem_->fontStyle->Has##name();                        \
42         }                                                                    \
43         return false;                                                        \
44     }                                                                        \
45     type Get##name##Value(const type& defaultValue) const                    \
46     {                                                                        \
47         if (spanItem_->fontStyle) {                                          \
48             return spanItem_->fontStyle->Get##name().value_or(defaultValue); \
49         }                                                                    \
50         return defaultValue;                                                 \
51     }                                                                        \
52     void Update##name(const type& value)                                     \
53     {                                                                        \
54         if (!spanItem_->fontStyle) {                                         \
55             spanItem_->fontStyle = std::make_unique<FontStyle>();            \
56         }                                                                    \
57         if (spanItem_->fontStyle->Check##name(value)) {                      \
58             LOGD("the %{public}s is same, just ignore", #name);              \
59             return;                                                          \
60         }                                                                    \
61         spanItem_->fontStyle->Update##name(value);                           \
62         RequestTextFlushDirty();                                             \
63     }
64 
65 namespace OHOS::Ace::NG {
66 
67 class Paragraph;
68 
69 struct SpanItem : public Referenced {
70     int32_t positon;
71     std::string content;
72     std::unique_ptr<FontStyle> fontStyle;
73     GestureEventFunc onClick;
74     std::list<RefPtr<SpanItem>> children;
75 
76     void UpdateParagraph(const RefPtr<Paragraph>& builder);
77 
78     void ToJsonValue(std::unique_ptr<JsonValue>& json) const;
79 };
80 
81 class ACE_EXPORT SpanNode : public UINode {
82     DECLARE_ACE_TYPE(SpanNode, UINode);
83 
84 public:
85     static RefPtr<SpanNode> GetOrCreateSpanNode(int32_t nodeId);
86 
SpanNode(int32_t nodeId)87     explicit SpanNode(int32_t nodeId) : UINode(V2::SPAN_ETS_TAG, nodeId) {}
88     ~SpanNode() override = default;
89 
IsAtomicNode()90     bool IsAtomicNode() const override
91     {
92         return true;
93     }
94 
GetSpanItem()95     const RefPtr<SpanItem>& GetSpanItem() const
96     {
97         return spanItem_;
98     }
99 
UpdateContent(const std::string & content)100     void UpdateContent(const std::string& content)
101     {
102         if (spanItem_->content == content) {
103             LOGD("the content is same, just ignore");
104             return;
105         }
106         spanItem_->content = content;
107         RequestTextFlushDirty();
108     }
109 
UpdateOnClickEvent(GestureEventFunc && onClick)110     void UpdateOnClickEvent(GestureEventFunc&& onClick)
111     {
112         spanItem_->onClick = std::move(onClick);
113     }
114 
115     DEFINE_SPAN_FONT_STYLE_ITEM(FontSize, Dimension);
116     DEFINE_SPAN_FONT_STYLE_ITEM(TextColor, Color);
117     DEFINE_SPAN_FONT_STYLE_ITEM(ItalicFontStyle, Ace::FontStyle);
118     DEFINE_SPAN_FONT_STYLE_ITEM(FontWeight, FontWeight);
119     DEFINE_SPAN_FONT_STYLE_ITEM(FontFamily, std::vector<std::string>);
120     DEFINE_SPAN_FONT_STYLE_ITEM(TextDecoration, TextDecoration);
121     DEFINE_SPAN_FONT_STYLE_ITEM(TextDecorationColor, Color);
122     DEFINE_SPAN_FONT_STYLE_ITEM(TextCase, TextCase);
123     DEFINE_SPAN_FONT_STYLE_ITEM(LetterSpacing, Dimension);
124 
125     // Mount to the previous Span node or Text node.
126     void MountToParagraph();
127 
AddChildSpanItem(const RefPtr<SpanNode> & child)128     void AddChildSpanItem(const RefPtr<SpanNode>& child)
129     {
130         spanItem_->children.emplace_back(child->GetSpanItem());
131     }
132 
CleanSpanItemChildren()133     void CleanSpanItemChildren()
134     {
135         spanItem_->children.clear();
136     }
137 
ToJsonValue(std::unique_ptr<JsonValue> & json)138     void ToJsonValue(std::unique_ptr<JsonValue>& json) const override
139     {
140         spanItem_->ToJsonValue(json);
141     }
142 
143     void RequestTextFlushDirty();
144     // The function is only used for fast preview.
FastPreviewUpdateChildDone()145     void FastPreviewUpdateChildDone() override
146     {
147         RequestTextFlushDirty();
148     }
149 
150 private:
151     std::list<RefPtr<SpanNode>> spanChildren_;
152 
153     RefPtr<SpanItem> spanItem_ = MakeRefPtr<SpanItem>();
154 
155     ACE_DISALLOW_COPY_AND_MOVE(SpanNode);
156 };
157 
158 } // namespace OHOS::Ace::NG
159 
160 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SYNTAX_FOR_EACH_NODE_H
161