Fix code in fpdf_flatten.cpp that does no make sense.

Fix some nits and simplify code as well.

Change-Id: I77c88f211b9ce32f38221d04c067f73818e0e970
Reviewed-on: https://pdfium-review.googlesource.com/6674
Reviewed-by: Nicolás Peña <npm@chromium.org>
Reviewed-by: dsinclair <dsinclair@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/fpdfsdk/fpdf_flatten.cpp b/fpdfsdk/fpdf_flatten.cpp
index 914008c..e530553 100644
--- a/fpdfsdk/fpdf_flatten.cpp
+++ b/fpdfsdk/fpdf_flatten.cpp
@@ -29,23 +29,19 @@
 
 namespace {
 
-bool IsValiableRect(CFX_FloatRect rect, CFX_FloatRect rcPage) {
-  if (rect.left - rect.right > 0.000001f || rect.bottom - rect.top > 0.000001f)
+bool IsValidRect(const CFX_FloatRect& rect, const CFX_FloatRect& rcPage) {
+  constexpr float kMinSize = 0.000001f;
+  if (rect.IsEmpty() || rect.Width() < kMinSize || rect.Height() < kMinSize)
     return false;
 
-  if (rect.left == 0.0f && rect.top == 0.0f && rect.right == 0.0f &&
-      rect.bottom == 0.0f)
-    return false;
+  if (rcPage.IsEmpty())
+    return true;
 
-  if (!rcPage.IsEmpty()) {
-    if (rect.left - rcPage.left < -10.000001f ||
-        rect.right - rcPage.right > 10.000001f ||
-        rect.top - rcPage.top > 10.000001f ||
-        rect.bottom - rcPage.bottom < -10.000001f)
-      return false;
-  }
-
-  return true;
+  constexpr float kMinBorderSize = 10.000001f;
+  return rect.left - rcPage.left >= -kMinBorderSize &&
+         rect.right - rcPage.right <= kMinBorderSize &&
+         rect.top - rcPage.top <= kMinBorderSize &&
+         rect.bottom - rcPage.bottom >= -kMinBorderSize;
 }
 
 void GetContentsRect(CPDF_Document* pDoc,
@@ -60,7 +56,7 @@
     rc.right = pPageObject->m_Right;
     rc.bottom = pPageObject->m_Bottom;
     rc.top = pPageObject->m_Top;
-    if (IsValiableRect(rc, pDict->GetRectFor("MediaBox")))
+    if (IsValidRect(rc, pDict->GetRectFor("MediaBox")))
       pRectArray->push_back(rc);
   }
 }
@@ -77,7 +73,7 @@
   else if (pStream->KeyExist("BBox"))
     rect = pStream->GetRectFor("BBox");
 
-  if (IsValiableRect(rect, pPageDic->GetRectFor("MediaBox")))
+  if (IsValidRect(rect, pPageDic->GetRectFor("MediaBox")))
     pRectArray->push_back(rect);
 
   pObjectArray->push_back(pStream);
@@ -96,9 +92,8 @@
   if (!pAnnots)
     return FLATTEN_NOTHINGTODO;
 
-  uint32_t dwSize = pAnnots->GetCount();
-  for (int i = 0; i < (int)dwSize; i++) {
-    CPDF_Dictionary* pAnnotDic = ToDictionary(pAnnots->GetDirectObjectAt(i));
+  for (const auto& pAnnot : *pAnnots) {
+    CPDF_Dictionary* pAnnotDic = ToDictionary(pAnnot->GetDirect());
     if (!pAnnotDic)
       continue;
 
@@ -110,15 +105,13 @@
     if (nAnnotFlag & ANNOTFLAG_HIDDEN)
       continue;
 
-    if (nUsage == FLAT_NORMALDISPLAY) {
-      if (nAnnotFlag & ANNOTFLAG_INVISIBLE)
-        continue;
-
+    bool bParseStream;
+    if (nUsage == FLAT_NORMALDISPLAY)
+      bParseStream = !(nAnnotFlag & ANNOTFLAG_INVISIBLE);
+    else
+      bParseStream = !!(nAnnotFlag & ANNOTFLAG_PRINT);
+    if (bParseStream)
       ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray);
-    } else {
-      if (nAnnotFlag & ANNOTFLAG_PRINT)
-        ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray);
-    }
   }
   return FLATTEN_SUCCESS;
 }
@@ -126,10 +119,10 @@
 float GetMinMaxValue(const std::vector<CFX_FloatRect>& array,
                      FPDF_TYPE type,
                      FPDF_VALUE value) {
-  size_t nRects = array.size();
-  if (nRects <= 0)
+  if (array.empty())
     return 0.0f;
 
+  size_t nRects = array.size();
   std::vector<float> pArray(nRects);
   switch (value) {
     case LEFT:
@@ -149,7 +142,7 @@
         pArray[i] = array[i].bottom;
       break;
     default:
-      // Not reachable.
+      NOTREACHED();
       return 0.0f;
   }
 
@@ -330,7 +323,6 @@
     pNewOXbjectDic->SetNewFor<CPDF_Name>("Type", "XObject");
     pNewOXbjectDic->SetNewFor<CPDF_Name>("Subtype", "Form");
     pNewOXbjectDic->SetNewFor<CPDF_Number>("FormType", 1);
-    pNewOXbjectDic->SetNewFor<CPDF_Name>("Name", "FRM");
     CFX_FloatRect rcBBox = pPageDict->GetRectFor("ArtBox");
     pNewOXbjectDic->SetRectFor("BBox", rcBBox);
   }
@@ -357,9 +349,8 @@
       if (!sAnnotState.IsEmpty()) {
         pAPStream = pAPDic->GetStreamFor(sAnnotState);
       } else {
-        auto it = pAPDic->begin();
-        if (it != pAPDic->end()) {
-          CPDF_Object* pFirstObj = it->second.get();
+        if (pAPDic->GetCount() > 0) {
+          CPDF_Object* pFirstObj = pAPDic->begin()->second.get();
           if (pFirstObj) {
             if (pFirstObj->IsReference())
               pFirstObj = pFirstObj->GetDirect();
@@ -409,17 +400,8 @@
     pAcc->LoadAllData();
     CFX_ByteString sStream(pAcc->GetData(), pAcc->GetSize());
     CFX_Matrix matrix = pAPDic->GetMatrixFor("Matrix");
-    if (matrix.IsIdentity()) {
-      matrix.a = 1.0f;
-      matrix.b = 0.0f;
-      matrix.c = 0.0f;
-      matrix.d = 1.0f;
-      matrix.e = 0.0f;
-      matrix.f = 0.0f;
-    }
-
-    CFX_ByteString sTemp;
     CFX_Matrix m = GetMatrix(rcAnnot, rcStream, matrix);
+    CFX_ByteString sTemp;
     sTemp.Format("q %f 0 0 %f %f %f cm /%s Do Q\n", m.a, m.d, m.e, m.f,
                  sFormName.c_str());
     sStream += sTemp;
diff --git a/fpdfsdk/javascript/Document.cpp b/fpdfsdk/javascript/Document.cpp
index 5d5c2b5..d5b075d 100644
--- a/fpdfsdk/javascript/Document.cpp
+++ b/fpdfsdk/javascript/Document.cpp
@@ -273,11 +273,6 @@
   return true;
 }
 
-// Maps a field object in PDF document to a JavaScript variable
-// comment:
-// note: the paremter cName, this is clue how to treat if the cName is not a
-// valiable filed name in this document
-
 bool Document::getField(CJS_Runtime* pRuntime,
                         const std::vector<CJS_Value>& params,
                         CJS_Value& vRet,