1 /*
2 * Copyright (c) 2024 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 #ifndef PRINT_SERVICE_CONVERTER_H
17 #define PRINT_SERVICE_CONVERTER_H
18
19 #include <string>
20 #include <vector>
21 #include <json/json.h>
22 #include "print_page_size.h"
23 #include "print_json_util.h"
24
25 namespace OHOS {
26 namespace Print {
27 enum DuplexModeCode {
28 DUPLEX_MODE_ONE_SIDED = 0,
29 DUPLEX_MODE_TWO_SIDED_LONG_EDGE = 1,
30 DUPLEX_MODE_TWO_SIDED_SHORT_EDGE = 2,
31 };
32
33 enum ColorModeCode {
34 COLOR_MODE_MONOCHROME = 0,
35 COLOR_MODE_COLOR = 1,
36 COLOR_MODE_AUTO = 2
37 };
38
39 const int CONST_VALUE_300 = 300;
40 const int CONST_VALUE_120 = 120;
41
DpcToDpi(int dpc)42 inline int DpcToDpi(int dpc)
43 {
44 return dpc * CONST_VALUE_300 / CONST_VALUE_120; // 300 DPI = 120 DPC
45 }
46
47 template <typename T>
AddToUniqueList(std::vector<T> & list,T value)48 void AddToUniqueList(std::vector<T> &list, T value)
49 {
50 for (auto &item : list) {
51 if (item == value) {
52 return;
53 }
54 }
55 list.push_back(value);
56 }
57
58 template <typename T>
ConvertListToJson(const std::vector<T> & list,bool (* ConvertToJson)(const T &,Json::Value &))59 std::string ConvertListToJson(const std::vector<T> &list, bool (*ConvertToJson)(const T &, Json::Value &))
60 {
61 Json::Value array;
62 for (auto &item : list) {
63 Json::Value object;
64 if (ConvertToJson(item, object)) {
65 array.append(object);
66 }
67 }
68 return PrintJsonUtil::WriteString(array);
69 }
70
71 bool ConvertColorModeCode(const char *src, ColorModeCode &dst);
72 bool ConvertColorModeToJson(const ColorModeCode &code, Json::Value &jsonObject);
73 bool ConvertDuplexModeToJson(const DuplexModeCode &code, Json::Value &jsonObject);
74 bool ConvertPageSizeId(const char *src, std::string &id);
75 bool ConvertPrintPageSize(const char *src, PrintPageSize &dst);
76 bool ConvertPageSizeToJson(const PrintPageSize &code, Json::Value &jsonObject);
77 bool ConvertCustomPageSizeFromPwgName(const char *src, PrintPageSize &dst);
78 std::string GetQulityString(int code);
79 } // namespace Print
80 } // namespace OHOS
81 #endif // PRINT_SERVICE_CONVERTER_H
82