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