diff options
Diffstat (limited to 'fxbarcode/oned/BC_OneDimWriter.cpp')
-rw-r--r-- | fxbarcode/oned/BC_OneDimWriter.cpp | 175 |
1 files changed, 53 insertions, 122 deletions
diff --git a/fxbarcode/oned/BC_OneDimWriter.cpp b/fxbarcode/oned/BC_OneDimWriter.cpp index 57b6c22a93..b898340519 100644 --- a/fxbarcode/oned/BC_OneDimWriter.cpp +++ b/fxbarcode/oned/BC_OneDimWriter.cpp @@ -24,6 +24,7 @@ #include <algorithm> #include <memory> +#include <vector> #include "core/fxge/cfx_fxgedevice.h" #include "core/fxge/cfx_gemodule.h" @@ -90,44 +91,25 @@ wchar_t CBC_OneDimWriter::Upper(wchar_t ch) { 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 = nullptr; +uint8_t* CBC_OneDimWriter::EncodeWithHint(const CFX_ByteString& contents, + BCFORMAT format, + int32_t& outWidth, + int32_t& outHeight, + int32_t hints) { outHeight = 1; - if (m_Width >= 20) { - ret = Encode(contents, outWidth, e); - } else { - ret = Encode(contents, outWidth, e); - } - if (e != BCExceptionNO) - return nullptr; - return ret; + return EncodeImpl(contents, outWidth); } 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); - if (e != BCExceptionNO) - return nullptr; - return ret; -} - -uint8_t* CBC_OneDimWriter::Encode(const CFX_ByteString& contents, - int32_t& outLength, - int32_t& e) { - return nullptr; + int32_t& outHeight) { + return EncodeWithHint(contents, format, outWidth, outHeight, 0); } int32_t CBC_OneDimWriter::AppendPattern(uint8_t* target, int32_t pos, - const int32_t* pattern, + const int8_t* pattern, int32_t patternLength, int32_t startColor, int32_t& e) { @@ -139,8 +121,7 @@ int32_t CBC_OneDimWriter::AppendPattern(uint8_t* target, 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; + target[pos++] = color; numAdded += 1; } color ^= 1; @@ -223,24 +204,17 @@ void CBC_OneDimWriter::ShowDeviceChars(CFX_RenderDevice* device, m_fontColor, FXTEXT_CLEARTYPE); } -void CBC_OneDimWriter::ShowChars(const CFX_WideStringC& contents, +bool CBC_OneDimWriter::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; - } - if (!m_pFont) { - e = BCExceptionNullPointer; - return; - } + int32_t multiple) { + if (!device || !m_pFont) + return false; + CFX_ByteString str = FX_UTF8Encode(contents); int32_t iLen = str.GetLength(); - FXTEXT_CHARPOS* pCharPos = FX_Alloc(FXTEXT_CHARPOS, iLen); - memset(pCharPos, 0, sizeof(FXTEXT_CHARPOS) * iLen); + std::vector<FXTEXT_CHARPOS> charpos(iLen); float charsLen = 0; float geWidth = 0; if (m_locTextLoc == BC_TEXT_LOC_ABOVEEMBED || @@ -252,10 +226,10 @@ void CBC_OneDimWriter::ShowChars(const CFX_WideStringC& contents, } 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; - } + CalcTextInfo(str, charpos.data(), m_pFont, geWidth, iFontSize, charsLen); + if (charsLen < 1) + return true; + int32_t locX = 0; int32_t locY = 0; switch (m_locTextLoc) { @@ -281,18 +255,16 @@ void CBC_OneDimWriter::ShowChars(const CFX_WideStringC& contents, geWidth = (float)barWidth; break; } - ShowDeviceChars(device, matrix, str, geWidth, pCharPos, (float)locX, + ShowDeviceChars(device, matrix, str, geWidth, charpos.data(), (float)locX, (float)locY, barWidth); - FX_Free(pCharPos); + return true; } -void CBC_OneDimWriter::RenderDeviceResult(CFX_RenderDevice* device, +bool CBC_OneDimWriter::RenderDeviceResult(CFX_RenderDevice* device, const CFX_Matrix* matrix, - const CFX_WideStringC& contents, - int32_t& e) { + const CFX_WideStringC& contents) { if (!m_output) - if (e != BCExceptionNO) - return; + return false; CFX_GraphStateData stateData; CFX_PathData path; @@ -311,109 +283,68 @@ void CBC_OneDimWriter::RenderDeviceResult(CFX_RenderDevice* device, } } } - 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, device, matrix, m_barWidth, m_multiple, e); - if (e != BCExceptionNO) - return; - } + return m_locTextLoc == BC_TEXT_LOC_NONE || contents.Find(' ') == -1 || + ShowChars(contents, device, matrix, m_barWidth, m_multiple); } -void CBC_OneDimWriter::RenderResult(const CFX_WideStringC& contents, +bool CBC_OneDimWriter::RenderResult(const CFX_WideStringC& contents, uint8_t* code, int32_t codeLength, - bool isDevice, - int32_t& e) { - if (codeLength < 1) { - if (e != BCExceptionNO) - return; - } - 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; - } + bool isDevice) { + if (codeLength < 1) + return false; + + m_ModuleHeight = std::max(m_ModuleHeight, 20); + const int32_t codeOldLength = codeLength; + const int32_t leftPadding = m_bLeftPadding ? 7 : 0; + const int32_t rightPadding = m_bRightPadding ? 7 : 0; codeLength += leftPadding; codeLength += rightPadding; - m_outputHScale = 1.0; - if (m_Width > 0) { - m_outputHScale = (float)m_Width / (float)codeLength; - } + m_outputHScale = + m_Width > 0 ? static_cast<float>(m_Width) / static_cast<float>(codeLength) + : 1.0; if (!isDevice) { m_outputHScale = std::max(m_outputHScale, static_cast<float>(m_ModuleWidth)); } float dataLengthScale = 1.0; - if (m_iDataLenth > 0 && contents.GetLength() != 0) { + if (m_iDataLenth > 0 && contents.GetLength() != 0) dataLengthScale = float(contents.GetLength()) / float(m_iDataLenth); - } - if (m_iDataLenth > 0 && contents.GetLength() == 0) { + if (m_iDataLenth > 0 && contents.GetLength() == 0) dataLengthScale = float(1) / 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; - } - } + if (!isDevice) + outputHeight = m_Height ? m_Height : std::max(20, m_ModuleHeight); int32_t outputWidth = codeLength; - if (!isDevice) { + if (!isDevice) outputWidth = (int32_t)(codeLength * m_multiple / dataLengthScale); - } m_barWidth = m_Width; - if (!isDevice) { + if (!isDevice) m_barWidth = codeLength * m_multiple; - } m_output = pdfium::MakeUnique<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 >= outputWidth) + return true; + if (outputX + m_multiple > outputWidth && outputWidth - outputX > 0) { - if (!m_output->SetRegion(outputX, 0, outputWidth - outputX, - outputHeight)) { - e = BCExceptionGeneric; - } - break; - } - if (!m_output->SetRegion(outputX, 0, m_multiple, outputHeight)) { - e = BCExceptionGeneric; - return; + return m_output->SetRegion(outputX, 0, outputWidth - outputX, + outputHeight); } + if (!m_output->SetRegion(outputX, 0, m_multiple, outputHeight)) + return false; } outputX += m_multiple; } -} - -bool CBC_OneDimWriter::CheckContentValidity(const CFX_WideStringC& contents) { return true; } -CFX_WideString CBC_OneDimWriter::FilterContents( - const CFX_WideStringC& contents) { - return CFX_WideString(); -} - CFX_WideString CBC_OneDimWriter::RenderTextContents( const CFX_WideStringC& contents) { return CFX_WideString(); |