mirror of
https://gitee.com/openharmony/graphic_graphic_2d
synced 2024-11-30 10:41:29 +00:00
!9910 add paragraph_builder interface
Merge pull request !9910 from changleipeng/0410yqf
This commit is contained in:
commit
d238749263
@ -30,6 +30,7 @@ config("local_text") {
|
||||
|
||||
include_dirs = [
|
||||
"../text",
|
||||
"../drawing",
|
||||
"$graphic_2d_root/rosen/modules/2d_engine/rosen_text/export/rosen_text",
|
||||
"$graphic_2d_root/rosen/modules/2d_graphics/include",
|
||||
]
|
||||
@ -56,6 +57,8 @@ ohos_shared_library("text_napi_impl") {
|
||||
"fontcollection_napi/js_fontcollection.cpp",
|
||||
"js_text_init.cpp",
|
||||
"js_text_utils.cpp",
|
||||
"paragraph_builder_napi/js_paragraph_builder.cpp",
|
||||
"paragraph_napi/js_paragraph.cpp",
|
||||
"paragraph_style_napi/js_paragraphstyle.cpp",
|
||||
]
|
||||
|
||||
@ -65,6 +68,7 @@ ohos_shared_library("text_napi_impl") {
|
||||
]
|
||||
|
||||
deps = [
|
||||
"$graphic_2d_root/interfaces/kits/napi/graphic/drawing:drawing_napi_impl",
|
||||
"$graphic_2d_root/rosen/modules/2d_graphics:2d_graphics",
|
||||
"$graphic_2d_root/rosen/modules/texgine:libtexgine_source",
|
||||
]
|
||||
|
@ -14,10 +14,11 @@
|
||||
*/
|
||||
|
||||
#include "js_text_init.h"
|
||||
|
||||
#include "enum_napi/text_enum_napi.h"
|
||||
#include "fontcollection_napi/js_fontcollection.h"
|
||||
#include "paragraph_builder_napi/js_paragraph_builder.h"
|
||||
#include "paragraph_style_napi/js_paragraphstyle.h"
|
||||
#include "paragraph_napi/js_paragraph.h"
|
||||
#include "utils/log.h"
|
||||
|
||||
namespace OHOS::Rosen {
|
||||
@ -25,6 +26,8 @@ napi_value TextInit(napi_env env, napi_value exportObj)
|
||||
{
|
||||
JsFontCollection::Init(env, exportObj);
|
||||
JsEnum::Init(env, exportObj);
|
||||
JsParagraphBuilder::Init(env, exportObj);
|
||||
JsParagraph::Init(env, exportObj);
|
||||
return exportObj;
|
||||
}
|
||||
} // namespace OHOS::Rosen
|
||||
|
@ -0,0 +1,238 @@
|
||||
/*
|
||||
* 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 "../drawing/js_drawing_utils.h"
|
||||
#include "fontcollection_napi/js_fontcollection.h"
|
||||
#include "js_paragraph_builder.h"
|
||||
#include "paragraph_napi/js_paragraph.h"
|
||||
#include "../text/js_text_utils.h"
|
||||
#include "utils/log.h"
|
||||
|
||||
namespace OHOS::Rosen {
|
||||
std::unique_ptr<Typography> drawingTypography;
|
||||
thread_local napi_ref JsParagraphBuilder::constructor_ = nullptr;
|
||||
const std::string CLASS_NAME = "ParagraphBuilder";
|
||||
napi_value JsParagraphBuilder::Constructor(napi_env env, napi_callback_info info)
|
||||
{
|
||||
size_t argCount = ARGC_TWO;
|
||||
napi_value jsThis = nullptr;
|
||||
napi_value argv[ARGC_TWO] = {nullptr};
|
||||
napi_status status = napi_get_cb_info(env, info, &argCount, argv, &jsThis, nullptr);
|
||||
if (status != napi_ok || argCount < ARGC_ONE || argCount > ARGC_TWO) {
|
||||
ROSEN_LOGE("JsParagraphBuilder::Constructor Argc is invalid: %{public}zu", argCount);
|
||||
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
|
||||
}
|
||||
TypographyStyle typographyStyle;
|
||||
GetParagraphStyleFromJS(env, argv[0], typographyStyle);
|
||||
JsFontCollection* jsFontCollection = nullptr;
|
||||
status = napi_unwrap(env, argv[1], reinterpret_cast<void**>(&jsFontCollection));
|
||||
|
||||
if (jsFontCollection == nullptr) {
|
||||
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
|
||||
}
|
||||
if (status != napi_ok) {
|
||||
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Get jsFontCollection error.");
|
||||
}
|
||||
|
||||
std::shared_ptr<FontCollection> fontCollection = jsFontCollection->GetFontCollection();
|
||||
std::unique_ptr<TypographyCreate> typographyCreate = TypographyCreate::Create(typographyStyle, fontCollection);
|
||||
|
||||
if (!typographyCreate) {
|
||||
ROSEN_LOGE("JsParagraphBuilder::Constructor TypographyCreate Create error");
|
||||
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "TypographyCreate Create error.");
|
||||
}
|
||||
JsParagraphBuilder* jsParagraphBuilder = new(std::nothrow) JsParagraphBuilder();
|
||||
if (!jsParagraphBuilder) {
|
||||
ROSEN_LOGE("JsParagraphBuilder::Constructor jsParagraphBuilder Create error");
|
||||
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "JsParagraphBuilder Create error.");
|
||||
}
|
||||
jsParagraphBuilder->SetTypographyCreate(std::move(typographyCreate));
|
||||
|
||||
status = napi_wrap(env, jsThis, jsParagraphBuilder,
|
||||
JsParagraphBuilder::Destructor, nullptr, nullptr);
|
||||
if (status != napi_ok) {
|
||||
delete jsParagraphBuilder;
|
||||
ROSEN_LOGE("JsParagraphBuilder::Constructor Failed to wrap native instance");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return jsThis;
|
||||
}
|
||||
|
||||
void JsParagraphBuilder::SetTypographyCreate(std::unique_ptr<TypographyCreate> typographyCreate)
|
||||
{
|
||||
typographyCreate_ = std::move(typographyCreate);
|
||||
}
|
||||
|
||||
napi_value JsParagraphBuilder::Init(napi_env env, napi_value exportObj)
|
||||
{
|
||||
napi_property_descriptor properties[] = {
|
||||
DECLARE_NAPI_FUNCTION("pushStyle", JsParagraphBuilder::PushStyle),
|
||||
DECLARE_NAPI_FUNCTION("addText", JsParagraphBuilder::AddText),
|
||||
DECLARE_NAPI_FUNCTION("popStyle", JsParagraphBuilder::PopStyle),
|
||||
DECLARE_NAPI_FUNCTION("addPlaceholder", JsParagraphBuilder::AddPlaceholder),
|
||||
DECLARE_NAPI_FUNCTION("build", JsParagraphBuilder::Build),
|
||||
};
|
||||
|
||||
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("JsParagraphBuilder::Init Failed to define FontCollection class");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
status = napi_create_reference(env, constructor, 1, &constructor_);
|
||||
if (status != napi_ok) {
|
||||
ROSEN_LOGE("JsParagraphBuilder::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("JsParagraphBuilder::Init Failed to set constructor");
|
||||
return nullptr;
|
||||
}
|
||||
return exportObj;
|
||||
}
|
||||
|
||||
void JsParagraphBuilder::Destructor(napi_env env, void* nativeObject, void* finalize)
|
||||
{
|
||||
(void)finalize;
|
||||
if (nativeObject != nullptr) {
|
||||
JsParagraphBuilder* napi = reinterpret_cast<JsParagraphBuilder *>(nativeObject);
|
||||
delete napi;
|
||||
}
|
||||
}
|
||||
|
||||
napi_value JsParagraphBuilder::PushStyle(napi_env env, napi_callback_info info)
|
||||
{
|
||||
JsParagraphBuilder* me = CheckParamsAndGetThis<JsParagraphBuilder>(env, info);
|
||||
return (me != nullptr) ? me->OnPushStyle(env, info) : nullptr;
|
||||
}
|
||||
|
||||
napi_value JsParagraphBuilder::OnPushStyle(napi_env env, napi_callback_info info)
|
||||
{
|
||||
if (typographyCreate_ == nullptr) {
|
||||
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
|
||||
}
|
||||
size_t argc = ARGC_ONE;
|
||||
napi_value argv[ARGC_ONE] = {nullptr};
|
||||
napi_status status = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
|
||||
if (status != napi_ok || argc < ARGC_ONE) {
|
||||
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
|
||||
}
|
||||
napi_valuetype valueType = napi_undefined;
|
||||
if (argv[0] == nullptr || napi_typeof(env, argv[0], &valueType) != napi_ok || valueType != napi_object) {
|
||||
return NapiGetUndefined(env);
|
||||
}
|
||||
TextStyle textStyle;
|
||||
if (GetTextStyleFromJS(env, argv[0], textStyle)) {
|
||||
typographyCreate_->PushStyle(textStyle);
|
||||
}
|
||||
return NapiGetUndefined(env);
|
||||
}
|
||||
|
||||
napi_value JsParagraphBuilder::AddText(napi_env env, napi_callback_info info)
|
||||
{
|
||||
JsParagraphBuilder* me = CheckParamsAndGetThis<JsParagraphBuilder>(env, info);
|
||||
return (me != nullptr) ? me->OnAddText(env, info) : nullptr;
|
||||
}
|
||||
napi_value JsParagraphBuilder::OnAddText(napi_env env, napi_callback_info info)
|
||||
{
|
||||
if (typographyCreate_ == nullptr) {
|
||||
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
|
||||
}
|
||||
size_t argc = ARGC_ONE;
|
||||
napi_value argv[ARGC_ONE] = {nullptr};
|
||||
napi_status status = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
|
||||
if (status != napi_ok || argc < ARGC_ONE) {
|
||||
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
|
||||
}
|
||||
napi_valuetype valueType = napi_undefined;
|
||||
if (argv[0] == nullptr || napi_typeof(env, argv[0], &valueType) != napi_ok) {
|
||||
return NapiGetUndefined(env);
|
||||
}
|
||||
std::string text = "";
|
||||
if (ConvertFromJsValue(env, argv[0], text)) {
|
||||
typographyCreate_->AppendText(Str8ToStr16(text));
|
||||
}
|
||||
return NapiGetUndefined(env);
|
||||
}
|
||||
|
||||
napi_value JsParagraphBuilder::PopStyle(napi_env env, napi_callback_info info)
|
||||
{
|
||||
JsParagraphBuilder* me = CheckParamsAndGetThis<JsParagraphBuilder>(env, info);
|
||||
return (me != nullptr) ? me->OnPopStyle(env, info) : nullptr;
|
||||
}
|
||||
|
||||
napi_value JsParagraphBuilder::OnPopStyle(napi_env env, napi_callback_info info)
|
||||
{
|
||||
if (typographyCreate_ == nullptr) {
|
||||
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
|
||||
}
|
||||
typographyCreate_->PopStyle();
|
||||
return NapiGetUndefined(env);
|
||||
}
|
||||
|
||||
napi_value JsParagraphBuilder::AddPlaceholder(napi_env env, napi_callback_info info)
|
||||
{
|
||||
JsParagraphBuilder* me = CheckParamsAndGetThis<JsParagraphBuilder>(env, info);
|
||||
return (me != nullptr) ? me->OnAddPlaceholder(env, info) : nullptr;
|
||||
}
|
||||
|
||||
napi_value JsParagraphBuilder::OnAddPlaceholder(napi_env env, napi_callback_info info)
|
||||
{
|
||||
if (typographyCreate_ == nullptr) {
|
||||
ROSEN_LOGE("OnAddPlaceholder typographyCreate_ is null");
|
||||
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
|
||||
}
|
||||
size_t argc = ARGC_ONE;
|
||||
napi_value argv[ARGC_ONE] = {nullptr};
|
||||
napi_status status = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
|
||||
if (status != napi_ok || argc < ARGC_ONE) {
|
||||
ROSEN_LOGE("JsParagraphBuilder::AddPlaceholder Argc is invalid: %{public}zu", argc);
|
||||
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
|
||||
}
|
||||
napi_valuetype valueType = napi_undefined;
|
||||
if (argv[0] == nullptr || napi_typeof(env, argv[0], &valueType) != napi_ok || valueType != napi_object) {
|
||||
ROSEN_LOGE("JsParagraphBuilder::AddPlaceholder Argv[0] is invalid");
|
||||
return NapiGetUndefined(env);
|
||||
}
|
||||
PlaceholderSpan placeholderSpan;
|
||||
bool res = GetPlaceholderSpanFromJS(env, argv[0], placeholderSpan);
|
||||
if (!res) {
|
||||
return NapiGetUndefined(env);
|
||||
}
|
||||
typographyCreate_->AppendPlaceholder(placeholderSpan);
|
||||
return NapiGetUndefined(env);
|
||||
}
|
||||
|
||||
napi_value JsParagraphBuilder::Build(napi_env env, napi_callback_info info)
|
||||
{
|
||||
JsParagraphBuilder* me = CheckParamsAndGetThis<JsParagraphBuilder>(env, info);
|
||||
return (me != nullptr) ? me->OnBuild(env, info) : nullptr;
|
||||
}
|
||||
napi_value JsParagraphBuilder::OnBuild(napi_env env, napi_callback_info info)
|
||||
{
|
||||
if (typographyCreate_ == nullptr) {
|
||||
ROSEN_LOGE("JsParagraphBuilder::OnAddPlaceholder typographyCreate_ is null");
|
||||
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
|
||||
}
|
||||
|
||||
std::unique_ptr<OHOS::Rosen::Typography> typography = typographyCreate_->CreateTypography();
|
||||
return JsParagraph::CreateJsTypography(env, std::move(typography));
|
||||
}
|
||||
|
||||
} // namespace OHOS::Rosen
|
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* 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_PARAGRAPH_BUILDER_H
|
||||
#define OHOS_ROSEN_JS_PARAGRAPH_BUILDER_H
|
||||
|
||||
#include "font_collection.h"
|
||||
#include <native_engine/native_engine.h>
|
||||
#include <native_engine/native_value.h>
|
||||
#include "text_style.h"
|
||||
#include "typography_create.h"
|
||||
|
||||
|
||||
namespace OHOS::Rosen {
|
||||
class JsParagraphBuilder final {
|
||||
public:
|
||||
JsParagraphBuilder() {}
|
||||
void SetTypographyCreate(std::unique_ptr<TypographyCreate> typographyCreate);
|
||||
|
||||
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 PushStyle(napi_env env, napi_callback_info info);
|
||||
static napi_value AddText(napi_env env, napi_callback_info info);
|
||||
static napi_value PopStyle(napi_env env, napi_callback_info info);
|
||||
static napi_value AddPlaceholder(napi_env env, napi_callback_info info);
|
||||
static napi_value Build(napi_env env, napi_callback_info info);
|
||||
|
||||
private:
|
||||
napi_value OnPushStyle(napi_env env, napi_callback_info info);
|
||||
napi_value OnAddText(napi_env env, napi_callback_info info);
|
||||
napi_value OnPopStyle(napi_env env, napi_callback_info info);
|
||||
napi_value OnAddPlaceholder(napi_env env, napi_callback_info info);
|
||||
napi_value OnBuild(napi_env env, napi_callback_info info);
|
||||
static thread_local napi_ref constructor_;
|
||||
std::unique_ptr<TypographyCreate> typographyCreate_ = nullptr;
|
||||
std::shared_ptr<FontCollection> fontCollection_ = nullptr;
|
||||
};
|
||||
} // namespace OHOS::Rosen
|
||||
#endif // OHOS_ROSEN_JS_PARAGRAPH_BUILDER_H
|
180
interfaces/kits/napi/graphic/text/paragraph_napi/js_paragraph.cpp
Executable file
180
interfaces/kits/napi/graphic/text/paragraph_napi/js_paragraph.cpp
Executable file
@ -0,0 +1,180 @@
|
||||
/*
|
||||
* 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 "canvas_napi/js_canvas.h"
|
||||
#include "draw/canvas.h"
|
||||
#include "../drawing/js_drawing_utils.h"
|
||||
#include "js_paragraph.h"
|
||||
#include "../js_text_utils.h"
|
||||
#include "paragraph_builder_napi/js_paragraph_builder.h"
|
||||
#include "utils/log.h"
|
||||
|
||||
namespace OHOS::Rosen {
|
||||
std::unique_ptr<Typography> g_Typography = nullptr;
|
||||
thread_local napi_ref JsParagraph::constructor_ = nullptr;
|
||||
const std::string CLASS_NAME = "JsParagraph";
|
||||
|
||||
napi_value JsParagraph::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("JsParagraph::Constructor failed to napi_get_cb_info");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!g_Typography) {
|
||||
ROSEN_LOGE("JsParagraph::Constructor g_Typography is nullptr");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
JsParagraph *jsParagraph = new(std::nothrow) JsParagraph(std::move(g_Typography));
|
||||
|
||||
status = napi_wrap(env, jsThis, jsParagraph,
|
||||
JsParagraph::Destructor, nullptr, nullptr);
|
||||
if (status != napi_ok) {
|
||||
delete jsParagraph;
|
||||
ROSEN_LOGE("JsParagraph::Constructor failed to wrap native instance");
|
||||
return nullptr;
|
||||
}
|
||||
return jsThis;
|
||||
}
|
||||
|
||||
napi_value JsParagraph::Init(napi_env env, napi_value exportObj)
|
||||
{
|
||||
napi_property_descriptor properties[] = {
|
||||
DECLARE_NAPI_FUNCTION("layout", JsParagraph::Layout),
|
||||
DECLARE_NAPI_FUNCTION("paint", JsParagraph::Paint),
|
||||
};
|
||||
|
||||
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("Failed to define Paragraph class");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
status = napi_create_reference(env, constructor, 1, &constructor_);
|
||||
if (status != napi_ok) {
|
||||
ROSEN_LOGE("Failed to create reference of result");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
status = napi_set_named_property(env, exportObj, CLASS_NAME.c_str(), constructor);
|
||||
if (status != napi_ok) {
|
||||
ROSEN_LOGE("Failed to set result");
|
||||
return nullptr;
|
||||
}
|
||||
return exportObj;
|
||||
}
|
||||
|
||||
|
||||
void JsParagraph::Destructor(napi_env env, void *nativeObject, void *finalize)
|
||||
{
|
||||
(void)finalize;
|
||||
if (nativeObject != nullptr) {
|
||||
JsParagraph *napi = reinterpret_cast<JsParagraph *>(nativeObject);
|
||||
delete napi;
|
||||
}
|
||||
}
|
||||
|
||||
napi_value JsParagraph::Layout(napi_env env, napi_callback_info info)
|
||||
{
|
||||
JsParagraph* me = CheckParamsAndGetThis<JsParagraph>(env, info);
|
||||
return (me != nullptr) ? me->OnLayout(env, info) : nullptr;
|
||||
}
|
||||
|
||||
napi_value JsParagraph::OnLayout(napi_env env, napi_callback_info info)
|
||||
{
|
||||
if (!paragraphCurrent_) {
|
||||
ROSEN_LOGE("JsParagraph::OnLayout paragraphCurrent is nullptr");
|
||||
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
|
||||
}
|
||||
size_t argc = ARGC_ONE;
|
||||
napi_value argv[ARGC_ONE] = {nullptr};
|
||||
napi_status status = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
|
||||
if (status != napi_ok || argc < ARGC_ONE) {
|
||||
ROSEN_LOGE("JsParagraph::OnLayout Argc is invalid: %{public}zu", argc);
|
||||
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
|
||||
}
|
||||
double width = 0.0;
|
||||
if (!(ConvertFromJsValue(env, argv[0], width))) {
|
||||
ROSEN_LOGE("JsParagraph::OnLayout Argv is invalid");
|
||||
return NapiGetUndefined(env);
|
||||
}
|
||||
|
||||
paragraphCurrent_->Layout(width);
|
||||
return NapiGetUndefined(env);
|
||||
}
|
||||
|
||||
napi_value JsParagraph::Paint(napi_env env, napi_callback_info info)
|
||||
{
|
||||
JsParagraph* me = CheckParamsAndGetThis<JsParagraph>(env, info);
|
||||
return (me != nullptr) ? me->OnPaint(env, info) : nullptr;
|
||||
}
|
||||
|
||||
napi_value JsParagraph::OnPaint(napi_env env, napi_callback_info info)
|
||||
{
|
||||
if (paragraphCurrent_ == nullptr) {
|
||||
ROSEN_LOGE("JsParagraph::OnPaint paragraphCurrent_ is nullptr");
|
||||
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
|
||||
}
|
||||
size_t argc = ARGC_THREE;
|
||||
napi_value argv[ARGC_THREE] = {nullptr};
|
||||
napi_status status = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
|
||||
if (status != napi_ok || argc < ARGC_THREE) {
|
||||
ROSEN_LOGE("JsParagraph::OnPaint Argc is invalid: %{public}zu", argc);
|
||||
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
|
||||
}
|
||||
Drawing::JsCanvas* jsCanvas = nullptr;
|
||||
double x = 0.0;
|
||||
double y = 0.0;
|
||||
napi_unwrap(env, argv[0], reinterpret_cast<void **>(&jsCanvas));
|
||||
if (jsCanvas == nullptr ||
|
||||
!(ConvertFromJsValue(env, argv[ARGC_ONE], x) && ConvertFromJsValue(env, argv[ARGC_TWO], y))) {
|
||||
ROSEN_LOGE("JsParagraph::OnPaint Argv is invalid");
|
||||
return NapiGetUndefined(env);
|
||||
}
|
||||
paragraphCurrent_->Paint(jsCanvas->GetCanvas(), x, y);
|
||||
return NapiGetUndefined(env);
|
||||
}
|
||||
|
||||
JsParagraph::JsParagraph(std::shared_ptr<Typography> typography)
|
||||
: paragraphCurrent_(typography)
|
||||
{
|
||||
}
|
||||
|
||||
JsParagraph::~JsParagraph()
|
||||
{
|
||||
}
|
||||
|
||||
napi_value JsParagraph::CreateJsTypography(napi_env env, std::unique_ptr<Typography> typography)
|
||||
{
|
||||
napi_value constructor = nullptr;
|
||||
napi_value result = nullptr;
|
||||
napi_status status = napi_get_reference_value(env, constructor_, &constructor);
|
||||
if (status == napi_ok) {
|
||||
g_Typography = std::move(typography);
|
||||
status = napi_new_instance(env, constructor, 0, nullptr, &result);
|
||||
if (status == napi_ok) {
|
||||
return result;
|
||||
} else {
|
||||
ROSEN_LOGE("CreateJsTypography: New instance could not be obtained");
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
} // namespace OHOS::Rosen
|
47
interfaces/kits/napi/graphic/text/paragraph_napi/js_paragraph.h
Executable file
47
interfaces/kits/napi/graphic/text/paragraph_napi/js_paragraph.h
Executable file
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* 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_PARAGRAPH_H
|
||||
#define OHOS_ROSEN_JS_PARAGRAPH_H
|
||||
|
||||
#include <memory>
|
||||
#include <native_engine/native_engine.h>
|
||||
#include <native_engine/native_value.h>
|
||||
|
||||
#include "typography.h"
|
||||
|
||||
namespace OHOS::Rosen {
|
||||
class JsParagraph final {
|
||||
public:
|
||||
explicit JsParagraph(std::shared_ptr<Typography> paragraph);
|
||||
~JsParagraph();
|
||||
|
||||
static napi_value Init(napi_env env, napi_value exportObj);
|
||||
static napi_value Layout(napi_env env, napi_callback_info info);
|
||||
static napi_value Paint(napi_env env, napi_callback_info info);
|
||||
static void Destructor(napi_env env, void *nativeObject, void *finalize);
|
||||
static napi_value CreateJsTypography(napi_env env, std::unique_ptr<Typography> typography);
|
||||
static napi_value Constructor(napi_env env, napi_callback_info info);
|
||||
|
||||
private:
|
||||
static thread_local napi_ref constructor_;
|
||||
napi_value OnLayout(napi_env env, napi_callback_info info);
|
||||
napi_value OnPaint(napi_env env, napi_callback_info info);
|
||||
|
||||
std::shared_ptr<Typography> paragraphCurrent_ = nullptr;
|
||||
std::shared_ptr<Typography> paragraphOld_ = nullptr;
|
||||
};
|
||||
} // namespace OHOS::Rosen
|
||||
#endif // OHOS_ROSEN_JS_PARAGRAPH_H
|
Loading…
Reference in New Issue
Block a user