diff --git a/interfaces/kits/napi/graphic/drawing/BUILD.gn b/interfaces/kits/napi/graphic/drawing/BUILD.gn index 525397ce2bbc8e126b2f33395886d8ec762b14d6..d1bf92a949e83072eb7a62e43ddd0f137a7b0c31 100644 --- a/interfaces/kits/napi/graphic/drawing/BUILD.gn +++ b/interfaces/kits/napi/graphic/drawing/BUILD.gn @@ -67,6 +67,7 @@ ohos_shared_library("drawing_napi_impl") { "font_napi/js_typeface.cpp", "image_filter_napi/js_image_filter.cpp", "js_common.cpp", + "js_common_init.cpp", "js_drawing_init.cpp", "js_drawing_utils.cpp", "lattice_napi/js_lattice.cpp", @@ -81,6 +82,7 @@ ohos_shared_library("drawing_napi_impl") { "shader_effect_napi/js_shader_effect.cpp", "shadow_layer_napi/js_shadow_layer.cpp", "text_blob_napi/js_text_blob.cpp", + "utils_napi/js_utils.cpp", ] defines = [] diff --git a/interfaces/kits/napi/graphic/drawing/drawing_module.cpp b/interfaces/kits/napi/graphic/drawing/drawing_module.cpp index ae2a898f968dea816f8a454d183f838a2b294d5c..c2a0ea145e8a797e7608b5f45ae1e8cae3c9ad32 100644 --- a/interfaces/kits/napi/graphic/drawing/drawing_module.cpp +++ b/interfaces/kits/napi/graphic/drawing/drawing_module.cpp @@ -15,8 +15,15 @@ #include +#include "js_common_init.h" #include "js_drawing_init.h" +static napi_module g_commonModule = { + .nm_filename = nullptr, + .nm_register_func = OHOS::Rosen::Drawing::CommonInit, + .nm_modname = "graphics.common2D", +}; + static napi_module g_drawingModule = { .nm_filename = "libdrawing_napi_register.so/drawing.js", .nm_register_func = OHOS::Rosen::Drawing::DrawingInit, @@ -25,5 +32,6 @@ static napi_module g_drawingModule = { extern "C" __attribute__((constructor)) void NAPI_drawing_AutoRegister() { + napi_module_register(&g_commonModule); napi_module_register(&g_drawingModule); } \ No newline at end of file diff --git a/interfaces/kits/napi/graphic/drawing/js_common_init.cpp b/interfaces/kits/napi/graphic/drawing/js_common_init.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ec7390ac7d6fbac96b2cc58176f6373c26163ccb --- /dev/null +++ b/interfaces/kits/napi/graphic/drawing/js_common_init.cpp @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "js_common_init.h" +#include "utils_napi/js_utils.h" + +namespace OHOS::Rosen { +namespace Drawing { +napi_value CommonInit(napi_env env, napi_value exportObj) +{ + JsUtils::Init(env, exportObj); + return exportObj; +} +} // namespace Drawing +} // namespace OHOS::Rosen diff --git a/interfaces/kits/napi/graphic/drawing/js_common_init.h b/interfaces/kits/napi/graphic/drawing/js_common_init.h new file mode 100644 index 0000000000000000000000000000000000000000..d4f2f5737ca5fb1118c1261adae141baef2c7e53 --- /dev/null +++ b/interfaces/kits/napi/graphic/drawing/js_common_init.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_JS_COMMON_INIT_H +#define OHOS_JS_COMMON_INIT_H + +#include + +#include "native_engine/native_engine.h" +#include "native_engine/native_value.h" + +#include "js_drawing_utils.h" + +namespace OHOS::Rosen { +namespace Drawing { +DRAWING_API napi_value CommonInit(napi_env env, napi_value exportObj); +} // namespace Drawing +} // namespace OHOS::Rosen +#endif // OHOS_JS_COMMON_INIT_H \ No newline at end of file diff --git a/interfaces/kits/napi/graphic/drawing/utils_napi/js_utils.cpp b/interfaces/kits/napi/graphic/drawing/utils_napi/js_utils.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c18f2fb50e54a5b5f8bb876a1348762bbde7e96c --- /dev/null +++ b/interfaces/kits/napi/graphic/drawing/utils_napi/js_utils.cpp @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "js_utils.h" + +#include "native_value.h" + +#include "js_drawing_utils.h" + +namespace OHOS::Rosen { +namespace Drawing { +thread_local napi_ref JsUtils::constructor_ = nullptr; +const std::string CLASS_NAME = "Utils"; +napi_value JsUtils::Init(napi_env env, napi_value exportObj) +{ + napi_property_descriptor properties[] = { + DECLARE_NAPI_STATIC_FUNCTION("joinRect", JsUtils::JoinRect), + }; + + napi_value constructor = nullptr; + napi_status status = napi_define_class(env, CLASS_NAME.c_str(), NAPI_AUTO_LENGTH, Constructor, nullptr, + sizeof(properties) / sizeof(properties[0]), properties, &constructor); + if (status != napi_ok) { + ROSEN_LOGE("JsUtils::Init: Failed to define JsUtils class"); + return nullptr; + } + + status = napi_create_reference(env, constructor, 1, &constructor_); + if (status != napi_ok) { + ROSEN_LOGE("JsUtils::Init: Failed to create reference of constructor"); + return nullptr; + } + + status = napi_set_named_property(env, exportObj, CLASS_NAME.c_str(), constructor); + if (status != napi_ok) { + ROSEN_LOGE("JsUtils::Init: Failed to set constructor"); + return nullptr; + } + + return exportObj; +} + +napi_value JsUtils::Constructor(napi_env env, napi_callback_info info) +{ + size_t argCount = 0; + napi_value jsThis = nullptr; + napi_status status = napi_get_cb_info(env, info, &argCount, nullptr, &jsThis, nullptr); + if (status != napi_ok) { + ROSEN_LOGE("JsUtils::Constructor: failed to napi_get_cb_info"); + return nullptr; + } + + JsUtils *jsUtils = new(std::nothrow) JsUtils(); + if (!jsUtils) { + ROSEN_LOGE("JsUtils::Constructor: Failed to create JsUtils"); + return nullptr; + } + + status = napi_wrap(env, jsThis, jsUtils, JsUtils::Destructor, nullptr, nullptr); + if (status != napi_ok) { + delete jsUtils; + ROSEN_LOGE("JsUtils::Constructor: Failed to wrap native instance"); + return nullptr; + } + return jsThis; +} + +void JsUtils::Destructor(napi_env env, void *nativeObject, void *finalize) +{ + (void)finalize; + if (nativeObject != nullptr) { + JsUtils *napi = reinterpret_cast(nativeObject); + delete napi; + } +} + +napi_value JsUtils::JoinRect(napi_env env, napi_callback_info info) +{ + napi_value argv[ARGC_TWO] = {nullptr}; + CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_TWO); + + double ltrb[ARGC_FOUR] = {0}; + if (!ConvertFromJsRect(env, argv[ARGC_ZERO], ltrb, ARGC_FOUR)) { + return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, + "Incorrect parameter 0 type. The type of left, top, right and bottom must be number."); + } + Drawing::Rect drawingRect = Drawing::Rect(ltrb[ARGC_ZERO], ltrb[ARGC_ONE], ltrb[ARGC_TWO], ltrb[ARGC_THREE]); + + double ltrb2[ARGC_FOUR] = {0}; + if (!ConvertFromJsRect(env, argv[ARGC_ONE], ltrb2, ARGC_FOUR)) { + return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, + "Incorrect parameter 1 type. The type of left, top, right and bottom must be number."); + } + Drawing::Rect otherRect = Drawing::Rect(ltrb2[ARGC_ZERO], ltrb2[ARGC_ONE], ltrb2[ARGC_TWO], ltrb2[ARGC_THREE]); + + bool joinResult = drawingRect.Join(otherRect); + if (joinResult) { + napi_set_named_property(env, argv[ARGC_ZERO], "left", CreateJsNumber(env, drawingRect.GetLeft())); + napi_set_named_property(env, argv[ARGC_ZERO], "top", CreateJsNumber(env, drawingRect.GetTop())); + napi_set_named_property(env, argv[ARGC_ZERO], "right", CreateJsNumber(env, drawingRect.GetRight())); + napi_set_named_property(env, argv[ARGC_ZERO], "bottom", CreateJsNumber(env, drawingRect.GetBottom())); + } + + return CreateJsValue(env, joinResult); +} +} // namespace Drawing +} // namespace OHOS::Rosen \ No newline at end of file diff --git a/interfaces/kits/napi/graphic/drawing/utils_napi/js_utils.h b/interfaces/kits/napi/graphic/drawing/utils_napi/js_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..0e8bb3208a5719e674e65881d676ee0048d8ec48 --- /dev/null +++ b/interfaces/kits/napi/graphic/drawing/utils_napi/js_utils.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_ROSEN_JS_UTILS_H +#define OHOS_ROSEN_JS_UTILS_H + +#include +#include + +#include "utils/rect.h" + +namespace OHOS::Rosen { +namespace Drawing { +class JsUtils final { +public: + JsUtils() = default; + ~JsUtils() = default; + + static napi_value Init(napi_env env, napi_value exportObj); + static napi_value Constructor(napi_env env, napi_callback_info info); + static void Destructor(napi_env env, void *nativeObject, void *finalize); + + static napi_value JoinRect(napi_env env, napi_callback_info info); + +private: + static thread_local napi_ref constructor_; +}; +} // namespace Drawing +} // namespace OHOS::Rosen +#endif // OHOS_ROSEN_JS_UTILS_H \ No newline at end of file