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 7 #ifndef XFA_FXBARCODE_QRCODE_BC_QRCODERENCODER_H_ 8 #define XFA_FXBARCODE_QRCODE_BC_QRCODERENCODER_H_ 9 10 #include <utility> 11 #include <vector> 12 13 #include "core/fxcrt/fx_basic.h" 14 #include "core/fxcrt/fx_string.h" 15 16 class CBC_QRCoder; 17 class CBC_QRCoderErrorCorrectionLevel; 18 class CBC_QRCoderMode; 19 class CBC_QRCoderBitVector; 20 class CBC_CommonByteArray; 21 class CBC_CommonByteMatrix; 22 23 class CBC_QRCoderEncoder { 24 public: 25 CBC_QRCoderEncoder(); 26 virtual ~CBC_QRCoderEncoder(); 27 28 static void Encode(const CFX_ByteString& content, 29 CBC_QRCoderErrorCorrectionLevel* ecLevel, 30 CBC_QRCoder* qrCode, 31 int32_t& e, 32 int32_t versionSpecify = 0); 33 static void Encode(const CFX_WideString& content, 34 CBC_QRCoderErrorCorrectionLevel* ecLevel, 35 CBC_QRCoder* qrCode, 36 int32_t& e); 37 static void EncodeWithSpecifyVersion(const CFX_ByteString& content, 38 CBC_QRCoderErrorCorrectionLevel* ecLevel, 39 CBC_QRCoder* qrCode, 40 int32_t versionSpecify, 41 int32_t& e); 42 static void EncodeWithAutoVersion(const CFX_ByteString& content, 43 CBC_QRCoderErrorCorrectionLevel* ecLevel, 44 CBC_QRCoder* qrCode, 45 int32_t& e); 46 static CBC_QRCoderMode* ChooseMode(const CFX_ByteString& content, 47 CFX_ByteString encoding); 48 static int32_t GetAlphaNumericCode(int32_t code); 49 static void AppendECI(CBC_QRCoderBitVector* bits); 50 static void AppendBytes(const CFX_ByteString& content, 51 CBC_QRCoderMode* mode, 52 CBC_QRCoderBitVector* bits, 53 CFX_ByteString encoding, 54 int32_t& e); 55 static void AppendNumericBytes(const CFX_ByteString& content, 56 CBC_QRCoderBitVector* bits, 57 int32_t& e); 58 static void AppendAlphaNumericBytes(const CFX_ByteString& content, 59 CBC_QRCoderBitVector* bits, 60 int32_t& e); 61 static void Append8BitBytes(const CFX_ByteString& content, 62 CBC_QRCoderBitVector* bits, 63 CFX_ByteString encoding, 64 int32_t& e); 65 static void Append8BitBytes(CFX_ArrayTemplate<uint8_t>& bytes, 66 CBC_QRCoderBitVector* bits, 67 int32_t& e); 68 static void AppendKanjiBytes(const CFX_ByteString& content, 69 CBC_QRCoderBitVector* bits, 70 int32_t& e); 71 static void AppendGBKBytes(const CFX_ByteString& content, 72 CBC_QRCoderBitVector* bits, 73 int32_t& e); 74 static void InitQRCode(int32_t numInputBytes, 75 int32_t versionNumber, 76 CBC_QRCoderErrorCorrectionLevel* ecLevel, 77 CBC_QRCoderMode* mode, 78 CBC_QRCoder* qrCode, 79 int32_t& e); 80 static void InitQRCode(int32_t numInputBytes, 81 CBC_QRCoderErrorCorrectionLevel* ecLevel, 82 CBC_QRCoderMode* mode, 83 CBC_QRCoder* qrCode, 84 int32_t& e); 85 static void AppendModeInfo(CBC_QRCoderMode* mode, 86 CBC_QRCoderBitVector* bits, 87 int32_t& e); 88 static void AppendLengthInfo(int32_t numLetters, 89 int32_t version, 90 CBC_QRCoderMode* mode, 91 CBC_QRCoderBitVector* bits, 92 int32_t& e); 93 94 static void InterleaveWithECBytes(CBC_QRCoderBitVector* bits, 95 int32_t numTotalBytes, 96 int32_t numDataBytes, 97 int32_t numRSBlocks, 98 CBC_QRCoderBitVector* result, 99 int32_t& e); 100 static void GetNumDataBytesAndNumECBytesForBlockID( 101 int32_t numTotalBytes, 102 int32_t numDataBytes, 103 int32_t numRSBlocks, 104 int32_t blockID, 105 int32_t& numDataBytesInBlock, 106 int32_t& numECBytesInBlocks); 107 static CBC_CommonByteArray* GenerateECBytes(CBC_CommonByteArray* dataBytes, 108 int32_t numEcBytesInBlock, 109 int32_t& e); 110 static int32_t ChooseMaskPattern(CBC_QRCoderBitVector* bits, 111 CBC_QRCoderErrorCorrectionLevel* ecLevel, 112 int32_t version, 113 CBC_CommonByteMatrix* matrix, 114 int32_t& e); 115 static int32_t CalculateMaskPenalty(CBC_CommonByteMatrix* matrix); 116 static void TerminateBits(int32_t numDataBytes, 117 CBC_QRCoderBitVector* bits, 118 int32_t& e); 119 static int32_t GetSpanByVersion(CBC_QRCoderMode* modeFirst, 120 CBC_QRCoderMode* modeSecond, 121 int32_t versionNum, 122 int32_t& e); 123 static void MergeString( 124 std::vector<std::pair<CBC_QRCoderMode*, CFX_ByteString>>* result, 125 int32_t versionNum, 126 int32_t& e); 127 static void SplitString( 128 const CFX_ByteString& content, 129 std::vector<std::pair<CBC_QRCoderMode*, CFX_ByteString>>* result); 130 static void AppendDataModeLenghInfo( 131 const std::vector<std::pair<CBC_QRCoderMode*, CFX_ByteString>>& 132 splitResult, 133 CBC_QRCoderBitVector& headerAndDataBits, 134 CBC_QRCoderMode* tempMode, 135 CBC_QRCoder* qrCode, 136 CFX_ByteString& encoding, 137 int32_t& e); 138 }; 139 140 #endif // XFA_FXBARCODE_QRCODE_BC_QRCODERENCODER_H_ 141