• 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 
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 
GetCustomPageNameFromMilSize(const std::string & name,const uint32_t width,const uint32_t height)39 std::string PrintPageSizeHelper::GetCustomPageNameFromMilSize(const std::string &name,
40     const uint32_t width, const uint32_t height)
41 {
42     if (name.find(CUSTOM_PREFIX) != std::string::npos) {
43         PRINT_HILOGI("Already custom page size.");
44         return name;
45     }
46     std::stringstream sizeName;
47     sizeName << CUSTOM_PREFIX << round(width / HUNDRED_OF_MILLIMETRE_TO_INCH) << "x"
48         << round(height / HUNDRED_OF_MILLIMETRE_TO_INCH) << "mm";
49     return sizeName.str();
50 }
51 
BuildFromJs(napi_env env,napi_value jsValue)52 std::shared_ptr<PrintPageSize> PrintPageSizeHelper::BuildFromJs(napi_env env, napi_value jsValue)
53 {
54     auto nativeObj = std::make_shared<PrintPageSize>();
55 
56     if (!ValidateProperty(env, jsValue)) {
57         PRINT_HILOGE("Invalid property of print page size");
58         return nullptr;
59     }
60 
61     std::string id = NapiPrintUtils::GetStringPropertyUtf8(env, jsValue, PARAM_PAGESIZE_ID);
62     std::string name = NapiPrintUtils::GetStringPropertyUtf8(env, jsValue, PARAM_PAGESIZE_NAME);
63     uint32_t width = NapiPrintUtils::GetUint32Property(env, jsValue, PARAM_PAGESIZE_WIDTH);
64     uint32_t height = NapiPrintUtils::GetUint32Property(env, jsValue, PARAM_PAGESIZE_HEIGHT);
65 
66     if (id.empty() || name.empty()) {
67         PRINT_HILOGE("Invalid resolution id or name");
68         return nullptr;
69     }
70 
71     PAGE_SIZE_ID ret = PrintPageSize::MatchPageSize(name);
72     if (ret.empty()) {
73         std::string customName = GetCustomPageNameFromMilSize(name, width, height);
74         id = customName;
75         name = customName;
76     }
77 
78     nativeObj->SetId(id);
79     nativeObj->SetName(name);
80     nativeObj->SetWidth(width);
81     nativeObj->SetHeight(height);
82     PRINT_HILOGI("Build Page Size success, id: %{public}s, name: %{public}s", id.c_str(), name.c_str());
83     return nativeObj;
84 }
85 
ValidateProperty(napi_env env,napi_value object)86 bool PrintPageSizeHelper::ValidateProperty(napi_env env, napi_value object)
87 {
88     std::map<std::string, PrintParamStatus> propertyList = {
89         {PARAM_PAGESIZE_ID, PRINT_PARAM_NOT_SET},
90         {PARAM_PAGESIZE_NAME, PRINT_PARAM_NOT_SET},
91         {PARAM_PAGESIZE_WIDTH, PRINT_PARAM_NOT_SET},
92         {PARAM_PAGESIZE_HEIGHT, PRINT_PARAM_NOT_SET},
93     };
94 
95     auto names = NapiPrintUtils::GetPropertyNames(env, object);
96     return NapiPrintUtils::VerifyProperty(names, propertyList);
97 }
98 } // namespace OHOS::Print
99