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