• 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_SPAN_SPAN_STRING_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_SPAN_SPAN_STRING_H
18 
19 #include <list>
20 #include <string>
21 #include <unordered_map>
22 #include <vector>
23 
24 #include "base/memory/referenced.h"
25 #include "core/components_ng/pattern/text/span/tlv_util.h"
26 #include "core/components_ng/pattern/text/span/span_object.h"
27 #include "core/components_ng/pattern/text/text_model.h"
28 
29 namespace OHOS::Ace {
30 
31 class SpanStringBase;
32 
33 enum class SpanStringOperation { REPLACE = 0, INSERT, REMOVE };
34 
35 class ACE_EXPORT SpanString : public SpanStringBase {
36     DECLARE_ACE_TYPE(SpanString, SpanStringBase);
37 
38 public:
39     explicit SpanString(const std::u16string& text);
40     explicit SpanString(const ImageSpanOptions& options);
41     explicit SpanString(RefPtr<CustomSpan>& span);
42     ~SpanString() override;
43     const std::string GetString() const;
44     const std::u16string& GetU16string() const;
45     int32_t GetLength() const;
46     void SetString(const std::u16string& text);
47     void SetGroupId(const RefPtr<SpanBase>& span);
48     void SetFramNode(const WeakPtr<NG::FrameNode>& frameNode);
49     void MarkDirtyFrameNode();
50     void AddCustomSpan();
51     void RemoveCustomSpan();
52     void SetSpanItems(const std::list<RefPtr<NG::SpanItem>>&& spanItems);
53     void SetSpanMap(std::unordered_map<SpanType, std::list<RefPtr<SpanBase>>>&& spansMap);
54     const std::unordered_map<SpanType, std::list<RefPtr<SpanBase>>>& GetSpansMap() const;
55     bool IsEqualToSpanString(const RefPtr<SpanString>& other) const;
56     RefPtr<SpanString> GetSubSpanString(int32_t start, int32_t length, bool includeStartHalf = false,
57         bool includeEndHalf = true, bool rangeNeedNotChange = true) const;
58     std::vector<RefPtr<SpanBase>> GetSpans(int32_t start, int32_t length) const;
59     std::vector<RefPtr<SpanBase>> GetSpans(int32_t start, int32_t length, SpanType spanType) const;
60     RefPtr<SpanBase> GetSpan(int32_t start, int32_t length, SpanType spanType) const;
61     bool operator==(const SpanString& other) const;
62     std::list<RefPtr<NG::SpanItem>> GetSpanItems() const;
63     void AddSpan(const RefPtr<SpanBase>& span);
64     void RemoveSpan(int32_t start, int32_t length, SpanType key);
65     bool CheckRange(int32_t start, int32_t length, bool allowLengthZero = false) const;
66     void BindWithSpans(const std::vector<RefPtr<SpanBase>>& spans);
67     bool EncodeTlv(std::vector<uint8_t>& buff);
68     static RefPtr<SpanString> DecodeTlv(std::vector<uint8_t>& buff);
69     static RefPtr<SpanString> DecodeTlv(std::vector<uint8_t>& buff,
70         const std::function<RefPtr<ExtSpan>(const std::vector<uint8_t>&, int32_t, int32_t)>&& unmarshallCallback,
71         int32_t instanceId = -1);
72     static void DecodeTlvOldExt(std::vector<uint8_t>& buff, SpanString* spanString, int32_t& cursor);
73     static void DecodeTlvExt(std::vector<uint8_t>& buff, SpanString* spanString,
74         const std::function<RefPtr<ExtSpan>(const std::vector<uint8_t>&, int32_t, int32_t)>&& unmarshallCallback,
75         int32_t instanceId = -1);
76     static void DecodeSpanItemList(std::vector<uint8_t>& buff, int32_t& cursor, RefPtr<SpanString>& spanStr);
77     static void DecodeSpanItemListExt(std::vector<uint8_t>& buff, int32_t& cursor, SpanString* spanStr);
78     void ClearSpans();
79     void AppendSpanItem(const RefPtr<NG::SpanItem>& spanItem);
80     void UpdateSpansMap();
81     RefPtr<LineHeightSpan> ToLineHeightSpan(const RefPtr<NG::SpanItem>& spanItem, int32_t start, int32_t end);
82     RefPtr<BackgroundColorSpan> ToBackgroundColorSpan(const RefPtr<NG::SpanItem>& spanItem, int32_t start, int32_t end);
83     RefPtr<ParagraphStyleSpan> ToParagraphStyleSpan(
84         const RefPtr<NG::SpanItem>& spanItem, int32_t start, int32_t end);
85     RefPtr<ImageSpan> ToImageSpan(const RefPtr<NG::SpanItem>& spanItem, int32_t start, int32_t end);
86     RefPtr<TextShadowSpan> ToTextShadowSpan(const RefPtr<NG::SpanItem>& spanItem, int32_t start, int32_t end);
87     RefPtr<GestureSpan> ToGestureSpan(const RefPtr<NG::SpanItem>& spanItem, int32_t start, int32_t end);
88     RefPtr<LetterSpacingSpan> ToLetterSpacingSpan(
89         const RefPtr<NG::SpanItem>& spanItem, int32_t start, int32_t end);
90     RefPtr<BaselineOffsetSpan> ToBaselineOffsetSpan(
91         const RefPtr<NG::SpanItem>& spanItem, int32_t start, int32_t end);
92     RefPtr<DecorationSpan> ToDecorationSpan(const RefPtr<NG::SpanItem>& spanItem, int32_t start, int32_t end);
93     RefPtr<FontSpan> ToFontSpan(const RefPtr<NG::SpanItem>& spanItem, int32_t start, int32_t end);
94     RefPtr<UrlSpan> ToUrlSpan(const RefPtr<NG::SpanItem>& spanItem, int32_t start, int32_t end);
95     std::string ToString();
96 protected:
97     std::list<RefPtr<SpanBase>> GetSubSpanList(
98         int32_t start, int32_t length, const std::list<RefPtr<SpanBase>>& spans) const;
99     void MergeIntervals(std::list<RefPtr<SpanBase>>& spans);
100     void SplitInterval(std::list<RefPtr<SpanBase>>& spans, std::pair<int32_t, int32_t> interval);
101     void ApplyToSpans(const RefPtr<SpanBase>& span, std::pair<int32_t, int32_t> interval, SpanOperation operation);
102     void SortSpans(std::list<RefPtr<SpanBase>>& spans);
103     bool CanMerge(const RefPtr<SpanBase>& a, const RefPtr<SpanBase>& b);
104     static RefPtr<NG::SpanItem> GetDefaultSpanItem(const std::u16string& text);
105     static RefPtr<SpanBase> GetDefaultSpan(SpanType type);
106     void AddSpecialSpan(const RefPtr<SpanBase>& span, SpanType type, int32_t start);
107     int32_t GetStepsByPosition(int32_t pos);
108     void UpdateSpansWithOffset(int32_t start, int32_t offset);
109     void UpdateSpanMapWithOffset(int32_t start, int32_t offset);
110     void UpdateSpanBaseWithOffset(RefPtr<SpanBase>& span, int32_t start, int32_t offset);
111     void RemoveSpecialSpan(int32_t start, int32_t end, SpanType type);
112     // For the scene after image remove
113     bool CheckRange(const RefPtr<SpanBase>& spanBase) const;
114     static std::u16string GetWideStringSubstr(const std::u16string& content, int32_t start);
115     static std::u16string GetWideStringSubstr(const std::u16string& content, int32_t start, int32_t length);
116     std::list<RefPtr<NG::SpanItem>>::iterator SplitSpansAndForward(std::list<RefPtr<NG::SpanItem>>::iterator& it);
117     RefPtr<NG::ImageSpanItem> MakeImageSpanItem(const RefPtr<ImageSpan>& imageSpan);
118     RefPtr<NG::CustomSpanItem> MakeCustomSpanItem(const RefPtr<CustomSpan>& customSpan);
119     void GetSpecialTypesVector(std::list<int32_t>& indexList, int32_t start, int32_t length);
120     void GetNormalTypesVector(std::list<std::pair<int32_t, int32_t>>& indexList, int32_t start, int32_t length);
121     bool ContainSpecialNode(int32_t start, int32_t length);
122     bool IsSpecialNode(RefPtr<SpanBase> span);
123 
124     std::u16string text_;
125     std::unordered_map<SpanType, std::list<RefPtr<SpanBase>>> spansMap_;
126     std::list<RefPtr<NG::SpanItem>> spans_;
127     int32_t groupId_ = 0;
128     WeakPtr<NG::FrameNode> framNode_;
129 };
130 } // namespace OHOS::Ace
131 
132 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_SPAN_SPAN_STRING_H