• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "print_resolution_helper.h"
16 #include "print_constant.h"
17 #include "napi_print_utils.h"
18 #include "print_log.h"
19 
20 namespace OHOS::Print {
21 static constexpr const char *PARAM_RESOLUTION_ID = "id";
22 static constexpr const char *PARAM_RESOLUTION_HORIZONTALDPI = "horizontalDpi";
23 static constexpr const char *PARAM_RESOLUTION_VERTICALDPI = "verticalDpi";
MakeJsObject(napi_env env,const PrintResolution & resolution)24 napi_value PrintResolutionHelper::MakeJsObject(napi_env env, const PrintResolution &resolution)
25 {
26     napi_value jsObj = nullptr;
27     PRINT_CALL(env, napi_create_object(env, &jsObj));
28     NapiPrintUtils::SetStringPropertyUtf8(env, jsObj, PARAM_RESOLUTION_ID, resolution.GetId());
29     NapiPrintUtils::SetUint32Property(env, jsObj, PARAM_RESOLUTION_HORIZONTALDPI, resolution.GetHorizontalDpi());
30     NapiPrintUtils::SetUint32Property(env, jsObj, PARAM_RESOLUTION_VERTICALDPI, resolution.GetVerticalDpi());
31     return jsObj;
32 }
33 
BuildFromJs(napi_env env,napi_value jsValue)34 std::shared_ptr<PrintResolution> PrintResolutionHelper::BuildFromJs(napi_env env, napi_value jsValue)
35 {
36     std::shared_ptr<PrintResolution> nativeObj = nullptr;
37     if (ValidateProperty(env, jsValue)) {
38         nativeObj = std::make_shared<PrintResolution>();
39         std::string id = NapiPrintUtils::GetStringPropertyUtf8(env, jsValue, PARAM_RESOLUTION_ID);
40         uint32_t horizontalDpi = NapiPrintUtils::GetUint32Property(env, jsValue, PARAM_RESOLUTION_HORIZONTALDPI);
41         uint32_t verticalDpi = NapiPrintUtils::GetUint32Property(env, jsValue, PARAM_RESOLUTION_VERTICALDPI);
42         if (nativeObj == nullptr) {
43             PRINT_HILOGE("nativeObj is nullptr");
44             return nullptr;
45         }
46         nativeObj->SetId(id);
47         nativeObj->SetHorizontalDpi(horizontalDpi);
48         nativeObj->SetVerticalDpi(verticalDpi);
49     }
50     return nativeObj;
51 }
52 
ValidateProperty(napi_env env,napi_value object)53 bool PrintResolutionHelper::ValidateProperty(napi_env env, napi_value object)
54 {
55     std::map<std::string, PrintParamStatus> propertyList = {
56         {PARAM_RESOLUTION_ID, PRINT_PARAM_NOT_SET},
57         {PARAM_RESOLUTION_HORIZONTALDPI, PRINT_PARAM_NOT_SET},
58         {PARAM_RESOLUTION_VERTICALDPI, PRINT_PARAM_NOT_SET},
59     };
60 
61     auto names = NapiPrintUtils::GetPropertyNames(env, object);
62     return NapiPrintUtils::VerifyProperty(names, propertyList);
63 }
64 }  // namespace OHOS::Print
65