1 /*
2  * Copyright (c) 2021-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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_RICH_EDITOR_RICH_EDITOR_SELECTION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_RICH_EDITOR_RICH_EDITOR_SELECTION_H
18 
19 #include <list>
20 
21 #include "base/geometry/offset.h"
22 #include "base/image/pixel_map.h"
23 #include "base/memory/ace_type.h"
24 #include "core/common/resource/resource_object.h"
25 #include "core/components/common/properties/color.h"
26 #include "core/components/common/properties/text_style.h"
27 #include "core/event/ace_events.h"
28 #include "core/event/axis_event.h"
29 namespace OHOS::Ace::NG {
30 struct SpanItem;
31 }
32 
33 namespace OHOS::Ace {
34 namespace {
35 Color DEFAULT_SYMBOL_COLOR = Color::BLACK;
36 }
37 using FONT_FEATURES_LIST = std::list<std::pair<std::string, int32_t>>;
38 enum GetSpansMethod : int32_t {
39     GETSPANS,
40     ONSELECT,
41 };
42 
43 enum RichEditorImageSize : int32_t {
44     SIZEWIDTH,
45     SIZEHEIGHT,
46 };
47 
48 enum RichEditorSpanRange : int32_t {
49     RANGESTART,
50     RANGEEND,
51 };
52 
53 enum SelectSpanType : int32_t {
54     TYPESPAN,
55     TYPEIMAGE,
56     TYPESYMBOLSPAN,
57     TYPEBUILDERSPAN,
58 };
59 
60 enum RichEditorLeadingRange : int32_t {
61     LEADING_START,
62     LEADING_END,
63 };
64 
65 struct SpanPosition {
66     int32_t spanIndex = 0;
67     int32_t spanRange[2] = { 0, 0 };
68 };
69 
70 struct SymbolSpanStyle {
71     double fontSize = 0.0;
72     double lineHeight = 0.0;
73     double letterSpacing = 0.0;
74     double lineSpacing = 0.0;
75     std::string symbolColor;
76     FONT_FEATURES_LIST fontFeature;
77     int32_t fontWeight = 0;
78     int32_t renderingStrategy = 0;
79     int32_t effectStrategy = 0;
80 
SymbolSpanStyleSymbolSpanStyle81     SymbolSpanStyle() {}
SymbolSpanStyleSymbolSpanStyle82     SymbolSpanStyle(const TextStyle& style)
83     {
84         fontSize = style.GetFontSize().ConvertToVp();
85         lineHeight = style.GetLineHeight().ConvertToPx();
86         letterSpacing = style.GetLetterSpacing().ConvertToPx();
87         lineSpacing = style.GetLineSpacing().ConvertToPx();
88 
89         for (const auto& color : style.GetSymbolColorList()) {
90             symbolColor += color.ColorToString() + ",";
91         }
92         if (symbolColor.size() > 0) {
93             symbolColor = symbolColor.substr(0, symbolColor.size() - 1);
94         }
95         symbolColor = symbolColor.empty() ? DEFAULT_SYMBOL_COLOR.ColorToString() : symbolColor;
96 
97         fontFeature = style.GetFontFeatures();
98         fontWeight = static_cast<int32_t>(style.GetFontWeight());
99         renderingStrategy = style.GetRenderStrategy();
100         effectStrategy = style.GetEffectStrategy();
101     }
102 
103     bool operator==(const SymbolSpanStyle& rhs) const
104     {
105         return fontSize == rhs.fontSize
106             && lineHeight == rhs.lineHeight
107             && letterSpacing == rhs.letterSpacing
108             && lineSpacing == rhs.lineSpacing
109             && symbolColor == rhs.symbolColor
110             && fontFeature == rhs.fontFeature
111             && fontFeature == rhs.fontFeature
112             && fontWeight == rhs.fontWeight
113             && renderingStrategy == rhs.renderingStrategy
114             && effectStrategy == rhs.effectStrategy;
115     }
116 
117     bool operator!=(const SymbolSpanStyle& rhs) const
118     {
119         return !operator==(rhs);
120     }
121 };
122 
123 struct TextStyleResult {
124     std::string fontColor;
125     double fontSize = 0.0;
126     double lineHeight = 0.0;
127     double letterSpacing = 0.0;
128     double lineSpacing = 0.0;
129     int32_t fontStyle = 0;
130     int32_t fontWeight = 0;
131     FONT_FEATURES_LIST fontFeature;
132     std::string fontFamily;
133     int32_t decorationType = 0;
134     std::string decorationColor;
135     int32_t decorationStyle = 0;
136     int32_t wordBreak = static_cast<int32_t>(WordBreak::BREAK_WORD);
137     int32_t lineBreakStrategy = static_cast<int32_t>(LineBreakStrategy::GREEDY);
138     int32_t textAlign = 0;
139     std::string leadingMarginSize[2] = { "0.00px", "0.00px" };
140     std::vector<Shadow> textShadows;
141 };
142 
143 struct ImageStyleResult {
144     double size[2] = { 0.0, 0.0 };
145     int32_t verticalAlign = 0;
146     int32_t objectFit = 0;
147     std::string borderRadius;
148     std::string margin;
149 };
150 
151 struct ResultObject {
152     SpanPosition spanPosition;
153     SelectSpanType type = SelectSpanType::TYPESPAN;
154     int32_t offsetInSpan[2] = { 0, 0 };
155     std::string valueString;
156     std::string previewText;
157     RefPtr<PixelMap> valuePixelMap;
158     TextStyleResult textStyle;
159     ImageStyleResult imageStyle;
160     SymbolSpanStyle symbolSpanStyle;
161     RefPtr<ResourceObject> valueResource;
162     WeakPtr<NG::SpanItem> span;
163     bool isDraggable = true;
164     bool isInit = false;
165 };
166 
167 struct Selection {
168     int32_t selection[2] = { 0, 0 };
169     std::list<ResultObject> resultObjects;
170 };
171 
172 class SelectionInfo : public BaseEventInfo {
173     DECLARE_RELATIONSHIP_OF_CLASSES(SelectionInfo, BaseEventInfo);
174 
175 public:
SelectionInfo()176     SelectionInfo() : BaseEventInfo("SelectionInfo") {}
177 
178     ~SelectionInfo() = default;
179 
GetSelection()180     Selection GetSelection() const
181     {
182         return selection_;
183     }
184 
SetSelectionStart(int32_t start)185     void SetSelectionStart(int32_t start)
186     {
187         selection_.selection[RichEditorSpanRange::RANGESTART] = start;
188     }
189 
SetSelectionEnd(int32_t end)190     void SetSelectionEnd(int32_t end)
191     {
192         selection_.selection[RichEditorSpanRange::RANGEEND] = end;
193     }
194 
SetResultObjectList(const std::list<ResultObject> & resultObjectList)195     void SetResultObjectList(const std::list<ResultObject>& resultObjectList)
196     {
197         for (auto& resultObject : resultObjectList) {
198             selection_.resultObjects.emplace_back(resultObject);
199         }
200     }
201 
202 private:
203     Selection selection_;
204 };
205 
206 class SelectionRangeInfo : public BaseEventInfo {
207     DECLARE_RELATIONSHIP_OF_CLASSES(SelectionRangeInfo, BaseEventInfo);
208 
209 public:
SelectionRangeInfo(int32_t start,int32_t end)210     SelectionRangeInfo(int32_t start, int32_t end) : BaseEventInfo("SelectionRangeInfo"), start_(start), end_(end) {};
211 
212     ~SelectionRangeInfo() = default;
213 
214     int32_t start_;
215 
216     int32_t end_;
217 
reset()218     void reset()
219     {
220         start_ = -1;
221         end_ = -1;
222     }
223 
224     bool operator==(const SelectionRangeInfo& rhs) const
225     {
226         return start_ == rhs.start_ && end_ == rhs.end_;
227     }
228 };
229 
230 struct ParagraphInfo {
231     // style
232     RefPtr<PixelMap> leadingMarginPixmap;
233     std::string leadingMarginSize[2] = { "0.00px", "0.00px" };
234     int32_t textAlign = 0;
235     int32_t wordBreak = static_cast<int32_t>(WordBreak::BREAK_WORD);
236     int32_t lineBreakStrategy = static_cast<int32_t>(LineBreakStrategy::GREEDY);
237 
238     std::pair<int32_t, int32_t> range;
239 };
240 } // namespace OHOS::Ace
241 
242 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_RICH_EDITOR_RICH_EDITOR_SELECTION_H
243