1 /*
2  * Copyright (c) 2022 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 "print_margin_helper.h"
17 #include "napi_print_utils.h"
18 #include "print_constant.h"
19 #include "print_log.h"
20 
21 namespace OHOS::Print {
22 static constexpr const char *PARAM_MARGIN_TOP = "top";
23 static constexpr const char *PARAM_MARGIN_BOTTOM = "bottom";
24 static constexpr const char *PARAM_MARGIN_LEFT = "left";
25 static constexpr const char *PARAM_MARGIN_RIGHT = "right";
MakeJsObject(napi_env env,const PrintMargin & margin)26 napi_value PrintMarginHelper::MakeJsObject(napi_env env,  const PrintMargin &margin)
27 {
28     napi_value jsObj = nullptr;
29     PRINT_CALL(env, napi_create_object(env, &jsObj));
30     if (margin.HasTop()) {
31         NapiPrintUtils::SetUint32Property(env, jsObj, PARAM_MARGIN_TOP, margin.GetTop());
32     }
33 
34     if (margin.HasBottom()) {
35         NapiPrintUtils::SetUint32Property(env, jsObj, PARAM_MARGIN_BOTTOM, margin.GetBottom());
36     }
37 
38     if (margin.HasLeft()) {
39         NapiPrintUtils::SetUint32Property(env, jsObj, PARAM_MARGIN_LEFT, margin.GetLeft());
40     }
41 
42     if (margin.HasRight()) {
43         NapiPrintUtils::SetUint32Property(env, jsObj, PARAM_MARGIN_RIGHT, margin.GetRight());
44     }
45     return jsObj;
46 }
47 
BuildFromJs(napi_env env,napi_value jsValue)48 std::shared_ptr<PrintMargin> PrintMarginHelper::BuildFromJs(napi_env env, napi_value jsValue)
49 {
50     auto nativeObj = std::make_shared<PrintMargin>();
51 
52     if (!ValidateProperty(env, jsValue)) {
53         PRINT_HILOGE("Invalid property of print margin");
54         return nullptr;
55     }
56 
57     auto jsTop = NapiPrintUtils::GetNamedProperty(env, jsValue, PARAM_MARGIN_TOP);
58     if (jsTop != nullptr) {
59         nativeObj->SetTop(NapiPrintUtils::GetUint32FromValue(env, jsTop));
60     }
61 
62     auto jsBottom = NapiPrintUtils::GetNamedProperty(env, jsValue, PARAM_MARGIN_BOTTOM);
63     if (jsBottom != nullptr) {
64         nativeObj->SetBottom(NapiPrintUtils::GetUint32FromValue(env, jsBottom));
65     }
66 
67     auto jsLeft = NapiPrintUtils::GetNamedProperty(env, jsValue, PARAM_MARGIN_LEFT);
68     if (jsLeft != nullptr) {
69         nativeObj->SetLeft(NapiPrintUtils::GetUint32FromValue(env, jsLeft));
70     }
71 
72     auto jsRight = NapiPrintUtils::GetNamedProperty(env, jsValue, PARAM_MARGIN_RIGHT);
73     if (jsRight != nullptr) {
74         nativeObj->SetRight(NapiPrintUtils::GetUint32FromValue(env, jsRight));
75     }
76 
77     PRINT_HILOGI("Build Print Margin succeed");
78     return nativeObj;
79 }
80 
ValidateProperty(napi_env env,napi_value object)81 bool PrintMarginHelper::ValidateProperty(napi_env env, napi_value object)
82 {
83     std::map<std::string, PrintParamStatus> propertyList = {
84         {PARAM_MARGIN_TOP, PRINT_PARAM_OPT},
85         {PARAM_MARGIN_BOTTOM, PRINT_PARAM_OPT},
86         {PARAM_MARGIN_LEFT, PRINT_PARAM_OPT},
87         {PARAM_MARGIN_RIGHT, PRINT_PARAM_OPT},
88     };
89 
90     auto names = NapiPrintUtils::GetPropertyNames(env, object);
91     for (auto name : names) {
92         if (propertyList.find(name) == propertyList.end()) {
93             PRINT_HILOGE("Invalid property: %{public}s", name.c_str());
94             return false;
95         }
96         propertyList[name] = PRINT_PARAM_SET;
97     }
98 
99     return true;
100 }
101 }  // namespace OHOS::Print
102