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