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> CreateJsTextShadowSpan(const RefPtr<SpanBase>& spanObject); 70 static RefPtr<SpanBase> ParseJsTextShadowSpan(int32_t start, int32_t length, const JSRef<JSObject>& obj); 71 72 static JSRef<JSObject> CreateJsLineHeightSpan(const RefPtr<SpanBase>& spanObject); 73 static RefPtr<SpanBase> ParseJsLineHeightSpan(int32_t start, int32_t length, const JSRef<JSObject>& obj); 74 75 static JSRef<JSObject> CreateJsImageSpan(const RefPtr<SpanBase>& spanObject); 76 static RefPtr<SpanBase> GetImageAttachment(int32_t start, int32_t length, const JSRef<JSObject>& obj); 77 static ImageSpanOptions ParseJsImageAttachment(const JSRef<JSObject>& obj); 78 79 static RefPtr<CustomSpan> ParseJsCustomSpan(const JSCallbackInfo& args); 80 static RefPtr<SpanBase> ParseJsCustomSpan(int32_t start, int32_t length, const JSCallbackInfo& args); 81 82 static RefPtr<SpanBase> ParseJsExtSpan(int32_t start, int32_t length, const JSRef<JSObject>& obj); 83 84 static bool CheckSpanType(int32_t spanType); 85 bool CheckParameters(int32_t start, int32_t length); 86 static JSRef<JSObject> CreateJsParagraphStyleSpan(const RefPtr<SpanBase>& spanObject); 87 static RefPtr<SpanBase> ParseJsParagraphStyleSpan(int32_t start, int32_t length, const JSRef<JSObject>& obj); 88 void GetSpans(const JSCallbackInfo& info); 89 const RefPtr<SpanString>& GetController(); 90 void SetController(const RefPtr<SpanString>& spanString); 91 92 static void FromHtml(const JSCallbackInfo& info); 93 static void Marshalling(const JSCallbackInfo& info); 94 static void Unmarshalling(const JSCallbackInfo& info); 95 private: 96 static void UnmarshallingExec(napi_env env, void *data); 97 static void UnmarshallingComplete(napi_env env, napi_status status, void *data); 98 ACE_DISALLOW_COPY_AND_MOVE(JSSpanString); 99 RefPtr<SpanString> spanString_; 100 }; 101 102 class JSMutableSpanString final : public JSSpanString { 103 public: 104 JSMutableSpanString() = default; 105 ~JSMutableSpanString() override = default; 106 static void Constructor(const JSCallbackInfo& args); 107 static void Destructor(JSMutableSpanString* spanString); 108 static void JSBind(BindingTarget globalObj); 109 110 void ReplaceString(const JSCallbackInfo& info); 111 void InsertString(const JSCallbackInfo& info); 112 void RemoveString(const JSCallbackInfo& info); 113 void ReplaceSpan(const JSCallbackInfo& info); 114 void AddSpan(const JSCallbackInfo& info); 115 void RemoveSpan(const JSCallbackInfo& info); 116 void RemoveSpans(const JSCallbackInfo& info); 117 void ClearAllSpans(); 118 void ReplaceSpanString(const JSCallbackInfo& info); 119 void InsertSpanString(const JSCallbackInfo& info); 120 void AppendSpanString(const JSCallbackInfo& info); 121 WeakPtr<MutableSpanString>& GetMutableController(); 122 void SetMutableController(const RefPtr<MutableSpanString>& mutableSpanString); 123 124 private: 125 bool IsImageNode(int32_t location); 126 bool IsCustomSpanNode(int32_t location); 127 bool VerifyImageParameters(int32_t start, int32_t length); 128 bool VerifyCustomSpanParameters(int32_t start, int32_t length); 129 130 ACE_DISALLOW_COPY_AND_MOVE(JSMutableSpanString); 131 WeakPtr<MutableSpanString> mutableSpanString_; 132 }; 133 134 } // namespace OHOS::Ace::Framework 135 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_STYLE_STRING_JS_SPAN_STRING_H