Break off parts of CXFA_LayoutPageMgr::FinishPaginatedPageSets().

Move them into helper functions. Add const versions of
CXFA_LayoutItem::As{AsContainer,AsContent}LayoutItem() so the new helper
functions can have const pointers.

Change-Id: I776b4151bdf3a8624996ca3bc62c2da7958c0a5e
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/49413
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/xfa/fxfa/layout/cxfa_layoutitem.cpp b/xfa/fxfa/layout/cxfa_layoutitem.cpp
index 927d83b..8711e1a 100644
--- a/xfa/fxfa/layout/cxfa_layoutitem.cpp
+++ b/xfa/fxfa/layout/cxfa_layoutitem.cpp
@@ -44,11 +44,23 @@
                                  : nullptr;
 }
 
+const CXFA_ContainerLayoutItem* CXFA_LayoutItem::AsContainerLayoutItem() const {
+  return IsContainerLayoutItem()
+             ? static_cast<const CXFA_ContainerLayoutItem*>(this)
+             : nullptr;
+}
+
 CXFA_ContentLayoutItem* CXFA_LayoutItem::AsContentLayoutItem() {
   return IsContentLayoutItem() ? static_cast<CXFA_ContentLayoutItem*>(this)
                                : nullptr;
 }
 
+const CXFA_ContentLayoutItem* CXFA_LayoutItem::AsContentLayoutItem() const {
+  return IsContentLayoutItem()
+             ? static_cast<const CXFA_ContentLayoutItem*>(this)
+             : nullptr;
+}
+
 CXFA_ContainerLayoutItem* CXFA_LayoutItem::GetPage() const {
   for (CXFA_LayoutItem* pCurNode = const_cast<CXFA_LayoutItem*>(this); pCurNode;
        pCurNode = pCurNode->m_pParent) {
diff --git a/xfa/fxfa/layout/cxfa_layoutitem.h b/xfa/fxfa/layout/cxfa_layoutitem.h
index 9d00526..46b9b04 100644
--- a/xfa/fxfa/layout/cxfa_layoutitem.h
+++ b/xfa/fxfa/layout/cxfa_layoutitem.h
@@ -21,7 +21,9 @@
   bool IsContainerLayoutItem() const { return m_ItemType == kContainerItem; }
   bool IsContentLayoutItem() const { return m_ItemType == kContentItem; }
   CXFA_ContainerLayoutItem* AsContainerLayoutItem();
+  const CXFA_ContainerLayoutItem* AsContainerLayoutItem() const;
   CXFA_ContentLayoutItem* AsContentLayoutItem();
+  const CXFA_ContentLayoutItem* AsContentLayoutItem() const;
 
   CXFA_ContainerLayoutItem* GetPage() const;
   CXFA_LayoutItem* GetParent() const { return m_pParent; }
diff --git a/xfa/fxfa/layout/cxfa_layoutpagemgr.cpp b/xfa/fxfa/layout/cxfa_layoutpagemgr.cpp
index 56cb8aa..79c889c 100644
--- a/xfa/fxfa/layout/cxfa_layoutpagemgr.cpp
+++ b/xfa/fxfa/layout/cxfa_layoutpagemgr.cpp
@@ -251,6 +251,27 @@
       pTestScript, pTestScript->GetContainerParent());
 }
 
+float CalculateLayoutItemHeight(const CXFA_LayoutItem* pItem) {
+  float fHeight = 0;
+  for (const CXFA_LayoutItem* pChild = pItem->GetFirstChild(); pChild;
+       pChild = pChild->GetNextSibling()) {
+    const CXFA_ContentLayoutItem* pContent = pChild->AsContentLayoutItem();
+    if (pContent)
+      fHeight += pContent->m_sSize.height;
+  }
+  return fHeight;
+}
+
+std::vector<float> GetHeightsForContentAreas(const CXFA_LayoutItem* pItem) {
+  std::vector<float> heights;
+  for (const CXFA_LayoutItem* pChild = pItem->GetFirstChild(); pChild;
+       pChild = pChild->GetNextSibling()) {
+    if (pChild->GetFormNode()->GetElementType() == XFA_Element::ContentArea)
+      heights.push_back(CalculateLayoutItemHeight(pChild));
+  }
+  return heights;
+}
+
 }  // namespace
 
 class CXFA_ContainerRecord {
@@ -669,28 +690,8 @@
             }
           }
           bool bUsable = true;
-          std::vector<float> rgUsedHeights;
-          for (CXFA_LayoutItem* pChildLayoutItem =
-                   pLastPageAreaLayoutItem->GetFirstChild();
-               pChildLayoutItem;
-               pChildLayoutItem = pChildLayoutItem->GetNextSibling()) {
-            if (pChildLayoutItem->GetFormNode()->GetElementType() !=
-                XFA_Element::ContentArea) {
-              continue;
-            }
-            float fUsedHeight = 0;
-            for (CXFA_LayoutItem* pContentChildLayoutItem =
-                     pChildLayoutItem->GetFirstChild();
-                 pContentChildLayoutItem;
-                 pContentChildLayoutItem =
-                     pContentChildLayoutItem->GetNextSibling()) {
-              if (CXFA_ContentLayoutItem* pContent =
-                      pContentChildLayoutItem->AsContentLayoutItem()) {
-                fUsedHeight += pContent->m_sSize.height;
-              }
-            }
-            rgUsedHeights.push_back(fUsedHeight);
-          }
+          std::vector<float> rgUsedHeights =
+              GetHeightsForContentAreas(pLastPageAreaLayoutItem);
           int32_t iCurContentAreaIndex = -1;
           for (CXFA_Node* pContentAreaNode = pNode->GetFirstChild();
                pContentAreaNode;