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