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/qrcode/BC_QRCodeWriter.h"
24
25 #include "fxbarcode/common/BC_CommonByteMatrix.h"
26 #include "fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h"
27 #include "fxbarcode/qrcode/BC_QRCoder.h"
28 #include "fxbarcode/qrcode/BC_QRCoderEncoder.h"
29 #include "fxbarcode/qrcode/BC_QRCoderErrorCorrectionLevel.h"
30 #include "fxbarcode/qrcode/BC_QRCoderMode.h"
31 #include "fxbarcode/qrcode/BC_QRCoderVersion.h"
32 #include "third_party/base/stl_util.h"
33
CBC_QRCodeWriter()34 CBC_QRCodeWriter::CBC_QRCodeWriter() : CBC_TwoDimWriter(true) {}
35
36 CBC_QRCodeWriter::~CBC_QRCodeWriter() = default;
37
SetErrorCorrectionLevel(int32_t level)38 bool CBC_QRCodeWriter::SetErrorCorrectionLevel(int32_t level) {
39 if (level < 0 || level > 3) {
40 return false;
41 }
42 set_error_correction_level(level);
43 return true;
44 }
45
Encode(WideStringView contents,int32_t ecLevel,int32_t * pOutWidth,int32_t * pOutHeight)46 std::vector<uint8_t> CBC_QRCodeWriter::Encode(WideStringView contents,
47 int32_t ecLevel,
48 int32_t* pOutWidth,
49 int32_t* pOutHeight) {
50 std::vector<uint8_t> results;
51 CBC_QRCoderErrorCorrectionLevel* ec = nullptr;
52 switch (ecLevel) {
53 case 0:
54 ec = CBC_QRCoderErrorCorrectionLevel::L;
55 break;
56 case 1:
57 ec = CBC_QRCoderErrorCorrectionLevel::M;
58 break;
59 case 2:
60 ec = CBC_QRCoderErrorCorrectionLevel::Q;
61 break;
62 case 3:
63 ec = CBC_QRCoderErrorCorrectionLevel::H;
64 break;
65 default:
66 return results;
67 }
68 CBC_QRCoder qr;
69 if (!CBC_QRCoderEncoder::Encode(contents, ec, &qr))
70 return results;
71
72 *pOutWidth = qr.GetMatrixWidth();
73 *pOutHeight = qr.GetMatrixWidth();
74 results = pdfium::Vector2D<uint8_t>(*pOutWidth, *pOutHeight);
75 memcpy(results.data(), qr.GetMatrix()->GetArray().data(),
76 *pOutWidth * *pOutHeight);
77 return results;
78 }
79