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::string& text);
40     explicit SpanString(const ImageSpanOptions& options);
41     explicit SpanString(RefPtr<CustomSpan>& span);
42     ~SpanString() override;
43     const std::string& GetString() const;
44     std::wstring GetWideString();
45     int32_t GetLength() const;
46     void SetString(const std::string& text);
47     void SetFramNode(const WeakPtr<NG::FrameNode>& frameNode);
48     void MarkDirtyFrameNode();
49     void AddCustomSpan();
50     void RemoveCustomSpan();
51     void SetGroupId(const RefPtr<SpanBase>& span);
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) const;
57     std::vector<RefPtr<SpanBase>> GetSpans(int32_t start, int32_t length) const;
58     std::vector<RefPtr<SpanBase>> GetSpans(int32_t start, int32_t length, SpanType spanType) const;
59     bool operator==(const SpanString& other) const;
60     std::list<RefPtr<NG::SpanItem>> GetSpanItems() const;
61     void AddSpan(const RefPtr<SpanBase>& span);
62     void RemoveSpan(int32_t start, int32_t length, SpanType key);
63     bool CheckRange(int32_t start, int32_t length, bool allowLengthZero = false) const;
64     void BindWithSpans(const std::vector<RefPtr<SpanBase>>& spans);
65     bool EncodeTlv(std::vector<uint8_t>& buff);
66     static RefPtr<SpanString> DecodeTlv(std::vector<uint8_t>& buff);
67     static void DecodeTlvExt(std::vector<uint8_t>& buff, SpanString* spanString);
68     static void DecodeSpanItemList(std::vector<uint8_t>& buff, int32_t& cursor, RefPtr<SpanString>& spanStr);
69     static void DecodeSpanItemListExt(std::vector<uint8_t>& buff, int32_t& cursor, SpanString* spanStr);
70     void ClearSpans();
71     void AppendSpanItem(const RefPtr<NG::SpanItem>& spanItem);
72     void UpdateSpansMap();
73     RefPtr<LineHeightSpan> ToLineHeightSpan(const RefPtr<NG::SpanItem>& spanItem, int32_t start, int32_t end);
74     RefPtr<ParagraphStyleSpan> ToParagraphStyleSpan(
75         const RefPtr<NG::SpanItem>& spanItem, int32_t start, int32_t end);
76     RefPtr<ImageSpan> ToImageSpan(const RefPtr<NG::SpanItem>& spanItem, int32_t start, int32_t end);
77     RefPtr<TextShadowSpan> ToTextShadowSpan(const RefPtr<NG::SpanItem>& spanItem, int32_t start, int32_t end);
78     RefPtr<GestureSpan> ToGestureSpan(const RefPtr<NG::SpanItem>& spanItem, int32_t start, int32_t end);
79     RefPtr<LetterSpacingSpan> ToLetterSpacingSpan(
80         const RefPtr<NG::SpanItem>& spanItem, int32_t start, int32_t end);
81     RefPtr<BaselineOffsetSpan> ToBaselineOffsetSpan(
82         const RefPtr<NG::SpanItem>& spanItem, int32_t start, int32_t end);
83     RefPtr<DecorationSpan> ToDecorationSpan(const RefPtr<NG::SpanItem>& spanItem, int32_t start, int32_t end);
84     RefPtr<FontSpan> ToFontSpan(const RefPtr<NG::SpanItem>& spanItem, int32_t start, int32_t end);
85     void ChangeStartAndEndToCorrectNum(int32_t& start, int32_t& end);
86     void ChangeStartToCorrectNum(int32_t& start);
87     void ChangeEndToCorrectNum(int32_t& end);
88 protected:
89     RefPtr<SpanBase> GetSpan(int32_t start, int32_t length, SpanType spanType) const;
90     std::list<RefPtr<SpanBase>> GetSubSpanList(
91         int32_t start, int32_t length, const std::list<RefPtr<SpanBase>>& spans) const;
92     void MergeIntervals(std::list<RefPtr<SpanBase>>& spans);
93     void SplitInterval(std::list<RefPtr<SpanBase>>& spans, std::pair<int32_t, int32_t> interval);
94     void ApplyToSpans(const RefPtr<SpanBase>& span, std::pair<int32_t, int32_t> interval, SpanOperation operation);
95     void SortSpans(std::list<RefPtr<SpanBase>>& spans);
96     bool CanMerge(const RefPtr<SpanBase>& a, const RefPtr<SpanBase>& b);
97     static RefPtr<NG::SpanItem> GetDefaultSpanItem(const std::string& text);
98     static RefPtr<SpanBase> GetDefaultSpan(SpanType type);
99     void AddSpecialSpan(const RefPtr<SpanBase>& span, SpanType type, int32_t start);
100     int32_t GetStepsByPosition(int32_t pos);
101     void UpdateSpansWithOffset(int32_t start, int32_t offset);
102     void UpdateSpanMapWithOffset(int32_t start, int32_t offset);
103     void UpdateSpanBaseWithOffset(RefPtr<SpanBase>& span, int32_t start, int32_t offset);
104     void RemoveSpecialSpan(int32_t start, int32_t end, SpanType type);
105     // For the scene after image remove
106     bool CheckRange(const RefPtr<SpanBase>& spanBase) const;
107     static std::wstring GetWideStringSubstr(const std::wstring& content, int32_t start);
108     static std::wstring GetWideStringSubstr(const std::wstring& content, int32_t start, int32_t length);
109     std::list<RefPtr<NG::SpanItem>>::iterator SplitSpansAndForward(std::list<RefPtr<NG::SpanItem>>::iterator& it);
110     RefPtr<NG::ImageSpanItem> MakeImageSpanItem(const RefPtr<ImageSpan>& imageSpan);
111     RefPtr<NG::CustomSpanItem> MakeCustomSpanItem(const RefPtr<CustomSpan>& customSpan);
112     void GetSpecialTypesVector(std::list<int32_t>& indexList, int32_t start, int32_t length);
113     void GetNormalTypesVector(std::list<std::pair<int32_t, int32_t>>& indexList, int32_t start, int32_t length);
114     bool ContainSpecialNode(int32_t start, int32_t length);
115     bool IsSpecialNode(RefPtr<SpanBase> span);
116 
117     std::string text_;
118     std::unordered_map<SpanType, std::list<RefPtr<SpanBase>>> spansMap_;
119     std::list<RefPtr<NG::SpanItem>> spans_;
120     WeakPtr<NG::FrameNode> framNode_;
121     int32_t groupId_ = 0;
122 };
123 } // namespace OHOS::Ace
124 
125 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_SPAN_SPAN_STRING_H