• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 PDFium Authors. All rights reserved.
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 <algorithm>
26 #include <utility>
27 
28 #include "fxbarcode/BC_TwoDimWriter.h"
29 #include "fxbarcode/common/BC_CommonBitMatrix.h"
30 #include "fxbarcode/pdf417/BC_PDF417.h"
31 #include "fxbarcode/pdf417/BC_PDF417BarcodeMatrix.h"
32 #include "third_party/base/stl_util.h"
33 
CBC_PDF417Writer()34 CBC_PDF417Writer::CBC_PDF417Writer() : CBC_TwoDimWriter(false) {}
35 
36 CBC_PDF417Writer::~CBC_PDF417Writer() = default;
37 
SetErrorCorrectionLevel(int32_t level)38 bool CBC_PDF417Writer::SetErrorCorrectionLevel(int32_t level) {
39   if (level < 0 || level > 8) {
40     return false;
41   }
42   set_error_correction_level(level);
43   return true;
44 }
45 
Encode(WideStringView contents,int32_t * pOutWidth,int32_t * pOutHeight)46 std::vector<uint8_t> CBC_PDF417Writer::Encode(WideStringView contents,
47                                               int32_t* pOutWidth,
48                                               int32_t* pOutHeight) {
49   std::vector<uint8_t> results;
50   CBC_PDF417 encoder;
51   int32_t col = (m_Width / m_ModuleWidth - 69) / 17;
52   int32_t row = m_Height / (m_ModuleWidth * 20);
53   if (row >= 3 && row <= 90 && col >= 1 && col <= 30)
54     encoder.setDimensions(col, 1, row, 3);
55   else if (col >= 1 && col <= 30)
56     encoder.setDimensions(col, col, 90, 3);
57   else if (row >= 3 && row <= 90)
58     encoder.setDimensions(30, 1, row, row);
59   if (!encoder.GenerateBarcodeLogic(contents, error_correction_level()))
60     return results;
61 
62   CBC_BarcodeMatrix* barcodeMatrix = encoder.getBarcodeMatrix();
63   std::vector<uint8_t> matrixData = barcodeMatrix->toBitArray();
64   int32_t matrixWidth = barcodeMatrix->getWidth();
65   int32_t matrixHeight = barcodeMatrix->getHeight();
66 
67   if (matrixWidth < matrixHeight) {
68     RotateArray(&matrixData, matrixHeight, matrixWidth);
69     std::swap(matrixWidth, matrixHeight);
70   }
71   *pOutWidth = matrixWidth;
72   *pOutHeight = matrixHeight;
73   results = pdfium::Vector2D<uint8_t>(*pOutWidth, *pOutHeight);
74   memcpy(results.data(), matrixData.data(), *pOutWidth * *pOutHeight);
75   return results;
76 }
77 
RotateArray(std::vector<uint8_t> * bitarray,int32_t height,int32_t width)78 void CBC_PDF417Writer::RotateArray(std::vector<uint8_t>* bitarray,
79                                    int32_t height,
80                                    int32_t width) {
81   std::vector<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