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/src/fxbarcode/barcode.h"
24 #include "xfa/src/fxbarcode/BC_TwoDimWriter.h"
25 #include "xfa/src/fxbarcode/BC_Reader.h"
26 #include "xfa/src/fxbarcode/common/BC_CommonByteMatrix.h"
27 #include "BC_QRCodeWriter.h"
28 #include "BC_QRCoderEncoder.h"
29 #include "BC_QRCoder.h"
30 #include "BC_QRCodeReader.h"
31 #include "BC_QRCoderErrorCorrectionLevel.h"
CBC_QRCodeWriter()32 CBC_QRCodeWriter::CBC_QRCodeWriter() {
33   m_bFixedSize = TRUE;
34   m_iCorrectLevel = 1;
35   m_iVersion = 0;
36 }
~CBC_QRCodeWriter()37 CBC_QRCodeWriter::~CBC_QRCodeWriter() {}
ReleaseAll()38 void CBC_QRCodeWriter::ReleaseAll() {
39   CBC_QRCodeReader::ReleaseAll();
40 }
SetVersion(int32_t version)41 FX_BOOL CBC_QRCodeWriter::SetVersion(int32_t version) {
42   if (version < 0 || version > 40) {
43     return FALSE;
44   }
45   m_iVersion = version;
46   return TRUE;
47 }
SetErrorCorrectionLevel(int32_t level)48 FX_BOOL CBC_QRCodeWriter::SetErrorCorrectionLevel(int32_t level) {
49   if (level < 0 || level > 3) {
50     return FALSE;
51   }
52   m_iCorrectLevel = level;
53   return TRUE;
54 }
Encode(const CFX_WideString & contents,int32_t ecLevel,int32_t & outWidth,int32_t & outHeight,int32_t & e)55 uint8_t* CBC_QRCodeWriter::Encode(const CFX_WideString& contents,
56                                   int32_t ecLevel,
57                                   int32_t& outWidth,
58                                   int32_t& outHeight,
59                                   int32_t& e) {
60   CBC_QRCoderErrorCorrectionLevel* ec = NULL;
61   switch (ecLevel) {
62     case 0:
63       ec = CBC_QRCoderErrorCorrectionLevel::L;
64       break;
65     case 1:
66       ec = CBC_QRCoderErrorCorrectionLevel::M;
67       break;
68     case 2:
69       ec = CBC_QRCoderErrorCorrectionLevel::Q;
70       break;
71     case 3:
72       ec = CBC_QRCoderErrorCorrectionLevel::H;
73       break;
74     default: {
75       e = BCExceptionUnSupportEclevel;
76       BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
77     }
78   }
79   CBC_QRCoder qr;
80   if (m_iVersion > 0 && m_iVersion < 41) {
81     CFX_ByteString byteStr = contents.UTF8Encode();
82     CBC_QRCoderEncoder::Encode(byteStr, ec, &qr, e, m_iVersion);
83   } else {
84     CBC_QRCoderEncoder::Encode(contents, ec, &qr, e);
85   }
86   BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
87   outWidth = qr.GetMatrixWidth();
88   outHeight = qr.GetMatrixWidth();
89   uint8_t* result = FX_Alloc2D(uint8_t, outWidth, outHeight);
90   FXSYS_memcpy(result, qr.GetMatrix()->GetArray(), outWidth * outHeight);
91   return result;
92 }
Encode(const CFX_ByteString & contents,BCFORMAT format,int32_t & outWidth,int32_t & outHeight,int32_t hints,int32_t & e)93 uint8_t* CBC_QRCodeWriter::Encode(const CFX_ByteString& contents,
94                                   BCFORMAT format,
95                                   int32_t& outWidth,
96                                   int32_t& outHeight,
97                                   int32_t hints,
98                                   int32_t& e) {
99   return NULL;
100 }
Encode(const CFX_ByteString & contents,BCFORMAT format,int32_t & outWidth,int32_t & outHeight,int32_t & e)101 uint8_t* CBC_QRCodeWriter::Encode(const CFX_ByteString& contents,
102                                   BCFORMAT format,
103                                   int32_t& outWidth,
104                                   int32_t& outHeight,
105                                   int32_t& e) {
106   return NULL;
107 }
108