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 #ifndef OHOS_ACE_FRAMEWORK_CJ_RICHEDITOR_CONTROLLER_FFI_H
16 #define OHOS_ACE_FRAMEWORK_CJ_RICHEDITOR_CONTROLLER_FFI_H
17 
18 #include <cinttypes>
19 #include <optional>
20 #include <string>
21 
22 #include "bridge/cj_frontend/interfaces/cj_ffi/cj_macro.h"
23 #include "bridge/cj_frontend/interfaces/cj_ffi/cj_view_abstract_ffi.h"
24 #include "core/components_ng/pattern/rich_editor/rich_editor_model.h"
25 #include "core/components_ng/pattern/rich_editor/rich_editor_model_ng.h"
26 #include "ffi_remote_data.h"
27 
28 extern "C" {
29 struct NativeTextDecoration {
30     int32_t type;
31     uint32_t color;
32 };
33 
34 struct NativeRichEditorTextStyle {
35     uint32_t fontColor;
36     double fontSize;
37     int32_t fontSizeUnit;
38     int32_t fontStyle;
39     const char* fontWeight;
40     const char* fontFamily;
41     NativeTextDecoration decoration;
42 };
43 
44 struct NativeRichEditorTextSpanOptions {
45     int32_t offset;
46     NativeRichEditorTextStyle style;
47 };
48 
49 struct NativeMargin {
50     double top;
51     int32_t topUnit;
52     double right;
53     int32_t rightUnit;
54     double bottom;
55     int32_t bottomUnit;
56     double left;
57     int32_t leftUnit;
58 };
59 
60 struct NativeBorderRadiuses {
61     double top;
62     int32_t topUnit;
63     double right;
64     int32_t rightUnit;
65     double bottom;
66     int32_t bottomUnit;
67     double left;
68     int32_t leftUnit;
69 };
70 
71 struct NativeLayoutStyle {
72     NativeMargin margin;
73     NativeBorderRadiuses borderRadius;
74 };
75 
76 struct LengthOption {
77     double value;
78     int32_t unit;
79     bool hasValue;
80 };
81 
82 struct NativeRichEditorImageSpanStyle {
83     LengthOption width;
84     LengthOption height;
85     int32_t verticalAlign;
86     int32_t objectFit;
87     NativeLayoutStyle layoutStyle;
88 };
89 
90 struct NativeRichEditorImageSpanOptions {
91     int32_t offset;
92     NativeRichEditorImageSpanStyle imageStyle;
93 };
94 
95 struct NativeRichEditorSpanPosition {
96     int32_t spanIndex;
97     int32_t spanStart;
98     int32_t spanEnd;
99 };
100 
101 struct NativeTextDecorationResult {
102     int32_t type;
103     const char* color;
104 };
105 
106 struct NativeRichEditorTextStyleResult {
107     const char* fontColor;
108     double fontSize;
109     int32_t fontStyle;
110     int32_t fontWeight;
111     const char* fontFamily;
112     NativeTextDecorationResult decoration;
113 };
114 
115 struct NativeRichEditorTextSpanResult {
116     NativeRichEditorSpanPosition spanPosition;
117     const char* value;
118     NativeRichEditorTextStyleResult textStyle;
119     int32_t offsetInSpanStart;
120     int32_t offsetInSpanEnd;
121 };
122 
123 struct NativeRichEditorImageSpanResult {
124     double width;
125     double height;
126     int32_t verticalAlign;
127     int32_t objectFit;
128 };
129 
130 struct NativeRichEditorSpanResult {
131     bool isText;
132     NativeRichEditorTextSpanResult textResult;
133     NativeRichEditorImageSpanResult imageResult;
134 };
135 
136 struct NativeRichEditorSpanResultList {
137     NativeRichEditorSpanResult* array;
138     int64_t size;
139     void (*free)(int64_t, NativeRichEditorSpanResult*);
140 };
141 
142 struct NativeRichEditorSelection {
143     int32_t selectionStart;
144     int32_t selectionEnd;
145     NativeRichEditorSpanResult* spans;
146     int64_t spanSize;
147 };
148 
149 struct NativeRichEditorDeleteValue {
150     int32_t offset;
151     int32_t direction;
152     int32_t length;
153     NativeRichEditorSpanResult* richEditorDeleteSpans;
154     int64_t spanSize;
155 };
156 
157 struct NativeLeadingMarginPlaceholder {
158     int64_t pixelMap;
159     double width;
160     int32_t widthUnit;
161     double height;
162     int32_t heightUnit;
163 };
164 
165 struct NativeRichEditorParagraphStyle {
166     int32_t textAlign;
167     int32_t marginType;
168     double margin;
169     int32_t marginUnit;
170     NativeLeadingMarginPlaceholder placeholder;
171 };
172 
173 CJ_EXPORT int64_t FfiOHOSAceFrameworkRichEditorControllerCtor();
174 CJ_EXPORT int64_t FfiOHOSAceFrameworkRichEditorControllerGetCaretOffset(int64_t id);
175 CJ_EXPORT bool FfiOHOSAceFrameworkRichEditorControllerSetCaretOffset(int64_t id, int64_t value);
176 CJ_EXPORT int32_t FfiOHOSAceFrameworkRichEditorControllerAddTextSpan(int64_t id, const char* value,
177     NativeRichEditorTextSpanOptions params);
178 CJ_EXPORT int32_t FfiOHOSAceFrameworkRichEditorControllerAddImageSpan(int64_t id, const char* value,
179     NativeRichEditorImageSpanOptions params);
180 CJ_EXPORT void FfiOHOSAceFrameworkRichEditorControllerDeleteSpans(int64_t id, int32_t start,  int32_t end);
181 CJ_EXPORT void FfiOHOSAceFrameworkRichEditorControllerDeleteAllSpans(int64_t id);
182 CJ_EXPORT void FfiOHOSAceFrameworkRichEditorControllerCloseSelectionMenu(int64_t id);
183 CJ_EXPORT void FfiOHOSAceFrameworkRichEditorControllerUpdateSpanStyleText(int64_t id, int32_t start,
184     int32_t end, NativeRichEditorTextStyle params);
185 CJ_EXPORT void FfiOHOSAceFrameworkRichEditorControllerUpdateSpanStyleImage(int64_t id, int32_t start,
186     int32_t end, NativeRichEditorImageSpanStyle params);
187 CJ_EXPORT NativeRichEditorSpanResultList FfiOHOSAceFrameworkRichEditorControllerGetSpans(int64_t id,
188     int32_t start, int32_t end);
189 CJ_EXPORT void FfiOHOSAceFrameworkRichEditorControllerUpdateParagraphStyle(int64_t id, int32_t start,
190     int32_t end, NativeRichEditorParagraphStyle params);
191 }
192 
193 namespace OHOS::Ace::Framework {
194 
195 enum MarginType {
196     NONE = 0,
197     MARGIN_LENGTH = 1,
198     MARGIN_PLACEHOLDER = 2
199 };
200 
201 class ACE_EXPORT NativeRichEditorController : public OHOS::FFI::FFIData {
202     DECL_TYPE(NativeRichEditorController, OHOS::FFI::FFIData)
203 public:
204     NativeRichEditorController();
205 
SetController(const RefPtr<RichEditorBaseControllerBase> & controller)206     void SetController(const RefPtr<RichEditorBaseControllerBase>& controller)
207     {
208         controller_ = controller;
209     }
210 
211     int32_t GetCaretOffset();
212 
213     bool SetCaretOffset(int32_t value);
214 
215     int32_t AddTextSpan(std::string value, NativeRichEditorTextSpanOptions params);
216 
217     int32_t AddImageSpan(std::string value, NativeRichEditorImageSpanOptions params);
218 
219     void DeleteSpans(int32_t start, int32_t end);
220 
221     void DeleteAllSpans();
222 
223     void CloseSelectionMenu();
224 
225     void UpdateSpanStyleText(int32_t start, int32_t end, NativeRichEditorTextStyle style);
226 
227     void UpdateSpanStyleImage(int32_t start, int32_t end, NativeRichEditorImageSpanStyle style);
228 
229     NativeRichEditorSpanResultList GetSpans(int32_t start, int32_t end);
230 
231     void UpdateParagraphStyle(int32_t start, int32_t end, NativeRichEditorParagraphStyle params);
232 
233     void ParseImageSpanStyle(const NativeRichEditorImageSpanStyle& nativeStyle, ImageSpanAttribute& imageStyle);
234 
235     void ParseTextStyle(const NativeRichEditorTextStyle& nativeStyle, TextStyle& style);
236 
237     static void ParseTextStyleResult(const TextStyleResult& textStyle,
238         NativeRichEditorTextStyleResult& nativeTextStyle);
239 
240     static void ParseRichEditorTextSpanResult(const ResultObject& spanObject,
241         NativeRichEditorTextSpanResult& nativeTextResult);
242 
243     static void ParseRichEditorImageSpanResult(const ResultObject& spanObject,
244         NativeRichEditorImageSpanResult& nativeImageResult);
245 
246     static void ParseRichEditorAbstractTextSpanResult(const NG::RichEditorAbstractSpanResult& spanObject,
247         NativeRichEditorTextSpanResult& nativeTextResult);
248 
249     static void ParseRichEditorAbstractImageSpanResult(const NG::RichEditorAbstractSpanResult& spanObject,
250         NativeRichEditorImageSpanResult& nativeImageResult);
251 
SetInstanceId(int32_t id)252     void SetInstanceId(int32_t id)
253     {
254         instanceId_ = id;
255     }
256 
257 private:
258     WeakPtr<RichEditorBaseControllerBase> controller_;
259     struct UpdateSpanStyle updateSpanStyle_;
260     int32_t instanceId_ = INSTANCE_ID_UNDEFINED;
261 };
262 } // namespace OHOS::Ace::Framework
263 
264 #endif //OHOS_ACE_FRAMEWORK_CJ_RICHEDITOR_CONTROLLER_FFI_H