diff options
author | Dan Sinclair <dsinclair@chromium.org> | 2017-03-29 15:18:41 -0400 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2017-03-29 21:01:07 +0000 |
commit | e778668fe92b8c60e0537ee48f79d5af6c1a2f1e (patch) | |
tree | ce7ce115b6f7306a6363f4a3d26d0de2c5646aea /fxbarcode/datamatrix | |
parent | b929ab0886a2b0ceb701989ef126e5b0cabf6997 (diff) | |
download | pdfium-e778668fe92b8c60e0537ee48f79d5af6c1a2f1e.tar.xz |
Move xfa/fxbarcode fxbarcode/
Nothing in fxbarcode/ depends on XFA code. This CL moves xfa/fxbarcode
to be fxbarcode/ and creates a static_library for fxbarcode which is
depend on by the xfa library.
Change-Id: I0b708737b07efb94b769a5238d92af92bc62880d
Reviewed-on: https://pdfium-review.googlesource.com/3291
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: dsinclair <dsinclair@chromium.org>
Diffstat (limited to 'fxbarcode/datamatrix')
30 files changed, 2592 insertions, 0 deletions
diff --git a/fxbarcode/datamatrix/BC_ASCIIEncoder.cpp b/fxbarcode/datamatrix/BC_ASCIIEncoder.cpp new file mode 100644 index 0000000000..2d7b9d7995 --- /dev/null +++ b/fxbarcode/datamatrix/BC_ASCIIEncoder.cpp @@ -0,0 +1,97 @@ +// 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 "fxbarcode/BC_Dimension.h" +#include "fxbarcode/datamatrix/BC_ASCIIEncoder.h" +#include "fxbarcode/datamatrix/BC_Encoder.h" +#include "fxbarcode/datamatrix/BC_EncoderContext.h" +#include "fxbarcode/datamatrix/BC_HighLevelEncoder.h" +#include "fxbarcode/datamatrix/BC_SymbolInfo.h" +#include "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) { + wchar_t 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 { + wchar_t 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((wchar_t)(c - 128 + 1)); + context.m_pos++; + } else { + context.writeCodeword((wchar_t)(c + 1)); + context.m_pos++; + } + } +} +wchar_t CBC_ASCIIEncoder::encodeASCIIDigits(wchar_t digit1, + wchar_t digit2, + int32_t& e) { + if (CBC_HighLevelEncoder::isDigit(digit1) && + CBC_HighLevelEncoder::isDigit(digit2)) { + int32_t num = (digit1 - 48) * 10 + (digit2 - 48); + return (wchar_t)(num + 130); + } + e = BCExceptionIllegalArgumentNotGigits; + return 0; +} diff --git a/fxbarcode/datamatrix/BC_ASCIIEncoder.h b/fxbarcode/datamatrix/BC_ASCIIEncoder.h new file mode 100644 index 0000000000..6eb3a50669 --- /dev/null +++ b/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 FXBARCODE_DATAMATRIX_BC_ASCIIENCODER_H_ +#define FXBARCODE_DATAMATRIX_BC_ASCIIENCODER_H_ + +#include "fxbarcode/datamatrix/BC_Encoder.h" + +class CBC_EncoderContext; + +class CBC_ASCIIEncoder : public CBC_Encoder { + public: + CBC_ASCIIEncoder(); + ~CBC_ASCIIEncoder() override; + + // CBC_Encoder + int32_t getEncodingMode() override; + void Encode(CBC_EncoderContext& context, int32_t& e) override; + + private: + static wchar_t encodeASCIIDigits(wchar_t digit1, wchar_t digit2, int32_t& e); +}; + +#endif // FXBARCODE_DATAMATRIX_BC_ASCIIENCODER_H_ diff --git a/fxbarcode/datamatrix/BC_Base256Encoder.cpp b/fxbarcode/datamatrix/BC_Base256Encoder.cpp new file mode 100644 index 0000000000..1b8db85bdd --- /dev/null +++ b/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 "fxbarcode/BC_Dimension.h" +#include "fxbarcode/datamatrix/BC_Base256Encoder.h" +#include "fxbarcode/datamatrix/BC_Encoder.h" +#include "fxbarcode/datamatrix/BC_EncoderContext.h" +#include "fxbarcode/datamatrix/BC_HighLevelEncoder.h" +#include "fxbarcode/datamatrix/BC_SymbolInfo.h" +#include "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 += (wchar_t)'\0'; + while (context.hasMoreCharacters()) { + wchar_t 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; + char buf[128]; + FXSYS_itoa(dataCount, buf, 10); + buffer.SetAt(0, wchar_t(*buf) - '0'); + int32_t lengthFieldSize = 1; + int32_t currentSize = + context.getCodewordCount() + dataCount + lengthFieldSize; + context.updateSymbolInfo(currentSize, e); + if (e != BCExceptionNO) { + return; + } + bool mustPad = (context.m_symbolInfo->m_dataCapacity - currentSize) > 0; + if (context.hasMoreCharacters() || mustPad) { + if (dataCount <= 249) { + buffer.SetAt(0, (wchar_t)dataCount); + } else if (dataCount > 249 && dataCount <= 1555) { + buffer.SetAt(0, (wchar_t)((dataCount / 250) + 249)); + buffer.Insert(1, (wchar_t)(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)); + } +} +wchar_t CBC_Base256Encoder::randomize255State(wchar_t ch, + int32_t codewordPosition) { + int32_t pseudoRandom = ((149 * codewordPosition) % 255) + 1; + int32_t tempVariable = ch + pseudoRandom; + if (tempVariable <= 255) { + return (wchar_t)tempVariable; + } else { + return (wchar_t)(tempVariable - 256); + } +} diff --git a/fxbarcode/datamatrix/BC_Base256Encoder.h b/fxbarcode/datamatrix/BC_Base256Encoder.h new file mode 100644 index 0000000000..65abf59662 --- /dev/null +++ b/fxbarcode/datamatrix/BC_Base256Encoder.h @@ -0,0 +1,25 @@ +// 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 FXBARCODE_DATAMATRIX_BC_BASE256ENCODER_H_ +#define FXBARCODE_DATAMATRIX_BC_BASE256ENCODER_H_ + +#include "fxbarcode/datamatrix/BC_Encoder.h" + +class CBC_Base256Encoder : public CBC_Encoder { + public: + CBC_Base256Encoder(); + ~CBC_Base256Encoder() override; + + // CBC_Encoder + int32_t getEncodingMode() override; + void Encode(CBC_EncoderContext& context, int32_t& e) override; + + private: + static wchar_t randomize255State(wchar_t ch, int32_t codewordPosition); +}; + +#endif // FXBARCODE_DATAMATRIX_BC_BASE256ENCODER_H_ diff --git a/fxbarcode/datamatrix/BC_C40Encoder.cpp b/fxbarcode/datamatrix/BC_C40Encoder.cpp new file mode 100644 index 0000000000..7c3f4e7d7a --- /dev/null +++ b/fxbarcode/datamatrix/BC_C40Encoder.cpp @@ -0,0 +1,200 @@ +// 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 "fxbarcode/BC_Dimension.h" +#include "fxbarcode/common/BC_CommonBitMatrix.h" +#include "fxbarcode/datamatrix/BC_C40Encoder.h" +#include "fxbarcode/datamatrix/BC_Encoder.h" +#include "fxbarcode/datamatrix/BC_EncoderContext.h" +#include "fxbarcode/datamatrix/BC_HighLevelEncoder.h" +#include "fxbarcode/datamatrix/BC_SymbolInfo.h" +#include "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()) { + wchar_t 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 += (wchar_t)'\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(wchar_t c, CFX_WideString& sb, int32_t& e) { + if (c == ' ') { + sb += (wchar_t)'\3'; + return 1; + } else if ((c >= '0') && (c <= '9')) { + sb += (wchar_t)(c - 48 + 4); + return 1; + } else if ((c >= 'A') && (c <= 'Z')) { + sb += (wchar_t)(c - 65 + 14); + return 1; + } else if (c <= 0x1f) { + sb += (wchar_t)'\0'; + sb += c; + return 2; + } else if ((c >= '!') && (c <= '/')) { + sb += (wchar_t)'\1'; + sb += (wchar_t)(c - 33); + return 2; + } else if ((c >= ':') && (c <= '@')) { + sb += (wchar_t)'\1'; + sb += (wchar_t)(c - 58 + 15); + return 2; + } else if ((c >= '[') && (c <= '_')) { + sb += (wchar_t)'\1'; + sb += (wchar_t)(c - 91 + 22); + return 2; + } else if ((c >= 60) && (c <= 0x7f)) { + sb += (wchar_t)'\2'; + sb += (wchar_t)(c - 96); + return 2; + } else if (c >= 80) { + sb += (wchar_t)'\1'; + sb += (wchar_t)0x001e; + int32_t len = 2; + len += encodeChar((c - 128), sb, e); + if (e != BCExceptionNO) + return 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--; + wchar_t c = context.getCurrentChar(); + lastCharSize = encodeChar(c, removed, e); + if (e != BCExceptionNO) + return -1; + context.resetSymbolInfo(); + return lastCharSize; +} +CFX_WideString CBC_C40Encoder::encodeToCodewords(CFX_WideString sb, + int32_t startPos) { + wchar_t c1 = sb.GetAt(startPos); + wchar_t c2 = sb.GetAt(startPos + 1); + wchar_t c3 = sb.GetAt(startPos + 2); + int32_t v = (1600 * c1) + (40 * c2) + c3 + 1; + wchar_t cw1 = (wchar_t)(v / 256); + wchar_t cw2 = (wchar_t)(v % 256); + CFX_WideString b1(cw1); + CFX_WideString b2(cw2); + return b1 + b2; +} diff --git a/fxbarcode/datamatrix/BC_C40Encoder.h b/fxbarcode/datamatrix/BC_C40Encoder.h new file mode 100644 index 0000000000..217404913e --- /dev/null +++ b/fxbarcode/datamatrix/BC_C40Encoder.h @@ -0,0 +1,38 @@ +// 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 FXBARCODE_DATAMATRIX_BC_C40ENCODER_H_ +#define FXBARCODE_DATAMATRIX_BC_C40ENCODER_H_ + +#include "fxbarcode/datamatrix/BC_Encoder.h" + +class CBC_C40Encoder : public CBC_Encoder { + public: + CBC_C40Encoder(); + ~CBC_C40Encoder() override; + + // CBC_Encoder + int32_t getEncodingMode() override; + void Encode(CBC_EncoderContext& context, int32_t& e) override; + + 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(wchar_t 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 // FXBARCODE_DATAMATRIX_BC_C40ENCODER_H_ diff --git a/fxbarcode/datamatrix/BC_DataMatrixSymbolInfo144.cpp b/fxbarcode/datamatrix/BC_DataMatrixSymbolInfo144.cpp new file mode 100644 index 0000000000..d0ccfc2f1e --- /dev/null +++ b/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 "fxbarcode/datamatrix/BC_DataMatrixSymbolInfo144.h" +#include "fxbarcode/datamatrix/BC_Encoder.h" +#include "fxbarcode/datamatrix/BC_SymbolInfo.h" +#include "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/fxbarcode/datamatrix/BC_DataMatrixSymbolInfo144.h b/fxbarcode/datamatrix/BC_DataMatrixSymbolInfo144.h new file mode 100644 index 0000000000..18ef979141 --- /dev/null +++ b/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 FXBARCODE_DATAMATRIX_BC_DATAMATRIXSYMBOLINFO144_H_ +#define FXBARCODE_DATAMATRIX_BC_DATAMATRIXSYMBOLINFO144_H_ + +#include "fxbarcode/datamatrix/BC_SymbolInfo.h" + +class CBC_DataMatrixSymbolInfo144 : public CBC_SymbolInfo { + public: + CBC_DataMatrixSymbolInfo144(); + ~CBC_DataMatrixSymbolInfo144() override; + + int32_t getInterleavedBlockCount(); +}; + +#endif // FXBARCODE_DATAMATRIX_BC_DATAMATRIXSYMBOLINFO144_H_ diff --git a/fxbarcode/datamatrix/BC_DataMatrixWriter.cpp b/fxbarcode/datamatrix/BC_DataMatrixWriter.cpp new file mode 100644 index 0000000000..3b1b9641d1 --- /dev/null +++ b/fxbarcode/datamatrix/BC_DataMatrixWriter.cpp @@ -0,0 +1,143 @@ +// 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 "fxbarcode/BC_Dimension.h" +#include "fxbarcode/BC_TwoDimWriter.h" +#include "fxbarcode/BC_UtilCodingConvert.h" +#include "fxbarcode/BC_Writer.h" +#include "fxbarcode/common/BC_CommonBitMatrix.h" +#include "fxbarcode/common/BC_CommonByteMatrix.h" +#include "fxbarcode/datamatrix/BC_ASCIIEncoder.h" +#include "fxbarcode/datamatrix/BC_Base256Encoder.h" +#include "fxbarcode/datamatrix/BC_C40Encoder.h" +#include "fxbarcode/datamatrix/BC_DataMatrixSymbolInfo144.h" +#include "fxbarcode/datamatrix/BC_DataMatrixWriter.h" +#include "fxbarcode/datamatrix/BC_DefaultPlacement.h" +#include "fxbarcode/datamatrix/BC_EdifactEncoder.h" +#include "fxbarcode/datamatrix/BC_Encoder.h" +#include "fxbarcode/datamatrix/BC_EncoderContext.h" +#include "fxbarcode/datamatrix/BC_ErrorCorrection.h" +#include "fxbarcode/datamatrix/BC_HighLevelEncoder.h" +#include "fxbarcode/datamatrix/BC_SymbolInfo.h" +#include "fxbarcode/datamatrix/BC_SymbolShapeHint.h" +#include "fxbarcode/datamatrix/BC_TextEncoder.h" +#include "fxbarcode/datamatrix/BC_X12Encoder.h" + +CBC_DataMatrixWriter::CBC_DataMatrixWriter() {} +CBC_DataMatrixWriter::~CBC_DataMatrixWriter() {} +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; + return nullptr; + } + CBC_SymbolShapeHint::SymbolShapeHint shape = + CBC_SymbolShapeHint::FORCE_SQUARE; + CBC_Dimension* minSize = nullptr; + CBC_Dimension* maxSize = nullptr; + CFX_WideString ecLevel; + CFX_WideString encoded = CBC_HighLevelEncoder::encodeHighLevel( + contents, ecLevel, shape, minSize, maxSize, e); + if (e != BCExceptionNO) + return nullptr; + CBC_SymbolInfo* symbolInfo = CBC_SymbolInfo::lookup( + encoded.GetLength(), shape, minSize, maxSize, true, e); + if (e != BCExceptionNO) + return nullptr; + CFX_WideString codewords = + CBC_ErrorCorrection::encodeECC200(encoded, symbolInfo, e); + if (e != BCExceptionNO) + return nullptr; + CBC_DefaultPlacement* placement = + new CBC_DefaultPlacement(codewords, symbolInfo->getSymbolDataWidth(e), + symbolInfo->getSymbolDataHeight(e)); + if (e != BCExceptionNO) + return nullptr; + placement->place(); + CBC_CommonByteMatrix* bytematrix = encodeLowLevel(placement, symbolInfo, e); + if (e != BCExceptionNO) + return nullptr; + 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); + if (e != BCExceptionNO) + return nullptr; + int32_t symbolHeight = symbolInfo->getSymbolDataHeight(e); + if (e != BCExceptionNO) + return nullptr; + CBC_CommonByteMatrix* matrix = new CBC_CommonByteMatrix( + symbolInfo->getSymbolWidth(e), symbolInfo->getSymbolHeight(e)); + if (e != BCExceptionNO) + return nullptr; + 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/fxbarcode/datamatrix/BC_DataMatrixWriter.h b/fxbarcode/datamatrix/BC_DataMatrixWriter.h new file mode 100644 index 0000000000..cf5f6e4c4d --- /dev/null +++ b/fxbarcode/datamatrix/BC_DataMatrixWriter.h @@ -0,0 +1,36 @@ +// 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 FXBARCODE_DATAMATRIX_BC_DATAMATRIXWRITER_H_ +#define FXBARCODE_DATAMATRIX_BC_DATAMATRIXWRITER_H_ + +#include "fxbarcode/BC_TwoDimWriter.h" + +class CBC_CommonByteMatrix; +class CBC_DefaultPlacement; +class CBC_SymbolInfo; + +class CBC_DataMatrixWriter : public CBC_TwoDimWriter { + public: + CBC_DataMatrixWriter(); + ~CBC_DataMatrixWriter() override; + + virtual uint8_t* Encode(const CFX_WideString& contents, + int32_t& outWidth, + int32_t& outHeight, + int32_t& e); + + // CBC_TwoDimWriter + bool SetErrorCorrectionLevel(int32_t level) override; + + private: + static CBC_CommonByteMatrix* encodeLowLevel(CBC_DefaultPlacement* placement, + CBC_SymbolInfo* symbolInfo, + int32_t& e); + int32_t m_iCorrectLevel; +}; + +#endif // FXBARCODE_DATAMATRIX_BC_DATAMATRIXWRITER_H_ diff --git a/fxbarcode/datamatrix/BC_DefaultPlacement.cpp b/fxbarcode/datamatrix/BC_DefaultPlacement.cpp new file mode 100644 index 0000000000..405b4f72a1 --- /dev/null +++ b/fxbarcode/datamatrix/BC_DefaultPlacement.cpp @@ -0,0 +1,163 @@ +// 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 "fxbarcode/datamatrix/BC_DefaultPlacement.h" +#include "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.resize(numcols * numrows); + for (int32_t i = 0; i < numcols * numrows; i++) { + m_bits[i] = (uint8_t)2; + } +} +CBC_DefaultPlacement::~CBC_DefaultPlacement() {} + +int32_t CBC_DefaultPlacement::getNumrows() { + return m_numrows; +} +int32_t CBC_DefaultPlacement::getNumcols() { + return m_numcols; +} +std::vector<uint8_t>& CBC_DefaultPlacement::getBits() { + return m_bits; +} +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, bool bit) { + m_bits[row * m_numcols + col] = bit ? (uint8_t)1 : (uint8_t)0; +} +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/fxbarcode/datamatrix/BC_DefaultPlacement.h b/fxbarcode/datamatrix/BC_DefaultPlacement.h new file mode 100644 index 0000000000..5036b121e5 --- /dev/null +++ b/fxbarcode/datamatrix/BC_DefaultPlacement.h @@ -0,0 +1,42 @@ +// 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 FXBARCODE_DATAMATRIX_BC_DEFAULTPLACEMENT_H_ +#define FXBARCODE_DATAMATRIX_BC_DEFAULTPLACEMENT_H_ + +#include <vector> + +#include "core/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(); + std::vector<uint8_t>& getBits(); + bool getBit(int32_t col, int32_t row); + void setBit(int32_t col, int32_t row, bool bit); + bool hasBit(int32_t col, int32_t row); + void place(); + + private: + CFX_WideString m_codewords; + int32_t m_numrows; + int32_t m_numcols; + std::vector<uint8_t> 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 // FXBARCODE_DATAMATRIX_BC_DEFAULTPLACEMENT_H_ diff --git a/fxbarcode/datamatrix/BC_EdifactEncoder.cpp b/fxbarcode/datamatrix/BC_EdifactEncoder.cpp new file mode 100644 index 0000000000..61eea93d33 --- /dev/null +++ b/fxbarcode/datamatrix/BC_EdifactEncoder.cpp @@ -0,0 +1,152 @@ +// 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 "fxbarcode/BC_Dimension.h" +#include "fxbarcode/common/BC_CommonBitMatrix.h" +#include "fxbarcode/datamatrix/BC_EdifactEncoder.h" +#include "fxbarcode/datamatrix/BC_Encoder.h" +#include "fxbarcode/datamatrix/BC_EncoderContext.h" +#include "fxbarcode/datamatrix/BC_HighLevelEncoder.h" +#include "fxbarcode/datamatrix/BC_SymbolInfo.h" +#include "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()) { + wchar_t 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 += (wchar_t)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; + } + bool endOfSymbolReached = !context.hasMoreCharacters(); + 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(wchar_t c, CFX_WideString& sb, int32_t& e) { + if (c >= ' ' && c <= '?') { + sb += c; + } else if (c >= '@' && c <= '^') { + sb += (wchar_t)(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 CFX_WideString(); + } + wchar_t c1 = sb.GetAt(startPos); + wchar_t c2 = len >= 2 ? sb.GetAt(startPos + 1) : 0; + wchar_t c3 = len >= 3 ? sb.GetAt(startPos + 2) : 0; + wchar_t c4 = len >= 4 ? sb.GetAt(startPos + 3) : 0; + int32_t v = (c1 << 18) + (c2 << 12) + (c3 << 6) + c4; + wchar_t cw1 = (wchar_t)((v >> 16) & 255); + wchar_t cw2 = (wchar_t)((v >> 8) & 255); + wchar_t cw3 = (wchar_t)(v & 255); + CFX_WideString res; + res += cw1; + if (len >= 2) { + res += cw2; + } + if (len >= 3) { + res += cw3; + } + return res; +} diff --git a/fxbarcode/datamatrix/BC_EdifactEncoder.h b/fxbarcode/datamatrix/BC_EdifactEncoder.h new file mode 100644 index 0000000000..6fe0934de1 --- /dev/null +++ b/fxbarcode/datamatrix/BC_EdifactEncoder.h @@ -0,0 +1,31 @@ +// 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 FXBARCODE_DATAMATRIX_BC_EDIFACTENCODER_H_ +#define FXBARCODE_DATAMATRIX_BC_EDIFACTENCODER_H_ + +#include "fxbarcode/datamatrix/BC_Encoder.h" + +class CBC_EdifactEncoder : public CBC_Encoder { + public: + CBC_EdifactEncoder(); + ~CBC_EdifactEncoder() override; + + // CBC_Encoder + int32_t getEncodingMode() override; + void Encode(CBC_EncoderContext& context, int32_t& e) override; + + private: + static void handleEOD(CBC_EncoderContext& context, + CFX_WideString buffer, + int32_t& e); + static void encodeChar(wchar_t c, CFX_WideString& sb, int32_t& e); + static CFX_WideString encodeToCodewords(CFX_WideString sb, + int32_t startPos, + int32_t& e); +}; + +#endif // FXBARCODE_DATAMATRIX_BC_EDIFACTENCODER_H_ diff --git a/fxbarcode/datamatrix/BC_Encoder.cpp b/fxbarcode/datamatrix/BC_Encoder.cpp new file mode 100644 index 0000000000..9403904f32 --- /dev/null +++ b/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 "fxbarcode/datamatrix/BC_Encoder.h" + +CBC_Encoder::CBC_Encoder() {} +CBC_Encoder::~CBC_Encoder() {} diff --git a/fxbarcode/datamatrix/BC_Encoder.h b/fxbarcode/datamatrix/BC_Encoder.h new file mode 100644 index 0000000000..40d491c2cd --- /dev/null +++ b/fxbarcode/datamatrix/BC_Encoder.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 FXBARCODE_DATAMATRIX_BC_ENCODER_H_ +#define FXBARCODE_DATAMATRIX_BC_ENCODER_H_ + +#include "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 // FXBARCODE_DATAMATRIX_BC_ENCODER_H_ diff --git a/fxbarcode/datamatrix/BC_EncoderContext.cpp b/fxbarcode/datamatrix/BC_EncoderContext.cpp new file mode 100644 index 0000000000..8fe5bf5b26 --- /dev/null +++ b/fxbarcode/datamatrix/BC_EncoderContext.cpp @@ -0,0 +1,112 @@ +// 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 "fxbarcode/BC_Dimension.h" +#include "fxbarcode/BC_UtilCodingConvert.h" +#include "fxbarcode/common/BC_CommonBitMatrix.h" +#include "fxbarcode/datamatrix/BC_Encoder.h" +#include "fxbarcode/datamatrix/BC_EncoderContext.h" +#include "fxbarcode/datamatrix/BC_SymbolInfo.h" +#include "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++) { + wchar_t ch = (wchar_t)(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 = nullptr; + m_skipAtEnd = 0; + m_maxSize = nullptr; + m_minSize = nullptr; +} +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; +} +wchar_t CBC_EncoderContext::getCurrentChar() { + return m_msg.GetAt(m_pos); +} +wchar_t CBC_EncoderContext::getCurrent() { + return m_msg.GetAt(m_pos); +} +void CBC_EncoderContext::writeCodewords(CFX_WideString codewords) { + m_codewords += codewords; +} +void CBC_EncoderContext::writeCodeword(wchar_t 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; +} +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 || len > m_symbolInfo->m_dataCapacity) { + m_symbolInfo = + CBC_SymbolInfo::lookup(len, m_shape, m_minSize, m_maxSize, true, e); + if (e != BCExceptionNO) + return; + } +} +void CBC_EncoderContext::resetSymbolInfo() { + m_shape = FORCE_NONE; +} +int32_t CBC_EncoderContext::getTotalMessageCharCount() { + return m_msg.GetLength() - m_skipAtEnd; +} diff --git a/fxbarcode/datamatrix/BC_EncoderContext.h b/fxbarcode/datamatrix/BC_EncoderContext.h new file mode 100644 index 0000000000..6be824116f --- /dev/null +++ b/fxbarcode/datamatrix/BC_EncoderContext.h @@ -0,0 +1,56 @@ +// 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 FXBARCODE_DATAMATRIX_BC_ENCODERCONTEXT_H_ +#define FXBARCODE_DATAMATRIX_BC_ENCODERCONTEXT_H_ + +#include "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); + ~CBC_EncoderContext() override; + + void setSymbolShape(SymbolShapeHint shape); + void setSizeConstraints(CBC_Dimension* minSize, CBC_Dimension* maxSize); + CFX_WideString getMessage(); + void setSkipAtEnd(int32_t count); + wchar_t getCurrentChar(); + wchar_t getCurrent(); + void writeCodewords(CFX_WideString codewords); + void writeCodeword(wchar_t codeword); + int32_t getCodewordCount(); + void signalEncoderChange(int32_t encoding); + void resetEncoderSignal(); + 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 // FXBARCODE_DATAMATRIX_BC_ENCODERCONTEXT_H_ diff --git a/fxbarcode/datamatrix/BC_ErrorCorrection.cpp b/fxbarcode/datamatrix/BC_ErrorCorrection.cpp new file mode 100644 index 0000000000..72ce8fcdf3 --- /dev/null +++ b/fxbarcode/datamatrix/BC_ErrorCorrection.cpp @@ -0,0 +1,209 @@ +// 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 <vector> + +#include "fxbarcode/datamatrix/BC_Encoder.h" +#include "fxbarcode/datamatrix/BC_ErrorCorrection.h" +#include "fxbarcode/datamatrix/BC_SymbolInfo.h" +#include "fxbarcode/datamatrix/BC_SymbolShapeHint.h" + +namespace { + +const uint8_t FACTOR_SETS[] = {5, 7, 10, 11, 12, 14, 18, 20, + 24, 28, 36, 42, 48, 56, 62, 68}; + +const uint8_t FACTORS_0[5] = {228, 48, 15, 111, 62}; +const uint8_t FACTORS_1[7] = {23, 68, 144, 134, 240, 92, 254}; +const uint8_t FACTORS_2[10] = {28, 24, 185, 166, 223, 248, 116, 255, 110, 61}; +const uint8_t FACTORS_3[11] = {175, 138, 205, 12, 194, 168, + 39, 245, 60, 97, 120}; + +const uint8_t FACTORS_4[12] = {41, 153, 158, 91, 61, 42, + 142, 213, 97, 178, 100, 242}; + +const uint8_t FACTORS_5[14] = {156, 97, 192, 252, 95, 9, 157, + 119, 138, 45, 18, 186, 83, 185}; + +const uint8_t FACTORS_6[18] = {83, 195, 100, 39, 188, 75, 66, 61, 241, + 213, 109, 129, 94, 254, 225, 48, 90, 188}; + +const uint8_t FACTORS_7[20] = {15, 195, 244, 9, 233, 71, 168, 2, 188, 160, + 153, 145, 253, 79, 108, 82, 27, 174, 186, 172}; + +const uint8_t FACTORS_8[24] = {52, 190, 88, 205, 109, 39, 176, 21, + 155, 197, 251, 223, 155, 21, 5, 172, + 254, 124, 12, 181, 184, 96, 50, 193}; + +const uint8_t FACTORS_9[28] = {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}; + +const uint8_t FACTORS_10[36] = {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}; + +const uint8_t FACTORS_11[42] = { + 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}; + +const uint8_t FACTORS_12[48] = { + 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}; + +const uint8_t FACTORS_13[56] = { + 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}; + +const uint8_t FACTORS_14[62] = { + 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}; + +const uint8_t FACTORS_15[68] = { + 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}; + +const uint8_t* const FACTORS[16] = { + FACTORS_0, FACTORS_1, FACTORS_2, FACTORS_3, FACTORS_4, FACTORS_5, + FACTORS_6, FACTORS_7, FACTORS_8, FACTORS_9, FACTORS_10, FACTORS_11, + FACTORS_12, FACTORS_13, FACTORS_14, FACTORS_15}; + +} // namespace + +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 CFX_WideString(); + } + CFX_WideString sb; + sb += codewords; + int32_t blockCount = symbolInfo->getInterleavedBlockCount(); + if (blockCount == 1) { + CFX_WideString ecc = + createECCBlock(codewords, symbolInfo->m_errorCodewords, e); + if (e != BCExceptionNO) + return CFX_WideString(); + sb += ecc; + } else { + std::vector<int32_t> dataSizes(blockCount); + std::vector<int32_t> errorSizes(blockCount); + std::vector<int32_t> startPos(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 += (wchar_t)codewords.GetAt(d); + } + CFX_WideString ecc = createECCBlock(temp, errorSizes[block], e); + if (e != BCExceptionNO) + return CFX_WideString(); + 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) { + static const size_t kFactorTableNum = sizeof(FACTOR_SETS) / sizeof(int32_t); + size_t table = 0; + while (table < kFactorTableNum && FACTOR_SETS[table] != numECWords) + table++; + + if (table >= kFactorTableNum) { + e = BCExceptionIllegalArgument; + return CFX_WideString(); + } + uint16_t* ecc = FX_Alloc(uint16_t, numECWords); + FXSYS_memset(ecc, 0, numECWords * sizeof(uint16_t)); + for (int32_t l = start; l < start + len; l++) { + uint16_t 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] = (uint16_t)(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] = (uint16_t)ALOG[(LOG[m] + LOG[FACTORS[table][0]]) % 255]; + } else { + ecc[0] = 0; + } + } + CFX_WideString strecc; + for (int32_t j = 0; j < numECWords; j++) { + strecc += (wchar_t)ecc[numECWords - j - 1]; + } + FX_Free(ecc); + return strecc; +} diff --git a/fxbarcode/datamatrix/BC_ErrorCorrection.h b/fxbarcode/datamatrix/BC_ErrorCorrection.h new file mode 100644 index 0000000000..f09763da2b --- /dev/null +++ b/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 FXBARCODE_DATAMATRIX_BC_ERRORCORRECTION_H_ +#define 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 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 // FXBARCODE_DATAMATRIX_BC_ERRORCORRECTION_H_ diff --git a/fxbarcode/datamatrix/BC_HighLevelEncoder.cpp b/fxbarcode/datamatrix/BC_HighLevelEncoder.cpp new file mode 100644 index 0000000000..e2d483cbdf --- /dev/null +++ b/fxbarcode/datamatrix/BC_HighLevelEncoder.cpp @@ -0,0 +1,360 @@ +// 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 <limits> +#include <memory> +#include <vector> + +#include "fxbarcode/BC_Dimension.h" +#include "fxbarcode/BC_UtilCodingConvert.h" +#include "fxbarcode/common/BC_CommonBitMatrix.h" +#include "fxbarcode/datamatrix/BC_ASCIIEncoder.h" +#include "fxbarcode/datamatrix/BC_Base256Encoder.h" +#include "fxbarcode/datamatrix/BC_C40Encoder.h" +#include "fxbarcode/datamatrix/BC_EdifactEncoder.h" +#include "fxbarcode/datamatrix/BC_Encoder.h" +#include "fxbarcode/datamatrix/BC_EncoderContext.h" +#include "fxbarcode/datamatrix/BC_HighLevelEncoder.h" +#include "fxbarcode/datamatrix/BC_SymbolInfo.h" +#include "fxbarcode/datamatrix/BC_SymbolShapeHint.h" +#include "fxbarcode/datamatrix/BC_TextEncoder.h" +#include "fxbarcode/datamatrix/BC_X12Encoder.h" +#include "fxbarcode/utils.h" + +wchar_t CBC_HighLevelEncoder::LATCH_TO_C40 = 230; +wchar_t CBC_HighLevelEncoder::LATCH_TO_BASE256 = 231; +wchar_t CBC_HighLevelEncoder::UPPER_SHIFT = 235; +wchar_t CBC_HighLevelEncoder::LATCH_TO_ANSIX12 = 238; +wchar_t CBC_HighLevelEncoder::LATCH_TO_TEXT = 239; +wchar_t CBC_HighLevelEncoder::LATCH_TO_EDIFACT = 240; +wchar_t CBC_HighLevelEncoder::C40_UNLATCH = 254; +wchar_t CBC_HighLevelEncoder::X12_UNLATCH = 254; +wchar_t CBC_HighLevelEncoder::PAD = 129; +wchar_t CBC_HighLevelEncoder::MACRO_05 = 236; +wchar_t 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() {} + +std::vector<uint8_t>& 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.push_back(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, nullptr, nullptr, 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); + if (e != BCExceptionNO) + return CFX_WideString(); + 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; + } + + std::vector<std::unique_ptr<CBC_Encoder>> encoders; + encoders.push_back(std::unique_ptr<CBC_Encoder>(new CBC_ASCIIEncoder())); + encoders.push_back(std::unique_ptr<CBC_Encoder>(new CBC_C40Encoder())); + encoders.push_back(std::unique_ptr<CBC_Encoder>(new CBC_TextEncoder())); + encoders.push_back(std::unique_ptr<CBC_Encoder>(new CBC_X12Encoder())); + encoders.push_back(std::unique_ptr<CBC_Encoder>(new CBC_EdifactEncoder())); + encoders.push_back(std::unique_ptr<CBC_Encoder>(new CBC_Base256Encoder())); + int32_t encodingMode = ASCII_ENCODATION; + while (context.hasMoreCharacters()) { + encoders[encodingMode]->Encode(context, e); + if (e != BCExceptionNO) + return L""; + + if (context.m_newEncoding >= 0) { + encodingMode = context.m_newEncoding; + context.resetEncoderSignal(); + } + } + int32_t len = context.m_codewords.GetLength(); + context.updateSymbolInfo(e); + if (e != BCExceptionNO) + return L""; + + 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)); + } + return codewords; +} +int32_t CBC_HighLevelEncoder::lookAheadTest(CFX_WideString msg, + int32_t startpos, + int32_t currentMode) { + if (startpos >= msg.GetLength()) { + return currentMode; + } + std::vector<float> charCounts; + if (currentMode == ASCII_ENCODATION) { + charCounts.push_back(0); + charCounts.push_back(1); + charCounts.push_back(1); + charCounts.push_back(1); + charCounts.push_back(1); + charCounts.push_back(1.25f); + } else { + charCounts.push_back(1); + charCounts.push_back(2); + charCounts.push_back(2); + charCounts.push_back(2); + charCounts.push_back(2); + charCounts.push_back(2.25f); + charCounts[currentMode] = 0; + } + int32_t charsProcessed = 0; + while (true) { + if ((startpos + charsProcessed) == msg.GetLength()) { + int32_t min = std::numeric_limits<int32_t>::max(); + std::vector<uint8_t> mins(6); + std::vector<int32_t> intCharCounts(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; + } + wchar_t c = msg.GetAt(startpos + charsProcessed); + charsProcessed++; + if (isDigit(c)) { + charCounts[ASCII_ENCODATION] += 0.5; + } else if (isExtendedASCII(c)) { + charCounts[ASCII_ENCODATION] = (float)ceil(charCounts[ASCII_ENCODATION]); + charCounts[ASCII_ENCODATION] += 2; + } else { + charCounts[ASCII_ENCODATION] = (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) { + std::vector<int32_t> intCharCounts(6); + std::vector<uint8_t> mins(6); + findMinimums(charCounts, intCharCounts, + std::numeric_limits<int32_t>::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()) { + wchar_t tc = msg.GetAt(p); + if (isX12TermSep(tc)) { + return X12_ENCODATION; + } + if (!isNativeX12(tc)) { + break; + } + p++; + } + return C40_ENCODATION; + } + } + } + } +} +bool CBC_HighLevelEncoder::isDigit(wchar_t ch) { + return ch >= '0' && ch <= '9'; +} +bool CBC_HighLevelEncoder::isExtendedASCII(wchar_t 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) { + wchar_t ch = msg.GetAt(idx); + while (isDigit(ch) && idx < len) { + count++; + idx++; + if (idx < len) { + ch = msg.GetAt(idx); + } + } + } + return count; +} +void CBC_HighLevelEncoder::illegalCharacter(wchar_t c, int32_t& e) { + e = BCExceptionIllegalArgument; +} +wchar_t CBC_HighLevelEncoder::randomize253State(wchar_t ch, + int32_t codewordPosition) { + int32_t pseudoRandom = ((149 * codewordPosition) % 253) + 1; + int32_t tempVariable = ch + pseudoRandom; + return tempVariable <= 254 ? (wchar_t)tempVariable + : (wchar_t)(tempVariable - 254); +} +int32_t CBC_HighLevelEncoder::findMinimums(std::vector<float>& charCounts, + std::vector<int32_t>& intCharCounts, + int32_t min, + std::vector<uint8_t>& mins) { + for (size_t l = 0; l < mins.size(); l++) + mins[l] = 0; + + for (size_t i = 0; i < 6; i++) { + intCharCounts[i] = static_cast<int32_t>(ceil(charCounts[i])); + int32_t current = intCharCounts[i]; + if (min > current) { + min = current; + for (size_t j = 0; j < mins.size(); j++) + mins[j] = 0; + } + if (min == current) + mins[i]++; + } + return min; +} +int32_t CBC_HighLevelEncoder::getMinimumCount(std::vector<uint8_t>& mins) { + int32_t minCount = 0; + for (int32_t i = 0; i < 6; i++) { + minCount += mins[i]; + } + return minCount; +} +bool CBC_HighLevelEncoder::isNativeC40(wchar_t ch) { + return (ch == ' ') || (ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z'); +} +bool CBC_HighLevelEncoder::isNativeText(wchar_t ch) { + return (ch == ' ') || (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z'); +} +bool CBC_HighLevelEncoder::isNativeX12(wchar_t ch) { + return isX12TermSep(ch) || (ch == ' ') || (ch >= '0' && ch <= '9') || + (ch >= 'A' && ch <= 'Z'); +} +bool CBC_HighLevelEncoder::isX12TermSep(wchar_t ch) { + return (ch == '\r') || (ch == '*') || (ch == '>'); +} +bool CBC_HighLevelEncoder::isNativeEDIFACT(wchar_t ch) { + return ch >= ' ' && ch <= '^'; +} +bool CBC_HighLevelEncoder::isSpecialB256(wchar_t ch) { + return false; +} diff --git a/fxbarcode/datamatrix/BC_HighLevelEncoder.h b/fxbarcode/datamatrix/BC_HighLevelEncoder.h new file mode 100644 index 0000000000..83136be2f4 --- /dev/null +++ b/fxbarcode/datamatrix/BC_HighLevelEncoder.h @@ -0,0 +1,79 @@ +// 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 FXBARCODE_DATAMATRIX_BC_HIGHLEVELENCODER_H_ +#define FXBARCODE_DATAMATRIX_BC_HIGHLEVELENCODER_H_ + +#include <vector> + +#include "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(); + ~CBC_HighLevelEncoder() override; + + std::vector<uint8_t>& 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 bool isDigit(wchar_t ch); + static bool isExtendedASCII(wchar_t ch); + static int32_t determineConsecutiveDigitCount(CFX_WideString msg, + int32_t startpos); + static void illegalCharacter(wchar_t c, int32_t& e); + + public: + static wchar_t LATCH_TO_C40; + static wchar_t LATCH_TO_BASE256; + static wchar_t UPPER_SHIFT; + static wchar_t LATCH_TO_ANSIX12; + static wchar_t LATCH_TO_TEXT; + static wchar_t LATCH_TO_EDIFACT; + static wchar_t C40_UNLATCH; + static wchar_t X12_UNLATCH; + + private: + static wchar_t PAD; + static wchar_t MACRO_05; + static wchar_t MACRO_06; + static const wchar_t* MACRO_05_HEADER; + static const wchar_t* MACRO_06_HEADER; + static const wchar_t MACRO_TRAILER; + std::vector<uint8_t> m_bytearray; + + private: + static wchar_t randomize253State(wchar_t ch, int32_t codewordPosition); + static int32_t findMinimums(std::vector<float>& charCounts, + std::vector<int32_t>& intCharCounts, + int32_t min, + std::vector<uint8_t>& mins); + static int32_t getMinimumCount(std::vector<uint8_t>& mins); + static bool isNativeC40(wchar_t ch); + static bool isNativeText(wchar_t ch); + static bool isNativeX12(wchar_t ch); + static bool isX12TermSep(wchar_t ch); + static bool isNativeEDIFACT(wchar_t ch); + static bool isSpecialB256(wchar_t ch); +}; + +#endif // FXBARCODE_DATAMATRIX_BC_HIGHLEVELENCODER_H_ diff --git a/fxbarcode/datamatrix/BC_SymbolInfo.cpp b/fxbarcode/datamatrix/BC_SymbolInfo.cpp new file mode 100644 index 0000000000..f98370b90f --- /dev/null +++ b/fxbarcode/datamatrix/BC_SymbolInfo.cpp @@ -0,0 +1,230 @@ +// 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 "fxbarcode/datamatrix/BC_SymbolInfo.h" + +#include "fxbarcode/BC_Dimension.h" +#include "fxbarcode/common/BC_CommonBitMatrix.h" +#include "fxbarcode/datamatrix/BC_DataMatrixSymbolInfo144.h" +#include "fxbarcode/datamatrix/BC_Encoder.h" +#include "fxbarcode/datamatrix/BC_SymbolShapeHint.h" + +namespace { + +const size_t kSymbolsCount = 30; + +CBC_SymbolInfo* g_symbols[kSymbolsCount] = { + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr}; + +} // namespace + +void CBC_SymbolInfo::Initialize() { + g_symbols[0] = new CBC_SymbolInfo(false, 3, 5, 8, 8, 1); + g_symbols[1] = new CBC_SymbolInfo(false, 5, 7, 10, 10, 1); + g_symbols[2] = new CBC_SymbolInfo(true, 5, 7, 16, 6, 1); + g_symbols[3] = new CBC_SymbolInfo(false, 8, 10, 12, 12, 1); + g_symbols[4] = new CBC_SymbolInfo(true, 10, 11, 14, 6, 2); + g_symbols[5] = new CBC_SymbolInfo(false, 12, 12, 14, 14, 1); + g_symbols[6] = new CBC_SymbolInfo(true, 16, 14, 24, 10, 1); + g_symbols[7] = new CBC_SymbolInfo(false, 18, 14, 16, 16, 1); + g_symbols[8] = new CBC_SymbolInfo(false, 22, 18, 18, 18, 1); + g_symbols[9] = new CBC_SymbolInfo(true, 22, 18, 16, 10, 2); + g_symbols[10] = new CBC_SymbolInfo(false, 30, 20, 20, 20, 1); + g_symbols[11] = new CBC_SymbolInfo(true, 32, 24, 16, 14, 2); + g_symbols[12] = new CBC_SymbolInfo(false, 36, 24, 22, 22, 1); + g_symbols[13] = new CBC_SymbolInfo(false, 44, 28, 24, 24, 1); + g_symbols[14] = new CBC_SymbolInfo(true, 49, 28, 22, 14, 2); + g_symbols[15] = new CBC_SymbolInfo(false, 62, 36, 14, 14, 4); + g_symbols[16] = new CBC_SymbolInfo(false, 86, 42, 16, 16, 4); + g_symbols[17] = new CBC_SymbolInfo(false, 114, 48, 18, 18, 4); + g_symbols[18] = new CBC_SymbolInfo(false, 144, 56, 20, 20, 4); + g_symbols[19] = new CBC_SymbolInfo(false, 174, 68, 22, 22, 4); + g_symbols[20] = new CBC_SymbolInfo(false, 204, 84, 24, 24, 4, 102, 42); + g_symbols[21] = new CBC_SymbolInfo(false, 280, 112, 14, 14, 16, 140, 56); + g_symbols[22] = new CBC_SymbolInfo(false, 368, 144, 16, 16, 16, 92, 36); + g_symbols[23] = new CBC_SymbolInfo(false, 456, 192, 18, 18, 16, 114, 48); + g_symbols[24] = new CBC_SymbolInfo(false, 576, 224, 20, 20, 16, 144, 56); + g_symbols[25] = new CBC_SymbolInfo(false, 696, 272, 22, 22, 16, 174, 68); + g_symbols[26] = new CBC_SymbolInfo(false, 816, 336, 24, 24, 16, 136, 56); + g_symbols[27] = new CBC_SymbolInfo(false, 1050, 408, 18, 18, 36, 175, 68); + g_symbols[28] = new CBC_SymbolInfo(false, 1304, 496, 20, 20, 36, 163, 62); + g_symbols[29] = new CBC_DataMatrixSymbolInfo144(); +} + +void CBC_SymbolInfo::Finalize() { + for (size_t i = 0; i < kSymbolsCount; i++) { + delete g_symbols[i]; + g_symbols[i] = nullptr; + } +} + +CBC_SymbolInfo::CBC_SymbolInfo(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(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, + bool allowRectangular, + 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, + bool fail, + int32_t& e) { + return lookup(dataCodewords, shape, nullptr, nullptr, fail, e); +} +CBC_SymbolInfo* CBC_SymbolInfo::lookup(int32_t dataCodewords, + SymbolShapeHint shape, + CBC_Dimension* minSize, + CBC_Dimension* maxSize, + bool fail, + int32_t& e) { + for (size_t i = 0; i < kSymbolsCount; i++) { + CBC_SymbolInfo* symbol = g_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())) { + if (e != BCExceptionNO) + return nullptr; + continue; + } + if (maxSize && (symbol->getSymbolWidth(e) > maxSize->getWidth() || + symbol->getSymbolHeight(e) > maxSize->getHeight())) { + if (e != BCExceptionNO) + return nullptr; + continue; + } + if (dataCodewords <= symbol->m_dataCapacity) { + return symbol; + } + } + if (fail) + e = BCExceptionIllegalDataCodewords; + return nullptr; +} + +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; +} diff --git a/fxbarcode/datamatrix/BC_SymbolInfo.h b/fxbarcode/datamatrix/BC_SymbolInfo.h new file mode 100644 index 0000000000..36ed01d7e4 --- /dev/null +++ b/fxbarcode/datamatrix/BC_SymbolInfo.h @@ -0,0 +1,79 @@ +// 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 FXBARCODE_DATAMATRIX_BC_SYMBOLINFO_H_ +#define FXBARCODE_DATAMATRIX_BC_SYMBOLINFO_H_ + +#include "core/fxcrt/fx_string.h" +#include "core/fxcrt/fx_system.h" +#include "fxbarcode/datamatrix/BC_SymbolShapeHint.h" + +class CBC_Dimension; + +class CBC_SymbolInfo : public CBC_SymbolShapeHint { + public: + CBC_SymbolInfo(bool rectangular, + int32_t dataCapacity, + int32_t errorCodewords, + int32_t matrixWidth, + int32_t matrixHeight, + int32_t dataRegions); + ~CBC_SymbolInfo() override; + + 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, + bool allowRectangular, + bool fail, + int32_t& e); + static CBC_SymbolInfo* lookup(int32_t dataCodewords, + SymbolShapeHint shape, + bool fail, + int32_t& e); + static CBC_SymbolInfo* lookup(int32_t dataCodewords, + SymbolShapeHint shape, + CBC_Dimension* minSize, + CBC_Dimension* maxSize, + 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); + + int32_t m_dataCapacity; + int32_t m_errorCodewords; + int32_t m_matrixWidth; + int32_t m_matrixHeight; + int32_t m_rsBlockData; + int32_t m_rsBlockError; + + private: + CBC_SymbolInfo(bool rectangular, + int32_t dataCapacity, + int32_t errorCodewords, + int32_t matrixWidth, + int32_t matrixHeight, + int32_t dataRegions, + int32_t rsBlockData, + int32_t rsBlockError); + + bool m_rectangular; + int32_t m_dataRegions; +}; + +#endif // FXBARCODE_DATAMATRIX_BC_SYMBOLINFO_H_ diff --git a/fxbarcode/datamatrix/BC_SymbolShapeHint.cpp b/fxbarcode/datamatrix/BC_SymbolShapeHint.cpp new file mode 100644 index 0000000000..294580bb5a --- /dev/null +++ b/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 "fxbarcode/datamatrix/BC_SymbolShapeHint.h" + +CBC_SymbolShapeHint::CBC_SymbolShapeHint() {} +CBC_SymbolShapeHint::~CBC_SymbolShapeHint() {} diff --git a/fxbarcode/datamatrix/BC_SymbolShapeHint.h b/fxbarcode/datamatrix/BC_SymbolShapeHint.h new file mode 100644 index 0000000000..409ee05d75 --- /dev/null +++ b/fxbarcode/datamatrix/BC_SymbolShapeHint.h @@ -0,0 +1,22 @@ +// 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 FXBARCODE_DATAMATRIX_BC_SYMBOLSHAPEHINT_H_ +#define FXBARCODE_DATAMATRIX_BC_SYMBOLSHAPEHINT_H_ + +class CBC_SymbolShapeHint { + public: + CBC_SymbolShapeHint(); + virtual ~CBC_SymbolShapeHint(); + + enum SymbolShapeHint { + FORCE_NONE, + FORCE_SQUARE, + FORCE_RECTANGLE, + }; +}; + +#endif // FXBARCODE_DATAMATRIX_BC_SYMBOLSHAPEHINT_H_ diff --git a/fxbarcode/datamatrix/BC_TextEncoder.cpp b/fxbarcode/datamatrix/BC_TextEncoder.cpp new file mode 100644 index 0000000000..a521acb6c3 --- /dev/null +++ b/fxbarcode/datamatrix/BC_TextEncoder.cpp @@ -0,0 +1,97 @@ +// 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 "fxbarcode/BC_Dimension.h" +#include "fxbarcode/common/BC_CommonBitMatrix.h" +#include "fxbarcode/datamatrix/BC_C40Encoder.h" +#include "fxbarcode/datamatrix/BC_Encoder.h" +#include "fxbarcode/datamatrix/BC_EncoderContext.h" +#include "fxbarcode/datamatrix/BC_HighLevelEncoder.h" +#include "fxbarcode/datamatrix/BC_SymbolInfo.h" +#include "fxbarcode/datamatrix/BC_SymbolShapeHint.h" +#include "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(wchar_t c, CFX_WideString& sb, int32_t& e) { + if (c == ' ') { + sb += (wchar_t)'\3'; + return 1; + } + if (c >= '0' && c <= '9') { + sb += (wchar_t)(c - 48 + 4); + return 1; + } + if (c >= 'a' && c <= 'z') { + sb += (wchar_t)(c - 97 + 14); + return 1; + } + if (c <= 0x1f) { + sb += (wchar_t)'\0'; + sb += c; + return 2; + } + if (c >= '!' && c <= '/') { + sb += (wchar_t)'\1'; + sb += (wchar_t)(c - 33); + return 2; + } + if (c >= ':' && c <= '@') { + sb += (wchar_t)'\1'; + sb += (wchar_t)(c - 58 + 15); + return 2; + } + if (c >= '[' && c <= '_') { + sb += (wchar_t)'\1'; + sb += (wchar_t)(c - 91 + 22); + return 2; + } + if (c == 0x0060) { + sb += (wchar_t)'\2'; + sb += (wchar_t)(c - 96); + return 2; + } + if (c >= 'A' && c <= 'Z') { + sb += (wchar_t)'\2'; + sb += (wchar_t)(c - 65 + 1); + return 2; + } + if (c >= '{' && c <= 0x007f) { + sb += (wchar_t)'\2'; + sb += (wchar_t)(c - 123 + 27); + return 2; + } + if (c >= 0x0080) { + sb += (wchar_t)'\1'; + sb += (wchar_t)0x001e; + int32_t len = 2; + len += encodeChar((wchar_t)(c - 128), sb, e); + if (e != BCExceptionNO) + return -1; + return len; + } + CBC_HighLevelEncoder::illegalCharacter(c, e); + return -1; +} diff --git a/fxbarcode/datamatrix/BC_TextEncoder.h b/fxbarcode/datamatrix/BC_TextEncoder.h new file mode 100644 index 0000000000..dde1a7f46e --- /dev/null +++ b/fxbarcode/datamatrix/BC_TextEncoder.h @@ -0,0 +1,22 @@ +// 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 FXBARCODE_DATAMATRIX_BC_TEXTENCODER_H_ +#define FXBARCODE_DATAMATRIX_BC_TEXTENCODER_H_ + +class CBC_TextEncoder; + +class CBC_TextEncoder : public CBC_C40Encoder { + public: + CBC_TextEncoder(); + ~CBC_TextEncoder() override; + + // CBC_C40Encoder + int32_t getEncodingMode() override; + int32_t encodeChar(wchar_t c, CFX_WideString& sb, int32_t& e) override; +}; + +#endif // FXBARCODE_DATAMATRIX_BC_TEXTENCODER_H_ diff --git a/fxbarcode/datamatrix/BC_X12Encoder.cpp b/fxbarcode/datamatrix/BC_X12Encoder.cpp new file mode 100644 index 0000000000..dd8254bff7 --- /dev/null +++ b/fxbarcode/datamatrix/BC_X12Encoder.cpp @@ -0,0 +1,101 @@ +// 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 "fxbarcode/BC_Dimension.h" +#include "fxbarcode/common/BC_CommonBitMatrix.h" +#include "fxbarcode/datamatrix/BC_C40Encoder.h" +#include "fxbarcode/datamatrix/BC_Encoder.h" +#include "fxbarcode/datamatrix/BC_EncoderContext.h" +#include "fxbarcode/datamatrix/BC_HighLevelEncoder.h" +#include "fxbarcode/datamatrix/BC_SymbolInfo.h" +#include "fxbarcode/datamatrix/BC_SymbolShapeHint.h" +#include "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()) { + wchar_t 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(wchar_t c, CFX_WideString& sb, int32_t& e) { + if (c == '\r') { + sb += (wchar_t)'\0'; + } else if (c == '*') { + sb += (wchar_t)'\1'; + } else if (c == '>') { + sb += (wchar_t)'\2'; + } else if (c == ' ') { + sb += (wchar_t)'\3'; + } else if (c >= '0' && c <= '9') { + sb += (wchar_t)(c - 48 + 4); + } else if (c >= 'A' && c <= 'Z') { + sb += (wchar_t)(c - 65 + 14); + } else { + CBC_HighLevelEncoder::illegalCharacter(c, e); + if (e != BCExceptionNO) + return -1; + } + return 1; +} diff --git a/fxbarcode/datamatrix/BC_X12Encoder.h b/fxbarcode/datamatrix/BC_X12Encoder.h new file mode 100644 index 0000000000..a90dd8c7ba --- /dev/null +++ b/fxbarcode/datamatrix/BC_X12Encoder.h @@ -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 + +#ifndef FXBARCODE_DATAMATRIX_BC_X12ENCODER_H_ +#define FXBARCODE_DATAMATRIX_BC_X12ENCODER_H_ + +class CBC_C40Encoder; +class CBC_X12Encoder; +class CBC_X12Encoder : public CBC_C40Encoder { + public: + CBC_X12Encoder(); + ~CBC_X12Encoder() override; + + // CBC_C40Encoder + int32_t getEncodingMode() override; + void Encode(CBC_EncoderContext& context, int32_t& e) override; + void handleEOD(CBC_EncoderContext& context, + CFX_WideString& buffer, + int32_t& e) override; + int32_t encodeChar(wchar_t c, CFX_WideString& sb, int32_t& e) override; +}; + +#endif // FXBARCODE_DATAMATRIX_BC_X12ENCODER_H_ |