Split CXFA_TextLayout::DoLayout() into two.
There are two distinct patterns for calling DoLayout(). Copy DoLayout()
to DoSplitLayout(), and optimize the two layout methods for the two
types of callers. Do more cleanup along the way to make the code easier
to read.
Change-Id: I82abbbd6775eff66e7133a956e57041822c48cd9
Reviewed-on: https://pdfium-review.googlesource.com/c/49711
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/xfa/fxfa/cxfa_textlayout.cpp b/xfa/fxfa/cxfa_textlayout.cpp
index 786a8fe..951c72a 100644
--- a/xfa/fxfa/cxfa_textlayout.cpp
+++ b/xfa/fxfa/cxfa_textlayout.cpp
@@ -313,20 +313,25 @@
return szDef.width;
}
-float CXFA_TextLayout::DoLayout(int32_t iBlockIndex,
- float fCalcHeight,
- float fContentAreaHeight,
- float fTextHeight) {
+float CXFA_TextLayout::DoLayout(float fTextHeight) {
+ if (!m_pLoader)
+ return fTextHeight;
+
+ UpdateLoaderHeight(fTextHeight);
+ return fTextHeight;
+}
+
+float CXFA_TextLayout::DoSplitLayout(int32_t iBlockIndex,
+ float fCalcHeight,
+ float fTextHeight) {
ASSERT(iBlockIndex >= 0);
if (!m_pLoader)
return fCalcHeight;
- m_pLoader->fHeight = fTextHeight;
- if (m_pLoader->fHeight < 0)
- m_pLoader->fHeight = GetLayoutHeight();
+ UpdateLoaderHeight(fTextHeight);
- if (fContentAreaHeight < 0)
+ if (fCalcHeight < 0)
return fCalcHeight;
m_bHasBlock = true;
@@ -346,34 +351,31 @@
float fLinePos = m_pLoader->fStartLineOffset;
int32_t iLineIndex = 0;
if (iBlockCount > 0) {
- if (iBlockCount >= iBlockIndex + 1) {
+ if (iBlockIndex < iBlockCount)
iLineIndex = m_Blocks[iBlockIndex].iIndex;
- } else {
- iLineIndex = GetNextIndexForLastBlockData();
- }
+ else
+ iLineIndex = GetNextIndexFromLastBlockData();
if (!m_pLoader->blockHeights.empty()) {
for (int32_t i = 0; i < iBlockIndex; i++)
fLinePos -= m_pLoader->blockHeights[i].fHeight;
}
}
- int32_t iCount = pdfium::CollectionSize<int32_t>(m_pLoader->lineHeights);
- if (iLineIndex < 0 || iLineIndex >= iCount)
+ int32_t iLineCount = pdfium::CollectionSize<int32_t>(m_pLoader->lineHeights);
+ if (iLineIndex < 0 || iLineIndex >= iLineCount)
return fCalcHeight;
- if (m_pLoader->lineHeights[iLineIndex] - fContentAreaHeight >
- kHeightTolerance) {
+ if (m_pLoader->lineHeights[iLineIndex] - fCalcHeight > kHeightTolerance)
return 0;
- }
- for (int32_t i = iLineIndex; i < iCount; ++i) {
+ for (int32_t i = iLineIndex; i < iLineCount; ++i) {
float fLineHeight = m_pLoader->lineHeights[i];
- if (fLinePos + fLineHeight - fContentAreaHeight <= kHeightTolerance) {
+ if (fLinePos + fLineHeight - fCalcHeight <= kHeightTolerance) {
fLinePos += fLineHeight;
continue;
}
- if (iBlockCount >= iBlockIndex + 1)
+ if (iBlockIndex < iBlockCount)
m_Blocks[iBlockIndex] = {iLineIndex, i - iLineIndex};
else
m_Blocks.push_back({iLineIndex, i - iLineIndex});
@@ -401,10 +403,16 @@
return iCount > 0 ? iCount : 1;
}
-int32_t CXFA_TextLayout::GetNextIndexForLastBlockData() const {
+int32_t CXFA_TextLayout::GetNextIndexFromLastBlockData() const {
return m_Blocks.back().iIndex + m_Blocks.back().iLength;
}
+void CXFA_TextLayout::UpdateLoaderHeight(float fTextHeight) {
+ m_pLoader->fHeight = fTextHeight;
+ if (m_pLoader->fHeight < 0)
+ m_pLoader->fHeight = GetLayoutHeight();
+}
+
CFX_SizeF CXFA_TextLayout::CalcSize(const CFX_SizeF& minSize,
const CFX_SizeF& maxSize) {
float width = maxSize.width;
@@ -550,7 +558,7 @@
} else {
fLinePos = 0;
}
- iLineIndex = GetNextIndexForLastBlockData();
+ iLineIndex = GetNextIndexFromLastBlockData();
}
int32_t i = 0;
diff --git a/xfa/fxfa/cxfa_textlayout.h b/xfa/fxfa/cxfa_textlayout.h
index 777ec1e..980d839 100644
--- a/xfa/fxfa/cxfa_textlayout.h
+++ b/xfa/fxfa/cxfa_textlayout.h
@@ -38,10 +38,10 @@
float GetLayoutHeight();
float StartLayout(float fWidth);
- float DoLayout(int32_t iBlockIndex,
- float fCalcHeight,
- float fContentAreaHeight,
- float fTextHeight);
+ float DoLayout(float fTextHeight);
+ float DoSplitLayout(int32_t iBlockIndex,
+ float fCalcHeight,
+ float fTextHeight);
float Layout(const CFX_SizeF& size);
CFX_SizeF CalcSize(const CFX_SizeF& minSize, const CFX_SizeF& maxSize);
@@ -116,7 +116,8 @@
void DoTabstops(CFX_CSSComputedStyle* pStyle, CXFA_PieceLine* pPieceLine);
bool Layout(int32_t iBlock);
int32_t CountBlocks() const;
- int GetNextIndexForLastBlockData() const;
+ int GetNextIndexFromLastBlockData() const;
+ void UpdateLoaderHeight(float fTextHeight);
bool m_bHasBlock = false;
bool m_bRichText = false;
diff --git a/xfa/fxfa/parser/cxfa_node.cpp b/xfa/fxfa/parser/cxfa_node.cpp
index df2abf1..c32fd48 100644
--- a/xfa/fxfa/parser/cxfa_node.cpp
+++ b/xfa/fxfa/parser/cxfa_node.cpp
@@ -3475,9 +3475,8 @@
}
CXFA_TextLayout* pTextLayout =
m_pLayoutData->AsTextLayoutData()->GetTextLayout();
- *pCalcHeight =
- pTextLayout->DoLayout(iBlockIndex, *pCalcHeight, *pCalcHeight,
- m_pLayoutData->m_fWidgetHeight - fTopInset);
+ *pCalcHeight = pTextLayout->DoSplitLayout(
+ iBlockIndex, *pCalcHeight, m_pLayoutData->m_fWidgetHeight - fTopInset);
if (*pCalcHeight != 0) {
if (iBlockIndex == 0)
*pCalcHeight += fTopInset;
@@ -3710,7 +3709,7 @@
pTextLayout->StartLayout(fWidth);
fTextHeight = *pCalcHeight;
fTextHeight = GetHeightWithoutMargin(fTextHeight);
- pTextLayout->DoLayout(0, fTextHeight, -1, fTextHeight);
+ pTextLayout->DoLayout(fTextHeight);
return;
}
if (*pCalcWidth > 0 && *pCalcHeight < 0) {
@@ -3735,7 +3734,7 @@
}
fTextHeight = m_pLayoutData->m_fWidgetHeight;
fTextHeight = GetHeightWithoutMargin(fTextHeight);
- pTextLayout->DoLayout(0, fTextHeight, -1, fTextHeight);
+ pTextLayout->DoLayout(fTextHeight);
*pCalcHeight = m_pLayoutData->m_fWidgetHeight;
}