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_page_size_helper.h"
17 #include "napi_print_utils.h"
18 #include "print_constant.h"
19 #include "print_log.h"
20 #include <sstream>
21
22 namespace OHOS::Print {
23 static constexpr const char *PARAM_PAGESIZE_ID = "id";
24 static constexpr const char *PARAM_PAGESIZE_NAME = "name";
25 static constexpr const char *PARAM_PAGESIZE_WIDTH = "width";
26 static constexpr const char *PARAM_PAGESIZE_HEIGHT = "height";
MakeJsObject(napi_env env,const PrintPageSize & pageSize)27 napi_value PrintPageSizeHelper::MakeJsObject(napi_env env, const PrintPageSize &pageSize)
28 {
29 napi_value jsObj = nullptr;
30 PRINT_CALL(env, napi_create_object(env, &jsObj));
31
32 NapiPrintUtils::SetStringPropertyUtf8(env, jsObj, PARAM_PAGESIZE_ID, pageSize.GetId());
33 NapiPrintUtils::SetStringPropertyUtf8(env, jsObj, PARAM_PAGESIZE_NAME, pageSize.GetName());
34 NapiPrintUtils::SetUint32Property(env, jsObj, PARAM_PAGESIZE_WIDTH, pageSize.GetWidth());
35 NapiPrintUtils::SetUint32Property(env, jsObj, PARAM_PAGESIZE_HEIGHT, pageSize.GetHeight());
36 return jsObj;
37 }
38
BuildFromJs(napi_env env,napi_value jsValue)39 std::shared_ptr<PrintPageSize> PrintPageSizeHelper::BuildFromJs(napi_env env, napi_value jsValue)
40 {
41 auto nativeObj = std::make_shared<PrintPageSize>();
42
43 if (!ValidateProperty(env, jsValue)) {
44 PRINT_HILOGE("Invalid property of print page size");
45 return nullptr;
46 }
47
48 std::string id = NapiPrintUtils::GetStringPropertyUtf8(env, jsValue, PARAM_PAGESIZE_ID);
49 std::string name = NapiPrintUtils::GetStringPropertyUtf8(env, jsValue, PARAM_PAGESIZE_NAME);
50 uint32_t width = NapiPrintUtils::GetUint32Property(env, jsValue, PARAM_PAGESIZE_WIDTH);
51 uint32_t height = NapiPrintUtils::GetUint32Property(env, jsValue, PARAM_PAGESIZE_HEIGHT);
52
53 if (id.empty() || name.empty()) {
54 PRINT_HILOGE("Invalid resolution id or name");
55 return nullptr;
56 }
57 PrintPageSize pageSize(id, name, width, height);
58 PAGE_SIZE_ID ret = PrintPageSize::MatchPageSize(name);
59 if (ret.empty() && !pageSize.ConvertToPwgStyle()) {
60 PRINT_HILOGE("pwgMedia build fail from JS!");
61 return nullptr;
62 }
63 *nativeObj = std::move(pageSize);
64 PRINT_HILOGI("Build Page Size success, id: %{public}s, name: %{public}s",
65 pageSize.GetId().c_str(), pageSize.GetName().c_str());
66 return nativeObj;
67 }
68
ValidateProperty(napi_env env,napi_value object)69 bool PrintPageSizeHelper::ValidateProperty(napi_env env, napi_value object)
70 {
71 std::map<std::string, PrintParamStatus> propertyList = {
72 {PARAM_PAGESIZE_ID, PRINT_PARAM_NOT_SET},
73 {PARAM_PAGESIZE_NAME, PRINT_PARAM_NOT_SET},
74 {PARAM_PAGESIZE_WIDTH, PRINT_PARAM_NOT_SET},
75 {PARAM_PAGESIZE_HEIGHT, PRINT_PARAM_NOT_SET},
76 };
77
78 auto names = NapiPrintUtils::GetPropertyNames(env, object);
79 return NapiPrintUtils::VerifyProperty(names, propertyList);
80 }
81 } // namespace OHOS::Print
82