From 1770c021cf998ff1b33855b1397f6ea8ff9f7cd7 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Mon, 14 Mar 2016 14:14:16 -0400 Subject: Move xfa/src up to xfa/. This CL moves the xfa/src files up to the xfa/ directory and fixes the includes, include guards, and build files. R=tsepez@chromium.org Review URL: https://codereview.chromium.org/1803723002 . --- xfa/fxbarcode/datamatrix/BC_ASCIIEncoder.cpp | 98 +++++ xfa/fxbarcode/datamatrix/BC_ASCIIEncoder.h | 27 ++ xfa/fxbarcode/datamatrix/BC_Base256Encoder.cpp | 87 ++++ xfa/fxbarcode/datamatrix/BC_Base256Encoder.h | 23 + xfa/fxbarcode/datamatrix/BC_C40Encoder.cpp | 198 +++++++++ xfa/fxbarcode/datamatrix/BC_C40Encoder.h | 34 ++ .../datamatrix/BC_DataMatrixBitMatrixParser.cpp | 378 ++++++++++++++++ .../datamatrix/BC_DataMatrixBitMatrixParser.h | 45 ++ .../datamatrix/BC_DataMatrixDataBlock.cpp | 115 +++++ xfa/fxbarcode/datamatrix/BC_DataMatrixDataBlock.h | 32 ++ .../BC_DataMatrixDecodedBitStreamParser.cpp | 473 +++++++++++++++++++++ .../BC_DataMatrixDecodedBitStreamParser.h | 58 +++ xfa/fxbarcode/datamatrix/BC_DataMatrixDecoder.cpp | 114 +++++ xfa/fxbarcode/datamatrix/BC_DataMatrixDecoder.h | 30 ++ xfa/fxbarcode/datamatrix/BC_DataMatrixDetector.cpp | 406 ++++++++++++++++++ xfa/fxbarcode/datamatrix/BC_DataMatrixDetector.h | 78 ++++ xfa/fxbarcode/datamatrix/BC_DataMatrixReader.cpp | 65 +++ xfa/fxbarcode/datamatrix/BC_DataMatrixReader.h | 28 ++ .../datamatrix/BC_DataMatrixSymbolInfo144.cpp | 40 ++ .../datamatrix/BC_DataMatrixSymbolInfo144.h | 20 + xfa/fxbarcode/datamatrix/BC_DataMatrixVersion.cpp | 173 ++++++++ xfa/fxbarcode/datamatrix/BC_DataMatrixVersion.h | 92 ++++ xfa/fxbarcode/datamatrix/BC_DataMatrixWriter.cpp | 136 ++++++ xfa/fxbarcode/datamatrix/BC_DataMatrixWriter.h | 33 ++ xfa/fxbarcode/datamatrix/BC_DefaultPlacement.cpp | 164 +++++++ xfa/fxbarcode/datamatrix/BC_DefaultPlacement.h | 40 ++ xfa/fxbarcode/datamatrix/BC_EdifactEncoder.cpp | 154 +++++++ xfa/fxbarcode/datamatrix/BC_EdifactEncoder.h | 29 ++ xfa/fxbarcode/datamatrix/BC_Encoder.cpp | 10 + xfa/fxbarcode/datamatrix/BC_Encoder.h | 21 + xfa/fxbarcode/datamatrix/BC_EncoderContext.cpp | 111 +++++ xfa/fxbarcode/datamatrix/BC_EncoderContext.h | 55 +++ xfa/fxbarcode/datamatrix/BC_ErrorCorrection.cpp | 175 ++++++++ xfa/fxbarcode/datamatrix/BC_ErrorCorrection.h | 39 ++ xfa/fxbarcode/datamatrix/BC_HighLevelEncoder.cpp | 375 ++++++++++++++++ xfa/fxbarcode/datamatrix/BC_HighLevelEncoder.h | 76 ++++ xfa/fxbarcode/datamatrix/BC_SymbolInfo.cpp | 255 +++++++++++ xfa/fxbarcode/datamatrix/BC_SymbolInfo.h | 83 ++++ xfa/fxbarcode/datamatrix/BC_SymbolShapeHint.cpp | 26 ++ xfa/fxbarcode/datamatrix/BC_SymbolShapeHint.h | 21 + xfa/fxbarcode/datamatrix/BC_TextEncoder.cpp | 98 +++++ xfa/fxbarcode/datamatrix/BC_TextEncoder.h | 19 + xfa/fxbarcode/datamatrix/BC_X12Encoder.cpp | 100 +++++ xfa/fxbarcode/datamatrix/BC_X12Encoder.h | 24 ++ 44 files changed, 4658 insertions(+) create mode 100644 xfa/fxbarcode/datamatrix/BC_ASCIIEncoder.cpp create mode 100644 xfa/fxbarcode/datamatrix/BC_ASCIIEncoder.h create mode 100644 xfa/fxbarcode/datamatrix/BC_Base256Encoder.cpp create mode 100644 xfa/fxbarcode/datamatrix/BC_Base256Encoder.h create mode 100644 xfa/fxbarcode/datamatrix/BC_C40Encoder.cpp create mode 100644 xfa/fxbarcode/datamatrix/BC_C40Encoder.h create mode 100644 xfa/fxbarcode/datamatrix/BC_DataMatrixBitMatrixParser.cpp create mode 100644 xfa/fxbarcode/datamatrix/BC_DataMatrixBitMatrixParser.h create mode 100644 xfa/fxbarcode/datamatrix/BC_DataMatrixDataBlock.cpp create mode 100644 xfa/fxbarcode/datamatrix/BC_DataMatrixDataBlock.h create mode 100644 xfa/fxbarcode/datamatrix/BC_DataMatrixDecodedBitStreamParser.cpp create mode 100644 xfa/fxbarcode/datamatrix/BC_DataMatrixDecodedBitStreamParser.h create mode 100644 xfa/fxbarcode/datamatrix/BC_DataMatrixDecoder.cpp create mode 100644 xfa/fxbarcode/datamatrix/BC_DataMatrixDecoder.h create mode 100644 xfa/fxbarcode/datamatrix/BC_DataMatrixDetector.cpp create mode 100644 xfa/fxbarcode/datamatrix/BC_DataMatrixDetector.h create mode 100644 xfa/fxbarcode/datamatrix/BC_DataMatrixReader.cpp create mode 100644 xfa/fxbarcode/datamatrix/BC_DataMatrixReader.h create mode 100644 xfa/fxbarcode/datamatrix/BC_DataMatrixSymbolInfo144.cpp create mode 100644 xfa/fxbarcode/datamatrix/BC_DataMatrixSymbolInfo144.h create mode 100644 xfa/fxbarcode/datamatrix/BC_DataMatrixVersion.cpp create mode 100644 xfa/fxbarcode/datamatrix/BC_DataMatrixVersion.h create mode 100644 xfa/fxbarcode/datamatrix/BC_DataMatrixWriter.cpp create mode 100644 xfa/fxbarcode/datamatrix/BC_DataMatrixWriter.h create mode 100644 xfa/fxbarcode/datamatrix/BC_DefaultPlacement.cpp create mode 100644 xfa/fxbarcode/datamatrix/BC_DefaultPlacement.h create mode 100644 xfa/fxbarcode/datamatrix/BC_EdifactEncoder.cpp create mode 100644 xfa/fxbarcode/datamatrix/BC_EdifactEncoder.h create mode 100644 xfa/fxbarcode/datamatrix/BC_Encoder.cpp create mode 100644 xfa/fxbarcode/datamatrix/BC_Encoder.h create mode 100644 xfa/fxbarcode/datamatrix/BC_EncoderContext.cpp create mode 100644 xfa/fxbarcode/datamatrix/BC_EncoderContext.h create mode 100644 xfa/fxbarcode/datamatrix/BC_ErrorCorrection.cpp create mode 100644 xfa/fxbarcode/datamatrix/BC_ErrorCorrection.h create mode 100644 xfa/fxbarcode/datamatrix/BC_HighLevelEncoder.cpp create mode 100644 xfa/fxbarcode/datamatrix/BC_HighLevelEncoder.h create mode 100644 xfa/fxbarcode/datamatrix/BC_SymbolInfo.cpp create mode 100644 xfa/fxbarcode/datamatrix/BC_SymbolInfo.h create mode 100644 xfa/fxbarcode/datamatrix/BC_SymbolShapeHint.cpp create mode 100644 xfa/fxbarcode/datamatrix/BC_SymbolShapeHint.h create mode 100644 xfa/fxbarcode/datamatrix/BC_TextEncoder.cpp create mode 100644 xfa/fxbarcode/datamatrix/BC_TextEncoder.h create mode 100644 xfa/fxbarcode/datamatrix/BC_X12Encoder.cpp create mode 100644 xfa/fxbarcode/datamatrix/BC_X12Encoder.h (limited to 'xfa/fxbarcode/datamatrix') diff --git a/xfa/fxbarcode/datamatrix/BC_ASCIIEncoder.cpp b/xfa/fxbarcode/datamatrix/BC_ASCIIEncoder.cpp new file mode 100644 index 0000000000..00d8fc1615 --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_ASCIIEncoder.cpp @@ -0,0 +1,98 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2006-2007 Jeremias Maerki. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/BC_Dimension.h" +#include "xfa/fxbarcode/datamatrix/BC_ASCIIEncoder.h" +#include "xfa/fxbarcode/datamatrix/BC_Encoder.h" +#include "xfa/fxbarcode/datamatrix/BC_EncoderContext.h" +#include "xfa/fxbarcode/datamatrix/BC_HighLevelEncoder.h" +#include "xfa/fxbarcode/datamatrix/BC_SymbolInfo.h" +#include "xfa/fxbarcode/datamatrix/BC_SymbolShapeHint.h" + +CBC_ASCIIEncoder::CBC_ASCIIEncoder() {} +CBC_ASCIIEncoder::~CBC_ASCIIEncoder() {} +int32_t CBC_ASCIIEncoder::getEncodingMode() { + return ASCII_ENCODATION; +} +void CBC_ASCIIEncoder::Encode(CBC_EncoderContext& context, int32_t& e) { + int32_t n = CBC_HighLevelEncoder::determineConsecutiveDigitCount( + context.m_msg, context.m_pos); + if (n >= 2) { + FX_WCHAR code = + encodeASCIIDigits(context.m_msg.GetAt(context.m_pos), + context.m_msg.GetAt(context.m_pos + 1), e); + if (e != BCExceptionNO) { + return; + } + context.writeCodeword(code); + context.m_pos += 2; + } else { + FX_WCHAR c = context.getCurrentChar(); + int32_t newMode = CBC_HighLevelEncoder::lookAheadTest( + context.m_msg, context.m_pos, getEncodingMode()); + if (newMode != getEncodingMode()) { + switch (newMode) { + case BASE256_ENCODATION: + context.writeCodeword(CBC_HighLevelEncoder::LATCH_TO_BASE256); + context.signalEncoderChange(BASE256_ENCODATION); + return; + case C40_ENCODATION: + context.writeCodeword(CBC_HighLevelEncoder::LATCH_TO_C40); + context.signalEncoderChange(C40_ENCODATION); + return; + case X12_ENCODATION: + context.writeCodeword(CBC_HighLevelEncoder::LATCH_TO_ANSIX12); + context.signalEncoderChange(X12_ENCODATION); + break; + case TEXT_ENCODATION: + context.writeCodeword(CBC_HighLevelEncoder::LATCH_TO_TEXT); + context.signalEncoderChange(TEXT_ENCODATION); + break; + case EDIFACT_ENCODATION: + context.writeCodeword(CBC_HighLevelEncoder::LATCH_TO_EDIFACT); + context.signalEncoderChange(EDIFACT_ENCODATION); + break; + default: + e = BCExceptionIllegalStateIllegalMode; + return; + } + } else if (CBC_HighLevelEncoder::isExtendedASCII(c)) { + context.writeCodeword(CBC_HighLevelEncoder::UPPER_SHIFT); + context.writeCodeword((FX_WCHAR)(c - 128 + 1)); + context.m_pos++; + } else { + context.writeCodeword((FX_WCHAR)(c + 1)); + context.m_pos++; + } + } +} +FX_WCHAR CBC_ASCIIEncoder::encodeASCIIDigits(FX_WCHAR digit1, + FX_WCHAR digit2, + int32_t& e) { + if (CBC_HighLevelEncoder::isDigit(digit1) && + CBC_HighLevelEncoder::isDigit(digit2)) { + int32_t num = (digit1 - 48) * 10 + (digit2 - 48); + return (FX_WCHAR)(num + 130); + } + e = BCExceptionIllegalArgumentNotGigits; + return 0; +} diff --git a/xfa/fxbarcode/datamatrix/BC_ASCIIEncoder.h b/xfa/fxbarcode/datamatrix/BC_ASCIIEncoder.h new file mode 100644 index 0000000000..11be5e046a --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_ASCIIEncoder.h @@ -0,0 +1,27 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_DATAMATRIX_BC_ASCIIENCODER_H_ +#define XFA_FXBARCODE_DATAMATRIX_BC_ASCIIENCODER_H_ + +#include "xfa/fxbarcode/datamatrix/BC_Encoder.h" + +class CBC_EncoderContext; + +class CBC_ASCIIEncoder : public CBC_Encoder { + public: + CBC_ASCIIEncoder(); + virtual ~CBC_ASCIIEncoder(); + int32_t getEncodingMode(); + void Encode(CBC_EncoderContext& context, int32_t& e); + + private: + static FX_WCHAR encodeASCIIDigits(FX_WCHAR digit1, + FX_WCHAR digit2, + int32_t& e); +}; + +#endif // XFA_FXBARCODE_DATAMATRIX_BC_ASCIIENCODER_H_ diff --git a/xfa/fxbarcode/datamatrix/BC_Base256Encoder.cpp b/xfa/fxbarcode/datamatrix/BC_Base256Encoder.cpp new file mode 100644 index 0000000000..98e7848ce8 --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_Base256Encoder.cpp @@ -0,0 +1,87 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2006-2007 Jeremias Maerki. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/BC_Dimension.h" +#include "xfa/fxbarcode/datamatrix/BC_Base256Encoder.h" +#include "xfa/fxbarcode/datamatrix/BC_Encoder.h" +#include "xfa/fxbarcode/datamatrix/BC_EncoderContext.h" +#include "xfa/fxbarcode/datamatrix/BC_HighLevelEncoder.h" +#include "xfa/fxbarcode/datamatrix/BC_SymbolInfo.h" +#include "xfa/fxbarcode/datamatrix/BC_SymbolShapeHint.h" + +CBC_Base256Encoder::CBC_Base256Encoder() {} +CBC_Base256Encoder::~CBC_Base256Encoder() {} +int32_t CBC_Base256Encoder::getEncodingMode() { + return BASE256_ENCODATION; +} +void CBC_Base256Encoder::Encode(CBC_EncoderContext& context, int32_t& e) { + CFX_WideString buffer; + buffer += (FX_WCHAR)'\0'; + while (context.hasMoreCharacters()) { + FX_WCHAR c = context.getCurrentChar(); + buffer += c; + context.m_pos++; + int32_t newMode = CBC_HighLevelEncoder::lookAheadTest( + context.m_msg, context.m_pos, getEncodingMode()); + if (newMode != getEncodingMode()) { + context.signalEncoderChange(newMode); + break; + } + } + int32_t dataCount = buffer.GetLength() - 1; + FX_CHAR buf[128]; + FXSYS_itoa(dataCount, buf, 10); + buffer.SetAt(0, FX_WCHAR(*buf) - '0'); + int32_t lengthFieldSize = 1; + int32_t currentSize = + context.getCodewordCount() + dataCount + lengthFieldSize; + context.updateSymbolInfo(currentSize, e); + if (e != BCExceptionNO) { + return; + } + FX_BOOL mustPad = (context.m_symbolInfo->m_dataCapacity - currentSize) > 0; + if (context.hasMoreCharacters() || mustPad) { + if (dataCount <= 249) { + buffer.SetAt(0, (FX_WCHAR)dataCount); + } else if (dataCount > 249 && dataCount <= 1555) { + buffer.SetAt(0, (FX_WCHAR)((dataCount / 250) + 249)); + buffer.Insert(1, (FX_WCHAR)(dataCount % 250)); + } else { + e = BCExceptionIllegalStateMessageLengthInvalid; + return; + } + } + for (int32_t i = 0, c = buffer.GetLength(); i < c; i++) { + context.writeCodeword( + randomize255State(buffer.GetAt(i), context.getCodewordCount() + 1)); + } +} +FX_WCHAR CBC_Base256Encoder::randomize255State(FX_WCHAR ch, + int32_t codewordPosition) { + int32_t pseudoRandom = ((149 * codewordPosition) % 255) + 1; + int32_t tempVariable = ch + pseudoRandom; + if (tempVariable <= 255) { + return (FX_WCHAR)tempVariable; + } else { + return (FX_WCHAR)(tempVariable - 256); + } +} diff --git a/xfa/fxbarcode/datamatrix/BC_Base256Encoder.h b/xfa/fxbarcode/datamatrix/BC_Base256Encoder.h new file mode 100644 index 0000000000..64dbdc7ba4 --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_Base256Encoder.h @@ -0,0 +1,23 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_DATAMATRIX_BC_BASE256ENCODER_H_ +#define XFA_FXBARCODE_DATAMATRIX_BC_BASE256ENCODER_H_ + +#include "xfa/fxbarcode/datamatrix/BC_Encoder.h" + +class CBC_Base256Encoder : public CBC_Encoder { + public: + CBC_Base256Encoder(); + virtual ~CBC_Base256Encoder(); + int32_t getEncodingMode(); + void Encode(CBC_EncoderContext& context, int32_t& e); + + private: + static FX_WCHAR randomize255State(FX_WCHAR ch, int32_t codewordPosition); +}; + +#endif // XFA_FXBARCODE_DATAMATRIX_BC_BASE256ENCODER_H_ diff --git a/xfa/fxbarcode/datamatrix/BC_C40Encoder.cpp b/xfa/fxbarcode/datamatrix/BC_C40Encoder.cpp new file mode 100644 index 0000000000..8858f60012 --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_C40Encoder.cpp @@ -0,0 +1,198 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2006-2007 Jeremias Maerki. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/BC_Dimension.h" +#include "xfa/fxbarcode/common/BC_CommonBitMatrix.h" +#include "xfa/fxbarcode/datamatrix/BC_C40Encoder.h" +#include "xfa/fxbarcode/datamatrix/BC_Encoder.h" +#include "xfa/fxbarcode/datamatrix/BC_EncoderContext.h" +#include "xfa/fxbarcode/datamatrix/BC_HighLevelEncoder.h" +#include "xfa/fxbarcode/datamatrix/BC_SymbolInfo.h" +#include "xfa/fxbarcode/datamatrix/BC_SymbolShapeHint.h" + +CBC_C40Encoder::CBC_C40Encoder() {} +CBC_C40Encoder::~CBC_C40Encoder() {} +int32_t CBC_C40Encoder::getEncodingMode() { + return C40_ENCODATION; +} +void CBC_C40Encoder::Encode(CBC_EncoderContext& context, int32_t& e) { + CFX_WideString buffer; + while (context.hasMoreCharacters()) { + FX_WCHAR c = context.getCurrentChar(); + context.m_pos++; + int32_t lastCharSize = encodeChar(c, buffer, e); + if (e != BCExceptionNO) { + return; + } + int32_t unwritten = (buffer.GetLength() / 3) * 2; + int32_t curCodewordCount = context.getCodewordCount() + unwritten; + context.updateSymbolInfo(curCodewordCount, e); + if (e != BCExceptionNO) { + return; + } + int32_t available = context.m_symbolInfo->m_dataCapacity - curCodewordCount; + if (!context.hasMoreCharacters()) { + CFX_WideString removed; + if ((buffer.GetLength() % 3) == 2) { + if (available < 2 || available > 2) { + lastCharSize = + backtrackOneCharacter(context, buffer, removed, lastCharSize, e); + if (e != BCExceptionNO) { + return; + } + } + } + while ((buffer.GetLength() % 3) == 1 && + ((lastCharSize <= 3 && available != 1) || lastCharSize > 3)) { + lastCharSize = + backtrackOneCharacter(context, buffer, removed, lastCharSize, e); + if (e != BCExceptionNO) { + return; + } + } + break; + } + int32_t count = buffer.GetLength(); + if ((count % 3) == 0) { + int32_t newMode = CBC_HighLevelEncoder::lookAheadTest( + context.m_msg, context.m_pos, getEncodingMode()); + if (newMode != getEncodingMode()) { + context.signalEncoderChange(newMode); + break; + } + } + } + handleEOD(context, buffer, e); +} +void CBC_C40Encoder::writeNextTriplet(CBC_EncoderContext& context, + CFX_WideString& buffer) { + context.writeCodewords(encodeToCodewords(buffer, 0)); + buffer.Delete(0, 3); +} +void CBC_C40Encoder::handleEOD(CBC_EncoderContext& context, + CFX_WideString& buffer, + int32_t& e) { + int32_t unwritten = (buffer.GetLength() / 3) * 2; + int32_t rest = buffer.GetLength() % 3; + int32_t curCodewordCount = context.getCodewordCount() + unwritten; + context.updateSymbolInfo(curCodewordCount, e); + if (e != BCExceptionNO) { + return; + } + int32_t available = context.m_symbolInfo->m_dataCapacity - curCodewordCount; + if (rest == 2) { + buffer += (FX_WCHAR)'\0'; + while (buffer.GetLength() >= 3) { + writeNextTriplet(context, buffer); + } + if (context.hasMoreCharacters()) { + context.writeCodeword(CBC_HighLevelEncoder::C40_UNLATCH); + } + } else if (available == 1 && rest == 1) { + while (buffer.GetLength() >= 3) { + writeNextTriplet(context, buffer); + } + if (context.hasMoreCharacters()) { + context.writeCodeword(CBC_HighLevelEncoder::C40_UNLATCH); + } + context.m_pos--; + } else if (rest == 0) { + while (buffer.GetLength() >= 3) { + writeNextTriplet(context, buffer); + } + if (available > 0 || context.hasMoreCharacters()) { + context.writeCodeword(CBC_HighLevelEncoder::C40_UNLATCH); + } + } else { + e = BCExceptionIllegalStateUnexpectedCase; + return; + } + context.signalEncoderChange(ASCII_ENCODATION); +} +int32_t CBC_C40Encoder::encodeChar(FX_WCHAR c, CFX_WideString& sb, int32_t& e) { + if (c == ' ') { + sb += (FX_WCHAR)'\3'; + return 1; + } else if ((c >= '0') && (c <= '9')) { + sb += (FX_WCHAR)(c - 48 + 4); + return 1; + } else if ((c >= 'A') && (c <= 'Z')) { + sb += (FX_WCHAR)(c - 65 + 14); + return 1; + } else if (c <= 0x1f) { + sb += (FX_WCHAR)'\0'; + sb += c; + return 2; + } else if ((c >= '!') && (c <= '/')) { + sb += (FX_WCHAR)'\1'; + sb += (FX_WCHAR)(c - 33); + return 2; + } else if ((c >= ':') && (c <= '@')) { + sb += (FX_WCHAR)'\1'; + sb += (FX_WCHAR)(c - 58 + 15); + return 2; + } else if ((c >= '[') && (c <= '_')) { + sb += (FX_WCHAR)'\1'; + sb += (FX_WCHAR)(c - 91 + 22); + return 2; + } else if ((c >= 60) && (c <= 0x7f)) { + sb += (FX_WCHAR)'\2'; + sb += (FX_WCHAR)(c - 96); + return 2; + } else if (c >= 80) { + sb += (FX_WCHAR)'\1'; + sb += (FX_WCHAR)0x001e; + int32_t len = 2; + len += encodeChar((c - 128), sb, e); + BC_EXCEPTION_CHECK_ReturnValue(e, 0); + return len; + } else { + e = BCExceptionIllegalArgument; + return 0; + } +} +int32_t CBC_C40Encoder::backtrackOneCharacter(CBC_EncoderContext& context, + CFX_WideString& buffer, + CFX_WideString& removed, + int32_t lastCharSize, + int32_t& e) { + int32_t count = buffer.GetLength(); + buffer.Delete(count - lastCharSize, count); + context.m_pos--; + FX_WCHAR c = context.getCurrentChar(); + lastCharSize = encodeChar(c, removed, e); + BC_EXCEPTION_CHECK_ReturnValue(e, -1); + context.resetSymbolInfo(); + return lastCharSize; +} +CFX_WideString CBC_C40Encoder::encodeToCodewords(CFX_WideString sb, + int32_t startPos) { + FX_WCHAR c1 = sb.GetAt(startPos); + FX_WCHAR c2 = sb.GetAt(startPos + 1); + FX_WCHAR c3 = sb.GetAt(startPos + 2); + int32_t v = (1600 * c1) + (40 * c2) + c3 + 1; + FX_WCHAR cw1 = (FX_WCHAR)(v / 256); + FX_WCHAR cw2 = (FX_WCHAR)(v % 256); + CFX_WideString b1(cw1); + CFX_WideString b2(cw2); + return b1 + b2; +} diff --git a/xfa/fxbarcode/datamatrix/BC_C40Encoder.h b/xfa/fxbarcode/datamatrix/BC_C40Encoder.h new file mode 100644 index 0000000000..9dcb1e5c5f --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_C40Encoder.h @@ -0,0 +1,34 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_DATAMATRIX_BC_C40ENCODER_H_ +#define XFA_FXBARCODE_DATAMATRIX_BC_C40ENCODER_H_ + +#include "xfa/fxbarcode/datamatrix/BC_Encoder.h" + +class CBC_C40Encoder : public CBC_Encoder { + public: + CBC_C40Encoder(); + virtual ~CBC_C40Encoder(); + virtual int32_t getEncodingMode(); + virtual void Encode(CBC_EncoderContext& context, int32_t& e); + static void writeNextTriplet(CBC_EncoderContext& context, + CFX_WideString& buffer); + virtual void handleEOD(CBC_EncoderContext& context, + CFX_WideString& buffer, + int32_t& e); + virtual int32_t encodeChar(FX_WCHAR c, CFX_WideString& sb, int32_t& e); + + private: + int32_t backtrackOneCharacter(CBC_EncoderContext& context, + CFX_WideString& buffer, + CFX_WideString& removed, + int32_t lastCharSize, + int32_t& e); + static CFX_WideString encodeToCodewords(CFX_WideString sb, int32_t startPos); +}; + +#endif // XFA_FXBARCODE_DATAMATRIX_BC_C40ENCODER_H_ diff --git a/xfa/fxbarcode/datamatrix/BC_DataMatrixBitMatrixParser.cpp b/xfa/fxbarcode/datamatrix/BC_DataMatrixBitMatrixParser.cpp new file mode 100644 index 0000000000..a0c00e9506 --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_DataMatrixBitMatrixParser.cpp @@ -0,0 +1,378 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2007 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/datamatrix/BC_DataMatrixBitMatrixParser.h" + +#include + +#include "xfa/fxbarcode/common/BC_CommonBitMatrix.h" +#include "xfa/fxbarcode/datamatrix/BC_DataMatrixVersion.h" +#include "xfa/fxbarcode/utils.h" + +CBC_DataMatrixBitMatrixParser::CBC_DataMatrixBitMatrixParser() { + m_mappingBitMatrix = NULL; + m_version = NULL; + m_readMappingMatrix = NULL; +} +void CBC_DataMatrixBitMatrixParser::Init(CBC_CommonBitMatrix* bitMatrix, + int32_t& e) { + int32_t dimension = bitMatrix->GetHeight(); + if (dimension < 8 || dimension > 144 || (dimension & 0x01) != 0) { + e = BCExceptionFormatException; + return; + } + m_version = ReadVersion(bitMatrix, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + m_mappingBitMatrix = ExtractDataRegion(bitMatrix, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + m_readMappingMatrix = new CBC_CommonBitMatrix(); + m_readMappingMatrix->Init(m_mappingBitMatrix->GetWidth(), + m_mappingBitMatrix->GetHeight()); +} +CBC_DataMatrixBitMatrixParser::~CBC_DataMatrixBitMatrixParser() { + delete m_mappingBitMatrix; + delete m_readMappingMatrix; +} +CBC_DataMatrixVersion* CBC_DataMatrixBitMatrixParser::GetVersion() { + return m_version; +} +CBC_DataMatrixVersion* CBC_DataMatrixBitMatrixParser::ReadVersion( + CBC_CommonBitMatrix* bitMatrix, + int32_t& e) { + int32_t rows = bitMatrix->GetHeight(); + int32_t columns = bitMatrix->GetWidth(); + CBC_DataMatrixVersion* temp = + CBC_DataMatrixVersion::GetVersionForDimensions(rows, columns, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return temp; +} +CFX_ByteArray* CBC_DataMatrixBitMatrixParser::ReadCodewords(int32_t& e) { + std::unique_ptr result(new CFX_ByteArray()); + result->SetSize(m_version->GetTotalCodewords()); + int32_t resultOffset = 0; + int32_t row = 4; + int32_t column = 0; + int32_t numRows = m_mappingBitMatrix->GetHeight(); + int32_t numColumns = m_mappingBitMatrix->GetWidth(); + FX_BOOL corner1Read = FALSE; + FX_BOOL corner2Read = FALSE; + FX_BOOL corner3Read = FALSE; + FX_BOOL corner4Read = FALSE; + do { + if ((row == numRows) && (column == 0) && !corner1Read) { + (*result)[resultOffset++] = (uint8_t)ReadCorner1(numRows, numColumns); + row -= 2; + column += 2; + corner1Read = TRUE; + } else if ((row == numRows - 2) && (column == 0) && + ((numColumns & 0x03) != 0) && !corner2Read) { + (*result)[resultOffset++] = (uint8_t)ReadCorner2(numRows, numColumns); + row -= 2; + column += 2; + corner2Read = TRUE; + } else if ((row == numRows + 4) && (column == 2) && + ((numColumns & 0x07) == 0) && !corner3Read) { + (*result)[resultOffset++] = (uint8_t)ReadCorner3(numRows, numColumns); + row -= 2; + column += 2; + corner3Read = TRUE; + } else if ((row == numRows - 2) && (column == 0) && + ((numColumns & 0x07) == 4) && !corner4Read) { + (*result)[resultOffset++] = (uint8_t)ReadCorner4(numRows, numColumns); + row -= 2; + column += 2; + corner4Read = TRUE; + } else { + do { + if ((row < numRows) && (column >= 0) && + !m_readMappingMatrix->Get(column, row)) { + if (resultOffset < (*result).GetSize()) { + (*result)[resultOffset++] = + (uint8_t)ReadUtah(row, column, numRows, numColumns); + } + } + row -= 2; + column += 2; + } while ((row >= 0) && (column < numColumns)); + row += 1; + column += 3; + do { + if ((row >= 0) && (column < numColumns) && + !m_readMappingMatrix->Get(column, row)) { + if (resultOffset < (*result).GetSize()) { + (*result)[resultOffset++] = + (uint8_t)ReadUtah(row, column, numRows, numColumns); + } + } + row += 2; + column -= 2; + } while ((row < numRows) && (column >= 0)); + row += 3; + column += 1; + } + } while ((row < numRows) || (column < numColumns)); + if (resultOffset != m_version->GetTotalCodewords()) { + e = BCExceptionFormatException; + return NULL; + } + return result.release(); +} +FX_BOOL CBC_DataMatrixBitMatrixParser::ReadModule(int32_t row, + int32_t column, + int32_t numRows, + int32_t numColumns) { + if (row < 0) { + row += numRows; + column += 4 - ((numRows + 4) & 0x07); + } + if (column < 0) { + column += numColumns; + row += 4 - ((numColumns + 4) & 0x07); + } + m_readMappingMatrix->Set(column, row); + return m_mappingBitMatrix->Get(column, row); +} +int32_t CBC_DataMatrixBitMatrixParser::ReadUtah(int32_t row, + int32_t column, + int32_t numRows, + int32_t numColumns) { + int32_t currentByte = 0; + if (ReadModule(row - 2, column - 2, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(row - 2, column - 1, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(row - 1, column - 2, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(row - 1, column - 1, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(row - 1, column, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(row, column - 2, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(row, column - 1, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(row, column, numRows, numColumns)) { + currentByte |= 1; + } + return currentByte; +} +int32_t CBC_DataMatrixBitMatrixParser::ReadCorner1(int32_t numRows, + int32_t numColumns) { + int32_t currentByte = 0; + if (ReadModule(numRows - 1, 0, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(numRows - 1, 1, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(numRows - 1, 2, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(0, numColumns - 2, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(0, numColumns - 1, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(1, numColumns - 1, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(2, numColumns - 1, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(3, numColumns - 1, numRows, numColumns)) { + currentByte |= 1; + } + return currentByte; +} +int32_t CBC_DataMatrixBitMatrixParser::ReadCorner2(int32_t numRows, + int32_t numColumns) { + int32_t currentByte = 0; + if (ReadModule(numRows - 3, 0, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(numRows - 2, 0, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(numRows - 1, 0, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(0, numColumns - 4, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(0, numColumns - 3, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(0, numColumns - 2, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(0, numColumns - 1, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(1, numColumns - 1, numRows, numColumns)) { + currentByte |= 1; + } + return currentByte; +} +int32_t CBC_DataMatrixBitMatrixParser::ReadCorner3(int32_t numRows, + int32_t numColumns) { + int32_t currentByte = 0; + if (ReadModule(numRows - 1, 0, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(numRows - 1, numColumns - 1, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(0, numColumns - 3, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(0, numColumns - 2, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(0, numColumns - 1, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(1, numColumns - 3, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(1, numColumns - 2, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(1, numColumns - 1, numRows, numColumns)) { + currentByte |= 1; + } + return currentByte; +} +int32_t CBC_DataMatrixBitMatrixParser::ReadCorner4(int32_t numRows, + int32_t numColumns) { + int32_t currentByte = 0; + if (ReadModule(numRows - 3, 0, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(numRows - 2, 0, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(numRows - 1, 0, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(0, numColumns - 2, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(0, numColumns - 1, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(1, numColumns - 1, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(2, numColumns - 1, numRows, numColumns)) { + currentByte |= 1; + } + currentByte <<= 1; + if (ReadModule(3, numColumns - 1, numRows, numColumns)) { + currentByte |= 1; + } + return currentByte; +} +CBC_CommonBitMatrix* CBC_DataMatrixBitMatrixParser::ExtractDataRegion( + CBC_CommonBitMatrix* bitMatrix, + int32_t& e) { + int32_t symbolSizeRows = m_version->GetSymbolSizeRows(); + int32_t symbolSizeColumns = m_version->GetSymbolSizeColumns(); + if (bitMatrix->GetHeight() != symbolSizeRows) { + e = BCExceptionCanNotCallGetDimensionOnNonSquareMatrix; + return NULL; + } + int32_t dataRegionSizeRows = m_version->GetDataRegionSizeRows(); + int32_t dataRegionSizeColumns = m_version->GetDataRegionSizeColumns(); + int32_t numDataRegionsRow = symbolSizeRows / dataRegionSizeRows; + int32_t numDataRegionsColumn = symbolSizeColumns / dataRegionSizeColumns; + int32_t sizeDataRegionRow = numDataRegionsRow * dataRegionSizeRows; + int32_t sizeDataRegionColumn = numDataRegionsColumn * dataRegionSizeColumns; + CBC_CommonBitMatrix* bitMatrixWithoutAlignment = new CBC_CommonBitMatrix(); + bitMatrixWithoutAlignment->Init(sizeDataRegionColumn, sizeDataRegionRow); + int32_t dataRegionRow; + for (dataRegionRow = 0; dataRegionRow < numDataRegionsRow; ++dataRegionRow) { + int32_t dataRegionRowOffset = dataRegionRow * dataRegionSizeRows; + int32_t dataRegionColumn; + for (dataRegionColumn = 0; dataRegionColumn < numDataRegionsColumn; + ++dataRegionColumn) { + int32_t dataRegionColumnOffset = dataRegionColumn * dataRegionSizeColumns; + int32_t i; + for (i = 0; i < dataRegionSizeRows; ++i) { + int32_t readRowOffset = + dataRegionRow * (dataRegionSizeRows + 2) + 1 + i; + int32_t writeRowOffset = dataRegionRowOffset + i; + int32_t j; + for (j = 0; j < dataRegionSizeColumns; ++j) { + int32_t readColumnOffset = + dataRegionColumn * (dataRegionSizeColumns + 2) + 1 + j; + if (bitMatrix->Get(readColumnOffset, readRowOffset)) { + int32_t writeColumnOffset = dataRegionColumnOffset + j; + bitMatrixWithoutAlignment->Set(writeColumnOffset, writeRowOffset); + } + } + } + } + } + return bitMatrixWithoutAlignment; +} diff --git a/xfa/fxbarcode/datamatrix/BC_DataMatrixBitMatrixParser.h b/xfa/fxbarcode/datamatrix/BC_DataMatrixBitMatrixParser.h new file mode 100644 index 0000000000..4c54d61eef --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_DataMatrixBitMatrixParser.h @@ -0,0 +1,45 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_DATAMATRIX_BC_DATAMATRIXBITMATRIXPARSER_H_ +#define XFA_FXBARCODE_DATAMATRIX_BC_DATAMATRIXBITMATRIXPARSER_H_ + +#include "core/include/fxcrt/fx_basic.h" + +class CBC_CommonBitMatrix; +class CBC_DataMatrixVersion; + +class CBC_DataMatrixBitMatrixParser { + public: + CBC_DataMatrixBitMatrixParser(); + virtual ~CBC_DataMatrixBitMatrixParser(); + CBC_DataMatrixVersion* GetVersion(); + CFX_ByteArray* ReadCodewords(int32_t& e); + FX_BOOL ReadModule(int32_t row, + int32_t column, + int32_t numRows, + int32_t numColumns); + int32_t ReadUtah(int32_t row, + int32_t column, + int32_t numRows, + int32_t numColumns); + int32_t ReadCorner1(int32_t numRows, int32_t numColumns); + int32_t ReadCorner2(int32_t numRows, int32_t numColumns); + int32_t ReadCorner3(int32_t numRows, int32_t numColumns); + int32_t ReadCorner4(int32_t numRows, int32_t numColumns); + CBC_CommonBitMatrix* ExtractDataRegion(CBC_CommonBitMatrix* bitMatrix, + int32_t& e); + virtual void Init(CBC_CommonBitMatrix* bitMatrix, int32_t& e); + + private: + static CBC_DataMatrixVersion* ReadVersion(CBC_CommonBitMatrix* bitMatrix, + int32_t& e); + CBC_CommonBitMatrix* m_mappingBitMatrix; + CBC_CommonBitMatrix* m_readMappingMatrix; + CBC_DataMatrixVersion* m_version; +}; + +#endif // XFA_FXBARCODE_DATAMATRIX_BC_DATAMATRIXBITMATRIXPARSER_H_ diff --git a/xfa/fxbarcode/datamatrix/BC_DataMatrixDataBlock.cpp b/xfa/fxbarcode/datamatrix/BC_DataMatrixDataBlock.cpp new file mode 100644 index 0000000000..89c09c22a8 --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_DataMatrixDataBlock.cpp @@ -0,0 +1,115 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2008 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/datamatrix/BC_DataMatrixDataBlock.h" + +#include + +#include "xfa/fxbarcode/datamatrix/BC_DataMatrixVersion.h" +#include "xfa/fxbarcode/utils.h" + +CBC_DataMatrixDataBlock::~CBC_DataMatrixDataBlock() {} +CBC_DataMatrixDataBlock::CBC_DataMatrixDataBlock(int32_t numDataCodewords, + CFX_ByteArray* codewords) { + m_codewords.Copy(*codewords); + m_numDataCodewords = numDataCodewords; +} +CFX_PtrArray* CBC_DataMatrixDataBlock::GetDataBlocks( + CFX_ByteArray* rawCodewords, + CBC_DataMatrixVersion* version, + int32_t& e) { + ECBlocks* ecBlocks = version->GetECBlocks(); + int32_t totalBlocks = 0; + const CFX_PtrArray& ecBlockArray = ecBlocks->GetECBlocks(); + int32_t i; + for (i = 0; i < ecBlockArray.GetSize(); i++) { + totalBlocks += ((ECB*)ecBlockArray[i])->GetCount(); + } + std::unique_ptr result(new CFX_PtrArray()); + result->SetSize(totalBlocks); + int32_t numResultBlocks = 0; + int32_t j; + for (j = 0; j < ecBlockArray.GetSize(); j++) { + for (i = 0; i < ((ECB*)ecBlockArray[j])->GetCount(); i++) { + int32_t numDataCodewords = ((ECB*)ecBlockArray[j])->GetDataCodewords(); + int32_t numBlockCodewords = ecBlocks->GetECCodewords() + numDataCodewords; + CFX_ByteArray codewords; + codewords.SetSize(numBlockCodewords); + (*result)[numResultBlocks++] = + new CBC_DataMatrixDataBlock(numDataCodewords, &codewords); + codewords.SetSize(0); + } + } + int32_t longerBlocksTotalCodewords = + ((CBC_DataMatrixDataBlock*)(*result)[0])->GetCodewords()->GetSize(); + int32_t longerBlocksNumDataCodewords = + longerBlocksTotalCodewords - ecBlocks->GetECCodewords(); + int32_t shorterBlocksNumDataCodewords = longerBlocksNumDataCodewords - 1; + int32_t rawCodewordsOffset = 0; + for (i = 0; i < shorterBlocksNumDataCodewords; i++) { + int32_t j; + for (j = 0; j < numResultBlocks; j++) { + if (rawCodewordsOffset < rawCodewords->GetSize()) { + ((CBC_DataMatrixDataBlock*)(*result)[j]) + ->GetCodewords() + -> + operator[](i) = (*rawCodewords)[rawCodewordsOffset++]; + } + } + } + const bool specialVersion = version->GetVersionNumber() == 24; + int32_t numLongerBlocks = specialVersion ? 8 : numResultBlocks; + for (j = 0; j < numLongerBlocks; j++) { + if (rawCodewordsOffset < rawCodewords->GetSize()) { + ((CBC_DataMatrixDataBlock*)(*result)[j]) + ->GetCodewords() + -> + operator[](longerBlocksNumDataCodewords - 1) = + (*rawCodewords)[rawCodewordsOffset++]; + } + } + int32_t max = + ((CBC_DataMatrixDataBlock*)(*result)[0])->GetCodewords()->GetSize(); + for (i = longerBlocksNumDataCodewords; i < max; i++) { + int32_t j; + for (j = 0; j < numResultBlocks; j++) { + int32_t iOffset = specialVersion && j > 7 ? i - 1 : i; + if (rawCodewordsOffset < rawCodewords->GetSize()) { + ((CBC_DataMatrixDataBlock*)(*result)[j]) + ->GetCodewords() + -> + operator[](iOffset) = (*rawCodewords)[rawCodewordsOffset++]; + } + } + } + if (rawCodewordsOffset != rawCodewords->GetSize()) { + e = BCExceptionIllegalArgument; + return NULL; + } + return result.release(); +} +int32_t CBC_DataMatrixDataBlock::GetNumDataCodewords() { + return m_numDataCodewords; +} +CFX_ByteArray* CBC_DataMatrixDataBlock::GetCodewords() { + return &m_codewords; +} diff --git a/xfa/fxbarcode/datamatrix/BC_DataMatrixDataBlock.h b/xfa/fxbarcode/datamatrix/BC_DataMatrixDataBlock.h new file mode 100644 index 0000000000..218bb0bb49 --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_DataMatrixDataBlock.h @@ -0,0 +1,32 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_DATAMATRIX_BC_DATAMATRIXDATABLOCK_H_ +#define XFA_FXBARCODE_DATAMATRIX_BC_DATAMATRIXDATABLOCK_H_ + +#include "core/include/fxcrt/fx_basic.h" + +class CBC_DataMatrixVersion; + +class CBC_DataMatrixDataBlock { + public: + virtual ~CBC_DataMatrixDataBlock(); + + int32_t GetNumDataCodewords(); + CFX_ByteArray* GetCodewords(); + + static CFX_PtrArray* GetDataBlocks(CFX_ByteArray* rawCodewords, + CBC_DataMatrixVersion* version, + int32_t& e); + + private: + int32_t m_numDataCodewords; + CFX_ByteArray m_codewords; + + CBC_DataMatrixDataBlock(int32_t numDataCodewords, CFX_ByteArray* codewords); +}; + +#endif // XFA_FXBARCODE_DATAMATRIX_BC_DATAMATRIXDATABLOCK_H_ diff --git a/xfa/fxbarcode/datamatrix/BC_DataMatrixDecodedBitStreamParser.cpp b/xfa/fxbarcode/datamatrix/BC_DataMatrixDecodedBitStreamParser.cpp new file mode 100644 index 0000000000..693b6b9209 --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_DataMatrixDecodedBitStreamParser.cpp @@ -0,0 +1,473 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2008 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/common/BC_CommonBitSource.h" +#include "xfa/fxbarcode/common/BC_CommonDecoderResult.h" +#include "xfa/fxbarcode/datamatrix/BC_DataMatrixDecodedBitStreamParser.h" + +const FX_CHAR CBC_DataMatrixDecodedBitStreamParser::C40_BASIC_SET_CHARS[] = { + '*', '*', '*', ' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', + 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; +const FX_CHAR CBC_DataMatrixDecodedBitStreamParser::C40_SHIFT2_SET_CHARS[] = { + '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', + '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_'}; +const FX_CHAR CBC_DataMatrixDecodedBitStreamParser::TEXT_BASIC_SET_CHARS[] = { + '*', '*', '*', ' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', + 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; +const FX_CHAR CBC_DataMatrixDecodedBitStreamParser::TEXT_SHIFT3_SET_CHARS[] = { + '\'', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', + 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', + 'V', 'W', 'X', 'Y', 'Z', '{', '|', '}', '~', (FX_CHAR)127}; +const int32_t CBC_DataMatrixDecodedBitStreamParser::PAD_ENCODE = 0; +const int32_t CBC_DataMatrixDecodedBitStreamParser::ASCII_ENCODE = 1; +const int32_t CBC_DataMatrixDecodedBitStreamParser::C40_ENCODE = 2; +const int32_t CBC_DataMatrixDecodedBitStreamParser::TEXT_ENCODE = 3; +const int32_t CBC_DataMatrixDecodedBitStreamParser::ANSIX12_ENCODE = 4; +const int32_t CBC_DataMatrixDecodedBitStreamParser::EDIFACT_ENCODE = 5; +const int32_t CBC_DataMatrixDecodedBitStreamParser::BASE256_ENCODE = 6; +CBC_DataMatrixDecodedBitStreamParser::CBC_DataMatrixDecodedBitStreamParser() {} +CBC_DataMatrixDecodedBitStreamParser::~CBC_DataMatrixDecodedBitStreamParser() {} +CBC_CommonDecoderResult* CBC_DataMatrixDecodedBitStreamParser::Decode( + CFX_ByteArray& bytes, + int32_t& e) { + CBC_CommonBitSource bits(&bytes); + CFX_ByteString result; + CFX_ByteString resultTrailer; + CFX_Int32Array byteSegments; + int32_t mode = ASCII_ENCODE; + do { + if (mode == 1) { + mode = DecodeAsciiSegment(&bits, result, resultTrailer, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } else { + switch (mode) { + case 2: + DecodeC40Segment(&bits, result, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + break; + case 3: + DecodeTextSegment(&bits, result, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + break; + case 4: + DecodeAnsiX12Segment(&bits, result, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + break; + case 5: + DecodeEdifactSegment(&bits, result, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + break; + case 6: + DecodeBase256Segment(&bits, result, byteSegments, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + break; + default: + e = BCExceptionFormatException; + return NULL; + } + mode = ASCII_ENCODE; + } + } while (mode != PAD_ENCODE && bits.Available() > 0); + if (resultTrailer.GetLength() > 0) { + result += resultTrailer; + } + CBC_CommonDecoderResult* tempCp = new CBC_CommonDecoderResult(); + tempCp->Init(bytes, result, + (byteSegments.GetSize() <= 0) ? CFX_Int32Array() : byteSegments, + NULL, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return tempCp; +} +int32_t CBC_DataMatrixDecodedBitStreamParser::DecodeAsciiSegment( + CBC_CommonBitSource* bits, + CFX_ByteString& result, + CFX_ByteString& resultTrailer, + int32_t& e) { + FX_CHAR buffer[128]; + FX_BOOL upperShift = FALSE; + do { + int32_t oneByte = bits->ReadBits(8, e); + BC_EXCEPTION_CHECK_ReturnValue(e, 0); + if (oneByte == 0) { + e = BCExceptionFormatException; + return 0; + } else if (oneByte <= 128) { + oneByte = upperShift ? oneByte + 128 : oneByte; + upperShift = FALSE; + result += ((FX_CHAR)(oneByte - 1)); + return ASCII_ENCODE; + } else if (oneByte == 129) { + return PAD_ENCODE; + } else if (oneByte <= 229) { + int32_t value = oneByte - 130; + FXSYS_itoa(value, buffer, 10); + if (value < 10) { + result += '0'; + buffer[1] = '\0'; + } else { + buffer[2] = '\0'; + } + result += buffer; + } else if (oneByte == 230) { + return C40_ENCODE; + } else if (oneByte == 231) { + return BASE256_ENCODE; + } else if (oneByte == 232 || oneByte == 233 || oneByte == 234) { + } else if (oneByte == 235) { + upperShift = TRUE; + } else if (oneByte == 236) { + result += "[)>"; + result += 0x1E; + result += "05"; + result += 0x1D; + resultTrailer.Insert(0, 0x1E); + resultTrailer.Insert(0 + 1, 0x04); + } else if (oneByte == 237) { + result += "[)>"; + result += 0x1E; + result += "06"; + result += 0x1D; + resultTrailer.Insert(0, 0x1E); + resultTrailer.Insert(0 + 1, 0x04); + } else if (oneByte == 238) { + return ANSIX12_ENCODE; + } else if (oneByte == 239) { + return TEXT_ENCODE; + } else if (oneByte == 240) { + return EDIFACT_ENCODE; + } else if (oneByte == 241) { + } else if (oneByte >= 242) { + if (oneByte == 254 && bits->Available() == 0) { + } else { + e = BCExceptionFormatException; + return 0; + } + } + } while (bits->Available() > 0); + return ASCII_ENCODE; +} +void CBC_DataMatrixDecodedBitStreamParser::DecodeC40Segment( + CBC_CommonBitSource* bits, + CFX_ByteString& result, + int32_t& e) { + FX_BOOL upperShift = FALSE; + CFX_Int32Array cValues; + cValues.SetSize(3); + do { + if (bits->Available() == 8) { + return; + } + int32_t firstByte = bits->ReadBits(8, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + if (firstByte == 254) { + return; + } + int32_t tempp = bits->ReadBits(8, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + ParseTwoBytes(firstByte, tempp, cValues); + int32_t shift = 0; + int32_t i; + for (i = 0; i < 3; i++) { + int32_t cValue = cValues[i]; + switch (shift) { + case 0: + if (cValue < 3) { + shift = cValue + 1; + } else if (cValue < 27) { + FX_CHAR c40char = C40_BASIC_SET_CHARS[cValue]; + if (upperShift) { + result += (FX_CHAR)(c40char + 128); + upperShift = FALSE; + } else { + result += c40char; + } + } else { + e = BCExceptionFormatException; + return; + } + break; + case 1: + if (upperShift) { + result += (FX_CHAR)(cValue + 128); + upperShift = FALSE; + } else { + result += cValue; + } + shift = 0; + break; + case 2: + if (cValue < 27) { + FX_CHAR c40char = C40_SHIFT2_SET_CHARS[cValue]; + if (upperShift) { + result += (FX_CHAR)(c40char + 128); + upperShift = FALSE; + } else { + result += c40char; + } + } else if (cValue == 27) { + e = BCExceptionFormatException; + return; + } else if (cValue == 30) { + upperShift = TRUE; + } else { + e = BCExceptionFormatException; + return; + } + shift = 0; + break; + case 3: + if (upperShift) { + result += (FX_CHAR)(cValue + 224); + upperShift = FALSE; + } else { + result += (FX_CHAR)(cValue + 96); + } + shift = 0; + break; + default: + break; + e = BCExceptionFormatException; + return; + } + } + } while (bits->Available() > 0); +} +void CBC_DataMatrixDecodedBitStreamParser::DecodeTextSegment( + CBC_CommonBitSource* bits, + CFX_ByteString& result, + int32_t& e) { + FX_BOOL upperShift = FALSE; + CFX_Int32Array cValues; + cValues.SetSize(3); + int32_t shift = 0; + do { + if (bits->Available() == 8) { + return; + } + int32_t firstByte = bits->ReadBits(8, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + if (firstByte == 254) { + return; + } + int32_t inTp = bits->ReadBits(8, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + ParseTwoBytes(firstByte, inTp, cValues); + for (int32_t i = 0; i < 3; i++) { + int32_t cValue = cValues[i]; + switch (shift) { + case 0: + if (cValue < 3) { + shift = cValue + 1; + } else if (cValue < 40) { + FX_CHAR textChar = TEXT_BASIC_SET_CHARS[cValue]; + if (upperShift) { + result += (FX_CHAR)(textChar + 128); + upperShift = FALSE; + } else { + result += textChar; + } + } else { + e = BCExceptionFormatException; + return; + } + break; + case 1: + if (upperShift) { + result += (FX_CHAR)(cValue + 128); + upperShift = FALSE; + } else { + result += cValue; + } + shift = 0; + break; + case 2: + if (cValue < 27) { + FX_CHAR c40char = C40_SHIFT2_SET_CHARS[cValue]; + if (upperShift) { + result += (FX_CHAR)(c40char + 128); + upperShift = FALSE; + } else { + result += c40char; + } + } else if (cValue == 27) { + e = BCExceptionFormatException; + return; + } else if (cValue == 30) { + upperShift = TRUE; + } else { + e = BCExceptionFormatException; + return; + } + shift = 0; + break; + case 3: + if (cValue < 19) { + FX_CHAR textChar = TEXT_SHIFT3_SET_CHARS[cValue]; + if (upperShift) { + result += (FX_CHAR)(textChar + 128); + upperShift = FALSE; + } else { + result += textChar; + } + shift = 0; + } else { + e = BCExceptionFormatException; + return; + } + break; + default: + break; + e = BCExceptionFormatException; + return; + } + } + } while (bits->Available() > 0); +} +void CBC_DataMatrixDecodedBitStreamParser::DecodeAnsiX12Segment( + CBC_CommonBitSource* bits, + CFX_ByteString& result, + int32_t& e) { + CFX_Int32Array cValues; + cValues.SetSize(3); + do { + if (bits->Available() == 8) { + return; + } + int32_t firstByte = bits->ReadBits(8, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + if (firstByte == 254) { + return; + } + int32_t iTemp1 = bits->ReadBits(8, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + ParseTwoBytes(firstByte, iTemp1, cValues); + int32_t i; + for (i = 0; i < 3; i++) { + int32_t cValue = cValues[i]; + if (cValue == 0) { + BC_FX_ByteString_Append(result, 1, '\r'); + } else if (cValue == 1) { + BC_FX_ByteString_Append(result, 1, '*'); + } else if (cValue == 2) { + BC_FX_ByteString_Append(result, 1, '>'); + } else if (cValue == 3) { + BC_FX_ByteString_Append(result, 1, ' '); + } else if (cValue < 14) { + BC_FX_ByteString_Append(result, 1, (FX_CHAR)(cValue + 44)); + } else if (cValue < 40) { + BC_FX_ByteString_Append(result, 1, (FX_CHAR)(cValue + 51)); + } else { + e = BCExceptionFormatException; + return; + } + } + } while (bits->Available() > 0); +} +void CBC_DataMatrixDecodedBitStreamParser::ParseTwoBytes( + int32_t firstByte, + int32_t secondByte, + CFX_Int32Array& result) { + int32_t fullBitValue = (firstByte << 8) + secondByte - 1; + int32_t temp = fullBitValue / 1600; + result[0] = temp; + fullBitValue -= temp * 1600; + temp = fullBitValue / 40; + result[1] = temp; + result[2] = fullBitValue - temp * 40; +} + +void CBC_DataMatrixDecodedBitStreamParser::DecodeEdifactSegment( + CBC_CommonBitSource* bits, + CFX_ByteString& result, + int32_t& e) { + FX_CHAR buffer[128]; + FX_BOOL unlatch = FALSE; + do { + if (bits->Available() <= 16) + return; + + for (int32_t i = 0; i < 4; i++) { + int32_t edifactValue = bits->ReadBits(6, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + if (edifactValue == 0x1F) + unlatch = TRUE; + + if (!unlatch) { + if ((edifactValue & 32) == 0) + edifactValue |= 64; + result += FXSYS_itoa(edifactValue, buffer, 10); + } + } + } while (!unlatch && bits->Available() > 0); +} + +void CBC_DataMatrixDecodedBitStreamParser::DecodeBase256Segment( + CBC_CommonBitSource* bits, + CFX_ByteString& result, + CFX_Int32Array& byteSegments, + int32_t& e) { + int32_t codewordPosition = 1 + bits->getByteOffset(); + int32_t iTmp = bits->ReadBits(8, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + int32_t d1 = Unrandomize255State(iTmp, codewordPosition++); + int32_t count; + if (d1 == 0) { + count = bits->Available() / 8; + } else if (d1 < 250) { + count = d1; + } else { + int32_t iTmp3 = bits->ReadBits(8, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + count = 250 * (d1 - 249) + Unrandomize255State(iTmp3, codewordPosition++); + } + if (count < 0) { + e = BCExceptionFormatException; + return; + } + CFX_ByteArray* bytes = new CFX_ByteArray(); + bytes->SetSize(count); + int32_t i; + for (i = 0; i < count; i++) { + if (bits->Available() < 8) { + e = BCExceptionFormatException; + delete bytes; + return; + } + int32_t iTemp5 = bits->ReadBits(8, e); + if (e != BCExceptionNO) { + delete bytes; + return; + } + bytes->SetAt(i, Unrandomize255State(iTemp5, codewordPosition++)); + } + BC_FX_ByteString_Append(result, *bytes); + delete bytes; +} +uint8_t CBC_DataMatrixDecodedBitStreamParser::Unrandomize255State( + int32_t randomizedBase256Codeword, + int32_t base256CodewordPosition) { + int32_t pseudoRandomNumber = ((149 * base256CodewordPosition) % 255) + 1; + int32_t tempVariable = randomizedBase256Codeword - pseudoRandomNumber; + return (uint8_t)(tempVariable >= 0 ? tempVariable : tempVariable + 256); +} diff --git a/xfa/fxbarcode/datamatrix/BC_DataMatrixDecodedBitStreamParser.h b/xfa/fxbarcode/datamatrix/BC_DataMatrixDecodedBitStreamParser.h new file mode 100644 index 0000000000..415e27b166 --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_DataMatrixDecodedBitStreamParser.h @@ -0,0 +1,58 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_DATAMATRIX_BC_DATAMATRIXDECODEDBITSTREAMPARSER_H_ +#define XFA_FXBARCODE_DATAMATRIX_BC_DATAMATRIXDECODEDBITSTREAMPARSER_H_ +class CBC_CommonDecoderResult; +class CBC_CommonBitSource; +class CBC_DataMatrixDecodedBitStreamParser { + public: + CBC_DataMatrixDecodedBitStreamParser(); + virtual ~CBC_DataMatrixDecodedBitStreamParser(); + static CBC_CommonDecoderResult* Decode(CFX_ByteArray& bytes, int32_t& e); + + private: + static int32_t DecodeAsciiSegment(CBC_CommonBitSource* bits, + CFX_ByteString& result, + CFX_ByteString& resultTrailer, + int32_t& e); + static void DecodeC40Segment(CBC_CommonBitSource* bits, + CFX_ByteString& result, + int32_t& e); + static void DecodeTextSegment(CBC_CommonBitSource* bits, + CFX_ByteString& result, + int32_t& e); + static void DecodeAnsiX12Segment(CBC_CommonBitSource* bits, + CFX_ByteString& result, + int32_t& e); + static void ParseTwoBytes(int32_t firstByte, + int32_t secondByte, + CFX_Int32Array& result); + static void DecodeEdifactSegment(CBC_CommonBitSource* bits, + CFX_ByteString& result, + int32_t& e); + static void DecodeBase256Segment(CBC_CommonBitSource* bits, + CFX_ByteString& result, + CFX_Int32Array& byteSegments, + int32_t& e); + static uint8_t Unrandomize255State(int32_t randomizedBase256Codeword, + int32_t base256CodewordPosition); + + static const FX_CHAR C40_BASIC_SET_CHARS[]; + static const FX_CHAR C40_SHIFT2_SET_CHARS[]; + + static const FX_CHAR TEXT_BASIC_SET_CHARS[]; + static const FX_CHAR TEXT_SHIFT3_SET_CHARS[]; + static const int32_t PAD_ENCODE; + static const int32_t ASCII_ENCODE; + static const int32_t C40_ENCODE; + static const int32_t TEXT_ENCODE; + static const int32_t ANSIX12_ENCODE; + static const int32_t EDIFACT_ENCODE; + static const int32_t BASE256_ENCODE; +}; + +#endif // XFA_FXBARCODE_DATAMATRIX_BC_DATAMATRIXDECODEDBITSTREAMPARSER_H_ diff --git a/xfa/fxbarcode/datamatrix/BC_DataMatrixDecoder.cpp b/xfa/fxbarcode/datamatrix/BC_DataMatrixDecoder.cpp new file mode 100644 index 0000000000..3f765aa8f9 --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_DataMatrixDecoder.cpp @@ -0,0 +1,114 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2007 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/datamatrix/BC_DataMatrixDecoder.h" + +#include + +#include "xfa/fxbarcode/common/BC_CommonBitMatrix.h" +#include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonDecoder.h" +#include "xfa/fxbarcode/common/reedsolomon/BC_ReedSolomonGF256.h" +#include "xfa/fxbarcode/datamatrix/BC_DataMatrixBitMatrixParser.h" +#include "xfa/fxbarcode/datamatrix/BC_DataMatrixDataBlock.h" +#include "xfa/fxbarcode/datamatrix/BC_DataMatrixDecodedBitStreamParser.h" +#include "xfa/fxbarcode/datamatrix/BC_DataMatrixVersion.h" + +CBC_DataMatrixDecoder::CBC_DataMatrixDecoder() { + m_rsDecoder = NULL; +} +void CBC_DataMatrixDecoder::Init() { + m_rsDecoder = + new CBC_ReedSolomonDecoder(CBC_ReedSolomonGF256::DataMatrixField); +} +CBC_DataMatrixDecoder::~CBC_DataMatrixDecoder() { + delete m_rsDecoder; +} +CBC_CommonDecoderResult* CBC_DataMatrixDecoder::Decode( + CBC_CommonBitMatrix* bits, + int32_t& e) { + CBC_DataMatrixBitMatrixParser parser; + parser.Init(bits, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_DataMatrixVersion* version = parser.GetVersion(); + std::unique_ptr codewords(parser.ReadCodewords(e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CFX_PtrArray* dataBlocks = + CBC_DataMatrixDataBlock::GetDataBlocks(codewords.get(), version, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + int32_t dataBlocksCount = dataBlocks->GetSize(); + int32_t totalBytes = 0; + int32_t i, j; + for (i = 0; i < dataBlocksCount; i++) { + totalBytes += + ((CBC_DataMatrixDataBlock*)(*dataBlocks)[i])->GetNumDataCodewords(); + } + CFX_ByteArray resultBytes; + resultBytes.SetSize(totalBytes); + for (j = 0; j < dataBlocksCount; j++) { + CFX_ByteArray* codewordBytes = + ((CBC_DataMatrixDataBlock*)(*dataBlocks)[j])->GetCodewords(); + int32_t numDataCodewords = + ((CBC_DataMatrixDataBlock*)(*dataBlocks)[j])->GetNumDataCodewords(); + CorrectErrors(*codewordBytes, numDataCodewords, e); + if (e != BCExceptionNO) { + for (int32_t i = 0; i < dataBlocks->GetSize(); i++) { + delete (CBC_DataMatrixDataBlock*)(*dataBlocks)[i]; + } + delete dataBlocks; + dataBlocks = NULL; + return NULL; + } + int32_t i; + for (i = 0; i < numDataCodewords; i++) { + resultBytes[i * dataBlocksCount + j] = (*codewordBytes)[i]; + } + } + for (i = 0; i < (dataBlocks->GetSize()); i++) { + delete (CBC_DataMatrixDataBlock*)(*dataBlocks)[i]; + } + delete dataBlocks; + dataBlocks = NULL; + CBC_CommonDecoderResult* resultR = + CBC_DataMatrixDecodedBitStreamParser::Decode(resultBytes, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return resultR; +} +void CBC_DataMatrixDecoder::CorrectErrors(CFX_ByteArray& codewordBytes, + int32_t numDataCodewords, + int32_t& e) { + int32_t numCodewords = codewordBytes.GetSize(); + CFX_Int32Array codewordsInts; + codewordsInts.SetSize(numCodewords); + int32_t i; + for (i = 0; i < numCodewords; i++) { + codewordsInts[i] = codewordBytes[i] & 0xFF; + } + int32_t numECCodewords = codewordBytes.GetSize() - numDataCodewords; + m_rsDecoder->Decode(&codewordsInts, numECCodewords, e); + if (e != BCExceptionNO) { + e = BCExceptionChecksumException; + return; + } + for (i = 0; i < numDataCodewords; i++) { + codewordBytes[i] = (uint8_t)codewordsInts[i]; + } +} diff --git a/xfa/fxbarcode/datamatrix/BC_DataMatrixDecoder.h b/xfa/fxbarcode/datamatrix/BC_DataMatrixDecoder.h new file mode 100644 index 0000000000..2f70282d01 --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_DataMatrixDecoder.h @@ -0,0 +1,30 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_DATAMATRIX_BC_DATAMATRIXDECODER_H_ +#define XFA_FXBARCODE_DATAMATRIX_BC_DATAMATRIXDECODER_H_ + +#include "core/include/fxcrt/fx_basic.h" + +class CBC_ReedSolomonDecoder; +class CBC_CommonDecoderResult; +class CBC_CommonBitMatrix; + +class CBC_DataMatrixDecoder { + public: + CBC_DataMatrixDecoder(); + virtual ~CBC_DataMatrixDecoder(); + CBC_CommonDecoderResult* Decode(CBC_CommonBitMatrix* bits, int32_t& e); + virtual void Init(); + + private: + void CorrectErrors(CFX_ByteArray& codewordBytes, + int32_t numDataCodewords, + int32_t& e); + CBC_ReedSolomonDecoder* m_rsDecoder; +}; + +#endif // XFA_FXBARCODE_DATAMATRIX_BC_DATAMATRIXDECODER_H_ diff --git a/xfa/fxbarcode/datamatrix/BC_DataMatrixDetector.cpp b/xfa/fxbarcode/datamatrix/BC_DataMatrixDetector.cpp new file mode 100644 index 0000000000..fd410097cc --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_DataMatrixDetector.cpp @@ -0,0 +1,406 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2008 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/datamatrix/BC_DataMatrixDetector.h" + +#include +#include + +#include "xfa/fxbarcode/BC_ResultPoint.h" +#include "xfa/fxbarcode/common/BC_CommonBitMatrix.h" +#include "xfa/fxbarcode/common/BC_WhiteRectangleDetector.h" +#include "xfa/fxbarcode/qrcode/BC_QRDetectorResult.h" +#include "xfa/fxbarcode/qrcode/BC_QRFinderPatternFinder.h" +#include "xfa/fxbarcode/qrcode/BC_QRGridSampler.h" +#include "xfa/fxbarcode/utils.h" + +const int32_t CBC_DataMatrixDetector::INTEGERS[5] = {0, 1, 2, 3, 4}; +CBC_DataMatrixDetector::CBC_DataMatrixDetector(CBC_CommonBitMatrix* image) + : m_image(image), m_rectangleDetector(NULL) {} +void CBC_DataMatrixDetector::Init(int32_t& e) { + m_rectangleDetector = new CBC_WhiteRectangleDetector(m_image); + m_rectangleDetector->Init(e); + BC_EXCEPTION_CHECK_ReturnVoid(e); +} +CBC_DataMatrixDetector::~CBC_DataMatrixDetector() { + delete m_rectangleDetector; +} +inline FX_BOOL ResultPointsAndTransitionsComparator(void* a, void* b) { + return ((CBC_ResultPointsAndTransitions*)b)->GetTransitions() > + ((CBC_ResultPointsAndTransitions*)a)->GetTransitions(); +} +CBC_QRDetectorResult* CBC_DataMatrixDetector::Detect(int32_t& e) { + CFX_PtrArray* cornerPoints = m_rectangleDetector->Detect(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_ResultPoint* pointA = (CBC_ResultPoint*)(*cornerPoints)[0]; + CBC_ResultPoint* pointB = (CBC_ResultPoint*)(*cornerPoints)[1]; + CBC_ResultPoint* pointC = (CBC_ResultPoint*)(*cornerPoints)[2]; + CBC_ResultPoint* pointD = (CBC_ResultPoint*)(*cornerPoints)[3]; + delete cornerPoints; + cornerPoints = NULL; + CFX_PtrArray transitions; + transitions.Add(TransitionsBetween(pointA, pointB)); + transitions.Add(TransitionsBetween(pointA, pointC)); + transitions.Add(TransitionsBetween(pointB, pointD)); + transitions.Add(TransitionsBetween(pointC, pointD)); + BC_FX_PtrArray_Sort(transitions, &ResultPointsAndTransitionsComparator); + delete ((CBC_ResultPointsAndTransitions*)transitions[2]); + delete ((CBC_ResultPointsAndTransitions*)transitions[3]); + CBC_ResultPointsAndTransitions* lSideOne = + (CBC_ResultPointsAndTransitions*)transitions[0]; + CBC_ResultPointsAndTransitions* lSideTwo = + (CBC_ResultPointsAndTransitions*)transitions[1]; + CFX_MapPtrTemplate pointCount; + Increment(pointCount, lSideOne->GetFrom()); + Increment(pointCount, lSideOne->GetTo()); + Increment(pointCount, lSideTwo->GetFrom()); + Increment(pointCount, lSideTwo->GetTo()); + delete ((CBC_ResultPointsAndTransitions*)transitions[1]); + delete ((CBC_ResultPointsAndTransitions*)transitions[0]); + transitions.RemoveAll(); + CBC_ResultPoint* maybeTopLeft = NULL; + CBC_ResultPoint* bottomLeft = NULL; + CBC_ResultPoint* maybeBottomRight = NULL; + FX_POSITION itBegin = pointCount.GetStartPosition(); + while (itBegin) { + CBC_ResultPoint* key = 0; + int32_t value = 0; + pointCount.GetNextAssoc(itBegin, key, value); + if (value == 2) { + bottomLeft = key; + } else { + if (maybeBottomRight == NULL) { + maybeBottomRight = key; + } else { + maybeTopLeft = key; + } + } + } + if (maybeTopLeft == NULL || bottomLeft == NULL || maybeBottomRight == NULL) { + delete pointA; + delete pointB; + delete pointC; + delete pointD; + e = BCExceptionNotFound; + return NULL; + } + CFX_PtrArray corners; + corners.SetSize(3); + corners[0] = maybeTopLeft; + corners[1] = bottomLeft; + corners[2] = maybeBottomRight; + OrderBestPatterns(&corners); + CBC_ResultPoint* bottomRight = (CBC_ResultPoint*)corners[0]; + bottomLeft = (CBC_ResultPoint*)corners[1]; + CBC_ResultPoint* topLeft = (CBC_ResultPoint*)corners[2]; + CBC_ResultPoint* topRight = NULL; + int32_t value; + if (!pointCount.Lookup(pointA, value)) { + topRight = pointA; + } else if (!pointCount.Lookup(pointB, value)) { + topRight = pointB; + } else if (!pointCount.Lookup(pointC, value)) { + topRight = pointC; + } else { + topRight = pointD; + } + int32_t dimensionTop = std::unique_ptr( + TransitionsBetween(topLeft, topRight)) + ->GetTransitions(); + int32_t dimensionRight = std::unique_ptr( + TransitionsBetween(bottomRight, topRight)) + ->GetTransitions(); + if ((dimensionTop & 0x01) == 1) { + dimensionTop++; + } + dimensionTop += 2; + if ((dimensionRight & 0x01) == 1) { + dimensionRight++; + } + dimensionRight += 2; + std::unique_ptr bits; + std::unique_ptr correctedTopRight; + if (4 * dimensionTop >= 7 * dimensionRight || + 4 * dimensionRight >= 7 * dimensionTop) { + correctedTopRight.reset( + CorrectTopRightRectangular(bottomLeft, bottomRight, topLeft, topRight, + dimensionTop, dimensionRight)); + if (correctedTopRight.get() == NULL) { + correctedTopRight.reset(topRight); + } else { + delete topRight; + topRight = NULL; + } + dimensionTop = std::unique_ptr( + TransitionsBetween(topLeft, correctedTopRight.get())) + ->GetTransitions(); + dimensionRight = + std::unique_ptr( + TransitionsBetween(bottomRight, correctedTopRight.get())) + ->GetTransitions(); + if ((dimensionTop & 0x01) == 1) { + dimensionTop++; + } + if ((dimensionRight & 0x01) == 1) { + dimensionRight++; + } + bits.reset(SampleGrid(m_image, topLeft, bottomLeft, bottomRight, + correctedTopRight.get(), dimensionTop, dimensionRight, + e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } else { + int32_t dimension = std::min(dimensionRight, dimensionTop); + correctedTopRight.reset( + CorrectTopRight(bottomLeft, bottomRight, topLeft, topRight, dimension)); + if (correctedTopRight.get() == NULL) { + correctedTopRight.reset(topRight); + } else { + delete topRight; + topRight = NULL; + } + int32_t dimensionCorrected = + std::max(std::unique_ptr( + TransitionsBetween(topLeft, correctedTopRight.get())) + ->GetTransitions(), + std::unique_ptr( + TransitionsBetween(bottomRight, correctedTopRight.get())) + ->GetTransitions()); + dimensionCorrected++; + if ((dimensionCorrected & 0x01) == 1) { + dimensionCorrected++; + } + bits.reset(SampleGrid(m_image, topLeft, bottomLeft, bottomRight, + correctedTopRight.get(), dimensionCorrected, + dimensionCorrected, e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + CFX_PtrArray* result = new CFX_PtrArray; + result->SetSize(4); + result->Add(topLeft); + result->Add(bottomLeft); + result->Add(bottomRight); + result->Add(correctedTopRight.release()); + return new CBC_QRDetectorResult(bits.release(), result); +} +CBC_ResultPoint* CBC_DataMatrixDetector::CorrectTopRightRectangular( + CBC_ResultPoint* bottomLeft, + CBC_ResultPoint* bottomRight, + CBC_ResultPoint* topLeft, + CBC_ResultPoint* topRight, + int32_t dimensionTop, + int32_t dimensionRight) { + FX_FLOAT corr = Distance(bottomLeft, bottomRight) / (FX_FLOAT)dimensionTop; + int32_t norm = Distance(topLeft, topRight); + FX_FLOAT cos = (topRight->GetX() - topLeft->GetX()) / norm; + FX_FLOAT sin = (topRight->GetY() - topLeft->GetY()) / norm; + std::unique_ptr c1(new CBC_ResultPoint( + topRight->GetX() + corr * cos, topRight->GetY() + corr * sin)); + corr = Distance(bottomLeft, topLeft) / (FX_FLOAT)dimensionRight; + norm = Distance(bottomRight, topRight); + cos = (topRight->GetX() - bottomRight->GetX()) / norm; + sin = (topRight->GetY() - bottomRight->GetY()) / norm; + std::unique_ptr c2(new CBC_ResultPoint( + topRight->GetX() + corr * cos, topRight->GetY() + corr * sin)); + if (!IsValid(c1.get())) { + if (IsValid(c2.get())) { + return c2.release(); + } + return NULL; + } else if (!IsValid(c2.get())) { + return c1.release(); + } + int32_t l1 = FXSYS_abs(dimensionTop - + std::unique_ptr( + TransitionsBetween(topLeft, c1.get())) + ->GetTransitions()) + + FXSYS_abs(dimensionRight - + std::unique_ptr( + TransitionsBetween(bottomRight, c1.get())) + ->GetTransitions()); + int32_t l2 = FXSYS_abs(dimensionTop - + std::unique_ptr( + TransitionsBetween(topLeft, c2.get())) + ->GetTransitions()) + + FXSYS_abs(dimensionRight - + std::unique_ptr( + TransitionsBetween(bottomRight, c2.get())) + ->GetTransitions()); + if (l1 <= l2) { + return c1.release(); + } + return c2.release(); +} +CBC_ResultPoint* CBC_DataMatrixDetector::CorrectTopRight( + CBC_ResultPoint* bottomLeft, + CBC_ResultPoint* bottomRight, + CBC_ResultPoint* topLeft, + CBC_ResultPoint* topRight, + int32_t dimension) { + FX_FLOAT corr = Distance(bottomLeft, bottomRight) / (FX_FLOAT)dimension; + int32_t norm = Distance(topLeft, topRight); + FX_FLOAT cos = (topRight->GetX() - topLeft->GetX()) / norm; + FX_FLOAT sin = (topRight->GetY() - topLeft->GetY()) / norm; + std::unique_ptr c1(new CBC_ResultPoint( + topRight->GetX() + corr * cos, topRight->GetY() + corr * sin)); + corr = Distance(bottomLeft, bottomRight) / (FX_FLOAT)dimension; + norm = Distance(bottomRight, topRight); + cos = (topRight->GetX() - bottomRight->GetX()) / norm; + sin = (topRight->GetY() - bottomRight->GetY()) / norm; + std::unique_ptr c2(new CBC_ResultPoint( + topRight->GetX() + corr * cos, topRight->GetY() + corr * sin)); + if (!IsValid(c1.get())) { + if (IsValid(c2.get())) { + return c2.release(); + } + return NULL; + } else if (!IsValid(c2.get())) { + return c1.release(); + } + int32_t l1 = FXSYS_abs(std::unique_ptr( + TransitionsBetween(topLeft, c1.get())) + ->GetTransitions() - + std::unique_ptr( + TransitionsBetween(bottomRight, c1.get())) + ->GetTransitions()); + int32_t l2 = FXSYS_abs(std::unique_ptr( + TransitionsBetween(topLeft, c2.get())) + ->GetTransitions() - + std::unique_ptr( + TransitionsBetween(bottomRight, c2.get())) + ->GetTransitions()); + return l1 <= l2 ? c1.release() : c2.release(); +} +FX_BOOL CBC_DataMatrixDetector::IsValid(CBC_ResultPoint* p) { + return p->GetX() >= 0 && p->GetX() < m_image->GetWidth() && p->GetY() > 0 && + p->GetY() < m_image->GetHeight(); +} +int32_t CBC_DataMatrixDetector::Round(FX_FLOAT d) { + return (int32_t)(d + 0.5f); +} +int32_t CBC_DataMatrixDetector::Distance(CBC_ResultPoint* a, + CBC_ResultPoint* b) { + return Round( + (FX_FLOAT)sqrt((a->GetX() - b->GetX()) * (a->GetX() - b->GetX()) + + (a->GetY() - b->GetY()) * (a->GetY() - b->GetY()))); +} +void CBC_DataMatrixDetector::Increment( + CFX_MapPtrTemplate& table, + CBC_ResultPoint* key) { + int32_t value; + if (table.Lookup(key, value)) { + table.SetAt(key, INTEGERS[value + 1]); + } else { + table.SetAt(key, INTEGERS[1]); + } +} +CBC_CommonBitMatrix* CBC_DataMatrixDetector::SampleGrid( + CBC_CommonBitMatrix* image, + CBC_ResultPoint* topLeft, + CBC_ResultPoint* bottomLeft, + CBC_ResultPoint* bottomRight, + CBC_ResultPoint* topRight, + int32_t dimensionX, + int32_t dimensionY, + int32_t& e) { + CBC_QRGridSampler& sampler = CBC_QRGridSampler::GetInstance(); + CBC_CommonBitMatrix* cbm = sampler.SampleGrid( + image, dimensionX, dimensionY, 0.5f, 0.5f, dimensionX - 0.5f, 0.5f, + dimensionX - 0.5f, dimensionY - 0.5f, 0.5f, dimensionY - 0.5f, + topLeft->GetX(), topLeft->GetY(), topRight->GetX(), topRight->GetY(), + bottomRight->GetX(), bottomRight->GetY(), bottomLeft->GetX(), + bottomLeft->GetY(), e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return cbm; +} +CBC_ResultPointsAndTransitions* CBC_DataMatrixDetector::TransitionsBetween( + CBC_ResultPoint* from, + CBC_ResultPoint* to) { + int32_t fromX = (int32_t)from->GetX(); + int32_t fromY = (int32_t)from->GetY(); + int32_t toX = (int32_t)to->GetX(); + int32_t toY = (int32_t)to->GetY(); + FX_BOOL steep = FXSYS_abs(toY - fromY) > FXSYS_abs(toX - fromX); + if (steep) { + int32_t temp = fromX; + fromX = fromY; + fromY = temp; + temp = toX; + toX = toY; + toY = temp; + } + int32_t dx = FXSYS_abs(toX - fromX); + int32_t dy = FXSYS_abs(toY - fromY); + int32_t error = -dx >> 1; + int32_t ystep = fromY < toY ? 1 : -1; + int32_t xstep = fromX < toX ? 1 : -1; + int32_t transitions = 0; + FX_BOOL inBlack = m_image->Get(steep ? fromY : fromX, steep ? fromX : fromY); + for (int32_t x = fromX, y = fromY; x != toX; x += xstep) { + FX_BOOL isBlack = m_image->Get(steep ? y : x, steep ? x : y); + if (isBlack != inBlack) { + transitions++; + inBlack = isBlack; + } + error += dy; + if (error > 0) { + if (y == toY) { + break; + } + y += ystep; + error -= dx; + } + } + return new CBC_ResultPointsAndTransitions(from, to, transitions); +} +void CBC_DataMatrixDetector::OrderBestPatterns(CFX_PtrArray* patterns) { + FX_FLOAT abDistance = (FX_FLOAT)Distance((CBC_ResultPoint*)(*patterns)[0], + (CBC_ResultPoint*)(*patterns)[1]); + FX_FLOAT bcDistance = (FX_FLOAT)Distance((CBC_ResultPoint*)(*patterns)[1], + (CBC_ResultPoint*)(*patterns)[2]); + FX_FLOAT acDistance = (FX_FLOAT)Distance((CBC_ResultPoint*)(*patterns)[0], + (CBC_ResultPoint*)(*patterns)[2]); + CBC_ResultPoint *topLeft, *topRight, *bottomLeft; + if (bcDistance >= abDistance && bcDistance >= acDistance) { + topLeft = (CBC_ResultPoint*)(*patterns)[0]; + topRight = (CBC_ResultPoint*)(*patterns)[1]; + bottomLeft = (CBC_ResultPoint*)(*patterns)[2]; + } else if (acDistance >= bcDistance && acDistance >= abDistance) { + topLeft = (CBC_ResultPoint*)(*patterns)[1]; + topRight = (CBC_ResultPoint*)(*patterns)[0]; + bottomLeft = (CBC_ResultPoint*)(*patterns)[2]; + } else { + topLeft = (CBC_ResultPoint*)(*patterns)[2]; + topRight = (CBC_ResultPoint*)(*patterns)[0]; + bottomLeft = (CBC_ResultPoint*)(*patterns)[1]; + } + if ((bottomLeft->GetY() - topLeft->GetY()) * + (topRight->GetX() - topLeft->GetX()) < + (bottomLeft->GetX() - topLeft->GetX()) * + (topRight->GetY() - topLeft->GetY())) { + CBC_ResultPoint* temp = topRight; + topRight = bottomLeft; + bottomLeft = temp; + } + (*patterns)[0] = bottomLeft; + (*patterns)[1] = topLeft; + (*patterns)[2] = topRight; +} diff --git a/xfa/fxbarcode/datamatrix/BC_DataMatrixDetector.h b/xfa/fxbarcode/datamatrix/BC_DataMatrixDetector.h new file mode 100644 index 0000000000..62d1699c0a --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_DataMatrixDetector.h @@ -0,0 +1,78 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_DATAMATRIX_BC_DATAMATRIXDETECTOR_H_ +#define XFA_FXBARCODE_DATAMATRIX_BC_DATAMATRIXDETECTOR_H_ + +#include "core/include/fxcrt/fx_basic.h" + +class CBC_CommonBitMatrix; +class CBC_WhiteRectangleDetector; +class CBC_ResultPoint; +class CBC_QRDetectorResult; +class CBC_DataMatrixDetector; +class ResultPointsAndTransitions; +class CBC_ResultPointsAndTransitions { + public: + CBC_ResultPointsAndTransitions(CBC_ResultPoint* from, + CBC_ResultPoint* to, + int32_t transitions) { + m_from = from; + m_to = to; + m_transitions = transitions; + } + ~CBC_ResultPointsAndTransitions() {} + CBC_ResultPoint* GetFrom() { return m_from; } + CBC_ResultPoint* GetTo() { return m_to; } + int32_t GetTransitions() { return m_transitions; } + + private: + CBC_ResultPoint* m_from; + CBC_ResultPoint* m_to; + int32_t m_transitions; +}; +class CBC_DataMatrixDetector { + public: + CBC_DataMatrixDetector(CBC_CommonBitMatrix* image); + virtual ~CBC_DataMatrixDetector(); + CBC_QRDetectorResult* Detect(int32_t& e); + CBC_ResultPoint* CorrectTopRightRectangular(CBC_ResultPoint* bottomLeft, + CBC_ResultPoint* bottomRight, + CBC_ResultPoint* topLeft, + CBC_ResultPoint* topRight, + int32_t dimensionTop, + int32_t dimensionRight); + CBC_ResultPoint* CorrectTopRight(CBC_ResultPoint* bottomLeft, + CBC_ResultPoint* bottomRight, + CBC_ResultPoint* topLeft, + CBC_ResultPoint* topRight, + int32_t dimension); + CBC_CommonBitMatrix* SampleGrid(CBC_CommonBitMatrix* image, + CBC_ResultPoint* topLeft, + CBC_ResultPoint* bottomLeft, + CBC_ResultPoint* bottomRight, + CBC_ResultPoint* topRight, + int32_t dimensionX, + int32_t dimensionY, + int32_t& e); + CBC_ResultPointsAndTransitions* TransitionsBetween(CBC_ResultPoint* from, + CBC_ResultPoint* to); + FX_BOOL IsValid(CBC_ResultPoint* p); + int32_t Distance(CBC_ResultPoint* a, CBC_ResultPoint* b); + void Increment(CFX_MapPtrTemplate& table, + CBC_ResultPoint* key); + int32_t Round(FX_FLOAT d); + void OrderBestPatterns(CFX_PtrArray* patterns); + virtual void Init(int32_t& e); + + private: + static const int32_t INTEGERS[5]; + + CBC_CommonBitMatrix* m_image; + CBC_WhiteRectangleDetector* m_rectangleDetector; +}; + +#endif // XFA_FXBARCODE_DATAMATRIX_BC_DATAMATRIXDETECTOR_H_ diff --git a/xfa/fxbarcode/datamatrix/BC_DataMatrixReader.cpp b/xfa/fxbarcode/datamatrix/BC_DataMatrixReader.cpp new file mode 100644 index 0000000000..9a22d4db3a --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_DataMatrixReader.cpp @@ -0,0 +1,65 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2007 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/datamatrix/BC_DataMatrixReader.h" + +#include + +#include "xfa/fxbarcode/BC_BinaryBitmap.h" +#include "xfa/fxbarcode/BC_Reader.h" +#include "xfa/fxbarcode/common/BC_CommonDecoderResult.h" +#include "xfa/fxbarcode/datamatrix/BC_DataMatrixDecoder.h" +#include "xfa/fxbarcode/datamatrix/BC_DataMatrixDetector.h" +#include "xfa/fxbarcode/qrcode/BC_QRDetectorResult.h" +#include "xfa/fxbarcode/utils.h" + +CBC_DataMatrixReader::CBC_DataMatrixReader() { + m_decoder = NULL; +} +void CBC_DataMatrixReader::Init() { + m_decoder = new CBC_DataMatrixDecoder; + m_decoder->Init(); +} +CBC_DataMatrixReader::~CBC_DataMatrixReader() { + delete m_decoder; +} +CFX_ByteString CBC_DataMatrixReader::Decode(CBC_BinaryBitmap* image, + int32_t hints, + int32_t& e) { + CBC_CommonBitMatrix* cdr = image->GetBlackMatrix(e); + BC_EXCEPTION_CHECK_ReturnValue(e, ""); + CBC_DataMatrixDetector detector(cdr); + detector.Init(e); + BC_EXCEPTION_CHECK_ReturnValue(e, ""); + std::unique_ptr detectorResult(detector.Detect(e)); + BC_EXCEPTION_CHECK_ReturnValue(e, ""); + std::unique_ptr decodeResult( + m_decoder->Decode(detectorResult->GetBits(), e)); + BC_EXCEPTION_CHECK_ReturnValue(e, ""); + return decodeResult->GetText(); +} +CFX_ByteString CBC_DataMatrixReader::Decode(CBC_BinaryBitmap* image, + int32_t& e) { + CFX_ByteString bs = Decode(image, 0, e); + BC_EXCEPTION_CHECK_ReturnValue(e, ""); + return bs; +} diff --git a/xfa/fxbarcode/datamatrix/BC_DataMatrixReader.h b/xfa/fxbarcode/datamatrix/BC_DataMatrixReader.h new file mode 100644 index 0000000000..21fc54eeb5 --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_DataMatrixReader.h @@ -0,0 +1,28 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_DATAMATRIX_BC_DATAMATRIXREADER_H_ +#define XFA_FXBARCODE_DATAMATRIX_BC_DATAMATRIXREADER_H_ + +#include "xfa/fxbarcode/BC_Reader.h" + +class CBC_BinaryBitmap; +class CBC_DataMatrixDecoder; + +class CBC_DataMatrixReader : public CBC_Reader { + public: + CBC_DataMatrixReader(); + virtual ~CBC_DataMatrixReader(); + CFX_ByteString Decode(CBC_BinaryBitmap* image, int32_t& e); + CFX_ByteString Decode(CBC_BinaryBitmap* image, int hints, int32_t& e); + + virtual void Init(); + + private: + CBC_DataMatrixDecoder* m_decoder; +}; + +#endif // XFA_FXBARCODE_DATAMATRIX_BC_DATAMATRIXREADER_H_ diff --git a/xfa/fxbarcode/datamatrix/BC_DataMatrixSymbolInfo144.cpp b/xfa/fxbarcode/datamatrix/BC_DataMatrixSymbolInfo144.cpp new file mode 100644 index 0000000000..16c253f062 --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_DataMatrixSymbolInfo144.cpp @@ -0,0 +1,40 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2006 Jeremias Maerki + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/datamatrix/BC_DataMatrixSymbolInfo144.h" +#include "xfa/fxbarcode/datamatrix/BC_Encoder.h" +#include "xfa/fxbarcode/datamatrix/BC_SymbolInfo.h" +#include "xfa/fxbarcode/datamatrix/BC_SymbolShapeHint.h" + +CBC_DataMatrixSymbolInfo144::CBC_DataMatrixSymbolInfo144() + : CBC_SymbolInfo(FALSE, 1558, 620, 22, 22, 36) { + m_rsBlockData = -1; + m_rsBlockError = 62; +} +CBC_DataMatrixSymbolInfo144::~CBC_DataMatrixSymbolInfo144() {} +int32_t CBC_DataMatrixSymbolInfo144::getInterleavedBlockCount() { + return 10; +} +int32_t CBC_DataMatrixSymbolInfo144getDataLengthForInterleavedBlock( + int32_t index) { + return (index <= 8) ? 156 : 155; +} diff --git a/xfa/fxbarcode/datamatrix/BC_DataMatrixSymbolInfo144.h b/xfa/fxbarcode/datamatrix/BC_DataMatrixSymbolInfo144.h new file mode 100644 index 0000000000..4d6cdc1291 --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_DataMatrixSymbolInfo144.h @@ -0,0 +1,20 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_DATAMATRIX_BC_DATAMATRIXSYMBOLINFO144_H_ +#define XFA_FXBARCODE_DATAMATRIX_BC_DATAMATRIXSYMBOLINFO144_H_ + +#include "xfa/fxbarcode/datamatrix/BC_SymbolInfo.h" + +class CBC_DataMatrixSymbolInfo144 : public CBC_SymbolInfo { + public: + CBC_DataMatrixSymbolInfo144(); + virtual ~CBC_DataMatrixSymbolInfo144(); + int32_t getInterleavedBlockCount(); + int32_t getDataLengthForInterleavedBlock(int32_t index); +}; + +#endif // XFA_FXBARCODE_DATAMATRIX_BC_DATAMATRIXSYMBOLINFO144_H_ diff --git a/xfa/fxbarcode/datamatrix/BC_DataMatrixVersion.cpp b/xfa/fxbarcode/datamatrix/BC_DataMatrixVersion.cpp new file mode 100644 index 0000000000..50131f3dc2 --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_DataMatrixVersion.cpp @@ -0,0 +1,173 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2007 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "xfa/fxbarcode/datamatrix/BC_DataMatrixVersion.h" +#include "xfa/fxbarcode/utils.h" + +CFX_PtrArray* CBC_DataMatrixVersion::VERSIONS = NULL; + +void CBC_DataMatrixVersion::Initialize() { + VERSIONS = new CFX_PtrArray(); +} +void CBC_DataMatrixVersion::Finalize() { + for (int32_t i = 0; i < VERSIONS->GetSize(); i++) { + delete ((CBC_DataMatrixVersion*)(VERSIONS->GetAt(i))); + } + VERSIONS->RemoveAll(); + delete VERSIONS; +} +CBC_DataMatrixVersion::CBC_DataMatrixVersion(int32_t versionNumber, + int32_t symbolSizeRows, + int32_t symbolSizeColumns, + int32_t dataRegionSizeRows, + int32_t dataRegionSizeColumns, + ECBlocks* ecBlocks) { + m_versionNumber = versionNumber; + m_symbolSizeRows = symbolSizeRows; + m_symbolSizeColumns = symbolSizeColumns; + m_dataRegionSizeRows = dataRegionSizeRows; + m_dataRegionSizeColumns = dataRegionSizeColumns; + m_ecBlocks = ecBlocks; + int32_t total = 0; + int32_t ecCodewords = ecBlocks->GetECCodewords(); + const CFX_PtrArray& ecbArray = ecBlocks->GetECBlocks(); + for (int32_t i = 0; i < ecbArray.GetSize(); i++) { + total += ((ECB*)ecbArray[i])->GetCount() * + (((ECB*)ecbArray[i])->GetDataCodewords() + ecCodewords); + } + m_totalCodewords = total; +} +CBC_DataMatrixVersion::~CBC_DataMatrixVersion() { + delete m_ecBlocks; +} +int32_t CBC_DataMatrixVersion::GetVersionNumber() { + return m_versionNumber; +} +int32_t CBC_DataMatrixVersion::GetSymbolSizeRows() { + return m_symbolSizeRows; +} +int32_t CBC_DataMatrixVersion::GetSymbolSizeColumns() { + return m_symbolSizeColumns; +} +int32_t CBC_DataMatrixVersion::GetDataRegionSizeRows() { + return m_dataRegionSizeRows; +} +int32_t CBC_DataMatrixVersion::GetDataRegionSizeColumns() { + return m_dataRegionSizeColumns; +} +int32_t CBC_DataMatrixVersion::GetTotalCodewords() { + return m_totalCodewords; +} +ECBlocks* CBC_DataMatrixVersion::GetECBlocks() { + return m_ecBlocks; +} +void CBC_DataMatrixVersion::ReleaseAll() { + for (int32_t i = 0; i < VERSIONS->GetSize(); i++) { + delete (CBC_DataMatrixVersion*)VERSIONS->GetAt(i); + } + VERSIONS->RemoveAll(); +} +CBC_DataMatrixVersion* CBC_DataMatrixVersion::GetVersionForDimensions( + int32_t numRows, + int32_t numColumns, + int32_t& e) { + if ((numRows & 0x01) != 0 || (numColumns & 0x01) != 0) { + e = BCExceptionNotFound; + return NULL; + } + if (VERSIONS->GetSize() == 0) { + VERSIONS->Add(new CBC_DataMatrixVersion(1, 10, 10, 8, 8, + new ECBlocks(5, new ECB(1, 3)))); + VERSIONS->Add(new CBC_DataMatrixVersion(2, 12, 12, 10, 10, + new ECBlocks(7, new ECB(1, 5)))); + VERSIONS->Add(new CBC_DataMatrixVersion(3, 14, 14, 12, 12, + new ECBlocks(10, new ECB(1, 8)))); + VERSIONS->Add(new CBC_DataMatrixVersion(4, 16, 16, 14, 14, + new ECBlocks(12, new ECB(1, 12)))); + VERSIONS->Add(new CBC_DataMatrixVersion(5, 18, 18, 16, 16, + new ECBlocks(14, new ECB(1, 18)))); + VERSIONS->Add(new CBC_DataMatrixVersion(6, 20, 20, 18, 18, + new ECBlocks(18, new ECB(1, 22)))); + VERSIONS->Add(new CBC_DataMatrixVersion(7, 22, 22, 20, 20, + new ECBlocks(20, new ECB(1, 30)))); + VERSIONS->Add(new CBC_DataMatrixVersion(8, 24, 24, 22, 22, + new ECBlocks(24, new ECB(1, 36)))); + VERSIONS->Add(new CBC_DataMatrixVersion(9, 26, 26, 24, 24, + new ECBlocks(28, new ECB(1, 44)))); + VERSIONS->Add(new CBC_DataMatrixVersion(10, 32, 32, 14, 14, + new ECBlocks(36, new ECB(1, 62)))); + VERSIONS->Add(new CBC_DataMatrixVersion(11, 36, 36, 16, 16, + new ECBlocks(42, new ECB(1, 86)))); + VERSIONS->Add(new CBC_DataMatrixVersion(12, 40, 40, 18, 18, + new ECBlocks(48, new ECB(1, 114)))); + VERSIONS->Add(new CBC_DataMatrixVersion(13, 44, 44, 20, 20, + new ECBlocks(56, new ECB(1, 144)))); + VERSIONS->Add(new CBC_DataMatrixVersion(14, 48, 48, 22, 22, + new ECBlocks(68, new ECB(1, 174)))); + VERSIONS->Add(new CBC_DataMatrixVersion(15, 52, 52, 24, 24, + new ECBlocks(42, new ECB(2, 102)))); + VERSIONS->Add(new CBC_DataMatrixVersion(16, 64, 64, 14, 14, + new ECBlocks(56, new ECB(2, 140)))); + VERSIONS->Add(new CBC_DataMatrixVersion(17, 72, 72, 16, 16, + new ECBlocks(36, new ECB(4, 92)))); + VERSIONS->Add(new CBC_DataMatrixVersion(18, 80, 80, 18, 18, + new ECBlocks(48, new ECB(4, 114)))); + VERSIONS->Add(new CBC_DataMatrixVersion(19, 88, 88, 20, 20, + new ECBlocks(56, new ECB(4, 144)))); + VERSIONS->Add(new CBC_DataMatrixVersion(20, 96, 96, 22, 22, + new ECBlocks(68, new ECB(4, 174)))); + VERSIONS->Add(new CBC_DataMatrixVersion(21, 104, 104, 24, 24, + new ECBlocks(56, new ECB(6, 136)))); + VERSIONS->Add(new CBC_DataMatrixVersion(22, 120, 120, 18, 18, + new ECBlocks(68, new ECB(6, 175)))); + VERSIONS->Add(new CBC_DataMatrixVersion(23, 132, 132, 20, 20, + new ECBlocks(62, new ECB(8, 163)))); + VERSIONS->Add(new CBC_DataMatrixVersion( + 24, 144, 144, 22, 22, + new ECBlocks(62, new ECB(8, 156), new ECB(2, 155)))); + VERSIONS->Add(new CBC_DataMatrixVersion(25, 8, 18, 6, 16, + new ECBlocks(7, new ECB(1, 5)))); + VERSIONS->Add(new CBC_DataMatrixVersion(26, 8, 32, 6, 14, + new ECBlocks(11, new ECB(1, 10)))); + VERSIONS->Add(new CBC_DataMatrixVersion(27, 12, 26, 10, 24, + new ECBlocks(14, new ECB(1, 16)))); + VERSIONS->Add(new CBC_DataMatrixVersion(28, 12, 36, 10, 16, + new ECBlocks(18, new ECB(1, 22)))); + VERSIONS->Add(new CBC_DataMatrixVersion(29, 16, 36, 14, 16, + new ECBlocks(24, new ECB(1, 32)))); + VERSIONS->Add(new CBC_DataMatrixVersion(30, 16, 48, 14, 22, + new ECBlocks(28, new ECB(1, 49)))); + } + int32_t numVersions = VERSIONS->GetSize(); + for (int32_t i = 0; i < numVersions; ++i) { + if (((CBC_DataMatrixVersion*)((*VERSIONS)[i]))->m_symbolSizeRows == + numRows && + ((CBC_DataMatrixVersion*)((*VERSIONS)[i]))->m_symbolSizeColumns == + numColumns) { + return (CBC_DataMatrixVersion*)(*VERSIONS)[i]; + } + } + e = BCExceptionNotFound; + return NULL; +} diff --git a/xfa/fxbarcode/datamatrix/BC_DataMatrixVersion.h b/xfa/fxbarcode/datamatrix/BC_DataMatrixVersion.h new file mode 100644 index 0000000000..8a79355153 --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_DataMatrixVersion.h @@ -0,0 +1,92 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_DATAMATRIX_BC_DATAMATRIXVERSION_H_ +#define XFA_FXBARCODE_DATAMATRIX_BC_DATAMATRIXVERSION_H_ + +#include "core/include/fxcrt/fx_basic.h" + +class CBC_DataMatrixVersion; + +class ECB { + public: + ECB(int32_t count, int32_t dataCodewords) { + m_count = count; + m_dataCodewords = dataCodewords; + } + + int32_t GetCount() { return m_count; } + + int32_t GetDataCodewords() { return m_dataCodewords; } + + private: + int32_t m_count; + int32_t m_dataCodewords; +}; + +class ECBlocks { + public: + ECBlocks(int32_t ecCodewords, ECB* ecBlocks) { + m_ecCodewords = ecCodewords; + m_ecBlocks.Add(ecBlocks); + } + + ECBlocks(int32_t ecCodewords, ECB* ecBlocks1, ECB* ecBlocks2) { + m_ecCodewords = ecCodewords; + m_ecBlocks.Add(ecBlocks1); + m_ecBlocks.Add(ecBlocks2); + } + ~ECBlocks() { + for (int32_t i = 0; i < m_ecBlocks.GetSize(); i++) { + delete (ECB*)m_ecBlocks[i]; + } + m_ecBlocks.RemoveAll(); + } + + int32_t GetECCodewords() { return m_ecCodewords; } + + const CFX_PtrArray& GetECBlocks() { return m_ecBlocks; } + + private: + int32_t m_ecCodewords; + CFX_PtrArray m_ecBlocks; +}; + +class CBC_DataMatrixVersion { + public: + CBC_DataMatrixVersion(int32_t versionNumber, + int32_t symbolSizeRows, + int32_t symbolSizeColumns, + int32_t dataRegionSizeRows, + int32_t dataRegionSizeColumns, + ECBlocks* ecBlocks); + virtual ~CBC_DataMatrixVersion(); + static void Initialize(); + static void Finalize(); + int32_t GetVersionNumber(); + int32_t GetSymbolSizeRows(); + int32_t GetSymbolSizeColumns(); + int32_t GetDataRegionSizeRows(); + int32_t GetDataRegionSizeColumns(); + int32_t GetTotalCodewords(); + ECBlocks* GetECBlocks(); + static CBC_DataMatrixVersion* GetVersionForDimensions(int32_t numRows, + int32_t numColumns, + int32_t& e); + static void ReleaseAll(); + + private: + int32_t m_versionNumber; + int32_t m_symbolSizeRows; + int32_t m_symbolSizeColumns; + int32_t m_dataRegionSizeRows; + int32_t m_dataRegionSizeColumns; + ECBlocks* m_ecBlocks; + int32_t m_totalCodewords; + static CFX_PtrArray* VERSIONS; +}; + +#endif // XFA_FXBARCODE_DATAMATRIX_BC_DATAMATRIXVERSION_H_ diff --git a/xfa/fxbarcode/datamatrix/BC_DataMatrixWriter.cpp b/xfa/fxbarcode/datamatrix/BC_DataMatrixWriter.cpp new file mode 100644 index 0000000000..03cd5fba50 --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_DataMatrixWriter.cpp @@ -0,0 +1,136 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2008 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/BC_BinaryBitmap.h" +#include "xfa/fxbarcode/BC_Dimension.h" +#include "xfa/fxbarcode/BC_TwoDimWriter.h" +#include "xfa/fxbarcode/BC_UtilCodingConvert.h" +#include "xfa/fxbarcode/BC_Writer.h" +#include "xfa/fxbarcode/common/BC_CommonBitMatrix.h" +#include "xfa/fxbarcode/common/BC_CommonByteMatrix.h" +#include "xfa/fxbarcode/datamatrix/BC_ASCIIEncoder.h" +#include "xfa/fxbarcode/datamatrix/BC_Base256Encoder.h" +#include "xfa/fxbarcode/datamatrix/BC_C40Encoder.h" +#include "xfa/fxbarcode/datamatrix/BC_DataMatrixSymbolInfo144.h" +#include "xfa/fxbarcode/datamatrix/BC_DataMatrixWriter.h" +#include "xfa/fxbarcode/datamatrix/BC_DefaultPlacement.h" +#include "xfa/fxbarcode/datamatrix/BC_EdifactEncoder.h" +#include "xfa/fxbarcode/datamatrix/BC_Encoder.h" +#include "xfa/fxbarcode/datamatrix/BC_EncoderContext.h" +#include "xfa/fxbarcode/datamatrix/BC_ErrorCorrection.h" +#include "xfa/fxbarcode/datamatrix/BC_HighLevelEncoder.h" +#include "xfa/fxbarcode/datamatrix/BC_SymbolInfo.h" +#include "xfa/fxbarcode/datamatrix/BC_SymbolShapeHint.h" +#include "xfa/fxbarcode/datamatrix/BC_TextEncoder.h" +#include "xfa/fxbarcode/datamatrix/BC_X12Encoder.h" + +CBC_DataMatrixWriter::CBC_DataMatrixWriter() {} +CBC_DataMatrixWriter::~CBC_DataMatrixWriter() {} +FX_BOOL CBC_DataMatrixWriter::SetErrorCorrectionLevel(int32_t level) { + m_iCorrectLevel = level; + return TRUE; +} +uint8_t* CBC_DataMatrixWriter::Encode(const CFX_WideString& contents, + int32_t& outWidth, + int32_t& outHeight, + int32_t& e) { + if (outWidth < 0 || outHeight < 0) { + e = BCExceptionHeightAndWidthMustBeAtLeast1; + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + } + CBC_SymbolShapeHint::SymbolShapeHint shape = + CBC_SymbolShapeHint::FORCE_SQUARE; + CBC_Dimension* minSize = NULL; + CBC_Dimension* maxSize = NULL; + CFX_WideString ecLevel; + CFX_WideString encoded = CBC_HighLevelEncoder::encodeHighLevel( + contents, ecLevel, shape, minSize, maxSize, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_SymbolInfo* symbolInfo = CBC_SymbolInfo::lookup( + encoded.GetLength(), shape, minSize, maxSize, TRUE, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CFX_WideString codewords = + CBC_ErrorCorrection::encodeECC200(encoded, symbolInfo, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_DefaultPlacement* placement = + new CBC_DefaultPlacement(codewords, symbolInfo->getSymbolDataWidth(e), + symbolInfo->getSymbolDataHeight(e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + placement->place(); + CBC_CommonByteMatrix* bytematrix = encodeLowLevel(placement, symbolInfo, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + outWidth = bytematrix->GetWidth(); + outHeight = bytematrix->GetHeight(); + uint8_t* result = FX_Alloc2D(uint8_t, outWidth, outHeight); + FXSYS_memcpy(result, bytematrix->GetArray(), outWidth * outHeight); + delete bytematrix; + delete placement; + return result; +} +CBC_CommonByteMatrix* CBC_DataMatrixWriter::encodeLowLevel( + CBC_DefaultPlacement* placement, + CBC_SymbolInfo* symbolInfo, + int32_t& e) { + int32_t symbolWidth = symbolInfo->getSymbolDataWidth(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + int32_t symbolHeight = symbolInfo->getSymbolDataHeight(e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + CBC_CommonByteMatrix* matrix = new CBC_CommonByteMatrix( + symbolInfo->getSymbolWidth(e), symbolInfo->getSymbolHeight(e)); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + matrix->Init(); + int32_t matrixY = 0; + for (int32_t y = 0; y < symbolHeight; y++) { + int32_t matrixX; + if ((y % symbolInfo->m_matrixHeight) == 0) { + matrixX = 0; + for (int32_t x = 0; x < symbolInfo->getSymbolWidth(e); x++) { + matrix->Set(matrixX, matrixY, (x % 2) == 0); + matrixX++; + } + matrixY++; + } + matrixX = 0; + for (int32_t x = 0; x < symbolWidth; x++) { + if ((x % symbolInfo->m_matrixWidth) == 0) { + matrix->Set(matrixX, matrixY, TRUE); + matrixX++; + } + matrix->Set(matrixX, matrixY, placement->getBit(x, y)); + matrixX++; + if ((x % symbolInfo->m_matrixWidth) == symbolInfo->m_matrixWidth - 1) { + matrix->Set(matrixX, matrixY, (y % 2) == 0); + matrixX++; + } + } + matrixY++; + if ((y % symbolInfo->m_matrixHeight) == symbolInfo->m_matrixHeight - 1) { + matrixX = 0; + for (int32_t x = 0; x < symbolInfo->getSymbolWidth(e); x++) { + matrix->Set(matrixX, matrixY, TRUE); + matrixX++; + } + matrixY++; + } + } + return matrix; +} diff --git a/xfa/fxbarcode/datamatrix/BC_DataMatrixWriter.h b/xfa/fxbarcode/datamatrix/BC_DataMatrixWriter.h new file mode 100644 index 0000000000..7c19a07c19 --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_DataMatrixWriter.h @@ -0,0 +1,33 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_DATAMATRIX_BC_DATAMATRIXWRITER_H_ +#define XFA_FXBARCODE_DATAMATRIX_BC_DATAMATRIXWRITER_H_ + +#include "xfa/fxbarcode/BC_TwoDimWriter.h" + +class CBC_CommonByteMatrix; +class CBC_DefaultPlacement; +class CBC_SymbolInfo; + +class CBC_DataMatrixWriter : public CBC_TwoDimWriter { + public: + CBC_DataMatrixWriter(); + virtual ~CBC_DataMatrixWriter(); + uint8_t* Encode(const CFX_WideString& contents, + int32_t& outWidth, + int32_t& outHeight, + int32_t& e); + FX_BOOL SetErrorCorrectionLevel(int32_t level); + + private: + static CBC_CommonByteMatrix* encodeLowLevel(CBC_DefaultPlacement* placement, + CBC_SymbolInfo* symbolInfo, + int32_t& e); + int32_t m_iCorrectLevel; +}; + +#endif // XFA_FXBARCODE_DATAMATRIX_BC_DATAMATRIXWRITER_H_ diff --git a/xfa/fxbarcode/datamatrix/BC_DefaultPlacement.cpp b/xfa/fxbarcode/datamatrix/BC_DefaultPlacement.cpp new file mode 100644 index 0000000000..830df224ca --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_DefaultPlacement.cpp @@ -0,0 +1,164 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2006 Jeremias Maerki. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/datamatrix/BC_DefaultPlacement.h" +#include "xfa/fxbarcode/datamatrix/BC_Encoder.h" + +CBC_DefaultPlacement::CBC_DefaultPlacement(CFX_WideString codewords, + int32_t numcols, + int32_t numrows) { + m_codewords = codewords; + m_numcols = numcols; + m_numrows = numrows; + m_bits.SetSize(numcols * numrows); + for (int32_t i = 0; i < numcols * numrows; i++) { + m_bits[i] = (uint8_t)2; + } +} +CBC_DefaultPlacement::~CBC_DefaultPlacement() { + m_bits.RemoveAll(); +} +int32_t CBC_DefaultPlacement::getNumrows() { + return m_numrows; +} +int32_t CBC_DefaultPlacement::getNumcols() { + return m_numcols; +} +CFX_ByteArray& CBC_DefaultPlacement::getBits() { + return m_bits; +} +FX_BOOL CBC_DefaultPlacement::getBit(int32_t col, int32_t row) { + return m_bits[row * m_numcols + col] == 1; +} +void CBC_DefaultPlacement::setBit(int32_t col, int32_t row, FX_BOOL bit) { + m_bits[row * m_numcols + col] = bit ? (uint8_t)1 : (uint8_t)0; +} +FX_BOOL CBC_DefaultPlacement::hasBit(int32_t col, int32_t row) { + return m_bits[row * m_numcols + col] != 2; +} +void CBC_DefaultPlacement::place() { + int32_t pos = 0; + int32_t row = 4; + int32_t col = 0; + do { + if ((row == m_numrows) && (col == 0)) { + corner1(pos++); + } + if ((row == m_numrows - 2) && (col == 0) && ((m_numcols % 4) != 0)) { + corner2(pos++); + } + if ((row == m_numrows - 2) && (col == 0) && (m_numcols % 8 == 4)) { + corner3(pos++); + } + if ((row == m_numrows + 4) && (col == 2) && ((m_numcols % 8) == 0)) { + corner4(pos++); + } + do { + if ((row < m_numrows) && (col >= 0) && !hasBit(col, row)) { + utah(row, col, pos++); + } + row -= 2; + col += 2; + } while (row >= 0 && (col < m_numcols)); + row++; + col += 3; + do { + if ((row >= 0) && (col < m_numcols) && !hasBit(col, row)) { + utah(row, col, pos++); + } + row += 2; + col -= 2; + } while ((row < m_numrows) && (col >= 0)); + row += 3; + col++; + } while ((row < m_numrows) || (col < m_numcols)); + if (!hasBit(m_numcols - 1, m_numrows - 1)) { + setBit(m_numcols - 1, m_numrows - 1, TRUE); + setBit(m_numcols - 2, m_numrows - 2, TRUE); + } +} +void CBC_DefaultPlacement::module(int32_t row, + int32_t col, + int32_t pos, + int32_t bit) { + if (row < 0) { + row += m_numrows; + col += 4 - ((m_numrows + 4) % 8); + } + if (col < 0) { + col += m_numcols; + row += 4 - ((m_numcols + 4) % 8); + } + int32_t v = m_codewords.GetAt(pos); + v &= 1 << (8 - bit); + setBit(col, row, v != 0); +} +void CBC_DefaultPlacement::utah(int32_t row, int32_t col, int32_t pos) { + module(row - 2, col - 2, pos, 1); + module(row - 2, col - 1, pos, 2); + module(row - 1, col - 2, pos, 3); + module(row - 1, col - 1, pos, 4); + module(row - 1, col, pos, 5); + module(row, col - 2, pos, 6); + module(row, col - 1, pos, 7); + module(row, col, pos, 8); +} +void CBC_DefaultPlacement::corner1(int32_t pos) { + module(m_numrows - 1, 0, pos, 1); + module(m_numrows - 1, 1, pos, 2); + module(m_numrows - 1, 2, pos, 3); + module(0, m_numcols - 2, pos, 4); + module(0, m_numcols - 1, pos, 5); + module(1, m_numcols - 1, pos, 6); + module(2, m_numcols - 1, pos, 7); + module(3, m_numcols - 1, pos, 8); +} +void CBC_DefaultPlacement::corner2(int32_t pos) { + module(m_numrows - 3, 0, pos, 1); + module(m_numrows - 2, 0, pos, 2); + module(m_numrows - 1, 0, pos, 3); + module(0, m_numcols - 4, pos, 4); + module(0, m_numcols - 3, pos, 5); + module(0, m_numcols - 2, pos, 6); + module(0, m_numcols - 1, pos, 7); + module(1, m_numcols - 1, pos, 8); +} +void CBC_DefaultPlacement::corner3(int32_t pos) { + module(m_numrows - 3, 0, pos, 1); + module(m_numrows - 2, 0, pos, 2); + module(m_numrows - 1, 0, pos, 3); + module(0, m_numcols - 2, pos, 4); + module(0, m_numcols - 1, pos, 5); + module(1, m_numcols - 1, pos, 6); + module(2, m_numcols - 1, pos, 7); + module(3, m_numcols - 1, pos, 8); +} +void CBC_DefaultPlacement::corner4(int32_t pos) { + module(m_numrows - 1, 0, pos, 1); + module(m_numrows - 1, m_numcols - 1, pos, 2); + module(0, m_numcols - 3, pos, 3); + module(0, m_numcols - 2, pos, 4); + module(0, m_numcols - 1, pos, 5); + module(1, m_numcols - 3, pos, 6); + module(1, m_numcols - 2, pos, 7); + module(1, m_numcols - 1, pos, 8); +} diff --git a/xfa/fxbarcode/datamatrix/BC_DefaultPlacement.h b/xfa/fxbarcode/datamatrix/BC_DefaultPlacement.h new file mode 100644 index 0000000000..49008f3356 --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_DefaultPlacement.h @@ -0,0 +1,40 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_DATAMATRIX_BC_DEFAULTPLACEMENT_H_ +#define XFA_FXBARCODE_DATAMATRIX_BC_DEFAULTPLACEMENT_H_ + +#include "core/include/fxcrt/fx_basic.h" + +class CBC_DefaultPlacement { + public: + CBC_DefaultPlacement(CFX_WideString codewords, + int32_t numcols, + int32_t numrows); + virtual ~CBC_DefaultPlacement(); + + int32_t getNumrows(); + int32_t getNumcols(); + CFX_ByteArray& getBits(); + FX_BOOL getBit(int32_t col, int32_t row); + void setBit(int32_t col, int32_t row, FX_BOOL bit); + FX_BOOL hasBit(int32_t col, int32_t row); + void place(); + + private: + CFX_WideString m_codewords; + int32_t m_numrows; + int32_t m_numcols; + CFX_ByteArray m_bits; + void module(int32_t row, int32_t col, int32_t pos, int32_t bit); + void utah(int32_t row, int32_t col, int32_t pos); + void corner1(int32_t pos); + void corner2(int32_t pos); + void corner3(int32_t pos); + void corner4(int32_t pos); +}; + +#endif // XFA_FXBARCODE_DATAMATRIX_BC_DEFAULTPLACEMENT_H_ diff --git a/xfa/fxbarcode/datamatrix/BC_EdifactEncoder.cpp b/xfa/fxbarcode/datamatrix/BC_EdifactEncoder.cpp new file mode 100644 index 0000000000..d1f5b9796d --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_EdifactEncoder.cpp @@ -0,0 +1,154 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2006-2007 Jeremias Maerki. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/BC_Dimension.h" +#include "xfa/fxbarcode/common/BC_CommonBitMatrix.h" +#include "xfa/fxbarcode/datamatrix/BC_EdifactEncoder.h" +#include "xfa/fxbarcode/datamatrix/BC_Encoder.h" +#include "xfa/fxbarcode/datamatrix/BC_EncoderContext.h" +#include "xfa/fxbarcode/datamatrix/BC_HighLevelEncoder.h" +#include "xfa/fxbarcode/datamatrix/BC_SymbolInfo.h" +#include "xfa/fxbarcode/datamatrix/BC_SymbolShapeHint.h" + +CBC_EdifactEncoder::CBC_EdifactEncoder() {} +CBC_EdifactEncoder::~CBC_EdifactEncoder() {} +int32_t CBC_EdifactEncoder::getEncodingMode() { + return EDIFACT_ENCODATION; +} +void CBC_EdifactEncoder::Encode(CBC_EncoderContext& context, int32_t& e) { + CFX_WideString buffer; + while (context.hasMoreCharacters()) { + FX_WCHAR c = context.getCurrentChar(); + encodeChar(c, buffer, e); + if (e != BCExceptionNO) { + return; + } + context.m_pos++; + int32_t count = buffer.GetLength(); + if (count >= 4) { + context.writeCodewords(encodeToCodewords(buffer, 0, e)); + if (e != BCExceptionNO) { + return; + } + buffer.Delete(0, 4); + int32_t newMode = CBC_HighLevelEncoder::lookAheadTest( + context.m_msg, context.m_pos, getEncodingMode()); + if (newMode != getEncodingMode()) { + context.signalEncoderChange(ASCII_ENCODATION); + break; + } + } + } + buffer += (FX_WCHAR)31; + handleEOD(context, buffer, e); +} +void CBC_EdifactEncoder::handleEOD(CBC_EncoderContext& context, + CFX_WideString buffer, + int32_t& e) { + int32_t count = buffer.GetLength(); + if (count == 0) { + return; + } + if (count == 1) { + context.updateSymbolInfo(e); + if (e != BCExceptionNO) { + return; + } + int32_t available = + context.m_symbolInfo->m_dataCapacity - context.getCodewordCount(); + int32_t remaining = context.getRemainingCharacters(); + if (remaining == 0 && available <= 2) { + return; + } + } + if (count > 4) { + e = BCExceptionIllegalStateCountMustNotExceed4; + return; + } + int32_t restChars = count - 1; + CFX_WideString encoded = encodeToCodewords(buffer, 0, e); + if (e != BCExceptionNO) { + return; + } + FX_BOOL endOfSymbolReached = !context.hasMoreCharacters(); + FX_BOOL restInAscii = endOfSymbolReached && restChars <= 2; + if (restChars <= 2) { + context.updateSymbolInfo(context.getCodewordCount() + restChars, e); + if (e != BCExceptionNO) { + return; + } + int32_t available = + context.m_symbolInfo->m_dataCapacity - context.getCodewordCount(); + if (available >= 3) { + restInAscii = FALSE; + context.updateSymbolInfo(context.getCodewordCount() + encoded.GetLength(), + e); + if (e != BCExceptionNO) { + return; + } + } + } + if (restInAscii) { + context.resetSymbolInfo(); + context.m_pos -= restChars; + } else { + context.writeCodewords(encoded); + } + context.signalEncoderChange(ASCII_ENCODATION); +} +void CBC_EdifactEncoder::encodeChar(FX_WCHAR c, + CFX_WideString& sb, + int32_t& e) { + if (c >= ' ' && c <= '?') { + sb += c; + } else if (c >= '@' && c <= '^') { + sb += (FX_WCHAR)(c - 64); + } else { + CBC_HighLevelEncoder::illegalCharacter(c, e); + } +} +CFX_WideString CBC_EdifactEncoder::encodeToCodewords(CFX_WideString sb, + int32_t startPos, + int32_t& e) { + int32_t len = sb.GetLength() - startPos; + if (len == 0) { + e = BCExceptionNoContents; + return (FX_WCHAR*)""; + } + FX_WCHAR c1 = sb.GetAt(startPos); + FX_WCHAR c2 = len >= 2 ? sb.GetAt(startPos + 1) : 0; + FX_WCHAR c3 = len >= 3 ? sb.GetAt(startPos + 2) : 0; + FX_WCHAR c4 = len >= 4 ? sb.GetAt(startPos + 3) : 0; + int32_t v = (c1 << 18) + (c2 << 12) + (c3 << 6) + c4; + FX_WCHAR cw1 = (FX_WCHAR)((v >> 16) & 255); + FX_WCHAR cw2 = (FX_WCHAR)((v >> 8) & 255); + FX_WCHAR cw3 = (FX_WCHAR)(v & 255); + CFX_WideString res; + res += cw1; + if (len >= 2) { + res += cw2; + } + if (len >= 3) { + res += cw3; + } + return res; +} diff --git a/xfa/fxbarcode/datamatrix/BC_EdifactEncoder.h b/xfa/fxbarcode/datamatrix/BC_EdifactEncoder.h new file mode 100644 index 0000000000..fd37ffb5b4 --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_EdifactEncoder.h @@ -0,0 +1,29 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_DATAMATRIX_BC_EDIFACTENCODER_H_ +#define XFA_FXBARCODE_DATAMATRIX_BC_EDIFACTENCODER_H_ + +#include "xfa/fxbarcode/datamatrix/BC_Encoder.h" + +class CBC_EdifactEncoder : public CBC_Encoder { + public: + CBC_EdifactEncoder(); + virtual ~CBC_EdifactEncoder(); + int32_t getEncodingMode(); + void Encode(CBC_EncoderContext& context, int32_t& e); + + private: + static void handleEOD(CBC_EncoderContext& context, + CFX_WideString buffer, + int32_t& e); + static void encodeChar(FX_WCHAR c, CFX_WideString& sb, int32_t& e); + static CFX_WideString encodeToCodewords(CFX_WideString sb, + int32_t startPos, + int32_t& e); +}; + +#endif // XFA_FXBARCODE_DATAMATRIX_BC_EDIFACTENCODER_H_ diff --git a/xfa/fxbarcode/datamatrix/BC_Encoder.cpp b/xfa/fxbarcode/datamatrix/BC_Encoder.cpp new file mode 100644 index 0000000000..bf815bdfa7 --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_Encoder.cpp @@ -0,0 +1,10 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "xfa/fxbarcode/datamatrix/BC_Encoder.h" + +CBC_Encoder::CBC_Encoder() {} +CBC_Encoder::~CBC_Encoder() {} diff --git a/xfa/fxbarcode/datamatrix/BC_Encoder.h b/xfa/fxbarcode/datamatrix/BC_Encoder.h new file mode 100644 index 0000000000..78dc6ac447 --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_Encoder.h @@ -0,0 +1,21 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_DATAMATRIX_BC_ENCODER_H_ +#define XFA_FXBARCODE_DATAMATRIX_BC_ENCODER_H_ + +#include "xfa/fxbarcode/utils.h" + +class CBC_EncoderContext; +class CBC_Encoder { + public: + CBC_Encoder(); + virtual ~CBC_Encoder(); + virtual int32_t getEncodingMode() = 0; + virtual void Encode(CBC_EncoderContext& context, int32_t& e) = 0; +}; + +#endif // XFA_FXBARCODE_DATAMATRIX_BC_ENCODER_H_ diff --git a/xfa/fxbarcode/datamatrix/BC_EncoderContext.cpp b/xfa/fxbarcode/datamatrix/BC_EncoderContext.cpp new file mode 100644 index 0000000000..c99dcb7d7a --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_EncoderContext.cpp @@ -0,0 +1,111 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2006-2007 Jeremias Maerki. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/BC_Dimension.h" +#include "xfa/fxbarcode/BC_UtilCodingConvert.h" +#include "xfa/fxbarcode/common/BC_CommonBitMatrix.h" +#include "xfa/fxbarcode/datamatrix/BC_Encoder.h" +#include "xfa/fxbarcode/datamatrix/BC_EncoderContext.h" +#include "xfa/fxbarcode/datamatrix/BC_SymbolInfo.h" +#include "xfa/fxbarcode/datamatrix/BC_SymbolShapeHint.h" + +CBC_EncoderContext::CBC_EncoderContext(const CFX_WideString msg, + CFX_WideString ecLevel, + int32_t& e) { + CFX_ByteString dststr; + CBC_UtilCodingConvert::UnicodeToUTF8(msg, dststr); + CFX_WideString sb; + int32_t c = dststr.GetLength(); + for (int32_t i = 0; i < c; i++) { + FX_WCHAR ch = (FX_WCHAR)(dststr.GetAt(i) & 0xff); + if (ch == '?' && dststr.GetAt(i) != '?') { + e = BCExceptionCharactersOutsideISO88591Encoding; + } + sb += ch; + } + m_msg = sb; + m_shape = FORCE_NONE; + m_newEncoding = -1; + m_pos = 0; + m_symbolInfo = NULL; + m_skipAtEnd = 0; + m_maxSize = NULL; + m_minSize = NULL; +} +CBC_EncoderContext::~CBC_EncoderContext() {} +void CBC_EncoderContext::setSymbolShape(SymbolShapeHint shape) { + m_shape = shape; +} +void CBC_EncoderContext::setSizeConstraints(CBC_Dimension* minSize, + CBC_Dimension* maxSize) { + m_maxSize = maxSize; + m_minSize = minSize; +} +CFX_WideString CBC_EncoderContext::getMessage() { + return m_msg; +} +void CBC_EncoderContext::setSkipAtEnd(int32_t count) { + m_skipAtEnd = count; +} +FX_WCHAR CBC_EncoderContext::getCurrentChar() { + return m_msg.GetAt(m_pos); +} +FX_WCHAR CBC_EncoderContext::getCurrent() { + return m_msg.GetAt(m_pos); +} +void CBC_EncoderContext::writeCodewords(CFX_WideString codewords) { + m_codewords += codewords; +} +void CBC_EncoderContext::writeCodeword(FX_WCHAR codeword) { + m_codewords += codeword; +} +int32_t CBC_EncoderContext::getCodewordCount() { + return m_codewords.GetLength(); +} +void CBC_EncoderContext::signalEncoderChange(int32_t encoding) { + m_newEncoding = encoding; +} +void CBC_EncoderContext::resetEncoderSignal() { + m_newEncoding = -1; +} +FX_BOOL CBC_EncoderContext::hasMoreCharacters() { + return m_pos < getTotalMessageCharCount(); +} +int32_t CBC_EncoderContext::getRemainingCharacters() { + return getTotalMessageCharCount() - m_pos; +} +void CBC_EncoderContext::updateSymbolInfo(int32_t& e) { + updateSymbolInfo(getCodewordCount(), e); +} +void CBC_EncoderContext::updateSymbolInfo(int32_t len, int32_t& e) { + if (m_symbolInfo == NULL || len > m_symbolInfo->m_dataCapacity) { + m_symbolInfo = + CBC_SymbolInfo::lookup(len, m_shape, m_minSize, m_maxSize, true, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + } +} +void CBC_EncoderContext::resetSymbolInfo() { + m_shape = FORCE_NONE; +} +int32_t CBC_EncoderContext::getTotalMessageCharCount() { + return m_msg.GetLength() - m_skipAtEnd; +} diff --git a/xfa/fxbarcode/datamatrix/BC_EncoderContext.h b/xfa/fxbarcode/datamatrix/BC_EncoderContext.h new file mode 100644 index 0000000000..ddc6988dc3 --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_EncoderContext.h @@ -0,0 +1,55 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_DATAMATRIX_BC_ENCODERCONTEXT_H_ +#define XFA_FXBARCODE_DATAMATRIX_BC_ENCODERCONTEXT_H_ + +#include "xfa/fxbarcode/datamatrix/BC_SymbolShapeHint.h" + +class CBC_SymbolInfo; +class CBC_Dimension; + +class CBC_EncoderContext : public CBC_SymbolShapeHint { + public: + CBC_EncoderContext(const CFX_WideString msg, + CFX_WideString ecLevel, + int32_t& e); + virtual ~CBC_EncoderContext(); + void setSymbolShape(SymbolShapeHint shape); + void setSizeConstraints(CBC_Dimension* minSize, CBC_Dimension* maxSize); + CFX_WideString getMessage(); + void setSkipAtEnd(int32_t count); + FX_WCHAR getCurrentChar(); + FX_WCHAR getCurrent(); + void writeCodewords(CFX_WideString codewords); + void writeCodeword(FX_WCHAR codeword); + int32_t getCodewordCount(); + void signalEncoderChange(int32_t encoding); + void resetEncoderSignal(); + FX_BOOL hasMoreCharacters(); + int32_t getRemainingCharacters(); + void updateSymbolInfo(int32_t& e); + void updateSymbolInfo(int32_t len, int32_t& e); + void resetSymbolInfo(); + + public: + CFX_WideString m_msg; + CFX_WideString m_codewords; + int32_t m_pos; + int32_t m_newEncoding; + CBC_SymbolInfo* m_symbolInfo; + + private: + int32_t getTotalMessageCharCount(); + + private: + SymbolShapeHint m_shape; + CBC_Dimension* m_minSize; + CBC_Dimension* m_maxSize; + int32_t m_skipAtEnd; +}; + +#endif // XFA_FXBARCODE_DATAMATRIX_BC_ENCODERCONTEXT_H_ diff --git a/xfa/fxbarcode/datamatrix/BC_ErrorCorrection.cpp b/xfa/fxbarcode/datamatrix/BC_ErrorCorrection.cpp new file mode 100644 index 0000000000..98c0599e50 --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_ErrorCorrection.cpp @@ -0,0 +1,175 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2006 Jeremias Maerki. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/datamatrix/BC_Encoder.h" +#include "xfa/fxbarcode/datamatrix/BC_ErrorCorrection.h" +#include "xfa/fxbarcode/datamatrix/BC_SymbolInfo.h" +#include "xfa/fxbarcode/datamatrix/BC_SymbolShapeHint.h" + +int32_t CBC_ErrorCorrection::FACTOR_SETS[] = {5, 7, 10, 11, 12, 14, 18, 20, + 24, 28, 36, 42, 48, 56, 62, 68}; +int32_t CBC_ErrorCorrection::FACTORS[][100] = { + {228, 48, 15, 111, 62}, + {23, 68, 144, 134, 240, 92, 254}, + {28, 24, 185, 166, 223, 248, 116, 255, 110, 61}, + {175, 138, 205, 12, 194, 168, 39, 245, 60, 97, 120}, + {41, 153, 158, 91, 61, 42, 142, 213, 97, 178, 100, 242}, + {156, 97, 192, 252, 95, 9, 157, 119, 138, 45, 18, 186, 83, 185}, + {83, 195, 100, 39, 188, 75, 66, 61, 241, 213, 109, 129, 94, 254, 225, 48, + 90, 188}, + {15, 195, 244, 9, 233, 71, 168, 2, 188, 160, 153, 145, 253, 79, 108, 82, 27, + 174, 186, 172}, + {52, 190, 88, 205, 109, 39, 176, 21, 155, 197, 251, 223, 155, 21, 5, 172, + 254, 124, 12, 181, 184, 96, 50, 193}, + {211, 231, 43, 97, 71, 96, 103, 174, 37, 151, 170, 53, 75, 34, 249, 121, 17, + 138, 110, 213, 141, 136, 120, 151, 233, 168, 93, 255}, + {245, 127, 242, 218, 130, 250, 162, 181, 102, 120, 84, 179, 220, 251, 80, + 182, 229, 18, 2, 4, 68, 33, 101, 137, 95, 119, 115, 44, 175, 184, 59, 25, + 225, 98, 81, 112}, + {77, 193, 137, 31, 19, 38, 22, 153, 247, 105, 122, 2, 245, 133, 242, 8, 175, + 95, 100, 9, 167, 105, 214, 111, 57, 121, 21, 1, 253, 57, 54, 101, 248, 202, + 69, 50, 150, 177, 226, 5, 9, 5}, + {245, 132, 172, 223, 96, 32, 117, 22, 238, 133, 238, 231, 205, 188, 237, 87, + 191, 106, 16, 147, 118, 23, 37, 90, 170, 205, 131, 88, 120, 100, 66, 138, + 186, 240, 82, 44, 176, 87, 187, 147, 160, 175, 69, 213, 92, 253, 225, 19}, + {175, 9, 223, 238, 12, 17, 220, 208, 100, 29, 175, 170, 230, 192, 215, 235, + 150, 159, 36, 223, 38, 200, 132, 54, 228, 146, 218, 234, 117, 203, 29, 232, + 144, 238, 22, 150, 201, 117, 62, 207, 164, 13, 137, 245, 127, 67, 247, 28, + 155, 43, 203, 107, 233, 53, 143, 46}, + {242, 93, 169, 50, 144, 210, 39, 118, 202, 188, 201, 189, 143, 108, 196, 37, + 185, 112, 134, 230, 245, 63, 197, 190, 250, 106, 185, 221, 175, 64, 114, + 71, 161, 44, 147, 6, 27, 218, 51, 63, 87, 10, 40, 130, 188, 17, 163, 31, + 176, 170, 4, 107, 232, 7, 94, 166, 224, 124, 86, 47, 11, 204}, + {220, 228, 173, 89, 251, 149, 159, 56, 89, 33, 147, 244, 154, 36, 73, 127, + 213, 136, 248, 180, 234, 197, 158, 177, 68, 122, 93, 213, 15, 160, 227, + 236, 66, 139, 153, 185, 202, 167, 179, 25, 220, 232, 96, 210, 231, 136, + 223, 239, 181, 241, 59, 52, 172, 25, 49, 232, 211, 189, 64, 54, 108, 153, + 132, 63, 96, 103, 82, 186}}; +int32_t CBC_ErrorCorrection::MODULO_VALUE = 0x12D; +int32_t CBC_ErrorCorrection::LOG[256] = {0}; +int32_t CBC_ErrorCorrection::ALOG[256] = {0}; +void CBC_ErrorCorrection::Initialize() { + int32_t p = 1; + for (int32_t i = 0; i < 255; i++) { + ALOG[i] = p; + LOG[p] = i; + p <<= 1; + if (p >= 256) { + p ^= MODULO_VALUE; + } + } +} +void CBC_ErrorCorrection::Finalize() {} +CBC_ErrorCorrection::CBC_ErrorCorrection() {} +CBC_ErrorCorrection::~CBC_ErrorCorrection() {} +CFX_WideString CBC_ErrorCorrection::encodeECC200(CFX_WideString codewords, + CBC_SymbolInfo* symbolInfo, + int32_t& e) { + if (codewords.GetLength() != symbolInfo->m_dataCapacity) { + e = BCExceptionIllegalArgument; + return (FX_WCHAR*)""; + } + CFX_WideString sb; + sb += codewords; + int32_t blockCount = symbolInfo->getInterleavedBlockCount(); + if (blockCount == 1) { + CFX_WideString ecc = + createECCBlock(codewords, symbolInfo->m_errorCodewords, e); + BC_EXCEPTION_CHECK_ReturnValue(e, (FX_WCHAR*)""); + sb += ecc; + } else { + CFX_Int32Array dataSizes; + dataSizes.SetSize(blockCount); + CFX_Int32Array errorSizes; + errorSizes.SetSize(blockCount); + CFX_Int32Array startPos; + startPos.SetSize(blockCount); + for (int32_t i = 0; i < blockCount; i++) { + dataSizes[i] = symbolInfo->getDataLengthForInterleavedBlock(i + 1); + errorSizes[i] = symbolInfo->getErrorLengthForInterleavedBlock(i + 1); + startPos[i] = 0; + if (i > 0) { + startPos[i] = startPos[i - 1] + dataSizes[i]; + } + } + for (int32_t block = 0; block < blockCount; block++) { + CFX_WideString temp; + for (int32_t d = block; d < symbolInfo->m_dataCapacity; d += blockCount) { + temp += (FX_WCHAR)codewords.GetAt(d); + } + CFX_WideString ecc = createECCBlock(temp, errorSizes[block], e); + BC_EXCEPTION_CHECK_ReturnValue(e, (FX_WCHAR*)""); + int32_t pos = 0; + for (int32_t l = block; l < errorSizes[block] * blockCount; + l += blockCount) { + sb.SetAt(symbolInfo->m_dataCapacity + l, ecc.GetAt(pos++)); + } + } + } + return sb; +} +CFX_WideString CBC_ErrorCorrection::createECCBlock(CFX_WideString codewords, + int32_t numECWords, + int32_t& e) { + return createECCBlock(codewords, 0, codewords.GetLength(), numECWords, e); +} +CFX_WideString CBC_ErrorCorrection::createECCBlock(CFX_WideString codewords, + int32_t start, + int32_t len, + int32_t numECWords, + int32_t& e) { + int32_t table = -1; + for (int32_t i = 0; i < sizeof(FACTOR_SETS) / sizeof(int32_t); i++) { + if (FACTOR_SETS[i] == numECWords) { + table = i; + break; + } + } + if (table < 0) { + e = BCExceptionIllegalArgument; + return (FX_WCHAR*)""; + } + FX_WORD* ecc = FX_Alloc(FX_WORD, numECWords); + FXSYS_memset(ecc, 0, numECWords * sizeof(FX_WORD)); + for (int32_t l = start; l < start + len; l++) { + FX_WORD m = ecc[numECWords - 1] ^ codewords.GetAt(l); + for (int32_t k = numECWords - 1; k > 0; k--) { + if (m != 0 && FACTORS[table][k] != 0) { + ecc[k] = (FX_WORD)(ecc[k - 1] ^ + ALOG[(LOG[m] + LOG[FACTORS[table][k]]) % 255]); + } else { + ecc[k] = ecc[k - 1]; + } + } + if (m != 0 && FACTORS[table][0] != 0) { + ecc[0] = (FX_WORD)ALOG[(LOG[m] + LOG[FACTORS[table][0]]) % 255]; + } else { + ecc[0] = 0; + } + } + CFX_WideString strecc; + for (int32_t j = 0; j < numECWords; j++) { + strecc += (FX_WCHAR)ecc[numECWords - j - 1]; + } + FX_Free(ecc); + return strecc; +} diff --git a/xfa/fxbarcode/datamatrix/BC_ErrorCorrection.h b/xfa/fxbarcode/datamatrix/BC_ErrorCorrection.h new file mode 100644 index 0000000000..a4264b6306 --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_ErrorCorrection.h @@ -0,0 +1,39 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_DATAMATRIX_BC_ERRORCORRECTION_H_ +#define XFA_FXBARCODE_DATAMATRIX_BC_ERRORCORRECTION_H_ + +class CBC_SymbolInfo; +class CBC_ErrorCorrection { + public: + CBC_ErrorCorrection(); + virtual ~CBC_ErrorCorrection(); + static void Initialize(); + static void Finalize(); + static CFX_WideString encodeECC200(CFX_WideString codewords, + CBC_SymbolInfo* symbolInfo, + int32_t& e); + + private: + static int32_t FACTOR_SETS[]; + static int32_t FACTORS[][100]; + static int32_t MODULO_VALUE; + static int32_t LOG[256]; + static int32_t ALOG[256]; + + private: + static CFX_WideString createECCBlock(CFX_WideString codewords, + int32_t numECWords, + int32_t& e); + static CFX_WideString createECCBlock(CFX_WideString codewords, + int32_t start, + int32_t len, + int32_t numECWords, + int32_t& e); +}; + +#endif // XFA_FXBARCODE_DATAMATRIX_BC_ERRORCORRECTION_H_ diff --git a/xfa/fxbarcode/datamatrix/BC_HighLevelEncoder.cpp b/xfa/fxbarcode/datamatrix/BC_HighLevelEncoder.cpp new file mode 100644 index 0000000000..d8e7bb1dc5 --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_HighLevelEncoder.cpp @@ -0,0 +1,375 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2006-2007 Jeremias Maerki. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "xfa/fxbarcode/BC_Dimension.h" +#include "xfa/fxbarcode/BC_UtilCodingConvert.h" +#include "xfa/fxbarcode/common/BC_CommonBitMatrix.h" +#include "xfa/fxbarcode/datamatrix/BC_ASCIIEncoder.h" +#include "xfa/fxbarcode/datamatrix/BC_Base256Encoder.h" +#include "xfa/fxbarcode/datamatrix/BC_C40Encoder.h" +#include "xfa/fxbarcode/datamatrix/BC_EdifactEncoder.h" +#include "xfa/fxbarcode/datamatrix/BC_Encoder.h" +#include "xfa/fxbarcode/datamatrix/BC_EncoderContext.h" +#include "xfa/fxbarcode/datamatrix/BC_HighLevelEncoder.h" +#include "xfa/fxbarcode/datamatrix/BC_SymbolInfo.h" +#include "xfa/fxbarcode/datamatrix/BC_SymbolShapeHint.h" +#include "xfa/fxbarcode/datamatrix/BC_TextEncoder.h" +#include "xfa/fxbarcode/datamatrix/BC_X12Encoder.h" +#include "xfa/fxbarcode/utils.h" + +FX_WCHAR CBC_HighLevelEncoder::LATCH_TO_C40 = 230; +FX_WCHAR CBC_HighLevelEncoder::LATCH_TO_BASE256 = 231; +FX_WCHAR CBC_HighLevelEncoder::UPPER_SHIFT = 235; +FX_WCHAR CBC_HighLevelEncoder::LATCH_TO_ANSIX12 = 238; +FX_WCHAR CBC_HighLevelEncoder::LATCH_TO_TEXT = 239; +FX_WCHAR CBC_HighLevelEncoder::LATCH_TO_EDIFACT = 240; +FX_WCHAR CBC_HighLevelEncoder::C40_UNLATCH = 254; +FX_WCHAR CBC_HighLevelEncoder::X12_UNLATCH = 254; +FX_WCHAR CBC_HighLevelEncoder::PAD = 129; +FX_WCHAR CBC_HighLevelEncoder::MACRO_05 = 236; +FX_WCHAR CBC_HighLevelEncoder::MACRO_06 = 237; +const wchar_t* CBC_HighLevelEncoder::MACRO_05_HEADER = L"[)>05"; +const wchar_t* CBC_HighLevelEncoder::MACRO_06_HEADER = L"[)>06"; +const wchar_t CBC_HighLevelEncoder::MACRO_TRAILER = 0x0004; + +CBC_HighLevelEncoder::CBC_HighLevelEncoder() {} +CBC_HighLevelEncoder::~CBC_HighLevelEncoder() {} +CFX_ByteArray& CBC_HighLevelEncoder::getBytesForMessage(CFX_WideString msg) { + CFX_ByteString bytestr; + CBC_UtilCodingConvert::UnicodeToUTF8(msg, bytestr); + for (int32_t i = 0; i < bytestr.GetLength(); i++) { + m_bytearray.Add(bytestr.GetAt(i)); + } + return m_bytearray; +} +CFX_WideString CBC_HighLevelEncoder::encodeHighLevel(CFX_WideString msg, + CFX_WideString ecLevel, + int32_t& e) { + return encodeHighLevel(msg, ecLevel, FORCE_NONE, NULL, NULL, e); +} +CFX_WideString CBC_HighLevelEncoder::encodeHighLevel(CFX_WideString msg, + CFX_WideString ecLevel, + SymbolShapeHint shape, + CBC_Dimension* minSize, + CBC_Dimension* maxSize, + int32_t& e) { + CBC_EncoderContext context(msg, ecLevel, e); + BC_EXCEPTION_CHECK_ReturnValue(e, (FX_WCHAR*)""); + context.setSymbolShape(shape); + context.setSizeConstraints(minSize, maxSize); + if ((msg.Mid(0, 6) == MACRO_05_HEADER) && + (msg.Mid(msg.GetLength() - 1, 1) == MACRO_TRAILER)) { + context.writeCodeword(MACRO_05); + context.setSkipAtEnd(2); + context.m_pos += 6; + } else if ((msg.Mid(0, 6) == MACRO_06_HEADER) && + (msg.Mid(msg.GetLength() - 1, 1) == MACRO_TRAILER)) { + context.writeCodeword(MACRO_06); + context.setSkipAtEnd(2); + context.m_pos += 6; + } + CFX_PtrArray encoders; + encoders.Add(new CBC_ASCIIEncoder()); + encoders.Add(new CBC_C40Encoder()); + encoders.Add(new CBC_TextEncoder()); + encoders.Add(new CBC_X12Encoder()); + encoders.Add(new CBC_EdifactEncoder()); + encoders.Add(new CBC_Base256Encoder()); + int32_t encodingMode = ASCII_ENCODATION; + while (context.hasMoreCharacters()) { + ((CBC_Encoder*)encoders.GetAt(encodingMode))->Encode(context, e); + if (e != BCExceptionNO) { + for (int32_t i = 0; i < encoders.GetSize(); i++) { + delete (CBC_Encoder*)encoders.GetAt(i); + } + encoders.RemoveAll(); + return (FX_WCHAR*)""; + } + if (context.m_newEncoding >= 0) { + encodingMode = context.m_newEncoding; + context.resetEncoderSignal(); + } + } + int32_t len = context.m_codewords.GetLength(); + context.updateSymbolInfo(e); + if (e != BCExceptionNO) { + for (int32_t i = 0; i < encoders.GetSize(); i++) { + delete (CBC_Encoder*)encoders.GetAt(i); + } + encoders.RemoveAll(); + return (FX_WCHAR*)""; + } + int32_t capacity = context.m_symbolInfo->m_dataCapacity; + if (len < capacity) { + if (encodingMode != ASCII_ENCODATION && + encodingMode != BASE256_ENCODATION) { + context.writeCodeword(0x00fe); + } + } + CFX_WideString codewords = context.m_codewords; + if (codewords.GetLength() < capacity) { + codewords += PAD; + } + while (codewords.GetLength() < capacity) { + codewords += (randomize253State(PAD, codewords.GetLength() + 1)); + } + for (int32_t i = 0; i < encoders.GetSize(); i++) { + delete (CBC_Encoder*)encoders.GetAt(i); + } + encoders.RemoveAll(); + return codewords; +} +int32_t CBC_HighLevelEncoder::lookAheadTest(CFX_WideString msg, + int32_t startpos, + int32_t currentMode) { + if (startpos >= msg.GetLength()) { + return currentMode; + } + CFX_FloatArray charCounts; + if (currentMode == ASCII_ENCODATION) { + charCounts.Add(0); + charCounts.Add(1); + charCounts.Add(1); + charCounts.Add(1); + charCounts.Add(1); + charCounts.Add(1.25f); + } else { + charCounts.Add(1); + charCounts.Add(2); + charCounts.Add(2); + charCounts.Add(2); + charCounts.Add(2); + charCounts.Add(2.25f); + charCounts[currentMode] = 0; + } + int32_t charsProcessed = 0; + while (TRUE) { + if ((startpos + charsProcessed) == msg.GetLength()) { + FX_DWORD min = std::numeric_limits::max(); + CFX_ByteArray mins; + mins.SetSize(6); + CFX_Int32Array intCharCounts; + intCharCounts.SetSize(6); + min = findMinimums(charCounts, intCharCounts, min, mins); + int32_t minCount = getMinimumCount(mins); + if (intCharCounts[ASCII_ENCODATION] == min) { + return ASCII_ENCODATION; + } + if (minCount == 1 && mins[BASE256_ENCODATION] > 0) { + return BASE256_ENCODATION; + } + if (minCount == 1 && mins[EDIFACT_ENCODATION] > 0) { + return EDIFACT_ENCODATION; + } + if (minCount == 1 && mins[TEXT_ENCODATION] > 0) { + return TEXT_ENCODATION; + } + if (minCount == 1 && mins[X12_ENCODATION] > 0) { + return X12_ENCODATION; + } + return C40_ENCODATION; + } + FX_WCHAR c = msg.GetAt(startpos + charsProcessed); + charsProcessed++; + if (isDigit(c)) { + charCounts[ASCII_ENCODATION] += 0.5; + } else if (isExtendedASCII(c)) { + charCounts[ASCII_ENCODATION] = + (FX_FLOAT)ceil(charCounts[ASCII_ENCODATION]); + charCounts[ASCII_ENCODATION] += 2; + } else { + charCounts[ASCII_ENCODATION] = + (FX_FLOAT)ceil(charCounts[ASCII_ENCODATION]); + charCounts[ASCII_ENCODATION]++; + } + if (isNativeC40(c)) { + charCounts[C40_ENCODATION] += 2.0f / 3.0f; + } else if (isExtendedASCII(c)) { + charCounts[C40_ENCODATION] += 8.0f / 3.0f; + } else { + charCounts[C40_ENCODATION] += 4.0f / 3.0f; + } + if (isNativeText(c)) { + charCounts[TEXT_ENCODATION] += 2.0f / 3.0f; + } else if (isExtendedASCII(c)) { + charCounts[TEXT_ENCODATION] += 8.0f / 3.0f; + } else { + charCounts[TEXT_ENCODATION] += 4.0f / 3.0f; + } + if (isNativeX12(c)) { + charCounts[X12_ENCODATION] += 2.0f / 3.0f; + } else if (isExtendedASCII(c)) { + charCounts[X12_ENCODATION] += 13.0f / 3.0f; + } else { + charCounts[X12_ENCODATION] += 10.0f / 3.0f; + } + if (isNativeEDIFACT(c)) { + charCounts[EDIFACT_ENCODATION] += 3.0f / 4.0f; + } else if (isExtendedASCII(c)) { + charCounts[EDIFACT_ENCODATION] += 17.0f / 4.0f; + } else { + charCounts[EDIFACT_ENCODATION] += 13.0f / 4.0f; + } + if (isSpecialB256(c)) { + charCounts[BASE256_ENCODATION] += 4; + } else { + charCounts[BASE256_ENCODATION]++; + } + if (charsProcessed >= 4) { + CFX_Int32Array intCharCounts; + intCharCounts.SetSize(6); + CFX_ByteArray mins; + mins.SetSize(6); + findMinimums(charCounts, intCharCounts, + std::numeric_limits::max(), mins); + int32_t minCount = getMinimumCount(mins); + if (intCharCounts[ASCII_ENCODATION] < intCharCounts[BASE256_ENCODATION] && + intCharCounts[ASCII_ENCODATION] < intCharCounts[C40_ENCODATION] && + intCharCounts[ASCII_ENCODATION] < intCharCounts[TEXT_ENCODATION] && + intCharCounts[ASCII_ENCODATION] < intCharCounts[X12_ENCODATION] && + intCharCounts[ASCII_ENCODATION] < intCharCounts[EDIFACT_ENCODATION]) { + return ASCII_ENCODATION; + } + if (intCharCounts[BASE256_ENCODATION] < intCharCounts[ASCII_ENCODATION] || + (mins[C40_ENCODATION] + mins[TEXT_ENCODATION] + mins[X12_ENCODATION] + + mins[EDIFACT_ENCODATION]) == 0) { + return BASE256_ENCODATION; + } + if (minCount == 1 && mins[EDIFACT_ENCODATION] > 0) { + return EDIFACT_ENCODATION; + } + if (minCount == 1 && mins[TEXT_ENCODATION] > 0) { + return TEXT_ENCODATION; + } + if (minCount == 1 && mins[X12_ENCODATION] > 0) { + return X12_ENCODATION; + } + if (intCharCounts[C40_ENCODATION] + 1 < intCharCounts[ASCII_ENCODATION] && + intCharCounts[C40_ENCODATION] + 1 < + intCharCounts[BASE256_ENCODATION] && + intCharCounts[C40_ENCODATION] + 1 < + intCharCounts[EDIFACT_ENCODATION] && + intCharCounts[C40_ENCODATION] + 1 < intCharCounts[TEXT_ENCODATION]) { + if (intCharCounts[C40_ENCODATION] < intCharCounts[X12_ENCODATION]) { + return C40_ENCODATION; + } + if (intCharCounts[C40_ENCODATION] == intCharCounts[X12_ENCODATION]) { + int32_t p = startpos + charsProcessed + 1; + while (p < msg.GetLength()) { + FX_WCHAR tc = msg.GetAt(p); + if (isX12TermSep(tc)) { + return X12_ENCODATION; + } + if (!isNativeX12(tc)) { + break; + } + p++; + } + return C40_ENCODATION; + } + } + } + } +} +FX_BOOL CBC_HighLevelEncoder::isDigit(FX_WCHAR ch) { + return ch >= '0' && ch <= '9'; +} +FX_BOOL CBC_HighLevelEncoder::isExtendedASCII(FX_WCHAR ch) { + return ch >= 128 && ch <= 255; +} +int32_t CBC_HighLevelEncoder::determineConsecutiveDigitCount(CFX_WideString msg, + int32_t startpos) { + int32_t count = 0; + int32_t len = msg.GetLength(); + int32_t idx = startpos; + if (idx < len) { + FX_WCHAR ch = msg.GetAt(idx); + while (isDigit(ch) && idx < len) { + count++; + idx++; + if (idx < len) { + ch = msg.GetAt(idx); + } + } + } + return count; +} +void CBC_HighLevelEncoder::illegalCharacter(FX_WCHAR c, int32_t& e) { + e = BCExceptionIllegalArgument; +} +FX_WCHAR CBC_HighLevelEncoder::randomize253State(FX_WCHAR ch, + int32_t codewordPosition) { + int32_t pseudoRandom = ((149 * codewordPosition) % 253) + 1; + int32_t tempVariable = ch + pseudoRandom; + return tempVariable <= 254 ? (FX_WCHAR)tempVariable + : (FX_WCHAR)(tempVariable - 254); +} +int32_t CBC_HighLevelEncoder::findMinimums(CFX_FloatArray& charCounts, + CFX_Int32Array& intCharCounts, + int32_t min, + CFX_ByteArray& mins) { + for (int32_t l = 0; l < mins.GetSize(); l++) { + mins[l] = (uint8_t)0; + } + for (int32_t i = 0; i < 6; i++) { + intCharCounts[i] = (int32_t)ceil(charCounts[i]); + int32_t current = intCharCounts[i]; + if (min > current) { + min = current; + for (int32_t j = 0; j < mins.GetSize(); j++) { + mins[j] = (uint8_t)0; + } + } + if (min == current) { + mins[i]++; + } + } + return min; +} +int32_t CBC_HighLevelEncoder::getMinimumCount(CFX_ByteArray& mins) { + int32_t minCount = 0; + for (int32_t i = 0; i < 6; i++) { + minCount += mins[i]; + } + return minCount; +} +FX_BOOL CBC_HighLevelEncoder::isNativeC40(FX_WCHAR ch) { + return (ch == ' ') || (ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z'); +} +FX_BOOL CBC_HighLevelEncoder::isNativeText(FX_WCHAR ch) { + return (ch == ' ') || (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z'); +} +FX_BOOL CBC_HighLevelEncoder::isNativeX12(FX_WCHAR ch) { + return isX12TermSep(ch) || (ch == ' ') || (ch >= '0' && ch <= '9') || + (ch >= 'A' && ch <= 'Z'); +} +FX_BOOL CBC_HighLevelEncoder::isX12TermSep(FX_WCHAR ch) { + return (ch == '\r') || (ch == '*') || (ch == '>'); +} +FX_BOOL CBC_HighLevelEncoder::isNativeEDIFACT(FX_WCHAR ch) { + return ch >= ' ' && ch <= '^'; +} +FX_BOOL CBC_HighLevelEncoder::isSpecialB256(FX_WCHAR ch) { + return FALSE; +} diff --git a/xfa/fxbarcode/datamatrix/BC_HighLevelEncoder.h b/xfa/fxbarcode/datamatrix/BC_HighLevelEncoder.h new file mode 100644 index 0000000000..0aad4d4da5 --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_HighLevelEncoder.h @@ -0,0 +1,76 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_DATAMATRIX_BC_HIGHLEVELENCODER_H_ +#define XFA_FXBARCODE_DATAMATRIX_BC_HIGHLEVELENCODER_H_ + +#include "xfa/fxbarcode/datamatrix/BC_SymbolShapeHint.h" + +#define ASCII_ENCODATION 0 +#define C40_ENCODATION 1 +#define TEXT_ENCODATION 2 +#define X12_ENCODATION 3 +#define EDIFACT_ENCODATION 4 +#define BASE256_ENCODATION 5 + +class CBC_HighLevelEncoder : public CBC_SymbolShapeHint { + public: + CBC_HighLevelEncoder(); + virtual ~CBC_HighLevelEncoder(); + CFX_ByteArray& getBytesForMessage(CFX_WideString msg); + static CFX_WideString encodeHighLevel(CFX_WideString msg, + CFX_WideString ecLevel, + int32_t& e); + static CFX_WideString encodeHighLevel(CFX_WideString msg, + CFX_WideString ecLevel, + SymbolShapeHint shape, + CBC_Dimension* minSize, + CBC_Dimension* maxSize, + int32_t& e); + static int32_t lookAheadTest(CFX_WideString msg, + int32_t startpos, + int32_t currentMode); + static FX_BOOL isDigit(FX_WCHAR ch); + static FX_BOOL isExtendedASCII(FX_WCHAR ch); + static int32_t determineConsecutiveDigitCount(CFX_WideString msg, + int32_t startpos); + static void illegalCharacter(FX_WCHAR c, int32_t& e); + + public: + static FX_WCHAR LATCH_TO_C40; + static FX_WCHAR LATCH_TO_BASE256; + static FX_WCHAR UPPER_SHIFT; + static FX_WCHAR LATCH_TO_ANSIX12; + static FX_WCHAR LATCH_TO_TEXT; + static FX_WCHAR LATCH_TO_EDIFACT; + static FX_WCHAR C40_UNLATCH; + static FX_WCHAR X12_UNLATCH; + + private: + static FX_WCHAR PAD; + static FX_WCHAR MACRO_05; + static FX_WCHAR MACRO_06; + static const wchar_t* MACRO_05_HEADER; + static const wchar_t* MACRO_06_HEADER; + static const wchar_t MACRO_TRAILER; + CFX_ByteArray m_bytearray; + + private: + static FX_WCHAR randomize253State(FX_WCHAR ch, int32_t codewordPosition); + static int32_t findMinimums(CFX_FloatArray& charCounts, + CFX_Int32Array& intCharCounts, + int32_t min, + CFX_ByteArray& mins); + static int32_t getMinimumCount(CFX_ByteArray& mins); + static FX_BOOL isNativeC40(FX_WCHAR ch); + static FX_BOOL isNativeText(FX_WCHAR ch); + static FX_BOOL isNativeX12(FX_WCHAR ch); + static FX_BOOL isX12TermSep(FX_WCHAR ch); + static FX_BOOL isNativeEDIFACT(FX_WCHAR ch); + static FX_BOOL isSpecialB256(FX_WCHAR ch); +}; + +#endif // XFA_FXBARCODE_DATAMATRIX_BC_HIGHLEVELENCODER_H_ diff --git a/xfa/fxbarcode/datamatrix/BC_SymbolInfo.cpp b/xfa/fxbarcode/datamatrix/BC_SymbolInfo.cpp new file mode 100644 index 0000000000..593f4cb162 --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_SymbolInfo.cpp @@ -0,0 +1,255 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2006 Jeremias Maerki + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/BC_Dimension.h" +#include "xfa/fxbarcode/common/BC_CommonBitMatrix.h" +#include "xfa/fxbarcode/datamatrix/BC_DataMatrixSymbolInfo144.h" +#include "xfa/fxbarcode/datamatrix/BC_Encoder.h" +#include "xfa/fxbarcode/datamatrix/BC_SymbolInfo.h" +#include "xfa/fxbarcode/datamatrix/BC_SymbolShapeHint.h" + +#define SYMBOLS_COUNT 30 + +CBC_SymbolInfo* CBC_SymbolInfo::m_PROD_SYMBOLS[30] = { + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}; +CBC_SymbolInfo* CBC_SymbolInfo::m_symbols[30] = { + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}; +void CBC_SymbolInfo::Initialize() { + m_PROD_SYMBOLS[0] = new CBC_SymbolInfo(FALSE, 3, 5, 8, 8, 1); + m_PROD_SYMBOLS[1] = new CBC_SymbolInfo(FALSE, 5, 7, 10, 10, 1); + m_PROD_SYMBOLS[2] = new CBC_SymbolInfo(TRUE, 5, 7, 16, 6, 1); + m_PROD_SYMBOLS[3] = new CBC_SymbolInfo(FALSE, 8, 10, 12, 12, 1); + m_PROD_SYMBOLS[4] = new CBC_SymbolInfo(TRUE, 10, 11, 14, 6, 2); + m_PROD_SYMBOLS[5] = new CBC_SymbolInfo(FALSE, 12, 12, 14, 14, 1); + m_PROD_SYMBOLS[6] = new CBC_SymbolInfo(TRUE, 16, 14, 24, 10, 1); + m_PROD_SYMBOLS[7] = new CBC_SymbolInfo(FALSE, 18, 14, 16, 16, 1); + m_PROD_SYMBOLS[8] = new CBC_SymbolInfo(FALSE, 22, 18, 18, 18, 1); + m_PROD_SYMBOLS[9] = new CBC_SymbolInfo(TRUE, 22, 18, 16, 10, 2); + m_PROD_SYMBOLS[10] = new CBC_SymbolInfo(FALSE, 30, 20, 20, 20, 1); + m_PROD_SYMBOLS[11] = new CBC_SymbolInfo(TRUE, 32, 24, 16, 14, 2); + m_PROD_SYMBOLS[12] = new CBC_SymbolInfo(FALSE, 36, 24, 22, 22, 1); + m_PROD_SYMBOLS[13] = new CBC_SymbolInfo(FALSE, 44, 28, 24, 24, 1); + m_PROD_SYMBOLS[14] = new CBC_SymbolInfo(TRUE, 49, 28, 22, 14, 2); + m_PROD_SYMBOLS[15] = new CBC_SymbolInfo(FALSE, 62, 36, 14, 14, 4); + m_PROD_SYMBOLS[16] = new CBC_SymbolInfo(FALSE, 86, 42, 16, 16, 4); + m_PROD_SYMBOLS[17] = new CBC_SymbolInfo(FALSE, 114, 48, 18, 18, 4); + m_PROD_SYMBOLS[18] = new CBC_SymbolInfo(FALSE, 144, 56, 20, 20, 4); + m_PROD_SYMBOLS[19] = new CBC_SymbolInfo(FALSE, 174, 68, 22, 22, 4); + m_PROD_SYMBOLS[20] = new CBC_SymbolInfo(FALSE, 204, 84, 24, 24, 4, 102, 42); + m_PROD_SYMBOLS[21] = new CBC_SymbolInfo(FALSE, 280, 112, 14, 14, 16, 140, 56); + m_PROD_SYMBOLS[22] = new CBC_SymbolInfo(FALSE, 368, 144, 16, 16, 16, 92, 36); + m_PROD_SYMBOLS[23] = new CBC_SymbolInfo(FALSE, 456, 192, 18, 18, 16, 114, 48); + m_PROD_SYMBOLS[24] = new CBC_SymbolInfo(FALSE, 576, 224, 20, 20, 16, 144, 56); + m_PROD_SYMBOLS[25] = new CBC_SymbolInfo(FALSE, 696, 272, 22, 22, 16, 174, 68); + m_PROD_SYMBOLS[26] = new CBC_SymbolInfo(FALSE, 816, 336, 24, 24, 16, 136, 56); + m_PROD_SYMBOLS[27] = + new CBC_SymbolInfo(FALSE, 1050, 408, 18, 18, 36, 175, 68); + m_PROD_SYMBOLS[28] = + new CBC_SymbolInfo(FALSE, 1304, 496, 20, 20, 36, 163, 62); + m_PROD_SYMBOLS[29] = new CBC_DataMatrixSymbolInfo144(); + for (int32_t i = 0; i < SYMBOLS_COUNT; i++) { + m_symbols[i] = m_PROD_SYMBOLS[i]; + } +} +void CBC_SymbolInfo::Finalize() { + for (int32_t i = 0; i < SYMBOLS_COUNT; i++) { + delete m_PROD_SYMBOLS[i]; + m_PROD_SYMBOLS[i] = NULL; + m_symbols[i] = NULL; + } +} +CBC_SymbolInfo::CBC_SymbolInfo(FX_BOOL rectangular, + int32_t dataCapacity, + int32_t errorCodewords, + int32_t matrixWidth, + int32_t matrixHeight, + int32_t dataRegions) { + m_rectangular = rectangular; + m_dataCapacity = dataCapacity; + m_errorCodewords = errorCodewords; + m_matrixWidth = matrixWidth; + m_matrixHeight = matrixHeight; + m_dataRegions = dataRegions; + m_rsBlockData = dataCapacity; + m_rsBlockError = errorCodewords; +} +CBC_SymbolInfo::CBC_SymbolInfo(FX_BOOL rectangular, + int32_t dataCapacity, + int32_t errorCodewords, + int32_t matrixWidth, + int32_t matrixHeight, + int32_t dataRegions, + int32_t rsBlockData, + int32_t rsBlockError) { + m_rectangular = rectangular; + m_dataCapacity = dataCapacity; + m_errorCodewords = errorCodewords; + m_matrixWidth = matrixWidth; + m_matrixHeight = matrixHeight; + m_dataRegions = dataRegions; + m_rsBlockData = rsBlockData; + m_rsBlockError = rsBlockError; +} +CBC_SymbolInfo::~CBC_SymbolInfo() {} + +CBC_SymbolInfo* CBC_SymbolInfo::lookup(int32_t dataCodewords, int32_t& e) { + return lookup(dataCodewords, FORCE_NONE, TRUE, e); +} +CBC_SymbolInfo* CBC_SymbolInfo::lookup(int32_t dataCodewords, + SymbolShapeHint shape, + int32_t& e) { + return lookup(dataCodewords, shape, TRUE, e); +} +CBC_SymbolInfo* CBC_SymbolInfo::lookup(int32_t dataCodewords, + FX_BOOL allowRectangular, + FX_BOOL fail, + int32_t& e) { + SymbolShapeHint shape = allowRectangular ? FORCE_NONE : FORCE_SQUARE; + return lookup(dataCodewords, shape, fail, e); +} +CBC_SymbolInfo* CBC_SymbolInfo::lookup(int32_t dataCodewords, + SymbolShapeHint shape, + FX_BOOL fail, + int32_t& e) { + return lookup(dataCodewords, shape, NULL, NULL, fail, e); +} +CBC_SymbolInfo* CBC_SymbolInfo::lookup(int32_t dataCodewords, + SymbolShapeHint shape, + CBC_Dimension* minSize, + CBC_Dimension* maxSize, + FX_BOOL fail, + int32_t& e) { + for (int32_t i = 0; i < SYMBOLS_COUNT; i++) { + CBC_SymbolInfo* symbol = m_symbols[i]; + if (shape == FORCE_SQUARE && symbol->m_rectangular) { + continue; + } + if (shape == FORCE_RECTANGLE && !symbol->m_rectangular) { + continue; + } + if (minSize && (symbol->getSymbolWidth(e) < minSize->getWidth() || + symbol->getSymbolHeight(e) < minSize->getHeight())) { + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + continue; + } + if (maxSize && (symbol->getSymbolWidth(e) > maxSize->getWidth() || + symbol->getSymbolHeight(e) > maxSize->getHeight())) { + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + continue; + } + if (dataCodewords <= symbol->m_dataCapacity) { + return symbol; + } + } + if (fail) { + e = BCExceptionIllegalDataCodewords; + return NULL; + } + return NULL; +} +int32_t CBC_SymbolInfo::getHorizontalDataRegions(int32_t& e) { + switch (m_dataRegions) { + case 1: + return 1; + case 2: + return 2; + case 4: + return 2; + case 16: + return 4; + case 36: + return 6; + default: + e = BCExceptionCannotHandleThisNumberOfDataRegions; + return 0; + } +} +int32_t CBC_SymbolInfo::getVerticalDataRegions(int32_t& e) { + switch (m_dataRegions) { + case 1: + return 1; + case 2: + return 1; + case 4: + return 2; + case 16: + return 4; + case 36: + return 6; + default: + e = BCExceptionCannotHandleThisNumberOfDataRegions; + return 0; + } +} +int32_t CBC_SymbolInfo::getSymbolDataWidth(int32_t& e) { + return getHorizontalDataRegions(e) * m_matrixWidth; +} +int32_t CBC_SymbolInfo::getSymbolDataHeight(int32_t& e) { + return getVerticalDataRegions(e) * m_matrixHeight; +} +int32_t CBC_SymbolInfo::getSymbolWidth(int32_t& e) { + return getSymbolDataWidth(e) + (getHorizontalDataRegions(e) * 2); +} +int32_t CBC_SymbolInfo::getSymbolHeight(int32_t& e) { + return getSymbolDataHeight(e) + (getVerticalDataRegions(e) * 2); +} +int32_t CBC_SymbolInfo::getCodewordCount() { + return m_dataCapacity + m_errorCodewords; +} +int32_t CBC_SymbolInfo::getInterleavedBlockCount() { + return m_dataCapacity / m_rsBlockData; +} +int32_t CBC_SymbolInfo::getDataLengthForInterleavedBlock(int32_t index) { + return m_rsBlockData; +} +int32_t CBC_SymbolInfo::getErrorLengthForInterleavedBlock(int32_t index) { + return m_rsBlockError; +} +CFX_WideString CBC_SymbolInfo::toString(int32_t& e) { + CFX_WideString sb; + sb += (FX_WCHAR*)(m_rectangular ? "Rectangular Symbol:" : "Square Symbol:"); + sb += (FX_WCHAR*)" data region "; + sb += m_matrixWidth; + sb += (FX_WCHAR)'x'; + sb += m_matrixHeight; + sb += (FX_WCHAR*)", symbol size "; + sb += getSymbolWidth(e); + BC_EXCEPTION_CHECK_ReturnValue(e, (FX_WCHAR*)""); + sb += (FX_WCHAR)'x'; + sb += getSymbolHeight(e); + BC_EXCEPTION_CHECK_ReturnValue(e, (FX_WCHAR*)""); + sb += (FX_WCHAR*)", symbol data size "; + sb += getSymbolDataWidth(e); + BC_EXCEPTION_CHECK_ReturnValue(e, (FX_WCHAR*)""); + sb += (FX_WCHAR)'x'; + sb += getSymbolDataHeight(e); + BC_EXCEPTION_CHECK_ReturnValue(e, (FX_WCHAR*)""); + sb += (FX_WCHAR*)", codewords "; + sb += m_dataCapacity; + sb += (FX_WCHAR)'+'; + sb += m_errorCodewords; + return sb; +} diff --git a/xfa/fxbarcode/datamatrix/BC_SymbolInfo.h b/xfa/fxbarcode/datamatrix/BC_SymbolInfo.h new file mode 100644 index 0000000000..8f7ed28da9 --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_SymbolInfo.h @@ -0,0 +1,83 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_DATAMATRIX_BC_SYMBOLINFO_H_ +#define XFA_FXBARCODE_DATAMATRIX_BC_SYMBOLINFO_H_ + +#include "core/include/fxcrt/fx_string.h" +#include "core/include/fxcrt/fx_system.h" +#include "xfa/fxbarcode/datamatrix/BC_SymbolShapeHint.h" + +class CBC_Dimension; + +class CBC_SymbolInfo : public CBC_SymbolShapeHint { + public: + CBC_SymbolInfo(FX_BOOL rectangular, + int32_t dataCapacity, + int32_t errorCodewords, + int32_t matrixWidth, + int32_t matrixHeight, + int32_t dataRegions); + virtual ~CBC_SymbolInfo(); + static void Initialize(); + static void Finalize(); + static void overrideSymbolSet(CBC_SymbolInfo* override); + static CBC_SymbolInfo* lookup(int32_t dataCodewords, int32_t& e); + static CBC_SymbolInfo* lookup(int32_t dataCodewords, + SymbolShapeHint shape, + int32_t& e); + static CBC_SymbolInfo* lookup(int32_t dataCodewords, + FX_BOOL allowRectangular, + FX_BOOL fail, + int32_t& e); + static CBC_SymbolInfo* lookup(int32_t dataCodewords, + SymbolShapeHint shape, + FX_BOOL fail, + int32_t& e); + static CBC_SymbolInfo* lookup(int32_t dataCodewords, + SymbolShapeHint shape, + CBC_Dimension* minSize, + CBC_Dimension* maxSize, + FX_BOOL fail, + int32_t& e); + int32_t getHorizontalDataRegions(int32_t& e); + int32_t getVerticalDataRegions(int32_t& e); + int32_t getSymbolDataWidth(int32_t& e); + int32_t getSymbolDataHeight(int32_t& e); + int32_t getSymbolWidth(int32_t& e); + int32_t getSymbolHeight(int32_t& e); + int32_t getCodewordCount(); + int32_t getInterleavedBlockCount(); + int32_t getDataLengthForInterleavedBlock(int32_t index); + int32_t getErrorLengthForInterleavedBlock(int32_t index); + CFX_WideString toString(int32_t& e); + + public: + int32_t m_dataCapacity; + int32_t m_errorCodewords; + int32_t m_matrixWidth; + int32_t m_matrixHeight; + int32_t m_rsBlockData; + int32_t m_rsBlockError; + static CBC_SymbolInfo* m_PROD_SYMBOLS[30]; + + private: + static CBC_SymbolInfo* m_symbols[30]; + FX_BOOL m_rectangular; + int32_t m_dataRegions; + + private: + CBC_SymbolInfo(FX_BOOL rectangular, + int32_t dataCapacity, + int32_t errorCodewords, + int32_t matrixWidth, + int32_t matrixHeight, + int32_t dataRegions, + int32_t rsBlockData, + int32_t rsBlockError); +}; + +#endif // XFA_FXBARCODE_DATAMATRIX_BC_SYMBOLINFO_H_ diff --git a/xfa/fxbarcode/datamatrix/BC_SymbolShapeHint.cpp b/xfa/fxbarcode/datamatrix/BC_SymbolShapeHint.cpp new file mode 100644 index 0000000000..8f91dd837a --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_SymbolShapeHint.cpp @@ -0,0 +1,26 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2007 Jeremias Maerki. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/datamatrix/BC_SymbolShapeHint.h" + +CBC_SymbolShapeHint::CBC_SymbolShapeHint() {} +CBC_SymbolShapeHint::~CBC_SymbolShapeHint() {} diff --git a/xfa/fxbarcode/datamatrix/BC_SymbolShapeHint.h b/xfa/fxbarcode/datamatrix/BC_SymbolShapeHint.h new file mode 100644 index 0000000000..5dfac0516f --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_SymbolShapeHint.h @@ -0,0 +1,21 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_DATAMATRIX_BC_SYMBOLSHAPEHINT_H_ +#define XFA_FXBARCODE_DATAMATRIX_BC_SYMBOLSHAPEHINT_H_ + +class CBC_SymbolShapeHint { + public: + CBC_SymbolShapeHint(); + virtual ~CBC_SymbolShapeHint(); + enum SymbolShapeHint { + FORCE_NONE, + FORCE_SQUARE, + FORCE_RECTANGLE, + }; +}; + +#endif // XFA_FXBARCODE_DATAMATRIX_BC_SYMBOLSHAPEHINT_H_ diff --git a/xfa/fxbarcode/datamatrix/BC_TextEncoder.cpp b/xfa/fxbarcode/datamatrix/BC_TextEncoder.cpp new file mode 100644 index 0000000000..c8b37e9c85 --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_TextEncoder.cpp @@ -0,0 +1,98 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2006-2007 Jeremias Maerki. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/BC_Dimension.h" +#include "xfa/fxbarcode/common/BC_CommonBitMatrix.h" +#include "xfa/fxbarcode/datamatrix/BC_C40Encoder.h" +#include "xfa/fxbarcode/datamatrix/BC_Encoder.h" +#include "xfa/fxbarcode/datamatrix/BC_EncoderContext.h" +#include "xfa/fxbarcode/datamatrix/BC_HighLevelEncoder.h" +#include "xfa/fxbarcode/datamatrix/BC_SymbolInfo.h" +#include "xfa/fxbarcode/datamatrix/BC_SymbolShapeHint.h" +#include "xfa/fxbarcode/datamatrix/BC_TextEncoder.h" + +CBC_TextEncoder::CBC_TextEncoder() {} +CBC_TextEncoder::~CBC_TextEncoder() {} +int32_t CBC_TextEncoder::getEncodingMode() { + return TEXT_ENCODATION; +} +int32_t CBC_TextEncoder::encodeChar(FX_WCHAR c, + CFX_WideString& sb, + int32_t& e) { + if (c == ' ') { + sb += (FX_WCHAR)'\3'; + return 1; + } + if (c >= '0' && c <= '9') { + sb += (FX_WCHAR)(c - 48 + 4); + return 1; + } + if (c >= 'a' && c <= 'z') { + sb += (FX_WCHAR)(c - 97 + 14); + return 1; + } + if (c <= 0x1f) { + sb += (FX_WCHAR)'\0'; + sb += c; + return 2; + } + if (c >= '!' && c <= '/') { + sb += (FX_WCHAR)'\1'; + sb += (FX_WCHAR)(c - 33); + return 2; + } + if (c >= ':' && c <= '@') { + sb += (FX_WCHAR)'\1'; + sb += (FX_WCHAR)(c - 58 + 15); + return 2; + } + if (c >= '[' && c <= '_') { + sb += (FX_WCHAR)'\1'; + sb += (FX_WCHAR)(c - 91 + 22); + return 2; + } + if (c == 0x0060) { + sb += (FX_WCHAR)'\2'; + sb += (FX_WCHAR)(c - 96); + return 2; + } + if (c >= 'A' && c <= 'Z') { + sb += (FX_WCHAR)'\2'; + sb += (FX_WCHAR)(c - 65 + 1); + return 2; + } + if (c >= '{' && c <= 0x007f) { + sb += (FX_WCHAR)'\2'; + sb += (FX_WCHAR)(c - 123 + 27); + return 2; + } + if (c >= 0x0080) { + sb += (FX_WCHAR)'\1'; + sb += (FX_WCHAR)0x001e; + int32_t len = 2; + len += encodeChar((FX_WCHAR)(c - 128), sb, e); + BC_EXCEPTION_CHECK_ReturnValue(e, -1); + return len; + } + CBC_HighLevelEncoder::illegalCharacter(c, e); + return -1; +} diff --git a/xfa/fxbarcode/datamatrix/BC_TextEncoder.h b/xfa/fxbarcode/datamatrix/BC_TextEncoder.h new file mode 100644 index 0000000000..1f97f3f8c6 --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_TextEncoder.h @@ -0,0 +1,19 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_DATAMATRIX_BC_TEXTENCODER_H_ +#define XFA_FXBARCODE_DATAMATRIX_BC_TEXTENCODER_H_ + +class CBC_TextEncoder; +class CBC_TextEncoder : public CBC_C40Encoder { + public: + CBC_TextEncoder(); + virtual ~CBC_TextEncoder(); + int32_t getEncodingMode(); + int32_t encodeChar(FX_WCHAR c, CFX_WideString& sb, int32_t& e); +}; + +#endif // XFA_FXBARCODE_DATAMATRIX_BC_TEXTENCODER_H_ diff --git a/xfa/fxbarcode/datamatrix/BC_X12Encoder.cpp b/xfa/fxbarcode/datamatrix/BC_X12Encoder.cpp new file mode 100644 index 0000000000..9ebfc46700 --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_X12Encoder.cpp @@ -0,0 +1,100 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2006-2007 Jeremias Maerki. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/BC_Dimension.h" +#include "xfa/fxbarcode/common/BC_CommonBitMatrix.h" +#include "xfa/fxbarcode/datamatrix/BC_C40Encoder.h" +#include "xfa/fxbarcode/datamatrix/BC_Encoder.h" +#include "xfa/fxbarcode/datamatrix/BC_EncoderContext.h" +#include "xfa/fxbarcode/datamatrix/BC_HighLevelEncoder.h" +#include "xfa/fxbarcode/datamatrix/BC_SymbolInfo.h" +#include "xfa/fxbarcode/datamatrix/BC_SymbolShapeHint.h" +#include "xfa/fxbarcode/datamatrix/BC_X12Encoder.h" + +CBC_X12Encoder::CBC_X12Encoder() {} +CBC_X12Encoder::~CBC_X12Encoder() {} +int32_t CBC_X12Encoder::getEncodingMode() { + return X12_ENCODATION; +} +void CBC_X12Encoder::Encode(CBC_EncoderContext& context, int32_t& e) { + CFX_WideString buffer; + while (context.hasMoreCharacters()) { + FX_WCHAR c = context.getCurrentChar(); + context.m_pos++; + encodeChar(c, buffer, e); + if (e != BCExceptionNO) { + return; + } + int32_t count = buffer.GetLength(); + if ((count % 3) == 0) { + writeNextTriplet(context, buffer); + int32_t newMode = CBC_HighLevelEncoder::lookAheadTest( + context.m_msg, context.m_pos, getEncodingMode()); + if (newMode != getEncodingMode()) { + context.signalEncoderChange(newMode); + break; + } + } + } + handleEOD(context, buffer, e); +} +void CBC_X12Encoder::handleEOD(CBC_EncoderContext& context, + CFX_WideString& buffer, + int32_t& e) { + context.updateSymbolInfo(e); + if (e != BCExceptionNO) { + return; + } + int32_t available = + context.m_symbolInfo->m_dataCapacity - context.getCodewordCount(); + int32_t count = buffer.GetLength(); + if (count == 2) { + context.writeCodeword(CBC_HighLevelEncoder::X12_UNLATCH); + context.m_pos -= 2; + context.signalEncoderChange(ASCII_ENCODATION); + } else if (count == 1) { + context.m_pos--; + if (available > 1) { + context.writeCodeword(CBC_HighLevelEncoder::X12_UNLATCH); + } + context.signalEncoderChange(ASCII_ENCODATION); + } +} +int32_t CBC_X12Encoder::encodeChar(FX_WCHAR c, CFX_WideString& sb, int32_t& e) { + if (c == '\r') { + sb += (FX_WCHAR)'\0'; + } else if (c == '*') { + sb += (FX_WCHAR)'\1'; + } else if (c == '>') { + sb += (FX_WCHAR)'\2'; + } else if (c == ' ') { + sb += (FX_WCHAR)'\3'; + } else if (c >= '0' && c <= '9') { + sb += (FX_WCHAR)(c - 48 + 4); + } else if (c >= 'A' && c <= 'Z') { + sb += (FX_WCHAR)(c - 65 + 14); + } else { + CBC_HighLevelEncoder::illegalCharacter(c, e); + BC_EXCEPTION_CHECK_ReturnValue(e, -1); + } + return 1; +} diff --git a/xfa/fxbarcode/datamatrix/BC_X12Encoder.h b/xfa/fxbarcode/datamatrix/BC_X12Encoder.h new file mode 100644 index 0000000000..5dccbb1d6f --- /dev/null +++ b/xfa/fxbarcode/datamatrix/BC_X12Encoder.h @@ -0,0 +1,24 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_DATAMATRIX_BC_X12ENCODER_H_ +#define XFA_FXBARCODE_DATAMATRIX_BC_X12ENCODER_H_ + +class CBC_C40Encoder; +class CBC_X12Encoder; +class CBC_X12Encoder : public CBC_C40Encoder { + public: + CBC_X12Encoder(); + virtual ~CBC_X12Encoder(); + int32_t getEncodingMode(); + void Encode(CBC_EncoderContext& context, int32_t& e); + void handleEOD(CBC_EncoderContext& context, + CFX_WideString& buffer, + int32_t& e); + int32_t encodeChar(FX_WCHAR c, CFX_WideString& sb, int32_t& e); +}; + +#endif // XFA_FXBARCODE_DATAMATRIX_BC_X12ENCODER_H_ -- cgit v1.2.3