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 
16 #include "print_page_size.h"
17 #include "print_log.h"
18 
19 namespace {
20 const uint32_t ISO_A0_WIDTH  = 33110;
21 const uint32_t ISO_A0_HEIGHT = 46810;
22 const uint32_t ISO_A1_WIDTH = 23390;
23 const uint32_t ISO_A1_HEIGHT = 33110;
24 const uint32_t ISO_A2_WIDTH = 16540;
25 const uint32_t ISO_A2_HEIGHT = 23390;
26 const uint32_t ISO_A3_WIDTH = 11690;
27 const uint32_t ISO_A3_HEIGHT = 16540;
28 const uint32_t ISO_A4_WIDTH = 8268;
29 const uint32_t ISO_A4_HEIGHT = 11692;
30 const uint32_t ISO_A5_WIDTH = 5830;
31 const uint32_t ISO_A5_HEIGHT = 8270;
32 const uint32_t ISO_A6_WIDTH = 4130;
33 const uint32_t ISO_A6_HEIGHT = 5830;
34 const uint32_t ISO_B0_WIDTH = 39370;
35 const uint32_t ISO_B0_HEIGHT = 55670;
36 const uint32_t ISO_B1_WIDTH = 27830;
37 const uint32_t ISO_B1_HEIGHT = 39370;
38 const uint32_t ISO_B2_WIDTH = 19690;
39 const uint32_t ISO_B2_HEIGHT = 27830;
40 const uint32_t ISO_B3_WIDTH = 13900;
41 const uint32_t ISO_B3_HEIGHT = 19690;
42 const uint32_t ISO_B4_WIDTH = 9840;
43 const uint32_t ISO_B4_HEIGHT = 13900;
44 const uint32_t ISO_B5_WIDTH = 6930;
45 const uint32_t ISO_B5_HEIGHT = 9840;
46 const uint32_t ISO_C5_WIDTH = 6380;
47 const uint32_t ISO_C5_HEIGHT = 9020;
48 const uint32_t ISO_DL_WIDTH = 4330;
49 const uint32_t ISO_DL_HEIGHT = 8660;
50 const uint32_t LEGAL_WIDTH = 8500;
51 const uint32_t LEGAL_HEIGHT = 14000;
52 const uint32_t LETTER_WIDTH = 8500;
53 const uint32_t LETTER_HEIGHT = 11000;
54 const uint32_t ROC_16K_WIDTH = 7677;
55 const uint32_t ROC_16K_HEIGHT = 10629;
56 const uint32_t ROC_8K_WIDTH = 10629;
57 const uint32_t ROC_8K_HEIGHT = 15354;
58 const uint32_t JIS_B4_WIDTH = 10119;
59 const uint32_t JIS_B4_HEIGHT = 14331;
60 const uint32_t JIS_B5_WIDTH = 7165;
61 const uint32_t JIS_B5_HEIGHT = 10118;
62 const uint32_t PHOTO_5R_WIDTH = 5000;
63 const uint32_t PHOTO_5R_HEIGHT = 7000;
64 const uint32_t PHOTO_4R_WIDTH = 4000;
65 const uint32_t PHOTO_4R_HEIGHT = 6000;
66 const uint32_t NA_GOVT_LETTER_8IN_WIDTH = 8000;
67 const uint32_t NA_GOVT_LETTER_8IN_HEIGHT = 10000;
68 const uint32_t NA_LEDGER_11IN_WIDTH = 11000;
69 const uint32_t NA_LEDGER_11IN_HEIGHT = 17000;
70 const uint32_t JPN_HAGAKI_WIDTH = 3940;
71 const uint32_t JPN_HAGAKI_HEIGHT = 5830;
72 const uint32_t OM_SDC_PHOTO_WIDTH = 3504;
73 const uint32_t OM_SDC_PHOTO_HEIGHT = 4685;
74 const uint32_t OM_CARD_WIDTH = 2126;
75 const uint32_t OM_CARD_HEIGHT = 3386;
76 const uint32_t OE_PHOTO_L_WIDTH = 3500;
77 const uint32_t OE_PHOTO_L_HEIGHT = 5000;
78 const uint32_t B_TABLOID_L_WIDTH = 11000;
79 const uint32_t B_TABLOID_L_HEIGHT = 17000;
80 }
81 
82 namespace OHOS::Print {
83 std::map<PAGE_SIZE_ID, std::shared_ptr<PrintPageSize>> PrintPageSize::pageSize_;
84 std::mutex PrintPageSize::pageSizeMapMutex;
85 
UpdatePageSizeMapIso(std::map<PAGE_SIZE_ID,std::shared_ptr<PrintPageSize>> & pageSizeMap)86 void UpdatePageSizeMapIso(std::map<PAGE_SIZE_ID, std::shared_ptr<PrintPageSize>>& pageSizeMap)
87 {
88     pageSizeMap["ISO_A0"] =
89         std::make_shared<PrintPageSize>("ISO_A0", "iso_a0_841x1189mm", ISO_A0_WIDTH, ISO_A0_HEIGHT);
90     pageSizeMap["ISO_A1"] =
91         std::make_shared<PrintPageSize>("ISO_A1", "iso_a1_594x841mm", ISO_A1_WIDTH, ISO_A1_HEIGHT);
92     pageSizeMap["ISO_A2"] =
93         std::make_shared<PrintPageSize>("ISO_A2", "iso_a2_420x594mm", ISO_A2_WIDTH, ISO_A2_HEIGHT);
94     pageSizeMap["ISO_A3"] =
95         std::make_shared<PrintPageSize>("ISO_A3", "iso_a3_297x420mm", ISO_A3_WIDTH, ISO_A3_HEIGHT);
96     pageSizeMap["ISO_A4"] =
97         std::make_shared<PrintPageSize>("ISO_A4", "iso_a4_210x297mm", ISO_A4_WIDTH, ISO_A4_HEIGHT);
98     pageSizeMap["ISO_A5"] =
99         std::make_shared<PrintPageSize>("ISO_A5", "iso_a5_148x210mm", ISO_A5_WIDTH, ISO_A5_HEIGHT);
100     pageSizeMap["ISO_A6"] =
101         std::make_shared<PrintPageSize>("ISO_A6", "iso_a6_105x148mm", ISO_A6_WIDTH, ISO_A6_HEIGHT);
102     pageSizeMap["ISO_B0"] =
103         std::make_shared<PrintPageSize>("ISO_B0", "iso_b0_1000x1414mm", ISO_B0_WIDTH, ISO_B0_HEIGHT);
104     pageSizeMap["ISO_B1"] =
105         std::make_shared<PrintPageSize>("ISO_B1", "iso_b1_707x1000mm", ISO_B1_WIDTH, ISO_B1_HEIGHT);
106     pageSizeMap["ISO_B2"] =
107         std::make_shared<PrintPageSize>("ISO_B2", "iso_b2_500x707mm", ISO_B2_WIDTH, ISO_B2_HEIGHT);
108     pageSizeMap["ISO_B3"] =
109         std::make_shared<PrintPageSize>("ISO_B3", "iso_b3_353x500mm", ISO_B3_WIDTH, ISO_B3_HEIGHT);
110     pageSizeMap["ISO_B4"] =
111         std::make_shared<PrintPageSize>("ISO_B4", "iso_b4_250x353mm", ISO_B4_WIDTH, ISO_B4_HEIGHT);
112     pageSizeMap["ISO_B5"] =
113         std::make_shared<PrintPageSize>("ISO_B5", "iso_b5_176x250mm", ISO_B5_WIDTH, ISO_B5_HEIGHT);
114     pageSizeMap["ISO_C5"] =
115         std::make_shared<PrintPageSize>("ISO_C5", "iso_c5_162x229mm", ISO_C5_WIDTH, ISO_C5_HEIGHT);
116     pageSizeMap["ISO_DL"] =
117         std::make_shared<PrintPageSize>("ISO_DL", "iso_dl_110x220mm", ISO_DL_WIDTH, ISO_DL_HEIGHT);
118 }
BuildPageSizeMap()119 void PrintPageSize::BuildPageSizeMap()
120 {
121     UpdatePageSizeMapIso(pageSize_);
122     pageSize_["NA_LEGAL"] =
123         std::make_shared<PrintPageSize>("NA_LEGAL", "na_legal_8.5x14in", LEGAL_WIDTH, LEGAL_HEIGHT);
124     pageSize_["NA_LETTER"] =
125         std::make_shared<PrintPageSize>("NA_LETTER", "na_letter_8.5x11in", LETTER_WIDTH, LETTER_HEIGHT);
126     pageSize_["JIS_B5"] =
127         std::make_shared<PrintPageSize>("JIS_B5", "jis_b5_182x257mm", JIS_B5_WIDTH, JIS_B5_HEIGHT);
128     pageSize_["JIS_B4"] =
129         std::make_shared<PrintPageSize>("JIS_B4", "jis_b4_257x364mm", JIS_B4_WIDTH, JIS_B4_HEIGHT);
130     pageSize_["NA_INDEX_5X7"] =
131         std::make_shared<PrintPageSize>("NA_INDEX_5X7", "na_5x7_5x7in", PHOTO_5R_WIDTH, PHOTO_5R_HEIGHT);
132     pageSize_["NA_INDEX_4X6"] =
133         std::make_shared<PrintPageSize>("NA_INDEX_4X6", "na_index-4x6_4x6in", PHOTO_4R_WIDTH, PHOTO_4R_HEIGHT);
134     pageSize_["NA_GOVT_LETTER"] = std::make_shared<PrintPageSize>(
135         "NA_GOVT_LETTER", "na_govt-letter_8x10in", NA_GOVT_LETTER_8IN_WIDTH, NA_GOVT_LETTER_8IN_HEIGHT);
136     pageSize_["NA_LEDGER"] = std::make_shared<PrintPageSize>(
137         "NA_LEDGER", "na_ledger_11x17in", NA_LEDGER_11IN_WIDTH, NA_LEDGER_11IN_HEIGHT);
138     pageSize_["JPN_HAGAKI"] = std::make_shared<PrintPageSize>(
139         "JPN_HAGAKI", "jpn_hagaki_100x148mm", JPN_HAGAKI_WIDTH, JPN_HAGAKI_HEIGHT);
140     pageSize_["OM_DSC_PHOTO"] = std::make_shared<PrintPageSize>(
141         "OM_DSC_PHOTO", "om_dsc-photo_89x119mm", OM_SDC_PHOTO_WIDTH, OM_SDC_PHOTO_HEIGHT);
142     pageSize_["OM_CARD"] = std::make_shared<PrintPageSize>(
143         "OM_CARD", "om_card_54x86mm", OM_CARD_WIDTH, OM_CARD_HEIGHT);
144     pageSize_["OE_PHOTO_L"] = std::make_shared<PrintPageSize>(
145         "OE_PHOTO_L", "oe_photo-l_3.5x5in", OE_PHOTO_L_WIDTH, OE_PHOTO_L_HEIGHT);
146     pageSize_["NA_TABLOID"] = std::make_shared<PrintPageSize>(
147         "NA_TABLOID", "B_TABLOID", B_TABLOID_L_WIDTH, B_TABLOID_L_HEIGHT);
148     pageSize_["ROC_16K"] = std::make_shared<PrintPageSize>(
149         "ROC_16K", "roc_16k_7.75x10.75in", ROC_16K_WIDTH, ROC_16K_HEIGHT);
150     pageSize_["ROC_8K"] = std::make_shared<PrintPageSize>(
151         "ROC_8K", "roc_8k_10.75x15.5in", ROC_8K_WIDTH, ROC_8K_HEIGHT);
152     PRINT_HILOGD("BuildPageSizeMap count = %{public}zu", pageSize_.size());
153 }
154 
MatchPageSize(const std::string & pageString)155 PAGE_SIZE_ID PrintPageSize::MatchPageSize(const std::string& pageString)
156 {
157     std::lock_guard<std::mutex> lock(pageSizeMapMutex);
158     if (pageSize_.size() == 0) {
159         BuildPageSizeMap();
160     }
161     for (auto& item : pageSize_) {
162         auto pageSizeItem = item.second;
163         if (pageSizeItem == nullptr) {
164             continue;
165         }
166         if (strcasestr(pageSizeItem->GetName().c_str(), pageString.c_str()) != nullptr) {
167             return item.first;
168         }
169     }
170     return "";
171 }
172 
FindPageSizeById(const PAGE_SIZE_ID & id,PrintPageSize & pageSize)173 bool PrintPageSize::FindPageSizeById(const PAGE_SIZE_ID &id, PrintPageSize& pageSize)
174 {
175     std::lock_guard<std::mutex> lock(pageSizeMapMutex);
176     if (pageSize_.size() == 0) {
177         BuildPageSizeMap();
178     }
179     auto iter = pageSize_.find(id);
180     if (iter == pageSize_.end() || iter->second == nullptr) {
181         return false;
182     }
183     pageSize = *(iter->second);
184     return true;
185 }
186 
PrintPageSize()187 PrintPageSize::PrintPageSize() : id_(""), name_("PrintPageSize"), width_(0), height_(0)
188 {}
189 
PrintPageSize(PAGE_SIZE_ID id,DiscretePageName name,uint32_t width,uint32_t height)190 PrintPageSize::PrintPageSize(PAGE_SIZE_ID id, DiscretePageName name, uint32_t width, uint32_t height)
191 {
192     id_ = id;
193     name_ = name;
194     width_ = width;
195     height_ = height;
196 }
197 
PrintPageSize(const PrintPageSize & right)198 PrintPageSize::PrintPageSize(const PrintPageSize &right)
199 {
200     id_ = right.id_;
201     name_ = right.name_;
202     width_ = right.width_;
203     height_ = right.height_;
204 }
205 
operator =(const PrintPageSize & right)206 PrintPageSize &PrintPageSize::operator=(const PrintPageSize &right)
207 {
208     if (this != &right) {
209         id_ = right.id_;
210         name_ = right.name_;
211         width_ = right.width_;
212         height_ = right.height_;
213     }
214     return *this;
215 }
216 
operator ==(const PrintPageSize & right)217 bool PrintPageSize::operator==(const PrintPageSize &right)
218 {
219     return id_ == right.id_;
220 }
221 
~PrintPageSize()222 PrintPageSize::~PrintPageSize()
223 {}
224 
Reset()225 void PrintPageSize::Reset()
226 {
227     SetId("");
228     SetName("");
229     SetWidth(0);
230     SetHeight(0);
231 }
232 
SetId(const std::string & id)233 void PrintPageSize::SetId(const std::string &id)
234 {
235     id_ = id;
236 }
237 
SetName(const std::string & name)238 void PrintPageSize::SetName(const std::string &name)
239 {
240     name_ = name;
241 }
242 
SetWidth(uint32_t width)243 void PrintPageSize::SetWidth(uint32_t width)
244 {
245     width_ = width;
246 }
247 
SetHeight(uint32_t height)248 void PrintPageSize::SetHeight(uint32_t height)
249 {
250     height_ = height;
251 }
252 
GetId() const253 const std::string &PrintPageSize::GetId() const
254 {
255     return id_;
256 }
257 
GetName() const258 const std::string &PrintPageSize::GetName() const
259 {
260     return name_;
261 }
262 
GetWidth() const263 uint32_t PrintPageSize::GetWidth() const
264 {
265     return width_;
266 }
267 
GetHeight() const268 uint32_t PrintPageSize::GetHeight() const
269 {
270     return height_;
271 }
272 
ReadFromParcel(Parcel & parcel)273 void PrintPageSize::ReadFromParcel(Parcel &parcel)
274 {
275     SetId(parcel.ReadString());
276     SetName(parcel.ReadString());
277     SetWidth(parcel.ReadUint32());
278     SetHeight(parcel.ReadUint32());
279 }
280 
Marshalling(Parcel & parcel) const281 bool PrintPageSize::Marshalling(Parcel &parcel) const
282 {
283     parcel.WriteString(GetId());
284     parcel.WriteString(GetName());
285     parcel.WriteUint32(GetWidth());
286     parcel.WriteUint32(GetHeight());
287     return true;
288 }
289 
Unmarshalling(Parcel & parcel)290 std::shared_ptr<PrintPageSize> PrintPageSize::Unmarshalling(Parcel &parcel)
291 {
292     auto nativeObj = std::make_shared<PrintPageSize>();
293     nativeObj->ReadFromParcel(parcel);
294     return nativeObj;
295 }
296 
Dump()297 void PrintPageSize::Dump()
298 {
299     PRINT_HILOGD("id_ = %{public}s", id_.c_str());
300     PRINT_HILOGD("name_ = %{public}s", name_.c_str());
301     PRINT_HILOGD("width_ = %{public}d", width_);
302     PRINT_HILOGD("height_ = %{public}d", height_);
303 }
304 } // namespace OHOS::Print
305