xref: /aosp_15_r20/external/pdfium/fxbarcode/pdf417/BC_PDF417Writer.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2014 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 // Original code is licensed as follows:
7 /*
8  * Copyright 2012 ZXing authors
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22 
23 #include "fxbarcode/pdf417/BC_PDF417Writer.h"
24 
25 #include <stdint.h>
26 
27 #include <algorithm>
28 #include <utility>
29 
30 #include "core/fxcrt/data_vector.h"
31 #include "core/fxcrt/stl_util.h"
32 #include "fxbarcode/BC_TwoDimWriter.h"
33 #include "fxbarcode/common/BC_CommonBitMatrix.h"
34 #include "fxbarcode/pdf417/BC_PDF417.h"
35 #include "fxbarcode/pdf417/BC_PDF417BarcodeMatrix.h"
36 
CBC_PDF417Writer()37 CBC_PDF417Writer::CBC_PDF417Writer() : CBC_TwoDimWriter(false) {}
38 
39 CBC_PDF417Writer::~CBC_PDF417Writer() = default;
40 
SetErrorCorrectionLevel(int32_t level)41 bool CBC_PDF417Writer::SetErrorCorrectionLevel(int32_t level) {
42   if (level < 0 || level > 8) {
43     return false;
44   }
45   set_error_correction_level(level);
46   return true;
47 }
48 
Encode(WideStringView contents,int32_t * pOutWidth,int32_t * pOutHeight)49 DataVector<uint8_t> CBC_PDF417Writer::Encode(WideStringView contents,
50                                              int32_t* pOutWidth,
51                                              int32_t* pOutHeight) {
52   CBC_PDF417 encoder;
53   int32_t col = (m_Width / m_ModuleWidth - 69) / 17;
54   int32_t row = m_Height / (m_ModuleWidth * 20);
55   if (row >= 3 && row <= 90 && col >= 1 && col <= 30)
56     encoder.setDimensions(col, 1, row, 3);
57   else if (col >= 1 && col <= 30)
58     encoder.setDimensions(col, col, 90, 3);
59   else if (row >= 3 && row <= 90)
60     encoder.setDimensions(30, 1, row, row);
61   if (!encoder.GenerateBarcodeLogic(contents, error_correction_level()))
62     return DataVector<uint8_t>();
63 
64   CBC_BarcodeMatrix* barcodeMatrix = encoder.getBarcodeMatrix();
65   DataVector<uint8_t> matrixData = barcodeMatrix->toBitArray();
66   int32_t matrixWidth = barcodeMatrix->getWidth();
67   int32_t matrixHeight = barcodeMatrix->getHeight();
68 
69   if (matrixWidth < matrixHeight) {
70     RotateArray(&matrixData, matrixHeight, matrixWidth);
71     std::swap(matrixWidth, matrixHeight);
72   }
73   *pOutWidth = matrixWidth;
74   *pOutHeight = matrixHeight;
75   return matrixData;
76 }
77 
RotateArray(DataVector<uint8_t> * bitarray,int32_t height,int32_t width)78 void CBC_PDF417Writer::RotateArray(DataVector<uint8_t>* bitarray,
79                                    int32_t height,
80                                    int32_t width) {
81   DataVector<uint8_t> temp = *bitarray;
82   for (int32_t ii = 0; ii < height; ii++) {
83     int32_t inverseii = height - ii - 1;
84     for (int32_t jj = 0; jj < width; jj++) {
85       (*bitarray)[jj * height + inverseii] = temp[ii * width + jj];
86     }
87   }
88 }
89