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 enum PrintQualityCode {
39 PRINT_QUALITY_DRAFT = 3,
40 PRINT_QUALITY_NORMAL = 4,
41 PRINT_QUALITY_HIGH = 5
42 };
43
DpcToDpi(int dpc)44 inline int DpcToDpi(int dpc)
45 {
46 return dpc * 300 / 120; // 300 DPI = 120 DPC
47 }
48
49 template <typename T>
AddToUniqueList(std::vector<T> & list,T value)50 void AddToUniqueList(std::vector<T> &list, T value)
51 {
52 for (auto &item : list) {
53 if (item == value) {
54 return;
55 }
56 }
57 list.push_back(value);
58 }
59
60 template <typename T>
ConvertListToJson(const std::vector<T> & list,bool (* ConvertToJson)(const T &,nlohmann::json &))61 std::string ConvertListToJson(const std::vector<T> &list, bool (*ConvertToJson)(const T &, nlohmann::json &))
62 {
63 nlohmann::json array = nlohmann::json::array();
64 for (auto &item : list) {
65 nlohmann::json object;
66 if (ConvertToJson(item, object)) {
67 array.push_back(object);
68 }
69 }
70 return array.dump();
71 }
72
73 bool ConvertColorModeCode(const char *src, ColorModeCode &dst);
74 bool ConvertColorModeToJson(const ColorModeCode &code, nlohmann::json &jsonObject);
75 bool ConvertDuplexModeToJson(const DuplexModeCode &code, nlohmann::json &jsonObject);
76 bool ConvertPageSizeId(const char *src, std::string &id);
77 bool ConvertPrintPageSize(const char *src, PrintPageSize &dst);
78 bool ConvertPageSizeToJson(const PrintPageSize &code, nlohmann::json &jsonObject);
79 std::string GetQulityString(int code);
80 } // namespace Print
81 } // namespace OHOS
82 #endif // PRINT_SERVICE_CONVERTER_H
83