mirror of
https://gitee.com/openharmony/graphic_graphic_2d
synced 2024-11-23 15:11:51 +00:00
Create TS interface for JsCanvas class
Signed-off-by: rechkinigor <rechkin.igor@huawei-partners.com> Change-Id: I9ec0da217bbd29498bc50d19d5725f9e6edba190
This commit is contained in:
parent
38e4fff1eb
commit
74f531b0d0
@ -30,6 +30,7 @@ config("local_drawing_config") {
|
||||
include_dirs = [
|
||||
"../drawing",
|
||||
"../../../../../rosen/modules/2d_graphics/include",
|
||||
"../../../../../rosen/modules/2d_graphics/src",
|
||||
"../../../../../rosen/modules/2d_graphics/src/drawing/engine_adapter",
|
||||
]
|
||||
}
|
||||
@ -78,6 +79,7 @@ ohos_shared_library("drawing_napi_impl") {
|
||||
"sampling_options_napi/js_sampling_options.cpp",
|
||||
"shadow_layer_napi/js_shadow_layer.cpp",
|
||||
"text_blob_napi/js_text_blob.cpp",
|
||||
"round_rect_napi/js_roundrect.cpp",
|
||||
]
|
||||
|
||||
defines = []
|
||||
|
@ -13,6 +13,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <cstdio>
|
||||
#include "js_canvas.h"
|
||||
|
||||
#include <mutex>
|
||||
@ -43,6 +44,7 @@
|
||||
#include "region_napi/js_region.h"
|
||||
#include "sampling_options_napi/js_sampling_options.h"
|
||||
#include "text_blob_napi/js_text_blob.h"
|
||||
#include "round_rect_napi/js_roundrect.h"
|
||||
#include "js_drawing_utils.h"
|
||||
#include "utils/performanceCaculate.h"
|
||||
|
||||
@ -363,10 +365,17 @@ bool JsCanvas::DeclareFuncAndCreateConstructor(napi_env env)
|
||||
DECLARE_NAPI_FUNCTION("restoreToCount", JsCanvas::RestoreToCount),
|
||||
DECLARE_NAPI_FUNCTION("scale", JsCanvas::Scale),
|
||||
DECLARE_NAPI_FUNCTION("clipPath", JsCanvas::ClipPath),
|
||||
DECLARE_NAPI_FUNCTION("clipRegion", JsCanvas::ClipRegion),
|
||||
DECLARE_NAPI_FUNCTION("clipRect", JsCanvas::ClipRect),
|
||||
DECLARE_NAPI_FUNCTION("concatMatrix", JsCanvas::ConcatMatrix),
|
||||
DECLARE_NAPI_FUNCTION("clipRoundRect", JsCanvas::ClipRoundRect),
|
||||
DECLARE_NAPI_FUNCTION("setMatrix", JsCanvas::SetMatrix),
|
||||
DECLARE_NAPI_FUNCTION("translate", JsCanvas::Translate),
|
||||
DECLARE_NAPI_FUNCTION("getImageInfo", JsCanvas::GetImageInfo),
|
||||
DECLARE_NAPI_FUNCTION("drawImageRect", JsCanvas::DrawImageRect),
|
||||
DECLARE_NAPI_FUNCTION("readPixels", JsCanvas::ReadPixels),
|
||||
DECLARE_NAPI_FUNCTION("resetMatrix", JsCanvas::ResetMatrix),
|
||||
DECLARE_NAPI_FUNCTION("isClipEmpty", JsCanvas::IsClipEmpty),
|
||||
};
|
||||
|
||||
napi_value constructor = nullptr;
|
||||
@ -1246,6 +1255,21 @@ napi_value JsCanvas::OnRotate(napi_env env, napi_callback_info info)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value JsCanvas::GetImageInfo(napi_env env, napi_callback_info info)
|
||||
{
|
||||
JsCanvas* me = CheckParamsAndGetThis<JsCanvas>(env, info);
|
||||
return (me != nullptr) ? me->OnGetImageInfo(env, info) : nullptr;
|
||||
}
|
||||
|
||||
napi_value JsCanvas::OnGetImageInfo(napi_env env, napi_callback_info info)
|
||||
{
|
||||
if (m_canvas == nullptr) {
|
||||
ROSEN_LOGE("JsCanvas::OnGetImageInfo canvas is null");
|
||||
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
|
||||
}
|
||||
return GetImageInfoAndConvertToJsValue(env, m_canvas->GetImageInfo());
|
||||
}
|
||||
|
||||
napi_value JsCanvas::GetSaveCount(napi_env env, napi_callback_info info)
|
||||
{
|
||||
JsCanvas* me = CheckParamsAndGetThis<JsCanvas>(env, info);
|
||||
@ -1335,6 +1359,42 @@ napi_value JsCanvas::OnClipPath(napi_env env, napi_callback_info info)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value JsCanvas::ClipRegion(napi_env env, napi_callback_info info)
|
||||
{
|
||||
JsCanvas* me = CheckParamsAndGetThis<JsCanvas>(env, info);
|
||||
return (me != nullptr) ? me->OnClipRegion(env, info) : nullptr;
|
||||
}
|
||||
|
||||
napi_value JsCanvas::OnClipRegion(napi_env env, napi_callback_info info)
|
||||
{
|
||||
if (m_canvas == nullptr) {
|
||||
ROSEN_LOGE("JsCanvas::OnClipRegion m_canvas is nullptr");
|
||||
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
|
||||
}
|
||||
size_t argc = ARGC_TWO;
|
||||
napi_value argv[ARGC_TWO] = {nullptr};
|
||||
CHECK_PARAM_NUMBER_WITH_OPTIONAL_PARAMS(argv, argc, ARGC_ONE, ARGC_TWO);
|
||||
|
||||
JsRegion* jsRegion = nullptr;
|
||||
GET_UNWRAP_PARAM(ARGC_ZERO, jsRegion);
|
||||
|
||||
Region* region = jsRegion->GetRegion();
|
||||
if (region == nullptr) {
|
||||
ROSEN_LOGE("JsCanvas::OnClipRegion region is nullptr");
|
||||
return nullptr;
|
||||
}
|
||||
if (argc == ARGC_ONE) {
|
||||
m_canvas->ClipRegion(*region);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int32_t jsClipOp = 0;
|
||||
GET_INT32_CHECK_GE_ZERO_PARAM(ARGC_ONE, jsClipOp);
|
||||
|
||||
m_canvas->ClipRegion(*region, static_cast<ClipOp>(jsClipOp));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value JsCanvas::Translate(napi_env env, napi_callback_info info)
|
||||
{
|
||||
JsCanvas* me = CheckParamsAndGetThis<JsCanvas>(env, info);
|
||||
@ -1528,6 +1588,50 @@ napi_value JsCanvas::OnClipRect(napi_env env, napi_callback_info info)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value JsCanvas::ClipRoundRect(napi_env env, napi_callback_info info)
|
||||
{
|
||||
JsCanvas* me = CheckParamsAndGetThis<JsCanvas>(env, info);
|
||||
return (me != nullptr) ? me->OnClipRoundRect(env, info) : nullptr;
|
||||
}
|
||||
|
||||
napi_value JsCanvas::OnClipRoundRect(napi_env env, napi_callback_info info)
|
||||
{
|
||||
if (m_canvas == nullptr) {
|
||||
ROSEN_LOGE("JsCanvas::OnClipRoundRect m_canvas is nullptr");
|
||||
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
|
||||
}
|
||||
|
||||
size_t argc = ARGC_THREE;
|
||||
napi_value argv[ARGC_THREE] = {nullptr};
|
||||
CHECK_PARAM_NUMBER_WITH_OPTIONAL_PARAMS(argv, argc, ARGC_ONE, ARGC_THREE);
|
||||
|
||||
JsRoundRect* jsRoundRect = nullptr;
|
||||
GET_UNWRAP_PARAM(ARGC_ZERO, jsRoundRect);
|
||||
if (jsRoundRect->GetRoundRect() == nullptr) {
|
||||
ROSEN_LOGE("JsCanvas::OnDrawRegion region is nullptr");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (argc == ARGC_ONE) {
|
||||
m_canvas->ClipRoundRect(*jsRoundRect->GetRoundRect());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int32_t clipOpInt = 0;
|
||||
GET_INT32_CHECK_GE_ZERO_PARAM(ARGC_ONE, clipOpInt);
|
||||
|
||||
if (argc == ARGC_TWO) {
|
||||
m_canvas->ClipRoundRect(*jsRoundRect->GetRoundRect(), static_cast<ClipOp>(clipOpInt));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool doAntiAlias = false;
|
||||
GET_BOOLEAN_PARAM(ARGC_TWO, doAntiAlias);
|
||||
|
||||
m_canvas->ClipRoundRect(*jsRoundRect->GetRoundRect(), static_cast<ClipOp>(clipOpInt), doAntiAlias);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value JsCanvas::SetMatrix(napi_env env, napi_callback_info info)
|
||||
{
|
||||
JsCanvas* me = CheckParamsAndGetThis<JsCanvas>(env, info);
|
||||
@ -1609,6 +1713,23 @@ napi_value JsCanvas::OnConcatMatrix(napi_env env, napi_callback_info info)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value JsCanvas::IsClipEmpty(napi_env env, napi_callback_info info)
|
||||
{
|
||||
JsCanvas* me = CheckParamsAndGetThis<JsCanvas>(env, info);
|
||||
return (me != nullptr) ? me->OnIsClipEmpty(env, info) : nullptr;
|
||||
}
|
||||
|
||||
napi_value JsCanvas::OnIsClipEmpty(napi_env env, napi_callback_info info)
|
||||
{
|
||||
if (m_canvas == nullptr) {
|
||||
ROSEN_LOGE("JsCanvas::OnIsClipEmpty canvas is nullptr");
|
||||
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
|
||||
}
|
||||
|
||||
return CreateJsValue(env, m_canvas->IsClipEmpty());
|
||||
}
|
||||
|
||||
|
||||
Canvas* JsCanvas::GetCanvas()
|
||||
{
|
||||
return m_canvas;
|
||||
@ -1641,5 +1762,146 @@ void JsCanvas::RestoreCanvas()
|
||||
JS_CALL_DRAWING_FUNC(m_canvas->Restore());
|
||||
}
|
||||
}
|
||||
|
||||
napi_value JsCanvas::DrawImageRect(napi_env env, napi_callback_info info)
|
||||
{
|
||||
JsCanvas* me = CheckParamsAndGetThis<JsCanvas>(env, info);
|
||||
return (me != nullptr) ? me->OnDrawImageRect(env, info) : nullptr;
|
||||
}
|
||||
|
||||
napi_value JsCanvas::OnDrawImageRect(napi_env env, napi_callback_info info)
|
||||
{
|
||||
#ifdef ROSEN_OHOS
|
||||
if (m_canvas == nullptr) {
|
||||
ROSEN_LOGE("JsCanvas::OnDrawImageRect canvas is nullptr");
|
||||
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
|
||||
}
|
||||
|
||||
size_t argc = ARGC_FIVE;
|
||||
napi_value argv[ARGC_FIVE] = {nullptr};
|
||||
CHECK_PARAM_NUMBER_WITH_OPTIONAL_PARAMS(argv, argc, ARGC_ONE, ARGC_FIVE);
|
||||
|
||||
PixelMapNapi* pixelMapNapi = nullptr;
|
||||
GET_UNWRAP_PARAM(ARGC_ZERO, pixelMapNapi);
|
||||
|
||||
if (pixelMapNapi->GetPixelNapiInner() == nullptr) {
|
||||
ROSEN_LOGE("JsCanvas::OnDrawImageRect pixelmap GetPixelNapiInner is nullptr");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::shared_ptr<Drawing::Image> image = ExtractDrawingImage(pixelMapNapi->GetPixelNapiInner());
|
||||
if (image == nullptr) {
|
||||
ROSEN_LOGE("JsCanvas::OnDrawImageRect image is nullptr");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
double ltrb[ARGC_FOUR] = {0};
|
||||
if (!ConvertFromJsRect(env, argv[(argc == ARGC_THREE) ? ARGC_ONE : ARGC_TWO], ltrb, ARGC_FOUR)) {
|
||||
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,
|
||||
"Incorrect rect dst parameter type. The type of left, top, right and bottom must be number.");
|
||||
}
|
||||
Drawing::Rect dstRect = Drawing::Rect(ltrb[ARGC_ZERO], ltrb[ARGC_ONE], ltrb[ARGC_TWO], ltrb[ARGC_THREE]);
|
||||
|
||||
JsSamplingOptions* jsSamplingOptions = nullptr;
|
||||
|
||||
GET_UNWRAP_PARAM((argc == ARGC_THREE) ? ARGC_TWO : ARGC_THREE, jsSamplingOptions);
|
||||
|
||||
std::shared_ptr<SamplingOptions> samplingOptions = jsSamplingOptions->GetSamplingOptions();
|
||||
if (samplingOptions == nullptr) {
|
||||
ROSEN_LOGE("JsCanvas::OnDrawImageRect get samplingOptions is nullptr");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (argc == ARGC_THREE) {
|
||||
JS_CALL_DRAWING_FUNC(m_canvas->DrawImageRect(*image, dstRect, *samplingOptions.get()));
|
||||
} else {
|
||||
double ltrb[ARGC_FOUR] = {0};
|
||||
if (!ConvertFromJsRect(env, argv[ARGC_ONE], ltrb, ARGC_FOUR)) {
|
||||
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,
|
||||
"Incorrect rect src parameter type. The type of left, top, right and bottom must be number.");
|
||||
}
|
||||
Drawing::Rect srcRect = Drawing::Rect(ltrb[ARGC_ZERO], ltrb[ARGC_ONE], ltrb[ARGC_TWO], ltrb[ARGC_THREE]);
|
||||
|
||||
if (argc == ARGC_FIVE) {
|
||||
int32_t jsConstraint = 0;
|
||||
GET_INT32_CHECK_GE_ZERO_PARAM(ARGC_FOUR, jsConstraint);
|
||||
JS_CALL_DRAWING_FUNC(m_canvas->DrawImageRect(*image, srcRect, dstRect, *samplingOptions.get(), SrcRectConstraint(jsConstraint)));
|
||||
} else {
|
||||
JS_CALL_DRAWING_FUNC(m_canvas->DrawImageRect(*image, srcRect, dstRect, *samplingOptions.get()));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value JsCanvas::ReadPixels(napi_env env, napi_callback_info info)
|
||||
{
|
||||
JsCanvas* me = CheckParamsAndGetThis<JsCanvas>(env, info);
|
||||
return (me != nullptr) ? me->OnReadPixels(env, info) : nullptr;
|
||||
}
|
||||
|
||||
napi_value JsCanvas::OnReadPixels(napi_env env, napi_callback_info info)
|
||||
{
|
||||
if (m_canvas == nullptr) {
|
||||
ROSEN_LOGE("JsCanvas::OnReadPixels canvas is nullptr");
|
||||
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
|
||||
}
|
||||
|
||||
napi_value argv[ARGC_FIVE] = {nullptr};
|
||||
CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_FIVE);
|
||||
|
||||
Drawing::ImageInfo imageInfo;
|
||||
if (!ConvertFromJsImageInfo(env, argv[ARGC_ZERO], imageInfo)) {
|
||||
ROSEN_LOGE("JsCanvas::OnReadPixels argv[0] is invalid");
|
||||
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid [0] param.");;
|
||||
}
|
||||
|
||||
napi_value dstPixels = argv[ARGC_ONE];
|
||||
|
||||
uint32_t size = 0;
|
||||
GET_UINT32_PARAM(ARGC_TWO, size);
|
||||
if (size == 0) {
|
||||
ROSEN_LOGE("JsCanvas::OnReadPixels size of dstPixels is 0");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int srcX = 0;
|
||||
GET_INT32_PARAM(ARGC_THREE, srcX);
|
||||
int srcY = 0;
|
||||
GET_INT32_PARAM(ARGC_FOUR, srcY);
|
||||
|
||||
std::vector<uint8_t> buffer(size);
|
||||
const bool res = m_canvas->ReadPixels(imageInfo, buffer.data(), size, srcX, srcY);
|
||||
if (!res) {
|
||||
ROSEN_LOGE("JsCanvas::OnReadPixels m_canvas->ReadPixels returns false");
|
||||
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "ReadPixels returns false.");;
|
||||
}
|
||||
|
||||
uint32_t i = 0;
|
||||
napi_value eleValue = nullptr;
|
||||
for (auto item : buffer) {
|
||||
eleValue = CreateJsNumber(env, static_cast<uint8_t>(item));
|
||||
napi_set_element(env, dstPixels, i, eleValue);
|
||||
++i;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
napi_value JsCanvas::ResetMatrix(napi_env env, napi_callback_info info)
|
||||
{
|
||||
JsCanvas* me = CheckParamsAndGetThis<JsCanvas>(env, info);
|
||||
return (me != nullptr) ? me->OnResetMatrix(env, info) : nullptr;
|
||||
}
|
||||
|
||||
napi_value JsCanvas::OnResetMatrix(napi_env env, napi_callback_info info)
|
||||
{
|
||||
if (m_canvas == nullptr) {
|
||||
ROSEN_LOGE("JsCanvas::OnResetMatrix m_canvas is null");
|
||||
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
|
||||
}
|
||||
m_canvas->ResetMatrix();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace Drawing
|
||||
} // namespace OHOS::Rosen
|
||||
|
@ -53,9 +53,11 @@ public:
|
||||
static napi_value AttachPen(napi_env env, napi_callback_info info);
|
||||
static napi_value Clear(napi_env env, napi_callback_info info);
|
||||
static napi_value ClipPath(napi_env env, napi_callback_info info);
|
||||
static napi_value ClipRegion(napi_env env, napi_callback_info info);
|
||||
static napi_value ClipRect(napi_env env, napi_callback_info info);
|
||||
static napi_value ConcatMatrix(napi_env env, napi_callback_info info);
|
||||
static napi_value DrawArc(napi_env env, napi_callback_info info);
|
||||
static napi_value ClipRoundRect(napi_env env, napi_callback_info info);
|
||||
static napi_value DrawCircle(napi_env env, napi_callback_info info);
|
||||
static napi_value DrawColor(napi_env env, napi_callback_info info);
|
||||
static napi_value DrawImage(napi_env env, napi_callback_info info);
|
||||
@ -71,9 +73,11 @@ public:
|
||||
static napi_value DrawText(napi_env env, napi_callback_info info);
|
||||
static napi_value DetachBrush(napi_env env, napi_callback_info info);
|
||||
static napi_value DetachPen(napi_env env, napi_callback_info info);
|
||||
static napi_value GetImageInfo(napi_env env, napi_callback_info info);
|
||||
static napi_value GetSaveCount(napi_env env, napi_callback_info info);
|
||||
static napi_value GetWidth(napi_env env, napi_callback_info info);
|
||||
static napi_value GetHeight(napi_env env, napi_callback_info info);
|
||||
static napi_value IsClipEmpty(napi_env env, napi_callback_info info);
|
||||
static napi_value Rotate(napi_env env, napi_callback_info info);
|
||||
static napi_value RestoreToCount(napi_env env, napi_callback_info info);
|
||||
static napi_value Restore(napi_env env, napi_callback_info info);
|
||||
@ -83,6 +87,9 @@ public:
|
||||
static napi_value Scale(napi_env env, napi_callback_info info);
|
||||
static napi_value SetMatrix(napi_env env, napi_callback_info info);
|
||||
static napi_value Translate(napi_env env, napi_callback_info info);
|
||||
static napi_value DrawImageRect(napi_env env, napi_callback_info info);
|
||||
static napi_value ReadPixels(napi_env env, napi_callback_info info);
|
||||
static napi_value ResetMatrix(napi_env env, napi_callback_info info);
|
||||
|
||||
Canvas* GetCanvas();
|
||||
DRAWING_API void ResetCanvas();
|
||||
@ -93,9 +100,11 @@ public:
|
||||
private:
|
||||
napi_value OnClear(napi_env env, napi_callback_info info);
|
||||
napi_value OnClipPath(napi_env env, napi_callback_info info);
|
||||
napi_value OnClipRegion(napi_env env, napi_callback_info info);
|
||||
napi_value OnClipRect(napi_env env, napi_callback_info info);
|
||||
napi_value OnConcatMatrix(napi_env env, napi_callback_info info);
|
||||
napi_value OnDrawArc(napi_env env, napi_callback_info info);
|
||||
napi_value OnClipRoundRect(napi_env env, napi_callback_info info);
|
||||
napi_value OnDrawCircle(napi_env env, napi_callback_info info);
|
||||
napi_value OnDrawColor(napi_env env, napi_callback_info info);
|
||||
napi_value OnDrawImage(napi_env env, napi_callback_info info);
|
||||
@ -109,9 +118,11 @@ private:
|
||||
napi_value OnDrawRegion(napi_env env, napi_callback_info info);
|
||||
napi_value OnDrawSingleCharacter(napi_env env, napi_callback_info info);
|
||||
napi_value OnDrawText(napi_env env, napi_callback_info info);
|
||||
napi_value OnGetImageInfo(napi_env env, napi_callback_info info);
|
||||
napi_value OnGetSaveCount(napi_env env, napi_callback_info info);
|
||||
napi_value OnGetWidth(napi_env env, napi_callback_info info);
|
||||
napi_value OnGetHeight(napi_env env, napi_callback_info info);
|
||||
napi_value OnIsClipEmpty(napi_env env, napi_callback_info info);
|
||||
napi_value OnRotate(napi_env env, napi_callback_info info);
|
||||
napi_value OnRestoreToCount(napi_env env, napi_callback_info info);
|
||||
napi_value OnRestore(napi_env env, napi_callback_info info);
|
||||
@ -121,6 +132,9 @@ private:
|
||||
napi_value OnScale(napi_env env, napi_callback_info info);
|
||||
napi_value OnSetMatrix(napi_env env, napi_callback_info info);
|
||||
napi_value OnTranslate(napi_env env, napi_callback_info info);
|
||||
napi_value OnDrawImageRect(napi_env env, napi_callback_info info);
|
||||
napi_value OnReadPixels(napi_env env, napi_callback_info info);
|
||||
napi_value OnResetMatrix(napi_env env, napi_callback_info info);
|
||||
|
||||
static bool DeclareFuncAndCreateConstructor(napi_env env);
|
||||
static thread_local napi_ref constructor_;
|
||||
|
@ -24,6 +24,7 @@
|
||||
#include "draw/core_canvas.h"
|
||||
#include "draw/path.h"
|
||||
#include "draw/pen.h"
|
||||
#include "draw/color.h"
|
||||
#include "effect/mask_filter.h"
|
||||
#include "text/font_types.h"
|
||||
#include "utils/region.h"
|
||||
@ -171,6 +172,22 @@ static const std::vector<struct JsEnumInt> g_pathFillType = {
|
||||
{ "INVERSE_EVEN_ODD", static_cast<int32_t>(PathFillType::INVERSE_EVENTODD) },
|
||||
};
|
||||
|
||||
static const std::vector<struct JsEnumInt> g_alphaFormat = {
|
||||
{ "UNKNOWN", static_cast<int32_t>(Drawing::AlphaType::ALPHATYPE_UNKNOWN) },
|
||||
{ "OPAQUE", static_cast<int32_t>(Drawing::AlphaType::ALPHATYPE_OPAQUE) },
|
||||
{ "PREMUL", static_cast<int32_t>(Drawing::AlphaType::ALPHATYPE_PREMUL) },
|
||||
{ "UNPREMUL", static_cast<int32_t>(Drawing::AlphaType::ALPHATYPE_UNPREMUL) },
|
||||
};
|
||||
|
||||
static const std::vector<struct JsEnumInt> g_colorType = {
|
||||
{ "UNKNOWN", static_cast<int32_t>(Drawing::ColorType::COLORTYPE_UNKNOWN) },
|
||||
{ "ALPHA_8", static_cast<int32_t>(Drawing::ColorType::COLORTYPE_ALPHA_8) },
|
||||
{ "RGB_565", static_cast<int32_t>(Drawing::ColorType::COLORTYPE_RGB_565) },
|
||||
{ "ARGB_4444", static_cast<int32_t>(Drawing::ColorType::COLORTYPE_ARGB_4444) },
|
||||
{ "RGBA_8888", static_cast<int32_t>(Drawing::ColorType::COLORTYPE_RGBA_8888) },
|
||||
{ "BGRA_8888", static_cast<int32_t>(Drawing::ColorType::COLORTYPE_BGRA_8888) },
|
||||
};
|
||||
|
||||
static const std::map<std::string_view, const std::vector<struct JsEnumInt>&> g_intEnumClassMap = {
|
||||
{ "BlendMode", g_blendMode },
|
||||
{ "TextEncoding", g_textEncoding },
|
||||
@ -187,6 +204,8 @@ static const std::map<std::string_view, const std::vector<struct JsEnumInt>&> g_
|
||||
{ "PointMode", g_pointMode },
|
||||
{ "PathDirection", g_pathDirection },
|
||||
{ "PathFillType", g_pathFillType },
|
||||
{ "AlphaFormat", g_alphaFormat },
|
||||
{ "ColorFormat", g_colorType },
|
||||
};
|
||||
|
||||
napi_value JsEnum::JsEnumIntInit(napi_env env, napi_value exports)
|
||||
|
@ -32,6 +32,7 @@
|
||||
#include "shadow_layer_napi/js_shadow_layer.h"
|
||||
#include "text_blob_napi/js_text_blob.h"
|
||||
#include "matrix_napi/js_matrix.h"
|
||||
#include "round_rect_napi/js_roundrect.h"
|
||||
|
||||
namespace OHOS::Rosen {
|
||||
namespace Drawing {
|
||||
@ -54,6 +55,7 @@ napi_value DrawingInit(napi_env env, napi_value exportObj)
|
||||
JsRoundRect::Init(env, exportObj);
|
||||
JsShadowLayer::Init(env, exportObj);
|
||||
JsMatrix::Init(env, exportObj);
|
||||
JsRoundRect::Init(env, exportObj);
|
||||
return exportObj;
|
||||
}
|
||||
} // namespace Drawing
|
||||
|
@ -88,6 +88,13 @@ napi_value NapiThrowError(napi_env env, DrawingErrorCode err, const std::string&
|
||||
static const char* g_argbString[4] = {"alpha", "red", "green", "blue"};
|
||||
static const char* g_ltrbString[4] = {"left", "top", "right", "bottom"};
|
||||
|
||||
#define IMAGEINFO_WIDTH_ID 0
|
||||
#define IMAGEINFO_HEIGHT_ID 1
|
||||
#define IMAGEINFO_CLRTYPE_ID 2
|
||||
#define IMAGEINFO_ALPHTYPE_ID 3
|
||||
#define IMAGEINFO_FIELDS_COUNT 4
|
||||
static const char* g_whcaString[IMAGEINFO_FIELDS_COUNT] = {"width", "height", "colorType", "alphaType"};
|
||||
|
||||
bool ConvertFromJsColor(napi_env env, napi_value jsValue, int32_t* argb, size_t size)
|
||||
{
|
||||
napi_value tempValue = nullptr;
|
||||
@ -156,5 +163,49 @@ napi_value GetFontMetricsAndConvertToJsValue(napi_env env, FontMetrics* metrics)
|
||||
}
|
||||
return objValue;
|
||||
}
|
||||
|
||||
napi_value GetImageInfoAndConvertToJsValue(napi_env env, const Drawing::ImageInfo& info)
|
||||
{
|
||||
napi_value objValue = nullptr;
|
||||
napi_create_object(env, &objValue);
|
||||
|
||||
if (objValue != nullptr) {
|
||||
napi_set_named_property(env, objValue, g_whcaString[IMAGEINFO_WIDTH_ID], CreateJsNumber(env, static_cast<int32_t>(info.GetWidth())));
|
||||
napi_set_named_property(env, objValue, g_whcaString[IMAGEINFO_HEIGHT_ID], CreateJsNumber(env, static_cast<int32_t>(info.GetHeight())));
|
||||
Drawing::ColorType clrType = info.GetColorType();
|
||||
if (clrType > Drawing::ColorType::COLORTYPE_BGRA_8888) {
|
||||
clrType = Drawing::ColorType::COLORTYPE_UNKNOWN;
|
||||
}
|
||||
napi_set_named_property(env, objValue, g_whcaString[IMAGEINFO_CLRTYPE_ID], CreateJsNumber(env, static_cast<int32_t>(clrType)));
|
||||
napi_set_named_property(env, objValue, g_whcaString[IMAGEINFO_ALPHTYPE_ID], CreateJsNumber(env, static_cast<int32_t>(info.GetAlphaType())));
|
||||
}
|
||||
return objValue;
|
||||
}
|
||||
|
||||
bool ConvertFromJsImageInfo(napi_env env, napi_value jsValue, Drawing::ImageInfo& out)
|
||||
{
|
||||
if (jsValue == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
out = Drawing::ImageInfo();
|
||||
napi_value tempValue = nullptr;
|
||||
int32_t values[IMAGEINFO_FIELDS_COUNT] = {0};
|
||||
for (size_t idx = 0; idx < IMAGEINFO_FIELDS_COUNT; idx++) {
|
||||
int32_t* curVal = values + idx;
|
||||
napi_get_named_property(env, jsValue, g_whcaString[idx], &tempValue);
|
||||
if (napi_get_value_int32(env, tempValue, curVal) != napi_ok) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
out.SetWidth(static_cast<int>(values[IMAGEINFO_WIDTH_ID]));
|
||||
out.SetHeight(static_cast<int>(values[IMAGEINFO_HEIGHT_ID]));
|
||||
out.SetColorType(static_cast<Drawing::ColorType>(values[IMAGEINFO_CLRTYPE_ID]));
|
||||
out.SetAlphaType(static_cast<Drawing::AlphaType>(values[IMAGEINFO_ALPHTYPE_ID]));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace Drawing
|
||||
} // namespace OHOS::Rosen
|
||||
|
@ -27,6 +27,7 @@
|
||||
#include "text/font_metrics.h"
|
||||
#include "text/font_types.h"
|
||||
#include "utils/rect.h"
|
||||
#include "image/image_info.h"
|
||||
|
||||
namespace OHOS::Rosen {
|
||||
|
||||
@ -355,6 +356,9 @@ inline napi_value GetStringAndConvertToJsValue(napi_env env, std::string str)
|
||||
|
||||
napi_value GetFontMetricsAndConvertToJsValue(napi_env env, FontMetrics* metrics);
|
||||
|
||||
bool ConvertFromJsImageInfo(napi_env env, napi_value jsValue, Drawing::ImageInfo& out);
|
||||
napi_value GetImageInfoAndConvertToJsValue(napi_env env, const Drawing::ImageInfo& info);
|
||||
|
||||
inline napi_value GetRectAndConvertToJsValue(napi_env env, std::shared_ptr<Rect> rect)
|
||||
{
|
||||
napi_value objValue = nullptr;
|
||||
|
@ -0,0 +1,116 @@
|
||||
/*
|
||||
* 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_roundrect.h"
|
||||
#include "../js_drawing_utils.h"
|
||||
#include "native_value.h"
|
||||
#include "../js_common.h"
|
||||
|
||||
namespace OHOS::Rosen {
|
||||
namespace Drawing {
|
||||
thread_local napi_ref JsRoundRect::constructor_ = nullptr;
|
||||
const std::string CLASS_NAME = "RoundRect";
|
||||
napi_value JsRoundRect::Init(napi_env env, napi_value exportObj)
|
||||
{
|
||||
napi_property_descriptor properties[] = {
|
||||
};
|
||||
|
||||
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("JsRoundRect::Init Failed to define RoundRect class");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
status = napi_create_reference(env, constructor, 1, &constructor_);
|
||||
if (status != napi_ok) {
|
||||
ROSEN_LOGE("JsRoundRect::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("JsRoundRect::Init Failed to set constructor");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return exportObj;
|
||||
}
|
||||
|
||||
napi_value JsRoundRect::Constructor(napi_env env, napi_callback_info info)
|
||||
{
|
||||
size_t argCount = ARGC_THREE;
|
||||
napi_value argv[ARGC_THREE] = {nullptr};
|
||||
napi_value jsThis = nullptr;
|
||||
napi_status status = napi_get_cb_info(env, info, &argCount, argv, &jsThis, nullptr);
|
||||
if (status != napi_ok) {
|
||||
ROSEN_LOGE("JsRoundRect::Constructor failed to napi_get_cb_info");
|
||||
return nullptr;
|
||||
}
|
||||
napi_valuetype valueType = napi_undefined;
|
||||
if (argv[0] == nullptr || napi_typeof(env, argv[0], &valueType) != napi_ok || valueType != napi_object) {
|
||||
ROSEN_LOGE("JsRoundRect::Constructor Argv[0] is invalid");
|
||||
return NapiGetUndefined(env);
|
||||
}
|
||||
|
||||
double ltrb[ARGC_FOUR] = {0};
|
||||
if (!ConvertFromJsRect(env, argv[ARGC_ZERO], ltrb, ARGC_FOUR)) {
|
||||
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,
|
||||
"Incorrect parameter0 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 xRad = 0.0;
|
||||
GET_DOUBLE_PARAM(ARGC_ONE, xRad);
|
||||
double yRad = 0.0;
|
||||
GET_DOUBLE_PARAM(ARGC_TWO, yRad);
|
||||
|
||||
JsRoundRect* jsRoundRect = nullptr;
|
||||
std::shared_ptr<RoundRect> roundRect = nullptr;
|
||||
roundRect = std::make_shared<RoundRect>(drawingRect, xRad, yRad);
|
||||
jsRoundRect = new(std::nothrow) JsRoundRect(roundRect);
|
||||
status = napi_wrap(env, jsThis, jsRoundRect,
|
||||
JsRoundRect::Destructor, nullptr, nullptr);
|
||||
if (status != napi_ok) {
|
||||
delete jsRoundRect;
|
||||
ROSEN_LOGE("JsRoundRect::Constructor Failed to wrap native instance");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return jsThis;
|
||||
}
|
||||
|
||||
void JsRoundRect::Destructor(napi_env env, void* nativeObject, void* finalize)
|
||||
{
|
||||
(void)finalize;
|
||||
if (nativeObject != nullptr) {
|
||||
JsRoundRect* napi = reinterpret_cast<JsRoundRect*>(nativeObject);
|
||||
delete napi;
|
||||
}
|
||||
}
|
||||
|
||||
JsRoundRect::~JsRoundRect()
|
||||
{
|
||||
m_roundRect = nullptr;
|
||||
}
|
||||
|
||||
std::shared_ptr<RoundRect> JsRoundRect::GetRoundRect()
|
||||
{
|
||||
return m_roundRect;
|
||||
}
|
||||
} // namespace Drawing
|
||||
} // namespace OHOS::Rosen
|
||||
|
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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_ROUNDRECT_H
|
||||
#define OHOS_ROSEN_JS_ROUNDRECT_H
|
||||
|
||||
#include <memory>
|
||||
#include <native_engine/native_engine.h>
|
||||
#include <native_engine/native_value.h>
|
||||
|
||||
#include "utils/round_rect.h"
|
||||
|
||||
namespace OHOS::Rosen {
|
||||
namespace Drawing {
|
||||
class JsRoundRect final {
|
||||
public:
|
||||
explicit JsRoundRect(std::shared_ptr<RoundRect> roundRect)
|
||||
: m_roundRect(roundRect) {};
|
||||
~JsRoundRect();
|
||||
|
||||
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);
|
||||
|
||||
std::shared_ptr<RoundRect> GetRoundRect();
|
||||
private:
|
||||
static thread_local napi_ref constructor_;
|
||||
|
||||
std::shared_ptr<RoundRect> m_roundRect = nullptr;
|
||||
};
|
||||
} // namespace Drawing
|
||||
} // namespace OHOS::Rosen
|
||||
#endif // OHOS_ROSEN_JS_ROUNDRECT_H
|
Loading…
Reference in New Issue
Block a user