From 59cdc7dc88310bd8cdf2ace60e0f567561d38dbb Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Mon, 29 Jan 2018 20:38:16 +0000 Subject: Move barcode code to CXFA_Barcode This CL moves the barcode specific code out of CXFA_Node and into CXFA_Barcode. The CXFA_FFBarcode widget was modified to take the barcode as a parameter. Change-Id: I8ff91cb58402665f4ba63f2eeced45feeaa9ff50 Reviewed-on: https://pdfium-review.googlesource.com/24450 Commit-Queue: dsinclair Reviewed-by: Ryan Harrison --- xfa/fxfa/cxfa_ffbarcode.cpp | 32 ++++----- xfa/fxfa/cxfa_ffbarcode.h | 8 ++- xfa/fxfa/cxfa_ffnotify.cpp | 5 +- xfa/fxfa/cxfa_fftextedit.cpp | 8 ++- xfa/fxfa/parser/cxfa_barcode.cpp | 142 +++++++++++++++++++++++++++++++++++++ xfa/fxfa/parser/cxfa_barcode.h | 14 ++++ xfa/fxfa/parser/cxfa_node.cpp | 149 --------------------------------------- xfa/fxfa/parser/cxfa_node.h | 15 ---- 8 files changed, 189 insertions(+), 184 deletions(-) diff --git a/xfa/fxfa/cxfa_ffbarcode.cpp b/xfa/fxfa/cxfa_ffbarcode.cpp index ce3ca6b791..c458696ce8 100644 --- a/xfa/fxfa/cxfa_ffbarcode.cpp +++ b/xfa/fxfa/cxfa_ffbarcode.cpp @@ -17,6 +17,7 @@ #include "xfa/fxfa/cxfa_ffpageview.h" #include "xfa/fxfa/cxfa_ffwidget.h" #include "xfa/fxfa/cxfa_fwladapterwidgetmgr.h" +#include "xfa/fxfa/parser/cxfa_barcode.h" #include "xfa/fxfa/parser/cxfa_border.h" namespace { @@ -111,7 +112,8 @@ const BarCodeInfo* CXFA_FFBarcode::GetBarcodeTypeByName( return nullptr; } -CXFA_FFBarcode::CXFA_FFBarcode(CXFA_Node* pNode) : CXFA_FFTextEdit(pNode) {} +CXFA_FFBarcode::CXFA_FFBarcode(CXFA_Node* pNode, CXFA_Barcode* barcode) + : CXFA_FFTextEdit(pNode), barcode_(barcode) {} CXFA_FFBarcode::~CXFA_FFBarcode() {} @@ -157,60 +159,58 @@ void CXFA_FFBarcode::RenderWidget(CXFA_Graphics* pGS, void CXFA_FFBarcode::UpdateWidgetProperty() { CXFA_FFTextEdit::UpdateWidgetProperty(); - auto* node = GetNode(); - const BarCodeInfo* info = GetBarcodeTypeByName(node->GetBarcodeType()); + const BarCodeInfo* info = GetBarcodeTypeByName(barcode_->GetBarcodeType()); if (!info) return; auto* pBarCodeWidget = static_cast(m_pNormalWidget.get()); pBarCodeWidget->SetType(info->eBCType); - Optional encoding = - node->GetBarcodeAttribute_CharEncoding(); + Optional encoding = barcode_->GetCharEncoding(); if (encoding) pBarCodeWidget->SetCharEncoding(*encoding); - Optional calcChecksum = node->GetBarcodeAttribute_Checksum(); + Optional calcChecksum = barcode_->GetChecksum(); if (calcChecksum) pBarCodeWidget->SetCalChecksum(*calcChecksum); - Optional dataLen = node->GetBarcodeAttribute_DataLength(); + Optional dataLen = barcode_->GetDataLength(); if (dataLen) pBarCodeWidget->SetDataLength(*dataLen); - Optional startChar = node->GetBarcodeAttribute_StartChar(); + Optional startChar = barcode_->GetStartChar(); if (startChar) pBarCodeWidget->SetStartChar(*startChar); - Optional endChar = node->GetBarcodeAttribute_EndChar(); + Optional endChar = barcode_->GetEndChar(); if (endChar) pBarCodeWidget->SetEndChar(*endChar); - Optional ecLevel = node->GetBarcodeAttribute_ECLevel(); + Optional ecLevel = barcode_->GetECLevel(); if (ecLevel) pBarCodeWidget->SetErrorCorrectionLevel(*ecLevel); - Optional width = node->GetBarcodeAttribute_ModuleWidth(); + Optional width = barcode_->GetModuleWidth(); if (width) pBarCodeWidget->SetModuleWidth(*width); - Optional height = node->GetBarcodeAttribute_ModuleHeight(); + Optional height = barcode_->GetModuleHeight(); if (height) pBarCodeWidget->SetModuleHeight(*height); - Optional printCheck = node->GetBarcodeAttribute_PrintChecksum(); + Optional printCheck = barcode_->GetPrintChecksum(); if (printCheck) pBarCodeWidget->SetPrintChecksum(*printCheck); - Optional textLoc = node->GetBarcodeAttribute_TextLocation(); + Optional textLoc = barcode_->GetTextLocation(); if (textLoc) pBarCodeWidget->SetTextLocation(*textLoc); - Optional truncate = node->GetBarcodeAttribute_Truncate(); + Optional truncate = barcode_->GetTruncate(); if (truncate) pBarCodeWidget->SetTruncated(*truncate); - Optional ratio = node->GetBarcodeAttribute_WideNarrowRatio(); + Optional ratio = barcode_->GetWideNarrowRatio(); if (ratio) pBarCodeWidget->SetWideNarrowRatio(*ratio); diff --git a/xfa/fxfa/cxfa_ffbarcode.h b/xfa/fxfa/cxfa_ffbarcode.h index 93984700da..df62b8ee7f 100644 --- a/xfa/fxfa/cxfa_ffbarcode.h +++ b/xfa/fxfa/cxfa_ffbarcode.h @@ -7,6 +7,7 @@ #ifndef XFA_FXFA_CXFA_FFBARCODE_H_ #define XFA_FXFA_CXFA_FFBARCODE_H_ +#include "core/fxcrt/unowned_ptr.h" #include "fxbarcode/BC_Library.h" #include "xfa/fxfa/cxfa_ffpageview.h" #include "xfa/fxfa/cxfa_fftextedit.h" @@ -83,11 +84,13 @@ struct BarCodeInfo { BC_TYPE eBCType; }; +class CXFA_Barcode; + class CXFA_FFBarcode : public CXFA_FFTextEdit { public: static const BarCodeInfo* GetBarcodeTypeByName(const WideString& wsName); - explicit CXFA_FFBarcode(CXFA_Node* pNode); + explicit CXFA_FFBarcode(CXFA_Node* pNode, CXFA_Barcode* barcode); ~CXFA_FFBarcode() override; // CXFA_FFTextEdit @@ -98,6 +101,9 @@ class CXFA_FFBarcode : public CXFA_FFTextEdit { void UpdateWidgetProperty() override; bool OnLButtonDown(uint32_t dwFlags, const CFX_PointF& point) override; bool OnRButtonDown(uint32_t dwFlags, const CFX_PointF& point) override; + + private: + UnownedPtr barcode_; }; #endif // XFA_FXFA_CXFA_FFBARCODE_H_ diff --git a/xfa/fxfa/cxfa_ffnotify.cpp b/xfa/fxfa/cxfa_ffnotify.cpp index d4834f02f2..36f860199a 100644 --- a/xfa/fxfa/cxfa_ffnotify.cpp +++ b/xfa/fxfa/cxfa_ffnotify.cpp @@ -35,6 +35,7 @@ #include "xfa/fxfa/cxfa_fwladapterwidgetmgr.h" #include "xfa/fxfa/cxfa_textlayout.h" #include "xfa/fxfa/cxfa_textprovider.h" +#include "xfa/fxfa/parser/cxfa_barcode.h" #include "xfa/fxfa/parser/cxfa_binditems.h" #include "xfa/fxfa/parser/cxfa_node.h" @@ -119,7 +120,9 @@ CXFA_ContentLayoutItem* CXFA_FFNotify::OnCreateContentLayoutItem( CXFA_FFWidget* pWidget = nullptr; switch (pNode->GetFFWidgetType()) { case XFA_FFWidgetType::kBarcode: - pWidget = new CXFA_FFBarcode(pNode); + ASSERT(pNode->GetUIChildNode()->GetElementType() == XFA_Element::Barcode); + pWidget = new CXFA_FFBarcode( + pNode, static_cast(pNode->GetUIChildNode())); break; case XFA_FFWidgetType::kButton: pWidget = new CXFA_FFPushButton(pNode); diff --git a/xfa/fxfa/cxfa_fftextedit.cpp b/xfa/fxfa/cxfa_fftextedit.cpp index ad03b5273b..d91e7f7086 100644 --- a/xfa/fxfa/cxfa_fftextedit.cpp +++ b/xfa/fxfa/cxfa_fftextedit.cpp @@ -19,6 +19,7 @@ #include "xfa/fxfa/cxfa_eventparam.h" #include "xfa/fxfa/cxfa_ffapp.h" #include "xfa/fxfa/cxfa_ffdoc.h" +#include "xfa/fxfa/parser/cxfa_barcode.h" #include "xfa/fxfa/parser/cxfa_node.h" #include "xfa/fxfa/parser/cxfa_para.h" @@ -271,8 +272,11 @@ bool CXFA_FFTextEdit::UpdateFWLData() { } } else if (m_pNode->GetFFWidgetType() == XFA_FFWidgetType::kBarcode) { int32_t nDataLen = 0; - if (eType == XFA_VALUEPICTURE_Edit) - nDataLen = m_pNode->GetBarcodeAttribute_DataLength().value_or(0); + if (eType == XFA_VALUEPICTURE_Edit) { + nDataLen = static_cast(m_pNode.Get()) + ->GetDataLength() + .value_or(0); + } pEdit->SetLimit(nDataLen); bUpdate = true; diff --git a/xfa/fxfa/parser/cxfa_barcode.cpp b/xfa/fxfa/parser/cxfa_barcode.cpp index 8c77c571f0..9fb7082754 100644 --- a/xfa/fxfa/parser/cxfa_barcode.cpp +++ b/xfa/fxfa/parser/cxfa_barcode.cpp @@ -8,6 +8,7 @@ #include "fxjs/xfa/cjx_barcode.h" #include "third_party/base/ptr_util.h" +#include "xfa/fxfa/parser/cxfa_measurement.h" namespace { @@ -59,3 +60,144 @@ CXFA_Barcode::~CXFA_Barcode() {} XFA_FFWidgetType CXFA_Barcode::GetDefaultFFWidgetType() const { return XFA_FFWidgetType::kBarcode; } + +WideString CXFA_Barcode::GetBarcodeType() { + return WideString(JSObject()->GetCData(XFA_Attribute::Type)); +} + +Optional CXFA_Barcode::GetCharEncoding() { + Optional wsCharEncoding = + JSObject()->TryCData(XFA_Attribute::CharEncoding, true); + if (!wsCharEncoding) + return {}; + if (wsCharEncoding->CompareNoCase(L"UTF-16")) + return {CHAR_ENCODING_UNICODE}; + if (wsCharEncoding->CompareNoCase(L"UTF-8")) + return {CHAR_ENCODING_UTF8}; + return {}; +} + +Optional CXFA_Barcode::GetChecksum() { + Optional checksum = + JSObject()->TryEnum(XFA_Attribute::Checksum, true); + if (!checksum) + return {}; + + switch (*checksum) { + case XFA_AttributeEnum::None: + return {false}; + case XFA_AttributeEnum::Auto: + return {true}; + case XFA_AttributeEnum::Checksum_1mod10: + case XFA_AttributeEnum::Checksum_1mod10_1mod11: + case XFA_AttributeEnum::Checksum_2mod10: + default: + break; + } + return {}; +} + +Optional CXFA_Barcode::GetDataLength() { + Optional wsDataLength = + JSObject()->TryCData(XFA_Attribute::DataLength, true); + if (!wsDataLength) + return {}; + + return {FXSYS_wtoi(wsDataLength->c_str())}; +} + +Optional CXFA_Barcode::GetStartChar() { + Optional wsStartEndChar = + JSObject()->TryCData(XFA_Attribute::StartChar, true); + if (!wsStartEndChar || wsStartEndChar->IsEmpty()) + return {}; + + return {static_cast((*wsStartEndChar)[0])}; +} + +Optional CXFA_Barcode::GetEndChar() { + Optional wsStartEndChar = + JSObject()->TryCData(XFA_Attribute::EndChar, true); + if (!wsStartEndChar || wsStartEndChar->IsEmpty()) + return {}; + + return {static_cast((*wsStartEndChar)[0])}; +} + +Optional CXFA_Barcode::GetECLevel() { + Optional wsECLevel = + JSObject()->TryCData(XFA_Attribute::ErrorCorrectionLevel, true); + if (!wsECLevel) + return {}; + return {FXSYS_wtoi(wsECLevel->c_str())}; +} + +Optional CXFA_Barcode::GetModuleWidth() { + Optional moduleWidthHeight = + JSObject()->TryMeasure(XFA_Attribute::ModuleWidth, true); + if (!moduleWidthHeight) + return {}; + + return {static_cast(moduleWidthHeight->ToUnit(XFA_Unit::Pt))}; +} + +Optional CXFA_Barcode::GetModuleHeight() { + Optional moduleWidthHeight = + JSObject()->TryMeasure(XFA_Attribute::ModuleHeight, true); + if (!moduleWidthHeight) + return {}; + + return {static_cast(moduleWidthHeight->ToUnit(XFA_Unit::Pt))}; +} + +Optional CXFA_Barcode::GetPrintChecksum() { + return JSObject()->TryBoolean(XFA_Attribute::PrintCheckDigit, true); +} + +Optional CXFA_Barcode::GetTextLocation() { + Optional textLocation = + JSObject()->TryEnum(XFA_Attribute::TextLocation, true); + if (!textLocation) + return {}; + + switch (*textLocation) { + case XFA_AttributeEnum::None: + return {BC_TEXT_LOC_NONE}; + case XFA_AttributeEnum::Above: + return {BC_TEXT_LOC_ABOVE}; + case XFA_AttributeEnum::Below: + return {BC_TEXT_LOC_BELOW}; + case XFA_AttributeEnum::AboveEmbedded: + return {BC_TEXT_LOC_ABOVEEMBED}; + case XFA_AttributeEnum::BelowEmbedded: + return {BC_TEXT_LOC_BELOWEMBED}; + default: + break; + } + return {}; +} + +Optional CXFA_Barcode::GetTruncate() { + return JSObject()->TryBoolean(XFA_Attribute::Truncate, true); +} + +Optional CXFA_Barcode::GetWideNarrowRatio() { + Optional wsWideNarrowRatio = + JSObject()->TryCData(XFA_Attribute::WideNarrowRatio, true); + if (!wsWideNarrowRatio) + return {}; + + Optional ptPos = wsWideNarrowRatio->Find(':'); + if (!ptPos) + return {static_cast(FXSYS_wtoi(wsWideNarrowRatio->c_str()))}; + + int32_t fB = FXSYS_wtoi( + wsWideNarrowRatio->Right(wsWideNarrowRatio->GetLength() - (*ptPos + 1)) + .c_str()); + if (!fB) + return {0}; + + int32_t fA = FXSYS_wtoi(wsWideNarrowRatio->Left(*ptPos).c_str()); + float result = static_cast(fA) / static_cast(fB); + return {static_cast(result)}; +} diff --git a/xfa/fxfa/parser/cxfa_barcode.h b/xfa/fxfa/parser/cxfa_barcode.h index 056196f3ac..0899740424 100644 --- a/xfa/fxfa/parser/cxfa_barcode.h +++ b/xfa/fxfa/parser/cxfa_barcode.h @@ -15,6 +15,20 @@ class CXFA_Barcode : public CXFA_Node { ~CXFA_Barcode() override; XFA_FFWidgetType GetDefaultFFWidgetType() const override; + + WideString GetBarcodeType(); + Optional GetCharEncoding(); + Optional GetChecksum(); + Optional GetDataLength(); + Optional GetStartChar(); + Optional GetEndChar(); + Optional GetECLevel(); + Optional GetModuleWidth(); + Optional GetModuleHeight(); + Optional GetPrintChecksum(); + Optional GetTextLocation(); + Optional GetTruncate(); + Optional GetWideNarrowRatio(); }; #endif // XFA_FXFA_PARSER_CXFA_BARCODE_H_ diff --git a/xfa/fxfa/parser/cxfa_node.cpp b/xfa/fxfa/parser/cxfa_node.cpp index 5b04700010..2ccdd1d919 100644 --- a/xfa/fxfa/parser/cxfa_node.cpp +++ b/xfa/fxfa/parser/cxfa_node.cpp @@ -2371,155 +2371,6 @@ std::pair CXFA_Node::ExecuteBoolScript( return {iRet, pTmpRetValue->IsBoolean() ? pTmpRetValue->ToBoolean() : false}; } -WideString CXFA_Node::GetBarcodeType() { - CXFA_Node* pUIChild = GetUIChildNode(); - return pUIChild - ? WideString(pUIChild->JSObject()->GetCData(XFA_Attribute::Type)) - : WideString(); -} - -Optional CXFA_Node::GetBarcodeAttribute_CharEncoding() { - Optional wsCharEncoding = - GetUIChildNode()->JSObject()->TryCData(XFA_Attribute::CharEncoding, true); - if (!wsCharEncoding) - return {}; - if (wsCharEncoding->CompareNoCase(L"UTF-16")) - return {CHAR_ENCODING_UNICODE}; - if (wsCharEncoding->CompareNoCase(L"UTF-8")) - return {CHAR_ENCODING_UTF8}; - return {}; -} - -Optional CXFA_Node::GetBarcodeAttribute_Checksum() { - Optional checksum = - GetUIChildNode()->JSObject()->TryEnum(XFA_Attribute::Checksum, true); - if (!checksum) - return {}; - - switch (*checksum) { - case XFA_AttributeEnum::None: - return {false}; - case XFA_AttributeEnum::Auto: - return {true}; - case XFA_AttributeEnum::Checksum_1mod10: - case XFA_AttributeEnum::Checksum_1mod10_1mod11: - case XFA_AttributeEnum::Checksum_2mod10: - default: - break; - } - return {}; -} - -Optional CXFA_Node::GetBarcodeAttribute_DataLength() { - Optional wsDataLength = - GetUIChildNode()->JSObject()->TryCData(XFA_Attribute::DataLength, true); - if (!wsDataLength) - return {}; - - return {FXSYS_wtoi(wsDataLength->c_str())}; -} - -Optional CXFA_Node::GetBarcodeAttribute_StartChar() { - Optional wsStartEndChar = - GetUIChildNode()->JSObject()->TryCData(XFA_Attribute::StartChar, true); - if (!wsStartEndChar || wsStartEndChar->IsEmpty()) - return {}; - - return {static_cast((*wsStartEndChar)[0])}; -} - -Optional CXFA_Node::GetBarcodeAttribute_EndChar() { - Optional wsStartEndChar = - GetUIChildNode()->JSObject()->TryCData(XFA_Attribute::EndChar, true); - if (!wsStartEndChar || wsStartEndChar->IsEmpty()) - return {}; - - return {static_cast((*wsStartEndChar)[0])}; -} - -Optional CXFA_Node::GetBarcodeAttribute_ECLevel() { - Optional wsECLevel = GetUIChildNode()->JSObject()->TryCData( - XFA_Attribute::ErrorCorrectionLevel, true); - if (!wsECLevel) - return {}; - return {FXSYS_wtoi(wsECLevel->c_str())}; -} - -Optional CXFA_Node::GetBarcodeAttribute_ModuleWidth() { - Optional moduleWidthHeight = - GetUIChildNode()->JSObject()->TryMeasure(XFA_Attribute::ModuleWidth, - true); - if (!moduleWidthHeight) - return {}; - - return {static_cast(moduleWidthHeight->ToUnit(XFA_Unit::Pt))}; -} - -Optional CXFA_Node::GetBarcodeAttribute_ModuleHeight() { - Optional moduleWidthHeight = - GetUIChildNode()->JSObject()->TryMeasure(XFA_Attribute::ModuleHeight, - true); - if (!moduleWidthHeight) - return {}; - - return {static_cast(moduleWidthHeight->ToUnit(XFA_Unit::Pt))}; -} - -Optional CXFA_Node::GetBarcodeAttribute_PrintChecksum() { - return GetUIChildNode()->JSObject()->TryBoolean( - XFA_Attribute::PrintCheckDigit, true); -} - -Optional CXFA_Node::GetBarcodeAttribute_TextLocation() { - Optional textLocation = - GetUIChildNode()->JSObject()->TryEnum(XFA_Attribute::TextLocation, true); - if (!textLocation) - return {}; - - switch (*textLocation) { - case XFA_AttributeEnum::None: - return {BC_TEXT_LOC_NONE}; - case XFA_AttributeEnum::Above: - return {BC_TEXT_LOC_ABOVE}; - case XFA_AttributeEnum::Below: - return {BC_TEXT_LOC_BELOW}; - case XFA_AttributeEnum::AboveEmbedded: - return {BC_TEXT_LOC_ABOVEEMBED}; - case XFA_AttributeEnum::BelowEmbedded: - return {BC_TEXT_LOC_BELOWEMBED}; - default: - break; - } - return {}; -} - -Optional CXFA_Node::GetBarcodeAttribute_Truncate() { - return GetUIChildNode()->JSObject()->TryBoolean(XFA_Attribute::Truncate, - true); -} - -Optional CXFA_Node::GetBarcodeAttribute_WideNarrowRatio() { - Optional wsWideNarrowRatio = - GetUIChildNode()->JSObject()->TryCData(XFA_Attribute::WideNarrowRatio, - true); - if (!wsWideNarrowRatio) - return {}; - - Optional ptPos = wsWideNarrowRatio->Find(':'); - if (!ptPos) - return {static_cast(FXSYS_wtoi(wsWideNarrowRatio->c_str()))}; - - int32_t fB = FXSYS_wtoi( - wsWideNarrowRatio->Right(wsWideNarrowRatio->GetLength() - (*ptPos + 1)) - .c_str()); - if (!fB) - return {0}; - - int32_t fA = FXSYS_wtoi(wsWideNarrowRatio->Left(*ptPos).c_str()); - float result = static_cast(fA) / static_cast(fB); - return {static_cast(result)}; -} - std::pair CXFA_Node::CreateChildUIAndValueNodesIfNeeded() { XFA_Element eType = GetElementType(); diff --git a/xfa/fxfa/parser/cxfa_node.h b/xfa/fxfa/parser/cxfa_node.h index ae7f4bae99..99b035dfc7 100644 --- a/xfa/fxfa/parser/cxfa_node.h +++ b/xfa/fxfa/parser/cxfa_node.h @@ -299,21 +299,6 @@ class CXFA_Node : public CXFA_Object { CXFA_Script* script, CXFA_EventParam* pEventParam); - // TODO(dsinclair): Figure out how to move this to cxfa_barcode. - WideString GetBarcodeType(); - Optional GetBarcodeAttribute_CharEncoding(); - Optional GetBarcodeAttribute_Checksum(); - Optional GetBarcodeAttribute_DataLength(); - Optional GetBarcodeAttribute_StartChar(); - Optional GetBarcodeAttribute_EndChar(); - Optional GetBarcodeAttribute_ECLevel(); - Optional GetBarcodeAttribute_ModuleWidth(); - Optional GetBarcodeAttribute_ModuleHeight(); - Optional GetBarcodeAttribute_PrintChecksum(); - Optional GetBarcodeAttribute_TextLocation(); - Optional GetBarcodeAttribute_Truncate(); - Optional GetBarcodeAttribute_WideNarrowRatio(); - CXFA_Node* GetUIChildNode(); XFA_FFWidgetType GetFFWidgetType(); -- cgit v1.2.3