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 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 "fxbarcode/datamatrix/BC_DataMatrixWriter.h"
24
25 #include <stdint.h>
26
27 #include <memory>
28
29 #include "core/fxcrt/data_vector.h"
30 #include "fxbarcode/BC_TwoDimWriter.h"
31 #include "fxbarcode/BC_Writer.h"
32 #include "fxbarcode/common/BC_CommonBitMatrix.h"
33 #include "fxbarcode/common/BC_CommonByteMatrix.h"
34 #include "fxbarcode/datamatrix/BC_ASCIIEncoder.h"
35 #include "fxbarcode/datamatrix/BC_Base256Encoder.h"
36 #include "fxbarcode/datamatrix/BC_C40Encoder.h"
37 #include "fxbarcode/datamatrix/BC_DataMatrixSymbolInfo144.h"
38 #include "fxbarcode/datamatrix/BC_DefaultPlacement.h"
39 #include "fxbarcode/datamatrix/BC_EdifactEncoder.h"
40 #include "fxbarcode/datamatrix/BC_Encoder.h"
41 #include "fxbarcode/datamatrix/BC_EncoderContext.h"
42 #include "fxbarcode/datamatrix/BC_ErrorCorrection.h"
43 #include "fxbarcode/datamatrix/BC_HighLevelEncoder.h"
44 #include "fxbarcode/datamatrix/BC_SymbolInfo.h"
45 #include "fxbarcode/datamatrix/BC_TextEncoder.h"
46 #include "fxbarcode/datamatrix/BC_X12Encoder.h"
47 #include "third_party/base/check.h"
48
49 namespace {
50
EncodeLowLevel(CBC_DefaultPlacement * placement,const CBC_SymbolInfo * symbolInfo)51 std::unique_ptr<CBC_CommonByteMatrix> EncodeLowLevel(
52 CBC_DefaultPlacement* placement,
53 const CBC_SymbolInfo* symbolInfo) {
54 int32_t symbolWidth = symbolInfo->GetSymbolDataWidth();
55 DCHECK(symbolWidth);
56 int32_t symbolHeight = symbolInfo->GetSymbolDataHeight();
57 DCHECK(symbolHeight);
58 int32_t width = symbolInfo->GetSymbolWidth();
59 DCHECK(width);
60 int32_t height = symbolInfo->GetSymbolHeight();
61 DCHECK(height);
62
63 auto matrix = std::make_unique<CBC_CommonByteMatrix>(width, height);
64 int32_t matrixY = 0;
65 for (int32_t y = 0; y < symbolHeight; y++) {
66 int32_t matrixX;
67 if ((y % symbolInfo->matrix_height()) == 0) {
68 matrixX = 0;
69 for (int32_t x = 0; x < width; x++) {
70 matrix->Set(matrixX, matrixY, x % 2 == 0);
71 matrixX++;
72 }
73 matrixY++;
74 }
75 matrixX = 0;
76 for (int32_t x = 0; x < symbolWidth; x++) {
77 if (x % symbolInfo->matrix_width() == 0) {
78 matrix->Set(matrixX, matrixY, true);
79 matrixX++;
80 }
81 matrix->Set(matrixX, matrixY, placement->GetBit(x, y));
82 matrixX++;
83 if (x % symbolInfo->matrix_width() == symbolInfo->matrix_width() - 1) {
84 matrix->Set(matrixX, matrixY, y % 2 == 0);
85 matrixX++;
86 }
87 }
88 matrixY++;
89 if (y % symbolInfo->matrix_height() == symbolInfo->matrix_height() - 1) {
90 matrixX = 0;
91 for (int32_t x = 0; x < width; x++) {
92 matrix->Set(matrixX, matrixY, true);
93 matrixX++;
94 }
95 matrixY++;
96 }
97 }
98 return matrix;
99 }
100
101 } // namespace
102
CBC_DataMatrixWriter()103 CBC_DataMatrixWriter::CBC_DataMatrixWriter() : CBC_TwoDimWriter(true) {}
104
105 CBC_DataMatrixWriter::~CBC_DataMatrixWriter() = default;
106
SetErrorCorrectionLevel(int32_t level)107 bool CBC_DataMatrixWriter::SetErrorCorrectionLevel(int32_t level) {
108 set_error_correction_level(level);
109 return true;
110 }
111
Encode(const WideString & contents,int32_t * pOutWidth,int32_t * pOutHeight)112 DataVector<uint8_t> CBC_DataMatrixWriter::Encode(const WideString& contents,
113 int32_t* pOutWidth,
114 int32_t* pOutHeight) {
115 WideString encoded = CBC_HighLevelEncoder::EncodeHighLevel(contents);
116 if (encoded.IsEmpty())
117 return DataVector<uint8_t>();
118
119 const CBC_SymbolInfo* pSymbolInfo =
120 CBC_SymbolInfo::Lookup(encoded.GetLength(), false);
121 if (!pSymbolInfo)
122 return DataVector<uint8_t>();
123
124 WideString codewords =
125 CBC_ErrorCorrection::EncodeECC200(encoded, pSymbolInfo);
126 if (codewords.IsEmpty())
127 return DataVector<uint8_t>();
128
129 int32_t width = pSymbolInfo->GetSymbolDataWidth();
130 DCHECK(width);
131 int32_t height = pSymbolInfo->GetSymbolDataHeight();
132 DCHECK(height);
133
134 auto placement =
135 std::make_unique<CBC_DefaultPlacement>(codewords, width, height);
136 auto bytematrix = EncodeLowLevel(placement.get(), pSymbolInfo);
137 DCHECK(bytematrix);
138
139 *pOutWidth = bytematrix->GetWidth();
140 *pOutHeight = bytematrix->GetHeight();
141 return bytematrix->TakeArray();
142 }
143