diff options
Diffstat (limited to 'xfa/fxbarcode/oned')
30 files changed, 4416 insertions, 0 deletions
diff --git a/xfa/fxbarcode/oned/BC_OneDReader.cpp b/xfa/fxbarcode/oned/BC_OneDReader.cpp new file mode 100644 index 0000000000..c66ec0bebd --- /dev/null +++ b/xfa/fxbarcode/oned/BC_OneDReader.cpp @@ -0,0 +1,175 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 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 <algorithm> +#include <memory> + +#include "xfa/fxbarcode/BC_BinaryBitmap.h" +#include "xfa/fxbarcode/BC_Reader.h" +#include "xfa/fxbarcode/common/BC_CommonBitArray.h" +#include "xfa/fxbarcode/oned/BC_OneDReader.h" +#include "xfa/fxbarcode/utils.h" + +CBC_OneDReader::CBC_OneDReader() {} +CBC_OneDReader::~CBC_OneDReader() {} +CFX_ByteString CBC_OneDReader::Decode(CBC_BinaryBitmap* image, int32_t& e) { + CFX_ByteString strtemp = Decode(image, 0, e); + BC_EXCEPTION_CHECK_ReturnValue(e, ""); + return strtemp; +} +CFX_ByteString CBC_OneDReader::Decode(CBC_BinaryBitmap* image, + int32_t hints, + int32_t& e) { + CFX_ByteString strtemp = DeDecode(image, hints, e); + BC_EXCEPTION_CHECK_ReturnValue(e, ""); + return strtemp; +} +CFX_ByteString CBC_OneDReader::DeDecode(CBC_BinaryBitmap* image, + int32_t hints, + int32_t& e) { + int32_t height = image->GetHeight(); + int32_t middle = height >> 1; + FX_BOOL tryHarder = FALSE; + int32_t rowStep = std::max(1, height >> (tryHarder ? 8 : 5)); + int32_t maxLines; + if (tryHarder) { + maxLines = height; + } else { + maxLines = 15; + } + for (int32_t x = 0; x < maxLines; x++) { + int32_t rowStepsAboveOrBelow = (x + 1) >> 1; + const bool isAbove = (x & 0x01) == 0; + int32_t rowNumber = + middle + + rowStep * (isAbove ? rowStepsAboveOrBelow : -rowStepsAboveOrBelow); + if (rowNumber < 0 || rowNumber >= height) { + break; + } + std::unique_ptr<CBC_CommonBitArray> row( + image->GetBlackRow(rowNumber, nullptr, e)); + if (e != BCExceptionNO) { + e = BCExceptionNO; + continue; + } + for (int32_t attempt = 0; attempt < 2; attempt++) { + if (attempt == 1) { + row->Reverse(); + } + CFX_ByteString result = DecodeRow(rowNumber, row.get(), hints, e); + if (e != BCExceptionNO) { + e = BCExceptionNO; + continue; + } + return result; + } + } + e = BCExceptionNotFound; + return ""; +} +void CBC_OneDReader::RecordPattern(CBC_CommonBitArray* row, + int32_t start, + CFX_Int32Array* counters, + int32_t& e) { + int32_t numCounters = counters->GetSize(); + for (int32_t i = 0; i < numCounters; i++) { + (*counters)[i] = 0; + } + int32_t end = row->GetSize(); + if (start >= end) { + e = BCExceptionNotFound; + return; + } + FX_BOOL isWhite = !row->Get(start); + int32_t counterPosition = 0; + int32_t j = start; + while (j < end) { + FX_BOOL pixel = row->Get(j); + if (pixel ^ isWhite) { + (*counters)[counterPosition]++; + } else { + counterPosition++; + if (counterPosition == numCounters) { + break; + } else { + (*counters)[counterPosition] = 1; + isWhite = !isWhite; + } + } + j++; + } + if (!(counterPosition == numCounters || + (counterPosition == numCounters - 1 && j == end))) { + e = BCExceptionNotFound; + return; + } +} +void CBC_OneDReader::RecordPatternInReverse(CBC_CommonBitArray* row, + int32_t start, + CFX_Int32Array* counters, + int32_t& e) { + int32_t numTransitionsLeft = counters->GetSize(); + FX_BOOL last = row->Get(start); + while (start > 0 && numTransitionsLeft >= 0) { + if (row->Get(--start) != last) { + numTransitionsLeft--; + last = !last; + } + } + if (numTransitionsLeft >= 0) { + e = BCExceptionNotFound; + return; + } + RecordPattern(row, start + 1, counters, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); +} +int32_t CBC_OneDReader::PatternMatchVariance(CFX_Int32Array* counters, + const int32_t* pattern, + int32_t maxIndividualVariance) { + int32_t numCounters = counters->GetSize(); + int32_t total = 0; + int32_t patternLength = 0; + for (int32_t i = 0; i < numCounters; i++) { + total += (*counters)[i]; + patternLength += pattern[i]; + } + if (total < patternLength) { +#undef max + return FXSYS_IntMax; + } + int32_t unitBarWidth = (total << INTEGER_MATH_SHIFT) / patternLength; + maxIndividualVariance = + (maxIndividualVariance * unitBarWidth) >> INTEGER_MATH_SHIFT; + int32_t totalVariance = 0; + for (int32_t x = 0; x < numCounters; x++) { + int32_t counter = (*counters)[x] << INTEGER_MATH_SHIFT; + int32_t scaledPattern = pattern[x] * unitBarWidth; + int32_t variance = counter > scaledPattern ? counter - scaledPattern + : scaledPattern - counter; + if (variance > maxIndividualVariance) { +#undef max + return FXSYS_IntMax; + } + totalVariance += variance; + } + return totalVariance / total; +} diff --git a/xfa/fxbarcode/oned/BC_OneDReader.h b/xfa/fxbarcode/oned/BC_OneDReader.h new file mode 100644 index 0000000000..2bab06e9b8 --- /dev/null +++ b/xfa/fxbarcode/oned/BC_OneDReader.h @@ -0,0 +1,50 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_ONED_BC_ONEDREADER_H_ +#define XFA_FXBARCODE_ONED_BC_ONEDREADER_H_ + +#include "xfa/fxbarcode/BC_Reader.h" + +class CBC_BinaryBitmap; +class CBC_CommonBitArray; + +class CBC_OneDReader : public CBC_Reader { + public: + CBC_OneDReader(); + virtual ~CBC_OneDReader(); + virtual CFX_ByteString Decode(CBC_BinaryBitmap* image, int32_t& e); + virtual CFX_ByteString Decode(CBC_BinaryBitmap* image, + int32_t hints, + int32_t& e); + virtual CFX_ByteString DecodeRow(int32_t rowNumber, + CBC_CommonBitArray* row, + int32_t hints, + int32_t& e) { + return ""; + } + + private: + CFX_ByteString DeDecode(CBC_BinaryBitmap* image, int32_t hints, int32_t& e); + + protected: + static const int32_t INTEGER_MATH_SHIFT = 8; + static const int32_t PATTERN_MATCH_RESULT_SCALE_FACTOR = 1 << 8; + + void RecordPattern(CBC_CommonBitArray* row, + int32_t start, + CFX_Int32Array* counters, + int32_t& e); + void RecordPatternInReverse(CBC_CommonBitArray* row, + int32_t start, + CFX_Int32Array* counters, + int32_t& e); + int32_t PatternMatchVariance(CFX_Int32Array* counters, + const int32_t* pattern, + int32_t maxIndividualVariance); +}; + +#endif // XFA_FXBARCODE_ONED_BC_ONEDREADER_H_ diff --git a/xfa/fxbarcode/oned/BC_OneDimReader.cpp b/xfa/fxbarcode/oned/BC_OneDimReader.cpp new file mode 100644 index 0000000000..5187c34af9 --- /dev/null +++ b/xfa/fxbarcode/oned/BC_OneDimReader.cpp @@ -0,0 +1,219 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2008 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/oned/BC_OneDimReader.h" + +#include <memory> + +#include "xfa/fxbarcode/BC_Reader.h" +#include "xfa/fxbarcode/common/BC_CommonBitArray.h" +#include "xfa/fxbarcode/oned/BC_OneDReader.h" +#include "xfa/fxbarcode/utils.h" + +const int32_t CBC_OneDimReader::START_END_PATTERN[3] = {1, 1, 1}; +const int32_t CBC_OneDimReader::MIDDLE_PATTERN[5] = {1, 1, 1, 1, 1}; +const int32_t CBC_OneDimReader::L_PATTERNS[10][4] = { + {3, 2, 1, 1}, {2, 2, 2, 1}, {2, 1, 2, 2}, {1, 4, 1, 1}, {1, 1, 3, 2}, + {1, 2, 3, 1}, {1, 1, 1, 4}, {1, 3, 1, 2}, {1, 2, 1, 3}, {3, 1, 1, 2}}; +const int32_t CBC_OneDimReader::L_AND_G_PATTERNS[20][4] = { + {3, 2, 1, 1}, {2, 2, 2, 1}, {2, 1, 2, 2}, {1, 4, 1, 1}, {1, 1, 3, 2}, + {1, 2, 3, 1}, {1, 1, 1, 4}, {1, 3, 1, 2}, {1, 2, 1, 3}, {3, 1, 1, 2}, + {1, 1, 2, 3}, {1, 2, 2, 2}, {2, 2, 1, 2}, {1, 1, 4, 1}, {2, 3, 1, 1}, + {1, 3, 2, 1}, {4, 1, 1, 1}, {2, 1, 3, 1}, {3, 1, 2, 1}, {2, 1, 1, 3}}; + +CBC_OneDimReader::CBC_OneDimReader() {} +CBC_OneDimReader::~CBC_OneDimReader() {} +CFX_Int32Array* CBC_OneDimReader::FindStartGuardPattern(CBC_CommonBitArray* row, + int32_t& e) { + FX_BOOL foundStart = FALSE; + CFX_Int32Array* startRange = NULL; + CFX_Int32Array startEndPattern; + startEndPattern.SetSize(3); + startEndPattern[0] = START_END_PATTERN[0]; + startEndPattern[1] = START_END_PATTERN[1]; + startEndPattern[2] = START_END_PATTERN[2]; + int32_t nextStart = 0; + while (!foundStart) { + delete startRange; + startRange = FindGuardPattern(row, nextStart, FALSE, &startEndPattern, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + int32_t start = (*startRange)[0]; + nextStart = (*startRange)[1]; + if (start <= 1) { + break; + } + int32_t quietStart = start - (nextStart - start); + if (quietStart >= 0) { + FX_BOOL booT = row->IsRange(quietStart, start, FALSE, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + foundStart = booT; + } + } + return startRange; +} +CFX_ByteString CBC_OneDimReader::DecodeRow(int32_t rowNumber, + CBC_CommonBitArray* row, + int32_t hints, + int32_t& e) { + std::unique_ptr<CFX_Int32Array> result(FindStartGuardPattern(row, e)); + BC_EXCEPTION_CHECK_ReturnValue(e, ""); + CFX_ByteString temp = DecodeRow(rowNumber, row, result.get(), hints, e); + BC_EXCEPTION_CHECK_ReturnValue(e, ""); + return temp; +} +CFX_ByteString CBC_OneDimReader::DecodeRow(int32_t rowNumber, + CBC_CommonBitArray* row, + CFX_Int32Array* startGuardRange, + int32_t hints, + int32_t& e) { + CFX_ByteString result; + DecodeMiddle(row, startGuardRange, result, e); + BC_EXCEPTION_CHECK_ReturnValue(e, ""); + FX_BOOL b = CheckChecksum(result, e); + BC_EXCEPTION_CHECK_ReturnValue(e, ""); + if (!b) { + e = BCExceptionChecksumException; + return ""; + } + return result; +} +FX_BOOL CBC_OneDimReader::CheckChecksum(CFX_ByteString& s, int32_t& e) { + FX_BOOL temp = CheckStandardUPCEANChecksum(s, e); + BC_EXCEPTION_CHECK_ReturnValue(e, FALSE); + return temp; +} +FX_BOOL CBC_OneDimReader::CheckStandardUPCEANChecksum(CFX_ByteString& s, + int32_t& e) { + int32_t length = s.GetLength(); + if (length == 0) { + return FALSE; + } + int32_t sum = 0; + for (int32_t i = length - 2; i >= 0; i -= 2) { + int32_t digit = (int32_t)s[i] - (int32_t)'0'; + if (digit < 0 || digit > 9) { + e = BCExceptionFormatException; + return FALSE; + } + sum += digit; + } + sum *= 3; + for (int32_t j = length - 1; j >= 0; j -= 2) { + int32_t digit = (int32_t)s[j] - (int32_t)'0'; + if (digit < 0 || digit > 9) { + e = BCExceptionFormatException; + return FALSE; + } + sum += digit; + } + return sum % 10 == 0; +} +CFX_Int32Array* CBC_OneDimReader::DecodeEnd(CBC_CommonBitArray* row, + int32_t endStart, + int32_t& e) { + CFX_Int32Array startEndPattern; + startEndPattern.Add(START_END_PATTERN[0]); + startEndPattern.Add(START_END_PATTERN[1]); + startEndPattern.Add(START_END_PATTERN[2]); + CFX_Int32Array* FindGuard = + FindGuardPattern(row, endStart, FALSE, &startEndPattern, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return FindGuard; +} +CFX_Int32Array* CBC_OneDimReader::FindGuardPattern(CBC_CommonBitArray* row, + int32_t rowOffset, + FX_BOOL whiteFirst, + CFX_Int32Array* pattern, + int32_t& e) { + int32_t patternLength = pattern->GetSize(); + CFX_Int32Array counters; + counters.SetSize(patternLength); + int32_t width = row->GetSize(); + FX_BOOL isWhite = FALSE; + while (rowOffset < width) { + isWhite = !row->Get(rowOffset); + if (whiteFirst == isWhite) { + break; + } + rowOffset++; + } + int32_t counterPosition = 0; + int32_t patternStart = rowOffset; + for (int32_t x = rowOffset; x < width; x++) { + FX_BOOL pixel = row->Get(x); + if (pixel ^ isWhite) { + counters[counterPosition]++; + } else { + if (counterPosition == patternLength - 1) { + if (PatternMatchVariance(&counters, &(*pattern)[0], + MAX_INDIVIDUAL_VARIANCE) < MAX_AVG_VARIANCE) { + CFX_Int32Array* result = new CFX_Int32Array(); + result->SetSize(2); + (*result)[0] = patternStart; + (*result)[1] = x; + return result; + } + patternStart += counters[0] + counters[1]; + for (int32_t y = 2; y < patternLength; y++) { + counters[y - 2] = counters[y]; + } + counters[patternLength - 2] = 0; + counters[patternLength - 1] = 0; + counterPosition--; + } else { + counterPosition++; + } + counters[counterPosition] = 1; + isWhite = !isWhite; + } + } + e = BCExceptionNotFound; + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return NULL; +} +int32_t CBC_OneDimReader::DecodeDigit(CBC_CommonBitArray* row, + CFX_Int32Array* counters, + int32_t rowOffset, + const int32_t* patterns, + int32_t patternLength, + int32_t& e) { + RecordPattern(row, rowOffset, counters, e); + BC_EXCEPTION_CHECK_ReturnValue(e, 0); + int32_t bestVariance = MAX_AVG_VARIANCE; + int32_t bestMatch = -1; + int32_t max = patternLength; + for (int32_t i = 0; i < max; i++) { + int32_t variance = PatternMatchVariance(counters, &patterns[i * 4], + MAX_INDIVIDUAL_VARIANCE); + if (variance < bestVariance) { + bestVariance = variance; + bestMatch = i; + } + } + if (bestMatch >= 0) { + return bestMatch; + } else { + e = BCExceptionNotFound; + return 0; + } + return 0; +} diff --git a/xfa/fxbarcode/oned/BC_OneDimReader.h b/xfa/fxbarcode/oned/BC_OneDimReader.h new file mode 100644 index 0000000000..cc84ffd39a --- /dev/null +++ b/xfa/fxbarcode/oned/BC_OneDimReader.h @@ -0,0 +1,66 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_ONED_BC_ONEDIMREADER_H_ +#define XFA_FXBARCODE_ONED_BC_ONEDIMREADER_H_ + +#include "core/include/fxcrt/fx_basic.h" +#include "xfa/fxbarcode/oned/BC_OneDReader.h" + +class CBC_CommonBitArray; + +class CBC_OneDimReader : public CBC_OneDReader { + private: + static const int32_t MAX_AVG_VARIANCE = (int32_t)(256 * 0.48f); + static const int32_t MAX_INDIVIDUAL_VARIANCE = (int32_t)(256 * 0.7f); + + FX_BOOL CheckStandardUPCEANChecksum(CFX_ByteString& s, int32_t& e); + + public: + static const int32_t START_END_PATTERN[3]; + static const int32_t MIDDLE_PATTERN[5]; + static const int32_t L_PATTERNS[10][4]; + static const int32_t L_AND_G_PATTERNS[20][4]; + + CBC_OneDimReader(); + virtual ~CBC_OneDimReader(); + + CFX_ByteString DecodeRow(int32_t rowNumber, + CBC_CommonBitArray* row, + int32_t hints, + int32_t& e); + CFX_ByteString DecodeRow(int32_t rowNumber, + CBC_CommonBitArray* row, + CFX_Int32Array* startGuardRange, + int32_t hints, + int32_t& e); + + protected: + CFX_Int32Array* FindStartGuardPattern(CBC_CommonBitArray* row, int32_t& e); + virtual FX_BOOL CheckChecksum(CFX_ByteString& s, int32_t& e); + CFX_Int32Array* FindGuardPattern(CBC_CommonBitArray* row, + int32_t rowOffset, + FX_BOOL whiteFirst, + CFX_Int32Array* pattern, + int32_t& e); + int32_t DecodeDigit(CBC_CommonBitArray* row, + CFX_Int32Array* counters, + int32_t rowOffset, + const int32_t* patterns, + int32_t patternLength, + int32_t& e); + virtual int32_t DecodeMiddle(CBC_CommonBitArray* row, + CFX_Int32Array* startRange, + CFX_ByteString& resultResult, + int32_t& e) { + return 0; + } + virtual CFX_Int32Array* DecodeEnd(CBC_CommonBitArray* row, + int32_t endStart, + int32_t& e); +}; + +#endif // XFA_FXBARCODE_ONED_BC_ONEDIMREADER_H_ diff --git a/xfa/fxbarcode/oned/BC_OneDimWriter.cpp b/xfa/fxbarcode/oned/BC_OneDimWriter.cpp new file mode 100644 index 0000000000..62ca97f488 --- /dev/null +++ b/xfa/fxbarcode/oned/BC_OneDimWriter.cpp @@ -0,0 +1,437 @@ +// 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 2011 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/oned/BC_OneDimWriter.h" + +#include <algorithm> +#include <memory> + +#include "xfa/fxbarcode/BC_Writer.h" +#include "xfa/fxbarcode/common/BC_CommonBitMatrix.h" + +CBC_OneDimWriter::CBC_OneDimWriter() { + m_locTextLoc = BC_TEXT_LOC_BELOWEMBED; + m_bPrintChecksum = TRUE; + m_iDataLenth = 0; + m_bCalcChecksum = FALSE; + m_pFont = NULL; + m_fFontSize = 10; + m_iFontStyle = 0; + m_fontColor = 0xff000000; + m_iContentLen = 0; + m_bLeftPadding = FALSE; + m_bRightPadding = FALSE; + m_output = NULL; +} +CBC_OneDimWriter::~CBC_OneDimWriter() { + delete m_output; +} +void CBC_OneDimWriter::SetPrintChecksum(FX_BOOL checksum) { + m_bPrintChecksum = checksum; +} +void CBC_OneDimWriter::SetDataLength(int32_t length) { + m_iDataLenth = length; +} +void CBC_OneDimWriter::SetCalcChecksum(int32_t state) { + m_bCalcChecksum = state; +} +FX_BOOL CBC_OneDimWriter::SetFont(CFX_Font* cFont) { + if (cFont == NULL) { + return FALSE; + } + m_pFont = cFont; + return TRUE; +} +void CBC_OneDimWriter::SetFontSize(FX_FLOAT size) { + m_fFontSize = size; +} +void CBC_OneDimWriter::SetFontStyle(int32_t style) { + m_iFontStyle = style; +} +void CBC_OneDimWriter::SetFontColor(FX_ARGB color) { + m_fontColor = color; +} +FX_WCHAR CBC_OneDimWriter::Upper(FX_WCHAR ch) { + if (ch >= 'a' && ch <= 'z') { + ch = ch - ('a' - 'A'); + } + return ch; +} +uint8_t* CBC_OneDimWriter::Encode(const CFX_ByteString& contents, + BCFORMAT format, + int32_t& outWidth, + int32_t& outHeight, + int32_t hints, + int32_t& e) { + uint8_t* ret = NULL; + outHeight = 1; + if (m_Width >= 20) { + ret = Encode(contents, outWidth, e); + } else { + ret = Encode(contents, outWidth, e); + } + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return ret; +} +uint8_t* CBC_OneDimWriter::Encode(const CFX_ByteString& contents, + BCFORMAT format, + int32_t& outWidth, + int32_t& outHeight, + int32_t& e) { + uint8_t* ret = Encode(contents, format, outWidth, outHeight, 0, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return ret; +} +int32_t CBC_OneDimWriter::AppendPattern(uint8_t* target, + int32_t pos, + const int32_t* pattern, + int32_t patternLength, + int32_t startColor, + int32_t& e) { + if (startColor != 0 && startColor != 1) { + e = BCExceptionValueMustBeEither0or1; + return 0; + } + uint8_t color = (uint8_t)startColor; + int32_t numAdded = 0; + for (int32_t i = 0; i < patternLength; i++) { + for (int32_t j = 0; j < pattern[i]; j++) { + target[pos] = color; + pos += 1; + numAdded += 1; + } + color ^= 1; + } + return numAdded; +} +void CBC_OneDimWriter::CalcTextInfo(const CFX_ByteString& text, + FXTEXT_CHARPOS* charPos, + CFX_Font* cFont, + FX_FLOAT geWidth, + int32_t fontSize, + FX_FLOAT& charsLen) { + std::unique_ptr<CFX_UnicodeEncodingEx> encoding( + FX_CreateFontEncodingEx(cFont)); + + int32_t length = text.GetLength(); + FX_DWORD* pCharCode = FX_Alloc(FX_DWORD, text.GetLength()); + FX_FLOAT charWidth = 0; + for (int32_t j = 0; j < text.GetLength(); j++) { + pCharCode[j] = encoding->CharCodeFromUnicode(text[j]); + int32_t glyp_code = encoding->GlyphFromCharCode(pCharCode[j]); + int32_t glyp_value = cFont->GetGlyphWidth(glyp_code); + FX_FLOAT temp = (FX_FLOAT)((glyp_value)*fontSize / 1000.0); + charWidth += temp; + } + charsLen = charWidth; + FX_FLOAT leftPositon = (FX_FLOAT)(geWidth - charsLen) / 2.0f; + if (leftPositon < 0 && geWidth == 0) { + leftPositon = 0; + } + FX_FLOAT penX = 0.0; + FX_FLOAT penY = + (FX_FLOAT)FXSYS_abs(cFont->GetDescent()) * (FX_FLOAT)fontSize / 1000.0f; + FX_FLOAT left = leftPositon; + FX_FLOAT top = 0.0; + charPos[0].m_OriginX = penX + left; + charPos[0].m_OriginY = penY + top; + charPos[0].m_GlyphIndex = encoding->GlyphFromCharCode(pCharCode[0]); + charPos[0].m_FontCharWidth = cFont->GetGlyphWidth(charPos[0].m_GlyphIndex); +#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ + charPos[0].m_ExtGID = charPos[0].m_GlyphIndex; +#endif + penX += (FX_FLOAT)(charPos[0].m_FontCharWidth) * (FX_FLOAT)fontSize / 1000.0f; + for (int32_t i = 1; i < length; i++) { + charPos[i].m_OriginX = penX + left; + charPos[i].m_OriginY = penY + top; + charPos[i].m_GlyphIndex = encoding->GlyphFromCharCode(pCharCode[i]); + charPos[i].m_FontCharWidth = cFont->GetGlyphWidth(charPos[i].m_GlyphIndex); +#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ + charPos[i].m_ExtGID = charPos[i].m_GlyphIndex; +#endif + penX += + (FX_FLOAT)(charPos[i].m_FontCharWidth) * (FX_FLOAT)fontSize / 1000.0f; + } + FX_Free(pCharCode); +} +void CBC_OneDimWriter::ShowDeviceChars(CFX_RenderDevice* device, + const CFX_Matrix* matrix, + const CFX_ByteString str, + FX_FLOAT geWidth, + FXTEXT_CHARPOS* pCharPos, + FX_FLOAT locX, + FX_FLOAT locY, + int32_t barWidth) { + int32_t iFontSize = (int32_t)fabs(m_fFontSize); + int32_t iTextHeight = iFontSize + 1; + CFX_FloatRect rect((FX_FLOAT)locX, (FX_FLOAT)locY, (FX_FLOAT)(locX + geWidth), + (FX_FLOAT)(locY + iTextHeight)); + if (geWidth != m_Width) { + rect.right -= 1; + } + matrix->TransformRect(rect); + FX_RECT re = rect.GetOutterRect(); + device->FillRect(&re, m_backgroundColor); + CFX_Matrix affine_matrix(1.0, 0.0, 0.0, -1.0, (FX_FLOAT)locX, + (FX_FLOAT)(locY + iFontSize)); + if (matrix) { + affine_matrix.Concat(*matrix); + } + device->DrawNormalText(str.GetLength(), pCharPos, m_pFont, + CFX_GEModule::Get()->GetFontCache(), + (FX_FLOAT)iFontSize, (CFX_Matrix*)&affine_matrix, + m_fontColor, FXTEXT_CLEARTYPE); +} +void CBC_OneDimWriter::ShowBitmapChars(CFX_DIBitmap* pOutBitmap, + const CFX_ByteString str, + FX_FLOAT geWidth, + FXTEXT_CHARPOS* pCharPos, + FX_FLOAT locX, + FX_FLOAT locY, + int32_t barWidth) { + int32_t iFontSize = (int32_t)fabs(m_fFontSize); + int32_t iTextHeight = iFontSize + 1; + CFX_FxgeDevice ge; + ge.Create((int)geWidth, iTextHeight, m_colorSpace); + FX_RECT geRect(0, 0, (int)geWidth, iTextHeight); + ge.FillRect(&geRect, m_backgroundColor); + CFX_Matrix affine_matrix(1.0, 0.0, 0.0, -1.0, 0.0, (FX_FLOAT)iFontSize); + ge.DrawNormalText(str.GetLength(), pCharPos, m_pFont, + CFX_GEModule::Get()->GetFontCache(), (FX_FLOAT)iFontSize, + (CFX_Matrix*)&affine_matrix, m_fontColor, FXTEXT_CLEARTYPE); + CFX_FxgeDevice geBitmap; + geBitmap.Attach(pOutBitmap); + geBitmap.SetDIBits(ge.GetBitmap(), (int)locX, (int)locY); +} +void CBC_OneDimWriter::ShowChars(const CFX_WideStringC& contents, + CFX_DIBitmap* pOutBitmap, + CFX_RenderDevice* device, + const CFX_Matrix* matrix, + int32_t barWidth, + int32_t multiple, + int32_t& e) { + if (device == NULL && pOutBitmap == NULL) { + e = BCExceptionIllegalArgument; + return; + } + if (m_pFont == NULL) { + e = BCExceptionNullPointer; + return; + } + CFX_ByteString str = FX_UTF8Encode(contents); + int32_t iLen = str.GetLength(); + FXTEXT_CHARPOS* pCharPos = FX_Alloc(FXTEXT_CHARPOS, iLen); + FXSYS_memset(pCharPos, 0, sizeof(FXTEXT_CHARPOS) * iLen); + FX_FLOAT charsLen = 0; + FX_FLOAT geWidth = 0; + if (m_locTextLoc == BC_TEXT_LOC_ABOVEEMBED || + m_locTextLoc == BC_TEXT_LOC_BELOWEMBED) { + geWidth = 0; + } else if (m_locTextLoc == BC_TEXT_LOC_ABOVE || + m_locTextLoc == BC_TEXT_LOC_BELOW) { + geWidth = (FX_FLOAT)barWidth; + } + int32_t iFontSize = (int32_t)fabs(m_fFontSize); + int32_t iTextHeight = iFontSize + 1; + CalcTextInfo(str, pCharPos, m_pFont, geWidth, iFontSize, charsLen); + if (charsLen < 1) { + return; + } + int32_t locX = 0; + int32_t locY = 0; + switch (m_locTextLoc) { + case BC_TEXT_LOC_ABOVEEMBED: + locX = (int32_t)(barWidth - charsLen) / 2; + locY = 0; + geWidth = charsLen; + break; + case BC_TEXT_LOC_ABOVE: + locX = 0; + locY = 0; + geWidth = (FX_FLOAT)barWidth; + break; + case BC_TEXT_LOC_BELOWEMBED: + locX = (int32_t)(barWidth - charsLen) / 2; + locY = m_Height - iTextHeight; + geWidth = charsLen; + break; + case BC_TEXT_LOC_BELOW: + default: + locX = 0; + locY = m_Height - iTextHeight; + geWidth = (FX_FLOAT)barWidth; + break; + } + if (device) { + ShowDeviceChars(device, matrix, str, geWidth, pCharPos, (FX_FLOAT)locX, + (FX_FLOAT)locY, barWidth); + } else { + ShowBitmapChars(pOutBitmap, str, geWidth, pCharPos, (FX_FLOAT)locX, + (FX_FLOAT)locY, barWidth); + } + FX_Free(pCharPos); +} +void CBC_OneDimWriter::RenderBitmapResult(CFX_DIBitmap*& pOutBitmap, + const CFX_WideStringC& contents, + int32_t& e) { + if (m_output == NULL) { + BC_EXCEPTION_CHECK_ReturnVoid(e); + } + pOutBitmap = CreateDIBitmap(m_output->GetWidth(), m_output->GetHeight()); + pOutBitmap->Clear(m_backgroundColor); + if (!pOutBitmap) { + e = BCExceptionFailToCreateBitmap; + return; + } + for (int32_t x = 0; x < m_output->GetWidth(); x++) { + for (int32_t y = 0; y < m_output->GetHeight(); y++) { + if (m_output->Get(x, y)) { + pOutBitmap->SetPixel(x, y, m_barColor); + } + } + } + int32_t i = 0; + for (; i < contents.GetLength(); i++) + if (contents.GetAt(i) != ' ') { + break; + } + if (m_locTextLoc != BC_TEXT_LOC_NONE && i < contents.GetLength()) { + ShowChars(contents, pOutBitmap, NULL, NULL, m_barWidth, m_multiple, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + } + CFX_DIBitmap* pStretchBitmap = pOutBitmap->StretchTo(m_Width, m_Height); + if (pOutBitmap) { + delete pOutBitmap; + } + pOutBitmap = pStretchBitmap; +} +void CBC_OneDimWriter::RenderDeviceResult(CFX_RenderDevice* device, + const CFX_Matrix* matrix, + const CFX_WideStringC& contents, + int32_t& e) { + if (m_output == NULL) { + BC_EXCEPTION_CHECK_ReturnVoid(e); + } + CFX_GraphStateData stateData; + CFX_PathData path; + path.AppendRect(0, 0, (FX_FLOAT)m_Width, (FX_FLOAT)m_Height); + device->DrawPath(&path, matrix, &stateData, m_backgroundColor, + m_backgroundColor, FXFILL_ALTERNATE); + CFX_Matrix matri(m_outputHScale, 0.0, 0.0, (FX_FLOAT)m_Height, 0.0, 0.0); + matri.Concat(*matrix); + for (int32_t x = 0; x < m_output->GetWidth(); x++) { + for (int32_t y = 0; y < m_output->GetHeight(); y++) { + CFX_PathData rect; + rect.AppendRect((FX_FLOAT)x, (FX_FLOAT)y, (FX_FLOAT)(x + 1), + (FX_FLOAT)(y + 1)); + CFX_GraphStateData stateData; + if (m_output->Get(x, y)) { + device->DrawPath(&rect, &matri, &stateData, m_barColor, 0, + FXFILL_WINDING); + } + } + } + int32_t i = 0; + for (; i < contents.GetLength(); i++) + if (contents.GetAt(i) != ' ') { + break; + } + if (m_locTextLoc != BC_TEXT_LOC_NONE && i < contents.GetLength()) { + ShowChars(contents, NULL, device, matrix, m_barWidth, m_multiple, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + } +} +void CBC_OneDimWriter::RenderResult(const CFX_WideStringC& contents, + uint8_t* code, + int32_t codeLength, + FX_BOOL isDevice, + int32_t& e) { + if (codeLength < 1) { + BC_EXCEPTION_CHECK_ReturnVoid(e); + } + if (m_ModuleHeight < 20.0) { + m_ModuleHeight = 20; + } + int32_t codeOldLength = codeLength; + int32_t leftPadding = 0; + int32_t rightPadding = 0; + if (m_bLeftPadding) { + leftPadding = 7; + } + if (m_bRightPadding) { + rightPadding = 7; + } + codeLength += leftPadding; + codeLength += rightPadding; + m_outputHScale = 1.0; + if (m_Width > 0) { + m_outputHScale = (FX_FLOAT)m_Width / (FX_FLOAT)codeLength; + } + if (!isDevice) { + m_outputHScale = + std::max(m_outputHScale, static_cast<FX_FLOAT>(m_ModuleWidth)); + } + FX_FLOAT dataLengthScale = 1.0; + if (m_iDataLenth > 0 && contents.GetLength() != 0) { + dataLengthScale = FX_FLOAT(contents.GetLength()) / FX_FLOAT(m_iDataLenth); + } + if (m_iDataLenth > 0 && contents.GetLength() == 0) { + dataLengthScale = FX_FLOAT(1) / FX_FLOAT(m_iDataLenth); + } + m_multiple = 1; + if (!isDevice) { + m_multiple = (int32_t)ceil(m_outputHScale * dataLengthScale); + } + int32_t outputHeight = 1; + if (!isDevice) { + if (m_Height == 0) { + outputHeight = std::max(20, m_ModuleHeight); + } else { + outputHeight = m_Height; + } + } + int32_t outputWidth = codeLength; + if (!isDevice) { + outputWidth = (int32_t)(codeLength * m_multiple / dataLengthScale); + } + m_barWidth = m_Width; + if (!isDevice) { + m_barWidth = codeLength * m_multiple; + } + m_output = new CBC_CommonBitMatrix; + m_output->Init(outputWidth, outputHeight); + int32_t outputX = leftPadding * m_multiple; + for (int32_t inputX = 0; inputX < codeOldLength; inputX++) { + if (code[inputX] == 1) { + if (outputX >= outputWidth) { + break; + } + if (outputX + m_multiple > outputWidth && outputWidth - outputX > 0) { + m_output->SetRegion(outputX, 0, outputWidth - outputX, outputHeight, e); + break; + } + m_output->SetRegion(outputX, 0, m_multiple, outputHeight, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + } + outputX += m_multiple; + } +} diff --git a/xfa/fxbarcode/oned/BC_OneDimWriter.h b/xfa/fxbarcode/oned/BC_OneDimWriter.h new file mode 100644 index 0000000000..e91a9297e1 --- /dev/null +++ b/xfa/fxbarcode/oned/BC_OneDimWriter.h @@ -0,0 +1,120 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_ONED_BC_ONEDIMWRITER_H_ +#define XFA_FXBARCODE_ONED_BC_ONEDIMWRITER_H_ + +#include "core/include/fxge/fx_ge.h" +#include "xfa/fxbarcode/BC_Writer.h" +#include "xfa/include/fxbarcode/BC_BarCode.h" + +class CBC_CommonBitMatrix; +class CFX_Font; +class CFX_RenderDevice; + +class CBC_OneDimWriter : public CBC_Writer { + public: + CBC_OneDimWriter(); + virtual ~CBC_OneDimWriter(); + uint8_t* Encode(const CFX_ByteString& contents, + BCFORMAT format, + int32_t& outWidth, + int32_t& outHeight, + int32_t& e); + uint8_t* Encode(const CFX_ByteString& contents, + BCFORMAT format, + int32_t& outWidth, + int32_t& outHeight, + int32_t hints, + int32_t& e); + virtual uint8_t* Encode(const CFX_ByteString& contents, + int32_t& outLength, + int32_t& e) { + return NULL; + } + virtual void RenderResult(const CFX_WideStringC& contents, + uint8_t* code, + int32_t codeLength, + FX_BOOL isDevice, + int32_t& e); + virtual void RenderBitmapResult(CFX_DIBitmap*& pOutBitmap, + const CFX_WideStringC& contents, + int32_t& e); + virtual void RenderDeviceResult(CFX_RenderDevice* device, + const CFX_Matrix* matrix, + const CFX_WideStringC& contents, + int32_t& e); + virtual FX_BOOL CheckContentValidity(const CFX_WideStringC& contents) { + return TRUE; + } + virtual CFX_WideString FilterContents(const CFX_WideStringC& contents) { + return CFX_WideString(); + } + virtual CFX_WideString RenderTextContents(const CFX_WideStringC& contents) { + return CFX_WideString(); + } + virtual void SetPrintChecksum(FX_BOOL checksum); + virtual void SetDataLength(int32_t length); + virtual void SetCalcChecksum(int32_t state); + virtual void SetFontSize(FX_FLOAT size); + virtual void SetFontStyle(int32_t style); + virtual void SetFontColor(FX_ARGB color); + virtual FX_BOOL SetFont(CFX_Font* cFont); + + protected: + FX_BOOL m_bPrintChecksum; + int32_t m_iDataLenth; + FX_BOOL m_bCalcChecksum; + CFX_Font* m_pFont; + FX_FLOAT m_fFontSize; + int32_t m_iFontStyle; + FX_DWORD m_fontColor; + BC_TEXT_LOC m_locTextLoc; + int32_t m_iContentLen; + FX_BOOL m_bLeftPadding; + FX_BOOL m_bRightPadding; + CBC_CommonBitMatrix* m_output; + int32_t m_barWidth; + int32_t m_multiple; + FX_FLOAT m_outputHScale; + void CalcTextInfo(const CFX_ByteString& text, + FXTEXT_CHARPOS* charPos, + CFX_Font* cFont, + FX_FLOAT geWidth, + int32_t fontSize, + FX_FLOAT& charsLen); + virtual void ShowChars(const CFX_WideStringC& contents, + CFX_DIBitmap* pOutBitmap, + CFX_RenderDevice* device, + const CFX_Matrix* matrix, + int32_t barWidth, + int32_t multiple, + int32_t& e); + virtual void ShowBitmapChars(CFX_DIBitmap* pOutBitmap, + const CFX_ByteString str, + FX_FLOAT geWidth, + FXTEXT_CHARPOS* pCharPos, + FX_FLOAT locX, + FX_FLOAT locY, + int32_t barWidth); + virtual void ShowDeviceChars(CFX_RenderDevice* device, + const CFX_Matrix* matrix, + const CFX_ByteString str, + FX_FLOAT geWidth, + FXTEXT_CHARPOS* pCharPos, + FX_FLOAT locX, + FX_FLOAT locY, + int32_t barWidth); + int32_t AppendPattern(uint8_t* target, + int32_t pos, + const int32_t* pattern, + int32_t patternLength, + int32_t startColor, + int32_t& e); + FX_WCHAR Upper(FX_WCHAR ch); +}; + +#endif // XFA_FXBARCODE_ONED_BC_ONEDIMWRITER_H_ diff --git a/xfa/fxbarcode/oned/BC_OnedCodaBarReader.cpp b/xfa/fxbarcode/oned/BC_OnedCodaBarReader.cpp new file mode 100644 index 0000000000..27f2191eac --- /dev/null +++ b/xfa/fxbarcode/oned/BC_OnedCodaBarReader.cpp @@ -0,0 +1,212 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2008 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/oned/BC_OnedCodaBarReader.h" + +#include <algorithm> +#include <memory> + +#include "core/include/fxcrt/fx_basic.h" +#include "xfa/fxbarcode/BC_Reader.h" +#include "xfa/fxbarcode/common/BC_CommonBitArray.h" +#include "xfa/fxbarcode/oned/BC_OneDReader.h" +#include "xfa/fxbarcode/oned/BC_OnedCode39Reader.h" +#include "xfa/fxbarcode/utils.h" + +const FX_CHAR* CBC_OnedCodaBarReader::ALPHABET_STRING = + "0123456789-$:/.+ABCDTN"; +const int32_t CBC_OnedCodaBarReader::CHARACTER_ENCODINGS[22] = { + 0x003, 0x006, 0x009, 0x060, 0x012, 0x042, 0x021, 0x024, + 0x030, 0x048, 0x00c, 0x018, 0x045, 0x051, 0x054, 0x015, + 0x01A, 0x029, 0x00B, 0x00E, 0x01A, 0x029}; +const FX_CHAR CBC_OnedCodaBarReader::STARTEND_ENCODING[8] = { + 'E', '*', 'A', 'B', 'C', 'D', 'T', 'N'}; + +CBC_OnedCodaBarReader::CBC_OnedCodaBarReader() {} +CBC_OnedCodaBarReader::~CBC_OnedCodaBarReader() {} +CFX_ByteString CBC_OnedCodaBarReader::DecodeRow(int32_t rowNumber, + CBC_CommonBitArray* row, + int32_t hints, + int32_t& e) { + std::unique_ptr<CFX_Int32Array> start(FindAsteriskPattern(row, e)); + BC_EXCEPTION_CHECK_ReturnValue(e, ""); + (*start)[1] = 0; + int32_t nextStart = (*start)[1]; + int32_t end = row->GetSize(); + while (nextStart < end && !row->Get(nextStart)) { + nextStart++; + } + CFX_ByteString result; + CFX_Int32Array counters; + counters.SetSize(7); + FX_CHAR decodedChar; + int32_t lastStart; + do { + RecordPattern(row, nextStart, &counters, e); + BC_EXCEPTION_CHECK_ReturnValue(e, ""); + decodedChar = ToNarrowWidePattern(&counters); + if (decodedChar == '!') { + e = BCExceptionNotFound; + return ""; + } + result += decodedChar; + lastStart = nextStart; + for (int32_t i = 0; i < counters.GetSize(); i++) { + nextStart += counters[i]; + } + while (nextStart < end && !row->Get(nextStart)) { + nextStart++; + } + } while (nextStart < end); + int32_t lastPatternSize = 0; + for (int32_t j = 0; j < counters.GetSize(); j++) { + lastPatternSize += counters[j]; + } + int32_t whiteSpaceAfterEnd = nextStart - lastStart - lastPatternSize; + if (nextStart != end && (whiteSpaceAfterEnd / 2 < lastPatternSize)) { + e = BCExceptionNotFound; + return ""; + } + if (result.GetLength() < 2) { + e = BCExceptionNotFound; + return ""; + } + FX_CHAR startchar = result[0]; + if (!ArrayContains(STARTEND_ENCODING, startchar)) { + e = BCExceptionNotFound; + return ""; + } + int32_t len = result.GetLength(); + CFX_ByteString temp = result; + for (int32_t k = 1; k < result.GetLength(); k++) { + if (ArrayContains(STARTEND_ENCODING, result[k])) { + if ((k + 1) != result.GetLength()) { + result.Delete(1, k); + k = 1; + } + } + } + if (result.GetLength() < 5) { + int32_t index = temp.Find(result.Mid(1, result.GetLength() - 1)); + if (index == len - (result.GetLength() - 1)) { + e = BCExceptionNotFound; + return ""; + } + } + if (result.GetLength() > minCharacterLength) { + result = result.Mid(1, result.GetLength() - 2); + } else { + e = BCExceptionNotFound; + return ""; + } + return result; +} +CFX_Int32Array* CBC_OnedCodaBarReader::FindAsteriskPattern( + CBC_CommonBitArray* row, + int32_t& e) { + int32_t width = row->GetSize(); + int32_t rowOffset = 0; + while (rowOffset < width) { + if (row->Get(rowOffset)) { + break; + } + rowOffset++; + } + int32_t counterPosition = 0; + CFX_Int32Array counters; + counters.SetSize(7); + int32_t patternStart = rowOffset; + FX_BOOL isWhite = FALSE; + int32_t patternLength = counters.GetSize(); + for (int32_t i = rowOffset; i < width; i++) { + FX_BOOL pixel = row->Get(i); + if (pixel ^ isWhite) { + counters[counterPosition]++; + } else { + if (counterPosition == patternLength - 1) { + if (ArrayContains(STARTEND_ENCODING, ToNarrowWidePattern(&counters))) { + FX_BOOL btemp3 = + row->IsRange(std::max(0, patternStart - (i - patternStart) / 2), + patternStart, FALSE, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + if (btemp3) { + CFX_Int32Array* result = new CFX_Int32Array(); + result->SetSize(2); + (*result)[0] = patternStart; + (*result)[1] = i; + return result; + } + } + patternStart += counters[0] + counters[1]; + for (int32_t y = 2; y < patternLength; y++) { + counters[y - 2] = counters[y]; + } + counters[patternLength - 2] = 0; + counters[patternLength - 1] = 0; + counterPosition--; + } else { + counterPosition++; + } + counters[counterPosition] = 1; + isWhite = !isWhite; + } + } + e = BCExceptionNotFound; + return NULL; +} +FX_BOOL CBC_OnedCodaBarReader::ArrayContains(const FX_CHAR array[], + FX_CHAR key) { + for (int32_t i = 0; i < 8; i++) { + if (array[i] == key) { + return TRUE; + } + } + return FALSE; +} +FX_CHAR CBC_OnedCodaBarReader::ToNarrowWidePattern(CFX_Int32Array* counter) { + int32_t numCounters = counter->GetSize(); + if (numCounters < 1) { + return '!'; + } + int32_t averageCounter = 0; + int32_t totalCounters = 0; + for (int32_t i = 0; i < numCounters; i++) { + totalCounters += (*counter)[i]; + } + averageCounter = totalCounters / numCounters; + int32_t pattern = 0; + int32_t wideCounters = 0; + for (int32_t j = 0; j < numCounters; j++) { + if ((*counter)[j] > averageCounter) { + pattern |= 1 << (numCounters - 1 - j); + wideCounters++; + } + } + if ((wideCounters == 2) || (wideCounters == 3)) { + for (int32_t k = 0; k < 22; k++) { + if (CHARACTER_ENCODINGS[k] == pattern) { + return (ALPHABET_STRING)[k]; + } + } + } + return '!'; +} diff --git a/xfa/fxbarcode/oned/BC_OnedCodaBarReader.h b/xfa/fxbarcode/oned/BC_OnedCodaBarReader.h new file mode 100644 index 0000000000..678795dcf3 --- /dev/null +++ b/xfa/fxbarcode/oned/BC_OnedCodaBarReader.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 XFA_FXBARCODE_ONED_BC_ONEDCODABARREADER_H_ +#define XFA_FXBARCODE_ONED_BC_ONEDCODABARREADER_H_ + +#include "core/include/fxcrt/fx_basic.h" +#include "xfa/fxbarcode/oned/BC_OneDReader.h" + +class CBC_CommonBitArray; +class CBC_OneDReader; + +class CBC_OnedCodaBarReader : public CBC_OneDReader { + public: + CBC_OnedCodaBarReader(); + virtual ~CBC_OnedCodaBarReader(); + CFX_ByteString DecodeRow(int32_t rowNumber, + CBC_CommonBitArray* row, + int32_t hints, + int32_t& e); + CFX_Int32Array* FindAsteriskPattern(CBC_CommonBitArray* row, int32_t& e); + FX_BOOL ArrayContains(const FX_CHAR array[], FX_CHAR key); + FX_CHAR ToNarrowWidePattern(CFX_Int32Array* counter); + + static const FX_CHAR* ALPHABET_STRING; + static const int32_t CHARACTER_ENCODINGS[22]; + + private: + static const int32_t minCharacterLength = 3; + static const FX_CHAR STARTEND_ENCODING[8]; +}; + +#endif // XFA_FXBARCODE_ONED_BC_ONEDCODABARREADER_H_ diff --git a/xfa/fxbarcode/oned/BC_OnedCodaBarWriter.cpp b/xfa/fxbarcode/oned/BC_OnedCodaBarWriter.cpp new file mode 100644 index 0000000000..81f0339aa8 --- /dev/null +++ b/xfa/fxbarcode/oned/BC_OnedCodaBarWriter.cpp @@ -0,0 +1,231 @@ +// 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 2011 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/BC_Reader.h" +#include "xfa/fxbarcode/BC_Writer.h" +#include "xfa/fxbarcode/common/BC_CommonBitArray.h" +#include "xfa/fxbarcode/common/BC_CommonBitMatrix.h" +#include "xfa/fxbarcode/oned/BC_OneDReader.h" +#include "xfa/fxbarcode/oned/BC_OneDimWriter.h" +#include "xfa/fxbarcode/oned/BC_OnedCodaBarReader.h" +#include "xfa/fxbarcode/oned/BC_OnedCodaBarWriter.h" + +const FX_CHAR CBC_OnedCodaBarWriter::START_END_CHARS[] = { + 'A', 'B', 'C', 'D', 'T', 'N', '*', 'E', 'a', 'b', 'c', 'd', 't', 'n', 'e'}; +const FX_CHAR CBC_OnedCodaBarWriter::CONTENT_CHARS[] = { + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', '-', '$', '/', ':', '+', '.'}; + +CBC_OnedCodaBarWriter::CBC_OnedCodaBarWriter() { + m_chStart = 'A'; + m_chEnd = 'B'; + m_iWideNarrRatio = 2; +} +CBC_OnedCodaBarWriter::~CBC_OnedCodaBarWriter() {} +FX_BOOL CBC_OnedCodaBarWriter::SetStartChar(FX_CHAR start) { + for (size_t i = 0; i < FX_ArraySize(START_END_CHARS); ++i) { + if (START_END_CHARS[i] == start) { + m_chStart = start; + return TRUE; + } + } + return FALSE; +} + +FX_BOOL CBC_OnedCodaBarWriter::SetEndChar(FX_CHAR end) { + for (size_t i = 0; i < FX_ArraySize(START_END_CHARS); ++i) { + if (START_END_CHARS[i] == end) { + m_chEnd = end; + return TRUE; + } + } + return FALSE; +} +void CBC_OnedCodaBarWriter::SetDataLength(int32_t length) { + m_iDataLenth = length + 2; +} +FX_BOOL CBC_OnedCodaBarWriter::SetTextLocation(BC_TEXT_LOC location) { + if (location < BC_TEXT_LOC_NONE || location > BC_TEXT_LOC_BELOWEMBED) { + return FALSE; + } + m_locTextLoc = location; + return TRUE; +} +FX_BOOL CBC_OnedCodaBarWriter::SetWideNarrowRatio(int32_t ratio) { + if (ratio < 2 || ratio > 3) { + return FALSE; + } + m_iWideNarrRatio = ratio; + return TRUE; +} +FX_BOOL CBC_OnedCodaBarWriter::FindChar(FX_WCHAR ch, FX_BOOL isContent) { + if (isContent) { + for (size_t i = 0; i < FX_ArraySize(CONTENT_CHARS); ++i) { + if (ch == (FX_WCHAR)CONTENT_CHARS[i]) { + return TRUE; + } + } + for (size_t j = 0; j < FX_ArraySize(START_END_CHARS); ++j) { + if (ch == (FX_WCHAR)START_END_CHARS[j]) { + return TRUE; + } + } + return FALSE; + } else { + for (size_t i = 0; i < FX_ArraySize(CONTENT_CHARS); ++i) { + if (ch == (FX_WCHAR)CONTENT_CHARS[i]) { + return TRUE; + } + } + return FALSE; + } +} +FX_BOOL CBC_OnedCodaBarWriter::CheckContentValidity( + const CFX_WideStringC& contents) { + FX_WCHAR ch; + int32_t index = 0; + for (index = 0; index < contents.GetLength(); index++) { + ch = contents.GetAt(index); + if (FindChar(ch, FALSE)) { + continue; + } else { + return FALSE; + } + } + return TRUE; +} +CFX_WideString CBC_OnedCodaBarWriter::FilterContents( + const CFX_WideStringC& contents) { + CFX_WideString filtercontents; + FX_WCHAR ch; + for (int32_t index = 0; index < contents.GetLength(); index++) { + ch = contents.GetAt(index); + if (ch > 175) { + index++; + continue; + } + if (FindChar(ch, TRUE)) { + filtercontents += ch; + } else { + continue; + } + } + return filtercontents; +} +uint8_t* CBC_OnedCodaBarWriter::Encode(const CFX_ByteString& contents, + BCFORMAT format, + int32_t& outWidth, + int32_t& outHeight, + int32_t& e) { + uint8_t* ret = Encode(contents, format, outWidth, outHeight, 0, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return ret; +} +uint8_t* CBC_OnedCodaBarWriter::Encode(const CFX_ByteString& contents, + BCFORMAT format, + int32_t& outWidth, + int32_t& outHeight, + int32_t hints, + int32_t& e) { + if (format != BCFORMAT_CODABAR) { + e = BCExceptionOnlyEncodeCODEBAR; + return NULL; + } + uint8_t* ret = + CBC_OneDimWriter::Encode(contents, format, outWidth, outHeight, hints, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return ret; +} +uint8_t* CBC_OnedCodaBarWriter::Encode(const CFX_ByteString& contents, + int32_t& outLength, + int32_t& e) { + CBC_OnedCodaBarReader CodaBarR; + CFX_ByteString data = m_chStart + contents + m_chEnd; + m_iContentLen = data.GetLength(); + uint8_t* result = FX_Alloc2D(uint8_t, m_iWideNarrRatio * 7, data.GetLength()); + FX_CHAR ch; + int32_t position = 0; + for (int32_t index = 0; index < data.GetLength(); index++) { + ch = data.GetAt(index); + if (((ch >= 'a') && (ch <= 'z'))) { + ch = ch - 32; + } + switch (ch) { + case 'T': + ch = 'A'; + break; + case 'N': + ch = 'B'; + break; + case '*': + ch = 'C'; + break; + case 'E': + ch = 'D'; + break; + default: + break; + } + int32_t code = 0; + int32_t len = (int32_t)strlen(CodaBarR.ALPHABET_STRING); + for (int32_t i = 0; i < len; i++) { + if (ch == CodaBarR.ALPHABET_STRING[i]) { + code = CodaBarR.CHARACTER_ENCODINGS[i]; + break; + } + } + uint8_t color = 1; + int32_t counter = 0; + int32_t bit = 0; + while (bit < 7) { + result[position] = color; + position++; + if (((code >> (6 - bit)) & 1) == 0 || counter == m_iWideNarrRatio - 1) { + color = !color; + bit++; + counter = 0; + } else { + counter++; + } + } + if (index < data.GetLength() - 1) { + result[position] = 0; + position++; + } + } + outLength = position; + return result; +} +CFX_WideString CBC_OnedCodaBarWriter::encodedContents( + const CFX_WideStringC& contents) { + CFX_WideString strStart(m_chStart); + CFX_WideString strEnd(m_chEnd); + return strStart + contents + strEnd; +} +void CBC_OnedCodaBarWriter::RenderResult(const CFX_WideStringC& contents, + uint8_t* code, + int32_t codeLength, + FX_BOOL isDevice, + int32_t& e) { + CBC_OneDimWriter::RenderResult(encodedContents(contents), code, codeLength, + isDevice, e); +} diff --git a/xfa/fxbarcode/oned/BC_OnedCodaBarWriter.h b/xfa/fxbarcode/oned/BC_OnedCodaBarWriter.h new file mode 100644 index 0000000000..92a31bd989 --- /dev/null +++ b/xfa/fxbarcode/oned/BC_OnedCodaBarWriter.h @@ -0,0 +1,54 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_ONED_BC_ONEDCODABARWRITER_H_ +#define XFA_FXBARCODE_ONED_BC_ONEDCODABARWRITER_H_ + +enum BC_TEXT_LOC; +class CBC_OneDimWriter; +class CBC_OnedCodaBarWriter; +class CBC_OnedCodaBarWriter : public CBC_OneDimWriter { + public: + CBC_OnedCodaBarWriter(); + virtual ~CBC_OnedCodaBarWriter(); + uint8_t* Encode(const CFX_ByteString& contents, + int32_t& outLength, + int32_t& e); + uint8_t* Encode(const CFX_ByteString& contents, + BCFORMAT format, + int32_t& outWidth, + int32_t& outHeight, + int32_t& e); + uint8_t* Encode(const CFX_ByteString& contents, + BCFORMAT format, + int32_t& outWidth, + int32_t& outHeight, + int32_t hints, + int32_t& e); + CFX_WideString encodedContents(const CFX_WideStringC& contents); + FX_BOOL CheckContentValidity(const CFX_WideStringC& contents); + CFX_WideString FilterContents(const CFX_WideStringC& contents); + FX_BOOL SetStartChar(FX_CHAR start); + FX_BOOL SetEndChar(FX_CHAR end); + void SetDataLength(int32_t length); + FX_BOOL SetTextLocation(BC_TEXT_LOC location); + FX_BOOL SetWideNarrowRatio(int32_t ratio); + FX_BOOL FindChar(FX_WCHAR ch, FX_BOOL isContent); + + private: + void RenderResult(const CFX_WideStringC& contents, + uint8_t* code, + int32_t codeLength, + FX_BOOL isDevice, + int32_t& e); + static const FX_CHAR START_END_CHARS[]; + static const FX_CHAR CONTENT_CHARS[]; + FX_CHAR m_chStart; + FX_CHAR m_chEnd; + int32_t m_iWideNarrRatio; +}; + +#endif // XFA_FXBARCODE_ONED_BC_ONEDCODABARWRITER_H_ diff --git a/xfa/fxbarcode/oned/BC_OnedCode128Reader.cpp b/xfa/fxbarcode/oned/BC_OnedCode128Reader.cpp new file mode 100644 index 0000000000..b5f5aa5fe4 --- /dev/null +++ b/xfa/fxbarcode/oned/BC_OnedCode128Reader.cpp @@ -0,0 +1,351 @@ +// 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 <algorithm> + +#include "xfa/fxbarcode/BC_Reader.h" +#include "xfa/fxbarcode/common/BC_CommonBitArray.h" +#include "xfa/fxbarcode/oned/BC_OneDReader.h" +#include "xfa/fxbarcode/oned/BC_OnedCode128Reader.h" +#include "xfa/fxbarcode/utils.h" + +const int32_t CBC_OnedCode128Reader::CODE_PATTERNS[107][7] = { + {2, 1, 2, 2, 2, 2, 0}, {2, 2, 2, 1, 2, 2, 0}, {2, 2, 2, 2, 2, 1, 0}, + {1, 2, 1, 2, 2, 3, 0}, {1, 2, 1, 3, 2, 2, 0}, {1, 3, 1, 2, 2, 2, 0}, + {1, 2, 2, 2, 1, 3, 0}, {1, 2, 2, 3, 1, 2, 0}, {1, 3, 2, 2, 1, 2, 0}, + {2, 2, 1, 2, 1, 3, 0}, {2, 2, 1, 3, 1, 2, 0}, {2, 3, 1, 2, 1, 2, 0}, + {1, 1, 2, 2, 3, 2, 0}, {1, 2, 2, 1, 3, 2, 0}, {1, 2, 2, 2, 3, 1, 0}, + {1, 1, 3, 2, 2, 2, 0}, {1, 2, 3, 1, 2, 2, 0}, {1, 2, 3, 2, 2, 1, 0}, + {2, 2, 3, 2, 1, 1, 0}, {2, 2, 1, 1, 3, 2, 0}, {2, 2, 1, 2, 3, 1, 0}, + {2, 1, 3, 2, 1, 2, 0}, {2, 2, 3, 1, 1, 2, 0}, {3, 1, 2, 1, 3, 1, 0}, + {3, 1, 1, 2, 2, 2, 0}, {3, 2, 1, 1, 2, 2, 0}, {3, 2, 1, 2, 2, 1, 0}, + {3, 1, 2, 2, 1, 2, 0}, {3, 2, 2, 1, 1, 2, 0}, {3, 2, 2, 2, 1, 1, 0}, + {2, 1, 2, 1, 2, 3, 0}, {2, 1, 2, 3, 2, 1, 0}, {2, 3, 2, 1, 2, 1, 0}, + {1, 1, 1, 3, 2, 3, 0}, {1, 3, 1, 1, 2, 3, 0}, {1, 3, 1, 3, 2, 1, 0}, + {1, 1, 2, 3, 1, 3, 0}, {1, 3, 2, 1, 1, 3, 0}, {1, 3, 2, 3, 1, 1, 0}, + {2, 1, 1, 3, 1, 3, 0}, {2, 3, 1, 1, 1, 3, 0}, {2, 3, 1, 3, 1, 1, 0}, + {1, 1, 2, 1, 3, 3, 0}, {1, 1, 2, 3, 3, 1, 0}, {1, 3, 2, 1, 3, 1, 0}, + {1, 1, 3, 1, 2, 3, 0}, {1, 1, 3, 3, 2, 1, 0}, {1, 3, 3, 1, 2, 1, 0}, + {3, 1, 3, 1, 2, 1, 0}, {2, 1, 1, 3, 3, 1, 0}, {2, 3, 1, 1, 3, 1, 0}, + {2, 1, 3, 1, 1, 3, 0}, {2, 1, 3, 3, 1, 1, 0}, {2, 1, 3, 1, 3, 1, 0}, + {3, 1, 1, 1, 2, 3, 0}, {3, 1, 1, 3, 2, 1, 0}, {3, 3, 1, 1, 2, 1, 0}, + {3, 1, 2, 1, 1, 3, 0}, {3, 1, 2, 3, 1, 1, 0}, {3, 3, 2, 1, 1, 1, 0}, + {3, 1, 4, 1, 1, 1, 0}, {2, 2, 1, 4, 1, 1, 0}, {4, 3, 1, 1, 1, 1, 0}, + {1, 1, 1, 2, 2, 4, 0}, {1, 1, 1, 4, 2, 2, 0}, {1, 2, 1, 1, 2, 4, 0}, + {1, 2, 1, 4, 2, 1, 0}, {1, 4, 1, 1, 2, 2, 0}, {1, 4, 1, 2, 2, 1, 0}, + {1, 1, 2, 2, 1, 4, 0}, {1, 1, 2, 4, 1, 2, 0}, {1, 2, 2, 1, 1, 4, 0}, + {1, 2, 2, 4, 1, 1, 0}, {1, 4, 2, 1, 1, 2, 0}, {1, 4, 2, 2, 1, 1, 0}, + {2, 4, 1, 2, 1, 1, 0}, {2, 2, 1, 1, 1, 4, 0}, {4, 1, 3, 1, 1, 1, 0}, + {2, 4, 1, 1, 1, 2, 0}, {1, 3, 4, 1, 1, 1, 0}, {1, 1, 1, 2, 4, 2, 0}, + {1, 2, 1, 1, 4, 2, 0}, {1, 2, 1, 2, 4, 1, 0}, {1, 1, 4, 2, 1, 2, 0}, + {1, 2, 4, 1, 1, 2, 0}, {1, 2, 4, 2, 1, 1, 0}, {4, 1, 1, 2, 1, 2, 0}, + {4, 2, 1, 1, 1, 2, 0}, {4, 2, 1, 2, 1, 1, 0}, {2, 1, 2, 1, 4, 1, 0}, + {2, 1, 4, 1, 2, 1, 0}, {4, 1, 2, 1, 2, 1, 0}, {1, 1, 1, 1, 4, 3, 0}, + {1, 1, 1, 3, 4, 1, 0}, {1, 3, 1, 1, 4, 1, 0}, {1, 1, 4, 1, 1, 3, 0}, + {1, 1, 4, 3, 1, 1, 0}, {4, 1, 1, 1, 1, 3, 0}, {4, 1, 1, 3, 1, 1, 0}, + {1, 1, 3, 1, 4, 1, 0}, {1, 1, 4, 1, 3, 1, 0}, {3, 1, 1, 1, 4, 1, 0}, + {4, 1, 1, 1, 3, 1, 0}, {2, 1, 1, 4, 1, 2, 0}, {2, 1, 1, 2, 1, 4, 0}, + {2, 1, 1, 2, 3, 2, 0}, {2, 3, 3, 1, 1, 1, 2}}; + +CBC_OnedCode128Reader::CBC_OnedCode128Reader() {} +CBC_OnedCode128Reader::~CBC_OnedCode128Reader() {} +CFX_Int32Array* CBC_OnedCode128Reader::FindStartPattern(CBC_CommonBitArray* row, + int32_t& e) { + int32_t width = row->GetSize(); + int32_t rowOffset = 0; + while (rowOffset < width) { + if (row->Get(rowOffset)) { + break; + } + rowOffset++; + } + int32_t counterPosition = 0; + CFX_Int32Array counters; + counters.SetSize(6); + int32_t patternStart = rowOffset; + FX_BOOL isWhite = FALSE; + int32_t patternLength = counters.GetSize(); + for (int32_t i = rowOffset; i < width; i++) { + FX_BOOL pixel = row->Get(i); + if (pixel ^ isWhite) { + counters[counterPosition]++; + } else { + if (counterPosition == patternLength - 1) { + int32_t bestVariance = MAX_AVG_VARIANCE; + int32_t bestMatch = -1; + for (int32_t startCode = CODE_START_A; startCode <= CODE_START_C; + startCode++) { + int32_t variance = PatternMatchVariance( + &counters, &CODE_PATTERNS[startCode][0], MAX_INDIVIDUAL_VARIANCE); + if (variance < bestVariance) { + bestVariance = variance; + bestMatch = startCode; + } + } + if (bestMatch >= 0) { + FX_BOOL btemp2 = + row->IsRange(std::max(0, patternStart - (i - patternStart) / 2), + patternStart, FALSE, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + if (btemp2) { + CFX_Int32Array* result = new CFX_Int32Array; + result->SetSize(3); + (*result)[0] = patternStart; + (*result)[1] = i; + (*result)[2] = bestMatch; + return result; + } + } + patternStart += counters[0] + counters[1]; + for (int32_t y = 2; y < patternLength; y++) { + counters[y - 2] = counters[y]; + } + counters[patternLength - 2] = 0; + counters[patternLength - 1] = 0; + counterPosition--; + } else { + counterPosition++; + } + counters[counterPosition] = 1; + isWhite = !isWhite; + } + } + e = BCExceptionNotFound; + return NULL; +} +int32_t CBC_OnedCode128Reader::DecodeCode(CBC_CommonBitArray* row, + CFX_Int32Array* counters, + int32_t rowOffset, + int32_t& e) { + RecordPattern(row, rowOffset, counters, e); + BC_EXCEPTION_CHECK_ReturnValue(e, 0); + int32_t bestVariance = MAX_AVG_VARIANCE; + int32_t bestMatch = -1; + for (int32_t d = 0; d < 107; d++) { + int32_t variance = PatternMatchVariance(counters, &CODE_PATTERNS[d][0], + MAX_INDIVIDUAL_VARIANCE); + if (variance < bestVariance) { + bestVariance = variance; + bestMatch = d; + } + } + if (bestMatch >= 0) { + return bestMatch; + } else { + e = BCExceptionNotFound; + return 0; + } + return 0; +} +CFX_ByteString CBC_OnedCode128Reader::DecodeRow(int32_t rowNumber, + CBC_CommonBitArray* row, + int32_t hints, + int32_t& e) { + CFX_Int32Array* startPatternInfo = FindStartPattern(row, e); + BC_EXCEPTION_CHECK_ReturnValue(e, ""); + int32_t startCode = (*startPatternInfo)[2]; + int32_t codeSet; + switch (startCode) { + case 103: + codeSet = CODE_CODE_A; + break; + case 104: + codeSet = CODE_CODE_B; + break; + case 105: + codeSet = CODE_CODE_C; + break; + default: + if (startPatternInfo) { + startPatternInfo->RemoveAll(); + delete startPatternInfo; + startPatternInfo = NULL; + } + e = BCExceptionFormatException; + return ""; + } + FX_BOOL done = FALSE; + FX_BOOL isNextShifted = FALSE; + CFX_ByteString result; + int32_t lastStart = (*startPatternInfo)[0]; + int32_t nextStart = (*startPatternInfo)[1]; + if (startPatternInfo) { + startPatternInfo->RemoveAll(); + delete startPatternInfo; + startPatternInfo = NULL; + } + CFX_Int32Array counters; + counters.SetSize(6); + int32_t lastCode = 0; + int32_t code = 0; + int32_t checksumTotal = startCode; + int32_t multiplier = 0; + FX_BOOL lastCharacterWasPrintable = TRUE; + while (!done) { + FX_BOOL unshift = isNextShifted; + isNextShifted = FALSE; + lastCode = code; + code = DecodeCode(row, &counters, nextStart, e); + BC_EXCEPTION_CHECK_ReturnValue(e, ""); + if (code != CODE_STOP) { + lastCharacterWasPrintable = TRUE; + } + if (code != CODE_STOP) { + multiplier++; + checksumTotal += multiplier * code; + } + lastStart = nextStart; + for (int32_t i = 0; i < counters.GetSize(); i++) { + nextStart += counters[i]; + } + switch (code) { + case 103: + case 104: + case 105: + e = BCExceptionFormatException; + return ""; + } + switch (codeSet) { + case 101: + if (code < 64) { + result += (FX_CHAR)(' ' + code); + } else if (code < 96) { + result += (FX_CHAR)(code - 64); + } else { + if (code != CODE_STOP) { + lastCharacterWasPrintable = FALSE; + } + switch (code) { + case 102: + case 97: + case 96: + case 101: + break; + case 98: + isNextShifted = TRUE; + codeSet = CODE_CODE_B; + break; + case 100: + codeSet = CODE_CODE_B; + break; + case 99: + codeSet = CODE_CODE_C; + break; + case 106: + done = TRUE; + break; + } + } + break; + case 100: + if (code < 96) { + result += (FX_CHAR)(' ' + code); + } else { + if (code != CODE_STOP) { + lastCharacterWasPrintable = FALSE; + } + switch (code) { + case 102: + case 97: + case 96: + case 100: + break; + case 98: + isNextShifted = TRUE; + codeSet = CODE_CODE_A; + break; + case 101: + codeSet = CODE_CODE_A; + break; + case 99: + codeSet = CODE_CODE_C; + break; + case 106: + done = TRUE; + break; + } + } + break; + case 99: + if (code < 100) { + if (code < 10) { + result += '0'; + } + FX_CHAR temp[128]; + // TODO(dsinclair): Should this be snprintf? + sprintf(temp, "%d", code); + result += temp; + } else { + if (code != CODE_STOP) { + lastCharacterWasPrintable = FALSE; + } + switch (code) { + case 102: + break; + case 101: + codeSet = CODE_CODE_A; + break; + case 100: + codeSet = CODE_CODE_B; + break; + case 106: + done = TRUE; + break; + } + } + break; + } + if (unshift) { + codeSet = codeSet == CODE_CODE_A ? CODE_CODE_B : CODE_CODE_A; + } + } + int32_t width = row->GetSize(); + while (nextStart < width && row->Get(nextStart)) { + nextStart++; + } + FX_BOOL boolT1 = row->IsRange( + nextStart, std::min(width, nextStart + (nextStart - lastStart) / 2), + FALSE, e); + BC_EXCEPTION_CHECK_ReturnValue(e, ""); + if (!boolT1) { + e = BCExceptionNotFound; + return ""; + } + checksumTotal -= multiplier * lastCode; + if (checksumTotal % 103 != lastCode) { + e = BCExceptionChecksumException; + return ""; + } + int32_t resultLength = result.GetLength(); + if (resultLength > 0 && lastCharacterWasPrintable) { + if (codeSet == CODE_CODE_C) { + result = result.Mid(0, result.GetLength() - 2); + } else { + result = result.Mid(0, result.GetLength() - 1); + } + } + if (result.GetLength() == 0) { + e = BCExceptionFormatException; + return ""; + } + return result; +} diff --git a/xfa/fxbarcode/oned/BC_OnedCode128Reader.h b/xfa/fxbarcode/oned/BC_OnedCode128Reader.h new file mode 100644 index 0000000000..d9ac4853bf --- /dev/null +++ b/xfa/fxbarcode/oned/BC_OnedCode128Reader.h @@ -0,0 +1,49 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_ONED_BC_ONEDCODE128READER_H_ +#define XFA_FXBARCODE_ONED_BC_ONEDCODE128READER_H_ + +class CBC_OneDReader; +class CBC_CommonBitArray; +class CBC_OnedCode128Reader : public CBC_OneDReader { + public: + CBC_OnedCode128Reader(); + virtual ~CBC_OnedCode128Reader(); + virtual CFX_ByteString DecodeRow(int32_t rowNumber, + CBC_CommonBitArray* row, + int32_t hints, + int32_t& e); + + static const int32_t CODE_PATTERNS[107][7]; + + private: + static const int32_t MAX_AVG_VARIANCE = (int32_t)(256 * 0.25f); + static const int32_t MAX_INDIVIDUAL_VARIANCE = (int32_t)(256 * 0.7f); + + static const int32_t CODE_SHIFT = 98; + static const int32_t CODE_CODE_C = 99; + static const int32_t CODE_CODE_B = 100; + static const int32_t CODE_CODE_A = 101; + static const int32_t CODE_FNC_1 = 102; + static const int32_t CODE_FNC_2 = 97; + static const int32_t CODE_FNC_3 = 96; + static const int32_t CODE_FNC_4_A = 101; + static const int32_t CODE_FNC_4_B = 100; + + static const int32_t CODE_START_A = 103; + static const int32_t CODE_START_B = 104; + static const int32_t CODE_START_C = 105; + static const int32_t CODE_STOP = 106; + + CFX_Int32Array* FindStartPattern(CBC_CommonBitArray* row, int32_t& e); + int32_t DecodeCode(CBC_CommonBitArray* row, + CFX_Int32Array* counters, + int32_t rowOffset, + int32_t& e); +}; + +#endif // XFA_FXBARCODE_ONED_BC_ONEDCODE128READER_H_ diff --git a/xfa/fxbarcode/oned/BC_OnedCode128Writer.cpp b/xfa/fxbarcode/oned/BC_OnedCode128Writer.cpp new file mode 100644 index 0000000000..4d759b48a3 --- /dev/null +++ b/xfa/fxbarcode/oned/BC_OnedCode128Writer.cpp @@ -0,0 +1,235 @@ +// 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 2010 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/BC_Reader.h" +#include "xfa/fxbarcode/BC_Writer.h" +#include "xfa/fxbarcode/oned/BC_OneDReader.h" +#include "xfa/fxbarcode/oned/BC_OneDimWriter.h" +#include "xfa/fxbarcode/oned/BC_OnedCode128Reader.h" +#include "xfa/fxbarcode/oned/BC_OnedCode128Writer.h" + +const int32_t CBC_OnedCode128Writer::CODE_CODE_B = 100; +const int32_t CBC_OnedCode128Writer::CODE_CODE_C = 99; +const int32_t CBC_OnedCode128Writer::CODE_START_B = 104; +const int32_t CBC_OnedCode128Writer::CODE_START_C = 105; +const int32_t CBC_OnedCode128Writer::CODE_STOP = 106; + +CBC_OnedCode128Writer::CBC_OnedCode128Writer() { + m_codeFormat = BC_CODE128_B; +} +CBC_OnedCode128Writer::CBC_OnedCode128Writer(BC_TYPE type) { + m_codeFormat = type; +} +CBC_OnedCode128Writer::~CBC_OnedCode128Writer() {} +BC_TYPE CBC_OnedCode128Writer::GetType() { + return m_codeFormat; +} +FX_BOOL CBC_OnedCode128Writer::CheckContentValidity( + const CFX_WideStringC& contents) { + FX_BOOL ret = TRUE; + int32_t position = 0; + int32_t patternIndex = -1; + if (m_codeFormat == BC_CODE128_B || m_codeFormat == BC_CODE128_C) { + while (position < contents.GetLength()) { + patternIndex = (int32_t)contents.GetAt(position); + if (patternIndex >= 32 && patternIndex <= 126 && patternIndex != 34) { + position++; + continue; + } else { + ret = FALSE; + break; + } + position++; + } + } else { + ret = FALSE; + } + return ret; +} +CFX_WideString CBC_OnedCode128Writer::FilterContents( + const CFX_WideStringC& contents) { + CFX_WideString filterChineseChar; + FX_WCHAR ch; + for (int32_t i = 0; i < contents.GetLength(); i++) { + ch = contents.GetAt(i); + if (ch > 175) { + i++; + continue; + } + filterChineseChar += ch; + } + CFX_WideString filtercontents; + if (m_codeFormat == BC_CODE128_B) { + for (int32_t i = 0; i < filterChineseChar.GetLength(); i++) { + ch = filterChineseChar.GetAt(i); + if (ch >= 32 && ch <= 126) { + filtercontents += ch; + } else { + continue; + } + } + } else if (m_codeFormat == BC_CODE128_C) { + for (int32_t i = 0; i < filterChineseChar.GetLength(); i++) { + ch = filterChineseChar.GetAt(i); + if (ch >= 32 && ch <= 106) { + filtercontents += ch; + } else { + continue; + } + } + } else { + filtercontents = contents; + } + return filtercontents; +} +FX_BOOL CBC_OnedCode128Writer::SetTextLocation(BC_TEXT_LOC location) { + if (location < BC_TEXT_LOC_NONE || location > BC_TEXT_LOC_BELOWEMBED) { + return FALSE; + } + m_locTextLoc = location; + return TRUE; +} +uint8_t* CBC_OnedCode128Writer::Encode(const CFX_ByteString& contents, + BCFORMAT format, + int32_t& outWidth, + int32_t& outHeight, + int32_t hints, + int32_t& e) { + if (format != BCFORMAT_CODE_128) { + e = BCExceptionOnlyEncodeCODE_128; + return NULL; + } + uint8_t* ret = + CBC_OneDimWriter::Encode(contents, format, outWidth, outHeight, hints, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return ret; +} +uint8_t* CBC_OnedCode128Writer::Encode(const CFX_ByteString& contents, + BCFORMAT format, + int32_t& outWidth, + int32_t& outHeight, + int32_t& e) { + uint8_t* ret = Encode(contents, format, outWidth, outHeight, 0, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return ret; +} +FX_BOOL CBC_OnedCode128Writer::IsDigits(const CFX_ByteString& contents, + int32_t start, + int32_t length) { + int32_t end = start + length; + for (int32_t i = start; i < end; i++) { + if (contents[i] < '0' || contents[i] > '9') { + return FALSE; + } + } + return TRUE; +} +uint8_t* CBC_OnedCode128Writer::Encode(const CFX_ByteString& contents, + int32_t& outLength, + int32_t& e) { + if (contents.GetLength() < 1 || contents.GetLength() > 80) { + e = BCExceptionContentsLengthShouldBetween1and80; + return NULL; + } + CFX_PtrArray patterns; + int32_t checkSum = 0; + if (m_codeFormat == BC_CODE128_B) { + checkSum = Encode128B(contents, patterns); + } else if (m_codeFormat == BC_CODE128_C) { + checkSum = Encode128C(contents, patterns); + } else { + e = BCExceptionFormatException; + return NULL; + } + checkSum %= 103; + patterns.Add((int32_t*)CBC_OnedCode128Reader::CODE_PATTERNS[checkSum]); + patterns.Add((int32_t*)CBC_OnedCode128Reader::CODE_PATTERNS[CODE_STOP]); + m_iContentLen = contents.GetLength() + 3; + int32_t codeWidth = 0; + for (int32_t k = 0; k < patterns.GetSize(); k++) { + int32_t* pattern = (int32_t*)patterns[k]; + for (int32_t j = 0; j < 7; j++) { + codeWidth += pattern[j]; + } + } + outLength = codeWidth; + uint8_t* result = FX_Alloc(uint8_t, outLength); + int32_t pos = 0; + for (int32_t j = 0; j < patterns.GetSize(); j++) { + int32_t* pattern = (int32_t*)patterns[j]; + pos += AppendPattern(result, pos, pattern, 7, 1, e); + if (e != BCExceptionNO) { + FX_Free(result); + return NULL; + } + } + return result; +} +int32_t CBC_OnedCode128Writer::Encode128B(const CFX_ByteString& contents, + CFX_PtrArray& patterns) { + int32_t checkSum = 0; + int32_t checkWeight = 1; + int32_t position = 0; + patterns.Add((int32_t*)CBC_OnedCode128Reader::CODE_PATTERNS[CODE_START_B]); + checkSum += CODE_START_B * checkWeight; + while (position < contents.GetLength()) { + int32_t patternIndex = 0; + patternIndex = contents[position] - ' '; + position += 1; + patterns.Add((int32_t*)CBC_OnedCode128Reader::CODE_PATTERNS[patternIndex]); + checkSum += patternIndex * checkWeight; + if (position != 0) { + checkWeight++; + } + } + return checkSum; +} +int32_t CBC_OnedCode128Writer::Encode128C(const CFX_ByteString& contents, + CFX_PtrArray& patterns) { + int32_t checkSum = 0; + int32_t checkWeight = 1; + int32_t position = 0; + patterns.Add((int32_t*)CBC_OnedCode128Reader::CODE_PATTERNS[CODE_START_C]); + checkSum += CODE_START_C * checkWeight; + while (position < contents.GetLength()) { + int32_t patternIndex = 0; + FX_CHAR ch = contents.GetAt(position); + if (ch < '0' || ch > '9') { + patternIndex = (int32_t)ch; + position++; + } else { + patternIndex = FXSYS_atoi(contents.Mid(position, 2)); + if (contents.GetAt(position + 1) < '0' || + contents.GetAt(position + 1) > '9') { + position += 1; + } else { + position += 2; + } + } + patterns.Add((int32_t*)CBC_OnedCode128Reader::CODE_PATTERNS[patternIndex]); + checkSum += patternIndex * checkWeight; + if (position != 0) { + checkWeight++; + } + } + return checkSum; +} diff --git a/xfa/fxbarcode/oned/BC_OnedCode128Writer.h b/xfa/fxbarcode/oned/BC_OnedCode128Writer.h new file mode 100644 index 0000000000..9be05a19b0 --- /dev/null +++ b/xfa/fxbarcode/oned/BC_OnedCode128Writer.h @@ -0,0 +1,49 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_ONED_BC_ONEDCODE128WRITER_H_ +#define XFA_FXBARCODE_ONED_BC_ONEDCODE128WRITER_H_ + +class CBC_OneDimWriter; +class CBC_OnedCode128Writer : public CBC_OneDimWriter { + public: + CBC_OnedCode128Writer(); + CBC_OnedCode128Writer(BC_TYPE type); + virtual ~CBC_OnedCode128Writer(); + uint8_t* Encode(const CFX_ByteString& contents, + BCFORMAT format, + int32_t& outWidth, + int32_t& outHeight, + int32_t hints, + int32_t& e); + uint8_t* Encode(const CFX_ByteString& contents, + BCFORMAT format, + int32_t& outWidth, + int32_t& outHeight, + int32_t& e); + uint8_t* Encode(const CFX_ByteString& contents, + int32_t& outLength, + int32_t& e); + FX_BOOL CheckContentValidity(const CFX_WideStringC& contents); + CFX_WideString FilterContents(const CFX_WideStringC& contents); + FX_BOOL SetTextLocation(BC_TEXT_LOC location); + BC_TYPE GetType(); + + private: + FX_BOOL IsDigits(const CFX_ByteString& contents, + int32_t start, + int32_t length); + int32_t Encode128B(const CFX_ByteString& contents, CFX_PtrArray& patterns); + int32_t Encode128C(const CFX_ByteString& contents, CFX_PtrArray& patterns); + BC_TYPE m_codeFormat; + static const int32_t CODE_START_B; + static const int32_t CODE_START_C; + static const int32_t CODE_CODE_B; + static const int32_t CODE_CODE_C; + static const int32_t CODE_STOP; +}; + +#endif // XFA_FXBARCODE_ONED_BC_ONEDCODE128WRITER_H_ diff --git a/xfa/fxbarcode/oned/BC_OnedCode39Reader.cpp b/xfa/fxbarcode/oned/BC_OnedCode39Reader.cpp new file mode 100644 index 0000000000..e2f88e7a79 --- /dev/null +++ b/xfa/fxbarcode/oned/BC_OnedCode39Reader.cpp @@ -0,0 +1,287 @@ +// 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 <algorithm> + +#include "xfa/fxbarcode/BC_Reader.h" +#include "xfa/fxbarcode/common/BC_CommonBitArray.h" +#include "xfa/fxbarcode/oned/BC_OneDReader.h" +#include "xfa/fxbarcode/oned/BC_OnedCode39Reader.h" +#include "xfa/fxbarcode/utils.h" + +const FX_CHAR* CBC_OnedCode39Reader::ALPHABET_STRING = + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. *$/+%"; +const FX_CHAR* CBC_OnedCode39Reader::CHECKSUM_STRING = + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%"; +const int32_t CBC_OnedCode39Reader::CHARACTER_ENCODINGS[44] = { + 0x034, 0x121, 0x061, 0x160, 0x031, 0x130, 0x070, 0x025, 0x124, + 0x064, 0x109, 0x049, 0x148, 0x019, 0x118, 0x058, 0x00D, 0x10C, + 0x04C, 0x01C, 0x103, 0x043, 0x142, 0x013, 0x112, 0x052, 0x007, + 0x106, 0x046, 0x016, 0x181, 0x0C1, 0x1C0, 0x091, 0x190, 0x0D0, + 0x085, 0x184, 0x0C4, 0x094, 0x0A8, 0x0A2, 0x08A, 0x02A}; +const int32_t CBC_OnedCode39Reader::ASTERISK_ENCODING = 0x094; + +CBC_OnedCode39Reader::CBC_OnedCode39Reader() + : m_usingCheckDigit(FALSE), m_extendedMode(FALSE) {} +CBC_OnedCode39Reader::CBC_OnedCode39Reader(FX_BOOL usingCheckDigit) + : m_usingCheckDigit(usingCheckDigit), m_extendedMode(FALSE) {} +CBC_OnedCode39Reader::CBC_OnedCode39Reader(FX_BOOL usingCheckDigit, + FX_BOOL extendedMode) + : m_usingCheckDigit(usingCheckDigit), m_extendedMode(extendedMode) {} +CBC_OnedCode39Reader::~CBC_OnedCode39Reader() {} +CFX_ByteString CBC_OnedCode39Reader::DecodeRow(int32_t rowNumber, + CBC_CommonBitArray* row, + int32_t hints, + int32_t& e) { + CFX_Int32Array* start = FindAsteriskPattern(row, e); + BC_EXCEPTION_CHECK_ReturnValue(e, ""); + int32_t nextStart = (*start)[1]; + delete start; + int32_t end = row->GetSize(); + while (nextStart < end && !row->Get(nextStart)) { + nextStart++; + } + CFX_ByteString result; + CFX_Int32Array counters; + counters.SetSize(9); + FX_CHAR decodedChar; + do { + RecordPattern(row, nextStart, &counters, e); + BC_EXCEPTION_CHECK_ReturnValue(e, ""); + int32_t pattern = ToNarrowWidePattern(&counters); + if (pattern < 0) { + e = BCExceptionNotFound; + return ""; + } + decodedChar = PatternToChar(pattern, e); + BC_EXCEPTION_CHECK_ReturnValue(e, ""); + result += decodedChar; + for (int32_t i = 0; i < counters.GetSize(); i++) { + nextStart += counters[i]; + } + while (nextStart < end && !row->Get(nextStart)) { + nextStart++; + } + } while (decodedChar != '*'); + result = result.Mid(0, result.GetLength() - 1); + int32_t lastPatternSize = 0; + for (int32_t j = 0; j < counters.GetSize(); j++) { + lastPatternSize += counters[j]; + } + if (m_usingCheckDigit) { + int32_t max = result.GetLength() - 1; + int32_t total = 0; + int32_t len = (int32_t)strlen(ALPHABET_STRING); + for (int32_t k = 0; k < max; k++) { + for (int32_t j = 0; j < len; j++) + if (ALPHABET_STRING[j] == result[k]) { + total += j; + } + } + if (result[max] != (ALPHABET_STRING)[total % 43]) { + e = BCExceptionChecksumException; + return ""; + } + result = result.Mid(0, result.GetLength() - 1); + } + if (result.GetLength() == 0) { + e = BCExceptionNotFound; + return ""; + } + if (m_extendedMode) { + CFX_ByteString bytestr = DecodeExtended(result, e); + BC_EXCEPTION_CHECK_ReturnValue(e, ""); + return bytestr; + } else { + return result; + } +} +CFX_Int32Array* CBC_OnedCode39Reader::FindAsteriskPattern( + CBC_CommonBitArray* row, + int32_t& e) { + int32_t width = row->GetSize(); + int32_t rowOffset = 0; + while (rowOffset < width) { + if (row->Get(rowOffset)) { + break; + } + rowOffset++; + } + int32_t counterPosition = 0; + CFX_Int32Array counters; + counters.SetSize(9); + int32_t patternStart = rowOffset; + FX_BOOL isWhite = FALSE; + int32_t patternLength = counters.GetSize(); + for (int32_t i = rowOffset; i < width; i++) { + FX_BOOL pixel = row->Get(i); + if (pixel ^ isWhite) { + counters[counterPosition]++; + } else { + if (counterPosition == patternLength - 1) { + if (ToNarrowWidePattern(&counters) == ASTERISK_ENCODING) { + FX_BOOL bT1 = + row->IsRange(std::max(0, patternStart - (i - patternStart) / 2), + patternStart, FALSE, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + if (bT1) { + CFX_Int32Array* result = new CFX_Int32Array; + result->SetSize(2); + (*result)[0] = patternStart; + (*result)[1] = i; + return result; + } + } + patternStart += counters[0] + counters[1]; + for (int32_t y = 2; y < patternLength; y++) { + counters[y - 2] = counters[y]; + } + counters[patternLength - 2] = 0; + counters[patternLength - 1] = 0; + counterPosition--; + } else { + counterPosition++; + } + counters[counterPosition] = 1; + isWhite = !isWhite; + } + } + e = BCExceptionNotFound; + return NULL; +} +int32_t CBC_OnedCode39Reader::ToNarrowWidePattern(CFX_Int32Array* counters) { + int32_t numCounters = counters->GetSize(); + int32_t maxNarrowCounter = 0; + int32_t wideCounters; + do { +#undef max + int32_t minCounter = FXSYS_IntMax; + for (int32_t i = 0; i < numCounters; i++) { + int32_t counter = (*counters)[i]; + if (counter < minCounter && counter > maxNarrowCounter) { + minCounter = counter; + } + } + maxNarrowCounter = minCounter; + wideCounters = 0; + int32_t totalWideCountersWidth = 0; + int32_t pattern = 0; + for (int32_t j = 0; j < numCounters; j++) { + int32_t counter = (*counters)[j]; + if ((*counters)[j] > maxNarrowCounter) { + pattern |= 1 << (numCounters - 1 - j); + wideCounters++; + totalWideCountersWidth += counter; + } + } + if (wideCounters == 3) { + for (int32_t k = 0; k < numCounters && wideCounters > 0; k++) { + int32_t counter = (*counters)[k]; + if ((*counters)[k] > maxNarrowCounter) { + wideCounters--; + if ((counter << 1) >= totalWideCountersWidth) { + return -1; + } + } + } + return pattern; + } + } while (wideCounters > 3); + return -1; +} +FX_CHAR CBC_OnedCode39Reader::PatternToChar(int32_t pattern, int32_t& e) { + for (int32_t i = 0; i < 44; i++) { + if (CHARACTER_ENCODINGS[i] == pattern) { + return (ALPHABET_STRING)[i]; + } + } + e = BCExceptionNotFound; + return 0; +} +CFX_ByteString CBC_OnedCode39Reader::DecodeExtended(CFX_ByteString& encoded, + int32_t& e) { + int32_t length = encoded.GetLength(); + CFX_ByteString decoded; + FX_CHAR c, next; + for (int32_t i = 0; i < length; i++) { + c = encoded[i]; + if (c == '+' || c == '$' || c == '%' || c == '/') { + next = encoded[i + 1]; + FX_CHAR decodedChar = '\0'; + switch (c) { + case '+': + if (next >= 'A' && next <= 'Z') { + decodedChar = (FX_CHAR)(next + 32); + } else { + e = BCExceptionFormatException; + return ""; + } + break; + case '$': + if (next >= 'A' && next <= 'Z') { + decodedChar = (FX_CHAR)(next - 64); + } else { + e = BCExceptionFormatException; + return ""; + } + break; + case '%': + if (next >= 'A' && next <= 'E') { + decodedChar = (FX_CHAR)(next - 38); + } else if (next >= 'F' && next <= 'J') { + decodedChar = (FX_CHAR)(next - 11); + } else if (next >= 'K' && next <= 'O' && next != 'M' && next != 'N') { + decodedChar = (FX_CHAR)(next + 16); + } else if (next >= 'P' && next <= 'S') { + decodedChar = (FX_CHAR)(next + 43); + } else if (next == 'U') { + decodedChar = (FX_CHAR)0; + } else if (next == 'V') { + decodedChar = (FX_CHAR)64; + } else if (next == 'W') { + decodedChar = (FX_CHAR)96; + } else if (next == 'T' || next == 'X' || next == 'Y' || next == 'Z') { + decodedChar = (FX_CHAR)127; + } else { + e = BCExceptionFormatException; + return ""; + } + break; + case '/': + if (next >= 'A' && next <= 'O') { + decodedChar = (FX_CHAR)(next - 32); + } else if (next == 'Z') { + decodedChar = ':'; + } else { + e = BCExceptionFormatException; + return ""; + } + break; + } + decoded += decodedChar; + i++; + } else { + decoded += c; + } + } + return decoded; +} diff --git a/xfa/fxbarcode/oned/BC_OnedCode39Reader.h b/xfa/fxbarcode/oned/BC_OnedCode39Reader.h new file mode 100644 index 0000000000..f1551e4761 --- /dev/null +++ b/xfa/fxbarcode/oned/BC_OnedCode39Reader.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 XFA_FXBARCODE_ONED_BC_ONEDCODE39READER_H_ +#define XFA_FXBARCODE_ONED_BC_ONEDCODE39READER_H_ + +class CBC_OneDReader; +class CBC_CommonBitArray; +class CBC_OnedCode39Reader : public CBC_OneDReader { + public: + static const FX_CHAR* ALPHABET_STRING; + static const FX_CHAR* CHECKSUM_STRING; + static const int32_t CHARACTER_ENCODINGS[44]; + static const int32_t ASTERISK_ENCODING; + CBC_OnedCode39Reader(); + CBC_OnedCode39Reader(FX_BOOL usingCheckDigit); + CBC_OnedCode39Reader(FX_BOOL usingCheckDigit, FX_BOOL extendedMode); + virtual ~CBC_OnedCode39Reader(); + CFX_ByteString DecodeRow(int32_t rowNumber, + CBC_CommonBitArray* row, + int32_t hints, + int32_t& e); + + private: + FX_BOOL m_usingCheckDigit; + FX_BOOL m_extendedMode; + CFX_Int32Array* FindAsteriskPattern(CBC_CommonBitArray* row, int32_t& e); + int32_t ToNarrowWidePattern(CFX_Int32Array* counters); + FX_CHAR PatternToChar(int32_t pattern, int32_t& e); + CFX_ByteString DecodeExtended(CFX_ByteString& encoded, int32_t& e); +}; + +#endif // XFA_FXBARCODE_ONED_BC_ONEDCODE39READER_H_ diff --git a/xfa/fxbarcode/oned/BC_OnedCode39Writer.cpp b/xfa/fxbarcode/oned/BC_OnedCode39Writer.cpp new file mode 100644 index 0000000000..31e75ca7df --- /dev/null +++ b/xfa/fxbarcode/oned/BC_OnedCode39Writer.cpp @@ -0,0 +1,364 @@ +// 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 2010 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/BC_Reader.h" +#include "xfa/fxbarcode/BC_Writer.h" +#include "xfa/fxbarcode/common/BC_CommonBitMatrix.h" +#include "xfa/fxbarcode/oned/BC_OneDReader.h" +#include "xfa/fxbarcode/oned/BC_OneDimWriter.h" +#include "xfa/fxbarcode/oned/BC_OnedCode39Reader.h" +#include "xfa/fxbarcode/oned/BC_OnedCode39Writer.h" + +CBC_OnedCode39Writer::CBC_OnedCode39Writer() { + m_extendedMode = FALSE; + m_iWideNarrRatio = 3; +} +CBC_OnedCode39Writer::CBC_OnedCode39Writer(FX_BOOL extendedMode) { + m_iWideNarrRatio = 3; + m_extendedMode = extendedMode; +} +CBC_OnedCode39Writer::~CBC_OnedCode39Writer() {} +FX_BOOL CBC_OnedCode39Writer::CheckContentValidity( + const CFX_WideStringC& contents) { + if (m_extendedMode) { + return CheckExtendedContentValidity(contents); + } + for (int32_t i = 0; i < contents.GetLength(); i++) { + FX_WCHAR ch = contents.GetAt(i); + if ((ch >= (FX_WCHAR)'0' && ch <= (FX_WCHAR)'9') || + (ch >= (FX_WCHAR)'A' && ch <= (FX_WCHAR)'Z') || ch == (FX_WCHAR)'-' || + ch == (FX_WCHAR)'.' || ch == (FX_WCHAR)' ' || ch == (FX_WCHAR)'*' || + ch == (FX_WCHAR)'$' || ch == (FX_WCHAR)'/' || ch == (FX_WCHAR)'+' || + ch == (FX_WCHAR)'%') { + continue; + } + return FALSE; + } + return TRUE; +} +FX_BOOL CBC_OnedCode39Writer::CheckExtendedContentValidity( + const CFX_WideStringC& contents) { + for (int32_t i = 0; i < contents.GetLength(); i++) { + FX_WCHAR ch = contents.GetAt(i); + if (ch > 127) { + return FALSE; + } + } + return TRUE; +} +CFX_WideString CBC_OnedCode39Writer::FilterContents( + const CFX_WideStringC& contents) { + if (m_extendedMode) { + return FilterExtendedContents(contents); + } + CFX_WideString filtercontents; + for (int32_t i = 0; i < contents.GetLength(); i++) { + FX_WCHAR ch = contents.GetAt(i); + if (ch == (FX_WCHAR)'*' && (i == 0 || i == contents.GetLength() - 1)) { + continue; + } + if (ch > 175) { + i++; + continue; + } else { + ch = Upper(ch); + } + if ((ch >= (FX_WCHAR)'0' && ch <= (FX_WCHAR)'9') || + (ch >= (FX_WCHAR)'A' && ch <= (FX_WCHAR)'Z') || ch == (FX_WCHAR)'-' || + ch == (FX_WCHAR)'.' || ch == (FX_WCHAR)' ' || ch == (FX_WCHAR)'*' || + ch == (FX_WCHAR)'$' || ch == (FX_WCHAR)'/' || ch == (FX_WCHAR)'+' || + ch == (FX_WCHAR)'%') { + filtercontents += ch; + } + } + return filtercontents; +} +CFX_WideString CBC_OnedCode39Writer::FilterExtendedContents( + const CFX_WideStringC& contents) { + CFX_WideString filtercontents; + for (int32_t i = 0; i < contents.GetLength(); i++) { + FX_WCHAR ch = contents.GetAt(i); + if (ch == (FX_WCHAR)'*' && (i == 0 || i == contents.GetLength() - 1)) { + continue; + } + if (ch > 175) { + i++; + continue; + } + if (ch > 127 && ch < 176) { + continue; + } + if (ch == 0) { + filtercontents += '%'; + filtercontents += 'U'; + } else if (ch >= 1 && ch <= 26) { + filtercontents += '$'; + filtercontents += (ch + 64); + } else if (ch >= 27 && ch <= 31) { + filtercontents += '%'; + filtercontents += (ch + 38); + } else if (ch >= 33 && ch <= 47 && ch != 45 && ch != 46) { + filtercontents += '/'; + filtercontents += (ch + 32); + } else if (ch == 58) { + filtercontents += '/'; + filtercontents += 'Z'; + } else if (ch >= 59 && ch <= 63) { + filtercontents += '%'; + filtercontents += ch + 11; + } else if (ch == 64) { + filtercontents += '%'; + filtercontents += 'V'; + } else if (ch >= 91 && ch <= 95) { + filtercontents += '%'; + filtercontents += ch - 16; + } else if (ch == 96) { + filtercontents += '%'; + filtercontents += 'W'; + } else if (ch >= 97 && ch <= 122) { + filtercontents += '+'; + filtercontents += ch - 32; + } else if (ch >= 123 && ch <= 126) { + filtercontents += '%'; + filtercontents += ch - 43; + } else if (ch == 127) { + filtercontents += '%'; + filtercontents += 'T'; + } else { + filtercontents += ch; + } + } + return filtercontents; +} +CFX_WideString CBC_OnedCode39Writer::RenderTextContents( + const CFX_WideStringC& contents) { + if (m_extendedMode) { + return RenderExtendedTextContents(contents); + } + CFX_WideString renderContents; + for (int32_t i = 0; i < contents.GetLength(); i++) { + FX_WCHAR ch = contents.GetAt(i); + if (ch == (FX_WCHAR)'*' && (i == 0 || i == contents.GetLength() - 1)) { + continue; + } + if (ch > 175) { + i++; + continue; + } + if ((ch >= (FX_WCHAR)'0' && ch <= (FX_WCHAR)'9') || + (ch >= (FX_WCHAR)'A' && ch <= (FX_WCHAR)'Z') || + (ch >= (FX_WCHAR)'a' && ch <= (FX_WCHAR)'z') || ch == (FX_WCHAR)'-' || + ch == (FX_WCHAR)'.' || ch == (FX_WCHAR)' ' || ch == (FX_WCHAR)'*' || + ch == (FX_WCHAR)'$' || ch == (FX_WCHAR)'/' || ch == (FX_WCHAR)'+' || + ch == (FX_WCHAR)'%') { + renderContents += ch; + } + } + return renderContents; +} +CFX_WideString CBC_OnedCode39Writer::RenderExtendedTextContents( + const CFX_WideStringC& contents) { + CFX_WideString renderContents; + for (int32_t i = 0; i < contents.GetLength(); i++) { + FX_WCHAR ch = contents.GetAt(i); + if (ch == (FX_WCHAR)'*' && (i == 0 || i == contents.GetLength() - 1)) { + continue; + } + if (ch > 175) { + i++; + continue; + } + if (ch > 127 && ch < 176) { + continue; + } + renderContents += ch; + } + return renderContents; +} +FX_BOOL CBC_OnedCode39Writer::SetTextLocation(BC_TEXT_LOC location) { + if (location < BC_TEXT_LOC_NONE || location > BC_TEXT_LOC_BELOWEMBED) { + return FALSE; + } + m_locTextLoc = location; + return TRUE; +} +FX_BOOL CBC_OnedCode39Writer::SetWideNarrowRatio(int32_t ratio) { + if (ratio < 2 || ratio > 3) { + return FALSE; + } + m_iWideNarrRatio = ratio; + return TRUE; +} +uint8_t* CBC_OnedCode39Writer::Encode(const CFX_ByteString& contents, + BCFORMAT format, + int32_t& outWidth, + int32_t& outHeight, + int32_t& e) { + uint8_t* ret = Encode(contents, format, outWidth, outHeight, 0, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return ret; +} +uint8_t* CBC_OnedCode39Writer::Encode(const CFX_ByteString& contents, + BCFORMAT format, + int32_t& outWidth, + int32_t& outHeight, + int32_t hints, + int32_t& e) { + if (format != BCFORMAT_CODE_39) { + e = BCExceptionOnlyEncodeCODE_39; + return NULL; + } + uint8_t* ret = + CBC_OneDimWriter::Encode(contents, format, outWidth, outHeight, hints, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return ret; +} +void CBC_OnedCode39Writer::ToIntArray(int32_t a, int32_t* toReturn) { + for (int32_t i = 0; i < 9; i++) { + toReturn[i] = (a & (1 << i)) == 0 ? 1 : m_iWideNarrRatio; + } +} +FX_CHAR CBC_OnedCode39Writer::CalcCheckSum(const CFX_ByteString& contents, + int32_t& e) { + int32_t length = contents.GetLength(); + if (length > 80) { + e = BCExceptionContentsLengthShouldBetween1and80; + return '*'; + } + int32_t checksum = 0; + int32_t len = (int32_t)strlen(CBC_OnedCode39Reader::ALPHABET_STRING); + for (int32_t i = 0; i < contents.GetLength(); i++) { + int32_t j = 0; + for (; j < len; j++) { + if (CBC_OnedCode39Reader::ALPHABET_STRING[j] == contents[i]) { + if (contents[i] != '*') { + checksum += j; + break; + } else { + break; + } + } + } + if (j >= len) { + e = BCExceptionUnSupportedString; + return '*'; + } + } + checksum = checksum % 43; + return CBC_OnedCode39Reader::CHECKSUM_STRING[checksum]; +} +uint8_t* CBC_OnedCode39Writer::Encode(const CFX_ByteString& contents, + int32_t& outlength, + int32_t& e) { + FX_CHAR checksum = CalcCheckSum(contents, e); + if (checksum == '*') { + return NULL; + } + int32_t widths[9] = {0}; + int32_t wideStrideNum = 3; + int32_t narrStrideNum = 9 - wideStrideNum; + CFX_ByteString encodedContents = contents; + if (m_bCalcChecksum) { + encodedContents += checksum; + } + m_iContentLen = encodedContents.GetLength(); + int32_t codeWidth = (wideStrideNum * m_iWideNarrRatio + narrStrideNum) * 2 + + 1 + m_iContentLen; + int32_t len = (int32_t)strlen(CBC_OnedCode39Reader::ALPHABET_STRING); + for (int32_t j = 0; j < m_iContentLen; j++) { + for (int32_t i = 0; i < len; i++) { + if (CBC_OnedCode39Reader::ALPHABET_STRING[i] == encodedContents[j]) { + ToIntArray(CBC_OnedCode39Reader::CHARACTER_ENCODINGS[i], widths); + for (int32_t k = 0; k < 9; k++) { + codeWidth += widths[k]; + } + } + } + } + outlength = codeWidth; + uint8_t* result = FX_Alloc(uint8_t, codeWidth); + ToIntArray(CBC_OnedCode39Reader::CHARACTER_ENCODINGS[39], widths); + int32_t pos = AppendPattern(result, 0, widths, 9, 1, e); + if (e != BCExceptionNO) { + FX_Free(result); + return NULL; + } + int32_t narrowWhite[] = {1}; + pos += AppendPattern(result, pos, narrowWhite, 1, 0, e); + if (e != BCExceptionNO) { + FX_Free(result); + return NULL; + } + for (int32_t l = m_iContentLen - 1; l >= 0; l--) { + for (int32_t i = 0; i < len; i++) { + if (CBC_OnedCode39Reader::ALPHABET_STRING[i] == encodedContents[l]) { + ToIntArray(CBC_OnedCode39Reader::CHARACTER_ENCODINGS[i], widths); + pos += AppendPattern(result, pos, widths, 9, 1, e); + if (e != BCExceptionNO) { + FX_Free(result); + return NULL; + } + } + } + pos += AppendPattern(result, pos, narrowWhite, 1, 0, e); + if (e != BCExceptionNO) { + FX_Free(result); + return NULL; + } + } + ToIntArray(CBC_OnedCode39Reader::CHARACTER_ENCODINGS[39], widths); + pos += AppendPattern(result, pos, widths, 9, 1, e); + if (e != BCExceptionNO) { + FX_Free(result); + return NULL; + } + for (int32_t i = 0; i < codeWidth / 2; i++) { + result[i] ^= result[codeWidth - 1 - i]; + result[codeWidth - 1 - i] ^= result[i]; + result[i] ^= result[codeWidth - 1 - i]; + } + return result; +} +CFX_WideString CBC_OnedCode39Writer::encodedContents( + const CFX_WideStringC& contents, + int32_t& e) { + CFX_WideString encodedContents = contents; + if (m_bCalcChecksum && m_bPrintChecksum) { + CFX_WideString checksumContent = FilterContents(contents); + CFX_ByteString str = checksumContent.UTF8Encode(); + FX_CHAR checksum; + checksum = CalcCheckSum(str, e); + BC_EXCEPTION_CHECK_ReturnValue(e, FX_WSTRC(L"")); + str += checksum; + encodedContents += checksum; + } + return encodedContents; +} +void CBC_OnedCode39Writer::RenderResult(const CFX_WideStringC& contents, + uint8_t* code, + int32_t codeLength, + FX_BOOL isDevice, + int32_t& e) { + CFX_WideString encodedCon = encodedContents(contents, e); + BC_EXCEPTION_CHECK_ReturnVoid(e); + CBC_OneDimWriter::RenderResult(encodedCon, code, codeLength, isDevice, e); +} diff --git a/xfa/fxbarcode/oned/BC_OnedCode39Writer.h b/xfa/fxbarcode/oned/BC_OnedCode39Writer.h new file mode 100644 index 0000000000..b438727130 --- /dev/null +++ b/xfa/fxbarcode/oned/BC_OnedCode39Writer.h @@ -0,0 +1,53 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_ONED_BC_ONEDCODE39WRITER_H_ +#define XFA_FXBARCODE_ONED_BC_ONEDCODE39WRITER_H_ + +enum BC_TEXT_LOC; +class CBC_OneDimWriter; +class CBC_OnedCode39Writer : public CBC_OneDimWriter { + public: + CBC_OnedCode39Writer(); + CBC_OnedCode39Writer(FX_BOOL extendedMode); + virtual ~CBC_OnedCode39Writer(); + uint8_t* Encode(const CFX_ByteString& contents, + BCFORMAT format, + int32_t& outWidth, + int32_t& outHeight, + int32_t& e); + uint8_t* Encode(const CFX_ByteString& contents, + BCFORMAT format, + int32_t& outWidth, + int32_t& outHeight, + int32_t hints, + int32_t& e); + uint8_t* Encode(const CFX_ByteString& contents, + int32_t& outLength, + int32_t& e); + void RenderResult(const CFX_WideStringC& contents, + uint8_t* code, + int32_t codeLength, + FX_BOOL isDevice, + int32_t& e); + CFX_WideString encodedContents(const CFX_WideStringC& contents, int32_t& e); + FX_BOOL CheckContentValidity(const CFX_WideStringC& contents); + FX_BOOL CheckExtendedContentValidity(const CFX_WideStringC& contents); + CFX_WideString FilterContents(const CFX_WideStringC& contents); + CFX_WideString FilterExtendedContents(const CFX_WideStringC& contents); + CFX_WideString RenderTextContents(const CFX_WideStringC& contents); + CFX_WideString RenderExtendedTextContents(const CFX_WideStringC& contents); + FX_BOOL SetTextLocation(BC_TEXT_LOC loction); + FX_BOOL SetWideNarrowRatio(int32_t ratio); + + private: + void ToIntArray(int32_t a, int32_t* toReturn); + FX_CHAR CalcCheckSum(const CFX_ByteString& contents, int32_t& e); + int32_t m_iWideNarrRatio; + FX_BOOL m_extendedMode; +}; + +#endif // XFA_FXBARCODE_ONED_BC_ONEDCODE39WRITER_H_ diff --git a/xfa/fxbarcode/oned/BC_OnedEAN13Reader.cpp b/xfa/fxbarcode/oned/BC_OnedEAN13Reader.cpp new file mode 100644 index 0000000000..83fea7d5f2 --- /dev/null +++ b/xfa/fxbarcode/oned/BC_OnedEAN13Reader.cpp @@ -0,0 +1,96 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2008 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/BC_Reader.h" +#include "xfa/fxbarcode/common/BC_CommonBitArray.h" +#include "xfa/fxbarcode/oned/BC_OneDReader.h" +#include "xfa/fxbarcode/oned/BC_OneDimReader.h" +#include "xfa/fxbarcode/oned/BC_OnedEAN13Reader.h" +#include "xfa/fxbarcode/utils.h" + +const int32_t CBC_OnedEAN13Reader::FIRST_DIGIT_ENCODINGS[10] = { + 0x00, 0x0B, 0x0D, 0xE, 0x13, 0x19, 0x1C, 0x15, 0x16, 0x1A}; + +CBC_OnedEAN13Reader::CBC_OnedEAN13Reader() {} +CBC_OnedEAN13Reader::~CBC_OnedEAN13Reader() {} +void CBC_OnedEAN13Reader::DetermineFirstDigit(CFX_ByteString& result, + int32_t lgPatternFound, + int32_t& e) { + for (int32_t d = 0; d < 10; d++) { + if (lgPatternFound == FIRST_DIGIT_ENCODINGS[d]) { + result.Insert(0, (FX_CHAR)('0' + d)); + return; + } + } + e = BCExceptionNotFound; + BC_EXCEPTION_CHECK_ReturnVoid(e); +} +int32_t CBC_OnedEAN13Reader::DecodeMiddle(CBC_CommonBitArray* row, + CFX_Int32Array* startRange, + CFX_ByteString& resultString, + int32_t& e) { + CFX_Int32Array counters; + counters.Add(0); + counters.Add(0); + counters.Add(0); + counters.Add(0); + int32_t end = row->GetSize(); + int32_t rowOffset = (*startRange)[1]; + int32_t lgPatternFound = 0; + for (int32_t x = 0; x < 6 && rowOffset < end; x++) { + int32_t bestMatch = + DecodeDigit(row, &counters, rowOffset, + &(CBC_OneDimReader::L_AND_G_PATTERNS[0][0]), 20, e); + BC_EXCEPTION_CHECK_ReturnValue(e, 0); + resultString += (FX_CHAR)('0' + bestMatch % 10); + for (int32_t i = 0; i < counters.GetSize(); i++) { + rowOffset += counters[i]; + } + if (bestMatch >= 10) { + lgPatternFound |= 1 << (5 - x); + } + } + DetermineFirstDigit(resultString, lgPatternFound, e); + BC_EXCEPTION_CHECK_ReturnValue(e, 0); + CFX_Int32Array result; + result.Add(CBC_OneDimReader::MIDDLE_PATTERN[0]); + result.Add(CBC_OneDimReader::MIDDLE_PATTERN[1]); + result.Add(CBC_OneDimReader::MIDDLE_PATTERN[2]); + result.Add(CBC_OneDimReader::MIDDLE_PATTERN[3]); + result.Add(CBC_OneDimReader::MIDDLE_PATTERN[4]); + CFX_Int32Array* middleRange = + FindGuardPattern(row, rowOffset, TRUE, &result, e); + BC_EXCEPTION_CHECK_ReturnValue(e, 0); + rowOffset = (*middleRange)[1]; + delete middleRange; + for (int32_t Y = 0; Y < 6 && rowOffset < end; Y++) { + int32_t bestMatch = + DecodeDigit(row, &counters, rowOffset, + &(CBC_OneDimReader::L_PATTERNS[0][0]), 10, e); + BC_EXCEPTION_CHECK_ReturnValue(e, 0); + resultString += (FX_CHAR)('0' + bestMatch); + for (int32_t k = 0; k < counters.GetSize(); k++) { + rowOffset += counters[k]; + } + } + return rowOffset; +} diff --git a/xfa/fxbarcode/oned/BC_OnedEAN13Reader.h b/xfa/fxbarcode/oned/BC_OnedEAN13Reader.h new file mode 100644 index 0000000000..0d9d614901 --- /dev/null +++ b/xfa/fxbarcode/oned/BC_OnedEAN13Reader.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 XFA_FXBARCODE_ONED_BC_ONEDEAN13READER_H_ +#define XFA_FXBARCODE_ONED_BC_ONEDEAN13READER_H_ +class CBC_OneDimReader; +class CBC_CommonBitArray; +class CBC_OnedEAN13Reader; +class CBC_OnedEAN13Reader : public CBC_OneDimReader { + public: + static const int32_t FIRST_DIGIT_ENCODINGS[10]; + CBC_OnedEAN13Reader(); + virtual ~CBC_OnedEAN13Reader(); + + private: + void DetermineFirstDigit(CFX_ByteString& result, + int32_t lgPatternFound, + int32_t& e); + + protected: + int32_t DecodeMiddle(CBC_CommonBitArray* row, + CFX_Int32Array* startRange, + CFX_ByteString& resultString, + int32_t& e); + friend class CBC_OnedUPCAReader; +}; + +#endif // XFA_FXBARCODE_ONED_BC_ONEDEAN13READER_H_ diff --git a/xfa/fxbarcode/oned/BC_OnedEAN13Writer.cpp b/xfa/fxbarcode/oned/BC_OnedEAN13Writer.cpp new file mode 100644 index 0000000000..b534602fb3 --- /dev/null +++ b/xfa/fxbarcode/oned/BC_OnedEAN13Writer.cpp @@ -0,0 +1,301 @@ +// 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 2009 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/BC_Reader.h" +#include "xfa/fxbarcode/BC_Writer.h" +#include "xfa/fxbarcode/oned/BC_OneDReader.h" +#include "xfa/fxbarcode/oned/BC_OneDimReader.h" +#include "xfa/fxbarcode/oned/BC_OneDimWriter.h" +#include "xfa/fxbarcode/oned/BC_OnedEAN13Reader.h" +#include "xfa/fxbarcode/oned/BC_OnedEAN13Writer.h" + +CBC_OnedEAN13Writer::CBC_OnedEAN13Writer() { + m_bLeftPadding = TRUE; + m_codeWidth = 3 + (7 * 6) + 5 + (7 * 6) + 3; +} +CBC_OnedEAN13Writer::~CBC_OnedEAN13Writer() {} +FX_BOOL CBC_OnedEAN13Writer::CheckContentValidity( + const CFX_WideStringC& contents) { + for (int32_t i = 0; i < contents.GetLength(); i++) { + if (contents.GetAt(i) >= '0' && contents.GetAt(i) <= '9') { + continue; + } else { + return FALSE; + } + } + return TRUE; +} +CFX_WideString CBC_OnedEAN13Writer::FilterContents( + const CFX_WideStringC& contents) { + CFX_WideString filtercontents; + FX_WCHAR ch; + for (int32_t i = 0; i < contents.GetLength(); i++) { + ch = contents.GetAt(i); + if (ch > 175) { + i++; + continue; + } + if (ch >= '0' && ch <= '9') { + filtercontents += ch; + } + } + return filtercontents; +} +int32_t CBC_OnedEAN13Writer::CalcChecksum(const CFX_ByteString& contents) { + int32_t odd = 0; + int32_t even = 0; + int32_t j = 1; + for (int32_t i = contents.GetLength() - 1; i >= 0; i--) { + if (j % 2) { + odd += FXSYS_atoi(contents.Mid(i, 1)); + } else { + even += FXSYS_atoi(contents.Mid(i, 1)); + } + j++; + } + int32_t checksum = (odd * 3 + even) % 10; + checksum = (10 - checksum) % 10; + return (checksum); +} +uint8_t* CBC_OnedEAN13Writer::Encode(const CFX_ByteString& contents, + BCFORMAT format, + int32_t& outWidth, + int32_t& outHeight, + int32_t& e) { + uint8_t* ret = Encode(contents, format, outWidth, outHeight, 0, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return ret; +} +uint8_t* CBC_OnedEAN13Writer::Encode(const CFX_ByteString& contents, + BCFORMAT format, + int32_t& outWidth, + int32_t& outHeight, + int32_t hints, + int32_t& e) { + if (format != BCFORMAT_EAN_13) { + e = BCExceptionOnlyEncodeEAN_13; + } + uint8_t* ret = + CBC_OneDimWriter::Encode(contents, format, outWidth, outHeight, hints, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return ret; +} +uint8_t* CBC_OnedEAN13Writer::Encode(const CFX_ByteString& contents, + int32_t& outLength, + int32_t& e) { + if (contents.GetLength() != 13) { + e = BCExceptionDigitLengthShould13; + return NULL; + } + m_iDataLenth = 13; + int32_t firstDigit = FXSYS_atoi(contents.Mid(0, 1)); + int32_t parities = CBC_OnedEAN13Reader::FIRST_DIGIT_ENCODINGS[firstDigit]; + outLength = m_codeWidth; + uint8_t* result = FX_Alloc(uint8_t, m_codeWidth); + int32_t pos = 0; + pos += + AppendPattern(result, pos, CBC_OneDimReader::START_END_PATTERN, 3, 1, e); + if (e != BCExceptionNO) { + FX_Free(result); + return NULL; + } + int32_t i = 0; + for (i = 1; i <= 6; i++) { + int32_t digit = FXSYS_atoi(contents.Mid(i, 1)); + if ((parities >> (6 - i) & 1) == 1) { + digit += 10; + } + pos += AppendPattern(result, pos, CBC_OneDimReader::L_AND_G_PATTERNS[digit], + 4, 0, e); + if (e != BCExceptionNO) { + FX_Free(result); + return NULL; + } + } + pos += AppendPattern(result, pos, CBC_OneDimReader::MIDDLE_PATTERN, 5, 0, e); + if (e != BCExceptionNO) { + FX_Free(result); + return NULL; + } + for (i = 7; i <= 12; i++) { + int32_t digit = FXSYS_atoi(contents.Mid(i, 1)); + pos += AppendPattern(result, pos, CBC_OneDimReader::L_PATTERNS[digit], 4, 1, + e); + if (e != BCExceptionNO) { + FX_Free(result); + return NULL; + } + } + pos += + AppendPattern(result, pos, CBC_OneDimReader::START_END_PATTERN, 3, 1, e); + if (e != BCExceptionNO) { + FX_Free(result); + return NULL; + } + return result; +} +void CBC_OnedEAN13Writer::ShowChars(const CFX_WideStringC& contents, + CFX_DIBitmap* pOutBitmap, + CFX_RenderDevice* device, + const CFX_Matrix* matrix, + int32_t barWidth, + int32_t multiple, + int32_t& e) { + if (device == NULL && pOutBitmap == NULL) { + e = BCExceptionIllegalArgument; + return; + } + int32_t leftPadding = 7 * multiple; + int32_t leftPosition = 3 * multiple + leftPadding; + CFX_ByteString str = FX_UTF8Encode(contents); + int32_t iLen = str.GetLength(); + FXTEXT_CHARPOS* pCharPos = FX_Alloc(FXTEXT_CHARPOS, iLen); + FXSYS_memset(pCharPos, 0, sizeof(FXTEXT_CHARPOS) * iLen); + CFX_FxgeDevice geBitmap; + if (pOutBitmap) { + geBitmap.Attach(pOutBitmap); + } + int32_t iFontSize = (int32_t)fabs(m_fFontSize); + int32_t iTextHeight = iFontSize + 1; + CFX_ByteString tempStr = str.Mid(1, 6); + int32_t strWidth = multiple * 42; + if (pOutBitmap == NULL) { + CFX_Matrix matr(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0); + CFX_FloatRect rect( + (FX_FLOAT)leftPosition, (FX_FLOAT)(m_Height - iTextHeight), + (FX_FLOAT)(leftPosition + strWidth - 0.5), (FX_FLOAT)m_Height); + matr.Concat(*matrix); + matr.TransformRect(rect); + FX_RECT re = rect.GetOutterRect(); + device->FillRect(&re, m_backgroundColor); + CFX_FloatRect rect1( + (FX_FLOAT)(leftPosition + 47 * multiple), + (FX_FLOAT)(m_Height - iTextHeight), + (FX_FLOAT)(leftPosition + 47 * multiple + strWidth - 0.5), + (FX_FLOAT)m_Height); + CFX_Matrix matr1(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0); + matr1.Concat(*matrix); + matr1.TransformRect(rect1); + re = rect1.GetOutterRect(); + device->FillRect(&re, m_backgroundColor); + int32_t strWidth1 = multiple * 7; + CFX_Matrix matr2(m_outputHScale, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f); + CFX_FloatRect rect2(0.0f, (FX_FLOAT)(m_Height - iTextHeight), + (FX_FLOAT)strWidth1 - 0.5f, (FX_FLOAT)m_Height); + matr2.Concat(*matrix); + matr2.TransformRect(rect2); + re = rect2.GetOutterRect(); + device->FillRect(&re, m_backgroundColor); + } + FX_FLOAT blank = 0.0; + iLen = tempStr.GetLength(); + if (pOutBitmap == NULL) { + strWidth = (int32_t)(strWidth * m_outputHScale); + } + CalcTextInfo(tempStr, pCharPos + 1, m_pFont, (FX_FLOAT)strWidth, iFontSize, + blank); + CFX_Matrix affine_matrix(1.0, 0.0, 0.0, -1.0, 0.0, (FX_FLOAT)iFontSize); + CFX_FxgeDevice ge; + if (pOutBitmap) { + ge.Create(strWidth, iTextHeight, FXDIB_Argb); + FX_RECT rect(0, 0, strWidth, iTextHeight); + ge.FillRect(&rect, m_backgroundColor); + ge.DrawNormalText(iLen, pCharPos + 1, m_pFont, + CFX_GEModule::Get()->GetFontCache(), (FX_FLOAT)iFontSize, + (CFX_Matrix*)&affine_matrix, m_fontColor, + FXTEXT_CLEARTYPE); + geBitmap.SetDIBits(ge.GetBitmap(), leftPosition, m_Height - iTextHeight); + } else { + CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0, + (FX_FLOAT)leftPosition * m_outputHScale, + (FX_FLOAT)(m_Height - iTextHeight) + iFontSize); + if (matrix) { + affine_matrix1.Concat(*matrix); + } + device->DrawNormalText(iLen, pCharPos + 1, m_pFont, + CFX_GEModule::Get()->GetFontCache(), + (FX_FLOAT)iFontSize, (CFX_Matrix*)&affine_matrix1, + m_fontColor, FXTEXT_CLEARTYPE); + } + tempStr = str.Mid(7, 6); + iLen = tempStr.GetLength(); + CalcTextInfo(tempStr, pCharPos + 7, m_pFont, (FX_FLOAT)strWidth, iFontSize, + blank); + if (pOutBitmap) { + FX_RECT rect1(0, 0, strWidth, iTextHeight); + ge.FillRect(&rect1, m_backgroundColor); + ge.DrawNormalText(iLen, pCharPos + 7, m_pFont, + CFX_GEModule::Get()->GetFontCache(), (FX_FLOAT)iFontSize, + (CFX_Matrix*)&affine_matrix, m_fontColor, + FXTEXT_CLEARTYPE); + geBitmap.SetDIBits(ge.GetBitmap(), leftPosition + 47 * multiple, + m_Height - iTextHeight); + } else { + CFX_Matrix affine_matrix1( + 1.0, 0.0, 0.0, -1.0, + (FX_FLOAT)(leftPosition + 47 * multiple) * m_outputHScale, + (FX_FLOAT)(m_Height - iTextHeight + iFontSize)); + if (matrix) { + affine_matrix1.Concat(*matrix); + } + device->DrawNormalText(iLen, pCharPos + 7, m_pFont, + CFX_GEModule::Get()->GetFontCache(), + (FX_FLOAT)iFontSize, (CFX_Matrix*)&affine_matrix1, + m_fontColor, FXTEXT_CLEARTYPE); + } + tempStr = str.Mid(0, 1); + iLen = tempStr.GetLength(); + strWidth = multiple * 7; + if (pOutBitmap == NULL) { + strWidth = (int32_t)(strWidth * m_outputHScale); + } + CalcTextInfo(tempStr, pCharPos, m_pFont, (FX_FLOAT)strWidth, iFontSize, + blank); + if (pOutBitmap) { + delete ge.GetBitmap(); + ge.Create(strWidth, iTextHeight, FXDIB_Argb); + ge.GetBitmap()->Clear(m_backgroundColor); + ge.DrawNormalText(iLen, pCharPos, m_pFont, + CFX_GEModule::Get()->GetFontCache(), (FX_FLOAT)iFontSize, + (CFX_Matrix*)&affine_matrix, m_fontColor, + FXTEXT_CLEARTYPE); + geBitmap.SetDIBits(ge.GetBitmap(), 0, m_Height - iTextHeight); + } else { + CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0, 0.0, + (FX_FLOAT)(m_Height - iTextHeight + iFontSize)); + if (matrix) { + affine_matrix1.Concat(*matrix); + } + device->DrawNormalText(iLen, pCharPos, m_pFont, + CFX_GEModule::Get()->GetFontCache(), + (FX_FLOAT)iFontSize, (CFX_Matrix*)&affine_matrix1, + m_fontColor, FXTEXT_CLEARTYPE); + } + FX_Free(pCharPos); +} +void CBC_OnedEAN13Writer::RenderResult(const CFX_WideStringC& contents, + uint8_t* code, + int32_t codeLength, + FX_BOOL isDevice, + int32_t& e) { + CBC_OneDimWriter::RenderResult(contents, code, codeLength, isDevice, e); +} diff --git a/xfa/fxbarcode/oned/BC_OnedEAN13Writer.h b/xfa/fxbarcode/oned/BC_OnedEAN13Writer.h new file mode 100644 index 0000000000..fd23129975 --- /dev/null +++ b/xfa/fxbarcode/oned/BC_OnedEAN13Writer.h @@ -0,0 +1,53 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_ONED_BC_ONEDEAN13WRITER_H_ +#define XFA_FXBARCODE_ONED_BC_ONEDEAN13WRITER_H_ + +class CBC_OneDimWriter; +class CBC_OnedEAN13Writer; +class CBC_OnedEAN13Writer : public CBC_OneDimWriter { + private: + int32_t m_codeWidth; + + public: + CBC_OnedEAN13Writer(); + virtual ~CBC_OnedEAN13Writer(); + + uint8_t* Encode(const CFX_ByteString& contents, + BCFORMAT format, + int32_t& outWidth, + int32_t& outHeight, + int32_t& e); + uint8_t* Encode(const CFX_ByteString& contents, + BCFORMAT format, + int32_t& outWidth, + int32_t& outHeight, + int32_t hints, + int32_t& e); + uint8_t* Encode(const CFX_ByteString& contents, + int32_t& outLength, + int32_t& e); + void RenderResult(const CFX_WideStringC& contents, + uint8_t* code, + int32_t codeLength, + FX_BOOL isDevice, + int32_t& e); + FX_BOOL CheckContentValidity(const CFX_WideStringC& contents); + CFX_WideString FilterContents(const CFX_WideStringC& contents); + int32_t CalcChecksum(const CFX_ByteString& contents); + + protected: + void ShowChars(const CFX_WideStringC& contents, + CFX_DIBitmap* pOutBitmap, + CFX_RenderDevice* device, + const CFX_Matrix* matrix, + int32_t barWidth, + int32_t multiple, + int32_t& e); +}; + +#endif // XFA_FXBARCODE_ONED_BC_ONEDEAN13WRITER_H_ diff --git a/xfa/fxbarcode/oned/BC_OnedEAN8Reader.cpp b/xfa/fxbarcode/oned/BC_OnedEAN8Reader.cpp new file mode 100644 index 0000000000..f3b21fde99 --- /dev/null +++ b/xfa/fxbarcode/oned/BC_OnedEAN8Reader.cpp @@ -0,0 +1,82 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com +// Original code is licensed as follows: +/* + * Copyright 2008 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/BC_Reader.h" +#include "xfa/fxbarcode/common/BC_CommonBitArray.h" +#include "xfa/fxbarcode/oned/BC_OneDReader.h" +#include "xfa/fxbarcode/oned/BC_OneDimReader.h" +#include "xfa/fxbarcode/oned/BC_OnedEAN8Reader.h" +#include "xfa/fxbarcode/utils.h" + +CBC_OnedEAN8Reader::CBC_OnedEAN8Reader() {} +CBC_OnedEAN8Reader::~CBC_OnedEAN8Reader() {} +int32_t CBC_OnedEAN8Reader::DecodeMiddle(CBC_CommonBitArray* row, + CFX_Int32Array* startRange, + CFX_ByteString& resultResult, + int32_t& e) { + CFX_Int32Array counters; + counters.Add(0); + counters.Add(0); + counters.Add(0); + counters.Add(0); + int32_t end = row->GetSize(); + int32_t rowOffset = (*startRange)[1]; + int32_t rowOffsetLeft = rowOffset; + for (int32_t x = 0; x < 4 && rowOffset < end; x++) { + int32_t bestMatch = + DecodeDigit(row, &counters, rowOffset, + &(CBC_OneDimReader::L_PATTERNS[0][0]), 10, e); + BC_EXCEPTION_CHECK_ReturnValue(e, 0); + resultResult += (FX_CHAR)('0' + bestMatch); + for (int32_t i = 0; i < counters.GetSize(); i++) { + rowOffset += counters[i]; + } + } + int32_t RowOffsetLen = (rowOffset - rowOffsetLeft) / 4; + CFX_Int32Array result; + result.Add(CBC_OneDimReader::MIDDLE_PATTERN[0]); + result.Add(CBC_OneDimReader::MIDDLE_PATTERN[1]); + result.Add(CBC_OneDimReader::MIDDLE_PATTERN[2]); + result.Add(CBC_OneDimReader::MIDDLE_PATTERN[3]); + result.Add(CBC_OneDimReader::MIDDLE_PATTERN[4]); + CFX_Int32Array* middleRange = + FindGuardPattern(row, rowOffset, TRUE, &result, e); + BC_EXCEPTION_CHECK_ReturnValue(e, 0); + int32_t rowOffsetMid = rowOffset; + rowOffset = (*middleRange)[1]; + if ((rowOffset - rowOffsetMid) > RowOffsetLen) { + e = BCExceptionNotFound; + BC_EXCEPTION_CHECK_ReturnValue(e, 0); + } + delete middleRange; + for (int32_t y = 0; y < 4 && rowOffset < end; y++) { + int32_t bestMatch = + DecodeDigit(row, &counters, rowOffset, + &(CBC_OneDimReader::L_PATTERNS[0][0]), 10, e); + BC_EXCEPTION_CHECK_ReturnValue(e, 0); + resultResult += (FX_CHAR)('0' + bestMatch); + for (int32_t i = 0; i < counters.GetSize(); i++) { + rowOffset += counters[i]; + } + } + return rowOffset; +} diff --git a/xfa/fxbarcode/oned/BC_OnedEAN8Reader.h b/xfa/fxbarcode/oned/BC_OnedEAN8Reader.h new file mode 100644 index 0000000000..fe2d33d66f --- /dev/null +++ b/xfa/fxbarcode/oned/BC_OnedEAN8Reader.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 XFA_FXBARCODE_ONED_BC_ONEDEAN8READER_H_ +#define XFA_FXBARCODE_ONED_BC_ONEDEAN8READER_H_ + +class CBC_OneDimReader; +class CBC_CommonBitArray; +class CBC_OnedEAN8Reader; +class CBC_OnedEAN8Reader : public CBC_OneDimReader { + public: + CBC_OnedEAN8Reader(); + virtual ~CBC_OnedEAN8Reader(); + + protected: + int32_t DecodeMiddle(CBC_CommonBitArray*, + CFX_Int32Array* startRange, + CFX_ByteString& result, + int32_t& e); +}; + +#endif // XFA_FXBARCODE_ONED_BC_ONEDEAN8READER_H_ diff --git a/xfa/fxbarcode/oned/BC_OnedEAN8Writer.cpp b/xfa/fxbarcode/oned/BC_OnedEAN8Writer.cpp new file mode 100644 index 0000000000..5336867538 --- /dev/null +++ b/xfa/fxbarcode/oned/BC_OnedEAN8Writer.cpp @@ -0,0 +1,268 @@ +// 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 2009 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/BC_Reader.h" +#include "xfa/fxbarcode/BC_Writer.h" +#include "xfa/fxbarcode/common/BC_CommonBitMatrix.h" +#include "xfa/fxbarcode/oned/BC_OneDReader.h" +#include "xfa/fxbarcode/oned/BC_OneDimReader.h" +#include "xfa/fxbarcode/oned/BC_OneDimWriter.h" +#include "xfa/fxbarcode/oned/BC_OnedEAN8Writer.h" + +CBC_OnedEAN8Writer::CBC_OnedEAN8Writer() { + m_iDataLenth = 8; + m_codeWidth = 3 + (7 * 4) + 5 + (7 * 4) + 3; +} +CBC_OnedEAN8Writer::~CBC_OnedEAN8Writer() {} +void CBC_OnedEAN8Writer::SetDataLength(int32_t length) { + m_iDataLenth = 8; +} +FX_BOOL CBC_OnedEAN8Writer::SetTextLocation(BC_TEXT_LOC location) { + if (location == BC_TEXT_LOC_BELOWEMBED) { + m_locTextLoc = location; + return TRUE; + } + return FALSE; +} +FX_BOOL CBC_OnedEAN8Writer::CheckContentValidity( + const CFX_WideStringC& contents) { + for (int32_t i = 0; i < contents.GetLength(); i++) { + if (contents.GetAt(i) >= '0' && contents.GetAt(i) <= '9') { + continue; + } else { + return FALSE; + } + } + return TRUE; +} +CFX_WideString CBC_OnedEAN8Writer::FilterContents( + const CFX_WideStringC& contents) { + CFX_WideString filtercontents; + FX_WCHAR ch; + for (int32_t i = 0; i < contents.GetLength(); i++) { + ch = contents.GetAt(i); + if (ch > 175) { + i++; + continue; + } + if (ch >= '0' && ch <= '9') { + filtercontents += ch; + } + } + return filtercontents; +} +int32_t CBC_OnedEAN8Writer::CalcChecksum(const CFX_ByteString& contents) { + int32_t odd = 0; + int32_t even = 0; + int32_t j = 1; + for (int32_t i = contents.GetLength() - 1; i >= 0; i--) { + if (j % 2) { + odd += FXSYS_atoi(contents.Mid(i, 1)); + } else { + even += FXSYS_atoi(contents.Mid(i, 1)); + } + j++; + } + int32_t checksum = (odd * 3 + even) % 10; + checksum = (10 - checksum) % 10; + return (checksum); +} +uint8_t* CBC_OnedEAN8Writer::Encode(const CFX_ByteString& contents, + BCFORMAT format, + int32_t& outWidth, + int32_t& outHeight, + int32_t& e) { + uint8_t* ret = Encode(contents, format, outWidth, outHeight, 0, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return ret; +} +uint8_t* CBC_OnedEAN8Writer::Encode(const CFX_ByteString& contents, + BCFORMAT format, + int32_t& outWidth, + int32_t& outHeight, + int32_t hints, + int32_t& e) { + if (format != BCFORMAT_EAN_8) { + e = BCExceptionOnlyEncodeEAN_8; + return NULL; + } + uint8_t* ret = + CBC_OneDimWriter::Encode(contents, format, outWidth, outHeight, hints, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return ret; +} +uint8_t* CBC_OnedEAN8Writer::Encode(const CFX_ByteString& contents, + int32_t& outLength, + int32_t& e) { + if (contents.GetLength() != 8) { + e = BCExceptionDigitLengthMustBe8; + return NULL; + } + outLength = m_codeWidth; + uint8_t* result = FX_Alloc(uint8_t, m_codeWidth); + int32_t pos = 0; + pos += + AppendPattern(result, pos, CBC_OneDimReader::START_END_PATTERN, 3, 1, e); + if (e != BCExceptionNO) { + FX_Free(result); + return NULL; + } + int32_t i = 0; + for (i = 0; i <= 3; i++) { + int32_t digit = FXSYS_atoi(contents.Mid(i, 1)); + pos += AppendPattern(result, pos, CBC_OneDimReader::L_PATTERNS[digit], 4, 0, + e); + if (e != BCExceptionNO) { + FX_Free(result); + return NULL; + } + } + pos += AppendPattern(result, pos, CBC_OneDimReader::MIDDLE_PATTERN, 5, 0, e); + if (e != BCExceptionNO) { + FX_Free(result); + return NULL; + } + for (i = 4; i <= 7; i++) { + int32_t digit = FXSYS_atoi(contents.Mid(i, 1)); + pos += AppendPattern(result, pos, CBC_OneDimReader::L_PATTERNS[digit], 4, 1, + e); + if (e != BCExceptionNO) { + FX_Free(result); + return NULL; + } + } + pos += + AppendPattern(result, pos, CBC_OneDimReader::START_END_PATTERN, 3, 1, e); + if (e != BCExceptionNO) { + FX_Free(result); + return NULL; + } + return result; +} +void CBC_OnedEAN8Writer::ShowChars(const CFX_WideStringC& contents, + CFX_DIBitmap* pOutBitmap, + CFX_RenderDevice* device, + const CFX_Matrix* matrix, + int32_t barWidth, + int32_t multiple, + int32_t& e) { + if (device == NULL && pOutBitmap == NULL) { + e = BCExceptionIllegalArgument; + return; + } + int32_t leftPosition = 3 * multiple; + CFX_ByteString str = FX_UTF8Encode(contents); + int32_t iLength = str.GetLength(); + FXTEXT_CHARPOS* pCharPos = FX_Alloc(FXTEXT_CHARPOS, iLength); + FXSYS_memset(pCharPos, 0, sizeof(FXTEXT_CHARPOS) * iLength); + CFX_ByteString tempStr = str.Mid(0, 4); + int32_t iLen = tempStr.GetLength(); + int32_t strWidth = 7 * multiple * 4; + FX_FLOAT blank = 0.0; + CFX_FxgeDevice geBitmap; + if (pOutBitmap) { + geBitmap.Attach(pOutBitmap); + } + int32_t iFontSize = (int32_t)fabs(m_fFontSize); + int32_t iTextHeight = iFontSize + 1; + if (pOutBitmap == NULL) { + CFX_Matrix matr(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0); + CFX_FloatRect rect( + (FX_FLOAT)leftPosition, (FX_FLOAT)(m_Height - iTextHeight), + (FX_FLOAT)(leftPosition + strWidth - 0.5), (FX_FLOAT)m_Height); + matr.Concat(*matrix); + matr.TransformRect(rect); + FX_RECT re = rect.GetOutterRect(); + device->FillRect(&re, m_backgroundColor); + CFX_Matrix matr1(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0); + CFX_FloatRect rect1( + (FX_FLOAT)(leftPosition + 33 * multiple), + (FX_FLOAT)(m_Height - iTextHeight), + (FX_FLOAT)(leftPosition + 33 * multiple + strWidth - 0.5), + (FX_FLOAT)m_Height); + matr1.Concat(*matrix); + matr1.TransformRect(rect1); + re = rect1.GetOutterRect(); + device->FillRect(&re, m_backgroundColor); + } + if (pOutBitmap == NULL) { + strWidth = (int32_t)(strWidth * m_outputHScale); + } + CalcTextInfo(tempStr, pCharPos, m_pFont, (FX_FLOAT)strWidth, iFontSize, + blank); + CFX_Matrix affine_matrix(1.0, 0.0, 0.0, -1.0, 0.0, (FX_FLOAT)iFontSize); + CFX_FxgeDevice ge; + if (pOutBitmap) { + delete ge.GetBitmap(); + ge.Create(strWidth, iTextHeight, FXDIB_Argb); + ge.GetBitmap()->Clear(m_backgroundColor); + ge.DrawNormalText(iLen, pCharPos, m_pFont, + CFX_GEModule::Get()->GetFontCache(), (FX_FLOAT)iFontSize, + (CFX_Matrix*)&affine_matrix, m_fontColor, + FXTEXT_CLEARTYPE); + geBitmap.SetDIBits(ge.GetBitmap(), leftPosition, m_Height - iTextHeight); + } else { + CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0, + (FX_FLOAT)leftPosition * m_outputHScale, + (FX_FLOAT)(m_Height - iTextHeight + iFontSize)); + affine_matrix1.Concat(*matrix); + device->DrawNormalText(iLen, pCharPos, m_pFont, + CFX_GEModule::Get()->GetFontCache(), + (FX_FLOAT)iFontSize, (CFX_Matrix*)&affine_matrix1, + m_fontColor, FXTEXT_CLEARTYPE); + } + tempStr = str.Mid(4, 4); + iLen = tempStr.GetLength(); + CalcTextInfo(tempStr, pCharPos + 4, m_pFont, (FX_FLOAT)strWidth, iFontSize, + blank); + if (pOutBitmap) { + delete ge.GetBitmap(); + ge.Create(strWidth, iTextHeight, FXDIB_Argb); + ge.GetBitmap()->Clear(m_backgroundColor); + ge.DrawNormalText(iLen, pCharPos + 4, m_pFont, + CFX_GEModule::Get()->GetFontCache(), (FX_FLOAT)iFontSize, + (CFX_Matrix*)&affine_matrix, m_fontColor, + FXTEXT_CLEARTYPE); + geBitmap.SetDIBits(ge.GetBitmap(), leftPosition + 33 * multiple, + m_Height - iTextHeight); + } else { + CFX_Matrix affine_matrix1( + 1.0, 0.0, 0.0, -1.0, + (FX_FLOAT)(leftPosition + 33 * multiple) * m_outputHScale, + (FX_FLOAT)(m_Height - iTextHeight + iFontSize)); + if (matrix) { + affine_matrix1.Concat(*matrix); + } + device->DrawNormalText(iLen, pCharPos + 4, m_pFont, + CFX_GEModule::Get()->GetFontCache(), + (FX_FLOAT)iFontSize, (CFX_Matrix*)&affine_matrix1, + m_fontColor, FXTEXT_CLEARTYPE); + } + FX_Free(pCharPos); +} +void CBC_OnedEAN8Writer::RenderResult(const CFX_WideStringC& contents, + uint8_t* code, + int32_t codeLength, + FX_BOOL isDevice, + int32_t& e) { + CBC_OneDimWriter::RenderResult(contents, code, codeLength, isDevice, e); +} diff --git a/xfa/fxbarcode/oned/BC_OnedEAN8Writer.h b/xfa/fxbarcode/oned/BC_OnedEAN8Writer.h new file mode 100644 index 0000000000..fc033ffcbf --- /dev/null +++ b/xfa/fxbarcode/oned/BC_OnedEAN8Writer.h @@ -0,0 +1,55 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_ONED_BC_ONEDEAN8WRITER_H_ +#define XFA_FXBARCODE_ONED_BC_ONEDEAN8WRITER_H_ + +enum BC_TEXT_LOC; +class CBC_OneDimWriter; +class CBC_OnedEAN8Writer; +class CBC_OnedEAN8Writer : public CBC_OneDimWriter { + private: + int32_t m_codeWidth; + + public: + CBC_OnedEAN8Writer(); + virtual ~CBC_OnedEAN8Writer(); + uint8_t* Encode(const CFX_ByteString& contents, + BCFORMAT format, + int32_t& outWidth, + int32_t& outHeight, + int32_t& e); + uint8_t* Encode(const CFX_ByteString& contents, + BCFORMAT format, + int32_t& outWidth, + int32_t& outHeight, + int32_t hints, + int32_t& e); + uint8_t* Encode(const CFX_ByteString& contents, + int32_t& outLength, + int32_t& e); + void RenderResult(const CFX_WideStringC& contents, + uint8_t* code, + int32_t codeLength, + FX_BOOL isDevice, + int32_t& e); + FX_BOOL CheckContentValidity(const CFX_WideStringC& contents); + CFX_WideString FilterContents(const CFX_WideStringC& contents); + void SetDataLength(int32_t length); + FX_BOOL SetTextLocation(BC_TEXT_LOC location); + int32_t CalcChecksum(const CFX_ByteString& contents); + + protected: + void ShowChars(const CFX_WideStringC& contents, + CFX_DIBitmap* pOutBitmap, + CFX_RenderDevice* device, + const CFX_Matrix* matrix, + int32_t barWidth, + int32_t multiple, + int32_t& e); +}; + +#endif // XFA_FXBARCODE_ONED_BC_ONEDEAN8WRITER_H_ diff --git a/xfa/fxbarcode/oned/BC_OnedUPCAReader.cpp b/xfa/fxbarcode/oned/BC_OnedUPCAReader.cpp new file mode 100644 index 0000000000..95b0f24775 --- /dev/null +++ b/xfa/fxbarcode/oned/BC_OnedUPCAReader.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 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 "core/include/fxcrt/fx_basic.h" +#include "xfa/fxbarcode/BC_Reader.h" +#include "xfa/fxbarcode/oned/BC_OneDReader.h" +#include "xfa/fxbarcode/oned/BC_OneDimReader.h" +#include "xfa/fxbarcode/oned/BC_OnedEAN13Reader.h" +#include "xfa/fxbarcode/oned/BC_OnedUPCAReader.h" +#include "xfa/fxbarcode/utils.h" + +CBC_OnedUPCAReader::CBC_OnedUPCAReader() { + m_ean13Reader = NULL; +} +void CBC_OnedUPCAReader::Init() { + m_ean13Reader = new CBC_OnedEAN13Reader; +} +CBC_OnedUPCAReader::~CBC_OnedUPCAReader() { + delete m_ean13Reader; +} +CFX_ByteString CBC_OnedUPCAReader::DecodeRow(int32_t rowNumber, + CBC_CommonBitArray* row, + int32_t hints, + int32_t& e) { + CFX_ByteString bytestring = + m_ean13Reader->DecodeRow(rowNumber, row, hints, e); + BC_EXCEPTION_CHECK_ReturnValue(e, ""); + CFX_ByteString temp = MaybeReturnResult(bytestring, e); + BC_EXCEPTION_CHECK_ReturnValue(e, ""); + return temp; +} +CFX_ByteString CBC_OnedUPCAReader::DecodeRow(int32_t rowNumber, + CBC_CommonBitArray* row, + CFX_Int32Array* startGuardRange, + int32_t hints, + int32_t& e) { + CFX_ByteString bytestring = + m_ean13Reader->DecodeRow(rowNumber, row, startGuardRange, hints, e); + BC_EXCEPTION_CHECK_ReturnValue(e, ""); + CFX_ByteString temp = MaybeReturnResult(bytestring, e); + BC_EXCEPTION_CHECK_ReturnValue(e, ""); + return temp; +} +CFX_ByteString CBC_OnedUPCAReader::Decode(CBC_BinaryBitmap* image, int32_t& e) { + CFX_ByteString bytestring = m_ean13Reader->Decode(image, e); + BC_EXCEPTION_CHECK_ReturnValue(e, ""); + CFX_ByteString temp = MaybeReturnResult(bytestring, e); + BC_EXCEPTION_CHECK_ReturnValue(e, ""); + return temp; +} +CFX_ByteString CBC_OnedUPCAReader::Decode(CBC_BinaryBitmap* image, + int32_t hints, + int32_t& e) { + CFX_ByteString bytestring = m_ean13Reader->Decode(image, hints, e); + BC_EXCEPTION_CHECK_ReturnValue(e, ""); + CFX_ByteString temp = MaybeReturnResult(bytestring, e); + BC_EXCEPTION_CHECK_ReturnValue(e, ""); + return temp; +} +int32_t CBC_OnedUPCAReader::DecodeMiddle(CBC_CommonBitArray* row, + CFX_Int32Array* startRange, + CFX_ByteString& resultString, + int32_t& e) { + int32_t temp = m_ean13Reader->DecodeMiddle(row, startRange, resultString, e); + BC_EXCEPTION_CHECK_ReturnValue(e, 0); + return temp; +} +CFX_ByteString CBC_OnedUPCAReader::MaybeReturnResult(CFX_ByteString& result, + int32_t& e) { + if (result[0] == '0') { + result.Delete(0); + return result; + } else { + e = BCExceptionFormatException; + return ""; + } + return ""; +} diff --git a/xfa/fxbarcode/oned/BC_OnedUPCAReader.h b/xfa/fxbarcode/oned/BC_OnedUPCAReader.h new file mode 100644 index 0000000000..306683e1cc --- /dev/null +++ b/xfa/fxbarcode/oned/BC_OnedUPCAReader.h @@ -0,0 +1,43 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_ONED_BC_ONEDUPCAREADER_H_ +#define XFA_FXBARCODE_ONED_BC_ONEDUPCAREADER_H_ + +class CBC_BinaryBitmap; +class CBC_CommonBitArray; +class CBC_OneDimReader; +class CBC_OnedEAN13Reader; +class CBC_OnedUPCAReader; +class CBC_OnedUPCAReader : public CBC_OneDimReader { + private: + CBC_OnedEAN13Reader* m_ean13Reader; + + public: + CBC_OnedUPCAReader(); + virtual ~CBC_OnedUPCAReader(); + CFX_ByteString DecodeRow(int32_t rowNumber, + CBC_CommonBitArray* row, + int32_t hints, + int32_t& e); + CFX_ByteString DecodeRow(int32_t rowNumber, + CBC_CommonBitArray* row, + CFX_Int32Array* startGuardRange, + int32_t hints, + int32_t& e); + CFX_ByteString Decode(CBC_BinaryBitmap* image, int32_t& e); + CFX_ByteString Decode(CBC_BinaryBitmap* image, int32_t hints, int32_t& e); + virtual void Init(); + + protected: + int32_t DecodeMiddle(CBC_CommonBitArray* row, + CFX_Int32Array* startRange, + CFX_ByteString& resultString, + int32_t& e); + CFX_ByteString MaybeReturnResult(CFX_ByteString& result, int32_t& e); +}; + +#endif // XFA_FXBARCODE_ONED_BC_ONEDUPCAREADER_H_ diff --git a/xfa/fxbarcode/oned/BC_OnedUPCAWriter.cpp b/xfa/fxbarcode/oned/BC_OnedUPCAWriter.cpp new file mode 100644 index 0000000000..21ccafc20c --- /dev/null +++ b/xfa/fxbarcode/oned/BC_OnedUPCAWriter.cpp @@ -0,0 +1,286 @@ +// 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 2010 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "xfa/fxbarcode/BC_Writer.h" +#include "xfa/fxbarcode/oned/BC_OneDimWriter.h" +#include "xfa/fxbarcode/oned/BC_OnedEAN13Writer.h" +#include "xfa/fxbarcode/oned/BC_OnedUPCAWriter.h" + +CBC_OnedUPCAWriter::CBC_OnedUPCAWriter() { + m_subWriter = NULL; + m_bLeftPadding = TRUE; + m_bRightPadding = TRUE; +} +void CBC_OnedUPCAWriter::Init() { + m_subWriter = new CBC_OnedEAN13Writer; +} +CBC_OnedUPCAWriter::~CBC_OnedUPCAWriter() { + delete m_subWriter; +} +FX_BOOL CBC_OnedUPCAWriter::CheckContentValidity( + const CFX_WideStringC& contents) { + int32_t i = 0; + for (i = 0; i < contents.GetLength(); i++) { + if (contents.GetAt(i) >= '0' && contents.GetAt(i) <= '9') { + continue; + } else { + return FALSE; + } + } + return TRUE; +} +CFX_WideString CBC_OnedUPCAWriter::FilterContents( + const CFX_WideStringC& contents) { + CFX_WideString filtercontents; + FX_WCHAR ch; + for (int32_t i = 0; i < contents.GetLength(); i++) { + ch = contents.GetAt(i); + if (ch > 175) { + i++; + continue; + } + if (ch >= '0' && ch <= '9') { + filtercontents += ch; + } + } + return filtercontents; +} +int32_t CBC_OnedUPCAWriter::CalcChecksum(const CFX_ByteString& contents) { + int32_t odd = 0; + int32_t even = 0; + int32_t j = 1; + for (int32_t i = contents.GetLength() - 1; i >= 0; i--) { + if (j % 2) { + odd += FXSYS_atoi(contents.Mid(i, 1)); + } else { + even += FXSYS_atoi(contents.Mid(i, 1)); + } + j++; + } + int32_t checksum = (odd * 3 + even) % 10; + checksum = (10 - checksum) % 10; + return (checksum); +} +uint8_t* CBC_OnedUPCAWriter::Encode(const CFX_ByteString& contents, + BCFORMAT format, + int32_t& outWidth, + int32_t& outHeight, + int32_t& e) { + uint8_t* ret = Encode(contents, format, outWidth, outHeight, 0, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return ret; +} +uint8_t* CBC_OnedUPCAWriter::Encode(const CFX_ByteString& contents, + BCFORMAT format, + int32_t& outWidth, + int32_t& outHeight, + int32_t hints, + int32_t& e) { + if (format != BCFORMAT_UPC_A) { + e = BCExceptionOnlyEncodeUPC_A; + return NULL; + } + CFX_ByteString toEAN13String = '0' + contents; + m_iDataLenth = 13; + uint8_t* ret = m_subWriter->Encode(toEAN13String, BCFORMAT_EAN_13, outWidth, + outHeight, hints, e); + BC_EXCEPTION_CHECK_ReturnValue(e, NULL); + return ret; +} +void CBC_OnedUPCAWriter::ShowChars(const CFX_WideStringC& contents, + CFX_DIBitmap* pOutBitmap, + CFX_RenderDevice* device, + const CFX_Matrix* matrix, + int32_t barWidth, + int32_t multiple, + int32_t& e) { + if (device == NULL && pOutBitmap == NULL) { + e = BCExceptionIllegalArgument; + return; + } + int32_t leftPadding = 7 * multiple; + int32_t leftPosition = 10 * multiple + leftPadding; + CFX_ByteString str = FX_UTF8Encode(contents); + int32_t iLen = str.GetLength(); + FXTEXT_CHARPOS* pCharPos = FX_Alloc(FXTEXT_CHARPOS, iLen); + FXSYS_memset(pCharPos, 0, sizeof(FXTEXT_CHARPOS) * iLen); + CFX_ByteString tempStr = str.Mid(1, 5); + FX_FLOAT strWidth = (FX_FLOAT)35 * multiple; + FX_FLOAT blank = 0.0; + CFX_FxgeDevice geBitmap; + if (pOutBitmap) { + geBitmap.Attach(pOutBitmap); + } + iLen = tempStr.GetLength(); + int32_t iFontSize = (int32_t)fabs(m_fFontSize); + int32_t iTextHeight = iFontSize + 1; + if (pOutBitmap == NULL) { + CFX_Matrix matr(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0); + CFX_FloatRect rect( + (FX_FLOAT)leftPosition, (FX_FLOAT)(m_Height - iTextHeight), + (FX_FLOAT)(leftPosition + strWidth - 0.5), (FX_FLOAT)m_Height); + matr.Concat(*matrix); + matr.TransformRect(rect); + FX_RECT re = rect.GetOutterRect(); + device->FillRect(&re, m_backgroundColor); + CFX_Matrix matr1(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0); + CFX_FloatRect rect1( + (FX_FLOAT)(leftPosition + 40 * multiple), + (FX_FLOAT)(m_Height - iTextHeight), + (FX_FLOAT)((leftPosition + 40 * multiple) + strWidth - 0.5), + (FX_FLOAT)m_Height); + matr1.Concat(*matrix); + matr1.TransformRect(rect1); + re = rect1.GetOutterRect(); + device->FillRect(&re, m_backgroundColor); + FX_FLOAT strWidth1 = (FX_FLOAT)multiple * 7; + CFX_Matrix matr2(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0); + CFX_FloatRect rect2(0.0, (FX_FLOAT)(m_Height - iTextHeight), + (FX_FLOAT)strWidth1 - 1, (FX_FLOAT)m_Height); + matr2.Concat(*matrix); + matr2.TransformRect(rect2); + re = rect2.GetOutterRect(); + device->FillRect(&re, m_backgroundColor); + CFX_Matrix matr3(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0); + CFX_FloatRect rect3( + (FX_FLOAT)(leftPosition + 85 * multiple), + (FX_FLOAT)(m_Height - iTextHeight), + (FX_FLOAT)((leftPosition + 85 * multiple) + strWidth1 - 0.5), + (FX_FLOAT)m_Height); + matr3.Concat(*matrix); + matr3.TransformRect(rect3); + re = rect3.GetOutterRect(); + device->FillRect(&re, m_backgroundColor); + } + if (pOutBitmap == NULL) { + strWidth = strWidth * m_outputHScale; + } + CalcTextInfo(tempStr, pCharPos + 1, m_pFont, strWidth, iFontSize, blank); + CFX_Matrix affine_matrix(1.0, 0.0, 0.0, -1.0, 0.0, (FX_FLOAT)iFontSize); + CFX_FxgeDevice ge; + if (pOutBitmap) { + ge.Create((int)strWidth, iTextHeight, FXDIB_Argb); + ge.GetBitmap()->Clear(m_backgroundColor); + ge.DrawNormalText(iLen, pCharPos + 1, m_pFont, + CFX_GEModule::Get()->GetFontCache(), (FX_FLOAT)iFontSize, + (CFX_Matrix*)&affine_matrix, m_fontColor, + FXTEXT_CLEARTYPE); + geBitmap.SetDIBits(ge.GetBitmap(), leftPosition, m_Height - iTextHeight); + } else { + CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0, + (FX_FLOAT)leftPosition * m_outputHScale, + (FX_FLOAT)(m_Height - iTextHeight + iFontSize)); + if (matrix) { + affine_matrix1.Concat(*matrix); + } + device->DrawNormalText(iLen, pCharPos + 1, m_pFont, + CFX_GEModule::Get()->GetFontCache(), + (FX_FLOAT)iFontSize, (CFX_Matrix*)&affine_matrix1, + m_fontColor, FXTEXT_CLEARTYPE); + } + tempStr = str.Mid(6, 5); + iLen = tempStr.GetLength(); + CalcTextInfo(tempStr, pCharPos + 6, m_pFont, strWidth, iFontSize, blank); + if (pOutBitmap) { + FX_RECT rect2(0, 0, (int)strWidth, iTextHeight); + ge.FillRect(&rect2, m_backgroundColor); + ge.DrawNormalText(iLen, pCharPos + 6, m_pFont, + CFX_GEModule::Get()->GetFontCache(), (FX_FLOAT)iFontSize, + (CFX_Matrix*)&affine_matrix, m_fontColor, + FXTEXT_CLEARTYPE); + geBitmap.SetDIBits(ge.GetBitmap(), leftPosition + 40 * multiple, + m_Height - iTextHeight); + } else { + CFX_Matrix affine_matrix1( + 1.0, 0.0, 0.0, -1.0, + (FX_FLOAT)(leftPosition + 40 * multiple) * m_outputHScale, + (FX_FLOAT)(m_Height - iTextHeight + iFontSize)); + if (matrix) { + affine_matrix1.Concat(*matrix); + } + device->DrawNormalText(iLen, pCharPos + 6, m_pFont, + CFX_GEModule::Get()->GetFontCache(), + (FX_FLOAT)iFontSize, (CFX_Matrix*)&affine_matrix1, + m_fontColor, FXTEXT_CLEARTYPE); + } + tempStr = str.Mid(0, 1); + iLen = tempStr.GetLength(); + strWidth = (FX_FLOAT)multiple * 7; + if (pOutBitmap == NULL) { + strWidth = strWidth * m_outputHScale; + } + CalcTextInfo(tempStr, pCharPos, m_pFont, strWidth, iFontSize, blank); + if (pOutBitmap) { + delete ge.GetBitmap(); + ge.Create((int)strWidth, iTextHeight, FXDIB_Argb); + ge.GetBitmap()->Clear(m_backgroundColor); + ge.DrawNormalText(iLen, pCharPos, m_pFont, + CFX_GEModule::Get()->GetFontCache(), (FX_FLOAT)iFontSize, + (CFX_Matrix*)&affine_matrix, m_fontColor, + FXTEXT_CLEARTYPE); + geBitmap.SetDIBits(ge.GetBitmap(), 0, m_Height - iTextHeight); + } else { + CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0, 0, + (FX_FLOAT)(m_Height - iTextHeight + iFontSize)); + if (matrix) { + affine_matrix1.Concat(*matrix); + } + device->DrawNormalText(iLen, pCharPos, m_pFont, + CFX_GEModule::Get()->GetFontCache(), + (FX_FLOAT)iFontSize, (CFX_Matrix*)&affine_matrix1, + m_fontColor, FXTEXT_CLEARTYPE); + } + tempStr = str.Mid(11, 1); + iLen = tempStr.GetLength(); + CalcTextInfo(tempStr, pCharPos + 11, m_pFont, strWidth, iFontSize, blank); + if (pOutBitmap) { + delete ge.GetBitmap(); + ge.Create((int)strWidth, iTextHeight, FXDIB_Argb); + ge.GetBitmap()->Clear(m_backgroundColor); + ge.DrawNormalText(iLen, pCharPos + 11, m_pFont, + CFX_GEModule::Get()->GetFontCache(), (FX_FLOAT)iFontSize, + (CFX_Matrix*)&affine_matrix, m_fontColor, + FXTEXT_CLEARTYPE); + geBitmap.SetDIBits(ge.GetBitmap(), leftPosition + 85 * multiple, + m_Height - iTextHeight); + } else { + CFX_Matrix affine_matrix1( + 1.0, 0.0, 0.0, -1.0, + (FX_FLOAT)(leftPosition + 85 * multiple) * m_outputHScale, + (FX_FLOAT)(m_Height - iTextHeight + iFontSize)); + if (matrix) { + affine_matrix1.Concat(*matrix); + } + device->DrawNormalText(iLen, pCharPos + 11, m_pFont, + CFX_GEModule::Get()->GetFontCache(), + (FX_FLOAT)iFontSize, (CFX_Matrix*)&affine_matrix1, + m_fontColor, FXTEXT_CLEARTYPE); + } + FX_Free(pCharPos); +} +void CBC_OnedUPCAWriter::RenderResult(const CFX_WideStringC& contents, + uint8_t* code, + int32_t codeLength, + FX_BOOL isDevice, + int32_t& e) { + CBC_OneDimWriter::RenderResult(contents, code, codeLength, isDevice, e); +} diff --git a/xfa/fxbarcode/oned/BC_OnedUPCAWriter.h b/xfa/fxbarcode/oned/BC_OnedUPCAWriter.h new file mode 100644 index 0000000000..f376929b1b --- /dev/null +++ b/xfa/fxbarcode/oned/BC_OnedUPCAWriter.h @@ -0,0 +1,55 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef XFA_FXBARCODE_ONED_BC_ONEDUPCAWRITER_H_ +#define XFA_FXBARCODE_ONED_BC_ONEDUPCAWRITER_H_ + +class CBC_OnedEAN13Writer; +class CBC_OnedUPCAWriter; +class CBC_OnedUPCAWriter : public CBC_OneDimWriter { + private: + CBC_OnedEAN13Writer* m_subWriter; + + public: + CBC_OnedUPCAWriter(); + virtual ~CBC_OnedUPCAWriter(); + virtual void Init(); + uint8_t* Encode(const CFX_ByteString& contents, + BCFORMAT format, + int32_t& outWidth, + int32_t& outHeight, + int32_t& e); + uint8_t* Encode(const CFX_ByteString& contents, + BCFORMAT format, + int32_t& outWidth, + int32_t& outHeight, + int32_t hints, + int32_t& e); + uint8_t* Encode(const CFX_ByteString& contents, + int32_t& outLength, + int32_t& e) { + return nullptr; + } + void RenderResult(const CFX_WideStringC& contents, + uint8_t* code, + int32_t codeLength, + FX_BOOL isDevice, + int32_t& e); + FX_BOOL CheckContentValidity(const CFX_WideStringC& contents); + CFX_WideString FilterContents(const CFX_WideStringC& contents); + int32_t CalcChecksum(const CFX_ByteString& contents); + + protected: + void ShowChars(const CFX_WideStringC& contents, + CFX_DIBitmap* pOutBitmap, + CFX_RenderDevice* device, + const CFX_Matrix* matrix, + int32_t barWidth, + int32_t multiple, + int32_t& e); +}; + +#endif // XFA_FXBARCODE_ONED_BC_ONEDUPCAWRITER_H_ |