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 "xfa/fxbarcode/BC_TwoDimWriter.h"
24 #include "xfa/fxbarcode/common/BC_CommonBitArray.h"
25 #include "xfa/fxbarcode/common/BC_CommonBitMatrix.h"
26 #include "xfa/fxbarcode/pdf417/BC_PDF417.h"
27 #include "xfa/fxbarcode/pdf417/BC_PDF417BarcodeMatrix.h"
28 #include "xfa/fxbarcode/pdf417/BC_PDF417Compaction.h"
29 #include "xfa/fxbarcode/pdf417/BC_PDF417Writer.h"
30
CBC_PDF417Writer()31 CBC_PDF417Writer::CBC_PDF417Writer() {
32 m_bFixedSize = false;
33 }
~CBC_PDF417Writer()34 CBC_PDF417Writer::~CBC_PDF417Writer() {
35 m_bTruncated = true;
36 }
SetErrorCorrectionLevel(int32_t level)37 bool CBC_PDF417Writer::SetErrorCorrectionLevel(int32_t level) {
38 if (level < 0 || level > 8) {
39 return false;
40 }
41 m_iCorrectLevel = level;
42 return true;
43 }
SetTruncated(bool truncated)44 void CBC_PDF417Writer::SetTruncated(bool truncated) {
45 m_bTruncated = truncated;
46 }
Encode(const CFX_WideString & contents,int32_t & outWidth,int32_t & outHeight,int32_t & e)47 uint8_t* CBC_PDF417Writer::Encode(const CFX_WideString& contents,
48 int32_t& outWidth,
49 int32_t& outHeight,
50 int32_t& e) {
51 CBC_PDF417 encoder;
52 int32_t col = (m_Width / m_ModuleWidth - 69) / 17;
53 int32_t row = m_Height / (m_ModuleWidth * 20);
54 if (row >= 3 && row <= 90 && col >= 1 && col <= 30) {
55 encoder.setDimensions(col, col, row, row);
56 } else if (col >= 1 && col <= 30) {
57 encoder.setDimensions(col, col, 90, 3);
58 } else if (row >= 3 && row <= 90) {
59 encoder.setDimensions(30, 1, row, row);
60 }
61 encoder.generateBarcodeLogic(contents, m_iCorrectLevel, e);
62 if (e != BCExceptionNO)
63 return nullptr;
64 int32_t lineThickness = 2;
65 int32_t aspectRatio = 4;
66 CBC_BarcodeMatrix* barcodeMatrix = encoder.getBarcodeMatrix();
67 CFX_ArrayTemplate<uint8_t> originalScale;
68 originalScale.Copy(barcodeMatrix->getScaledMatrix(
69 lineThickness, aspectRatio * lineThickness));
70 int32_t width = outWidth;
71 int32_t height = outHeight;
72 outWidth = barcodeMatrix->getWidth();
73 outHeight = barcodeMatrix->getHeight();
74 bool rotated = false;
75 if ((height > width) ^ (outWidth < outHeight)) {
76 rotateArray(originalScale, outHeight, outWidth);
77 rotated = true;
78 int32_t temp = outHeight;
79 outHeight = outWidth;
80 outWidth = temp;
81 }
82 int32_t scaleX = width / outWidth;
83 int32_t scaleY = height / outHeight;
84 int32_t scale;
85 if (scaleX < scaleY) {
86 scale = scaleX;
87 } else {
88 scale = scaleY;
89 }
90 if (scale > 1) {
91 originalScale.RemoveAll();
92 originalScale.Copy(barcodeMatrix->getScaledMatrix(
93 scale * lineThickness, scale * aspectRatio * lineThickness));
94 if (rotated) {
95 rotateArray(originalScale, outHeight, outWidth);
96 int32_t temp = outHeight;
97 outHeight = outWidth;
98 outWidth = temp;
99 }
100 }
101 uint8_t* result = FX_Alloc2D(uint8_t, outHeight, outWidth);
102 FXSYS_memcpy(result, originalScale.GetData(), outHeight * outWidth);
103 return result;
104 }
rotateArray(CFX_ArrayTemplate<uint8_t> & bitarray,int32_t height,int32_t width)105 void CBC_PDF417Writer::rotateArray(CFX_ArrayTemplate<uint8_t>& bitarray,
106 int32_t height,
107 int32_t width) {
108 CFX_ArrayTemplate<uint8_t> temp;
109 temp.Copy(bitarray);
110 for (int32_t ii = 0; ii < height; ii++) {
111 int32_t inverseii = height - ii - 1;
112 for (int32_t jj = 0; jj < width; jj++) {
113 bitarray[jj * height + inverseii] = temp[ii * width + jj];
114 }
115 }
116 }
117