From 0d54f09ae49c0cf85de5fe2bd3da885c16523018 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Tue, 21 Nov 2017 16:32:30 +0000 Subject: Convert CXFA_WidgetData methods to Optional This CL converts the CXFA_WidgetData size methods to return Optionals instead of boolean with an out parameter. Change-Id: Ibf2fef5a58896abfea9865e086029377c051bdb6 Reviewed-on: https://pdfium-review.googlesource.com/18892 Reviewed-by: Henrique Nakashima Commit-Queue: dsinclair --- xfa/fxfa/cxfa_textprovider.cpp | 4 +- xfa/fxfa/cxfa_widgetacc.cpp | 183 +++++++++++++++++++----------------- xfa/fxfa/cxfa_widgetacc.h | 4 + xfa/fxfa/parser/cxfa_widgetdata.cpp | 42 ++++++--- xfa/fxfa/parser/cxfa_widgetdata.h | 12 +-- 5 files changed, 139 insertions(+), 106 deletions(-) diff --git a/xfa/fxfa/cxfa_textprovider.cpp b/xfa/fxfa/cxfa_textprovider.cpp index edefc2960c..876e0b1708 100644 --- a/xfa/fxfa/cxfa_textprovider.cpp +++ b/xfa/fxfa/cxfa_textprovider.cpp @@ -132,9 +132,7 @@ bool CXFA_TextProvider::IsCheckButtonAndAutoWidth() { XFA_Element eType = m_pWidgetAcc->GetUIType(); if (eType != XFA_Element::CheckButton) return false; - - float fWidth = 0; - return !m_pWidgetAcc->TryWidth(fWidth); + return !m_pWidgetAcc->TryWidth(); } bool CXFA_TextProvider::GetEmbbedObj(bool bURI, diff --git a/xfa/fxfa/cxfa_widgetacc.cpp b/xfa/fxfa/cxfa_widgetacc.cpp index fc7e52252d..1f804e6ed2 100644 --- a/xfa/fxfa/cxfa_widgetacc.cpp +++ b/xfa/fxfa/cxfa_widgetacc.cpp @@ -783,30 +783,30 @@ bool CXFA_WidgetAcc::CalculateWidgetAutoSize(CFX_SizeF& size) { if (paraData.HasValidNode()) size.width += paraData.GetMarginLeft() + paraData.GetTextIndent(); - float width = 0; - if (TryWidth(width)) { - size.width = width; + pdfium::Optional width = TryWidth(); + if (width) { + size.width = *width; } else { - float fMin = 0; - if (TryMinWidth(fMin)) - size.width = std::max(size.width, fMin); + pdfium::Optional min = TryMinWidth(); + if (min) + size.width = std::max(size.width, *min); - float fMax = 0; - if (TryMaxWidth(fMax) && fMax > 0) - size.width = std::min(size.width, fMax); + pdfium::Optional max = TryMaxWidth(); + if (max && *max > 0) + size.width = std::min(size.width, *max); } - float height = 0; - if (TryHeight(height)) { - size.height = height; + pdfium::Optional height = TryHeight(); + if (height) { + size.height = *height; } else { - float fMin = 0; - if (TryMinHeight(fMin)) - size.height = std::max(size.height, fMin); + pdfium::Optional min = TryMinHeight(); + if (min) + size.height = std::max(size.height, *min); - float fMax = 0; - if (TryMaxHeight(fMax) && fMax > 0) - size.height = std::min(size.height, fMax); + pdfium::Optional max = TryMaxHeight(); + if (max && *max > 0) + size.height = std::min(size.height, *max); } return true; } @@ -905,33 +905,48 @@ bool CXFA_WidgetAcc::CalculatePushButtonAutoSize(CFX_SizeF& size) { return CalculateWidgetAutoSize(size); } +CFX_SizeF CXFA_WidgetAcc::CalculateImageSize(float img_width, + float img_height, + float dpi_x, + float dpi_y) { + CFX_RectF rtImage(0, 0, XFA_UnitPx2Pt(img_width, dpi_x), + XFA_UnitPx2Pt(img_height, dpi_y)); + + CFX_RectF rtFit; + pdfium::Optional width = TryWidth(); + if (width) { + rtFit.width = *width; + GetWidthWithoutMargin(rtFit.width); + } else { + rtFit.width = rtImage.width; + } + + pdfium::Optional height = TryHeight(); + if (height) { + rtFit.height = *height; + GetHeightWithoutMargin(rtFit.height); + } else { + rtFit.height = rtImage.height; + } + + return rtFit.Size(); +} + bool CXFA_WidgetAcc::CalculateImageAutoSize(CFX_SizeF& size) { if (!GetImageImage()) LoadImageImage(); size.clear(); RetainPtr pBitmap = GetImageImage(); - if (pBitmap) { - int32_t iImageXDpi = 0; - int32_t iImageYDpi = 0; - GetImageDpi(iImageXDpi, iImageYDpi); - CFX_RectF rtImage( - 0, 0, XFA_UnitPx2Pt((float)pBitmap->GetWidth(), (float)iImageXDpi), - XFA_UnitPx2Pt((float)pBitmap->GetHeight(), (float)iImageYDpi)); - - CFX_RectF rtFit; - if (TryWidth(rtFit.width)) - GetWidthWithoutMargin(rtFit.width); - else - rtFit.width = rtImage.width; + if (!pBitmap) + return CalculateWidgetAutoSize(size); - if (TryHeight(rtFit.height)) - GetHeightWithoutMargin(rtFit.height); - else - rtFit.height = rtImage.height; + int32_t iImageXDpi = 0; + int32_t iImageYDpi = 0; + GetImageDpi(iImageXDpi, iImageYDpi); - size = rtFit.Size(); - } + size = CalculateImageSize(pBitmap->GetWidth(), pBitmap->GetHeight(), + iImageXDpi, iImageYDpi); return CalculateWidgetAutoSize(size); } @@ -941,28 +956,15 @@ bool CXFA_WidgetAcc::CalculateImageEditAutoSize(CFX_SizeF& size) { size.clear(); RetainPtr pBitmap = GetImageEditImage(); - if (pBitmap) { - int32_t iImageXDpi = 0; - int32_t iImageYDpi = 0; - GetImageEditDpi(iImageXDpi, iImageYDpi); - CFX_RectF rtImage( - 0, 0, XFA_UnitPx2Pt((float)pBitmap->GetWidth(), (float)iImageXDpi), - XFA_UnitPx2Pt((float)pBitmap->GetHeight(), (float)iImageYDpi)); - - CFX_RectF rtFit; - if (TryWidth(rtFit.width)) - GetWidthWithoutMargin(rtFit.width); - else - rtFit.width = rtImage.width; + if (!pBitmap) + return CalculateFieldAutoSize(size); - if (TryHeight(rtFit.height)) - GetHeightWithoutMargin(rtFit.height); - else - rtFit.height = rtImage.height; + int32_t iImageXDpi = 0; + int32_t iImageYDpi = 0; + GetImageEditDpi(iImageXDpi, iImageYDpi); - size.width = rtFit.width; - size.height = rtFit.height; - } + size = CalculateImageSize(pBitmap->GetWidth(), pBitmap->GetHeight(), + iImageXDpi, iImageYDpi); return CalculateFieldAutoSize(size); } @@ -1013,13 +1015,13 @@ float CXFA_WidgetAcc::CalculateWidgetAutoWidth(float fWidthCalc) { if (marginData.HasValidNode()) fWidthCalc += marginData.GetLeftInset() + marginData.GetRightInset(); - float fMin = 0; - if (TryMinWidth(fMin)) - fWidthCalc = std::max(fWidthCalc, fMin); + pdfium::Optional min = TryMinWidth(); + if (min) + fWidthCalc = std::max(fWidthCalc, *min); - float fMax = 0; - if (TryMaxWidth(fMax) && fMax > 0) - fWidthCalc = std::min(fWidthCalc, fMax); + pdfium::Optional max = TryMaxWidth(); + if (max && *max > 0) + fWidthCalc = std::min(fWidthCalc, *max); return fWidthCalc; } @@ -1036,13 +1038,13 @@ float CXFA_WidgetAcc::CalculateWidgetAutoHeight(float fHeightCalc) { if (marginData.HasValidNode()) fHeightCalc += marginData.GetTopInset() + marginData.GetBottomInset(); - float fMin = 0; - if (TryMinHeight(fMin)) - fHeightCalc = std::max(fHeightCalc, fMin); + pdfium::Optional min = TryMinHeight(); + if (min) + fHeightCalc = std::max(fHeightCalc, *min); - float fMax = 0; - if (TryMaxHeight(fMax) && fMax > 0) - fHeightCalc = std::min(fHeightCalc, fMax); + pdfium::Optional max = TryMaxHeight(); + if (max && *max > 0) + fHeightCalc = std::min(fHeightCalc, *max); return fHeightCalc; } @@ -1058,8 +1060,7 @@ void CXFA_WidgetAcc::StartWidgetLayout(float& fCalcWidth, float& fCalcHeight) { InitLayoutData(); XFA_Element eUIType = GetUIType(); if (eUIType == XFA_Element::Text) { - m_pLayoutData->m_fWidgetHeight = -1; - TryHeight(m_pLayoutData->m_fWidgetHeight); + m_pLayoutData->m_fWidgetHeight = TryHeight().value_or(-1); StartTextLayout(fCalcWidth, fCalcHeight); return; } @@ -1069,14 +1070,26 @@ void CXFA_WidgetAcc::StartWidgetLayout(float& fCalcWidth, float& fCalcHeight) { m_pLayoutData->m_fWidgetHeight = -1; float fWidth = 0; if (fCalcWidth > 0 && fCalcHeight < 0) { - if (!TryHeight(fCalcHeight)) + pdfium::Optional height = TryHeight(); + if (height) + fCalcHeight = *height; + else CalculateAccWidthAndHeight(eUIType, fCalcWidth, fCalcHeight); m_pLayoutData->m_fWidgetHeight = fCalcHeight; return; } if (fCalcWidth < 0 && fCalcHeight < 0) { - if (!TryWidth(fWidth) || !TryHeight(fCalcHeight)) + pdfium::Optional height; + pdfium::Optional width = TryWidth(); + if (width) { + fWidth = *width; + + height = TryHeight(); + if (height) + fCalcHeight = *height; + } + if (!width || !height) CalculateAccWidthAndHeight(eUIType, fWidth, fCalcHeight); fCalcWidth = fWidth; @@ -1205,8 +1218,9 @@ bool CXFA_WidgetAcc::FindSplitPos(int32_t iBlockIndex, float& fCalcHeight) { iLinesCount = 1; } else { if (!pFieldData->m_pTextOut) { - float fWidth = 0; - TryWidth(fWidth); + // TODO(dsinclair): Inline fWidth when the 2nd param of + // CalculateAccWidthAndHeight isn't a ref-param. + float fWidth = TryWidth().value_or(0); CalculateAccWidthAndHeight(eUIType, fWidth, fHeight); } iLinesCount = pFieldData->m_pTextOut->GetTotalLines(); @@ -1397,20 +1411,19 @@ void CXFA_WidgetAcc::StartTextLayout(float& fCalcWidth, float& fCalcHeight) { float fWidth = GetWidthWithoutMargin(fCalcWidth); pTextLayout->StartLayout(fWidth); } + if (fCalcWidth < 0 && fCalcHeight < 0) { - float fMaxWidth = -1; - bool bRet = TryWidth(fMaxWidth); - if (bRet) { - float fWidth = GetWidthWithoutMargin(fMaxWidth); - pTextLayout->StartLayout(fWidth); + pdfium::Optional width = TryWidth(); + if (width) { + pTextLayout->StartLayout(GetWidthWithoutMargin(*width)); + fCalcWidth = *width; } else { - float fWidth = pTextLayout->StartLayout(fMaxWidth); - fMaxWidth = CalculateWidgetAutoWidth(fWidth); - fWidth = GetWidthWithoutMargin(fMaxWidth); - pTextLayout->StartLayout(fWidth); + float fMaxWidth = CalculateWidgetAutoWidth(pTextLayout->StartLayout(-1)); + pTextLayout->StartLayout(GetWidthWithoutMargin(fMaxWidth)); + fCalcWidth = fMaxWidth; } - fCalcWidth = fMaxWidth; } + if (m_pLayoutData->m_fWidgetHeight < 0) { m_pLayoutData->m_fWidgetHeight = pTextLayout->GetLayoutHeight(); m_pLayoutData->m_fWidgetHeight = diff --git a/xfa/fxfa/cxfa_widgetacc.h b/xfa/fxfa/cxfa_widgetacc.h index 6cb2e7f01d..e12954c19e 100644 --- a/xfa/fxfa/cxfa_widgetacc.h +++ b/xfa/fxfa/cxfa_widgetacc.h @@ -103,6 +103,10 @@ class CXFA_WidgetAcc : public CXFA_WidgetData { bool CalculateTextEditAutoSize(CFX_SizeF& size); bool CalculateCheckButtonAutoSize(CFX_SizeF& size); bool CalculatePushButtonAutoSize(CFX_SizeF& size); + CFX_SizeF CalculateImageSize(float img_width, + float img_height, + float dpi_x, + float dpi_y); bool CalculateImageEditAutoSize(CFX_SizeF& size); bool CalculateImageAutoSize(CFX_SizeF& size); bool CalculateTextAutoSize(CFX_SizeF& size); diff --git a/xfa/fxfa/parser/cxfa_widgetdata.cpp b/xfa/fxfa/parser/cxfa_widgetdata.cpp index 72c2d7fd34..3938884d0d 100644 --- a/xfa/fxfa/parser/cxfa_widgetdata.cpp +++ b/xfa/fxfa/parser/cxfa_widgetdata.cpp @@ -327,28 +327,46 @@ CXFA_BindData CXFA_WidgetData::GetBindData() { m_pNode->JSNode()->GetProperty(0, XFA_Element::Bind, false)); } -bool CXFA_WidgetData::TryWidth(float& fWidth) { - return TryMeasure(XFA_Attribute::W, fWidth); +pdfium::Optional CXFA_WidgetData::TryWidth() { + float width = 0; + if (TryMeasure(XFA_Attribute::W, width)) + return {width}; + return {}; } -bool CXFA_WidgetData::TryHeight(float& fHeight) { - return TryMeasure(XFA_Attribute::H, fHeight); +pdfium::Optional CXFA_WidgetData::TryHeight() { + float height = 0; + if (TryMeasure(XFA_Attribute::H, height)) + return {height}; + return {}; } -bool CXFA_WidgetData::TryMinWidth(float& fMinWidth) { - return TryMeasure(XFA_Attribute::MinW, fMinWidth); +pdfium::Optional CXFA_WidgetData::TryMinWidth() { + float minWidth = 0; + if (TryMeasure(XFA_Attribute::MinW, minWidth)) + return {minWidth}; + return {}; } -bool CXFA_WidgetData::TryMinHeight(float& fMinHeight) { - return TryMeasure(XFA_Attribute::MinH, fMinHeight); +pdfium::Optional CXFA_WidgetData::TryMinHeight() { + float minHeight = 0; + if (TryMeasure(XFA_Attribute::MinH, minHeight)) + return {minHeight}; + return {}; } -bool CXFA_WidgetData::TryMaxWidth(float& fMaxWidth) { - return TryMeasure(XFA_Attribute::MaxW, fMaxWidth); +pdfium::Optional CXFA_WidgetData::TryMaxWidth() { + float maxWidth = 0; + if (TryMeasure(XFA_Attribute::MaxW, maxWidth)) + return {maxWidth}; + return {}; } -bool CXFA_WidgetData::TryMaxHeight(float& fMaxHeight) { - return TryMeasure(XFA_Attribute::MaxH, fMaxHeight); +pdfium::Optional CXFA_WidgetData::TryMaxHeight() { + float maxHeight = 0; + if (TryMeasure(XFA_Attribute::MaxH, maxHeight)) + return {maxHeight}; + return {}; } CXFA_BorderData CXFA_WidgetData::GetUIBorderData() { diff --git a/xfa/fxfa/parser/cxfa_widgetdata.h b/xfa/fxfa/parser/cxfa_widgetdata.h index 9d955a3d99..309f00fe12 100644 --- a/xfa/fxfa/parser/cxfa_widgetdata.h +++ b/xfa/fxfa/parser/cxfa_widgetdata.h @@ -61,12 +61,12 @@ class CXFA_WidgetData : public CXFA_DataData { CXFA_CalculateData GetCalculateData(); CXFA_ValidateData GetValidateData(bool bModified); - bool TryWidth(float& fWidth); - bool TryHeight(float& fHeight); - bool TryMinWidth(float& fMinWidth); - bool TryMinHeight(float& fMinHeight); - bool TryMaxWidth(float& fMaxWidth); - bool TryMaxHeight(float& fMaxHeight); + pdfium::Optional TryWidth(); + pdfium::Optional TryHeight(); + pdfium::Optional TryMinWidth(); + pdfium::Optional TryMinHeight(); + pdfium::Optional TryMaxWidth(); + pdfium::Optional TryMaxHeight(); CXFA_BorderData GetUIBorderData(); CFX_RectF GetUIMargin(); -- cgit v1.2.3