• 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 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, 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     bool CheckMultiTypeDecorationSpan(const RefPtr<SpanBase>& span);
64     std::vector<RefPtr<SpanBase>> GetWholeSpans(int32_t start, int32_t end, SpanType spanType) const;
65     void ProcessMultiDecorationSpanForIntersection(
66         const RefPtr<SpanBase>& span, const RefPtr<SpanBase>& lastSpan,
67         std::vector<int32_t>& spanNoIntersection, int32_t start, int32_t end);
68     void ProcessMultiDecorationSpanForNoIntersection(
69         const RefPtr<SpanBase>& span, std::vector<int32_t>& spanNoIntersection,
70         int32_t start, int32_t end);
71     bool ProcessMultiDecorationSpan(const RefPtr<SpanBase>& span, int32_t start, int32_t end);
72     void AddSpan(const RefPtr<SpanBase>& span, bool processMultiDecoration = true, bool isFromHtml = false,
73         bool removeOriginStyle = true);
74     void RemoveSpan(int32_t start, int32_t length, SpanType key);
75     bool CheckRange(int32_t start, int32_t length, bool allowLengthZero = false) const;
76     void BindWithSpans(const std::vector<RefPtr<SpanBase>>& spans);
77     bool EncodeTlv(std::vector<uint8_t>& buff);
78     static RefPtr<SpanString> DecodeTlv(std::vector<uint8_t>& buff);
79     static RefPtr<SpanString> DecodeTlv(std::vector<uint8_t>& buff,
80         const std::function<RefPtr<ExtSpan>(const std::vector<uint8_t>&, int32_t, int32_t)>&& unmarshallCallback,
81         int32_t instanceId = -1);
82     static void DecodeTlvOldExt(std::vector<uint8_t>& buff, SpanString* spanString, int32_t& cursor);
83     static void DecodeTlvExt(std::vector<uint8_t>& buff, SpanString* spanString,
84         const std::function<RefPtr<ExtSpan>(const std::vector<uint8_t>&, int32_t, int32_t)>&& unmarshallCallback,
85         int32_t instanceId = -1);
86     static void DecodeSpanItemList(std::vector<uint8_t>& buff, int32_t& cursor, RefPtr<SpanString>& spanStr);
87     static void DecodeSpanItemListExt(std::vector<uint8_t>& buff, int32_t& cursor, SpanString* spanStr);
88     void ClearSpans();
89     void AppendSpanItem(const RefPtr<NG::SpanItem>& spanItem);
90     void UpdateSpansMap();
91     static std::vector<RefPtr<NG::Paragraph>> GetLayoutInfo(const RefPtr<SpanString>& spanStr,
92         std::optional<double>& maxWidth);
93     RefPtr<LineHeightSpan> ToLineHeightSpan(const RefPtr<NG::SpanItem>& spanItem, int32_t start, int32_t end);
94     RefPtr<BackgroundColorSpan> ToBackgroundColorSpan(const RefPtr<NG::SpanItem>& spanItem, int32_t start, int32_t end);
95     RefPtr<ParagraphStyleSpan> ToParagraphStyleSpan(
96         const RefPtr<NG::SpanItem>& spanItem, int32_t start, int32_t end);
97     RefPtr<ImageSpan> ToImageSpan(const RefPtr<NG::SpanItem>& spanItem, int32_t start, int32_t end);
98     RefPtr<TextShadowSpan> ToTextShadowSpan(const RefPtr<NG::SpanItem>& spanItem, int32_t start, int32_t end);
99     RefPtr<GestureSpan> ToGestureSpan(const RefPtr<NG::SpanItem>& spanItem, int32_t start, int32_t end);
100     RefPtr<LetterSpacingSpan> ToLetterSpacingSpan(
101         const RefPtr<NG::SpanItem>& spanItem, int32_t start, int32_t end);
102     RefPtr<BaselineOffsetSpan> ToBaselineOffsetSpan(
103         const RefPtr<NG::SpanItem>& spanItem, int32_t start, int32_t end);
104     RefPtr<DecorationSpan> ToDecorationSpan(const RefPtr<NG::SpanItem>& spanItem, int32_t start, int32_t end);
105     RefPtr<FontSpan> ToFontSpan(const RefPtr<NG::SpanItem>& spanItem, int32_t start, int32_t end);
106     RefPtr<UrlSpan> ToUrlSpan(const RefPtr<NG::SpanItem>& spanItem, int32_t start, int32_t end);
107     std::string ToString();
108     bool isFromStyledStringMode = false;
109 
110 protected:
111     std::list<RefPtr<SpanBase>> GetSubSpanList(
112         int32_t start, int32_t length, const std::list<RefPtr<SpanBase>>& spans) const;
113     void MergeIntervals(std::list<RefPtr<SpanBase>>& spans);
114     void SplitInterval(std::list<RefPtr<SpanBase>>& spans, std::pair<int32_t, int32_t> interval);
115     void ApplyToSpans(const RefPtr<SpanBase>& span, std::pair<int32_t, int32_t> interval, SpanOperation operation);
116     void SortSpans(std::list<RefPtr<SpanBase>>& spans);
117     bool CanMerge(const RefPtr<SpanBase>& a, const RefPtr<SpanBase>& b);
118     static RefPtr<NG::SpanItem> GetDefaultSpanItem(const std::u16string& text);
119     static RefPtr<SpanBase> GetDefaultSpan(SpanType type);
120     void AddSpecialSpan(const RefPtr<SpanBase>& span, SpanType type, int32_t start);
121     int32_t GetStepsByPosition(int32_t pos);
122     void UpdateSpansWithOffset(int32_t start, int32_t offset);
123     void UpdateSpanMapWithOffset(int32_t start, int32_t offset);
124     void UpdateSpanBaseWithOffset(RefPtr<SpanBase>& span, int32_t start, int32_t offset);
125     void RemoveSpecialSpan(int32_t start, int32_t end, SpanType type);
126     // For the scene after image remove
127     bool CheckRange(const RefPtr<SpanBase>& spanBase) const;
128     static std::u16string GetWideStringSubstr(const std::u16string& content, int32_t start);
129     static std::u16string GetWideStringSubstr(const std::u16string& content, int32_t start, int32_t length);
130     std::list<RefPtr<NG::SpanItem>>::iterator SplitSpansAndForward(std::list<RefPtr<NG::SpanItem>>::iterator& it);
131     RefPtr<NG::ImageSpanItem> MakeImageSpanItem(const RefPtr<ImageSpan>& imageSpan);
132     RefPtr<NG::CustomSpanItem> MakeCustomSpanItem(const RefPtr<CustomSpan>& customSpan);
133     void GetSpecialTypesVector(std::list<int32_t>& indexList, int32_t start, int32_t length);
134     void GetNormalTypesVector(std::list<std::pair<int32_t, int32_t>>& indexList, int32_t start, int32_t length);
135     bool ContainSpecialNode(int32_t start, int32_t length);
136     bool IsSpecialNode(RefPtr<SpanBase> span);
137 
138     std::u16string text_;
139     std::unordered_map<SpanType, std::list<RefPtr<SpanBase>>> spansMap_;
140     std::list<RefPtr<NG::SpanItem>> spans_;
141     WeakPtr<NG::FrameNode> framNode_;
142     int32_t groupId_ = 0;
143 };
144 } // namespace OHOS::Ace
145 
146 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_SPAN_SPAN_STRING_H