1 /*
2 * Copyright (c) 2023 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_service_converter.h"
17 #include "print_log.h"
18 #include <sstream>
19 #include <cstdlib> // for std::strtod
20 #include <cerrno> // for errno
21
22 namespace OHOS {
23 namespace Print {
GetQulityString(int code)24 std::string GetQulityString(int code)
25 {
26 return std::to_string(code);
27 }
28
ConvertColorModeCode(const char * src,ColorModeCode & dst)29 bool ConvertColorModeCode(const char *src, ColorModeCode &dst)
30 {
31 if (src == nullptr) {
32 return false;
33 }
34 if (strcasestr(src, "color")) {
35 dst = COLOR_MODE_COLOR;
36 } else if (strcasestr(src, "monochrome")) {
37 dst = COLOR_MODE_MONOCHROME;
38 } else if (strcasestr(src, "auto")) {
39 dst = COLOR_MODE_AUTO;
40 } else {
41 return false;
42 }
43 return true;
44 }
45
ConvertColorModeToJson(const ColorModeCode & code,Json::Value & jsonObject)46 bool ConvertColorModeToJson(const ColorModeCode &code, Json::Value &jsonObject)
47 {
48 jsonObject["color"] = std::to_string(static_cast<int>(code));
49 return true;
50 }
51
ConvertDuplexModeToJson(const DuplexModeCode & code,Json::Value & jsonObject)52 bool ConvertDuplexModeToJson(const DuplexModeCode &code, Json::Value &jsonObject)
53 {
54 jsonObject["duplex"] = std::to_string(static_cast<int>(code));
55 return true;
56 }
57
ConvertPageSizeId(const char * src,std::string & id)58 bool ConvertPageSizeId(const char *src, std::string &id)
59 {
60 if (src == nullptr) {
61 return false;
62 }
63 std::string pageString(src);
64 id = PrintPageSize::MatchPageSize(pageString);
65 return !id.empty();
66 }
67
ConvertStrToDouble(const std::string & str,double & value)68 bool ConvertStrToDouble(const std::string& str, double& value)
69 {
70 char* endPtr = nullptr;
71 const char* cstr = str.c_str();
72
73 errno = 0;
74 value = std::strtod(cstr, &endPtr);
75
76 if (cstr == endPtr) {
77 return false;
78 }
79 if (errno == ERANGE) {
80 errno = 0;
81 return false;
82 }
83 if (*endPtr != '\0') {
84 return false;
85 }
86 return true;
87 }
88
ConvertCustomPageSizeFromWidthAndLength(const double & widthValue,const double & lengthValue,const std::string & unit,PrintPageSize & dst)89 bool ConvertCustomPageSizeFromWidthAndLength(const double& widthValue, const double& lengthValue,
90 const std::string& unit, PrintPageSize &dst)
91 {
92 uint32_t width = 0;
93 uint32_t length = 0;
94
95 std::stringstream postfixName;
96 if (unit == "mm") {
97 width = round(widthValue * HUNDRED_OF_MILLIMETRE_TO_INCH);
98 length = round(lengthValue * HUNDRED_OF_MILLIMETRE_TO_INCH);
99 postfixName << round(widthValue) << "x" << round(lengthValue) << "mm";
100 } else if (unit == "in") {
101 width = round(widthValue * ONE_THOUSAND_INCH);
102 length = round(lengthValue * ONE_THOUSAND_INCH);
103 postfixName << round(widthValue * ONE_THOUSAND_INCH / HUNDRED_OF_MILLIMETRE_TO_INCH) << "x" <<
104 round(lengthValue * ONE_THOUSAND_INCH / HUNDRED_OF_MILLIMETRE_TO_INCH) << "mm";
105 } else {
106 PRINT_HILOGE("IPP media-supported do not found unitstr");
107 return false;
108 }
109 std::string pwgName = CUSTOM_PREFIX + postfixName.str();
110 dst = PrintPageSize(pwgName, pwgName, width, length);
111 return true;
112 }
113
ConvertCustomPageSizeFromPwgName(const char * src,PrintPageSize & dst)114 bool ConvertCustomPageSizeFromPwgName(const char *src, PrintPageSize &dst)
115 {
116 if (src == nullptr) {
117 PRINT_HILOGE("IPP media-supported src is empty");
118 return false;
119 }
120 std::string pwgNameStr(src);
121 size_t lastPartPos = pwgNameStr.find_last_of('_');
122 size_t firstPartPos = pwgNameStr.find_first_of('_');
123 if (lastPartPos == std::string::npos || firstPartPos == std::string::npos ||
124 lastPartPos + 1 >= pwgNameStr.size() || lastPartPos - firstPartPos - 1 < 0) {
125 PRINT_HILOGE("IPP media-supported do not found \"_\"");
126 return false;
127 }
128 std::string middleNmae = pwgNameStr.substr(firstPartPos + 1, lastPartPos - firstPartPos - 1);
129 if (middleNmae == "max" || middleNmae == "min") {
130 PRINT_HILOGE("IPP media-supported contains max or min");
131 return false;
132 }
133 std::string sizeName = pwgNameStr.substr(lastPartPos + 1);
134 size_t xPos = sizeName.find('x');
135 if (xPos == std::string::npos || xPos + 1 >= sizeName.size() ||
136 sizeName.size() - xPos - PAGE_SIZE_UNIT_LENGTH - 1 < 0 || sizeName.size() - PAGE_SIZE_UNIT_LENGTH < 0) {
137 PRINT_HILOGE("IPP media-supported do not found \"x\"");
138 return false;
139 }
140 std::string widthStr = sizeName.substr(0, xPos);
141 std::string lengthStr = sizeName.substr(xPos + 1, sizeName.size() - xPos - PAGE_SIZE_UNIT_LENGTH - 1);
142 std::string unit = sizeName.substr(sizeName.size() - PAGE_SIZE_UNIT_LENGTH);
143 double widthValue = 0;
144 double lengthValue = 0;
145
146 if (!ConvertStrToDouble(widthStr, widthValue) || !ConvertStrToDouble(lengthStr, lengthValue)) {
147 PRINT_HILOGE("Bad SafeStof Convert!");
148 return false;
149 }
150
151 return ConvertCustomPageSizeFromWidthAndLength(widthValue, lengthValue, unit, dst);
152 }
153
ConvertPrintPageSize(const char * src,PrintPageSize & dst)154 bool ConvertPrintPageSize(const char *src, PrintPageSize &dst)
155 {
156 if (src == nullptr) {
157 PRINT_HILOGE("ConvertPrintPageSize is empty");
158 return false;
159 }
160 std::string id;
161 if (ConvertPageSizeId(src, id)) {
162 PRINT_HILOGI("IPP media-supported match pageId: %{public}s", id.c_str());
163 return PrintPageSize::FindPageSizeById(id, dst);
164 }
165 // Not standard, use custom page size
166 return ConvertCustomPageSizeFromPwgName(src, dst);
167 }
168
ConvertPageSizeToJson(const PrintPageSize & pageSize,Json::Value & jsonObject)169 bool ConvertPageSizeToJson(const PrintPageSize &pageSize, Json::Value &jsonObject)
170 {
171 jsonObject["id"] = pageSize.GetId();
172 jsonObject["name"] = pageSize.GetName();
173 jsonObject["width"] = pageSize.GetWidth();
174 jsonObject["height"] = pageSize.GetHeight();
175 return true;
176 }
177 } // namespace Print
178 } // namespace OHOS