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/src/fxbarcode/barcode.h"
24 #include "xfa/src/fxbarcode/BC_TwoDimWriter.h"
25 #include "xfa/src/fxbarcode/common/BC_CommonBitArray.h"
26 #include "xfa/src/fxbarcode/common/BC_CommonBitMatrix.h"
27 #include "BC_PDF417Compaction.h"
28 #include "BC_PDF417.h"
29 #include "BC_PDF417BarcodeMatrix.h"
30 #include "BC_PDF417Writer.h"
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 FX_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(FX_BOOL truncated)44 void CBC_PDF417Writer::SetTruncated(FX_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 BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
63 int32_t lineThickness = 2;
64 int32_t aspectRatio = 4;
65 CBC_BarcodeMatrix* barcodeMatrix = encoder.getBarcodeMatrix();
66 CFX_ByteArray originalScale;
67 originalScale.Copy(barcodeMatrix->getScaledMatrix(
68 lineThickness, aspectRatio * lineThickness));
69 int32_t width = outWidth;
70 int32_t height = outHeight;
71 outWidth = barcodeMatrix->getWidth();
72 outHeight = barcodeMatrix->getHeight();
73 FX_BOOL rotated = FALSE;
74 if ((height > width) ^ (outWidth < outHeight)) {
75 rotateArray(originalScale, outHeight, outWidth);
76 rotated = TRUE;
77 int32_t temp = outHeight;
78 outHeight = outWidth;
79 outWidth = temp;
80 }
81 int32_t scaleX = width / outWidth;
82 int32_t scaleY = height / outHeight;
83 int32_t scale;
84 if (scaleX < scaleY) {
85 scale = scaleX;
86 } else {
87 scale = scaleY;
88 }
89 if (scale > 1) {
90 originalScale.RemoveAll();
91 originalScale.Copy(barcodeMatrix->getScaledMatrix(
92 scale * lineThickness, scale * aspectRatio * lineThickness));
93 if (rotated) {
94 rotateArray(originalScale, outHeight, outWidth);
95 int32_t temp = outHeight;
96 outHeight = outWidth;
97 outWidth = temp;
98 }
99 }
100 uint8_t* result = FX_Alloc2D(uint8_t, outHeight, outWidth);
101 FXSYS_memcpy(result, originalScale.GetData(), outHeight * outWidth);
102 return result;
103 }
rotateArray(CFX_ByteArray & bitarray,int32_t height,int32_t width)104 void CBC_PDF417Writer::rotateArray(CFX_ByteArray& bitarray,
105 int32_t height,
106 int32_t width) {
107 CFX_ByteArray temp;
108 temp.Copy(bitarray);
109 for (int32_t ii = 0; ii < height; ii++) {
110 int32_t inverseii = height - ii - 1;
111 for (int32_t jj = 0; jj < width; jj++) {
112 bitarray[jj * height + inverseii] = temp[ii * width + jj];
113 }
114 }
115 }
116