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 "printer_capability_helper.h"
16 #include "napi_print_utils.h"
17 #include "print_constant.h"
18 #include "print_log.h"
19 #include "print_margin_helper.h"
20 #include "print_page_size_helper.h"
21 #include "print_resolution_helper.h"
22
23 namespace OHOS::Print {
24 static constexpr const char *PARAM_CAPABILITY_COLORMODE = "colorMode";
25 static constexpr const char *PARAM_CAPABILITY_DUPLEXMODE = "duplexMode";
26 static constexpr const char *PARAM_CAPABILITY_PAGESIZE = "pageSize";
27 static constexpr const char *PARAM_CAPABILITY_RESOLUTION = "resolution";
28 static constexpr const char *PARAM_CAPABILITY_MINMARGIN = "minMargin";
29 static constexpr const char *PARAM_CAPABILITY_OPTION = "options";
30
MakeJsObject(napi_env env,const PrinterCapability & cap)31 napi_value PrinterCapabilityHelper::MakeJsObject(napi_env env, const PrinterCapability &cap)
32 {
33 napi_value jsObj = nullptr;
34 PRINT_CALL(env, napi_create_object(env, &jsObj));
35
36 NapiPrintUtils::SetUint32Property(env, jsObj, PARAM_CAPABILITY_COLORMODE, cap.GetColorMode());
37 NapiPrintUtils::SetUint32Property(env, jsObj, PARAM_CAPABILITY_DUPLEXMODE, cap.GetDuplexMode());
38
39 CreatePageSizeList(env, jsObj, cap);
40 if (cap.HasResolution()) {
41 CreateResolutionList(env, jsObj, cap);
42 }
43
44 if (cap.HasMargin()) {
45 PrintMargin margin;
46 cap.GetMinMargin(margin);
47 napi_value jsMargin = PrintMarginHelper::MakeJsObject(env, margin);
48 PRINT_CALL(env, napi_set_named_property(env, jsObj, PARAM_CAPABILITY_MINMARGIN, jsMargin));
49 }
50 if (cap.HasOption()) {
51 NapiPrintUtils::SetStringPropertyUtf8(env, jsObj, PARAM_CAPABILITY_OPTION, cap.GetOption());
52 }
53 return jsObj;
54 }
55
BuildFromJs(napi_env env,napi_value jsValue)56 std::shared_ptr<PrinterCapability> PrinterCapabilityHelper::BuildFromJs(napi_env env, napi_value jsValue)
57 {
58 auto nativeObj = std::make_shared<PrinterCapability>();
59
60 if (!ValidateProperty(env, jsValue)) {
61 PRINT_HILOGE("Invalid property of printer capability");
62 return nullptr;
63 }
64
65 uint32_t colorMode = NapiPrintUtils::GetUint32Property(env, jsValue, PARAM_CAPABILITY_COLORMODE);
66 uint32_t duplexMode = NapiPrintUtils::GetUint32Property(env, jsValue, PARAM_CAPABILITY_DUPLEXMODE);
67 nativeObj->SetColorMode(colorMode);
68 nativeObj->SetDuplexMode(duplexMode);
69
70 napi_value jsPageSizes = NapiPrintUtils::GetNamedProperty(env, jsValue, PARAM_CAPABILITY_PAGESIZE);
71 bool isArray = false;
72 napi_is_array(env, jsPageSizes, &isArray);
73 if (!isArray) {
74 PRINT_HILOGE("Invalid list of page size");
75 return nullptr;
76 }
77
78 std::vector<PrintPageSize> pageSizes;
79 uint32_t arrayLength = 0;
80 napi_get_array_length(env, jsPageSizes, &arrayLength);
81 for (uint32_t index = 0; index < arrayLength; index++) {
82 napi_value jsPageSize;
83 napi_get_element(env, jsPageSizes, index, &jsPageSize);
84 auto pageSizePtr = PrintPageSizeHelper::BuildFromJs(env, jsPageSize);
85 if (pageSizePtr == nullptr) {
86 PRINT_HILOGE("Failed to build printer capability object from js");
87 return nullptr;
88 }
89 pageSizes.emplace_back(*pageSizePtr);
90 }
91 nativeObj->SetPageSize(pageSizes);
92 auto jsOption = NapiPrintUtils::GetNamedProperty(env, jsValue, PARAM_CAPABILITY_OPTION);
93 if (jsOption != nullptr) {
94 nativeObj->SetOption(NapiPrintUtils::GetStringPropertyUtf8(env, jsValue, PARAM_CAPABILITY_OPTION));
95 }
96 PRINT_HILOGI("Build Print Capability succeed");
97 return BuildFromJsSecond(env, jsValue, jsPageSizes, nativeObj);
98 }
99
BuildFromJsSecond(napi_env env,napi_value jsValue,napi_value jsPageSizes,std::shared_ptr<PrinterCapability> nativeObj)100 std::shared_ptr<PrinterCapability> PrinterCapabilityHelper::BuildFromJsSecond(napi_env env, napi_value jsValue,
101 napi_value jsPageSizes, std::shared_ptr<PrinterCapability> nativeObj)
102 {
103 napi_value jsResolutionList = NapiPrintUtils::GetNamedProperty(env, jsValue, PARAM_CAPABILITY_RESOLUTION);
104 if (jsResolutionList != nullptr) {
105 bool isArray = false;
106 napi_is_array(env, jsResolutionList, &isArray);
107 if (!isArray) {
108 PRINT_HILOGE("Invalid list of print resolution");
109 return nullptr;
110 }
111 std::vector<PrintResolution> resolutionList;
112 uint32_t arrayLength = 0;
113 napi_get_array_length(env, jsPageSizes, &arrayLength);
114 for (uint32_t index = 0; index < arrayLength; index++) {
115 napi_value jsResolution;
116 napi_get_element(env, jsResolutionList, index, &jsResolution);
117 auto resolutionPtr = PrintResolutionHelper::BuildFromJs(env, jsResolution);
118 if (resolutionPtr == nullptr) {
119 PRINT_HILOGE("Failed to build printer capability object from js");
120 return nullptr;
121 }
122 resolutionList.emplace_back(*resolutionPtr);
123 }
124 nativeObj->SetResolution(resolutionList);
125 }
126
127 napi_value jsMargin = NapiPrintUtils::GetNamedProperty(env, jsValue, PARAM_CAPABILITY_MINMARGIN);
128 if (jsMargin != nullptr) {
129 auto marginPtr = PrintMarginHelper::BuildFromJs(env, jsMargin);
130 if (marginPtr == nullptr) {
131 PRINT_HILOGE("Failed to build printer capability object from js");
132 return nullptr;
133 }
134 nativeObj->SetMinMargin(*marginPtr);
135 }
136 return nativeObj;
137 }
138
CreatePageSizeList(napi_env env,napi_value & jsPrinterCap,const PrinterCapability & cap)139 bool PrinterCapabilityHelper::CreatePageSizeList(napi_env env, napi_value &jsPrinterCap, const PrinterCapability &cap)
140 {
141 napi_value jsPageSizes = nullptr;
142 PRINT_CALL_BASE(env, napi_create_array(env, &jsPageSizes), false);
143 std::vector<PrintPageSize> pageSizeList;
144 cap.GetPageSize(pageSizeList);
145 uint32_t arrLength = pageSizeList.size();
146
147 for (uint32_t index = 0; index < arrLength; index++) {
148 napi_value value = PrintPageSizeHelper::MakeJsObject(env, pageSizeList[index]);
149 PRINT_CALL_BASE(env, napi_set_element(env, jsPageSizes, index, value), false);
150 }
151 PRINT_CALL_BASE(env, napi_set_named_property(env, jsPrinterCap, PARAM_CAPABILITY_PAGESIZE, jsPageSizes), false);
152 return true;
153 }
154
CreateResolutionList(napi_env env,napi_value & jsPrinterCap,const PrinterCapability & cap)155 bool PrinterCapabilityHelper::CreateResolutionList(napi_env env, napi_value &jsPrinterCap, const PrinterCapability &cap)
156 {
157 napi_value jsResolutionList = nullptr;
158 PRINT_CALL_BASE(env, napi_create_array(env, &jsResolutionList), false);
159 std::vector<PrintResolution> resolutionList;
160 uint32_t arrLength = resolutionList.size();
161
162 for (uint32_t index = 0; index < arrLength; index++) {
163 napi_value value = PrintResolutionHelper::MakeJsObject(env, resolutionList[index]);
164 PRINT_CALL_BASE(env, napi_set_element(env, jsResolutionList, index, value), false);
165 }
166 PRINT_CALL_BASE(env,
167 napi_set_named_property(env, jsPrinterCap, PARAM_CAPABILITY_RESOLUTION, jsResolutionList), false);
168 return true;
169 }
170
ValidateProperty(napi_env env,napi_value object)171 bool PrinterCapabilityHelper::ValidateProperty(napi_env env, napi_value object)
172 {
173 std::map<std::string, PrintParamStatus> propertyList = {
174 {PARAM_CAPABILITY_COLORMODE, PRINT_PARAM_NOT_SET},
175 {PARAM_CAPABILITY_DUPLEXMODE, PRINT_PARAM_NOT_SET},
176 {PARAM_CAPABILITY_PAGESIZE, PRINT_PARAM_NOT_SET},
177 {PARAM_CAPABILITY_RESOLUTION, PRINT_PARAM_OPT},
178 {PARAM_CAPABILITY_MINMARGIN, PRINT_PARAM_OPT},
179 {PARAM_CAPABILITY_OPTION, PRINT_PARAM_OPT},
180 };
181
182 auto names = NapiPrintUtils::GetPropertyNames(env, object);
183 return NapiPrintUtils::VerifyProperty(names, propertyList);
184 }
185 } // namespace OHOS::Print
186