blob: 8796ffbecc02a20d1c2fa80de463461963106983 [file] [log] [blame]
K. Moon832a6942022-10-31 20:11:31 +00001// Copyright 2014 The PDFium Authors
Dan Sinclair1770c022016-03-14 14:14:16 -04002// 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
Lei Zhang834863a2018-12-04 22:53:48 +000023#include "fxbarcode/qrcode/BC_QRCodeWriter.h"
24
Lei Zhangb53f0602022-08-11 19:50:39 +000025#include <stdint.h>
26
Lei Zhangdd063d02022-09-12 20:30:17 +000027#include <memory>
28
Lei Zhangb53f0602022-08-11 19:50:39 +000029#include "core/fxcrt/data_vector.h"
Dan Sinclaire7786682017-03-29 15:18:41 -040030#include "fxbarcode/common/BC_CommonByteMatrix.h"
31#include "fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h"
Dan Sinclaire7786682017-03-29 15:18:41 -040032#include "fxbarcode/qrcode/BC_QRCoder.h"
33#include "fxbarcode/qrcode/BC_QRCoderEncoder.h"
34#include "fxbarcode/qrcode/BC_QRCoderErrorCorrectionLevel.h"
35#include "fxbarcode/qrcode/BC_QRCoderMode.h"
36#include "fxbarcode/qrcode/BC_QRCoderVersion.h"
Dan Sinclair1770c022016-03-14 14:14:16 -040037
Lei Zhang04bd91f2018-12-04 23:52:04 +000038CBC_QRCodeWriter::CBC_QRCodeWriter() : CBC_TwoDimWriter(true) {}
weilie76203d2016-08-09 13:45:03 -070039
Lei Zhang504deda2018-12-04 20:41:05 +000040CBC_QRCodeWriter::~CBC_QRCodeWriter() = default;
weilie76203d2016-08-09 13:45:03 -070041
tsepezd19e9122016-11-02 15:43:18 -070042bool CBC_QRCodeWriter::SetErrorCorrectionLevel(int32_t level) {
Dan Sinclair1770c022016-03-14 14:14:16 -040043 if (level < 0 || level > 3) {
tsepezd19e9122016-11-02 15:43:18 -070044 return false;
Dan Sinclair1770c022016-03-14 14:14:16 -040045 }
Lei Zhang04bd91f2018-12-04 23:52:04 +000046 set_error_correction_level(level);
tsepezd19e9122016-11-02 15:43:18 -070047 return true;
Dan Sinclair1770c022016-03-14 14:14:16 -040048}
weilie76203d2016-08-09 13:45:03 -070049
Lei Zhangb53f0602022-08-11 19:50:39 +000050DataVector<uint8_t> CBC_QRCodeWriter::Encode(WideStringView contents,
51 int32_t ecLevel,
52 int32_t* pOutWidth,
53 int32_t* pOutHeight) {
thestig6dc1d772016-06-09 18:39:33 -070054 CBC_QRCoderErrorCorrectionLevel* ec = nullptr;
Dan Sinclair1770c022016-03-14 14:14:16 -040055 switch (ecLevel) {
56 case 0:
57 ec = CBC_QRCoderErrorCorrectionLevel::L;
58 break;
59 case 1:
60 ec = CBC_QRCoderErrorCorrectionLevel::M;
61 break;
62 case 2:
63 ec = CBC_QRCoderErrorCorrectionLevel::Q;
64 break;
65 case 3:
66 ec = CBC_QRCoderErrorCorrectionLevel::H;
67 break;
Lei Zhang1badb852017-04-20 15:58:56 -070068 default:
Lei Zhangdd063d02022-09-12 20:30:17 +000069 return DataVector<uint8_t>();
Dan Sinclair1770c022016-03-14 14:14:16 -040070 }
71 CBC_QRCoder qr;
Lei Zhang1badb852017-04-20 15:58:56 -070072 if (!CBC_QRCoderEncoder::Encode(contents, ec, &qr))
Lei Zhangdd063d02022-09-12 20:30:17 +000073 return DataVector<uint8_t>();
Lei Zhang03d58932017-04-03 16:40:51 -070074
Lei Zhang8d0d9822018-12-05 00:47:22 +000075 *pOutWidth = qr.GetMatrixWidth();
76 *pOutHeight = qr.GetMatrixWidth();
Lei Zhangdd063d02022-09-12 20:30:17 +000077 std::unique_ptr<CBC_CommonByteMatrix> matrix = qr.TakeMatrix();
78 return matrix->TakeArray();
Dan Sinclair1770c022016-03-14 14:14:16 -040079}