summaryrefslogtreecommitdiff
path: root/fxbarcode/oned/BC_OnedEAN8Writer.cpp
diff options
context:
space:
mode:
authorLei Zhang <thestig@chromium.org>2017-04-20 15:58:56 -0700
committerChromium commit bot <commit-bot@chromium.org>2017-04-21 00:54:04 +0000
commit1badb85e5c3a4b4cd42ca1a2b223d6b3bc67cc4a (patch)
tree55bf63857592387531797b0f08e0a6effc0f40c9 /fxbarcode/oned/BC_OnedEAN8Writer.cpp
parentaeee187c927c07f47a9e5886a417dcc58badefb6 (diff)
downloadpdfium-1badb85e5c3a4b4cd42ca1a2b223d6b3bc67cc4a.tar.xz
Change more fxbarcode to use return values.
Change-Id: Idcc05fb8c5a1448f552b4db5ae131ad82aef4d59 Reviewed-on: https://pdfium-review.googlesource.com/4258 Reviewed-by: Tom Sepez <tsepez@chromium.org> Commit-Queue: Lei Zhang <thestig@chromium.org>
Diffstat (limited to 'fxbarcode/oned/BC_OnedEAN8Writer.cpp')
-rw-r--r--fxbarcode/oned/BC_OnedEAN8Writer.cpp151
1 files changed, 61 insertions, 90 deletions
diff --git a/fxbarcode/oned/BC_OnedEAN8Writer.cpp b/fxbarcode/oned/BC_OnedEAN8Writer.cpp
index 5ebc699c2c..052e0d0508 100644
--- a/fxbarcode/oned/BC_OnedEAN8Writer.cpp
+++ b/fxbarcode/oned/BC_OnedEAN8Writer.cpp
@@ -20,18 +20,24 @@
* limitations under the License.
*/
+#include "fxbarcode/oned/BC_OnedEAN8Writer.h"
+
+#include <algorithm>
+#include <cwctype>
+#include <memory>
+#include <vector>
+
#include "core/fxge/cfx_fxgedevice.h"
#include "core/fxge/cfx_gemodule.h"
#include "fxbarcode/BC_Writer.h"
#include "fxbarcode/common/BC_CommonBitMatrix.h"
#include "fxbarcode/oned/BC_OneDimWriter.h"
-#include "fxbarcode/oned/BC_OnedEAN8Writer.h"
namespace {
-const int32_t START_END_PATTERN[3] = {1, 1, 1};
-const int32_t MIDDLE_PATTERN[5] = {1, 1, 1, 1, 1};
-const int32_t L_PATTERNS[10][4] = {
+const int8_t START_END_PATTERN[3] = {1, 1, 1};
+const int8_t MIDDLE_PATTERN[5] = {1, 1, 1, 1, 1};
+const int8_t 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}};
@@ -41,10 +47,13 @@ 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;
}
+
bool CBC_OnedEAN8Writer::SetTextLocation(BC_TEXT_LOC location) {
if (location == BC_TEXT_LOC_BELOWEMBED) {
m_locTextLoc = location;
@@ -52,16 +61,11 @@ bool CBC_OnedEAN8Writer::SetTextLocation(BC_TEXT_LOC location) {
}
return false;
}
+
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;
+ return std::all_of(contents.begin(), contents.end(), std::iswdigit);
}
+
CFX_WideString CBC_OnedEAN8Writer::FilterContents(
const CFX_WideStringC& contents) {
CFX_WideString filtercontents;
@@ -78,6 +82,7 @@ CFX_WideString CBC_OnedEAN8Writer::FilterContents(
}
return filtercontents;
}
+
int32_t CBC_OnedEAN8Writer::CalcChecksum(const CFX_ByteString& contents) {
int32_t odd = 0;
int32_t even = 0;
@@ -94,94 +99,67 @@ int32_t CBC_OnedEAN8Writer::CalcChecksum(const CFX_ByteString& contents) {
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);
- if (e != BCExceptionNO)
- return nullptr;
- 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 nullptr;
- }
- uint8_t* ret =
- CBC_OneDimWriter::Encode(contents, format, outWidth, outHeight, hints, e);
- if (e != BCExceptionNO)
+
+uint8_t* CBC_OnedEAN8Writer::EncodeWithHint(const CFX_ByteString& contents,
+ BCFORMAT format,
+ int32_t& outWidth,
+ int32_t& outHeight,
+ int32_t hints) {
+ if (format != BCFORMAT_EAN_8)
return nullptr;
- return ret;
+ return CBC_OneDimWriter::EncodeWithHint(contents, format, outWidth, outHeight,
+ hints);
}
-uint8_t* CBC_OnedEAN8Writer::Encode(const CFX_ByteString& contents,
- int32_t& outLength,
- int32_t& e) {
- if (contents.GetLength() != 8) {
- e = BCExceptionDigitLengthMustBe8;
+
+uint8_t* CBC_OnedEAN8Writer::EncodeImpl(const CFX_ByteString& contents,
+ int32_t& outLength) {
+ if (contents.GetLength() != 8)
return nullptr;
- }
+
outLength = m_codeWidth;
- uint8_t* result = FX_Alloc(uint8_t, m_codeWidth);
+ std::unique_ptr<uint8_t, FxFreeDeleter> result(
+ FX_Alloc(uint8_t, m_codeWidth));
int32_t pos = 0;
- pos += AppendPattern(result, pos, START_END_PATTERN, 3, 1, e);
- if (e != BCExceptionNO) {
- FX_Free(result);
+ int32_t e = BCExceptionNO;
+ pos += AppendPattern(result.get(), pos, START_END_PATTERN, 3, 1, e);
+ if (e != BCExceptionNO)
return nullptr;
- }
+
int32_t i = 0;
for (i = 0; i <= 3; i++) {
int32_t digit = FXSYS_atoi(contents.Mid(i, 1).c_str());
- pos += AppendPattern(result, pos, L_PATTERNS[digit], 4, 0, e);
- if (e != BCExceptionNO) {
- FX_Free(result);
+ pos += AppendPattern(result.get(), pos, L_PATTERNS[digit], 4, 0, e);
+ if (e != BCExceptionNO)
return nullptr;
- }
}
- pos += AppendPattern(result, pos, MIDDLE_PATTERN, 5, 0, e);
- if (e != BCExceptionNO) {
- FX_Free(result);
+ pos += AppendPattern(result.get(), pos, MIDDLE_PATTERN, 5, 0, e);
+ if (e != BCExceptionNO)
return nullptr;
- }
+
for (i = 4; i <= 7; i++) {
int32_t digit = FXSYS_atoi(contents.Mid(i, 1).c_str());
- pos += AppendPattern(result, pos, L_PATTERNS[digit], 4, 1, e);
- if (e != BCExceptionNO) {
- FX_Free(result);
+ pos += AppendPattern(result.get(), pos, L_PATTERNS[digit], 4, 1, e);
+ if (e != BCExceptionNO)
return nullptr;
- }
}
- pos += AppendPattern(result, pos, START_END_PATTERN, 3, 1, e);
- if (e != BCExceptionNO) {
- FX_Free(result);
+ pos += AppendPattern(result.get(), pos, START_END_PATTERN, 3, 1, e);
+ if (e != BCExceptionNO)
return nullptr;
- }
- return result;
+ return result.release();
}
-void CBC_OnedEAN8Writer::ShowChars(
- const CFX_WideStringC& contents,
- CFX_RenderDevice* device,
- const CFX_Matrix* matrix,
- int32_t barWidth,
- int32_t multiple,
- int32_t& e) {
- if (!device) {
- e = BCExceptionIllegalArgument;
- return;
- }
+bool CBC_OnedEAN8Writer::ShowChars(const CFX_WideStringC& contents,
+ CFX_RenderDevice* device,
+ const CFX_Matrix* matrix,
+ int32_t barWidth,
+ int32_t multiple) {
+ if (!device)
+ return false;
int32_t leftPosition = 3 * multiple;
CFX_ByteString str = FX_UTF8Encode(contents);
int32_t iLength = str.GetLength();
- FXTEXT_CHARPOS* pCharPos = FX_Alloc(FXTEXT_CHARPOS, iLength);
- memset(pCharPos, 0, sizeof(FXTEXT_CHARPOS) * iLength);
+ std::vector<FXTEXT_CHARPOS> charpos(iLength);
CFX_ByteString tempStr = str.Mid(0, 4);
int32_t iLen = tempStr.GetLength();
int32_t strWidth = 7 * multiple * 4;
@@ -207,19 +185,20 @@ void CBC_OnedEAN8Writer::ShowChars(
device->FillRect(&re, m_backgroundColor);
strWidth = (int32_t)(strWidth * m_outputHScale);
- CalcTextInfo(tempStr, pCharPos, m_pFont, (float)strWidth, iFontSize, blank);
+ CalcTextInfo(tempStr, charpos.data(), m_pFont, (float)strWidth, iFontSize,
+ blank);
{
CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0,
(float)leftPosition * m_outputHScale,
(float)(m_Height - iTextHeight + iFontSize));
affine_matrix1.Concat(*matrix);
- device->DrawNormalText(iLen, pCharPos, m_pFont,
+ device->DrawNormalText(iLen, charpos.data(), m_pFont,
static_cast<float>(iFontSize), &affine_matrix1,
m_fontColor, FXTEXT_CLEARTYPE);
}
tempStr = str.Mid(4, 4);
iLen = tempStr.GetLength();
- CalcTextInfo(tempStr, pCharPos + 4, m_pFont, (float)strWidth, iFontSize,
+ CalcTextInfo(tempStr, &charpos[4], m_pFont, (float)strWidth, iFontSize,
blank);
{
CFX_Matrix affine_matrix1(
@@ -228,17 +207,9 @@ void CBC_OnedEAN8Writer::ShowChars(
(float)(m_Height - iTextHeight + iFontSize));
if (matrix)
affine_matrix1.Concat(*matrix);
- device->DrawNormalText(iLen, pCharPos + 4, m_pFont,
+ device->DrawNormalText(iLen, &charpos[4], m_pFont,
static_cast<float>(iFontSize), &affine_matrix1,
m_fontColor, FXTEXT_CLEARTYPE);
}
- FX_Free(pCharPos);
-}
-
-void CBC_OnedEAN8Writer::RenderResult(const CFX_WideStringC& contents,
- uint8_t* code,
- int32_t codeLength,
- bool isDevice,
- int32_t& e) {
- CBC_OneDimWriter::RenderResult(contents, code, codeLength, isDevice, e);
+ return true;
}