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/pdf417/BC_PDF417.h"
34 #include "fxbarcode/pdf417/BC_PDF417BarcodeMatrix.h"
35
36 namespace {
37
RotateArray(DataVector<uint8_t> & bitarray,int32_t width,int32_t height)38 void RotateArray(DataVector<uint8_t>& bitarray, int32_t width, int32_t height) {
39 DataVector<uint8_t> temp = bitarray;
40 for (int32_t i = 0; i < height; i++) {
41 int32_t inverse_i = height - i - 1;
42 for (int32_t j = 0; j < width; j++) {
43 bitarray[j * height + inverse_i] = temp[i * width + j];
44 }
45 }
46 }
47
48 } // namespace
49
CBC_PDF417Writer()50 CBC_PDF417Writer::CBC_PDF417Writer() : CBC_TwoDimWriter(false) {}
51
52 CBC_PDF417Writer::~CBC_PDF417Writer() = default;
53
SetErrorCorrectionLevel(int32_t level)54 bool CBC_PDF417Writer::SetErrorCorrectionLevel(int32_t level) {
55 if (level < 0 || level > 8) {
56 return false;
57 }
58 set_error_correction_level(level);
59 return true;
60 }
61
Encode(WideStringView contents) const62 CBC_PDF417Writer::EncodeResult CBC_PDF417Writer::Encode(
63 WideStringView contents) const {
64 CBC_PDF417 encoder;
65 int32_t col = (m_Width / m_ModuleWidth - 69) / 17;
66 int32_t row = m_Height / (m_ModuleWidth * 20);
67 if (row >= 3 && row <= 90 && col >= 1 && col <= 30)
68 encoder.setDimensions(col, 1, row, 3);
69 else if (col >= 1 && col <= 30)
70 encoder.setDimensions(col, col, 90, 3);
71 else if (row >= 3 && row <= 90)
72 encoder.setDimensions(30, 1, row, row);
73 if (!encoder.GenerateBarcodeLogic(contents, error_correction_level())) {
74 return {DataVector<uint8_t>(), 0, 0};
75 }
76
77 CBC_BarcodeMatrix* barcodeMatrix = encoder.getBarcodeMatrix();
78 DataVector<uint8_t> matrix_data = barcodeMatrix->toBitArray();
79 int32_t matrix_width = barcodeMatrix->getWidth();
80 int32_t matrix_height = barcodeMatrix->getHeight();
81
82 if (matrix_width < matrix_height) {
83 RotateArray(matrix_data, matrix_width, matrix_height);
84 std::swap(matrix_width, matrix_height);
85 }
86 return {std::move(matrix_data), matrix_width, matrix_height};
87 }
88
EncodeResult(DataVector<uint8_t> data,int32_t width,int32_t height)89 CBC_PDF417Writer::EncodeResult::EncodeResult(DataVector<uint8_t> data,
90 int32_t width,
91 int32_t height)
92 : data(std::move(data)), width(width), height(height) {}
93
94 CBC_PDF417Writer::EncodeResult::~EncodeResult() = default;
95