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 #include "js_typeface.h"
17 #include "../js_drawing_utils.h"
18 #include "native_value.h"
19
20 namespace OHOS::Rosen {
21 namespace Drawing {
22 thread_local napi_ref JsTypeface::constructor_ = nullptr;
23 std::shared_ptr<Typeface> drawingTypeface;
24 const std::string CLASS_NAME = "Typeface";
Init(napi_env env,napi_value exportObj)25 napi_value JsTypeface::Init(napi_env env, napi_value exportObj)
26 {
27 napi_property_descriptor properties[] = {
28 DECLARE_NAPI_FUNCTION("getFamilyName", JsTypeface::GetFamilyName),
29 };
30
31 napi_value constructor = nullptr;
32 napi_status status = napi_define_class(env, CLASS_NAME.c_str(), NAPI_AUTO_LENGTH, Constructor, nullptr,
33 sizeof(properties) / sizeof(properties[0]), properties, &constructor);
34 if (status != napi_ok) {
35 ROSEN_LOGE("Failed to define Typeface class");
36 return nullptr;
37 }
38
39 status = napi_create_reference(env, constructor, 1, &constructor_);
40 if (status != napi_ok) {
41 ROSEN_LOGE("Failed to create reference of constructor");
42 return nullptr;
43 }
44
45 status = napi_set_named_property(env, exportObj, CLASS_NAME.c_str(), constructor);
46 if (status != napi_ok) {
47 ROSEN_LOGE("Failed to set constructor");
48 return nullptr;
49 }
50
51 return exportObj;
52 }
53
Constructor(napi_env env,napi_callback_info info)54 napi_value JsTypeface::Constructor(napi_env env, napi_callback_info info)
55 {
56 size_t argCount = 0;
57 napi_value jsThis = nullptr;
58 napi_status status = napi_get_cb_info(env, info, &argCount, nullptr, &jsThis, nullptr);
59 if (status != napi_ok) {
60 ROSEN_LOGE("failed to napi_get_cb_info");
61 return nullptr;
62 }
63
64 JsTypeface *jsTypeface = new(std::nothrow) JsTypeface(Typeface::MakeDefault());
65
66 status = napi_wrap(env, jsThis, jsTypeface, JsTypeface::Destructor, nullptr, nullptr);
67 if (status != napi_ok) {
68 delete jsTypeface;
69 ROSEN_LOGE("Failed to wrap native instance");
70 return nullptr;
71 }
72 return jsThis;
73 }
74
Destructor(napi_env env,void * nativeObject,void * finalize)75 void JsTypeface::Destructor(napi_env env, void *nativeObject, void *finalize)
76 {
77 (void)finalize;
78 if (nativeObject != nullptr) {
79 JsTypeface *napi = reinterpret_cast<JsTypeface *>(nativeObject);
80 delete napi;
81 }
82 }
83
~JsTypeface()84 JsTypeface::~JsTypeface()
85 {
86 m_typeface = nullptr;
87 }
88
CreateJsTypeface(napi_env env,const std::shared_ptr<Typeface> typeface)89 napi_value JsTypeface::CreateJsTypeface(napi_env env, const std::shared_ptr<Typeface> typeface)
90 {
91 napi_value constructor = nullptr;
92 napi_value result = nullptr;
93 napi_status status = napi_get_reference_value(env, constructor_, &constructor);
94 if (status == napi_ok) {
95 drawingTypeface = typeface;
96 status = napi_new_instance(env, constructor, 0, nullptr, &result);
97 if (status == napi_ok) {
98 return result;
99 } else {
100 ROSEN_LOGE("Drawing_napi: New instance could not be obtained");
101 }
102 }
103 return result;
104 }
105
GetTypeface()106 std::shared_ptr<Typeface> JsTypeface::GetTypeface()
107 {
108 return m_typeface;
109 }
110
GetFamilyName(napi_env env,napi_callback_info info)111 napi_value JsTypeface::GetFamilyName(napi_env env, napi_callback_info info)
112 {
113 JsTypeface* me = CheckParamsAndGetThis<JsTypeface>(env, info);
114 return (me != nullptr) ? me->OnGetFamilyName(env, info) : nullptr;
115 }
116
OnGetFamilyName(napi_env env,napi_callback_info info)117 napi_value JsTypeface::OnGetFamilyName(napi_env env, napi_callback_info info)
118 {
119 if (m_typeface == nullptr) {
120 ROSEN_LOGE("[NAPI]typeface is null");
121 return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM, "Invalid params.");
122 }
123
124 auto name = m_typeface->GetFamilyName();
125 return GetStringAndConvertToJsValue(env, name);
126 }
127 } // namespace Drawing
128 } // namespace OHOS::Rosen