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 2008 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_Writer.h"
25 #include "xfa/src/fxbarcode/BC_TwoDimWriter.h"
26 #include "xfa/src/fxbarcode/BC_Dimension.h"
27 #include "xfa/src/fxbarcode/BC_BinaryBitmap.h"
28 #include "xfa/src/fxbarcode/BC_UtilCodingConvert.h"
29 #include "xfa/src/fxbarcode/common/BC_CommonBitMatrix.h"
30 #include "xfa/src/fxbarcode/common/BC_CommonByteMatrix.h"
31 #include "BC_Encoder.h"
32 #include "BC_DefaultPlacement.h"
33 #include "BC_SymbolShapeHint.h"
34 #include "BC_SymbolInfo.h"
35 #include "BC_DataMatrixSymbolInfo144.h"
36 #include "BC_ErrorCorrection.h"
37 #include "BC_EncoderContext.h"
38 #include "BC_C40Encoder.h"
39 #include "BC_TextEncoder.h"
40 #include "BC_X12Encoder.h"
41 #include "BC_EdifactEncoder.h"
42 #include "BC_Base256Encoder.h"
43 #include "BC_ASCIIEncoder.h"
44 #include "BC_HighLevelEncoder.h"
45 #include "BC_DataMatrixWriter.h"
CBC_DataMatrixWriter()46 CBC_DataMatrixWriter::CBC_DataMatrixWriter() {}
~CBC_DataMatrixWriter()47 CBC_DataMatrixWriter::~CBC_DataMatrixWriter() {}
SetErrorCorrectionLevel(int32_t level)48 FX_BOOL CBC_DataMatrixWriter::SetErrorCorrectionLevel(int32_t level) {
49   m_iCorrectLevel = level;
50   return TRUE;
51 }
Encode(const CFX_WideString & contents,int32_t & outWidth,int32_t & outHeight,int32_t & e)52 uint8_t* CBC_DataMatrixWriter::Encode(const CFX_WideString& contents,
53                                       int32_t& outWidth,
54                                       int32_t& outHeight,
55                                       int32_t& e) {
56   if (outWidth < 0 || outHeight < 0) {
57     e = BCExceptionHeightAndWidthMustBeAtLeast1;
58     BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
59   }
60   CBC_SymbolShapeHint::SymbolShapeHint shape =
61       CBC_SymbolShapeHint::FORCE_SQUARE;
62   CBC_Dimension* minSize = NULL;
63   CBC_Dimension* maxSize = NULL;
64   CFX_WideString ecLevel;
65   CFX_WideString encoded = CBC_HighLevelEncoder::encodeHighLevel(
66       contents, ecLevel, shape, minSize, maxSize, e);
67   BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
68   CBC_SymbolInfo* symbolInfo = CBC_SymbolInfo::lookup(
69       encoded.GetLength(), shape, minSize, maxSize, TRUE, e);
70   BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
71   CFX_WideString codewords =
72       CBC_ErrorCorrection::encodeECC200(encoded, symbolInfo, e);
73   BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
74   CBC_DefaultPlacement* placement =
75       new CBC_DefaultPlacement(codewords, symbolInfo->getSymbolDataWidth(e),
76                                symbolInfo->getSymbolDataHeight(e));
77   BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
78   placement->place();
79   CBC_CommonByteMatrix* bytematrix = encodeLowLevel(placement, symbolInfo, e);
80   BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
81   outWidth = bytematrix->GetWidth();
82   outHeight = bytematrix->GetHeight();
83   uint8_t* result = FX_Alloc2D(uint8_t, outWidth, outHeight);
84   FXSYS_memcpy(result, bytematrix->GetArray(), outWidth * outHeight);
85   delete bytematrix;
86   delete placement;
87   return result;
88 }
encodeLowLevel(CBC_DefaultPlacement * placement,CBC_SymbolInfo * symbolInfo,int32_t & e)89 CBC_CommonByteMatrix* CBC_DataMatrixWriter::encodeLowLevel(
90     CBC_DefaultPlacement* placement,
91     CBC_SymbolInfo* symbolInfo,
92     int32_t& e) {
93   int32_t symbolWidth = symbolInfo->getSymbolDataWidth(e);
94   BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
95   int32_t symbolHeight = symbolInfo->getSymbolDataHeight(e);
96   BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
97   CBC_CommonByteMatrix* matrix = new CBC_CommonByteMatrix(
98       symbolInfo->getSymbolWidth(e), symbolInfo->getSymbolHeight(e));
99   BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
100   matrix->Init();
101   int32_t matrixY = 0;
102   for (int32_t y = 0; y < symbolHeight; y++) {
103     int32_t matrixX;
104     if ((y % symbolInfo->m_matrixHeight) == 0) {
105       matrixX = 0;
106       for (int32_t x = 0; x < symbolInfo->getSymbolWidth(e); x++) {
107         matrix->Set(matrixX, matrixY, (x % 2) == 0);
108         matrixX++;
109       }
110       matrixY++;
111     }
112     matrixX = 0;
113     for (int32_t x = 0; x < symbolWidth; x++) {
114       if ((x % symbolInfo->m_matrixWidth) == 0) {
115         matrix->Set(matrixX, matrixY, TRUE);
116         matrixX++;
117       }
118       matrix->Set(matrixX, matrixY, placement->getBit(x, y));
119       matrixX++;
120       if ((x % symbolInfo->m_matrixWidth) == symbolInfo->m_matrixWidth - 1) {
121         matrix->Set(matrixX, matrixY, (y % 2) == 0);
122         matrixX++;
123       }
124     }
125     matrixY++;
126     if ((y % symbolInfo->m_matrixHeight) == symbolInfo->m_matrixHeight - 1) {
127       matrixX = 0;
128       for (int32_t x = 0; x < symbolInfo->getSymbolWidth(e); x++) {
129         matrix->Set(matrixX, matrixY, TRUE);
130         matrixX++;
131       }
132       matrixY++;
133     }
134   }
135   return matrix;
136 }
137