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 FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_STYLE_STRING_JS_SPAN_STRING_H
17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_STYLE_STRING_JS_SPAN_STRING_H
18 
19 #include "base/geometry/dimension.h"
20 #include "base/memory/ace_type.h"
21 #include "base/memory/referenced.h"
22 #include "bridge/declarative_frontend/engine/bindings_defines.h"
23 #include "bridge/declarative_frontend/engine/js_types.h"
24 #include "bridge/declarative_frontend/jsview/js_container_base.h"
25 #include "bridge/declarative_frontend/style_string/js_span_object.h"
26 #include "core/components_ng/pattern/text/span/mutable_span_string.h"
27 #include "core/components_ng/pattern/text/span/span_string.h"
28 #include "core/pipeline/pipeline_base.h"
29 namespace OHOS::Ace::Framework {
30 
31 class JSSpanString : public virtual AceType {
32     DECLARE_ACE_TYPE(JSSpanString, AceType)
33 
34 public:
35     JSSpanString() = default;
36     ~JSSpanString() override = default;
37     static void Constructor(const JSCallbackInfo& args);
38     static void Destructor(JSSpanString* spanString);
39     static void JSBind(BindingTarget globalObj);
40     void GetString(const JSCallbackInfo& info);
41     void GetLength(const JSCallbackInfo& info);
42     void SetLength(const JSCallbackInfo& info);
43     void IsEqualToSpanString(const JSCallbackInfo& info);
44     void GetSubSpanString(const JSCallbackInfo& info);
45     static std::vector<RefPtr<SpanBase>> ParseJsSpanBaseVector(const JSRef<JSObject>& obj, int32_t maxLength,
46         JsiRef<JsiObject> thisObj);
47 
48     static JSRef<JSObject> CreateJsSpanBaseObject(const RefPtr<SpanBase>& spanObject);
49     static JSRef<JSObject> CreateJsSpanObject(const RefPtr<SpanBase>& spanObject);
50     static RefPtr<SpanBase> ParseJsSpanBase(int32_t start, int32_t length, SpanType type, const JSRef<JSObject>& obj);
51     static RefPtr<SpanBase> ParseJsSpanBaseWithoutSpecialSpan(
52         int32_t start, int32_t length, SpanType type, const JSRef<JSObject>& obj, const JSCallbackInfo& info);
53 
54     static JSRef<JSObject> CreateJsFontSpan(const RefPtr<SpanBase>& spanObject);
55     static RefPtr<SpanBase> ParseJsFontSpan(int32_t start, int32_t length, const JSRef<JSObject>& obj);
56 
57     static JSRef<JSObject> CreateJsDecorationSpan(const RefPtr<SpanBase>& spanObject);
58     static RefPtr<SpanBase> ParseJsDecorationSpan(int32_t start, int32_t length, const JSRef<JSObject>& obj);
59 
60     static JSRef<JSObject> CreateJsLetterSpacingSpan(const RefPtr<SpanBase>& spanObject);
61     static RefPtr<SpanBase> ParseJsLetterSpacingSpan(int32_t start, int32_t length, const JSRef<JSObject>& obj);
62 
63     static JSRef<JSObject> CreateJsBaselineOffsetSpan(const RefPtr<SpanBase>& spanObject);
64     static RefPtr<SpanBase> ParseJsBaselineOffsetSpan(int32_t start, int32_t length, const JSRef<JSObject>& obj);
65 
66     static JSRef<JSObject> CreateJsGestureSpan(const RefPtr<SpanBase>& spanObject);
67     static RefPtr<SpanBase> ParseJsGestureSpan(int32_t start, int32_t length, const JSRef<JSObject>& obj);
68 
69     static JSRef<JSObject> CreateJSBackgroundColorSpan(const RefPtr<SpanBase>& spanObject);
70     static RefPtr<SpanBase> ParseJSBackgroundColorSpan(int32_t start, int32_t length, const JSRef<JSObject>& obj);
71 
72     static JSRef<JSObject> CreateJsTextShadowSpan(const RefPtr<SpanBase>& spanObject);
73     static RefPtr<SpanBase> ParseJsTextShadowSpan(int32_t start, int32_t length, const JSRef<JSObject>& obj);
74 
75     static JSRef<JSObject> CreateJsLineHeightSpan(const RefPtr<SpanBase>& spanObject);
76     static RefPtr<SpanBase> ParseJsLineHeightSpan(int32_t start, int32_t length, const JSRef<JSObject>& obj);
77 
78     static JSRef<JSObject> CreateJsImageSpan(const RefPtr<SpanBase>& spanObject);
79     static RefPtr<SpanBase> GetImageAttachment(int32_t start, int32_t length, const JSRef<JSObject>& obj);
80     static ImageSpanOptions ParseJsImageAttachment(const JSRef<JSObject>& obj);
81 
82     static RefPtr<CustomSpan> ParseJsCustomSpan(const JSCallbackInfo& args);
83     static RefPtr<SpanBase> ParseJsCustomSpan(int32_t start, int32_t length, const JSCallbackInfo& args);
84 
85     static RefPtr<SpanBase> ParseJsExtSpan(int32_t start, int32_t length, const JSRef<JSObject>& obj);
86 
87     static JSRef<JSObject> CreateJsUrlSpan(const RefPtr<SpanBase>& spanObject);
88     static RefPtr<SpanBase> ParseJsUrlSpan(int32_t start, int32_t length, const JSRef<JSObject>& obj);
89 
90     static bool CheckSpanType(int32_t spanType);
91     bool CheckParameters(int32_t start, int32_t length);
92     static JSRef<JSObject> CreateJsParagraphStyleSpan(const RefPtr<SpanBase>& spanObject);
93     static RefPtr<SpanBase> ParseJsParagraphStyleSpan(int32_t start, int32_t length, const JSRef<JSObject>& obj);
94     void GetSpans(const JSCallbackInfo& info);
95     const RefPtr<SpanString>& GetController();
96     void SetController(const RefPtr<SpanString>& spanString);
97 
98     static void FromHtml(const JSCallbackInfo& info);
99     static void ToHtml(const JSCallbackInfo& info);
100     static void Marshalling(const JSCallbackInfo& info);
101     static void Unmarshalling(const JSCallbackInfo& info);
102 private:
103     static void UnmarshallingExec(napi_env env, void *data);
104     static void UnmarshallingComplete(napi_env env, napi_status status, void *data);
105     ACE_DISALLOW_COPY_AND_MOVE(JSSpanString);
106     RefPtr<SpanString> spanString_;
107 };
108 
109 class JSMutableSpanString final : public JSSpanString {
110 public:
111     JSMutableSpanString() = default;
112     ~JSMutableSpanString() override = default;
113     static void Constructor(const JSCallbackInfo& args);
114     static void Destructor(JSMutableSpanString* spanString);
115     static void JSBind(BindingTarget globalObj);
116 
117     void ReplaceString(const JSCallbackInfo& info);
118     void InsertString(const JSCallbackInfo& info);
119     void RemoveString(const JSCallbackInfo& info);
120     void ReplaceSpan(const JSCallbackInfo& info);
121     void AddSpan(const JSCallbackInfo& info);
122     void RemoveSpan(const JSCallbackInfo& info);
123     void RemoveSpans(const JSCallbackInfo& info);
124     void ClearAllSpans();
125     void ReplaceSpanString(const JSCallbackInfo& info);
126     void InsertSpanString(const JSCallbackInfo& info);
127     void AppendSpanString(const JSCallbackInfo& info);
128     WeakPtr<MutableSpanString>& GetMutableController();
129     void SetMutableController(const RefPtr<MutableSpanString>& mutableSpanString);
130 
131 private:
132     bool IsImageNode(int32_t location);
133     bool IsCustomSpanNode(int32_t location);
134     bool VerifyImageParameters(int32_t start, int32_t length);
135     bool VerifyCustomSpanParameters(int32_t start, int32_t length);
136 
137     ACE_DISALLOW_COPY_AND_MOVE(JSMutableSpanString);
138     WeakPtr<MutableSpanString> mutableSpanString_;
139 };
140 
141 } // namespace OHOS::Ace::Framework
142 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_STYLE_STRING_JS_SPAN_STRING_H